blob: ea839d811f64ee7db983a8cac57c627256510d42 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown01f96c02011-09-21 15:30:20 +100061/* pers_list is a list of registered personalities protected
62 * by pers_lock.
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
65 */
NeilBrown2604b702006-01-06 00:20:36 -080066static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_SPINLOCK(pers_lock);
68
Bernd Schubert90b08712008-05-23 13:04:38 -070069static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020070static struct workqueue_struct *md_wq;
71static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070072
NeilBrown746d3202013-04-24 11:42:41 +100073static int remove_and_add_spares(struct mddev *mddev,
74 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110075static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
Robert Becker1e509152009-12-14 12:49:58 +110078 * Default number of read corrections we'll attempt on an rdev
79 * before ejecting it from the array. We divide the read error
80 * count by 2 for every hour elapsed between read errors.
81 */
82#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
83/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
85 * is 1000 KB/sec, so the extra system load does not show up that much.
86 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070087 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * subsystem is idle. There is also an 'absolute maximum' reconstruction
89 * speed limit - in case reconstruction slows down your system despite
90 * idle IO detection.
91 *
92 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080093 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
95
96static int sysctl_speed_limit_min = 1000;
97static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +110098static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -080099{
100 return mddev->sync_speed_min ?
101 mddev->sync_speed_min : sysctl_speed_limit_min;
102}
103
NeilBrownfd01b882011-10-11 16:47:53 +1100104static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800105{
106 return mddev->sync_speed_max ?
107 mddev->sync_speed_max : sysctl_speed_limit_max;
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110static struct ctl_table_header *raid_table_header;
111
Joe Perches82592c32013-11-14 15:16:18 +1100112static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .procname = "speed_limit_min",
115 .data = &sysctl_speed_limit_min,
116 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700117 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800118 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 },
120 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .procname = "speed_limit_max",
122 .data = &sysctl_speed_limit_max,
123 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800125 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800127 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
Joe Perches82592c32013-11-14 15:16:18 +1100130static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .procname = "raid",
133 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700134 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .child = raid_table,
136 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800137 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Joe Perches82592c32013-11-14 15:16:18 +1100140static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .procname = "dev",
143 .maxlen = 0,
144 .mode = 0555,
145 .child = raid_dir_table,
146 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800147 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700150static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
NeilBrownf91de922005-11-08 21:39:36 -0800152static int start_readonly;
153
NeilBrowna167f662010-10-26 18:31:13 +1100154/* bio_clone_mddev
155 * like bio_clone, but with a local bio set
156 */
157
NeilBrowna167f662010-10-26 18:31:13 +1100158struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100159 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100160{
161 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100162
163 if (!mddev || !mddev->bio_set)
164 return bio_alloc(gfp_mask, nr_iovecs);
165
Kent Overstreet395c72a2012-09-06 15:34:55 -0700166 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100167 if (!b)
168 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100169 return b;
170}
171EXPORT_SYMBOL_GPL(bio_alloc_mddev);
172
173struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100174 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100175{
NeilBrowna167f662010-10-26 18:31:13 +1100176 if (!mddev || !mddev->bio_set)
177 return bio_clone(bio, gfp_mask);
178
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700179 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100180}
181EXPORT_SYMBOL_GPL(bio_clone_mddev);
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/*
NeilBrownd7603b72006-01-06 00:20:30 -0800184 * We have a system wide 'event count' that is incremented
185 * on any 'interesting' event, and readers of /proc/mdstat
186 * can use 'poll' or 'select' to find out when the event
187 * count increases.
188 *
189 * Events are:
190 * start array, stop array, error, add device, remove device,
191 * start build, activate spare
192 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800193static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800194static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100195void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800196{
197 atomic_inc(&md_event_count);
198 wake_up(&md_event_waiters);
199}
NeilBrown29269552006-03-27 01:18:10 -0800200EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800201
NeilBrownc331eb02006-05-30 21:27:13 -0700202/* Alternate version that can be called from interrupts
203 * when calling sysfs_notify isn't needed.
204 */
NeilBrownfd01b882011-10-11 16:47:53 +1100205static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700206{
207 atomic_inc(&md_event_count);
208 wake_up(&md_event_waiters);
209}
210
NeilBrownd7603b72006-01-06 00:20:30 -0800211/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * Enables to iterate over all existing md arrays
213 * all_mddevs_lock protects this list.
214 */
215static LIST_HEAD(all_mddevs);
216static DEFINE_SPINLOCK(all_mddevs_lock);
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*
219 * iterates through all used mddevs in the system.
220 * We take care to grab the all_mddevs_lock whenever navigating
221 * the list, and to always hold a refcount when unlocked.
222 * Any code which breaks out of this loop while own
223 * a reference to the current mddev and must mddev_put it.
224 */
NeilBrownfd01b882011-10-11 16:47:53 +1100225#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000227 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100228 _tmp = all_mddevs.next; \
229 _mddev = NULL;}); \
230 ({ if (_tmp != &all_mddevs) \
231 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100233 if (_mddev) mddev_put(_mddev); \
234 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
235 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100237 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 )
239
NeilBrown409c57f2009-03-31 14:39:39 +1100240/* Rather than calling directly into the personality make_request function,
241 * IO requests come here first so that we can check if the device is
242 * being suspended pending a reconfiguration.
243 * We hold a refcount over the call to ->make_request. By the time that
244 * call has finished, the bio has been linked into some internal structure
245 * and so is visible to ->quiesce(), so we don't need the refcount any more.
246 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200247static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
NeilBrown49077322010-03-25 16:20:56 +1100249 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100250 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500251 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100252
NeilBrown0ca69882011-01-14 09:14:33 +1100253 if (mddev == NULL || mddev->pers == NULL
254 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100255 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200256 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100257 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100258 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
259 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
260 return;
261 }
NeilBrown0ca69882011-01-14 09:14:33 +1100262 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100263 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200264 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100265 DEFINE_WAIT(__wait);
266 for (;;) {
267 prepare_to_wait(&mddev->sb_wait, &__wait,
268 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200269 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100270 break;
271 rcu_read_unlock();
272 schedule();
273 rcu_read_lock();
274 }
275 finish_wait(&mddev->sb_wait, &__wait);
276 }
277 atomic_inc(&mddev->active_io);
278 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100279
Chris Masone91ece52011-02-07 19:21:48 -0500280 /*
281 * save the sectors now since our bio can
282 * go away inside make_request
283 */
284 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200285 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100286
Gu Zheng18c0b222014-11-24 11:05:26 +0800287 generic_start_io_acct(rw, sectors, &mddev->gendisk->part0);
NeilBrown49077322010-03-25 16:20:56 +1100288
NeilBrown409c57f2009-03-31 14:39:39 +1100289 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
290 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
NeilBrown9e35b992010-04-06 14:23:02 +1000293/* mddev_suspend makes sure no new requests are submitted
294 * to the device, and that any requests that have been submitted
295 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100296 * Once mddev_detach() is called and completes, the module will be
297 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000298 */
NeilBrownfd01b882011-10-11 16:47:53 +1100299void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100300{
301 BUG_ON(mddev->suspended);
302 mddev->suspended = 1;
303 synchronize_rcu();
304 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
305 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500306
307 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100308}
NeilBrown390ee602010-06-01 19:37:27 +1000309EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100310
NeilBrownfd01b882011-10-11 16:47:53 +1100311void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100312{
313 mddev->suspended = 0;
314 wake_up(&mddev->sb_wait);
315 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500316
Jonathan Brassow47525e52012-05-22 13:55:29 +1000317 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500318 md_wakeup_thread(mddev->thread);
319 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100320}
NeilBrown390ee602010-06-01 19:37:27 +1000321EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100322
NeilBrownfd01b882011-10-11 16:47:53 +1100323int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000324{
NeilBrown5c675f82014-12-15 12:56:56 +1100325 struct md_personality *pers = mddev->pers;
326 int ret = 0;
327
328 rcu_read_lock();
329 if (mddev->suspended)
330 ret = 1;
331 else if (pers && pers->congested)
332 ret = pers->congested(mddev, bits);
333 rcu_read_unlock();
334 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000335}
NeilBrown5c675f82014-12-15 12:56:56 +1100336EXPORT_SYMBOL_GPL(mddev_congested);
337static int md_congested(void *data, int bits)
338{
339 struct mddev *mddev = data;
340 return mddev_congested(mddev, bits);
341}
NeilBrown3fa841d2009-09-23 18:10:29 +1000342
NeilBrown64590f42014-12-15 12:56:57 +1100343static int md_mergeable_bvec(struct request_queue *q,
344 struct bvec_merge_data *bvm,
345 struct bio_vec *biovec)
346{
347 struct mddev *mddev = q->queuedata;
348 int ret;
349 rcu_read_lock();
350 if (mddev->suspended) {
351 /* Must always allow one vec */
352 if (bvm->bi_size == 0)
353 ret = biovec->bv_len;
354 else
355 ret = 0;
356 } else {
357 struct md_personality *pers = mddev->pers;
358 if (pers && pers->mergeable_bvec)
359 ret = pers->mergeable_bvec(mddev, bvm, biovec);
360 else
361 ret = biovec->bv_len;
362 }
363 rcu_read_unlock();
364 return ret;
365}
NeilBrowna2826aa2009-12-14 12:49:49 +1100366/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200367 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100368 */
369
Tejun Heoe9c74692010-09-03 11:56:18 +0200370static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100371{
NeilBrown3cb03002011-10-11 16:45:26 +1100372 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100373 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100374
375 rdev_dec_pending(rdev, mddev);
376
377 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200378 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200379 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100380 }
381 bio_put(bio);
382}
383
NeilBrowna7a07e62010-12-09 16:04:25 +1100384static void md_submit_flush_data(struct work_struct *ws);
385
NeilBrowna035fc32010-12-09 16:17:51 +1100386static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100387{
NeilBrownfd01b882011-10-11 16:47:53 +1100388 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100389 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100390
NeilBrowna7a07e62010-12-09 16:04:25 +1100391 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
392 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100393 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100394 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100395 if (rdev->raid_disk >= 0 &&
396 !test_bit(Faulty, &rdev->flags)) {
397 /* Take two references, one is dropped
398 * when request finishes, one after
399 * we reclaim rcu_read_lock
400 */
401 struct bio *bi;
402 atomic_inc(&rdev->nr_pending);
403 atomic_inc(&rdev->nr_pending);
404 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000405 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200406 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100407 bi->bi_private = rdev;
408 bi->bi_bdev = rdev->bdev;
409 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200410 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100411 rcu_read_lock();
412 rdev_dec_pending(rdev, mddev);
413 }
414 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100415 if (atomic_dec_and_test(&mddev->flush_pending))
416 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100417}
418
Tejun Heoe9c74692010-09-03 11:56:18 +0200419static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100420{
NeilBrownfd01b882011-10-11 16:47:53 +1100421 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200422 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100423
Kent Overstreet4f024f32013-10-11 15:44:27 -0700424 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100425 /* an empty barrier - all done */
426 bio_endio(bio, 0);
427 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200428 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200429 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100430 }
NeilBrown2b74e122010-12-09 15:59:01 +1100431
432 mddev->flush_bio = NULL;
433 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100434}
435
NeilBrownfd01b882011-10-11 16:47:53 +1100436void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100437{
NeilBrown85572d72014-12-15 12:56:56 +1100438 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100439 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200440 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100441 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200442 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100443 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100444
NeilBrowna035fc32010-12-09 16:17:51 +1100445 INIT_WORK(&mddev->flush_work, submit_flushes);
446 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100447}
Tejun Heoe9c74692010-09-03 11:56:18 +0200448EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100449
NeilBrown74018dc2012-07-31 09:08:15 +0200450void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000451{
NeilBrown9cbb1752012-07-31 09:08:14 +0200452 struct mddev *mddev = cb->data;
453 md_wakeup_thread(mddev->thread);
454 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000455}
NeilBrown9cbb1752012-07-31 09:08:14 +0200456EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000457
NeilBrownfd01b882011-10-11 16:47:53 +1100458static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 atomic_inc(&mddev->active);
461 return mddev;
462}
463
Dan Williams5fd3a172009-03-04 00:57:25 -0700464static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100465
NeilBrownfd01b882011-10-11 16:47:53 +1100466static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
NeilBrowna167f662010-10-26 18:31:13 +1100468 struct bio_set *bs = NULL;
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
471 return;
NeilBrownd3374822009-01-09 08:31:10 +1100472 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100473 mddev->ctime == 0 && !mddev->hold_active) {
474 /* Array is not configured at all, and not held active,
475 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100476 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100477 bs = mddev->bio_set;
478 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100479 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200480 /* We did a probe so need to clean up. Call
481 * queue_work inside the spinlock so that
482 * flush_workqueue() after mddev_find will
483 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100484 */
485 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200486 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100487 } else
488 kfree(mddev);
489 }
490 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100491 if (bs)
492 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
NeilBrownfd01b882011-10-11 16:47:53 +1100495void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100496{
497 mutex_init(&mddev->open_mutex);
498 mutex_init(&mddev->reconfig_mutex);
499 mutex_init(&mddev->bitmap_info.mutex);
500 INIT_LIST_HEAD(&mddev->disks);
501 INIT_LIST_HEAD(&mddev->all_mddevs);
502 init_timer(&mddev->safemode_timer);
503 atomic_set(&mddev->active, 1);
504 atomic_set(&mddev->openers, 0);
505 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100506 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100507 atomic_set(&mddev->flush_pending, 0);
508 init_waitqueue_head(&mddev->sb_wait);
509 init_waitqueue_head(&mddev->recovery_wait);
510 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000511 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500512 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100513 mddev->resync_min = 0;
514 mddev->resync_max = MaxSector;
515 mddev->level = LEVEL_NONE;
516}
NeilBrown390ee602010-06-01 19:37:27 +1000517EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100518
NeilBrownf72ffdd2014-09-30 14:23:59 +1000519static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
NeilBrownfd01b882011-10-11 16:47:53 +1100521 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
NeilBrown8f5f02c2011-02-16 13:58:51 +1100523 if (unit && MAJOR(unit) != MD_MAJOR)
524 unit &= ~((1<<MdpMinorShift)-1);
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 retry:
527 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
NeilBrownefeb53c2009-01-09 08:31:10 +1100529 if (unit) {
530 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
531 if (mddev->unit == unit) {
532 mddev_get(mddev);
533 spin_unlock(&all_mddevs_lock);
534 kfree(new);
535 return mddev;
536 }
537
538 if (new) {
539 list_add(&new->all_mddevs, &all_mddevs);
540 spin_unlock(&all_mddevs_lock);
541 new->hold_active = UNTIL_IOCTL;
542 return new;
543 }
544 } else if (new) {
545 /* find an unused unit number */
546 static int next_minor = 512;
547 int start = next_minor;
548 int is_free = 0;
549 int dev = 0;
550 while (!is_free) {
551 dev = MKDEV(MD_MAJOR, next_minor);
552 next_minor++;
553 if (next_minor > MINORMASK)
554 next_minor = 0;
555 if (next_minor == start) {
556 /* Oh dear, all in use. */
557 spin_unlock(&all_mddevs_lock);
558 kfree(new);
559 return NULL;
560 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000561
NeilBrownefeb53c2009-01-09 08:31:10 +1100562 is_free = 1;
563 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
564 if (mddev->unit == dev) {
565 is_free = 0;
566 break;
567 }
568 }
569 new->unit = dev;
570 new->md_minor = MINOR(dev);
571 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 list_add(&new->all_mddevs, &all_mddevs);
573 spin_unlock(&all_mddevs_lock);
574 return new;
575 }
576 spin_unlock(&all_mddevs_lock);
577
NeilBrown9ffae0c2006-01-06 00:20:32 -0800578 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (!new)
580 return NULL;
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 new->unit = unit;
583 if (MAJOR(unit) == MD_MAJOR)
584 new->md_minor = MINOR(unit);
585 else
586 new->md_minor = MINOR(unit) >> MdpMinorShift;
587
NeilBrownfafd7fb2010-04-01 15:55:30 +1100588 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 goto retry;
591}
592
NeilBrownb6eb1272010-04-15 10:13:47 +1000593static struct attribute_group md_redundancy_group;
594
NeilBrown5c47daf2014-12-15 12:57:01 +1100595void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
NeilBrowna64c8762010-04-14 17:15:37 +1000597 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000598 /* These cannot be removed under reconfig_mutex as
599 * an access to the files will try to take reconfig_mutex
600 * while holding the file unremovable, which leads to
601 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000602 * So hold set sysfs_active while the remove in happeing,
603 * and anything else which might set ->to_remove or my
604 * otherwise change the sysfs namespace will fail with
605 * -EBUSY if sysfs_active is still set.
606 * We set sysfs_active under reconfig_mutex and elsewhere
607 * test it under the same mutex to ensure its correct value
608 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000609 */
NeilBrowna64c8762010-04-14 17:15:37 +1000610 struct attribute_group *to_remove = mddev->to_remove;
611 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000612 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000613 mutex_unlock(&mddev->reconfig_mutex);
614
NeilBrown00bcb4a2010-06-01 19:37:23 +1000615 if (mddev->kobj.sd) {
616 if (to_remove != &md_redundancy_group)
617 sysfs_remove_group(&mddev->kobj, to_remove);
618 if (mddev->pers == NULL ||
619 mddev->pers->sync_request == NULL) {
620 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
621 if (mddev->sysfs_action)
622 sysfs_put(mddev->sysfs_action);
623 mddev->sysfs_action = NULL;
624 }
NeilBrowna64c8762010-04-14 17:15:37 +1000625 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000626 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000627 } else
628 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Chris Dunlop751e67c2011-10-19 16:48:26 +1100630 /* As we've dropped the mutex we need a spinlock to
631 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000632 */
633 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700634 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000635 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
NeilBrown5c47daf2014-12-15 12:57:01 +1100637EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
NeilBrown1ca69c42012-10-11 13:37:33 +1100639static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
640{
641 struct md_rdev *rdev;
642
643 rdev_for_each_rcu(rdev, mddev)
644 if (rdev->desc_nr == nr)
645 return rdev;
646
647 return NULL;
648}
649
650static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
NeilBrown3cb03002011-10-11 16:45:26 +1100652 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
NeilBrowndafb20f2012-03-19 12:46:39 +1100654 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (rdev->bdev->bd_dev == dev)
656 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return NULL;
659}
660
NeilBrown1ca69c42012-10-11 13:37:33 +1100661static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
662{
663 struct md_rdev *rdev;
664
665 rdev_for_each_rcu(rdev, mddev)
666 if (rdev->bdev->bd_dev == dev)
667 return rdev;
668
669 return NULL;
670}
671
NeilBrown84fc4b52011-10-11 16:49:58 +1100672static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800673{
NeilBrown84fc4b52011-10-11 16:49:58 +1100674 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800675 list_for_each_entry(pers, &pers_list, list) {
676 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800677 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800678 if (strcmp(pers->name, clevel)==0)
679 return pers;
680 }
NeilBrown2604b702006-01-06 00:20:36 -0800681 return NULL;
682}
683
Andre Nollb73df2d2008-07-11 22:02:23 +1000684/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100685static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100687 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000688 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
NeilBrownf72ffdd2014-09-30 14:23:59 +1000691static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 rdev->sb_page = alloc_page(GFP_KERNEL);
694 if (!rdev->sb_page) {
695 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000696 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
699 return 0;
700}
701
NeilBrown545c8792012-05-22 13:54:30 +1000702void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800705 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 rdev->sb_loaded = 0;
707 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000708 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100709 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
NeilBrown2699b672011-07-28 11:31:47 +1000711 if (rdev->bb_page) {
712 put_page(rdev->bb_page);
713 rdev->bb_page = NULL;
714 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000715 kfree(rdev->badblocks.page);
716 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
NeilBrown545c8792012-05-22 13:54:30 +1000718EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
NeilBrown6712ecf2007-09-27 12:47:43 +0200720static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700721{
NeilBrown3cb03002011-10-11 16:45:26 +1100722 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100723 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700724
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700725 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
726 printk("md: super_written gets error=%d, uptodate=%d\n",
727 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
728 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800729 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700730 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700731
NeilBrowna9701a32005-11-08 21:39:34 -0800732 if (atomic_dec_and_test(&mddev->pending_writes))
733 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700734 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700735}
736
NeilBrownfd01b882011-10-11 16:47:53 +1100737void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700738 sector_t sector, int size, struct page *page)
739{
740 /* write first size bytes of page to sector of rdev
741 * Increment mddev->pending_writes before returning
742 * and decrement it on completion, waking up sb_wait
743 * if zero is reached.
744 * If an error occurred, call md_error
745 */
NeilBrowna167f662010-10-26 18:31:13 +1100746 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700747
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100748 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700749 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700750 bio_add_page(bio, page, size, 0);
751 bio->bi_private = rdev;
752 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800753
NeilBrown7bfa19f2005-06-21 17:17:28 -0700754 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000755 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800756}
757
NeilBrownfd01b882011-10-11 16:47:53 +1100758void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800759{
Tejun Heoe9c74692010-09-03 11:56:18 +0200760 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000761 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700762}
763
NeilBrown3cb03002011-10-11 16:45:26 +1100764int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100765 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
NeilBrowna167f662010-10-26 18:31:13 +1100767 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 int ret;
769
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100770 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
771 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100772 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700773 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000774 else if (rdev->mddev->reshape_position != MaxSector &&
775 (rdev->mddev->reshape_backwards ==
776 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700777 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100778 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700779 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700781 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
784 bio_put(bio);
785 return ret;
786}
NeilBrowna8745db2006-01-06 00:20:34 -0800787EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
NeilBrownf72ffdd2014-09-30 14:23:59 +1000789static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (rdev->sb_loaded)
794 return 0;
795
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100796 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 goto fail;
798 rdev->sb_loaded = 1;
799 return 0;
800
801fail:
802 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
803 bdevname(rdev->bdev,b));
804 return -EINVAL;
805}
806
807static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
808{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000809 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000810 sb1->set_uuid1 == sb2->set_uuid1 &&
811 sb1->set_uuid2 == sb2->set_uuid2 &&
812 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
816{
817 int ret;
818 mdp_super_t *tmp1, *tmp2;
819
820 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
821 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
822
823 if (!tmp1 || !tmp2) {
824 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100825 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 goto abort;
827 }
828
829 *tmp1 = *sb1;
830 *tmp2 = *sb2;
831
832 /*
833 * nr_disks is not constant
834 */
835 tmp1->nr_disks = 0;
836 tmp2->nr_disks = 0;
837
Andre Nollce0c8e02008-07-11 22:02:20 +1000838 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700840 kfree(tmp1);
841 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return ret;
843}
844
NeilBrown4d167f02007-05-09 02:35:37 -0700845static u32 md_csum_fold(u32 csum)
846{
847 csum = (csum & 0xffff) + (csum >> 16);
848 return (csum & 0xffff) + (csum >> 16);
849}
850
NeilBrownf72ffdd2014-09-30 14:23:59 +1000851static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
NeilBrown4d167f02007-05-09 02:35:37 -0700853 u64 newcsum = 0;
854 u32 *sb32 = (u32*)sb;
855 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 unsigned int disk_csum, csum;
857
858 disk_csum = sb->sb_csum;
859 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700860
861 for (i = 0; i < MD_SB_BYTES/4 ; i++)
862 newcsum += sb32[i];
863 csum = (newcsum & 0xffffffff) + (newcsum>>32);
864
NeilBrown4d167f02007-05-09 02:35:37 -0700865#ifdef CONFIG_ALPHA
866 /* This used to use csum_partial, which was wrong for several
867 * reasons including that different results are returned on
868 * different architectures. It isn't critical that we get exactly
869 * the same return value as before (we always csum_fold before
870 * testing, and that removes any differences). However as we
871 * know that csum_partial always returned a 16bit value on
872 * alphas, do a fold to maximise conformity to previous behaviour.
873 */
874 sb->sb_csum = md_csum_fold(disk_csum);
875#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700877#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return csum;
879}
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881/*
882 * Handle superblock details.
883 * We want to be able to handle multiple superblock formats
884 * so we have a common interface to them all, and an array of
885 * different handlers.
886 * We rely on user-space to write the initial superblock, and support
887 * reading and updating of superblocks.
888 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100889 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 * loads and validates a superblock on dev.
891 * if refdev != NULL, compare superblocks on both devices
892 * Return:
893 * 0 - dev has a superblock that is compatible with refdev
894 * 1 - dev has a superblock that is compatible and newer than refdev
895 * so dev should be used as the refdev in future
896 * -EINVAL superblock incompatible or invalid
897 * -othererror e.g. -EIO
898 *
NeilBrownfd01b882011-10-11 16:47:53 +1100899 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 * Verify that dev is acceptable into mddev.
901 * The first time, mddev->raid_disks will be 0, and data from
902 * dev should be merged in. Subsequent calls check that dev
903 * is new enough. Return 0 or -EINVAL
904 *
NeilBrownfd01b882011-10-11 16:47:53 +1100905 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 * Update the superblock for rdev with data in mddev
907 * This does not write to disc.
908 *
909 */
910
911struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000912 char *name;
913 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000914 int (*load_super)(struct md_rdev *rdev,
915 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000916 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000917 int (*validate_super)(struct mddev *mddev,
918 struct md_rdev *rdev);
919 void (*sync_super)(struct mddev *mddev,
920 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100921 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000922 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000923 int (*allow_new_offset)(struct md_rdev *rdev,
924 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925};
926
927/*
Andre Noll0894cc32009-06-18 08:49:23 +1000928 * Check that the given mddev has no bitmap.
929 *
930 * This function is called from the run method of all personalities that do not
931 * support bitmaps. It prints an error message and returns non-zero if mddev
932 * has a bitmap. Otherwise, it returns 0.
933 *
934 */
NeilBrownfd01b882011-10-11 16:47:53 +1100935int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000936{
NeilBrownc3d97142009-12-14 12:49:52 +1100937 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000938 return 0;
939 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
940 mdname(mddev), mddev->pers->name);
941 return 1;
942}
943EXPORT_SYMBOL(md_check_no_bitmap);
944
945/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000946 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 */
NeilBrown3cb03002011-10-11 16:45:26 +1100948static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
951 mdp_super_t *sb;
952 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 /*
Andre Noll0f420352008-07-11 22:02:23 +1000955 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 * it's at the end of the disk.
957 *
958 * It also happens to be a multiple of 4Kb.
959 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100960 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
NeilBrown0002b272005-09-09 16:23:53 -0700962 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (ret) return ret;
964
965 ret = -EINVAL;
966
967 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000968 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 if (sb->md_magic != MD_SB_MAGIC) {
971 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
972 b);
973 goto abort;
974 }
975
976 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800977 sb->minor_version < 90 ||
978 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
980 sb->major_version, sb->minor_version,
981 b);
982 goto abort;
983 }
984
985 if (sb->raid_disks <= 0)
986 goto abort;
987
NeilBrown4d167f02007-05-09 02:35:37 -0700988 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
990 b);
991 goto abort;
992 }
993
994 rdev->preferred_minor = sb->md_minor;
995 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +1000996 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700997 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +1000998 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 if (sb->level == LEVEL_MULTIPATH)
1001 rdev->desc_nr = -1;
1002 else
1003 rdev->desc_nr = sb->this_disk.number;
1004
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001005 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001007 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001009 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (!uuid_equal(refsb, sb)) {
1011 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1012 b, bdevname(refdev->bdev,b2));
1013 goto abort;
1014 }
1015 if (!sb_equal(refsb, sb)) {
1016 printk(KERN_WARNING "md: %s has same UUID"
1017 " but different superblock to %s\n",
1018 b, bdevname(refdev->bdev, b2));
1019 goto abort;
1020 }
1021 ev1 = md_event(sb);
1022 ev2 = md_event(refsb);
1023 if (ev1 > ev2)
1024 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001025 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 ret = 0;
1027 }
NeilBrown8190e752009-06-18 08:48:58 +10001028 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001029 /* Limit to 4TB as metadata cannot record more than that.
1030 * (not needed for Linear and RAID0 as metadata doesn't
1031 * record this size)
1032 */
1033 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001034 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
NeilBrown27a7b262011-09-10 17:21:28 +10001036 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001037 /* "this cannot possibly happen" ... */
1038 ret = -EINVAL;
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 abort:
1041 return ret;
1042}
1043
1044/*
1045 * validate_super for 0.90.0
1046 */
NeilBrownfd01b882011-10-11 16:47:53 +11001047static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001050 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001051 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
NeilBrown41158c72005-06-21 17:17:25 -07001053 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001054 clear_bit(Faulty, &rdev->flags);
1055 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001056 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001057 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (mddev->raid_disks == 0) {
1060 mddev->major_version = 0;
1061 mddev->minor_version = sb->minor_version;
1062 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001063 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001064 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 mddev->ctime = sb->ctime;
1066 mddev->utime = sb->utime;
1067 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001068 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 mddev->layout = sb->layout;
1070 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001071 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001072 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001073 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001074 mddev->bitmap_info.space = 0;
1075 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001076 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001077 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001078 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
NeilBrownf6705572006-03-27 01:18:11 -08001080 if (mddev->minor_version >= 91) {
1081 mddev->reshape_position = sb->reshape_position;
1082 mddev->delta_disks = sb->delta_disks;
1083 mddev->new_level = sb->new_level;
1084 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001085 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001086 if (mddev->delta_disks < 0)
1087 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001088 } else {
1089 mddev->reshape_position = MaxSector;
1090 mddev->delta_disks = 0;
1091 mddev->new_level = mddev->level;
1092 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001093 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001094 }
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (sb->state & (1<<MD_SB_CLEAN))
1097 mddev->recovery_cp = MaxSector;
1098 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001099 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 sb->events_lo == sb->cp_events_lo) {
1101 mddev->recovery_cp = sb->recovery_cp;
1102 } else
1103 mddev->recovery_cp = 0;
1104 }
1105
1106 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1107 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1108 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1109 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1110
1111 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001112
1113 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001114 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001115 mddev->bitmap_info.offset =
1116 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001117 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001118 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001119 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001120
NeilBrown41158c72005-06-21 17:17:25 -07001121 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001122 /* Insist on good event counter while assembling, except
1123 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001125 if (sb->disks[rdev->desc_nr].state & (
1126 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001127 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001128 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001129 } else if (mddev->bitmap) {
1130 /* if adding to array with a bitmap, then we can accept an
1131 * older device ... but not too old.
1132 */
NeilBrown41158c72005-06-21 17:17:25 -07001133 if (ev1 < mddev->bitmap->events_cleared)
1134 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001135 if (ev1 < mddev->events)
1136 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001137 } else {
1138 if (ev1 < mddev->events)
1139 /* just a hot-add of a new device, leave raid_disk at -1 */
1140 return 0;
1141 }
NeilBrown41158c72005-06-21 17:17:25 -07001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 desc = sb->disks + rdev->desc_nr;
1145
1146 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001147 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001148 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1149 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001150 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001152 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001153 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1154 /* active but not in sync implies recovery up to
1155 * reshape position. We don't know exactly where
1156 * that is, so set to zero for now */
1157 if (mddev->minor_version >= 91) {
1158 rdev->recovery_offset = 0;
1159 rdev->raid_disk = desc->raid_disk;
1160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001162 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1163 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001164 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001165 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return 0;
1167}
1168
1169/*
1170 * sync_super for 0.90.0
1171 */
NeilBrownfd01b882011-10-11 16:47:53 +11001172static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001175 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /* make rdev->sb match mddev data..
1179 *
1180 * 1/ zero out disks
1181 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1182 * 3/ any empty disks < next_spare become removed
1183 *
1184 * disks[0] gets initialised to REMOVED because
1185 * we cannot be sure from other fields if it has
1186 * been initialised or not.
1187 */
1188 int i;
1189 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1190
NeilBrown61181562005-09-09 16:24:02 -07001191 rdev->sb_size = MD_SB_BYTES;
1192
Namhyung Kim65a06f062011-07-27 11:00:36 +10001193 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 memset(sb, 0, sizeof(*sb));
1196
1197 sb->md_magic = MD_SB_MAGIC;
1198 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 sb->patch_version = mddev->patch_version;
1200 sb->gvalid_words = 0; /* ignored */
1201 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1202 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1203 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1204 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1205
1206 sb->ctime = mddev->ctime;
1207 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001208 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 sb->raid_disks = mddev->raid_disks;
1210 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001211 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 sb->utime = mddev->utime;
1213 sb->state = 0;
1214 sb->events_hi = (mddev->events>>32);
1215 sb->events_lo = (u32)mddev->events;
1216
NeilBrownf6705572006-03-27 01:18:11 -08001217 if (mddev->reshape_position == MaxSector)
1218 sb->minor_version = 90;
1219 else {
1220 sb->minor_version = 91;
1221 sb->reshape_position = mddev->reshape_position;
1222 sb->new_level = mddev->new_level;
1223 sb->delta_disks = mddev->delta_disks;
1224 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001225 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001226 }
1227 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (mddev->in_sync)
1229 {
1230 sb->recovery_cp = mddev->recovery_cp;
1231 sb->cp_events_hi = (mddev->events>>32);
1232 sb->cp_events_lo = (u32)mddev->events;
1233 if (mddev->recovery_cp == MaxSector)
1234 sb->state = (1<< MD_SB_CLEAN);
1235 } else
1236 sb->recovery_cp = 0;
1237
1238 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001239 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
NeilBrownc3d97142009-12-14 12:49:52 +11001241 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001242 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001245 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001247 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001248 int is_active = test_bit(In_sync, &rdev2->flags);
1249
1250 if (rdev2->raid_disk >= 0 &&
1251 sb->minor_version >= 91)
1252 /* we have nowhere to store the recovery_offset,
1253 * but if it is not below the reshape_position,
1254 * we can piggy-back on that.
1255 */
1256 is_active = 1;
1257 if (rdev2->raid_disk < 0 ||
1258 test_bit(Faulty, &rdev2->flags))
1259 is_active = 0;
1260 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001261 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001263 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001264 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 d = &sb->disks[rdev2->desc_nr];
1266 nr_disks++;
1267 d->number = rdev2->desc_nr;
1268 d->major = MAJOR(rdev2->bdev->bd_dev);
1269 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001270 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 d->raid_disk = rdev2->raid_disk;
1272 else
1273 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001274 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001276 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001278 if (test_bit(In_sync, &rdev2->flags))
1279 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 active++;
1281 working++;
1282 } else {
1283 d->state = 0;
1284 spare++;
1285 working++;
1286 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001287 if (test_bit(WriteMostly, &rdev2->flags))
1288 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 /* now set the "removed" and "faulty" bits on any missing devices */
1291 for (i=0 ; i < mddev->raid_disks ; i++) {
1292 mdp_disk_t *d = &sb->disks[i];
1293 if (d->state == 0 && d->number == 0) {
1294 d->number = i;
1295 d->raid_disk = i;
1296 d->state = (1<<MD_DISK_REMOVED);
1297 d->state |= (1<<MD_DISK_FAULTY);
1298 failed++;
1299 }
1300 }
1301 sb->nr_disks = nr_disks;
1302 sb->active_disks = active;
1303 sb->working_disks = working;
1304 sb->failed_disks = failed;
1305 sb->spare_disks = spare;
1306
1307 sb->this_disk = sb->disks[rdev->desc_nr];
1308 sb->sb_csum = calc_sb_csum(sb);
1309}
1310
1311/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001312 * rdev_size_change for 0.90.0
1313 */
1314static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001315super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001316{
Andre Noll58c0fed2009-03-31 14:33:13 +11001317 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001318 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001319 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001320 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001321 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001322 if (!num_sectors || num_sectors > rdev->sb_start)
1323 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001324 /* Limit to 4TB as metadata cannot record more than that.
1325 * 4TB == 2^32 KB, or 2*2^32 sectors.
1326 */
NeilBrown667a5312012-08-16 16:46:12 +10001327 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001328 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001329 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001330 rdev->sb_page);
1331 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001332 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001333}
1334
NeilBrownc6563a82012-05-21 09:27:00 +10001335static int
1336super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1337{
1338 /* non-zero offset changes not possible with v0.90 */
1339 return new_offset == 0;
1340}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001341
1342/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 * version 1 superblock
1344 */
1345
NeilBrownf72ffdd2014-09-30 14:23:59 +10001346static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001348 __le32 disk_csum;
1349 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 unsigned long long newcsum;
1351 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001352 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 disk_csum = sb->sb_csum;
1355 sb->sb_csum = 0;
1356 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001357 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 newcsum += le32_to_cpu(*isuper++);
1359
1360 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001361 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1364 sb->sb_csum = disk_csum;
1365 return cpu_to_le32(csum);
1366}
1367
NeilBrown2699b672011-07-28 11:31:47 +10001368static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1369 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001370static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 struct mdp_superblock_1 *sb;
1373 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001374 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001375 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001377 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 /*
Andre Noll0f420352008-07-11 22:02:23 +10001380 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 * It is always aligned to a 4K boundary and
1382 * depeding on minor_version, it can be:
1383 * 0: At least 8K, but less than 12K, from end of device
1384 * 1: At start of device
1385 * 2: 4K from start of device.
1386 */
1387 switch(minor_version) {
1388 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001389 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001390 sb_start -= 8*2;
1391 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 break;
1393 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001394 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 break;
1396 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001397 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 break;
1399 default:
1400 return -EINVAL;
1401 }
Andre Noll0f420352008-07-11 22:02:23 +10001402 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
NeilBrown0002b272005-09-09 16:23:53 -07001404 /* superblock is rarely larger than 1K, but it can be larger,
1405 * and it is safe to read 4k, so we do that
1406 */
1407 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (ret) return ret;
1409
Namhyung Kim65a06f062011-07-27 11:00:36 +10001410 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1413 sb->major_version != cpu_to_le32(1) ||
1414 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001415 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001416 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return -EINVAL;
1418
1419 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1420 printk("md: invalid superblock checksum on %s\n",
1421 bdevname(rdev->bdev,b));
1422 return -EINVAL;
1423 }
1424 if (le64_to_cpu(sb->data_size) < 10) {
1425 printk("md: data_size too small on %s\n",
1426 bdevname(rdev->bdev,b));
1427 return -EINVAL;
1428 }
NeilBrownc6563a82012-05-21 09:27:00 +10001429 if (sb->pad0 ||
1430 sb->pad3[0] ||
1431 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1432 /* Some padding is non-zero, might be a new feature */
1433 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 rdev->preferred_minor = 0xffff;
1436 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001437 rdev->new_data_offset = rdev->data_offset;
1438 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1439 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1440 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001441 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
NeilBrown0002b272005-09-09 16:23:53 -07001443 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001444 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001445 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001446 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1447
1448 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001449 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001450 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001451 if (minor_version
1452 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1453 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001454
NeilBrown31b65a02006-07-10 04:44:14 -07001455 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1456 rdev->desc_nr = -1;
1457 else
1458 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1459
NeilBrown2699b672011-07-28 11:31:47 +10001460 if (!rdev->bb_page) {
1461 rdev->bb_page = alloc_page(GFP_KERNEL);
1462 if (!rdev->bb_page)
1463 return -ENOMEM;
1464 }
1465 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1466 rdev->badblocks.count == 0) {
1467 /* need to load the bad block list.
1468 * Currently we limit it to one page.
1469 */
1470 s32 offset;
1471 sector_t bb_sector;
1472 u64 *bbp;
1473 int i;
1474 int sectors = le16_to_cpu(sb->bblog_size);
1475 if (sectors > (PAGE_SIZE / 512))
1476 return -EINVAL;
1477 offset = le32_to_cpu(sb->bblog_offset);
1478 if (offset == 0)
1479 return -EINVAL;
1480 bb_sector = (long long)offset;
1481 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1482 rdev->bb_page, READ, true))
1483 return -EIO;
1484 bbp = (u64 *)page_address(rdev->bb_page);
1485 rdev->badblocks.shift = sb->bblog_shift;
1486 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1487 u64 bb = le64_to_cpu(*bbp);
1488 int count = bb & (0x3ff);
1489 u64 sector = bb >> 10;
1490 sector <<= sb->bblog_shift;
1491 count <<= sb->bblog_shift;
1492 if (bb + 1 == 0)
1493 break;
1494 if (md_set_badblocks(&rdev->badblocks,
1495 sector, count, 1) == 0)
1496 return -EINVAL;
1497 }
NeilBrown486adf72013-04-24 11:42:44 +10001498 } else if (sb->bblog_offset != 0)
1499 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001500
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001501 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001502 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001503 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001505 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1508 sb->level != refsb->level ||
1509 sb->layout != refsb->layout ||
1510 sb->chunksize != refsb->chunksize) {
1511 printk(KERN_WARNING "md: %s has strangely different"
1512 " superblock to %s\n",
1513 bdevname(rdev->bdev,b),
1514 bdevname(refdev->bdev,b2));
1515 return -EINVAL;
1516 }
1517 ev1 = le64_to_cpu(sb->events);
1518 ev2 = le64_to_cpu(refsb->events);
1519
1520 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001521 ret = 1;
1522 else
1523 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
NeilBrownc6563a82012-05-21 09:27:00 +10001525 if (minor_version) {
1526 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1527 sectors -= rdev->data_offset;
1528 } else
1529 sectors = rdev->sb_start;
1530 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001532 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001533 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
NeilBrownfd01b882011-10-11 16:47:53 +11001536static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001538 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001539 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
NeilBrown41158c72005-06-21 17:17:25 -07001541 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001542 clear_bit(Faulty, &rdev->flags);
1543 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001544 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001545 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (mddev->raid_disks == 0) {
1548 mddev->major_version = 1;
1549 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001550 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001551 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1553 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1554 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001555 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 mddev->layout = le32_to_cpu(sb->layout);
1557 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001558 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001559 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001560 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001561 mddev->bitmap_info.space = 0;
1562 /* Default location for bitmap is 1K after superblock
1563 * using 3K - total of 4K
1564 */
NeilBrownc3d97142009-12-14 12:49:52 +11001565 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001566 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001567 mddev->reshape_backwards = 0;
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1570 memcpy(mddev->uuid, sb->set_uuid, 16);
1571
1572 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001573
NeilBrown71c08052005-09-09 16:23:51 -07001574 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001575 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001576 mddev->bitmap_info.offset =
1577 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001578 /* Metadata doesn't record how much space is available.
1579 * For 1.0, we assume we can use up to the superblock
1580 * if before, else to 4K beyond superblock.
1581 * For others, assume no change is possible.
1582 */
1583 if (mddev->minor_version > 0)
1584 mddev->bitmap_info.space = 0;
1585 else if (mddev->bitmap_info.offset > 0)
1586 mddev->bitmap_info.space =
1587 8 - mddev->bitmap_info.offset;
1588 else
1589 mddev->bitmap_info.space =
1590 -mddev->bitmap_info.offset;
1591 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001592
NeilBrownf6705572006-03-27 01:18:11 -08001593 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1594 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1595 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1596 mddev->new_level = le32_to_cpu(sb->new_level);
1597 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001598 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001599 if (mddev->delta_disks < 0 ||
1600 (mddev->delta_disks == 0 &&
1601 (le32_to_cpu(sb->feature_map)
1602 & MD_FEATURE_RESHAPE_BACKWARDS)))
1603 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001604 } else {
1605 mddev->reshape_position = MaxSector;
1606 mddev->delta_disks = 0;
1607 mddev->new_level = mddev->level;
1608 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001609 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001610 }
1611
NeilBrown41158c72005-06-21 17:17:25 -07001612 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001613 /* Insist of good event counter while assembling, except for
1614 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001616 if (rdev->desc_nr >= 0 &&
1617 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1618 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1619 if (ev1 < mddev->events)
1620 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001621 } else if (mddev->bitmap) {
1622 /* If adding to array with a bitmap, then we can accept an
1623 * older device, but not too old.
1624 */
NeilBrown41158c72005-06-21 17:17:25 -07001625 if (ev1 < mddev->bitmap->events_cleared)
1626 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001627 if (ev1 < mddev->events)
1628 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001629 } else {
1630 if (ev1 < mddev->events)
1631 /* just a hot-add of a new device, leave raid_disk at -1 */
1632 return 0;
1633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (mddev->level != LEVEL_MULTIPATH) {
1635 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001636 if (rdev->desc_nr < 0 ||
1637 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1638 role = 0xffff;
1639 rdev->desc_nr = -1;
1640 } else
1641 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 switch(role) {
1643 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 break;
1645 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001646 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 break;
1648 default:
NeilBrownf4667222013-12-09 12:04:56 +11001649 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001650 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001651 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001652 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001653 if (!(le32_to_cpu(sb->feature_map) &
1654 MD_FEATURE_RECOVERY_BITMAP))
1655 rdev->saved_raid_disk = -1;
1656 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001657 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 rdev->raid_disk = role;
1659 break;
1660 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001661 if (sb->devflags & WriteMostly1)
1662 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001663 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1664 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001665 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001666 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return 0;
1669}
1670
NeilBrownfd01b882011-10-11 16:47:53 +11001671static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001674 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 int max_dev, i;
1676 /* make rdev->sb match mddev and rdev data. */
1677
Namhyung Kim65a06f062011-07-27 11:00:36 +10001678 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 sb->feature_map = 0;
1681 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001682 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 memset(sb->pad3, 0, sizeof(sb->pad3));
1684
1685 sb->utime = cpu_to_le64((__u64)mddev->utime);
1686 sb->events = cpu_to_le64(mddev->events);
1687 if (mddev->in_sync)
1688 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1689 else
1690 sb->resync_offset = cpu_to_le64(0);
1691
NeilBrown1c05b4b2006-10-21 10:24:08 -07001692 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001693
NeilBrownf0ca3402006-02-02 14:28:04 -08001694 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001695 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001696 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001697 sb->level = cpu_to_le32(mddev->level);
1698 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001699
NeilBrownaeb9b2112011-08-25 14:43:08 +10001700 if (test_bit(WriteMostly, &rdev->flags))
1701 sb->devflags |= WriteMostly1;
1702 else
1703 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001704 sb->data_offset = cpu_to_le64(rdev->data_offset);
1705 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001706
NeilBrownc3d97142009-12-14 12:49:52 +11001707 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1708 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001709 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001710 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001711
1712 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001713 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001714 sb->feature_map |=
1715 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1716 sb->recovery_offset =
1717 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001718 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1719 sb->feature_map |=
1720 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001721 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001722 if (test_bit(Replacement, &rdev->flags))
1723 sb->feature_map |=
1724 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001725
NeilBrownf6705572006-03-27 01:18:11 -08001726 if (mddev->reshape_position != MaxSector) {
1727 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1728 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1729 sb->new_layout = cpu_to_le32(mddev->new_layout);
1730 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1731 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001732 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001733 if (mddev->delta_disks == 0 &&
1734 mddev->reshape_backwards)
1735 sb->feature_map
1736 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001737 if (rdev->new_data_offset != rdev->data_offset) {
1738 sb->feature_map
1739 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1740 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1741 - rdev->data_offset));
1742 }
NeilBrownf6705572006-03-27 01:18:11 -08001743 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001744
NeilBrown2699b672011-07-28 11:31:47 +10001745 if (rdev->badblocks.count == 0)
1746 /* Nothing to do for bad blocks*/ ;
1747 else if (sb->bblog_offset == 0)
1748 /* Cannot record bad blocks on this device */
1749 md_error(mddev, rdev);
1750 else {
1751 struct badblocks *bb = &rdev->badblocks;
1752 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1753 u64 *p = bb->page;
1754 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1755 if (bb->changed) {
1756 unsigned seq;
1757
1758retry:
1759 seq = read_seqbegin(&bb->lock);
1760
1761 memset(bbp, 0xff, PAGE_SIZE);
1762
1763 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001764 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001765 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1766 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001767 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001768 }
NeilBrownd0962932012-03-19 12:46:41 +11001769 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001770 if (read_seqretry(&bb->lock, seq))
1771 goto retry;
1772
1773 bb->sector = (rdev->sb_start +
1774 (int)le32_to_cpu(sb->bblog_offset));
1775 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001776 }
1777 }
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001780 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (rdev2->desc_nr+1 > max_dev)
1782 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001783
NeilBrown70471da2009-08-03 10:59:57 +10001784 if (max_dev > le32_to_cpu(sb->max_dev)) {
1785 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001786 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001787 rdev->sb_size = max_dev * 2 + 256;
1788 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1789 if (rdev->sb_size & bmask)
1790 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001791 } else
1792 max_dev = le32_to_cpu(sb->max_dev);
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 for (i=0; i<max_dev;i++)
1795 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001796
NeilBrowndafb20f2012-03-19 12:46:39 +11001797 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001799 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001801 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001803 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001804 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 else
1806 sb->dev_roles[i] = cpu_to_le16(0xffff);
1807 }
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 sb->sb_csum = calc_sb_1_csum(sb);
1810}
1811
Chris Webb0cd17fe2008-06-28 08:31:46 +10001812static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001813super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001814{
1815 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001816 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001817 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001818 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001819 if (rdev->data_offset != rdev->new_data_offset)
1820 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001821 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001822 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001823 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001824 max_sectors -= rdev->data_offset;
1825 if (!num_sectors || num_sectors > max_sectors)
1826 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001827 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001828 /* minor version 0 with bitmap we can't move */
1829 return 0;
1830 } else {
1831 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001832 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001833 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001834 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001835 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001836 if (!num_sectors || num_sectors > max_sectors)
1837 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001838 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001839 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001840 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001841 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001842 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001843 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001844 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001845 rdev->sb_page);
1846 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001847 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001848
1849}
1850
1851static int
1852super_1_allow_new_offset(struct md_rdev *rdev,
1853 unsigned long long new_offset)
1854{
1855 /* All necessary checks on new >= old have been done */
1856 struct bitmap *bitmap;
1857 if (new_offset >= rdev->data_offset)
1858 return 1;
1859
1860 /* with 1.0 metadata, there is no metadata to tread on
1861 * so we can always move back */
1862 if (rdev->mddev->minor_version == 0)
1863 return 1;
1864
1865 /* otherwise we must be sure not to step on
1866 * any metadata, so stay:
1867 * 36K beyond start of superblock
1868 * beyond end of badblocks
1869 * beyond write-intent bitmap
1870 */
1871 if (rdev->sb_start + (32+4)*2 > new_offset)
1872 return 0;
1873 bitmap = rdev->mddev->bitmap;
1874 if (bitmap && !rdev->mddev->bitmap_info.file &&
1875 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001876 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001877 return 0;
1878 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1879 return 0;
1880
1881 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001882}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Adrian Bunk75c96f82005-05-05 16:16:09 -07001884static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 [0] = {
1886 .name = "0.90.0",
1887 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001888 .load_super = super_90_load,
1889 .validate_super = super_90_validate,
1890 .sync_super = super_90_sync,
1891 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001892 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 },
1894 [1] = {
1895 .name = "md-1",
1896 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001897 .load_super = super_1_load,
1898 .validate_super = super_1_validate,
1899 .sync_super = super_1_sync,
1900 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001901 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 },
1903};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
NeilBrownfd01b882011-10-11 16:47:53 +11001905static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001906{
1907 if (mddev->sync_super) {
1908 mddev->sync_super(mddev, rdev);
1909 return;
1910 }
1911
1912 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1913
1914 super_types[mddev->major_version].sync_super(mddev, rdev);
1915}
1916
NeilBrownfd01b882011-10-11 16:47:53 +11001917static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
NeilBrown3cb03002011-10-11 16:45:26 +11001919 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
NeilBrown4b809912008-07-21 17:05:25 +10001921 rcu_read_lock();
1922 rdev_for_each_rcu(rdev, mddev1)
1923 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001924 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001925 rdev2->bdev->bd_contains) {
1926 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001927 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001928 }
1929 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return 0;
1931}
1932
1933static LIST_HEAD(pending_raid_disks);
1934
Andre Nollac5e7112009-08-03 10:59:47 +10001935/*
1936 * Try to register data integrity profile for an mddev
1937 *
1938 * This is called when an array is started and after a disk has been kicked
1939 * from the array. It only succeeds if all working and active component devices
1940 * are integrity capable with matching profiles.
1941 */
NeilBrownfd01b882011-10-11 16:47:53 +11001942int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001943{
NeilBrown3cb03002011-10-11 16:45:26 +11001944 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001945
Andre Nollac5e7112009-08-03 10:59:47 +10001946 if (list_empty(&mddev->disks))
1947 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001948 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1949 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001950 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001951 /* skip spares and non-functional disks */
1952 if (test_bit(Faulty, &rdev->flags))
1953 continue;
1954 if (rdev->raid_disk < 0)
1955 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001956 if (!reference) {
1957 /* Use the first rdev as the reference */
1958 reference = rdev;
1959 continue;
1960 }
1961 /* does this rdev's profile match the reference profile? */
1962 if (blk_integrity_compare(reference->bdev->bd_disk,
1963 rdev->bdev->bd_disk) < 0)
1964 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001965 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001966 if (!reference || !bdev_get_integrity(reference->bdev))
1967 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001968 /*
1969 * All component devices are integrity capable and have matching
1970 * profiles, register the common profile for the md device.
1971 */
1972 if (blk_integrity_register(mddev->gendisk,
1973 bdev_get_integrity(reference->bdev)) != 0) {
1974 printk(KERN_ERR "md: failed to register integrity for %s\n",
1975 mdname(mddev));
1976 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001977 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001978 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1979 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1980 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1981 mdname(mddev));
1982 return -EINVAL;
1983 }
Andre Nollac5e7112009-08-03 10:59:47 +10001984 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001985}
Andre Nollac5e7112009-08-03 10:59:47 +10001986EXPORT_SYMBOL(md_integrity_register);
1987
1988/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001989void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10001990{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001991 struct blk_integrity *bi_rdev;
1992 struct blk_integrity *bi_mddev;
1993
1994 if (!mddev->gendisk)
1995 return;
1996
1997 bi_rdev = bdev_get_integrity(rdev->bdev);
1998 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10001999
2000 if (!bi_mddev) /* nothing to do */
2001 return;
2002 if (rdev->raid_disk < 0) /* skip spares */
2003 return;
2004 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2005 rdev->bdev->bd_disk) >= 0)
2006 return;
2007 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2008 blk_integrity_unregister(mddev->gendisk);
2009}
2010EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002011
NeilBrownf72ffdd2014-09-30 14:23:59 +10002012static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002014 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002015 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002016 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002017 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Dan Williams11e2ede2008-04-30 00:52:32 -07002019 /* prevent duplicates */
2020 if (find_rdev(mddev, rdev->bdev->bd_dev))
2021 return -EEXIST;
2022
Andre Nolldd8ac332009-03-31 14:33:13 +11002023 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2024 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2025 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002026 if (mddev->pers) {
2027 /* Cannot change size, so fail
2028 * If mddev->level <= 0, then we don't care
2029 * about aligning sizes (e.g. linear)
2030 */
2031 if (mddev->level > 0)
2032 return -ENOSPC;
2033 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002034 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 /* Verify rdev->desc_nr is unique.
2038 * If it is -1, assign a free number, else
2039 * check number is not in use
2040 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002041 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (rdev->desc_nr < 0) {
2043 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002044 if (mddev->pers)
2045 choice = mddev->raid_disks;
2046 while (find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 choice++;
2048 rdev->desc_nr = choice;
2049 } else {
NeilBrown4878e9e2014-09-25 17:00:11 +10002050 if (find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2051 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002055 rcu_read_unlock();
NeilBrownde01dfa2009-02-06 18:02:46 +11002056 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2057 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2058 mdname(mddev), mddev->max_disks);
2059 return -EBUSY;
2060 }
NeilBrown19133a42005-11-08 21:39:35 -08002061 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002062 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002063 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002066 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002067
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002068 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002069 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002070
Tejun Heo0762b8b2008-08-25 19:56:12 +09002071 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002072 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2073 /* failure here is OK */;
2074 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002075
NeilBrown4b809912008-07-21 17:05:25 +10002076 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002077 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002078
2079 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002080 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002083
2084 fail:
2085 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2086 b, mdname(mddev));
2087 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
NeilBrown177a99b2008-02-06 01:39:56 -08002090static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002091{
NeilBrown3cb03002011-10-11 16:45:26 +11002092 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002093 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002094 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002095}
2096
NeilBrownf72ffdd2014-09-30 14:23:59 +10002097static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
2099 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002100
Tejun Heo49731ba2011-01-14 18:43:57 +01002101 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002102 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2104 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002105 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002106 sysfs_put(rdev->sysfs_state);
2107 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002108 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002109 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002110 * writing to 'remove' to "dev/state". We also need
2111 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002112 */
NeilBrown4b809912008-07-21 17:05:25 +10002113 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002114 INIT_WORK(&rdev->del_work, md_delayed_delete);
2115 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002116 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
2119/*
2120 * prevent the device from being mounted, repartitioned or
2121 * otherwise reused by a RAID array (or any other kernel
2122 * subsystem), by bd_claiming the device.
2123 */
NeilBrown3cb03002011-10-11 16:45:26 +11002124static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 int err = 0;
2127 struct block_device *bdev;
2128 char b[BDEVNAME_SIZE];
2129
Tejun Heod4d77622010-11-13 11:55:18 +01002130 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002131 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (IS_ERR(bdev)) {
2133 printk(KERN_ERR "md: could not open %s.\n",
2134 __bdevname(dev, b));
2135 return PTR_ERR(bdev);
2136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 rdev->bdev = bdev;
2138 return err;
2139}
2140
NeilBrown3cb03002011-10-11 16:45:26 +11002141static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 struct block_device *bdev = rdev->bdev;
2144 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002145 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
2148void md_autodetect_dev(dev_t dev);
2149
NeilBrownf72ffdd2014-09-30 14:23:59 +10002150static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 printk(KERN_INFO "md: export_rdev(%s)\n",
2155 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002156 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002158 if (test_bit(AutoDetected, &rdev->flags))
2159 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160#endif
2161 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002162 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
NeilBrownf72ffdd2014-09-30 14:23:59 +10002165static void kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 unbind_rdev_from_array(rdev);
2168 export_rdev(rdev);
2169}
2170
NeilBrownfd01b882011-10-11 16:47:53 +11002171static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
NeilBrown0638bb02014-09-25 17:43:47 +10002173 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
NeilBrown0638bb02014-09-25 17:43:47 +10002175 while (!list_empty(&mddev->disks)) {
2176 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2177 same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 kick_rdev_from_array(rdev);
2179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 mddev->raid_disks = 0;
2181 mddev->major_version = 0;
2182}
2183
NeilBrownf72ffdd2014-09-30 14:23:59 +10002184static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
NeilBrown42543762006-06-26 00:27:57 -07002186 /* Update each superblock (in-memory image), but
2187 * if we are allowed to, skip spares which already
2188 * have the right event counter, or have one earlier
2189 * (which would mean they aren't being marked as dirty
2190 * with the rest of the array)
2191 */
NeilBrown3cb03002011-10-11 16:45:26 +11002192 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002193 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002194 if (rdev->sb_events == mddev->events ||
2195 (nospares &&
2196 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002197 rdev->sb_events+1 == mddev->events)) {
2198 /* Don't update this superblock */
2199 rdev->sb_loaded = 2;
2200 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002201 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002202 rdev->sb_loaded = 1;
2203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
2205}
2206
NeilBrownf72ffdd2014-09-30 14:23:59 +10002207static void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
NeilBrown3cb03002011-10-11 16:45:26 +11002209 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002210 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002211 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002212 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
NeilBrownd87f0642013-04-24 11:42:40 +10002214 if (mddev->ro) {
2215 if (force_change)
2216 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2217 return;
2218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002220 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002221 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002222 if (rdev->raid_disk >= 0 &&
2223 mddev->delta_disks >= 0 &&
2224 !test_bit(In_sync, &rdev->flags) &&
2225 mddev->curr_resync_completed > rdev->recovery_offset)
2226 rdev->recovery_offset = mddev->curr_resync_completed;
2227
NeilBrownf72ffdd2014-09-30 14:23:59 +10002228 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002229 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002230 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002231 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002232 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002233 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002234 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002235 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002236 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002237 md_ack_all_badblocks(&rdev->badblocks);
2238 md_error(mddev, rdev);
2239 }
2240 clear_bit(Blocked, &rdev->flags);
2241 clear_bit(BlockedBadBlocks, &rdev->flags);
2242 wake_up(&rdev->blocked_wait);
2243 }
2244 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002245 wake_up(&mddev->sb_wait);
2246 return;
2247 }
2248
NeilBrown85572d72014-12-15 12:56:56 +11002249 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002250
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002251 mddev->utime = get_seconds();
2252
NeilBrown850b2b42006-10-03 01:15:46 -07002253 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2254 force_change = 1;
2255 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2256 /* just a clean<-> dirty transition, possibly leave spares alone,
2257 * though if events isn't the right even/odd, we will have to do
2258 * spares after all
2259 */
2260 nospares = 1;
2261 if (force_change)
2262 nospares = 0;
2263 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002264 /* If the array is degraded, then skipping spares is both
2265 * dangerous and fairly pointless.
2266 * Dangerous because a device that was removed from the array
2267 * might have a event_count that still looks up-to-date,
2268 * so it can be re-added without a resync.
2269 * Pointless because if there are any spares to skip,
2270 * then a recovery will happen and soon that array won't
2271 * be degraded any more and the spare can go back to sleep then.
2272 */
NeilBrown850b2b42006-10-03 01:15:46 -07002273 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002274
NeilBrown06d91a52005-06-21 17:17:12 -07002275 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002276
2277 /* If this is just a dirty<->clean transition, and the array is clean
2278 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002279 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002280 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002281 && mddev->can_decrease_events
2282 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002283 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002284 mddev->can_decrease_events = 0;
2285 } else {
NeilBrown42543762006-06-26 00:27:57 -07002286 /* otherwise we have to go forward and ... */
2287 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002288 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
NeilBrown403df472014-09-30 15:52:29 +10002291 /*
2292 * This 64-bit counter should never wrap.
2293 * Either we are in around ~1 trillion A.C., assuming
2294 * 1 reboot per second, or we have a bug...
2295 */
2296 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002297
NeilBrowndafb20f2012-03-19 12:46:39 +11002298 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002299 if (rdev->badblocks.changed)
2300 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002301 if (test_bit(Faulty, &rdev->flags))
2302 set_bit(FaultRecorded, &rdev->flags);
2303 }
NeilBrown2699b672011-07-28 11:31:47 +10002304
NeilBrowne6910632008-02-06 01:39:51 -08002305 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002306 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
NeilBrown36a4e1f2011-10-07 14:23:17 +11002308 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2309 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
NeilBrown4ad13662007-07-17 04:06:13 -07002311 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002312 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002314
NeilBrown42543762006-06-26 00:27:57 -07002315 if (rdev->sb_loaded != 1)
2316 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
NeilBrownf4667222013-12-09 12:04:56 +11002318 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002319 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002320 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002321 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002322 pr_debug("md: (write) %s's sb offset: %llu\n",
2323 bdevname(rdev->bdev, b),
2324 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002325 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002326 if (rdev->badblocks.size) {
2327 md_super_write(mddev, rdev,
2328 rdev->badblocks.sector,
2329 rdev->badblocks.size << 9,
2330 rdev->bb_page);
2331 rdev->badblocks.size = 0;
2332 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002333
NeilBrownf4667222013-12-09 12:04:56 +11002334 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002335 pr_debug("md: %s (skipping faulty)\n",
2336 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002337
NeilBrown7bfa19f2005-06-21 17:17:28 -07002338 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /* only need to write one superblock... */
2340 break;
2341 }
NeilBrowna9701a32005-11-08 21:39:34 -08002342 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002343 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002344
NeilBrown85572d72014-12-15 12:56:56 +11002345 spin_lock(&mddev->lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002346 if (mddev->in_sync != sync_req ||
2347 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002348 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002349 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002350 goto repeat;
2351 }
NeilBrown850b2b42006-10-03 01:15:46 -07002352 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002353 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002354 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002355 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2356 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002357
NeilBrowndafb20f2012-03-19 12:46:39 +11002358 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002359 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2360 clear_bit(Blocked, &rdev->flags);
2361
2362 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002363 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002364 clear_bit(BlockedBadBlocks, &rdev->flags);
2365 wake_up(&rdev->blocked_wait);
2366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
Andre Noll7f6ce762008-03-23 18:34:54 +01002369/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002370 * We want to accept with case. For this we use cmd_match.
2371 */
2372static int cmd_match(const char *cmd, const char *str)
2373{
2374 /* See if cmd, written into a sysfs file, matches
2375 * str. They must either be the same, or cmd can
2376 * have a trailing newline
2377 */
2378 while (*cmd && *str && *cmd == *str) {
2379 cmd++;
2380 str++;
2381 }
2382 if (*cmd == '\n')
2383 cmd++;
2384 if (*str || *cmd)
2385 return 0;
2386 return 1;
2387}
2388
NeilBrown86e6ffd2005-11-08 21:39:24 -08002389struct rdev_sysfs_entry {
2390 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002391 ssize_t (*show)(struct md_rdev *, char *);
2392 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002393};
2394
2395static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002396state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002397{
2398 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002399 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002400 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002401
NeilBrown758bfc82014-12-15 12:56:59 +11002402 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002403 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002404 len+= sprintf(page+len, "%sfaulty",sep);
2405 sep = ",";
2406 }
NeilBrown758bfc82014-12-15 12:56:59 +11002407 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002408 len += sprintf(page+len, "%sin_sync",sep);
2409 sep = ",";
2410 }
NeilBrown758bfc82014-12-15 12:56:59 +11002411 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002412 len += sprintf(page+len, "%swrite_mostly",sep);
2413 sep = ",";
2414 }
NeilBrown758bfc82014-12-15 12:56:59 +11002415 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002416 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002417 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002418 len += sprintf(page+len, "%sblocked", sep);
2419 sep = ",";
2420 }
NeilBrown758bfc82014-12-15 12:56:59 +11002421 if (!test_bit(Faulty, &flags) &&
2422 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002423 len += sprintf(page+len, "%sspare", sep);
2424 sep = ",";
2425 }
NeilBrown758bfc82014-12-15 12:56:59 +11002426 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002427 len += sprintf(page+len, "%swrite_error", sep);
2428 sep = ",";
2429 }
NeilBrown758bfc82014-12-15 12:56:59 +11002430 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002431 len += sprintf(page+len, "%swant_replacement", sep);
2432 sep = ",";
2433 }
NeilBrown758bfc82014-12-15 12:56:59 +11002434 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002435 len += sprintf(page+len, "%sreplacement", sep);
2436 sep = ",";
2437 }
2438
NeilBrown86e6ffd2005-11-08 21:39:24 -08002439 return len+sprintf(page+len, "\n");
2440}
2441
NeilBrown45dc2de2006-06-26 00:27:58 -07002442static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002443state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002444{
2445 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002446 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002447 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002448 * writemostly - sets write_mostly
2449 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002450 * blocked - sets the Blocked flags
2451 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002452 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002453 * -insync - clear Insync for a device with a slot assigned,
2454 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002455 * write_error - sets WriteErrorSeen
2456 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002457 */
2458 int err = -EINVAL;
2459 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2460 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002461 if (test_bit(Faulty, &rdev->flags))
2462 err = 0;
2463 else
2464 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002465 } else if (cmd_match(buf, "remove")) {
2466 if (rdev->raid_disk >= 0)
2467 err = -EBUSY;
2468 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002469 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002470 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002471 if (mddev->pers)
2472 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002473 md_new_event(mddev);
2474 err = 0;
2475 }
NeilBrownf6556752006-06-26 00:28:01 -07002476 } else if (cmd_match(buf, "writemostly")) {
2477 set_bit(WriteMostly, &rdev->flags);
2478 err = 0;
2479 } else if (cmd_match(buf, "-writemostly")) {
2480 clear_bit(WriteMostly, &rdev->flags);
2481 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002482 } else if (cmd_match(buf, "blocked")) {
2483 set_bit(Blocked, &rdev->flags);
2484 err = 0;
2485 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002486 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002487 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002488 /* metadata handler doesn't understand badblocks,
2489 * so we need to fail the device
2490 */
2491 md_error(rdev->mddev, rdev);
2492 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002493 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002494 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002495 wake_up(&rdev->blocked_wait);
2496 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2497 md_wakeup_thread(rdev->mddev->thread);
2498
2499 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002500 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2501 set_bit(In_sync, &rdev->flags);
2502 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002503 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
NeilBrowne1960f82014-09-30 15:24:25 +10002504 if (rdev->mddev->pers == NULL) {
2505 clear_bit(In_sync, &rdev->flags);
2506 rdev->saved_raid_disk = rdev->raid_disk;
2507 rdev->raid_disk = -1;
2508 err = 0;
2509 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002510 } else if (cmd_match(buf, "write_error")) {
2511 set_bit(WriteErrorSeen, &rdev->flags);
2512 err = 0;
2513 } else if (cmd_match(buf, "-write_error")) {
2514 clear_bit(WriteErrorSeen, &rdev->flags);
2515 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002516 } else if (cmd_match(buf, "want_replacement")) {
2517 /* Any non-spare device that is not a replacement can
2518 * become want_replacement at any time, but we then need to
2519 * check if recovery is needed.
2520 */
2521 if (rdev->raid_disk >= 0 &&
2522 !test_bit(Replacement, &rdev->flags))
2523 set_bit(WantReplacement, &rdev->flags);
2524 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2525 md_wakeup_thread(rdev->mddev->thread);
2526 err = 0;
2527 } else if (cmd_match(buf, "-want_replacement")) {
2528 /* Clearing 'want_replacement' is always allowed.
2529 * Once replacements starts it is too late though.
2530 */
2531 err = 0;
2532 clear_bit(WantReplacement, &rdev->flags);
2533 } else if (cmd_match(buf, "replacement")) {
2534 /* Can only set a device as a replacement when array has not
2535 * yet been started. Once running, replacement is automatic
2536 * from spares, or by assigning 'slot'.
2537 */
2538 if (rdev->mddev->pers)
2539 err = -EBUSY;
2540 else {
2541 set_bit(Replacement, &rdev->flags);
2542 err = 0;
2543 }
2544 } else if (cmd_match(buf, "-replacement")) {
2545 /* Similarly, can only clear Replacement before start */
2546 if (rdev->mddev->pers)
2547 err = -EBUSY;
2548 else {
2549 clear_bit(Replacement, &rdev->flags);
2550 err = 0;
2551 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002552 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002553 if (!err)
2554 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002555 return err ? err : len;
2556}
NeilBrown80ca3a42006-07-10 04:44:18 -07002557static struct rdev_sysfs_entry rdev_state =
2558__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002559
2560static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002561errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002562{
2563 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2564}
2565
2566static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002567errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002568{
2569 char *e;
2570 unsigned long n = simple_strtoul(buf, &e, 10);
2571 if (*buf && (*e == 0 || *e == '\n')) {
2572 atomic_set(&rdev->corrected_errors, n);
2573 return len;
2574 }
2575 return -EINVAL;
2576}
2577static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002578__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002579
NeilBrown014236d2006-01-06 00:20:55 -08002580static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002581slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002582{
2583 if (rdev->raid_disk < 0)
2584 return sprintf(page, "none\n");
2585 else
2586 return sprintf(page, "%d\n", rdev->raid_disk);
2587}
2588
2589static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002590slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002591{
2592 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002593 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002594 int slot = simple_strtoul(buf, &e, 10);
2595 if (strncmp(buf, "none", 4)==0)
2596 slot = -1;
2597 else if (e==buf || (*e && *e!= '\n'))
2598 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002599 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002600 /* Setting 'slot' on an active array requires also
2601 * updating the 'rd%d' link, and communicating
2602 * with the personality with ->hot_*_disk.
2603 * For now we only support removing
2604 * failed/spare devices. This normally happens automatically,
2605 * but not when the metadata is externally managed.
2606 */
NeilBrownc303da62008-02-06 01:39:51 -08002607 if (rdev->raid_disk == -1)
2608 return -EEXIST;
2609 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002610 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002611 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002612 clear_bit(Blocked, &rdev->flags);
2613 remove_and_add_spares(rdev->mddev, rdev);
2614 if (rdev->raid_disk >= 0)
2615 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002616 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2617 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002618 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002619 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002620 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002621 */
2622
2623 if (rdev->raid_disk != -1)
2624 return -EBUSY;
2625
NeilBrownc6751b22011-02-02 11:57:13 +11002626 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2627 return -EBUSY;
2628
Neil Brown6c2fce22008-06-28 08:31:31 +10002629 if (rdev->mddev->pers->hot_add_disk == NULL)
2630 return -EINVAL;
2631
NeilBrownba1b41b2011-01-14 09:14:34 +11002632 if (slot >= rdev->mddev->raid_disks &&
2633 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2634 return -ENOSPC;
2635
Neil Brown6c2fce22008-06-28 08:31:31 +10002636 rdev->raid_disk = slot;
2637 if (test_bit(In_sync, &rdev->flags))
2638 rdev->saved_raid_disk = slot;
2639 else
2640 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002641 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002642 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002643 err = rdev->mddev->pers->
2644 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002645 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002646 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002647 return err;
Neil Brown52664732008-06-28 08:31:44 +10002648 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002649 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002650 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002651 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002652 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002653 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002654 if (slot >= rdev->mddev->raid_disks &&
2655 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002656 return -ENOSPC;
2657 rdev->raid_disk = slot;
2658 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002659 clear_bit(Faulty, &rdev->flags);
2660 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002661 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002662 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002663 }
NeilBrown014236d2006-01-06 00:20:55 -08002664 return len;
2665}
2666
NeilBrown014236d2006-01-06 00:20:55 -08002667static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002668__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002669
NeilBrown93c8cad2006-01-06 00:20:56 -08002670static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002671offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002672{
Andrew Morton6961ece2006-01-06 00:20:59 -08002673 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002674}
2675
2676static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002677offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002678{
NeilBrownc6563a82012-05-21 09:27:00 +10002679 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002680 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002681 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002682 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002683 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002684 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002685 /* Must set offset before size, so overlap checks
2686 * can be sane */
2687 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002688 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002689 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002690 return len;
2691}
2692
2693static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002694__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002695
NeilBrownc6563a82012-05-21 09:27:00 +10002696static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2697{
2698 return sprintf(page, "%llu\n",
2699 (unsigned long long)rdev->new_data_offset);
2700}
2701
2702static ssize_t new_offset_store(struct md_rdev *rdev,
2703 const char *buf, size_t len)
2704{
2705 unsigned long long new_offset;
2706 struct mddev *mddev = rdev->mddev;
2707
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002708 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002709 return -EINVAL;
2710
NeilBrownf851b602014-12-11 10:02:10 +11002711 if (mddev->sync_thread ||
2712 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002713 return -EBUSY;
2714 if (new_offset == rdev->data_offset)
2715 /* reset is always permitted */
2716 ;
2717 else if (new_offset > rdev->data_offset) {
2718 /* must not push array size beyond rdev_sectors */
2719 if (new_offset - rdev->data_offset
2720 + mddev->dev_sectors > rdev->sectors)
2721 return -E2BIG;
2722 }
2723 /* Metadata worries about other space details. */
2724
2725 /* decreasing the offset is inconsistent with a backwards
2726 * reshape.
2727 */
2728 if (new_offset < rdev->data_offset &&
2729 mddev->reshape_backwards)
2730 return -EINVAL;
2731 /* Increasing offset is inconsistent with forwards
2732 * reshape. reshape_direction should be set to
2733 * 'backwards' first.
2734 */
2735 if (new_offset > rdev->data_offset &&
2736 !mddev->reshape_backwards)
2737 return -EINVAL;
2738
2739 if (mddev->pers && mddev->persistent &&
2740 !super_types[mddev->major_version]
2741 .allow_new_offset(rdev, new_offset))
2742 return -E2BIG;
2743 rdev->new_data_offset = new_offset;
2744 if (new_offset > rdev->data_offset)
2745 mddev->reshape_backwards = 1;
2746 else if (new_offset < rdev->data_offset)
2747 mddev->reshape_backwards = 0;
2748
2749 return len;
2750}
2751static struct rdev_sysfs_entry rdev_new_offset =
2752__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2753
NeilBrown83303b62006-01-06 00:21:06 -08002754static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002755rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002756{
Andre Nolldd8ac332009-03-31 14:33:13 +11002757 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002758}
2759
NeilBrownc5d79ad2008-02-06 01:39:54 -08002760static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2761{
2762 /* check if two start/length pairs overlap */
2763 if (s1+l1 <= s2)
2764 return 0;
2765 if (s2+l2 <= s1)
2766 return 0;
2767 return 1;
2768}
2769
Dan Williamsb522adc2009-03-31 15:00:31 +11002770static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2771{
2772 unsigned long long blocks;
2773 sector_t new;
2774
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002775 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002776 return -EINVAL;
2777
2778 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2779 return -EINVAL; /* sector conversion overflow */
2780
2781 new = blocks * 2;
2782 if (new != blocks * 2)
2783 return -EINVAL; /* unsigned long long to sector_t overflow */
2784
2785 *sectors = new;
2786 return 0;
2787}
2788
NeilBrown83303b62006-01-06 00:21:06 -08002789static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002790rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002791{
NeilBrownfd01b882011-10-11 16:47:53 +11002792 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002793 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002794 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002795
Dan Williamsb522adc2009-03-31 15:00:31 +11002796 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002797 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002798 if (rdev->data_offset != rdev->new_data_offset)
2799 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002800 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002801 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002802 sectors = super_types[my_mddev->major_version].
2803 rdev_size_change(rdev, sectors);
2804 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002805 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002806 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002807 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002808 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002809 if (!my_mddev->pers->resize)
2810 /* Cannot change size for RAID0 or Linear etc */
2811 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002812 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002813 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002814 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002815
Andre Nolldd8ac332009-03-31 14:33:13 +11002816 rdev->sectors = sectors;
2817 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002818 /* Need to check that all other rdevs with the same
2819 * ->bdev do not overlap. 'rcu' is sufficient to walk
2820 * the rdev lists safely.
2821 * This check does not provide a hard guarantee, it
2822 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002823 */
NeilBrownfd01b882011-10-11 16:47:53 +11002824 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002825 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002826 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002827
NeilBrown8b1afc32014-09-29 15:33:20 +10002828 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002829 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002830 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002831
NeilBrowndafb20f2012-03-19 12:46:39 +11002832 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002833 if (rdev->bdev == rdev2->bdev &&
2834 rdev != rdev2 &&
2835 overlaps(rdev->data_offset, rdev->sectors,
2836 rdev2->data_offset,
2837 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002838 overlap = 1;
2839 break;
2840 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002841 if (overlap) {
2842 mddev_put(mddev);
2843 break;
2844 }
2845 }
NeilBrown8b1afc32014-09-29 15:33:20 +10002846 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08002847 if (overlap) {
2848 /* Someone else could have slipped in a size
2849 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002850 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002851 * safe, and trust userspace not to race with
2852 * itself
2853 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002854 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002855 return -EBUSY;
2856 }
2857 }
NeilBrown83303b62006-01-06 00:21:06 -08002858 return len;
2859}
2860
2861static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002862__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002863
NeilBrown3cb03002011-10-11 16:45:26 +11002864static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002865{
2866 unsigned long long recovery_start = rdev->recovery_offset;
2867
2868 if (test_bit(In_sync, &rdev->flags) ||
2869 recovery_start == MaxSector)
2870 return sprintf(page, "none\n");
2871
2872 return sprintf(page, "%llu\n", recovery_start);
2873}
2874
NeilBrown3cb03002011-10-11 16:45:26 +11002875static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002876{
2877 unsigned long long recovery_start;
2878
2879 if (cmd_match(buf, "none"))
2880 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002881 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07002882 return -EINVAL;
2883
2884 if (rdev->mddev->pers &&
2885 rdev->raid_disk >= 0)
2886 return -EBUSY;
2887
2888 rdev->recovery_offset = recovery_start;
2889 if (recovery_start == MaxSector)
2890 set_bit(In_sync, &rdev->flags);
2891 else
2892 clear_bit(In_sync, &rdev->flags);
2893 return len;
2894}
2895
2896static struct rdev_sysfs_entry rdev_recovery_start =
2897__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2898
NeilBrown16c791a2011-07-28 11:31:47 +10002899static ssize_t
2900badblocks_show(struct badblocks *bb, char *page, int unack);
2901static ssize_t
2902badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2903
NeilBrown3cb03002011-10-11 16:45:26 +11002904static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002905{
2906 return badblocks_show(&rdev->badblocks, page, 0);
2907}
NeilBrown3cb03002011-10-11 16:45:26 +11002908static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002909{
NeilBrownde393cd2011-07-28 11:31:48 +10002910 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2911 /* Maybe that ack was all we needed */
2912 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2913 wake_up(&rdev->blocked_wait);
2914 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002915}
2916static struct rdev_sysfs_entry rdev_bad_blocks =
2917__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2918
NeilBrown3cb03002011-10-11 16:45:26 +11002919static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002920{
2921 return badblocks_show(&rdev->badblocks, page, 1);
2922}
NeilBrown3cb03002011-10-11 16:45:26 +11002923static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002924{
2925 return badblocks_store(&rdev->badblocks, page, len, 1);
2926}
2927static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2928__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2929
NeilBrown86e6ffd2005-11-08 21:39:24 -08002930static struct attribute *rdev_default_attrs[] = {
2931 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002932 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002933 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002934 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10002935 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002936 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002937 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002938 &rdev_bad_blocks.attr,
2939 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002940 NULL,
2941};
2942static ssize_t
2943rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2944{
2945 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002946 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002947
2948 if (!entry->show)
2949 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11002950 if (!rdev->mddev)
2951 return -EBUSY;
2952 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002953}
2954
2955static ssize_t
2956rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2957 const char *page, size_t length)
2958{
2959 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002960 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002961 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11002962 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002963
2964 if (!entry->store)
2965 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002966 if (!capable(CAP_SYS_ADMIN))
2967 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002968 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002969 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002970 if (rdev->mddev == NULL)
2971 rv = -EBUSY;
2972 else
2973 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002974 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002975 }
2976 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002977}
2978
2979static void rdev_free(struct kobject *ko)
2980{
NeilBrown3cb03002011-10-11 16:45:26 +11002981 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002982 kfree(rdev);
2983}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002984static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002985 .show = rdev_attr_show,
2986 .store = rdev_attr_store,
2987};
2988static struct kobj_type rdev_ktype = {
2989 .release = rdev_free,
2990 .sysfs_ops = &rdev_sysfs_ops,
2991 .default_attrs = rdev_default_attrs,
2992};
2993
NeilBrown3cb03002011-10-11 16:45:26 +11002994int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10002995{
2996 rdev->desc_nr = -1;
2997 rdev->saved_raid_disk = -1;
2998 rdev->raid_disk = -1;
2999 rdev->flags = 0;
3000 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003001 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003002 rdev->sb_events = 0;
3003 rdev->last_read_error.tv_sec = 0;
3004 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003005 rdev->sb_loaded = 0;
3006 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003007 atomic_set(&rdev->nr_pending, 0);
3008 atomic_set(&rdev->read_errors, 0);
3009 atomic_set(&rdev->corrected_errors, 0);
3010
3011 INIT_LIST_HEAD(&rdev->same_set);
3012 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003013
3014 /* Add space to store bad block list.
3015 * This reserves the space even on arrays where it cannot
3016 * be used - I wonder if that matters
3017 */
3018 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003019 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003020 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3021 seqlock_init(&rdev->badblocks.lock);
3022 if (rdev->badblocks.page == NULL)
3023 return -ENOMEM;
3024
3025 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003026}
3027EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028/*
3029 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3030 *
3031 * mark the device faulty if:
3032 *
3033 * - the device is nonexistent (zero size)
3034 * - the device has no valid superblock
3035 *
3036 * a faulty rdev _never_ has rdev->sb set.
3037 */
NeilBrown3cb03002011-10-11 16:45:26 +11003038static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
3040 char b[BDEVNAME_SIZE];
3041 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003042 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 sector_t size;
3044
NeilBrown9ffae0c2006-01-06 00:20:32 -08003045 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 if (!rdev) {
3047 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3048 return ERR_PTR(-ENOMEM);
3049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
NeilBrown2230dfe2011-07-28 11:31:46 +10003051 err = md_rdev_init(rdev);
3052 if (err)
3053 goto abort_free;
3054 err = alloc_disk_sb(rdev);
3055 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 goto abort_free;
3057
NeilBrownc5d79ad2008-02-06 01:39:54 -08003058 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 if (err)
3060 goto abort_free;
3061
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003062 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003063
Mike Snitzer77304d22010-11-08 14:39:12 +01003064 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003066 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 "md: %s has zero or unknown size, marking faulty!\n",
3068 bdevname(rdev->bdev,b));
3069 err = -EINVAL;
3070 goto abort_free;
3071 }
3072
3073 if (super_format >= 0) {
3074 err = super_types[super_format].
3075 load_super(rdev, NULL, super_minor);
3076 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003077 printk(KERN_WARNING
3078 "md: %s does not have a valid v%d.%d "
3079 "superblock, not importing!\n",
3080 bdevname(rdev->bdev,b),
3081 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 goto abort_free;
3083 }
3084 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003085 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 "md: could not read %s's sb, not importing!\n",
3087 bdevname(rdev->bdev,b));
3088 goto abort_free;
3089 }
3090 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003091
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return rdev;
3093
3094abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003095 if (rdev->bdev)
3096 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003097 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 kfree(rdev);
3099 return ERR_PTR(err);
3100}
3101
3102/*
3103 * Check a full RAID array for plausibility
3104 */
3105
NeilBrownf72ffdd2014-09-30 14:23:59 +10003106static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
3108 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003109 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 char b[BDEVNAME_SIZE];
3111
3112 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003113 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 switch (super_types[mddev->major_version].
3115 load_super(rdev, freshest, mddev->minor_version)) {
3116 case 1:
3117 freshest = rdev;
3118 break;
3119 case 0:
3120 break;
3121 default:
3122 printk( KERN_ERR \
3123 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003124 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 bdevname(rdev->bdev,b));
3126 kick_rdev_from_array(rdev);
3127 }
3128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 super_types[mddev->major_version].
3130 validate_super(mddev, freshest);
3131
3132 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003133 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003134 if (mddev->max_disks &&
3135 (rdev->desc_nr >= mddev->max_disks ||
3136 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003137 printk(KERN_WARNING
3138 "md: %s: %s: only %d devices permitted\n",
3139 mdname(mddev), bdevname(rdev->bdev, b),
3140 mddev->max_disks);
3141 kick_rdev_from_array(rdev);
3142 continue;
3143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 if (rdev != freshest)
3145 if (super_types[mddev->major_version].
3146 validate_super(mddev, rdev)) {
3147 printk(KERN_WARNING "md: kicking non-fresh %s"
3148 " from array!\n",
3149 bdevname(rdev->bdev,b));
3150 kick_rdev_from_array(rdev);
3151 continue;
3152 }
3153 if (mddev->level == LEVEL_MULTIPATH) {
3154 rdev->desc_nr = i++;
3155 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003156 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003157 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003158 rdev->raid_disk = -1;
3159 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 }
3161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162}
3163
NeilBrown72e02072009-12-14 12:49:55 +11003164/* Read a fixed-point number.
3165 * Numbers in sysfs attributes should be in "standard" units where
3166 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003167 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003168 * milliseconds or jiffies.
3169 * This function takes a decimal number with a possible fractional
3170 * component, and produces an integer which is the result of
3171 * multiplying that number by 10^'scale'.
3172 * all without any floating-point arithmetic.
3173 */
3174int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3175{
3176 unsigned long result = 0;
3177 long decimals = -1;
3178 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3179 if (*cp == '.')
3180 decimals = 0;
3181 else if (decimals < scale) {
3182 unsigned int value;
3183 value = *cp - '0';
3184 result = result * 10 + value;
3185 if (decimals >= 0)
3186 decimals++;
3187 }
3188 cp++;
3189 }
3190 if (*cp == '\n')
3191 cp++;
3192 if (*cp)
3193 return -EINVAL;
3194 if (decimals < 0)
3195 decimals = 0;
3196 while (decimals < scale) {
3197 result *= 10;
3198 decimals ++;
3199 }
3200 *res = result;
3201 return 0;
3202}
3203
NeilBrown19052c02008-08-05 15:54:13 +10003204static void md_safemode_timeout(unsigned long data);
3205
NeilBrowneae17012005-11-08 21:39:23 -08003206static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003207safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003208{
3209 int msec = (mddev->safemode_delay*1000)/HZ;
3210 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3211}
3212static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003213safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003214{
NeilBrown16f17b32006-06-26 00:27:37 -07003215 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003216
NeilBrown72e02072009-12-14 12:49:55 +11003217 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003218 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003219 if (msec == 0)
3220 mddev->safemode_delay = 0;
3221 else {
NeilBrown19052c02008-08-05 15:54:13 +10003222 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003223 unsigned long new_delay = (msec*HZ)/1000;
3224
3225 if (new_delay == 0)
3226 new_delay = 1;
3227 mddev->safemode_delay = new_delay;
3228 if (new_delay < old_delay || old_delay == 0)
3229 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003230 }
3231 return len;
3232}
3233static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003234__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003235
3236static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003237level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003238{
NeilBrown36d091f2014-12-15 12:56:58 +11003239 struct md_personality *p;
3240 int ret;
3241 spin_lock(&mddev->lock);
3242 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003243 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003244 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003245 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003246 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003247 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003248 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003249 else
NeilBrown36d091f2014-12-15 12:56:58 +11003250 ret = 0;
3251 spin_unlock(&mddev->lock);
3252 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003253}
3254
NeilBrownd9d166c2006-01-06 00:20:51 -08003255static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003256level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003257{
Dan Williamsf2859af2010-05-02 10:04:16 -07003258 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003259 ssize_t rv = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003260 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003261 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003262 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003263 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003264
3265 if (mddev->pers == NULL) {
3266 if (len == 0)
3267 return 0;
3268 if (len >= sizeof(mddev->clevel))
3269 return -ENOSPC;
3270 strncpy(mddev->clevel, buf, len);
3271 if (mddev->clevel[len-1] == '\n')
3272 len--;
3273 mddev->clevel[len] = 0;
3274 mddev->level = LEVEL_NONE;
3275 return rv;
3276 }
NeilBrownbd8839e2014-05-28 13:39:21 +10003277 if (mddev->ro)
3278 return -EROFS;
NeilBrown245f46c2009-03-31 14:39:39 +11003279
3280 /* request to change the personality. Need to ensure:
3281 * - array is not engaged in resync/recovery/reshape
3282 * - old personality can be suspended
3283 * - new personality will access other array.
3284 */
3285
NeilBrownbb4f1e92010-08-08 21:18:03 +10003286 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003287 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003288 mddev->reshape_position != MaxSector ||
3289 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003290 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003291
3292 if (!mddev->pers->quiesce) {
3293 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3294 mdname(mddev), mddev->pers->name);
3295 return -EINVAL;
3296 }
3297
3298 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003299 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003300 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003301 strncpy(clevel, buf, len);
3302 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003303 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003304 clevel[len] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003305 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003306 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003307
Dan Williamsf2859af2010-05-02 10:04:16 -07003308 if (request_module("md-%s", clevel) != 0)
3309 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003310 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003311 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003312 if (!pers || !try_module_get(pers->owner)) {
3313 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003314 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003315 return -EINVAL;
3316 }
3317 spin_unlock(&pers_lock);
3318
3319 if (pers == mddev->pers) {
3320 /* Nothing to do! */
3321 module_put(pers->owner);
3322 return rv;
3323 }
3324 if (!pers->takeover) {
3325 module_put(pers->owner);
3326 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003327 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003328 return -EINVAL;
3329 }
3330
NeilBrowndafb20f2012-03-19 12:46:39 +11003331 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003332 rdev->new_raid_disk = rdev->raid_disk;
3333
NeilBrown245f46c2009-03-31 14:39:39 +11003334 /* ->takeover must set new_* and/or delta_disks
3335 * if it succeeds, and may set them when it fails.
3336 */
3337 priv = pers->takeover(mddev);
3338 if (IS_ERR(priv)) {
3339 mddev->new_level = mddev->level;
3340 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003341 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003342 mddev->raid_disks -= mddev->delta_disks;
3343 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003344 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003345 module_put(pers->owner);
3346 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003347 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003348 return PTR_ERR(priv);
3349 }
3350
3351 /* Looks like we have a winner */
3352 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003353 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003354
3355 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003356 oldpers = mddev->pers;
3357 oldpriv = mddev->private;
3358 mddev->pers = pers;
3359 mddev->private = priv;
3360 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3361 mddev->level = mddev->new_level;
3362 mddev->layout = mddev->new_layout;
3363 mddev->chunk_sectors = mddev->new_chunk_sectors;
3364 mddev->delta_disks = 0;
3365 mddev->reshape_backwards = 0;
3366 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003367 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003368
NeilBrowndb721d32014-12-15 12:56:58 +11003369 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003370 mddev->external) {
3371 /* We are converting from a no-redundancy array
3372 * to a redundancy array and metadata is managed
3373 * externally so we need to be sure that writes
3374 * won't block due to a need to transition
3375 * clean->dirty
3376 * until external management is started.
3377 */
3378 mddev->in_sync = 0;
3379 mddev->safemode_delay = 0;
3380 mddev->safemode = 0;
3381 }
3382
NeilBrowndb721d32014-12-15 12:56:58 +11003383 oldpers->free(mddev, oldpriv);
3384
3385 if (oldpers->sync_request == NULL &&
3386 pers->sync_request != NULL) {
3387 /* need to add the md_redundancy_group */
3388 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3389 printk(KERN_WARNING
3390 "md: cannot register extra attributes for %s\n",
3391 mdname(mddev));
3392 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3393 }
3394 if (oldpers->sync_request != NULL &&
3395 pers->sync_request == NULL) {
3396 /* need to remove the md_redundancy_group */
3397 if (mddev->to_remove == NULL)
3398 mddev->to_remove = &md_redundancy_group;
3399 }
3400
NeilBrowndafb20f2012-03-19 12:46:39 +11003401 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003402 if (rdev->raid_disk < 0)
3403 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003404 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003405 rdev->new_raid_disk = -1;
3406 if (rdev->new_raid_disk == rdev->raid_disk)
3407 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003408 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003409 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003410 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003411 if (rdev->raid_disk < 0)
3412 continue;
3413 if (rdev->new_raid_disk == rdev->raid_disk)
3414 continue;
3415 rdev->raid_disk = rdev->new_raid_disk;
3416 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003417 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003418 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003419 if (sysfs_link_rdev(mddev, rdev))
3420 printk(KERN_WARNING "md: cannot register rd%d"
3421 " for %s after level change\n",
3422 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003423 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003424 }
3425
NeilBrowndb721d32014-12-15 12:56:58 +11003426 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003427 /* this is now an array without redundancy, so
3428 * it must always be in_sync
3429 */
3430 mddev->in_sync = 1;
3431 del_timer_sync(&mddev->safemode_timer);
3432 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003433 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003434 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003435 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003436 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003437 if (!mddev->thread)
3438 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003439 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003440 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003441 return rv;
3442}
3443
3444static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003445__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003446
NeilBrownd4dbd022006-06-26 00:27:59 -07003447static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003448layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003449{
3450 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003451 if (mddev->reshape_position != MaxSector &&
3452 mddev->layout != mddev->new_layout)
3453 return sprintf(page, "%d (%d)\n",
3454 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003455 return sprintf(page, "%d\n", mddev->layout);
3456}
3457
3458static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003459layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003460{
3461 char *e;
3462 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003463
3464 if (!*buf || (*e && *e != '\n'))
3465 return -EINVAL;
3466
NeilBrownb3546032009-03-31 14:56:41 +11003467 if (mddev->pers) {
3468 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003469 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003470 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10003471 if (mddev->ro)
3472 return -EROFS;
NeilBrown597a7112009-06-18 08:47:42 +10003473 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003474 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003475 if (err) {
3476 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003477 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003478 }
NeilBrownb3546032009-03-31 14:56:41 +11003479 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003480 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003481 if (mddev->reshape_position == MaxSector)
3482 mddev->layout = n;
3483 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003484 return len;
3485}
3486static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003487__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003488
NeilBrowneae17012005-11-08 21:39:23 -08003489static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003490raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003491{
NeilBrownbb636542005-11-08 21:39:45 -08003492 if (mddev->raid_disks == 0)
3493 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003494 if (mddev->reshape_position != MaxSector &&
3495 mddev->delta_disks != 0)
3496 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3497 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003498 return sprintf(page, "%d\n", mddev->raid_disks);
3499}
3500
NeilBrownfd01b882011-10-11 16:47:53 +11003501static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003502
3503static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003504raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003505{
NeilBrownda943b992006-01-06 00:20:54 -08003506 char *e;
3507 int rv = 0;
3508 unsigned long n = simple_strtoul(buf, &e, 10);
3509
3510 if (!*buf || (*e && *e != '\n'))
3511 return -EINVAL;
3512
3513 if (mddev->pers)
3514 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003515 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003516 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003517 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003518
3519 rdev_for_each(rdev, mddev) {
3520 if (olddisks < n &&
3521 rdev->data_offset < rdev->new_data_offset)
3522 return -EINVAL;
3523 if (olddisks > n &&
3524 rdev->data_offset > rdev->new_data_offset)
3525 return -EINVAL;
3526 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003527 mddev->delta_disks = n - olddisks;
3528 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003529 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003530 } else
NeilBrownda943b992006-01-06 00:20:54 -08003531 mddev->raid_disks = n;
3532 return rv ? rv : len;
3533}
3534static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003535__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003536
NeilBrown24dd4692005-11-08 21:39:26 -08003537static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003538chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003539{
NeilBrown08a02ec2007-05-09 02:35:38 -07003540 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003541 mddev->chunk_sectors != mddev->new_chunk_sectors)
3542 return sprintf(page, "%d (%d)\n",
3543 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003544 mddev->chunk_sectors << 9);
3545 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003546}
3547
3548static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003549chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003550{
NeilBrown3b343802006-01-06 00:20:47 -08003551 char *e;
3552 unsigned long n = simple_strtoul(buf, &e, 10);
3553
NeilBrown3b343802006-01-06 00:20:47 -08003554 if (!*buf || (*e && *e != '\n'))
3555 return -EINVAL;
3556
NeilBrownb3546032009-03-31 14:56:41 +11003557 if (mddev->pers) {
3558 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003559 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003560 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10003561 if (mddev->ro)
3562 return -EROFS;
NeilBrown597a7112009-06-18 08:47:42 +10003563 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003564 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003565 if (err) {
3566 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003567 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003568 }
NeilBrownb3546032009-03-31 14:56:41 +11003569 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003570 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003571 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003572 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003573 }
NeilBrown3b343802006-01-06 00:20:47 -08003574 return len;
3575}
3576static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003577__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003578
NeilBrowna94213b2006-06-26 00:28:00 -07003579static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003580resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003581{
NeilBrownd1a7c502009-03-31 15:24:32 +11003582 if (mddev->recovery_cp == MaxSector)
3583 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003584 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3585}
3586
3587static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003588resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003589{
NeilBrowna94213b2006-06-26 00:28:00 -07003590 char *e;
3591 unsigned long long n = simple_strtoull(buf, &e, 10);
3592
NeilBrownb0986362011-05-11 15:52:21 +10003593 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003594 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003595 if (cmd_match(buf, "none"))
3596 n = MaxSector;
3597 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003598 return -EINVAL;
3599
3600 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003601 if (mddev->pers)
3602 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003603 return len;
3604}
3605static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003606__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003607
NeilBrown9e653b62006-06-26 00:27:58 -07003608/*
3609 * The array state can be:
3610 *
3611 * clear
3612 * No devices, no size, no level
3613 * Equivalent to STOP_ARRAY ioctl
3614 * inactive
3615 * May have some settings, but array is not active
3616 * all IO results in error
3617 * When written, doesn't tear down array, but just stops it
3618 * suspended (not supported yet)
3619 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003620 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003621 * readonly
3622 * no resync can happen. no superblocks get written.
3623 * write requests fail
3624 * read-auto
3625 * like readonly, but behaves like 'clean' on a write request.
3626 *
3627 * clean - no pending writes, but otherwise active.
3628 * When written to inactive array, starts without resync
3629 * If a write request arrives then
3630 * if metadata is known, mark 'dirty' and switch to 'active'.
3631 * if not known, block and switch to write-pending
3632 * If written to an active array that has pending writes, then fails.
3633 * active
3634 * fully active: IO and resync can be happening.
3635 * When written to inactive array, starts with resync
3636 *
3637 * write-pending
3638 * clean, but writes are blocked waiting for 'active' to be written.
3639 *
3640 * active-idle
3641 * like active, but no writes have been seen for a while (100msec).
3642 *
3643 */
3644enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3645 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003646static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003647 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3648 "write-pending", "active-idle", NULL };
3649
3650static int match_word(const char *word, char **list)
3651{
3652 int n;
3653 for (n=0; list[n]; n++)
3654 if (cmd_match(word, list[n]))
3655 break;
3656 return n;
3657}
3658
3659static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003660array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003661{
3662 enum array_state st = inactive;
3663
3664 if (mddev->pers)
3665 switch(mddev->ro) {
3666 case 1:
3667 st = readonly;
3668 break;
3669 case 2:
3670 st = read_auto;
3671 break;
3672 case 0:
3673 if (mddev->in_sync)
3674 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003675 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003676 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003677 else if (mddev->safemode)
3678 st = active_idle;
3679 else
3680 st = active;
3681 }
3682 else {
3683 if (list_empty(&mddev->disks) &&
3684 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003685 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003686 st = clear;
3687 else
3688 st = inactive;
3689 }
3690 return sprintf(page, "%s\n", array_states[st]);
3691}
3692
NeilBrownf72ffdd2014-09-30 14:23:59 +10003693static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3694static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3695static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003696static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003697
3698static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003699array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003700{
3701 int err = -EINVAL;
3702 enum array_state st = match_word(buf, array_states);
3703 switch(st) {
3704 case bad_word:
3705 break;
3706 case clear:
3707 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003708 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003709 break;
3710 case inactive:
3711 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003712 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003713 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003714 else
NeilBrowne6910632008-02-06 01:39:51 -08003715 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003716 break;
3717 case suspended:
3718 break; /* not supported yet */
3719 case readonly:
3720 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003721 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003722 else {
3723 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003724 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003725 err = do_md_run(mddev);
3726 }
3727 break;
3728 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003729 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003730 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003731 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003732 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003733 err = restart_array(mddev);
3734 if (err == 0) {
3735 mddev->ro = 2;
3736 set_disk_ro(mddev->gendisk, 0);
3737 }
NeilBrown9e653b62006-06-26 00:27:58 -07003738 } else {
3739 mddev->ro = 2;
3740 err = do_md_run(mddev);
3741 }
3742 break;
3743 case clean:
3744 if (mddev->pers) {
3745 restart_array(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11003746 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003747 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003748 if (mddev->in_sync == 0) {
3749 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003750 if (mddev->safemode == 1)
3751 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003752 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003753 }
3754 err = 0;
3755 } else
3756 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11003757 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003758 } else
3759 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003760 break;
3761 case active:
3762 if (mddev->pers) {
3763 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003764 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003765 wake_up(&mddev->sb_wait);
3766 err = 0;
3767 } else {
3768 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003769 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003770 err = do_md_run(mddev);
3771 }
3772 break;
3773 case write_pending:
3774 case active_idle:
3775 /* these cannot be set */
3776 break;
3777 }
3778 if (err)
3779 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003780 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003781 if (mddev->hold_active == UNTIL_IOCTL)
3782 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003783 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003784 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003785 }
NeilBrown9e653b62006-06-26 00:27:58 -07003786}
NeilBrown80ca3a42006-07-10 04:44:18 -07003787static struct md_sysfs_entry md_array_state =
3788__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003789
NeilBrown6d7ff7382006-01-06 00:21:16 -08003790static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003791max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003792 return sprintf(page, "%d\n",
3793 atomic_read(&mddev->max_corr_read_errors));
3794}
3795
3796static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003797max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003798{
3799 char *e;
3800 unsigned long n = simple_strtoul(buf, &e, 10);
3801
3802 if (*buf && (*e == 0 || *e == '\n')) {
3803 atomic_set(&mddev->max_corr_read_errors, n);
3804 return len;
3805 }
3806 return -EINVAL;
3807}
3808
3809static struct md_sysfs_entry max_corr_read_errors =
3810__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3811 max_corrected_read_errors_store);
3812
3813static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003814null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003815{
3816 return -EINVAL;
3817}
3818
3819static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003820new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003821{
3822 /* buf must be %d:%d\n? giving major and minor numbers */
3823 /* The new device is added to the array.
3824 * If the array has a persistent superblock, we read the
3825 * superblock to initialise info and check validity.
3826 * Otherwise, only checking done is that in bind_rdev_to_array,
3827 * which mainly checks size.
3828 */
3829 char *e;
3830 int major = simple_strtoul(buf, &e, 10);
3831 int minor;
3832 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003833 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003834 int err;
3835
3836 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3837 return -EINVAL;
3838 minor = simple_strtoul(e+1, &e, 10);
3839 if (*e && *e != '\n')
3840 return -EINVAL;
3841 dev = MKDEV(major, minor);
3842 if (major != MAJOR(dev) ||
3843 minor != MINOR(dev))
3844 return -EOVERFLOW;
3845
NeilBrown6d7ff7382006-01-06 00:21:16 -08003846 if (mddev->persistent) {
3847 rdev = md_import_device(dev, mddev->major_version,
3848 mddev->minor_version);
3849 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003850 struct md_rdev *rdev0
3851 = list_entry(mddev->disks.next,
3852 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08003853 err = super_types[mddev->major_version]
3854 .load_super(rdev, rdev0, mddev->minor_version);
3855 if (err < 0)
3856 goto out;
3857 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003858 } else if (mddev->external)
3859 rdev = md_import_device(dev, -2, -1);
3860 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003861 rdev = md_import_device(dev, -1, -1);
3862
3863 if (IS_ERR(rdev))
3864 return PTR_ERR(rdev);
3865 err = bind_rdev_to_array(rdev, mddev);
3866 out:
3867 if (err)
3868 export_rdev(rdev);
3869 return err ? err : len;
3870}
3871
3872static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003873__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003874
3875static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003876bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07003877{
3878 char *end;
3879 unsigned long chunk, end_chunk;
3880
3881 if (!mddev->bitmap)
3882 goto out;
3883 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3884 while (*buf) {
3885 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3886 if (buf == end) break;
3887 if (*end == '-') { /* range */
3888 buf = end + 1;
3889 end_chunk = simple_strtoul(buf, &end, 0);
3890 if (buf == end) break;
3891 }
3892 if (*end && !isspace(*end)) break;
3893 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003894 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003895 }
3896 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3897out:
3898 return len;
3899}
3900
3901static struct md_sysfs_entry md_bitmap =
3902__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3903
3904static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003905size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08003906{
Andre Noll58c0fed2009-03-31 14:33:13 +11003907 return sprintf(page, "%llu\n",
3908 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003909}
3910
NeilBrownfd01b882011-10-11 16:47:53 +11003911static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003912
3913static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003914size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08003915{
3916 /* If array is inactive, we can reduce the component size, but
3917 * not increase it (except from 0).
3918 * If array is active, we can try an on-line resize
3919 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003920 sector_t sectors;
3921 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003922
Andre Noll58c0fed2009-03-31 14:33:13 +11003923 if (err < 0)
3924 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003925 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003926 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003927 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003928 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003929 if (mddev->dev_sectors == 0 ||
3930 mddev->dev_sectors > sectors)
3931 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003932 else
3933 err = -ENOSPC;
3934 }
3935 return err ? err : len;
3936}
3937
3938static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003939__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003940
Masanari Iida83f0d772012-10-30 00:18:08 +09003941/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08003942 * This is one of
3943 * 'none' for arrays with no metadata (good luck...)
3944 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003945 * or N.M for internally known formats
3946 */
3947static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003948metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003949{
3950 if (mddev->persistent)
3951 return sprintf(page, "%d.%d\n",
3952 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003953 else if (mddev->external)
3954 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003955 else
3956 return sprintf(page, "none\n");
3957}
3958
3959static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003960metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003961{
3962 int major, minor;
3963 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003964 /* Changing the details of 'external' metadata is
3965 * always permitted. Otherwise there must be
3966 * no devices attached to the array.
3967 */
3968 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3969 ;
3970 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003971 return -EBUSY;
3972
3973 if (cmd_match(buf, "none")) {
3974 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003975 mddev->external = 0;
3976 mddev->major_version = 0;
3977 mddev->minor_version = 90;
3978 return len;
3979 }
3980 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003981 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003982 if (namelen >= sizeof(mddev->metadata_type))
3983 namelen = sizeof(mddev->metadata_type)-1;
3984 strncpy(mddev->metadata_type, buf+9, namelen);
3985 mddev->metadata_type[namelen] = 0;
3986 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3987 mddev->metadata_type[--namelen] = 0;
3988 mddev->persistent = 0;
3989 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003990 mddev->major_version = 0;
3991 mddev->minor_version = 90;
3992 return len;
3993 }
3994 major = simple_strtoul(buf, &e, 10);
3995 if (e==buf || *e != '.')
3996 return -EINVAL;
3997 buf = e+1;
3998 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003999 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004000 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004001 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004002 return -ENOENT;
4003 mddev->major_version = major;
4004 mddev->minor_version = minor;
4005 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004006 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004007 return len;
4008}
4009
4010static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004011__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004012
NeilBrowna35b0d62006-01-06 00:20:49 -08004013static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004014action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004015{
NeilBrown7eec3142005-11-08 21:39:44 -08004016 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004017 unsigned long recovery = mddev->recovery;
4018 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004019 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004020 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4021 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4022 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004023 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004024 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4025 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004026 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004027 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004028 type = "check";
4029 else
4030 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004031 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004032 type = "recover";
4033 }
4034 return sprintf(page, "%s\n", type);
4035}
4036
4037static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004038action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004039{
NeilBrown7eec3142005-11-08 21:39:44 -08004040 if (!mddev->pers || !mddev->pers->sync_request)
4041 return -EINVAL;
4042
NeilBrownb6a9ce62009-05-26 09:41:17 +10004043 if (cmd_match(page, "frozen"))
4044 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4045 else
4046 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4047
4048 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrownf851b602014-12-11 10:02:10 +11004049 flush_workqueue(md_misc_wq);
NeilBrown7eec3142005-11-08 21:39:44 -08004050 if (mddev->sync_thread) {
4051 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10004052 md_reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004053 }
NeilBrown03c902e2006-01-06 00:20:46 -08004054 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4055 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004056 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004057 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004058 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004059 else if (cmd_match(page, "recover")) {
4060 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4061 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4062 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004063 int err;
4064 if (mddev->pers->start_reshape == NULL)
4065 return -EINVAL;
4066 err = mddev->pers->start_reshape(mddev);
4067 if (err)
4068 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004069 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004070 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004071 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004072 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004073 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004074 return -EINVAL;
4075 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4076 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004077 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004078 if (mddev->ro == 2) {
4079 /* A write to sync_action is enough to justify
4080 * canceling read-auto mode
4081 */
4082 mddev->ro = 0;
4083 md_wakeup_thread(mddev->sync_thread);
4084 }
NeilBrown03c902e2006-01-06 00:20:46 -08004085 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004086 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004087 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004088 return len;
4089}
4090
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004091static struct md_sysfs_entry md_scan_mode =
4092__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4093
4094static ssize_t
4095last_sync_action_show(struct mddev *mddev, char *page)
4096{
4097 return sprintf(page, "%s\n", mddev->last_sync_action);
4098}
4099
4100static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4101
NeilBrown9d888832005-11-08 21:39:26 -08004102static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004103mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004104{
4105 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004106 (unsigned long long)
4107 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004108}
4109
NeilBrown80ca3a42006-07-10 04:44:18 -07004110static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004111
NeilBrown88202a02006-01-06 00:21:36 -08004112static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004113sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004114{
4115 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4116 mddev->sync_speed_min ? "local": "system");
4117}
4118
4119static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004120sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004121{
4122 int min;
4123 char *e;
4124 if (strncmp(buf, "system", 6)==0) {
4125 mddev->sync_speed_min = 0;
4126 return len;
4127 }
4128 min = simple_strtoul(buf, &e, 10);
4129 if (buf == e || (*e && *e != '\n') || min <= 0)
4130 return -EINVAL;
4131 mddev->sync_speed_min = min;
4132 return len;
4133}
4134
4135static struct md_sysfs_entry md_sync_min =
4136__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4137
4138static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004139sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004140{
4141 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4142 mddev->sync_speed_max ? "local": "system");
4143}
4144
4145static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004146sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004147{
4148 int max;
4149 char *e;
4150 if (strncmp(buf, "system", 6)==0) {
4151 mddev->sync_speed_max = 0;
4152 return len;
4153 }
4154 max = simple_strtoul(buf, &e, 10);
4155 if (buf == e || (*e && *e != '\n') || max <= 0)
4156 return -EINVAL;
4157 mddev->sync_speed_max = max;
4158 return len;
4159}
4160
4161static struct md_sysfs_entry md_sync_max =
4162__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4163
Iustin Popd7f3d292007-10-16 23:30:54 -07004164static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004165degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004166{
4167 return sprintf(page, "%d\n", mddev->degraded);
4168}
4169static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004170
4171static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004172sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004173{
4174 return sprintf(page, "%d\n", mddev->parallel_resync);
4175}
4176
4177static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004178sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004179{
4180 long n;
4181
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004182 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004183 return -EINVAL;
4184
4185 if (n != 0 && n != 1)
4186 return -EINVAL;
4187
4188 mddev->parallel_resync = n;
4189
4190 if (mddev->sync_thread)
4191 wake_up(&resync_wait);
4192
4193 return len;
4194}
4195
4196/* force parallel resync, even with shared block devices */
4197static struct md_sysfs_entry md_sync_force_parallel =
4198__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4199 sync_force_parallel_show, sync_force_parallel_store);
4200
4201static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004202sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004203{
4204 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004205 if (mddev->curr_resync == 0)
4206 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004207 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4208 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004209 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004210 db = resync - mddev->resync_mark_cnt;
4211 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004212}
4213
NeilBrown80ca3a42006-07-10 04:44:18 -07004214static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004215
4216static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004217sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004218{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004219 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004220
NeilBrownacb180b2009-04-14 16:28:34 +10004221 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4222 return sprintf(page, "none\n");
4223
NeilBrown72f36d52012-10-11 14:25:57 +11004224 if (mddev->curr_resync == 1 ||
4225 mddev->curr_resync == 2)
4226 return sprintf(page, "delayed\n");
4227
NeilBrownc804cde2012-05-21 09:28:33 +10004228 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4229 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004230 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004231 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004232 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004233
NeilBrownacb180b2009-04-14 16:28:34 +10004234 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004235 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004236}
4237
NeilBrown80ca3a42006-07-10 04:44:18 -07004238static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004239
NeilBrowne464eaf2006-03-27 01:18:14 -08004240static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004241min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004242{
4243 return sprintf(page, "%llu\n",
4244 (unsigned long long)mddev->resync_min);
4245}
4246static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004247min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004248{
4249 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004250 int err;
4251 int chunk;
4252
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004253 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004254 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004255
4256 spin_lock(&mddev->lock);
4257 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004258 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004259 goto out_unlock;
4260
4261 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004262 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004263 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004264
4265 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004266 chunk = mddev->chunk_sectors;
4267 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004268 sector_t temp = min;
NeilBrown23da4222014-12-15 12:57:01 +11004269
4270 err = -EINVAL;
4271 if (sector_div(temp, chunk))
4272 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004273 }
4274 mddev->resync_min = min;
NeilBrown23da4222014-12-15 12:57:01 +11004275 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004276
NeilBrown23da4222014-12-15 12:57:01 +11004277out_unlock:
4278 spin_unlock(&mddev->lock);
4279 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004280}
4281
4282static struct md_sysfs_entry md_min_sync =
4283__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4284
4285static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004286max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004287{
4288 if (mddev->resync_max == MaxSector)
4289 return sprintf(page, "max\n");
4290 else
4291 return sprintf(page, "%llu\n",
4292 (unsigned long long)mddev->resync_max);
4293}
4294static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004295max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004296{
NeilBrown23da4222014-12-15 12:57:01 +11004297 int err;
4298 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004299 if (strncmp(buf, "max", 3) == 0)
4300 mddev->resync_max = MaxSector;
4301 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004302 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004303 int chunk;
4304
4305 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004306 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004307 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004308 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004309 goto out_unlock;
4310
4311 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004312 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004313 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004314 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004315 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004316
4317 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004318 chunk = mddev->chunk_sectors;
4319 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004320 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004321
4322 err = -EINVAL;
4323 if (sector_div(temp, chunk))
4324 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004325 }
4326 mddev->resync_max = max;
4327 }
4328 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004329 err = 0;
4330out_unlock:
4331 spin_unlock(&mddev->lock);
4332 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004333}
4334
4335static struct md_sysfs_entry md_max_sync =
4336__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4337
4338static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004339suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004340{
4341 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4342}
4343
4344static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004345suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004346{
4347 char *e;
4348 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004349 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004350
NeilBrownf72ffdd2014-09-30 14:23:59 +10004351 if (mddev->pers == NULL ||
NeilBrownb8d966e2009-07-01 11:14:04 +10004352 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004353 return -EINVAL;
4354 if (buf == e || (*e && *e != '\n'))
4355 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004356
4357 mddev->suspend_lo = new;
4358 if (new >= old)
4359 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004360 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004361 else {
4362 /* Expanding suspended region - need to wait */
4363 mddev->pers->quiesce(mddev, 1);
4364 mddev->pers->quiesce(mddev, 0);
4365 }
4366 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004367}
4368static struct md_sysfs_entry md_suspend_lo =
4369__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4370
NeilBrowne464eaf2006-03-27 01:18:14 -08004371static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004372suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004373{
4374 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4375}
4376
4377static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004378suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004379{
4380 char *e;
4381 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004382 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004383
NeilBrownb8d966e2009-07-01 11:14:04 +10004384 if (mddev->pers == NULL ||
4385 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004386 return -EINVAL;
4387 if (buf == e || (*e && *e != '\n'))
4388 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004389
4390 mddev->suspend_hi = new;
4391 if (new <= old)
4392 /* Shrinking suspended region */
4393 mddev->pers->quiesce(mddev, 2);
4394 else {
4395 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004396 mddev->pers->quiesce(mddev, 1);
4397 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004398 }
4399 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004400}
4401static struct md_sysfs_entry md_suspend_hi =
4402__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4403
NeilBrown08a02ec2007-05-09 02:35:38 -07004404static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004405reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004406{
4407 if (mddev->reshape_position != MaxSector)
4408 return sprintf(page, "%llu\n",
4409 (unsigned long long)mddev->reshape_position);
4410 strcpy(page, "none\n");
4411 return 5;
4412}
4413
4414static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004415reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004416{
NeilBrownc6563a82012-05-21 09:27:00 +10004417 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004418 char *e;
4419 unsigned long long new = simple_strtoull(buf, &e, 10);
4420 if (mddev->pers)
4421 return -EBUSY;
4422 if (buf == e || (*e && *e != '\n'))
4423 return -EINVAL;
4424 mddev->reshape_position = new;
4425 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004426 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004427 mddev->new_level = mddev->level;
4428 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004429 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004430 rdev_for_each(rdev, mddev)
4431 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004432 return len;
4433}
4434
4435static struct md_sysfs_entry md_reshape_position =
4436__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4437 reshape_position_store);
4438
Dan Williamsb522adc2009-03-31 15:00:31 +11004439static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004440reshape_direction_show(struct mddev *mddev, char *page)
4441{
4442 return sprintf(page, "%s\n",
4443 mddev->reshape_backwards ? "backwards" : "forwards");
4444}
4445
4446static ssize_t
4447reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4448{
4449 int backwards = 0;
4450 if (cmd_match(buf, "forwards"))
4451 backwards = 0;
4452 else if (cmd_match(buf, "backwards"))
4453 backwards = 1;
4454 else
4455 return -EINVAL;
4456 if (mddev->reshape_backwards == backwards)
4457 return len;
4458
4459 /* check if we are allowed to change */
4460 if (mddev->delta_disks)
4461 return -EBUSY;
4462
4463 if (mddev->persistent &&
4464 mddev->major_version == 0)
4465 return -EINVAL;
4466
4467 mddev->reshape_backwards = backwards;
4468 return len;
4469}
4470
4471static struct md_sysfs_entry md_reshape_direction =
4472__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4473 reshape_direction_store);
4474
4475static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004476array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004477{
4478 if (mddev->external_size)
4479 return sprintf(page, "%llu\n",
4480 (unsigned long long)mddev->array_sectors/2);
4481 else
4482 return sprintf(page, "default\n");
4483}
4484
4485static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004486array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004487{
4488 sector_t sectors;
4489
4490 if (strncmp(buf, "default", 7) == 0) {
4491 if (mddev->pers)
4492 sectors = mddev->pers->size(mddev, 0, 0);
4493 else
4494 sectors = mddev->array_sectors;
4495
4496 mddev->external_size = 0;
4497 } else {
4498 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4499 return -EINVAL;
4500 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004501 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004502
4503 mddev->external_size = 1;
4504 }
4505
4506 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004507 if (mddev->pers) {
4508 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004509 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004510 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004511 return len;
4512}
4513
4514static struct md_sysfs_entry md_array_size =
4515__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4516 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004517
NeilBrowneae17012005-11-08 21:39:23 -08004518static struct attribute *md_default_attrs[] = {
4519 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004520 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004521 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004522 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004523 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004524 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004525 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004526 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004527 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004528 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004529 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004530 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004531 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004532 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004533 NULL,
4534};
4535
4536static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004537 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004538 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004539 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004540 &md_sync_min.attr,
4541 &md_sync_max.attr,
4542 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004543 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004544 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004545 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004546 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004547 &md_suspend_lo.attr,
4548 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004549 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004550 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004551 NULL,
4552};
NeilBrown411036f2005-11-08 21:39:40 -08004553static struct attribute_group md_redundancy_group = {
4554 .name = NULL,
4555 .attrs = md_redundancy_attrs,
4556};
4557
NeilBrowneae17012005-11-08 21:39:23 -08004558static ssize_t
4559md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4560{
4561 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004562 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004563 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004564
4565 if (!entry->show)
4566 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004567 spin_lock(&all_mddevs_lock);
4568 if (list_empty(&mddev->all_mddevs)) {
4569 spin_unlock(&all_mddevs_lock);
4570 return -EBUSY;
4571 }
4572 mddev_get(mddev);
4573 spin_unlock(&all_mddevs_lock);
4574
NeilBrownb7b17c92014-12-15 12:56:59 +11004575 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004576 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004577 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004578}
4579
4580static ssize_t
4581md_attr_store(struct kobject *kobj, struct attribute *attr,
4582 const char *page, size_t length)
4583{
4584 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004585 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004586 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004587
4588 if (!entry->store)
4589 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004590 if (!capable(CAP_SYS_ADMIN))
4591 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004592 spin_lock(&all_mddevs_lock);
4593 if (list_empty(&mddev->all_mddevs)) {
4594 spin_unlock(&all_mddevs_lock);
4595 return -EBUSY;
4596 }
4597 mddev_get(mddev);
4598 spin_unlock(&all_mddevs_lock);
NeilBrowna7a3f082012-12-11 13:35:54 +11004599 if (entry->store == new_dev_store)
4600 flush_workqueue(md_misc_wq);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004601 rv = mddev_lock(mddev);
4602 if (!rv) {
4603 rv = entry->store(mddev, page, length);
4604 mddev_unlock(mddev);
4605 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004606 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004607 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004608}
4609
4610static void md_free(struct kobject *ko)
4611{
NeilBrownfd01b882011-10-11 16:47:53 +11004612 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004613
4614 if (mddev->sysfs_state)
4615 sysfs_put(mddev->sysfs_state);
4616
4617 if (mddev->gendisk) {
4618 del_gendisk(mddev->gendisk);
4619 put_disk(mddev->gendisk);
4620 }
4621 if (mddev->queue)
4622 blk_cleanup_queue(mddev->queue);
4623
NeilBrowneae17012005-11-08 21:39:23 -08004624 kfree(mddev);
4625}
4626
Emese Revfy52cf25d2010-01-19 02:58:23 +01004627static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004628 .show = md_attr_show,
4629 .store = md_attr_store,
4630};
4631static struct kobj_type md_ktype = {
4632 .release = md_free,
4633 .sysfs_ops = &md_sysfs_ops,
4634 .default_attrs = md_default_attrs,
4635};
4636
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637int mdp_major = 0;
4638
Dan Williams5fd3a172009-03-04 00:57:25 -07004639static void mddev_delayed_delete(struct work_struct *ws)
4640{
NeilBrownfd01b882011-10-11 16:47:53 +11004641 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004642
NeilBrown43a70502009-12-14 12:49:55 +11004643 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004644 kobject_del(&mddev->kobj);
4645 kobject_put(&mddev->kobj);
4646}
4647
NeilBrownefeb53c2009-01-09 08:31:10 +11004648static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004650 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004651 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004653 int partitioned;
4654 int shift;
4655 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004656 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657
4658 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004659 return -ENODEV;
4660
4661 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4662 shift = partitioned ? MdpMinorShift : 0;
4663 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
Tejun Heoe804ac72010-10-15 15:36:08 +02004665 /* wait for any previous instance of this device to be
4666 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004667 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004668 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004669
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004670 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004671 error = -EEXIST;
4672 if (mddev->gendisk)
4673 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004674
4675 if (name) {
4676 /* Need to ensure that 'name' is not a duplicate.
4677 */
NeilBrownfd01b882011-10-11 16:47:53 +11004678 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004679 spin_lock(&all_mddevs_lock);
4680
4681 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4682 if (mddev2->gendisk &&
4683 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4684 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004685 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004686 }
4687 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 }
NeilBrown8b765392009-01-09 08:31:08 +11004689
NeilBrown0909dc42009-07-01 12:27:21 +10004690 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004691 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004692 if (!mddev->queue)
4693 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004694 mddev->queue->queuedata = mddev;
4695
NeilBrown409c57f2009-03-31 14:39:39 +11004696 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004697 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004698
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 disk = alloc_disk(1 << shift);
4700 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004701 blk_cleanup_queue(mddev->queue);
4702 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004703 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004705 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004707 if (name)
4708 strcpy(disk->disk_name, name);
4709 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004711 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 disk->fops = &md_fops;
4714 disk->private_data = mddev;
4715 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004716 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004717 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004718 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004719 * remove it now.
4720 */
4721 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004723 /* As soon as we call add_disk(), another thread could get
4724 * through to md_open, so make sure it doesn't get too far
4725 */
4726 mutex_lock(&mddev->open_mutex);
4727 add_disk(disk);
4728
Tejun Heoed9e1982008-08-25 19:56:05 +09004729 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4730 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004731 if (error) {
4732 /* This isn't possible, but as kobject_init_and_add is marked
4733 * __must_check, we must do something with the result
4734 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004735 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4736 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004737 error = 0;
4738 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004739 if (mddev->kobj.sd &&
4740 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004741 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004742 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004743 abort:
4744 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004745 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004746 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004747 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004748 }
NeilBrownd3374822009-01-09 08:31:10 +11004749 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004750 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004751}
4752
4753static struct kobject *md_probe(dev_t dev, int *part, void *data)
4754{
4755 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 return NULL;
4757}
4758
NeilBrownefeb53c2009-01-09 08:31:10 +11004759static int add_named_array(const char *val, struct kernel_param *kp)
4760{
4761 /* val must be "md_*" where * is not all digits.
4762 * We allocate an array with a large free minor number, and
4763 * set the name to val. val must not already be an active name.
4764 */
4765 int len = strlen(val);
4766 char buf[DISK_NAME_LEN];
4767
4768 while (len && val[len-1] == '\n')
4769 len--;
4770 if (len >= DISK_NAME_LEN)
4771 return -E2BIG;
4772 strlcpy(buf, val, len+1);
4773 if (strncmp(buf, "md_", 3) != 0)
4774 return -EINVAL;
4775 return md_alloc(0, buf);
4776}
4777
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778static void md_safemode_timeout(unsigned long data)
4779{
NeilBrownfd01b882011-10-11 16:47:53 +11004780 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
Neil Brown0fd62b82008-06-28 08:31:36 +10004782 if (!atomic_read(&mddev->writes_pending)) {
4783 mddev->safemode = 1;
4784 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004785 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 md_wakeup_thread(mddev->thread);
4788}
4789
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004790static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
NeilBrownfd01b882011-10-11 16:47:53 +11004792int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793{
NeilBrown2604b702006-01-06 00:20:36 -08004794 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004795 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004796 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
NeilBrowna757e642005-04-16 15:26:42 -07004798 if (list_empty(&mddev->disks))
4799 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
4802 if (mddev->pers)
4803 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004804 /* Cannot run until previous stop completes properly */
4805 if (mddev->sysfs_active)
4806 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004807
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 /*
4809 * Analyze all RAID superblock(s)
4810 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004811 if (!mddev->raid_disks) {
4812 if (!mddev->persistent)
4813 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004814 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
NeilBrownd9d166c2006-01-06 00:20:51 -08004817 if (mddev->level != LEVEL_NONE)
4818 request_module("md-level-%d", mddev->level);
4819 else if (mddev->clevel[0])
4820 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
4822 /*
4823 * Drop all container device buffers, from now on
4824 * the only valid external interface is through the md
4825 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004827 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004828 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 continue;
4830 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004831 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004832
4833 /* perform some consistency tests on the device.
4834 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004835 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004836 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004837 if (rdev->meta_bdev) {
4838 /* Nothing to check */;
4839 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004840 if (mddev->dev_sectors &&
4841 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004842 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004843 printk("md: %s: data overlaps metadata\n",
4844 mdname(mddev));
4845 return -EINVAL;
4846 }
4847 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004848 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004849 > rdev->data_offset) {
4850 printk("md: %s: metadata overlaps data\n",
4851 mdname(mddev));
4852 return -EINVAL;
4853 }
4854 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004855 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 }
4857
NeilBrowna167f662010-10-26 18:31:13 +11004858 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07004859 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11004860
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004862 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004863 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004865 if (mddev->level != LEVEL_NONE)
4866 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4867 mddev->level);
4868 else
4869 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4870 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 return -EINVAL;
4872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004874 if (mddev->level != pers->level) {
4875 mddev->level = pers->level;
4876 mddev->new_level = pers->level;
4877 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004878 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
NeilBrownf6705572006-03-27 01:18:11 -08004880 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004881 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004882 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08004883 module_put(pers->owner);
4884 return -EINVAL;
4885 }
4886
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004887 if (pers->sync_request) {
4888 /* Warn if this is a potentially silly
4889 * configuration.
4890 */
4891 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11004892 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004893 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004894
NeilBrowndafb20f2012-03-19 12:46:39 +11004895 rdev_for_each(rdev, mddev)
4896 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004897 if (rdev < rdev2 &&
4898 rdev->bdev->bd_contains ==
4899 rdev2->bdev->bd_contains) {
4900 printk(KERN_WARNING
4901 "%s: WARNING: %s appears to be"
4902 " on the same physical disk as"
4903 " %s.\n",
4904 mdname(mddev),
4905 bdevname(rdev->bdev,b),
4906 bdevname(rdev2->bdev,b2));
4907 warned = 1;
4908 }
4909 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004910
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004911 if (warned)
4912 printk(KERN_WARNING
4913 "True protection against single-disk"
4914 " failure might be compromised.\n");
4915 }
4916
NeilBrown657390d2005-08-26 18:34:16 -07004917 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004918 /* may be over-ridden by personality */
4919 mddev->resync_max_sectors = mddev->dev_sectors;
4920
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004921 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
NeilBrown0f9552b52009-12-30 12:08:50 +11004923 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004924 mddev->ro = 2; /* read-only, but switch on first write */
4925
NeilBrown36d091f2014-12-15 12:56:58 +11004926 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004927 if (err)
4928 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11004929 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11004930 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4931 " but 'external_size' not in effect?\n", __func__);
4932 printk(KERN_ERR
4933 "md: invalid array_size %llu > default size %llu\n",
4934 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11004935 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11004936 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11004937 }
NeilBrown36d091f2014-12-15 12:56:58 +11004938 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10004939 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004940 err = bitmap_create(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11004941 if (err)
NeilBrownb15c2e52006-01-06 00:20:16 -08004942 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4943 mdname(mddev), err);
NeilBrownb15c2e52006-01-06 00:20:16 -08004944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11004946 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11004947 pers->free(mddev, mddev->private);
4948 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07004949 bitmap_destroy(mddev);
4950 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 }
NeilBrown5c675f82014-12-15 12:56:56 +11004952 if (mddev->queue) {
4953 mddev->queue->backing_dev_info.congested_data = mddev;
4954 mddev->queue->backing_dev_info.congested_fn = md_congested;
NeilBrown64590f42014-12-15 12:56:57 +11004955 blk_queue_merge_bvec(mddev->queue, md_mergeable_bvec);
NeilBrown5c675f82014-12-15 12:56:56 +11004956 }
NeilBrown36d091f2014-12-15 12:56:58 +11004957 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004958 if (mddev->kobj.sd &&
4959 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004960 printk(KERN_WARNING
4961 "md: cannot register extra attributes for %s\n",
4962 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004963 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004964 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004965 mddev->ro = 0;
4966
NeilBrownf72ffdd2014-09-30 14:23:59 +10004967 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004968 atomic_set(&mddev->max_corr_read_errors,
4969 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 mddev->safemode = 0;
4971 mddev->safemode_timer.function = md_safemode_timeout;
4972 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004973 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004975 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11004976 spin_lock(&mddev->lock);
4977 mddev->pers = pers;
NeilBrown0ca69882011-01-14 09:14:33 +11004978 mddev->ready = 1;
NeilBrown36d091f2014-12-15 12:56:58 +11004979 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11004980 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10004981 if (rdev->raid_disk >= 0)
4982 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004983 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10004984
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10004986
NeilBrown7a0a5352013-08-27 16:28:23 +10004987 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07004988 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
NeilBrownd7603b72006-01-06 00:20:30 -08004990 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004991 sysfs_notify_dirent_safe(mddev->sysfs_state);
4992 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004993 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 return 0;
4995}
NeilBrown390ee602010-06-01 19:37:27 +10004996EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997
NeilBrownfd01b882011-10-11 16:47:53 +11004998static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11004999{
5000 int err;
5001
5002 err = md_run(mddev);
5003 if (err)
5004 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005005 err = bitmap_load(mddev);
5006 if (err) {
5007 bitmap_destroy(mddev);
5008 goto out;
5009 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005010
5011 md_wakeup_thread(mddev->thread);
5012 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5013
NeilBrownfe60b012010-03-29 11:10:42 +11005014 set_capacity(mddev->gendisk, mddev->array_sectors);
5015 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005016 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005017 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5018out:
5019 return err;
5020}
5021
NeilBrownfd01b882011-10-11 16:47:53 +11005022static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023{
5024 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
Andre Noll80fab1d2008-07-11 22:02:21 +10005026 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005028 return -ENXIO;
5029 if (!mddev->pers)
5030 return -EINVAL;
5031 if (!mddev->ro)
5032 return -EBUSY;
5033 mddev->safemode = 0;
5034 mddev->ro = 0;
5035 set_disk_ro(disk, 0);
5036 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5037 mdname(mddev));
5038 /* Kick recovery or resync if necessary */
5039 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5040 md_wakeup_thread(mddev->thread);
5041 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005042 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005043 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044}
5045
NeilBrownfd01b882011-10-11 16:47:53 +11005046static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005047{
5048 mddev->array_sectors = 0;
5049 mddev->external_size = 0;
5050 mddev->dev_sectors = 0;
5051 mddev->raid_disks = 0;
5052 mddev->recovery_cp = 0;
5053 mddev->resync_min = 0;
5054 mddev->resync_max = MaxSector;
5055 mddev->reshape_position = MaxSector;
5056 mddev->external = 0;
5057 mddev->persistent = 0;
5058 mddev->level = LEVEL_NONE;
5059 mddev->clevel[0] = 0;
5060 mddev->flags = 0;
5061 mddev->ro = 0;
5062 mddev->metadata_type[0] = 0;
5063 mddev->chunk_sectors = 0;
5064 mddev->ctime = mddev->utime = 0;
5065 mddev->layout = 0;
5066 mddev->max_disks = 0;
5067 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005068 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005069 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005070 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005071 mddev->new_level = LEVEL_NONE;
5072 mddev->new_layout = 0;
5073 mddev->new_chunk_sectors = 0;
5074 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005075 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005076 mddev->suspend_lo = mddev->suspend_hi = 0;
5077 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5078 mddev->recovery = 0;
5079 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005080 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005081 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005082 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005083 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005084 mddev->bitmap_info.offset = 0;
5085 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005086 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005087 mddev->bitmap_info.chunksize = 0;
5088 mddev->bitmap_info.daemon_sleep = 0;
5089 mddev->bitmap_info.max_write_behind = 0;
5090}
5091
NeilBrownfd01b882011-10-11 16:47:53 +11005092static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005093{
NeilBrown6b6204e2013-05-09 09:48:30 +10005094 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005095 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005096 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005097 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005098 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005099 }
5100
5101 del_timer_sync(&mddev->safemode_timer);
5102
5103 bitmap_flush(mddev);
5104 md_super_wait(mddev);
5105
NeilBrownb6d428c2013-04-24 11:42:42 +10005106 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005107 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005108 /* mark array as shutdown cleanly */
5109 mddev->in_sync = 1;
5110 md_update_sb(mddev, 1);
5111 }
5112}
NeilBrowndefad612011-01-14 09:14:33 +11005113
NeilBrownfd01b882011-10-11 16:47:53 +11005114void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005115{
NeilBrown29f097c2013-11-14 17:54:51 +11005116 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005117 __md_stop_writes(mddev);
5118 mddev_unlock(mddev);
5119}
NeilBrown390ee602010-06-01 19:37:27 +10005120EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005121
NeilBrown5aa61f42014-12-15 12:56:57 +11005122static void mddev_detach(struct mddev *mddev)
5123{
5124 struct bitmap *bitmap = mddev->bitmap;
5125 /* wait for behind writes to complete */
5126 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5127 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5128 mdname(mddev));
5129 /* need to kick something here to make sure I/O goes? */
5130 wait_event(bitmap->behind_wait,
5131 atomic_read(&bitmap->behind_writes) == 0);
5132 }
NeilBrown36d091f2014-12-15 12:56:58 +11005133 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005134 mddev->pers->quiesce(mddev, 1);
5135 mddev->pers->quiesce(mddev, 0);
5136 }
5137 md_unregister_thread(&mddev->thread);
5138 if (mddev->queue)
5139 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5140}
5141
NeilBrown5eff3c42012-11-19 10:47:48 +11005142static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005143{
NeilBrown36d091f2014-12-15 12:56:58 +11005144 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005145 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005146 spin_lock(&mddev->lock);
5147 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005148 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005149 spin_unlock(&mddev->lock);
5150 pers->free(mddev, mddev->private);
5151 if (pers->sync_request && mddev->to_remove == NULL)
5152 mddev->to_remove = &md_redundancy_group;
5153 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005154 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005155}
NeilBrown5eff3c42012-11-19 10:47:48 +11005156
5157void md_stop(struct mddev *mddev)
5158{
5159 /* stop the array and free an attached data structures.
5160 * This is called from dm-raid
5161 */
5162 __md_stop(mddev);
5163 bitmap_destroy(mddev);
5164 if (mddev->bio_set)
5165 bioset_free(mddev->bio_set);
5166}
5167
NeilBrown390ee602010-06-01 19:37:27 +10005168EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005169
NeilBrowna05b7ea2012-07-19 15:59:18 +10005170static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005171{
5172 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005173 int did_freeze = 0;
5174
5175 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5176 did_freeze = 1;
5177 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5178 md_wakeup_thread(mddev->thread);
5179 }
NeilBrownf851b602014-12-11 10:02:10 +11005180 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005181 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005182 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005183 /* Thread might be blocked waiting for metadata update
5184 * which will now never happen */
5185 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005186
NeilBrown30b8feb2013-11-14 15:16:17 +11005187 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005188 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5189 &mddev->recovery));
NeilBrown30b8feb2013-11-14 15:16:17 +11005190 mddev_lock_nointr(mddev);
5191
NeilBrowna4bd82d2010-03-29 13:23:10 +11005192 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005193 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005194 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005195 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005196 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005197 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005198 if (did_freeze) {
5199 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005200 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005201 md_wakeup_thread(mddev->thread);
5202 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005203 err = -EBUSY;
5204 goto out;
5205 }
5206 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005207 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005208
5209 err = -ENXIO;
5210 if (mddev->ro==1)
5211 goto out;
5212 mddev->ro = 1;
5213 set_disk_ro(mddev->gendisk, 1);
5214 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005215 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5216 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005217 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005218 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005219 }
5220out:
5221 mutex_unlock(&mddev->open_mutex);
5222 return err;
5223}
5224
NeilBrown9e653b62006-06-26 00:27:58 -07005225/* mode:
5226 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005227 * 2 - stop but do not disassemble array
5228 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005229static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005230 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005233 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005234 int did_freeze = 0;
5235
5236 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5237 did_freeze = 1;
5238 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5239 md_wakeup_thread(mddev->thread);
5240 }
NeilBrownf851b602014-12-11 10:02:10 +11005241 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005242 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005243 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005244 /* Thread might be blocked waiting for metadata update
5245 * which will now never happen */
5246 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005247
NeilBrown30b8feb2013-11-14 15:16:17 +11005248 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005249 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5250 !test_bit(MD_RECOVERY_RUNNING,
5251 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005252 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
NeilBrownc8c00a62009-08-10 12:50:52 +10005254 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005255 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005256 mddev->sysfs_active ||
5257 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005258 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005259 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005260 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005261 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005262 if (did_freeze) {
5263 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005264 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005265 md_wakeup_thread(mddev->thread);
5266 }
NeilBrown260fa032013-08-27 16:44:13 +10005267 return -EBUSY;
5268 }
NeilBrown6e17b022010-08-07 21:41:19 +10005269 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005270 if (mddev->ro)
5271 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005272
NeilBrowndefad612011-01-14 09:14:33 +11005273 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005274 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005275 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005276 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005277
NeilBrowna4bd82d2010-03-29 13:23:10 +11005278 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005279 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005280
NeilBrowndafb20f2012-03-19 12:46:39 +11005281 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005282 if (rdev->raid_disk >= 0)
5283 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005284
NeilBrowna4bd82d2010-03-29 13:23:10 +11005285 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005286 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005287 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005288 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005289
NeilBrowna4bd82d2010-03-29 13:23:10 +11005290 if (mddev->ro)
5291 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005292 } else
5293 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 /*
5295 * Free resources if final stop
5296 */
NeilBrown9e653b62006-06-26 00:27:58 -07005297 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5299
NeilBrown978f9462006-02-02 14:28:05 -08005300 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005301 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005302 struct file *f = mddev->bitmap_info.file;
5303 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005304 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005305 spin_unlock(&mddev->lock);
5306 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005307 }
NeilBrownc3d97142009-12-14 12:49:52 +11005308 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005309
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 export_array(mddev);
5311
NeilBrown6177b472010-03-29 11:37:13 +11005312 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005313 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005314 if (mddev->hold_active == UNTIL_STOP)
5315 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005316 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005317 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005318 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005319 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321}
5322
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005323#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005324static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325{
NeilBrown3cb03002011-10-11 16:45:26 +11005326 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 int err;
5328
NeilBrowna757e642005-04-16 15:26:42 -07005329 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331
5332 printk(KERN_INFO "md: running: ");
5333
NeilBrowndafb20f2012-03-19 12:46:39 +11005334 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 char b[BDEVNAME_SIZE];
5336 printk("<%s>", bdevname(rdev->bdev,b));
5337 }
5338 printk("\n");
5339
NeilBrownd710e132008-10-13 11:55:12 +11005340 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 if (err) {
5342 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005343 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 }
5345}
5346
5347/*
5348 * lets try to run arrays based on all disks that have arrived
5349 * until now. (those are in pending_raid_disks)
5350 *
5351 * the method: pick the first pending disk, collect all disks with
5352 * the same UUID, remove all from the pending list and put them into
5353 * the 'same_array' list. Then order this list based on superblock
5354 * update time (freshest comes first), kick out 'old' disks and
5355 * compare superblocks. If everything's fine then run it.
5356 *
5357 * If "unit" is allocated, then bump its reference count
5358 */
5359static void autorun_devices(int part)
5360{
NeilBrown3cb03002011-10-11 16:45:26 +11005361 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005362 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 char b[BDEVNAME_SIZE];
5364
5365 printk(KERN_INFO "md: autorun ...\n");
5366 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005367 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005369 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005371 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372
5373 printk(KERN_INFO "md: considering %s ...\n",
5374 bdevname(rdev0->bdev,b));
5375 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005376 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 if (super_90_load(rdev, rdev0, 0) >= 0) {
5378 printk(KERN_INFO "md: adding %s ...\n",
5379 bdevname(rdev->bdev,b));
5380 list_move(&rdev->same_set, &candidates);
5381 }
5382 /*
5383 * now we have a set of devices, with all of them having
5384 * mostly sane superblocks. It's time to allocate the
5385 * mddev.
5386 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005387 if (part) {
5388 dev = MKDEV(mdp_major,
5389 rdev0->preferred_minor << MdpMinorShift);
5390 unit = MINOR(dev) >> MdpMinorShift;
5391 } else {
5392 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5393 unit = MINOR(dev);
5394 }
5395 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5397 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5398 break;
5399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400
5401 md_probe(dev, NULL, NULL);
5402 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005403 if (!mddev || !mddev->gendisk) {
5404 if (mddev)
5405 mddev_put(mddev);
5406 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 "md: cannot allocate memory for md drive.\n");
5408 break;
5409 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005410 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 printk(KERN_WARNING "md: %s locked, cannot run\n",
5412 mdname(mddev));
5413 else if (mddev->raid_disks || mddev->major_version
5414 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005415 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 "md: %s already running, cannot run %s\n",
5417 mdname(mddev), bdevname(rdev0->bdev,b));
5418 mddev_unlock(mddev);
5419 } else {
5420 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005421 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005422 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 list_del_init(&rdev->same_set);
5424 if (bind_rdev_to_array(rdev, mddev))
5425 export_rdev(rdev);
5426 }
5427 autorun_array(mddev);
5428 mddev_unlock(mddev);
5429 }
5430 /* on success, candidates will be empty, on error
5431 * it won't...
5432 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005433 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005434 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 mddev_put(mddev);
5438 }
5439 printk(KERN_INFO "md: ... autorun DONE.\n");
5440}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005441#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
NeilBrownf72ffdd2014-09-30 14:23:59 +10005443static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444{
5445 mdu_version_t ver;
5446
5447 ver.major = MD_MAJOR_VERSION;
5448 ver.minor = MD_MINOR_VERSION;
5449 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5450
5451 if (copy_to_user(arg, &ver, sizeof(ver)))
5452 return -EFAULT;
5453
5454 return 0;
5455}
5456
NeilBrownf72ffdd2014-09-30 14:23:59 +10005457static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458{
5459 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005460 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005461 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
NeilBrown1ca69c42012-10-11 13:37:33 +11005463 nr = working = insync = failed = spare = 0;
5464 rcu_read_lock();
5465 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005467 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 failed++;
5469 else {
5470 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005471 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005472 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 else
5474 spare++;
5475 }
5476 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005477 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
5479 info.major_version = mddev->major_version;
5480 info.minor_version = mddev->minor_version;
5481 info.patch_version = MD_PATCHLEVEL_VERSION;
5482 info.ctime = mddev->ctime;
5483 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005484 info.size = mddev->dev_sectors / 2;
5485 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005486 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 info.nr_disks = nr;
5488 info.raid_disks = mddev->raid_disks;
5489 info.md_minor = mddev->md_minor;
5490 info.not_persistent= !mddev->persistent;
5491
5492 info.utime = mddev->utime;
5493 info.state = 0;
5494 if (mddev->in_sync)
5495 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005496 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005497 info.state |= (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005498 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 info.working_disks = working;
5500 info.failed_disks = failed;
5501 info.spare_disks = spare;
5502
5503 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005504 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
5506 if (copy_to_user(arg, &info, sizeof(info)))
5507 return -EFAULT;
5508
5509 return 0;
5510}
5511
NeilBrownf72ffdd2014-09-30 14:23:59 +10005512static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005513{
5514 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005515 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005516 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005517
NeilBrown60559da2013-07-16 16:44:29 +10005518 file = kmalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005519 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005520 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005521
NeilBrown32a76272005-06-21 17:17:14 -07005522 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005523 spin_lock(&mddev->lock);
5524 /* bitmap disabled, zero the first byte and copy out */
5525 if (!mddev->bitmap_info.file)
5526 file->pathname[0] = '\0';
5527 else if ((ptr = d_path(&mddev->bitmap_info.file->f_path,
5528 file->pathname, sizeof(file->pathname))),
5529 IS_ERR(ptr))
5530 err = PTR_ERR(ptr);
5531 else
5532 memmove(file->pathname, ptr,
5533 sizeof(file->pathname)-(ptr-file->pathname));
5534 spin_unlock(&mddev->lock);
5535
5536 if (err == 0 &&
5537 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005538 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005539
NeilBrown32a76272005-06-21 17:17:14 -07005540 kfree(file);
5541 return err;
5542}
5543
NeilBrownf72ffdd2014-09-30 14:23:59 +10005544static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545{
5546 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005547 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
5549 if (copy_from_user(&info, arg, sizeof(info)))
5550 return -EFAULT;
5551
NeilBrown1ca69c42012-10-11 13:37:33 +11005552 rcu_read_lock();
5553 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 if (rdev) {
5555 info.major = MAJOR(rdev->bdev->bd_dev);
5556 info.minor = MINOR(rdev->bdev->bd_dev);
5557 info.raid_disk = rdev->raid_disk;
5558 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005559 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005561 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 info.state |= (1<<MD_DISK_ACTIVE);
5563 info.state |= (1<<MD_DISK_SYNC);
5564 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005565 if (test_bit(WriteMostly, &rdev->flags))
5566 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 } else {
5568 info.major = info.minor = 0;
5569 info.raid_disk = -1;
5570 info.state = (1<<MD_DISK_REMOVED);
5571 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005572 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573
5574 if (copy_to_user(arg, &info, sizeof(info)))
5575 return -EFAULT;
5576
5577 return 0;
5578}
5579
NeilBrownf72ffdd2014-09-30 14:23:59 +10005580static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581{
5582 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005583 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 dev_t dev = MKDEV(info->major,info->minor);
5585
5586 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5587 return -EOVERFLOW;
5588
5589 if (!mddev->raid_disks) {
5590 int err;
5591 /* expecting a device which has a superblock */
5592 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5593 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005594 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 "md: md_import_device returned %ld\n",
5596 PTR_ERR(rdev));
5597 return PTR_ERR(rdev);
5598 }
5599 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005600 struct md_rdev *rdev0
5601 = list_entry(mddev->disks.next,
5602 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005603 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 .load_super(rdev, rdev0, mddev->minor_version);
5605 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005606 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005608 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 bdevname(rdev0->bdev,b2));
5610 export_rdev(rdev);
5611 return -EINVAL;
5612 }
5613 }
5614 err = bind_rdev_to_array(rdev, mddev);
5615 if (err)
5616 export_rdev(rdev);
5617 return err;
5618 }
5619
5620 /*
5621 * add_new_disk can be used once the array is assembled
5622 * to add "hot spares". They must already have a superblock
5623 * written
5624 */
5625 if (mddev->pers) {
5626 int err;
5627 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005628 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 "%s: personality does not support diskops!\n",
5630 mdname(mddev));
5631 return -EINVAL;
5632 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005633 if (mddev->persistent)
5634 rdev = md_import_device(dev, mddev->major_version,
5635 mddev->minor_version);
5636 else
5637 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005639 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 "md: md_import_device returned %ld\n",
5641 PTR_ERR(rdev));
5642 return PTR_ERR(rdev);
5643 }
NeilBrown1a855a02010-12-09 16:36:28 +11005644 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005645 if (!mddev->persistent) {
5646 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005647 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005648 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005649 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005650 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005651 } else
NeilBrown41158c72005-06-21 17:17:25 -07005652 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005653 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005654 } else
5655 super_types[mddev->major_version].
5656 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005657 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005658 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005659 /* This was a hot-add request, but events doesn't
5660 * match, so reject it.
5661 */
5662 export_rdev(rdev);
5663 return -EINVAL;
5664 }
5665
NeilBrownb2d444d2005-11-08 21:39:31 -08005666 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005667 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5668 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005669 else
5670 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005671
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 rdev->raid_disk = -1;
5673 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005674 if (!err && !mddev->pers->hot_remove_disk) {
5675 /* If there is hot_add_disk but no hot_remove_disk
5676 * then added disks for geometry changes,
5677 * and should be added immediately.
5678 */
5679 super_types[mddev->major_version].
5680 validate_super(mddev, rdev);
5681 err = mddev->pers->hot_add_disk(mddev, rdev);
5682 if (err)
5683 unbind_rdev_from_array(rdev);
5684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 if (err)
5686 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005687 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005688 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005689
NeilBrown7ceb17e2013-04-24 11:42:42 +10005690 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005691 if (mddev->degraded)
5692 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005693 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005694 if (!err)
5695 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005696 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 return err;
5698 }
5699
5700 /* otherwise, add_new_disk is only allowed
5701 * for major_version==0 superblocks
5702 */
5703 if (mddev->major_version != 0) {
5704 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5705 mdname(mddev));
5706 return -EINVAL;
5707 }
5708
5709 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5710 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005711 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005713 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 "md: error, md_import_device() returned %ld\n",
5715 PTR_ERR(rdev));
5716 return PTR_ERR(rdev);
5717 }
5718 rdev->desc_nr = info->number;
5719 if (info->raid_disk < mddev->raid_disks)
5720 rdev->raid_disk = info->raid_disk;
5721 else
5722 rdev->raid_disk = -1;
5723
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005725 if (info->state & (1<<MD_DISK_SYNC))
5726 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005728 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5729 set_bit(WriteMostly, &rdev->flags);
5730
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 if (!mddev->persistent) {
5732 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005733 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5734 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005735 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005736 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737
NeilBrown2bf071b2006-01-06 00:20:55 -08005738 err = bind_rdev_to_array(rdev, mddev);
5739 if (err) {
5740 export_rdev(rdev);
5741 return err;
5742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 }
5744
5745 return 0;
5746}
5747
NeilBrownf72ffdd2014-09-30 14:23:59 +10005748static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749{
5750 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005751 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 rdev = find_rdev(mddev, dev);
5754 if (!rdev)
5755 return -ENXIO;
5756
NeilBrown3ea8929d2013-04-24 11:42:41 +10005757 clear_bit(Blocked, &rdev->flags);
5758 remove_and_add_spares(mddev, rdev);
5759
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 if (rdev->raid_disk >= 0)
5761 goto busy;
5762
5763 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005764 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005765 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766
5767 return 0;
5768busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005769 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 bdevname(rdev->bdev,b), mdname(mddev));
5771 return -EBUSY;
5772}
5773
NeilBrownf72ffdd2014-09-30 14:23:59 +10005774static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775{
5776 char b[BDEVNAME_SIZE];
5777 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005778 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779
5780 if (!mddev->pers)
5781 return -ENODEV;
5782
5783 if (mddev->major_version != 0) {
5784 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5785 " version-0 superblocks.\n",
5786 mdname(mddev));
5787 return -EINVAL;
5788 }
5789 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005790 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 "%s: personality does not support diskops!\n",
5792 mdname(mddev));
5793 return -EINVAL;
5794 }
5795
NeilBrownd710e132008-10-13 11:55:12 +11005796 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005798 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 "md: error, md_import_device() returned %ld\n",
5800 PTR_ERR(rdev));
5801 return -EINVAL;
5802 }
5803
5804 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005805 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005807 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808
NeilBrown8190e752009-06-18 08:48:58 +10005809 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810
NeilBrownb2d444d2005-11-08 21:39:31 -08005811 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005812 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 "md: can not hot-add faulty %s disk to %s!\n",
5814 bdevname(rdev->bdev,b), mdname(mddev));
5815 err = -EINVAL;
5816 goto abort_export;
5817 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005818 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005820 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005821 err = bind_rdev_to_array(rdev, mddev);
5822 if (err)
5823 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824
5825 /*
5826 * The rest should better be atomic, we can have disk failures
5827 * noticed in interrupt contexts ...
5828 */
5829
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 rdev->raid_disk = -1;
5831
NeilBrown850b2b42006-10-03 01:15:46 -07005832 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
5834 /*
5835 * Kick recovery, maybe this spare has to be added to the
5836 * array immediately.
5837 */
5838 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5839 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005840 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 return 0;
5842
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843abort_export:
5844 export_rdev(rdev);
5845 return err;
5846}
5847
NeilBrownfd01b882011-10-11 16:47:53 +11005848static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005849{
NeilBrown035328c2014-04-09 12:25:40 +10005850 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07005851
NeilBrown36fa3062005-09-09 16:23:45 -07005852 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10005853 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07005854 return -EBUSY;
5855 if (mddev->recovery || mddev->sync_thread)
5856 return -EBUSY;
5857 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005858 }
5859
NeilBrown36fa3062005-09-09 16:23:45 -07005860 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10005861 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11005862 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07005863
NeilBrown1e594bb2014-12-15 12:57:00 +11005864 if (mddev->bitmap || mddev->bitmap_info.file)
5865 return -EEXIST; /* cannot add when bitmap is present */
5866 f = fget(fd);
5867
5868 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005869 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5870 mdname(mddev));
5871 return -EBADF;
5872 }
5873
NeilBrown1e594bb2014-12-15 12:57:00 +11005874 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10005875 if (!S_ISREG(inode->i_mode)) {
5876 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
5877 mdname(mddev));
5878 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11005879 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10005880 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
5881 mdname(mddev));
5882 err = -EBADF;
5883 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07005884 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5885 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10005886 err = -EBUSY;
5887 }
5888 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11005889 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07005890 return err;
5891 }
NeilBrown1e594bb2014-12-15 12:57:00 +11005892 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11005893 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005894 } else if (mddev->bitmap == NULL)
5895 return -ENOENT; /* cannot remove what isn't there */
5896 err = 0;
5897 if (mddev->pers) {
5898 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005899 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005900 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005901 if (!err)
5902 err = bitmap_load(mddev);
5903 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005904 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005905 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005906 fd = -1; /* make sure to put the file */
5907 }
NeilBrown36fa3062005-09-09 16:23:45 -07005908 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005909 }
5910 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11005911 struct file *f = mddev->bitmap_info.file;
5912 if (f) {
5913 spin_lock(&mddev->lock);
5914 mddev->bitmap_info.file = NULL;
5915 spin_unlock(&mddev->lock);
5916 fput(f);
5917 }
NeilBrown36fa3062005-09-09 16:23:45 -07005918 }
5919
NeilBrown32a76272005-06-21 17:17:14 -07005920 return err;
5921}
5922
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923/*
5924 * set_array_info is used two different ways
5925 * The original usage is when creating a new array.
5926 * In this usage, raid_disks is > 0 and it together with
5927 * level, size, not_persistent,layout,chunksize determine the
5928 * shape of the array.
5929 * This will always create an array with a type-0.90.0 superblock.
5930 * The newer usage is when assembling an array.
5931 * In this case raid_disks will be 0, and the major_version field is
5932 * use to determine which style super-blocks are to be found on the devices.
5933 * The minor and patch _version numbers are also kept incase the
5934 * super_block handler wishes to interpret them.
5935 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005936static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937{
5938
5939 if (info->raid_disks == 0) {
5940 /* just setting version number for superblock loading */
5941 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005942 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 super_types[info->major_version].name == NULL) {
5944 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005945 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 "md: superblock version %d not known\n",
5947 info->major_version);
5948 return -EINVAL;
5949 }
5950 mddev->major_version = info->major_version;
5951 mddev->minor_version = info->minor_version;
5952 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005953 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005954 /* ensure mddev_put doesn't delete this now that there
5955 * is some minimal configuration.
5956 */
5957 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 return 0;
5959 }
5960 mddev->major_version = MD_MAJOR_VERSION;
5961 mddev->minor_version = MD_MINOR_VERSION;
5962 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5963 mddev->ctime = get_seconds();
5964
5965 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005966 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005967 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 mddev->raid_disks = info->raid_disks;
5969 /* don't set md_minor, it is determined by which /dev/md* was
5970 * openned
5971 */
5972 if (info->state & (1<<MD_SB_CLEAN))
5973 mddev->recovery_cp = MaxSector;
5974 else
5975 mddev->recovery_cp = 0;
5976 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005977 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978
5979 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005980 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981
5982 mddev->max_disks = MD_SB_DISKS;
5983
NeilBrowne6910632008-02-06 01:39:51 -08005984 if (mddev->persistent)
5985 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005986 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987
NeilBrownc3d97142009-12-14 12:49:52 +11005988 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10005989 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11005990 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005991
NeilBrownf6705572006-03-27 01:18:11 -08005992 mddev->reshape_position = MaxSector;
5993
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 /*
5995 * Generate a 128 bit UUID
5996 */
5997 get_random_bytes(mddev->uuid, 16);
5998
NeilBrownf6705572006-03-27 01:18:11 -08005999 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006000 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006001 mddev->new_layout = mddev->layout;
6002 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006003 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006004
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 return 0;
6006}
6007
NeilBrownfd01b882011-10-11 16:47:53 +11006008void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006009{
Dan Williamsb522adc2009-03-31 15:00:31 +11006010 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6011
6012 if (mddev->external_size)
6013 return;
6014
Dan Williams1f403622009-03-31 14:59:03 +11006015 mddev->array_sectors = array_sectors;
6016}
6017EXPORT_SYMBOL(md_set_array_sectors);
6018
NeilBrownfd01b882011-10-11 16:47:53 +11006019static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006020{
NeilBrown3cb03002011-10-11 16:45:26 +11006021 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006022 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006023 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006024
6025 if (mddev->pers->resize == NULL)
6026 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006027 /* The "num_sectors" is the number of sectors of each device that
6028 * is used. This can only make sense for arrays with redundancy.
6029 * linear and raid0 always use whatever space is available. We can only
6030 * consider changing this number if no resync or reconstruction is
6031 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006032 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006033 * of each device. If num_sectors is zero, we find the largest size
6034 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006035 */
NeilBrownf851b602014-12-11 10:02:10 +11006036 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6037 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006038 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006039 if (mddev->ro)
6040 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006041
NeilBrowndafb20f2012-03-19 12:46:39 +11006042 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006043 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006044
Andre Nolld71f9f82008-07-11 22:02:22 +10006045 if (fit && (num_sectors == 0 || num_sectors > avail))
6046 num_sectors = avail;
6047 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006048 return -ENOSPC;
6049 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006050 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006051 if (!rv)
6052 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006053 return rv;
6054}
6055
NeilBrownfd01b882011-10-11 16:47:53 +11006056static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006057{
6058 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006059 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006060 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006061 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006062 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006063 if (mddev->ro)
6064 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006065 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006066 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006067 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006068 if (mddev->sync_thread ||
6069 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6070 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006071 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006072
6073 rdev_for_each(rdev, mddev) {
6074 if (mddev->raid_disks < raid_disks &&
6075 rdev->data_offset < rdev->new_data_offset)
6076 return -EINVAL;
6077 if (mddev->raid_disks > raid_disks &&
6078 rdev->data_offset > rdev->new_data_offset)
6079 return -EINVAL;
6080 }
6081
NeilBrown63c70c42006-03-27 01:18:13 -08006082 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006083 if (mddev->delta_disks < 0)
6084 mddev->reshape_backwards = 1;
6085 else if (mddev->delta_disks > 0)
6086 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006087
6088 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006089 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006090 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006091 mddev->reshape_backwards = 0;
6092 }
NeilBrownda943b992006-01-06 00:20:54 -08006093 return rv;
6094}
6095
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096/*
6097 * update_array_info is used to change the configuration of an
6098 * on-line array.
6099 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6100 * fields in the info are checked against the array.
6101 * Any differences that cannot be handled will cause an error.
6102 * Normally, only one change can be managed at a time.
6103 */
NeilBrownfd01b882011-10-11 16:47:53 +11006104static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105{
6106 int rv = 0;
6107 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006108 int state = 0;
6109
6110 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006111 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006112 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113
6114 if (mddev->major_version != info->major_version ||
6115 mddev->minor_version != info->minor_version ||
6116/* mddev->patch_version != info->patch_version || */
6117 mddev->ctime != info->ctime ||
6118 mddev->level != info->level ||
6119/* mddev->layout != info->layout || */
6120 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006121 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006122 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6123 ((state^info->state) & 0xfffffe00)
6124 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 return -EINVAL;
6126 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006127 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6128 cnt++;
6129 if (mddev->raid_disks != info->raid_disks)
6130 cnt++;
6131 if (mddev->layout != info->layout)
6132 cnt++;
6133 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6134 cnt++;
6135 if (cnt == 0)
6136 return 0;
6137 if (cnt > 1)
6138 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139
6140 if (mddev->layout != info->layout) {
6141 /* Change layout
6142 * we don't need to do anything at the md level, the
6143 * personality will take care of it all.
6144 */
NeilBrown50ac1682009-06-18 08:47:55 +10006145 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006147 else {
6148 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006149 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006150 if (rv)
6151 mddev->new_layout = mddev->layout;
6152 return rv;
6153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006155 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006156 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157
NeilBrownda943b992006-01-06 00:20:54 -08006158 if (mddev->raid_disks != info->raid_disks)
6159 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
NeilBrown36fa3062005-09-09 16:23:45 -07006161 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006162 if (mddev->pers->quiesce == NULL || mddev->thread == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -07006163 return -EINVAL;
6164 if (mddev->recovery || mddev->sync_thread)
6165 return -EBUSY;
6166 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6167 /* add the bitmap */
6168 if (mddev->bitmap)
6169 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006170 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006171 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006172 mddev->bitmap_info.offset =
6173 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006174 mddev->bitmap_info.space =
6175 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006176 mddev->pers->quiesce(mddev, 1);
6177 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006178 if (!rv)
6179 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006180 if (rv)
6181 bitmap_destroy(mddev);
6182 mddev->pers->quiesce(mddev, 0);
6183 } else {
6184 /* remove the bitmap */
6185 if (!mddev->bitmap)
6186 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006187 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006188 return -EINVAL;
6189 mddev->pers->quiesce(mddev, 1);
6190 bitmap_destroy(mddev);
6191 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006192 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006193 }
6194 }
NeilBrown850b2b42006-10-03 01:15:46 -07006195 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 return rv;
6197}
6198
NeilBrownfd01b882011-10-11 16:47:53 +11006199static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200{
NeilBrown3cb03002011-10-11 16:45:26 +11006201 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006202 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203
6204 if (mddev->pers == NULL)
6205 return -ENODEV;
6206
NeilBrown1ca69c42012-10-11 13:37:33 +11006207 rcu_read_lock();
6208 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006210 err = -ENODEV;
6211 else {
6212 md_error(mddev, rdev);
6213 if (!test_bit(Faulty, &rdev->flags))
6214 err = -EBUSY;
6215 }
6216 rcu_read_unlock();
6217 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218}
6219
Andre Noll2f9618c2008-04-25 18:57:58 +02006220/*
6221 * We have a problem here : there is no easy way to give a CHS
6222 * virtual geometry. We currently pretend that we have a 2 heads
6223 * 4 sectors (with a BIG number of cylinders...). This drives
6224 * dosfs just mad... ;-)
6225 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006226static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6227{
NeilBrownfd01b882011-10-11 16:47:53 +11006228 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006229
6230 geo->heads = 2;
6231 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006232 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006233 return 0;
6234}
6235
Nicolas Schichancb335f82014-01-15 16:58:52 +01006236static inline bool md_ioctl_valid(unsigned int cmd)
6237{
6238 switch (cmd) {
6239 case ADD_NEW_DISK:
6240 case BLKROSET:
6241 case GET_ARRAY_INFO:
6242 case GET_BITMAP_FILE:
6243 case GET_DISK_INFO:
6244 case HOT_ADD_DISK:
6245 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006246 case RAID_AUTORUN:
6247 case RAID_VERSION:
6248 case RESTART_ARRAY_RW:
6249 case RUN_ARRAY:
6250 case SET_ARRAY_INFO:
6251 case SET_BITMAP_FILE:
6252 case SET_DISK_FAULTY:
6253 case STOP_ARRAY:
6254 case STOP_ARRAY_RO:
6255 return true;
6256 default:
6257 return false;
6258 }
6259}
6260
Al Viroa39907f2008-03-02 10:31:15 -05006261static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 unsigned int cmd, unsigned long arg)
6263{
6264 int err = 0;
6265 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006266 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006267 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268
Nicolas Schichancb335f82014-01-15 16:58:52 +01006269 if (!md_ioctl_valid(cmd))
6270 return -ENOTTY;
6271
NeilBrown506c9e42011-12-23 10:17:26 +11006272 switch (cmd) {
6273 case RAID_VERSION:
6274 case GET_ARRAY_INFO:
6275 case GET_DISK_INFO:
6276 break;
6277 default:
6278 if (!capable(CAP_SYS_ADMIN))
6279 return -EACCES;
6280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
6282 /*
6283 * Commands dealing with the RAID driver but not any
6284 * particular array:
6285 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006286 switch (cmd) {
6287 case RAID_VERSION:
6288 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006289 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006292 case RAID_AUTORUN:
6293 err = 0;
6294 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006295 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006297 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298 }
6299
6300 /*
6301 * Commands creating/starting a new array:
6302 */
6303
Al Viroa39907f2008-03-02 10:31:15 -05006304 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305
6306 if (!mddev) {
6307 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006308 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 }
6310
NeilBrown1ca69c42012-10-11 13:37:33 +11006311 /* Some actions do not requires the mutex */
6312 switch (cmd) {
6313 case GET_ARRAY_INFO:
6314 if (!mddev->raid_disks && !mddev->external)
6315 err = -ENODEV;
6316 else
6317 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006318 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006319
6320 case GET_DISK_INFO:
6321 if (!mddev->raid_disks && !mddev->external)
6322 err = -ENODEV;
6323 else
6324 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006325 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006326
6327 case SET_DISK_FAULTY:
6328 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006329 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006330
6331 case GET_BITMAP_FILE:
6332 err = get_bitmap_file(mddev, argp);
6333 goto out;
6334
NeilBrown1ca69c42012-10-11 13:37:33 +11006335 }
6336
NeilBrowna7a3f082012-12-11 13:35:54 +11006337 if (cmd == ADD_NEW_DISK)
6338 /* need to ensure md_delayed_delete() has completed */
6339 flush_workqueue(md_misc_wq);
6340
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006341 if (cmd == HOT_REMOVE_DISK)
6342 /* need to ensure recovery thread has run */
6343 wait_event_interruptible_timeout(mddev->sb_wait,
6344 !test_bit(MD_RECOVERY_NEEDED,
6345 &mddev->flags),
6346 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006347 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6348 /* Need to flush page cache, and ensure no-one else opens
6349 * and writes
6350 */
6351 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006352 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006353 mutex_unlock(&mddev->open_mutex);
6354 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006355 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006356 }
6357 set_bit(MD_STILL_CLOSED, &mddev->flags);
6358 mutex_unlock(&mddev->open_mutex);
6359 sync_blockdev(bdev);
6360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 err = mddev_lock(mddev);
6362 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006363 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6365 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006366 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 }
6368
NeilBrownc02c0ae2012-12-11 13:39:21 +11006369 if (cmd == SET_ARRAY_INFO) {
6370 mdu_array_info_t info;
6371 if (!arg)
6372 memset(&info, 0, sizeof(info));
6373 else if (copy_from_user(&info, argp, sizeof(info))) {
6374 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006375 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006376 }
6377 if (mddev->pers) {
6378 err = update_array_info(mddev, &info);
6379 if (err) {
6380 printk(KERN_WARNING "md: couldn't update"
6381 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006382 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006384 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006385 }
6386 if (!list_empty(&mddev->disks)) {
6387 printk(KERN_WARNING
6388 "md: array %s already has disks!\n",
6389 mdname(mddev));
6390 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006391 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006392 }
6393 if (mddev->raid_disks) {
6394 printk(KERN_WARNING
6395 "md: array %s already initialised!\n",
6396 mdname(mddev));
6397 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006398 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006399 }
6400 err = set_array_info(mddev, &info);
6401 if (err) {
6402 printk(KERN_WARNING "md: couldn't set"
6403 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006404 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006405 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006406 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 }
6408
6409 /*
6410 * Commands querying/configuring an existing array:
6411 */
NeilBrown32a76272005-06-21 17:17:14 -07006412 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006413 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006414 if ((!mddev->raid_disks && !mddev->external)
6415 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6416 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6417 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006419 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 }
6421
6422 /*
6423 * Commands even a read-only array can execute:
6424 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006425 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006426 case RESTART_ARRAY_RW:
6427 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006428 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006429
6430 case STOP_ARRAY:
6431 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006432 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006433
6434 case STOP_ARRAY_RO:
6435 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006436 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006437
NeilBrown3ea8929d2013-04-24 11:42:41 +10006438 case HOT_REMOVE_DISK:
6439 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006440 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006441
NeilBrown7ceb17e2013-04-24 11:42:42 +10006442 case ADD_NEW_DISK:
6443 /* We can support ADD_NEW_DISK on read-only arrays
6444 * on if we are re-adding a preexisting device.
6445 * So require mddev->pers and MD_DISK_SYNC.
6446 */
6447 if (mddev->pers) {
6448 mdu_disk_info_t info;
6449 if (copy_from_user(&info, argp, sizeof(info)))
6450 err = -EFAULT;
6451 else if (!(info.state & (1<<MD_DISK_SYNC)))
6452 /* Need to clear read-only for this */
6453 break;
6454 else
6455 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006456 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006457 }
6458 break;
6459
NeilBrownc02c0ae2012-12-11 13:39:21 +11006460 case BLKROSET:
6461 if (get_user(ro, (int __user *)(arg))) {
6462 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006463 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006464 }
6465 err = -EINVAL;
6466
6467 /* if the bdev is going readonly the value of mddev->ro
6468 * does not matter, no writes are coming
6469 */
6470 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006471 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006472
NeilBrownc02c0ae2012-12-11 13:39:21 +11006473 /* are we are already prepared for writes? */
6474 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006475 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006476
6477 /* transitioning to readauto need only happen for
6478 * arrays that call md_write_start
6479 */
6480 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006482 if (err == 0) {
6483 mddev->ro = 2;
6484 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006485 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006486 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006487 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488 }
6489
6490 /*
6491 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006492 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 */
NeilBrown326eb172014-09-30 15:36:28 +10006494 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006495 if (mddev->ro == 2) {
6496 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006497 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006498 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006499 /* mddev_unlock will wake thread */
6500 /* If a device failed while we were read-only, we
6501 * need to make sure the metadata is updated now.
6502 */
6503 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6504 mddev_unlock(mddev);
6505 wait_event(mddev->sb_wait,
6506 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6507 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006508 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006509 }
NeilBrownf91de922005-11-08 21:39:36 -08006510 } else {
6511 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006512 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514 }
6515
NeilBrownc02c0ae2012-12-11 13:39:21 +11006516 switch (cmd) {
6517 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006519 mdu_disk_info_t info;
6520 if (copy_from_user(&info, argp, sizeof(info)))
6521 err = -EFAULT;
6522 else
6523 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006524 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526
NeilBrownc02c0ae2012-12-11 13:39:21 +11006527 case HOT_ADD_DISK:
6528 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006529 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530
NeilBrownc02c0ae2012-12-11 13:39:21 +11006531 case RUN_ARRAY:
6532 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006533 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534
NeilBrownc02c0ae2012-12-11 13:39:21 +11006535 case SET_BITMAP_FILE:
6536 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006537 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006538
NeilBrownc02c0ae2012-12-11 13:39:21 +11006539 default:
6540 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006541 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542 }
6543
NeilBrown3adc28d2014-09-30 15:46:41 +10006544unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006545 if (mddev->hold_active == UNTIL_IOCTL &&
6546 err != -EINVAL)
6547 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006549out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 return err;
6551}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006552#ifdef CONFIG_COMPAT
6553static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6554 unsigned int cmd, unsigned long arg)
6555{
6556 switch (cmd) {
6557 case HOT_REMOVE_DISK:
6558 case HOT_ADD_DISK:
6559 case SET_DISK_FAULTY:
6560 case SET_BITMAP_FILE:
6561 /* These take in integer arg, do not convert */
6562 break;
6563 default:
6564 arg = (unsigned long)compat_ptr(arg);
6565 break;
6566 }
6567
6568 return md_ioctl(bdev, mode, cmd, arg);
6569}
6570#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571
Al Viroa39907f2008-03-02 10:31:15 -05006572static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573{
6574 /*
6575 * Succeed if we can lock the mddev, which confirms that
6576 * it isn't being stopped right now.
6577 */
NeilBrownfd01b882011-10-11 16:47:53 +11006578 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 int err;
6580
Yuanhan Liu0c098222012-05-22 13:55:32 +10006581 if (!mddev)
6582 return -ENODEV;
6583
NeilBrownd3374822009-01-09 08:31:10 +11006584 if (mddev->gendisk != bdev->bd_disk) {
6585 /* we are racing with mddev_put which is discarding this
6586 * bd_disk.
6587 */
6588 mddev_put(mddev);
6589 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006590 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006591 /* Then retry the open from the top */
6592 return -ERESTARTSYS;
6593 }
6594 BUG_ON(mddev != bdev->bd_disk->private_data);
6595
NeilBrownc8c00a62009-08-10 12:50:52 +10006596 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 goto out;
6598
6599 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006600 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006601 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006602 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006604 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 out:
6606 return err;
6607}
6608
Al Virodb2a1442013-05-05 21:52:57 -04006609static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610{
NeilBrownf72ffdd2014-09-30 14:23:59 +10006611 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006613 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006614 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006617
6618static int md_media_changed(struct gendisk *disk)
6619{
NeilBrownfd01b882011-10-11 16:47:53 +11006620 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006621
6622 return mddev->changed;
6623}
6624
6625static int md_revalidate(struct gendisk *disk)
6626{
NeilBrownfd01b882011-10-11 16:47:53 +11006627 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006628
6629 mddev->changed = 0;
6630 return 0;
6631}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006632static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633{
6634 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006635 .open = md_open,
6636 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006637 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006638#ifdef CONFIG_COMPAT
6639 .compat_ioctl = md_compat_ioctl,
6640#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006641 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006642 .media_changed = md_media_changed,
6643 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644};
6645
NeilBrownf72ffdd2014-09-30 14:23:59 +10006646static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647{
NeilBrown2b8bf342011-10-11 16:48:23 +11006648 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 /*
6651 * md_thread is a 'system-thread', it's priority should be very
6652 * high. We avoid resource deadlocks individually in each
6653 * raid personality. (RAID5 does preallocation) We also use RR and
6654 * the very same RT priority as kswapd, thus we will never get
6655 * into a priority inversion deadlock.
6656 *
6657 * we definitely have to have equal or higher priority than
6658 * bdflush, otherwise bdflush will deadlock if there are too
6659 * many dirty RAID5 blocks.
6660 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661
NeilBrown6985c432005-10-19 21:23:47 -07006662 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006663 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664
NeilBrown93588e22005-11-15 00:09:12 -08006665 /* We need to wait INTERRUPTIBLE so that
6666 * we don't add to the load-average.
6667 * That means we need to be sure no signals are
6668 * pending
6669 */
6670 if (signal_pending(current))
6671 flush_signals(current);
6672
6673 wait_event_interruptible_timeout
6674 (thread->wqueue,
6675 test_bit(THREAD_WAKEUP, &thread->flags)
6676 || kthread_should_stop(),
6677 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678
NeilBrown6c987912011-01-14 09:13:53 +11006679 clear_bit(THREAD_WAKEUP, &thread->flags);
6680 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006681 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006683
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 return 0;
6685}
6686
NeilBrown2b8bf342011-10-11 16:48:23 +11006687void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688{
6689 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006690 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 set_bit(THREAD_WAKEUP, &thread->flags);
6692 wake_up(&thread->wqueue);
6693 }
6694}
NeilBrown6c144d32014-09-30 16:15:38 +10006695EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696
Shaohua Li4ed87312012-10-11 13:34:00 +11006697struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6698 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699{
NeilBrown2b8bf342011-10-11 16:48:23 +11006700 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701
NeilBrown2b8bf342011-10-11 16:48:23 +11006702 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 if (!thread)
6704 return NULL;
6705
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 init_waitqueue_head(&thread->wqueue);
6707
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 thread->run = run;
6709 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006710 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006711 thread->tsk = kthread_run(md_thread, thread,
6712 "%s_%s",
6713 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006714 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006715 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 kfree(thread);
6717 return NULL;
6718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719 return thread;
6720}
NeilBrown6c144d32014-09-30 16:15:38 +10006721EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722
NeilBrown2b8bf342011-10-11 16:48:23 +11006723void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724{
NeilBrown2b8bf342011-10-11 16:48:23 +11006725 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006726 if (!thread)
6727 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006728 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006729 /* Locking ensures that mddev_unlock does not wake_up a
6730 * non-existent thread
6731 */
6732 spin_lock(&pers_lock);
6733 *threadp = NULL;
6734 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006735
6736 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 kfree(thread);
6738}
NeilBrown6c144d32014-09-30 16:15:38 +10006739EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740
NeilBrownfd01b882011-10-11 16:47:53 +11006741void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742{
NeilBrownb2d444d2005-11-08 21:39:31 -08006743 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006745
NeilBrownde393cd2011-07-28 11:31:48 +10006746 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 return;
6748 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006749 if (mddev->degraded)
6750 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006751 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6753 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6754 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006755 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006756 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006757 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758}
NeilBrown6c144d32014-09-30 16:15:38 +10006759EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760
6761/* seq_file implementation /proc/mdstat */
6762
6763static void status_unused(struct seq_file *seq)
6764{
6765 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006766 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767
6768 seq_printf(seq, "unused devices: ");
6769
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006770 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 char b[BDEVNAME_SIZE];
6772 i++;
6773 seq_printf(seq, "%s ",
6774 bdevname(rdev->bdev,b));
6775 }
6776 if (!i)
6777 seq_printf(seq, "<none>");
6778
6779 seq_printf(seq, "\n");
6780}
6781
NeilBrownf72ffdd2014-09-30 14:23:59 +10006782static void status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783{
NeilBrowndd71cf62009-05-07 12:49:35 +10006784 sector_t max_sectors, resync, res;
6785 unsigned long dt, db;
6786 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006787 int scale;
6788 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789
NeilBrown72f36d52012-10-11 14:25:57 +11006790 if (mddev->curr_resync <= 3)
6791 resync = 0;
6792 else
6793 resync = mddev->curr_resync
6794 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795
NeilBrownc804cde2012-05-21 09:28:33 +10006796 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6797 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006798 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006800 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801
NeilBrown403df472014-09-30 15:52:29 +10006802 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08006803 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006804 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006805 * u32, as those are the requirements for sector_div.
6806 * Thus 'scale' must be at least 10
6807 */
6808 scale = 10;
6809 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006810 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006811 scale++;
6812 }
6813 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006814 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006815
6816 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 {
NeilBrown4588b422006-03-27 01:18:04 -08006818 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 seq_printf(seq, "[");
6820 for (i = 0; i < x; i++)
6821 seq_printf(seq, "=");
6822 seq_printf(seq, ">");
6823 for (i = 0; i < y; i++)
6824 seq_printf(seq, ".");
6825 seq_printf(seq, "] ");
6826 }
NeilBrown4588b422006-03-27 01:18:04 -08006827 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006828 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6829 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006830 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6831 "check" :
6832 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6833 "resync" : "recovery"))),
6834 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006835 (unsigned long long) resync/2,
6836 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837
6838 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839 * dt: time from mark until now
6840 * db: blocks written from mark until now
6841 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006842 *
6843 * rt is a sector_t, so could be 32bit or 64bit.
6844 * So we divide before multiply in case it is 32bit and close
6845 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006846 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006847 * near the end of resync when the number of remaining sectors
6848 * is close to 'db'.
6849 * We then divide rt by 32 after multiplying by db to compensate.
6850 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851 */
6852 dt = ((jiffies - mddev->resync_mark) / HZ);
6853 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006854 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6855 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856
NeilBrowndd71cf62009-05-07 12:49:35 +10006857 rt = max_sectors - resync; /* number of remaining sectors */
6858 sector_div(rt, db/32+1);
6859 rt *= dt;
6860 rt >>= 5;
6861
6862 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6863 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864
NeilBrownff4e8d92006-07-10 04:44:16 -07006865 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866}
6867
6868static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6869{
6870 struct list_head *tmp;
6871 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006872 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873
6874 if (l >= 0x10000)
6875 return NULL;
6876 if (!l--)
6877 /* header */
6878 return (void*)1;
6879
6880 spin_lock(&all_mddevs_lock);
6881 list_for_each(tmp,&all_mddevs)
6882 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006883 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 mddev_get(mddev);
6885 spin_unlock(&all_mddevs_lock);
6886 return mddev;
6887 }
6888 spin_unlock(&all_mddevs_lock);
6889 if (!l--)
6890 return (void*)2;/* tail */
6891 return NULL;
6892}
6893
6894static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6895{
6896 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006897 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10006898
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 ++*pos;
6900 if (v == (void*)2)
6901 return NULL;
6902
6903 spin_lock(&all_mddevs_lock);
6904 if (v == (void*)1)
6905 tmp = all_mddevs.next;
6906 else
6907 tmp = mddev->all_mddevs.next;
6908 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006909 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 else {
6911 next_mddev = (void*)2;
6912 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10006913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 spin_unlock(&all_mddevs_lock);
6915
6916 if (v != (void*)1)
6917 mddev_put(mddev);
6918 return next_mddev;
6919
6920}
6921
6922static void md_seq_stop(struct seq_file *seq, void *v)
6923{
NeilBrownfd01b882011-10-11 16:47:53 +11006924 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925
6926 if (mddev && v != (void*)1 && v != (void*)2)
6927 mddev_put(mddev);
6928}
6929
6930static int md_seq_show(struct seq_file *seq, void *v)
6931{
NeilBrownfd01b882011-10-11 16:47:53 +11006932 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006933 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006934 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935
6936 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006937 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 seq_printf(seq, "Personalities : ");
6939 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006940 list_for_each_entry(pers, &pers_list, list)
6941 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942
6943 spin_unlock(&pers_lock);
6944 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006945 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946 return 0;
6947 }
6948 if (v == (void*)2) {
6949 status_unused(seq);
6950 return 0;
6951 }
6952
NeilBrown36d091f2014-12-15 12:56:58 +11006953 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6955 seq_printf(seq, "%s : %sactive", mdname(mddev),
6956 mddev->pers ? "" : "in");
6957 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006958 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006960 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006961 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962 seq_printf(seq, " %s", mddev->pers->name);
6963 }
6964
Andre Nolldd8ac332009-03-31 14:33:13 +11006965 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11006966 rcu_read_lock();
6967 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968 char b[BDEVNAME_SIZE];
6969 seq_printf(seq, " %s[%d]",
6970 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006971 if (test_bit(WriteMostly, &rdev->flags))
6972 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006973 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974 seq_printf(seq, "(F)");
6975 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11006976 }
6977 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07006978 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11006979 if (test_bit(Replacement, &rdev->flags))
6980 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11006981 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982 }
NeilBrownf97fcad2014-12-15 12:56:59 +11006983 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984
6985 if (!list_empty(&mddev->disks)) {
6986 if (mddev->pers)
6987 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006988 (unsigned long long)
6989 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 else
6991 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006992 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006994 if (mddev->persistent) {
6995 if (mddev->major_version != 0 ||
6996 mddev->minor_version != 90) {
6997 seq_printf(seq," super %d.%d",
6998 mddev->major_version,
6999 mddev->minor_version);
7000 }
NeilBrowne6910632008-02-06 01:39:51 -08007001 } else if (mddev->external)
7002 seq_printf(seq, " super external:%s",
7003 mddev->metadata_type);
7004 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007005 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006
7007 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007008 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007009 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007010 if (mddev->pers->sync_request) {
7011 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007012 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007013 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007014 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007015 seq_printf(seq, "\tresync=DELAYED\n ");
7016 else if (mddev->recovery_cp < MaxSector)
7017 seq_printf(seq, "\tresync=PENDING\n ");
7018 }
NeilBrown32a76272005-06-21 17:17:14 -07007019 } else
7020 seq_printf(seq, "\n ");
7021
NeilBrown57148962012-03-19 12:46:40 +11007022 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023
7024 seq_printf(seq, "\n");
7025 }
NeilBrown36d091f2014-12-15 12:56:58 +11007026 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007027
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028 return 0;
7029}
7030
Jan Engelhardt110518b2009-05-07 12:49:37 +10007031static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 .start = md_seq_start,
7033 .next = md_seq_next,
7034 .stop = md_seq_stop,
7035 .show = md_seq_show,
7036};
7037
7038static int md_seq_open(struct inode *inode, struct file *file)
7039{
Kay Sieversf1514632011-07-12 20:48:39 +02007040 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 int error;
7042
7043 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007044 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007045 return error;
7046
7047 seq = file->private_data;
7048 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 return error;
7050}
7051
NeilBrowne2f23b62014-04-09 14:33:51 +10007052static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007053static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7054{
Kay Sieversf1514632011-07-12 20:48:39 +02007055 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007056 int mask;
7057
NeilBrowne2f23b62014-04-09 14:33:51 +10007058 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007059 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007060 poll_wait(filp, &md_event_waiters, wait);
7061
7062 /* always allow read */
7063 mask = POLLIN | POLLRDNORM;
7064
Kay Sieversf1514632011-07-12 20:48:39 +02007065 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007066 mask |= POLLERR | POLLPRI;
7067 return mask;
7068}
7069
Arjan van de Venfa027c22007-02-12 00:55:33 -08007070static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007071 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072 .open = md_seq_open,
7073 .read = seq_read,
7074 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007075 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007076 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077};
7078
NeilBrown84fc4b52011-10-11 16:49:58 +11007079int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080{
Chao Yu50bd3772014-09-25 15:28:34 +08007081 printk(KERN_INFO "md: %s personality registered for level %d\n",
7082 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007084 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 spin_unlock(&pers_lock);
7086 return 0;
7087}
NeilBrown6c144d32014-09-30 16:15:38 +10007088EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089
NeilBrown84fc4b52011-10-11 16:49:58 +11007090int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091{
NeilBrown2604b702006-01-06 00:20:36 -08007092 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007094 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 spin_unlock(&pers_lock);
7096 return 0;
7097}
NeilBrown6c144d32014-09-30 16:15:38 +10007098EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099
NeilBrownfd01b882011-10-11 16:47:53 +11007100static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007102 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007104 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105
7106 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007107 rcu_read_lock();
7108 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007110 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7111 (int)part_stat_read(&disk->part0, sectors[1]) -
7112 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007113 /* sync IO will cause sync_io to increase before the disk_stats
7114 * as sync_io is counted when a request starts, and
7115 * disk_stats is counted when it completes.
7116 * So resync activity will cause curr_events to be smaller than
7117 * when there was no such activity.
7118 * non-sync IO will cause disk_stat to increase without
7119 * increasing sync_io so curr_events will (eventually)
7120 * be larger than it was before. Once it becomes
7121 * substantially larger, the test below will cause
7122 * the array to appear non-idle, and resync will slow
7123 * down.
7124 * If there is a lot of outstanding resync activity when
7125 * we set last_event to curr_events, then all that activity
7126 * completing might cause the array to appear non-idle
7127 * and resync will be slowed down even though there might
7128 * not have been non-resync activity. This will only
7129 * happen once though. 'last_events' will soon reflect
7130 * the state where there is little or no outstanding
7131 * resync requests, and further resync activity will
7132 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007133 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007135 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 rdev->last_events = curr_events;
7137 idle = 0;
7138 }
7139 }
NeilBrown4b809912008-07-21 17:05:25 +10007140 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 return idle;
7142}
7143
NeilBrownfd01b882011-10-11 16:47:53 +11007144void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145{
7146 /* another "blocks" (512byte) blocks have been synced */
7147 atomic_sub(blocks, &mddev->recovery_active);
7148 wake_up(&mddev->recovery_wait);
7149 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007150 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007151 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 md_wakeup_thread(mddev->thread);
7153 // stop recovery, signal do_sync ....
7154 }
7155}
NeilBrown6c144d32014-09-30 16:15:38 +10007156EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157
NeilBrown06d91a52005-06-21 17:17:12 -07007158/* md_write_start(mddev, bi)
7159 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007160 * in superblock) before writing, schedule a superblock update
7161 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007162 */
NeilBrownfd01b882011-10-11 16:47:53 +11007163void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164{
Neil Brown0fd62b82008-06-28 08:31:36 +10007165 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007166 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007167 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007168
NeilBrownf91de922005-11-08 21:39:36 -08007169 BUG_ON(mddev->ro == 1);
7170 if (mddev->ro == 2) {
7171 /* need to switch to read/write */
7172 mddev->ro = 0;
7173 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7174 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007175 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007176 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007177 }
NeilBrown06d91a52005-06-21 17:17:12 -07007178 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007179 if (mddev->safemode == 1)
7180 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007181 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007182 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007183 if (mddev->in_sync) {
7184 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007185 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007186 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007187 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007188 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007189 }
NeilBrown85572d72014-12-15 12:56:56 +11007190 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007191 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007192 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007193 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007194 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007195 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196}
NeilBrown6c144d32014-09-30 16:15:38 +10007197EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198
NeilBrownfd01b882011-10-11 16:47:53 +11007199void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200{
7201 if (atomic_dec_and_test(&mddev->writes_pending)) {
7202 if (mddev->safemode == 2)
7203 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007204 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7206 }
7207}
NeilBrown6c144d32014-09-30 16:15:38 +10007208EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209
NeilBrown2a2275d2007-01-26 00:57:11 -08007210/* md_allow_write(mddev)
7211 * Calling this ensures that the array is marked 'active' so that writes
7212 * may proceed without blocking. It is important to call this before
7213 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7214 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007215 *
7216 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7217 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007218 */
NeilBrownfd01b882011-10-11 16:47:53 +11007219int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007220{
7221 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007222 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007223 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007224 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007225 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007226 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007227
NeilBrown85572d72014-12-15 12:56:56 +11007228 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007229 if (mddev->in_sync) {
7230 mddev->in_sync = 0;
7231 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007232 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007233 if (mddev->safemode_delay &&
7234 mddev->safemode == 0)
7235 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007236 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007237 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007238 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007239 } else
NeilBrown85572d72014-12-15 12:56:56 +11007240 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007241
NeilBrown070dc6d2010-08-30 17:33:34 +10007242 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007243 return -EAGAIN;
7244 else
7245 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007246}
7247EXPORT_SYMBOL_GPL(md_allow_write);
7248
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249#define SYNC_MARKS 10
7250#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007251#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007252void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253{
Shaohua Li4ed87312012-10-11 13:34:00 +11007254 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007255 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256 unsigned int currspeed = 0,
7257 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007258 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007260 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261 sector_t mark_cnt[SYNC_MARKS];
7262 int last_mark,m;
7263 struct list_head *tmp;
7264 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007265 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007266 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007267 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007268 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269
7270 /* just incase thread restarts... */
7271 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7272 return;
NeilBrown3991b312014-05-28 13:39:23 +10007273 if (mddev->ro) {/* never try to sync a read-only array */
7274 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007275 return;
NeilBrown3991b312014-05-28 13:39:23 +10007276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277
NeilBrown61df9d92006-10-03 01:15:57 -07007278 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007279 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007280 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007281 action = "check";
7282 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007283 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007284 action = "repair";
7285 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007286 desc = "resync";
7287 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7288 desc = "reshape";
7289 else
7290 desc = "recovery";
7291
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007292 mddev->last_sync_action = action ?: desc;
7293
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294 /* we overload curr_resync somewhat here.
7295 * 0 == not engaged in resync at all
7296 * 2 == checking that there is no conflict with another sync
7297 * 1 == like 2, but have yielded to allow conflicting resync to
7298 * commense
7299 * other == active in resync - this many blocks
7300 *
7301 * Before starting a resync we must have set curr_resync to
7302 * 2, and then checked that every "conflicting" array has curr_resync
7303 * less than ours. When we find one that is the same or higher
7304 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7305 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7306 * This will mean we have to start checking from the beginning again.
7307 *
7308 */
7309
7310 do {
7311 mddev->curr_resync = 2;
7312
7313 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007314 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007316 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 if (mddev2 == mddev)
7318 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007319 if (!mddev->parallel_resync
7320 && mddev2->curr_resync
7321 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322 DEFINE_WAIT(wq);
7323 if (mddev < mddev2 && mddev->curr_resync == 2) {
7324 /* arbitrarily yield */
7325 mddev->curr_resync = 1;
7326 wake_up(&resync_wait);
7327 }
7328 if (mddev > mddev2 && mddev->curr_resync == 1)
7329 /* no need to wait here, we can wait the next
7330 * time 'round when curr_resync == 2
7331 */
7332 continue;
NeilBrown97441972008-09-19 11:49:54 +10007333 /* We need to wait 'interruptible' so as not to
7334 * contribute to the load average, and not to
7335 * be caught by 'softlockup'
7336 */
7337 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007338 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007339 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007340 printk(KERN_INFO "md: delaying %s of %s"
7341 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007343 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007345 if (signal_pending(current))
7346 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347 schedule();
7348 finish_wait(&resync_wait, &wq);
7349 goto try_again;
7350 }
7351 finish_wait(&resync_wait, &wq);
7352 }
7353 }
7354 } while (mddev->curr_resync < 2);
7355
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007356 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007357 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007359 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 */
7361 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007362 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007363 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007364 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7365 j = mddev->resync_min;
7366 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007367 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007368
NeilBrownccfcc3c2006-03-27 01:18:09 -08007369 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007370 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007371 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007373 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007374 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007375 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007376 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007377 if (rdev->raid_disk >= 0 &&
7378 !test_bit(Faulty, &rdev->flags) &&
7379 !test_bit(In_sync, &rdev->flags) &&
7380 rdev->recovery_offset < j)
7381 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007382 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007383
7384 /* If there is a bitmap, we need to make sure all
7385 * writes that started before we added a spare
7386 * complete before we start doing a recovery.
7387 * Otherwise the write might complete and (via
7388 * bitmap_endwrite) set a bit in the bitmap after the
7389 * recovery has checked that bit and skipped that
7390 * region.
7391 */
7392 if (mddev->bitmap) {
7393 mddev->pers->quiesce(mddev, 1);
7394 mddev->pers->quiesce(mddev, 0);
7395 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397
NeilBrown61df9d92006-10-03 01:15:57 -07007398 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7399 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7400 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007401 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007402 "(but not more than %d KB/sec) for %s.\n",
7403 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404
NeilBrowneea1bf32009-03-31 14:27:02 +11007405 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007406
NeilBrown57afd892005-06-21 17:17:13 -07007407 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 for (m = 0; m < SYNC_MARKS; m++) {
7409 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007410 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 }
7412 last_mark = 0;
7413 mddev->resync_mark = mark[last_mark];
7414 mddev->resync_mark_cnt = mark_cnt[last_mark];
7415
7416 /*
7417 * Tune reconstruction:
7418 */
7419 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007420 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7421 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422
7423 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424 last_check = 0;
7425
7426 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007427 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007428 "md: resuming %s of %s from checkpoint.\n",
7429 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007431 } else
7432 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007433 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007434 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7435 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007436 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437
majianpeng7c2c57c2012-07-03 12:12:26 +10007438 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007440 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441
NeilBrown57afd892005-06-21 17:17:13 -07007442 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007443
NeilBrown7a91ee12009-05-26 12:57:21 +10007444 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7445 ((mddev->curr_resync > mddev->curr_resync_completed &&
7446 (mddev->curr_resync - mddev->curr_resync_completed)
7447 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007448 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007449 (j - mddev->curr_resync_completed)*2
7450 >= mddev->resync_max - mddev->curr_resync_completed
7451 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007452 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007453 wait_event(mddev->recovery_wait,
7454 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007455 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007456 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7457 j > mddev->recovery_cp)
7458 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007459 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007460 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007461 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007462 }
NeilBrownacb180b2009-04-14 16:28:34 +10007463
NeilBrownc91abf52013-11-19 12:02:01 +11007464 while (j >= mddev->resync_max &&
7465 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007466 /* As this condition is controlled by user-space,
7467 * we can block indefinitely, so use '_interruptible'
7468 * to avoid triggering warnings.
7469 */
7470 flush_signals(current); /* just in case */
7471 wait_event_interruptible(mddev->recovery_wait,
7472 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007473 || test_bit(MD_RECOVERY_INTR,
7474 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007475 }
NeilBrownacb180b2009-04-14 16:28:34 +10007476
NeilBrownc91abf52013-11-19 12:02:01 +11007477 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7478 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007479
NeilBrown57afd892005-06-21 17:17:13 -07007480 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007481 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007482 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007483 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007484 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485 }
NeilBrown57afd892005-06-21 17:17:13 -07007486
7487 if (!skipped) { /* actual IO requested */
7488 io_sectors += sectors;
7489 atomic_add(sectors, &mddev->recovery_active);
7490 }
7491
NeilBrowne875ece2011-07-28 11:39:24 +10007492 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7493 break;
7494
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007496 if (j > 2)
7497 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007498 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007499 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007500 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007501 * visible in /proc/mdstat
7502 */
7503 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007504
7505 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506 continue;
7507
NeilBrown57afd892005-06-21 17:17:13 -07007508 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509 repeat:
7510 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7511 /* step marks */
7512 int next = (last_mark+1) % SYNC_MARKS;
7513
7514 mddev->resync_mark = mark[next];
7515 mddev->resync_mark_cnt = mark_cnt[next];
7516 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007517 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 last_mark = next;
7519 }
7520
NeilBrownc91abf52013-11-19 12:02:01 +11007521 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523
7524 /*
7525 * this loop exits only if either when we are slower than
7526 * the 'hard' speed limit, or the system was IO-idle for
7527 * a jiffy.
7528 * the system might be non-idle CPU-wise, but we only care
7529 * about not overloading the IO subsystem. (things like an
7530 * e2fsck being done on the RAID array should execute fast)
7531 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532 cond_resched();
7533
Xiao Niac7e50a2014-08-07 09:37:41 -04007534 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7535 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07007536 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537
NeilBrown88202a02006-01-06 00:21:36 -08007538 if (currspeed > speed_min(mddev)) {
7539 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007540 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007541 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542 goto repeat;
7543 }
7544 }
7545 }
NeilBrownc91abf52013-11-19 12:02:01 +11007546 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7547 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7548 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549 /*
7550 * this also signals 'finished resyncing' to md_stop
7551 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007552 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7554
7555 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007556 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557
NeilBrowndfc70642008-05-23 13:04:39 -07007558 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007559 mddev->curr_resync > 2) {
7560 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7561 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7562 if (mddev->curr_resync >= mddev->recovery_cp) {
7563 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007564 "md: checkpointing %s of %s.\n",
7565 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007566 if (test_bit(MD_RECOVERY_ERROR,
7567 &mddev->recovery))
7568 mddev->recovery_cp =
7569 mddev->curr_resync_completed;
7570 else
7571 mddev->recovery_cp =
7572 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007573 }
7574 } else
7575 mddev->recovery_cp = MaxSector;
7576 } else {
7577 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7578 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007579 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007580 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007581 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007582 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007583 !test_bit(Faulty, &rdev->flags) &&
7584 !test_bit(In_sync, &rdev->flags) &&
7585 rdev->recovery_offset < mddev->curr_resync)
7586 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007587 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007590 skip:
NeilBrown17571282006-12-10 02:20:52 -08007591 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592
NeilBrown23da4222014-12-15 12:57:01 +11007593 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11007594 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7595 /* We completed so min/max setting can be forgotten if used. */
7596 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7597 mddev->resync_min = 0;
7598 mddev->resync_max = MaxSector;
7599 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7600 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007601 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11007602 spin_unlock(&mddev->lock);
7603
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604 wake_up(&resync_wait);
7605 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7606 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007607 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007608}
NeilBrown29269552006-03-27 01:18:10 -08007609EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610
NeilBrown746d3202013-04-24 11:42:41 +10007611static int remove_and_add_spares(struct mddev *mddev,
7612 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007613{
NeilBrown3cb03002011-10-11 16:45:26 +11007614 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007615 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007616 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007617
NeilBrowndafb20f2012-03-19 12:46:39 +11007618 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007619 if ((this == NULL || rdev == this) &&
7620 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007621 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007622 (test_bit(Faulty, &rdev->flags) ||
7623 ! test_bit(In_sync, &rdev->flags)) &&
7624 atomic_read(&rdev->nr_pending)==0) {
7625 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007626 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007627 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007628 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007629 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007630 }
7631 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007632 if (removed && mddev->kobj.sd)
7633 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007634
NeilBrown746d3202013-04-24 11:42:41 +10007635 if (this)
7636 goto no_add;
7637
NeilBrowndafb20f2012-03-19 12:46:39 +11007638 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007639 if (rdev->raid_disk >= 0 &&
7640 !test_bit(In_sync, &rdev->flags) &&
7641 !test_bit(Faulty, &rdev->flags))
7642 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007643 if (rdev->raid_disk >= 0)
7644 continue;
7645 if (test_bit(Faulty, &rdev->flags))
7646 continue;
7647 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11007648 ! (rdev->saved_raid_disk >= 0 &&
7649 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10007650 continue;
7651
NeilBrown7eb41882014-01-14 15:55:14 +11007652 if (rdev->saved_raid_disk < 0)
7653 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007654 if (mddev->pers->
7655 hot_add_disk(mddev, rdev) == 0) {
7656 if (sysfs_link_rdev(mddev, rdev))
7657 /* failure here is OK */;
7658 spares++;
7659 md_new_event(mddev);
7660 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007661 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007662 }
NeilBrown746d3202013-04-24 11:42:41 +10007663no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007664 if (removed)
7665 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007666 return spares;
7667}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007668
NeilBrownac05f252014-09-30 08:10:42 +10007669static void md_start_sync(struct work_struct *ws)
7670{
7671 struct mddev *mddev = container_of(ws, struct mddev, del_work);
7672
7673 mddev->sync_thread = md_register_thread(md_do_sync,
7674 mddev,
7675 "resync");
7676 if (!mddev->sync_thread) {
7677 printk(KERN_ERR "%s: could not start resync"
7678 " thread...\n",
7679 mdname(mddev));
7680 /* leave the spares where they are, it shouldn't hurt */
7681 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7682 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7683 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7684 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7685 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11007686 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10007687 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7688 &mddev->recovery))
7689 if (mddev->sysfs_action)
7690 sysfs_notify_dirent_safe(mddev->sysfs_action);
7691 } else
7692 md_wakeup_thread(mddev->sync_thread);
7693 sysfs_notify_dirent_safe(mddev->sysfs_action);
7694 md_new_event(mddev);
7695}
7696
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697/*
7698 * This routine is regularly called by all per-raid-array threads to
7699 * deal with generic issues like resync and super-block update.
7700 * Raid personalities that don't have a thread (linear/raid0) do not
7701 * need this as they never do any recovery or update the superblock.
7702 *
7703 * It does not do any resync itself, but rather "forks" off other threads
7704 * to do that as needed.
7705 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7706 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007707 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007708 * and wakeups up this thread which will reap the thread and finish up.
7709 * This thread also removes any faulty devices (with nr_pending == 0).
7710 *
7711 * The overall approach is:
7712 * 1/ if the superblock needs updating, update it.
7713 * 2/ If a recovery thread is running, don't do anything else.
7714 * 3/ If recovery has finished, clean up, possibly marking spares active.
7715 * 4/ If there are any faulty devices, remove them.
7716 * 5/ If array is degraded, try to add spares devices
7717 * 6/ If array has spares or is not in-sync, start a resync thread.
7718 */
NeilBrownfd01b882011-10-11 16:47:53 +11007719void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007721 if (mddev->suspended)
7722 return;
7723
NeilBrown5f404022005-06-21 17:17:16 -07007724 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007725 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007726
NeilBrownfca4d842005-06-21 17:17:11 -07007727 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007728 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007729 printk(KERN_INFO "md: %s in immediate safe mode\n",
7730 mdname(mddev));
7731 mddev->safemode = 2;
7732 }
7733 flush_signals(current);
7734 }
7735
NeilBrownc89a8ee2008-08-05 15:54:13 +10007736 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7737 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11007739 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007741 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007742 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007743 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7744 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745 ))
7746 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007747
NeilBrowndf5b89b2006-03-27 01:18:20 -08007748 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007749 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007750
NeilBrownc89a8ee2008-08-05 15:54:13 +10007751 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10007752 /* On a read-only array we can:
7753 * - remove failed devices
7754 * - add already-in_sync devices if the array itself
7755 * is in-sync.
7756 * As we only add devices that are already in-sync,
7757 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10007758 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10007759 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11007760 /* There is no thread, but we need to call
7761 * ->spare_active and clear saved_raid_disk
7762 */
NeilBrown2ac295a2014-05-29 11:40:03 +10007763 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11007764 md_reap_sync_thread(mddev);
7765 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownc89a8ee2008-08-05 15:54:13 +10007766 goto unlock;
7767 }
7768
NeilBrown31a59e32008-04-30 00:52:30 -07007769 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007770 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11007771 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07007772 if (mddev->safemode &&
7773 !atomic_read(&mddev->writes_pending) &&
7774 !mddev->in_sync &&
7775 mddev->recovery_cp == MaxSector) {
7776 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007777 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007778 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007779 }
7780 if (mddev->safemode == 1)
7781 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11007782 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007783 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007784 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007785 }
NeilBrownfca4d842005-06-21 17:17:11 -07007786
NeilBrown7a0a5352013-08-27 16:28:23 +10007787 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07007788 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007789
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7791 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7792 /* resync/recovery still happening */
7793 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7794 goto unlock;
7795 }
7796 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007797 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007798 goto unlock;
7799 }
Neil Brown72a23c22008-06-28 08:31:41 +10007800 /* Set RUNNING before clearing NEEDED to avoid
7801 * any transients in the value of "sync_action".
7802 */
NeilBrown72f36d52012-10-11 14:25:57 +11007803 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11007804 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10007805 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11007806 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08007807 /* Clear some bits that don't mean anything, but
7808 * might be left set
7809 */
NeilBrown24dd4692005-11-08 21:39:26 -08007810 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7811 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812
NeilBrowned209582012-04-24 10:23:14 +10007813 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7814 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10007815 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816 /* no recovery is running.
7817 * remove any failed drives, then
7818 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007819 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820 * the personality to fail the re-add.
7821 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007823 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007824 if (mddev->pers->check_reshape == NULL ||
7825 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007826 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10007827 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007828 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007829 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10007830 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007831 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7832 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007833 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007834 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007835 } else if (mddev->recovery_cp < MaxSector) {
7836 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007837 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007838 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7839 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10007840 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08007841
Linus Torvalds1da177e2005-04-16 15:20:36 -07007842 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007843 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007844 /* We are adding a device or devices to an array
7845 * which has the bitmap stored on all devices.
7846 * So make sure all bitmap pages get written
7847 */
7848 bitmap_write_all(mddev->bitmap);
7849 }
NeilBrownac05f252014-09-30 08:10:42 +10007850 INIT_WORK(&mddev->del_work, md_start_sync);
7851 queue_work(md_misc_wq, &mddev->del_work);
7852 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853 }
NeilBrownac05f252014-09-30 08:10:42 +10007854 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10007855 if (!mddev->sync_thread) {
7856 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11007857 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10007858 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7859 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007860 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007861 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007862 }
NeilBrownac05f252014-09-30 08:10:42 +10007863 unlock:
7864 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865 mddev_unlock(mddev);
7866 }
7867}
NeilBrown6c144d32014-09-30 16:15:38 +10007868EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007870void md_reap_sync_thread(struct mddev *mddev)
7871{
7872 struct md_rdev *rdev;
7873
7874 /* resync has finished, collect result */
7875 md_unregister_thread(&mddev->sync_thread);
7876 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7877 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7878 /* success...*/
7879 /* activate any spares */
7880 if (mddev->pers->spare_active(mddev)) {
7881 sysfs_notify(&mddev->kobj, NULL,
7882 "degraded");
7883 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7884 }
7885 }
7886 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7887 mddev->pers->finish_reshape)
7888 mddev->pers->finish_reshape(mddev);
7889
7890 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11007891 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007892 */
NeilBrownf4667222013-12-09 12:04:56 +11007893 if (!mddev->degraded)
7894 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007895 rdev->saved_raid_disk = -1;
7896
7897 md_update_sb(mddev, 1);
7898 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7899 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7900 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7901 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7902 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11007903 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007904 /* flag recovery needed just to double check */
7905 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7906 sysfs_notify_dirent_safe(mddev->sysfs_action);
7907 md_new_event(mddev);
7908 if (mddev->event_work.func)
7909 queue_work(md_misc_wq, &mddev->event_work);
7910}
NeilBrown6c144d32014-09-30 16:15:38 +10007911EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007912
NeilBrownfd01b882011-10-11 16:47:53 +11007913void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007914{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007915 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007916 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007917 !test_bit(Blocked, &rdev->flags) &&
7918 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007919 msecs_to_jiffies(5000));
7920 rdev_dec_pending(rdev, mddev);
7921}
7922EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7923
NeilBrownc6563a82012-05-21 09:27:00 +10007924void md_finish_reshape(struct mddev *mddev)
7925{
7926 /* called be personality module when reshape completes. */
7927 struct md_rdev *rdev;
7928
7929 rdev_for_each(rdev, mddev) {
7930 if (rdev->data_offset > rdev->new_data_offset)
7931 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7932 else
7933 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7934 rdev->data_offset = rdev->new_data_offset;
7935 }
7936}
7937EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10007938
7939/* Bad block management.
7940 * We can record which blocks on each device are 'bad' and so just
7941 * fail those blocks, or that stripe, rather than the whole device.
7942 * Entries in the bad-block table are 64bits wide. This comprises:
7943 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7944 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7945 * A 'shift' can be set so that larger blocks are tracked and
7946 * consequently larger devices can be covered.
7947 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7948 *
7949 * Locking of the bad-block table uses a seqlock so md_is_badblock
7950 * might need to retry if it is very unlucky.
7951 * We will sometimes want to check for bad blocks in a bi_end_io function,
7952 * so we use the write_seqlock_irq variant.
7953 *
7954 * When looking for a bad block we specify a range and want to
7955 * know if any block in the range is bad. So we binary-search
7956 * to the last range that starts at-or-before the given endpoint,
7957 * (or "before the sector after the target range")
7958 * then see if it ends after the given start.
7959 * We return
7960 * 0 if there are no known bad blocks in the range
7961 * 1 if there are known bad block which are all acknowledged
7962 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7963 * plus the start/length of the first bad section we overlap.
7964 */
7965int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7966 sector_t *first_bad, int *bad_sectors)
7967{
7968 int hi;
majianpengab056132012-11-06 17:13:44 +08007969 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10007970 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08007971 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10007972 sector_t target = s + sectors;
7973 unsigned seq;
7974
7975 if (bb->shift > 0) {
7976 /* round the start down, and the end up */
7977 s >>= bb->shift;
7978 target += (1<<bb->shift) - 1;
7979 target >>= bb->shift;
7980 sectors = target - s;
7981 }
7982 /* 'target' is now the first block after the bad range */
7983
7984retry:
7985 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08007986 lo = 0;
7987 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10007988 hi = bb->count;
7989
7990 /* Binary search between lo and hi for 'target'
7991 * i.e. for the last range that starts before 'target'
7992 */
7993 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7994 * are known not to be the last range before target.
7995 * VARIANT: hi-lo is the number of possible
7996 * ranges, and decreases until it reaches 1
7997 */
7998 while (hi - lo > 1) {
7999 int mid = (lo + hi) / 2;
8000 sector_t a = BB_OFFSET(p[mid]);
8001 if (a < target)
8002 /* This could still be the one, earlier ranges
8003 * could not. */
8004 lo = mid;
8005 else
8006 /* This and later ranges are definitely out. */
8007 hi = mid;
8008 }
8009 /* 'lo' might be the last that started before target, but 'hi' isn't */
8010 if (hi > lo) {
8011 /* need to check all range that end after 's' to see if
8012 * any are unacknowledged.
8013 */
8014 while (lo >= 0 &&
8015 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8016 if (BB_OFFSET(p[lo]) < target) {
8017 /* starts before the end, and finishes after
8018 * the start, so they must overlap
8019 */
8020 if (rv != -1 && BB_ACK(p[lo]))
8021 rv = 1;
8022 else
8023 rv = -1;
8024 *first_bad = BB_OFFSET(p[lo]);
8025 *bad_sectors = BB_LEN(p[lo]);
8026 }
8027 lo--;
8028 }
8029 }
8030
8031 if (read_seqretry(&bb->lock, seq))
8032 goto retry;
8033
8034 return rv;
8035}
8036EXPORT_SYMBOL_GPL(md_is_badblock);
8037
8038/*
8039 * Add a range of bad blocks to the table.
8040 * This might extend the table, or might contract it
8041 * if two adjacent ranges can be merged.
8042 * We binary-search to find the 'insertion' point, then
8043 * decide how best to handle it.
8044 */
8045static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8046 int acknowledged)
8047{
8048 u64 *p;
8049 int lo, hi;
8050 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008051 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008052
8053 if (bb->shift < 0)
8054 /* badblocks are disabled */
8055 return 0;
8056
8057 if (bb->shift) {
8058 /* round the start down, and the end up */
8059 sector_t next = s + sectors;
8060 s >>= bb->shift;
8061 next += (1<<bb->shift) - 1;
8062 next >>= bb->shift;
8063 sectors = next - s;
8064 }
8065
Bian Yu905b0292013-10-12 01:10:03 -04008066 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008067
8068 p = bb->page;
8069 lo = 0;
8070 hi = bb->count;
8071 /* Find the last range that starts at-or-before 's' */
8072 while (hi - lo > 1) {
8073 int mid = (lo + hi) / 2;
8074 sector_t a = BB_OFFSET(p[mid]);
8075 if (a <= s)
8076 lo = mid;
8077 else
8078 hi = mid;
8079 }
8080 if (hi > lo && BB_OFFSET(p[lo]) > s)
8081 hi = lo;
8082
8083 if (hi > lo) {
8084 /* we found a range that might merge with the start
8085 * of our new range
8086 */
8087 sector_t a = BB_OFFSET(p[lo]);
8088 sector_t e = a + BB_LEN(p[lo]);
8089 int ack = BB_ACK(p[lo]);
8090 if (e >= s) {
8091 /* Yes, we can merge with a previous range */
8092 if (s == a && s + sectors >= e)
8093 /* new range covers old */
8094 ack = acknowledged;
8095 else
8096 ack = ack && acknowledged;
8097
8098 if (e < s + sectors)
8099 e = s + sectors;
8100 if (e - a <= BB_MAX_LEN) {
8101 p[lo] = BB_MAKE(a, e-a, ack);
8102 s = e;
8103 } else {
8104 /* does not all fit in one range,
8105 * make p[lo] maximal
8106 */
8107 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8108 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8109 s = a + BB_MAX_LEN;
8110 }
8111 sectors = e - s;
8112 }
8113 }
8114 if (sectors && hi < bb->count) {
8115 /* 'hi' points to the first range that starts after 's'.
8116 * Maybe we can merge with the start of that range */
8117 sector_t a = BB_OFFSET(p[hi]);
8118 sector_t e = a + BB_LEN(p[hi]);
8119 int ack = BB_ACK(p[hi]);
8120 if (a <= s + sectors) {
8121 /* merging is possible */
8122 if (e <= s + sectors) {
8123 /* full overlap */
8124 e = s + sectors;
8125 ack = acknowledged;
8126 } else
8127 ack = ack && acknowledged;
8128
8129 a = s;
8130 if (e - a <= BB_MAX_LEN) {
8131 p[hi] = BB_MAKE(a, e-a, ack);
8132 s = e;
8133 } else {
8134 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8135 s = a + BB_MAX_LEN;
8136 }
8137 sectors = e - s;
8138 lo = hi;
8139 hi++;
8140 }
8141 }
8142 if (sectors == 0 && hi < bb->count) {
8143 /* we might be able to combine lo and hi */
8144 /* Note: 's' is at the end of 'lo' */
8145 sector_t a = BB_OFFSET(p[hi]);
8146 int lolen = BB_LEN(p[lo]);
8147 int hilen = BB_LEN(p[hi]);
8148 int newlen = lolen + hilen - (s - a);
8149 if (s >= a && newlen < BB_MAX_LEN) {
8150 /* yes, we can combine them */
8151 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8152 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8153 memmove(p + hi, p + hi + 1,
8154 (bb->count - hi - 1) * 8);
8155 bb->count--;
8156 }
8157 }
8158 while (sectors) {
8159 /* didn't merge (it all).
8160 * Need to add a range just before 'hi' */
8161 if (bb->count >= MD_MAX_BADBLOCKS) {
8162 /* No room for more */
8163 rv = 0;
8164 break;
8165 } else {
8166 int this_sectors = sectors;
8167 memmove(p + hi + 1, p + hi,
8168 (bb->count - hi) * 8);
8169 bb->count++;
8170
8171 if (this_sectors > BB_MAX_LEN)
8172 this_sectors = BB_MAX_LEN;
8173 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8174 sectors -= this_sectors;
8175 s += this_sectors;
8176 }
8177 }
8178
8179 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008180 if (!acknowledged)
8181 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008182 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008183
8184 return rv;
8185}
8186
NeilBrown3cb03002011-10-11 16:45:26 +11008187int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008188 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008189{
NeilBrownc6563a82012-05-21 09:27:00 +10008190 int rv;
8191 if (is_new)
8192 s += rdev->new_data_offset;
8193 else
8194 s += rdev->data_offset;
8195 rv = md_set_badblocks(&rdev->badblocks,
8196 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008197 if (rv) {
8198 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008199 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008200 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8201 md_wakeup_thread(rdev->mddev->thread);
8202 }
8203 return rv;
8204}
8205EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8206
8207/*
8208 * Remove a range of bad blocks from the table.
8209 * This may involve extending the table if we spilt a region,
8210 * but it must not fail. So if the table becomes full, we just
8211 * drop the remove request.
8212 */
8213static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8214{
8215 u64 *p;
8216 int lo, hi;
8217 sector_t target = s + sectors;
8218 int rv = 0;
8219
8220 if (bb->shift > 0) {
8221 /* When clearing we round the start up and the end down.
8222 * This should not matter as the shift should align with
8223 * the block size and no rounding should ever be needed.
8224 * However it is better the think a block is bad when it
8225 * isn't than to think a block is not bad when it is.
8226 */
8227 s += (1<<bb->shift) - 1;
8228 s >>= bb->shift;
8229 target >>= bb->shift;
8230 sectors = target - s;
8231 }
8232
8233 write_seqlock_irq(&bb->lock);
8234
8235 p = bb->page;
8236 lo = 0;
8237 hi = bb->count;
8238 /* Find the last range that starts before 'target' */
8239 while (hi - lo > 1) {
8240 int mid = (lo + hi) / 2;
8241 sector_t a = BB_OFFSET(p[mid]);
8242 if (a < target)
8243 lo = mid;
8244 else
8245 hi = mid;
8246 }
8247 if (hi > lo) {
8248 /* p[lo] is the last range that could overlap the
8249 * current range. Earlier ranges could also overlap,
8250 * but only this one can overlap the end of the range.
8251 */
8252 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8253 /* Partial overlap, leave the tail of this range */
8254 int ack = BB_ACK(p[lo]);
8255 sector_t a = BB_OFFSET(p[lo]);
8256 sector_t end = a + BB_LEN(p[lo]);
8257
8258 if (a < s) {
8259 /* we need to split this range */
8260 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008261 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008262 goto out;
8263 }
8264 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8265 bb->count++;
8266 p[lo] = BB_MAKE(a, s-a, ack);
8267 lo++;
8268 }
8269 p[lo] = BB_MAKE(target, end - target, ack);
8270 /* there is no longer an overlap */
8271 hi = lo;
8272 lo--;
8273 }
8274 while (lo >= 0 &&
8275 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8276 /* This range does overlap */
8277 if (BB_OFFSET(p[lo]) < s) {
8278 /* Keep the early parts of this range. */
8279 int ack = BB_ACK(p[lo]);
8280 sector_t start = BB_OFFSET(p[lo]);
8281 p[lo] = BB_MAKE(start, s - start, ack);
8282 /* now low doesn't overlap, so.. */
8283 break;
8284 }
8285 lo--;
8286 }
8287 /* 'lo' is strictly before, 'hi' is strictly after,
8288 * anything between needs to be discarded
8289 */
8290 if (hi - lo > 1) {
8291 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8292 bb->count -= (hi - lo - 1);
8293 }
8294 }
8295
8296 bb->changed = 1;
8297out:
8298 write_sequnlock_irq(&bb->lock);
8299 return rv;
8300}
8301
NeilBrownc6563a82012-05-21 09:27:00 +10008302int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8303 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008304{
NeilBrownc6563a82012-05-21 09:27:00 +10008305 if (is_new)
8306 s += rdev->new_data_offset;
8307 else
8308 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008309 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008310 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008311}
8312EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8313
8314/*
8315 * Acknowledge all bad blocks in a list.
8316 * This only succeeds if ->changed is clear. It is used by
8317 * in-kernel metadata updates
8318 */
8319void md_ack_all_badblocks(struct badblocks *bb)
8320{
8321 if (bb->page == NULL || bb->changed)
8322 /* no point even trying */
8323 return;
8324 write_seqlock_irq(&bb->lock);
8325
majianpengecb178b2012-03-19 12:46:42 +11008326 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008327 u64 *p = bb->page;
8328 int i;
8329 for (i = 0; i < bb->count ; i++) {
8330 if (!BB_ACK(p[i])) {
8331 sector_t start = BB_OFFSET(p[i]);
8332 int len = BB_LEN(p[i]);
8333 p[i] = BB_MAKE(start, len, 1);
8334 }
8335 }
NeilBrownde393cd2011-07-28 11:31:48 +10008336 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008337 }
8338 write_sequnlock_irq(&bb->lock);
8339}
8340EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8341
NeilBrown16c791a2011-07-28 11:31:47 +10008342/* sysfs access to bad-blocks list.
8343 * We present two files.
8344 * 'bad-blocks' lists sector numbers and lengths of ranges that
8345 * are recorded as bad. The list is truncated to fit within
8346 * the one-page limit of sysfs.
8347 * Writing "sector length" to this file adds an acknowledged
8348 * bad block list.
8349 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8350 * been acknowledged. Writing to this file adds bad blocks
8351 * without acknowledging them. This is largely for testing.
8352 */
8353
8354static ssize_t
8355badblocks_show(struct badblocks *bb, char *page, int unack)
8356{
8357 size_t len;
8358 int i;
8359 u64 *p = bb->page;
8360 unsigned seq;
8361
8362 if (bb->shift < 0)
8363 return 0;
8364
8365retry:
8366 seq = read_seqbegin(&bb->lock);
8367
8368 len = 0;
8369 i = 0;
8370
8371 while (len < PAGE_SIZE && i < bb->count) {
8372 sector_t s = BB_OFFSET(p[i]);
8373 unsigned int length = BB_LEN(p[i]);
8374 int ack = BB_ACK(p[i]);
8375 i++;
8376
8377 if (unack && ack)
8378 continue;
8379
8380 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8381 (unsigned long long)s << bb->shift,
8382 length << bb->shift);
8383 }
NeilBrownde393cd2011-07-28 11:31:48 +10008384 if (unack && len == 0)
8385 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008386
8387 if (read_seqretry(&bb->lock, seq))
8388 goto retry;
8389
8390 return len;
8391}
8392
8393#define DO_DEBUG 1
8394
8395static ssize_t
8396badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8397{
8398 unsigned long long sector;
8399 int length;
8400 char newline;
8401#ifdef DO_DEBUG
8402 /* Allow clearing via sysfs *only* for testing/debugging.
8403 * Normally only a successful write may clear a badblock
8404 */
8405 int clear = 0;
8406 if (page[0] == '-') {
8407 clear = 1;
8408 page++;
8409 }
8410#endif /* DO_DEBUG */
8411
8412 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8413 case 3:
8414 if (newline != '\n')
8415 return -EINVAL;
8416 case 2:
8417 if (length <= 0)
8418 return -EINVAL;
8419 break;
8420 default:
8421 return -EINVAL;
8422 }
8423
8424#ifdef DO_DEBUG
8425 if (clear) {
8426 md_clear_badblocks(bb, sector, length);
8427 return len;
8428 }
8429#endif /* DO_DEBUG */
8430 if (md_set_badblocks(bb, sector, length, !unack))
8431 return len;
8432 else
8433 return -ENOSPC;
8434}
8435
Adrian Bunk75c96f82005-05-05 16:16:09 -07008436static int md_notify_reboot(struct notifier_block *this,
8437 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008438{
8439 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008440 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008441 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442
NeilBrownc744a652012-03-19 12:46:37 +11008443 for_each_mddev(mddev, tmp) {
8444 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008445 if (mddev->pers)
8446 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008447 if (mddev->persistent)
8448 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008449 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008450 }
NeilBrownc744a652012-03-19 12:46:37 +11008451 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008452 }
NeilBrownc744a652012-03-19 12:46:37 +11008453 /*
8454 * certain more exotic SCSI devices are known to be
8455 * volatile wrt too early system reboots. While the
8456 * right place to handle this issue is the given
8457 * driver, we do want to have a safe RAID driver ...
8458 */
8459 if (need_delay)
8460 mdelay(1000*1);
8461
Linus Torvalds1da177e2005-04-16 15:20:36 -07008462 return NOTIFY_DONE;
8463}
8464
Adrian Bunk75c96f82005-05-05 16:16:09 -07008465static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008466 .notifier_call = md_notify_reboot,
8467 .next = NULL,
8468 .priority = INT_MAX, /* before any real devices */
8469};
8470
8471static void md_geninit(void)
8472{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008473 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008474
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008475 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008476}
8477
Adrian Bunk75c96f82005-05-05 16:16:09 -07008478static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008479{
Tejun Heoe804ac72010-10-15 15:36:08 +02008480 int ret = -ENOMEM;
8481
Tejun Heoada609e2011-01-25 14:35:54 +01008482 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008483 if (!md_wq)
8484 goto err_wq;
8485
8486 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8487 if (!md_misc_wq)
8488 goto err_misc_wq;
8489
8490 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8491 goto err_md;
8492
8493 if ((ret = register_blkdev(0, "mdp")) < 0)
8494 goto err_mdp;
8495 mdp_major = ret;
8496
NeilBrownaf5628f2014-07-31 13:54:54 +10008497 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008498 md_probe, NULL, NULL);
8499 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008500 md_probe, NULL, NULL);
8501
Linus Torvalds1da177e2005-04-16 15:20:36 -07008502 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008503 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504
8505 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507
Tejun Heoe804ac72010-10-15 15:36:08 +02008508err_mdp:
8509 unregister_blkdev(MD_MAJOR, "md");
8510err_md:
8511 destroy_workqueue(md_misc_wq);
8512err_misc_wq:
8513 destroy_workqueue(md_wq);
8514err_wq:
8515 return ret;
8516}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008517
8518#ifndef MODULE
8519
8520/*
8521 * Searches all registered partitions for autorun RAID arrays
8522 * at boot time.
8523 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008524
8525static LIST_HEAD(all_detected_devices);
8526struct detected_devices_node {
8527 struct list_head list;
8528 dev_t dev;
8529};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530
8531void md_autodetect_dev(dev_t dev)
8532{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008533 struct detected_devices_node *node_detected_dev;
8534
8535 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8536 if (node_detected_dev) {
8537 node_detected_dev->dev = dev;
8538 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8539 } else {
8540 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8541 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543}
8544
Linus Torvalds1da177e2005-04-16 15:20:36 -07008545static void autostart_arrays(int part)
8546{
NeilBrown3cb03002011-10-11 16:45:26 +11008547 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008548 struct detected_devices_node *node_detected_dev;
8549 dev_t dev;
8550 int i_scanned, i_passed;
8551
8552 i_scanned = 0;
8553 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008554
8555 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8556
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008557 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8558 i_scanned++;
8559 node_detected_dev = list_entry(all_detected_devices.next,
8560 struct detected_devices_node, list);
8561 list_del(&node_detected_dev->list);
8562 dev = node_detected_dev->dev;
8563 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008564 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565 if (IS_ERR(rdev))
8566 continue;
8567
NeilBrown403df472014-09-30 15:52:29 +10008568 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008569 continue;
NeilBrown403df472014-09-30 15:52:29 +10008570
NeilBrownd0fae182008-03-04 14:29:31 -08008571 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008572 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008573 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008574 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008575
8576 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8577 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008578
8579 autorun_devices(part);
8580}
8581
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008582#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008583
8584static __exit void md_exit(void)
8585{
NeilBrownfd01b882011-10-11 16:47:53 +11008586 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008587 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008588 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008589
NeilBrownaf5628f2014-07-31 13:54:54 +10008590 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008591 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008592
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008593 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008594 unregister_blkdev(mdp_major, "mdp");
8595 unregister_reboot_notifier(&md_notifier);
8596 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008597
8598 /* We cannot unload the modules while some process is
8599 * waiting for us in select() or poll() - wake them up
8600 */
8601 md_unloading = 1;
8602 while (waitqueue_active(&md_event_waiters)) {
8603 /* not safe to leave yet */
8604 wake_up(&md_event_waiters);
8605 msleep(delay);
8606 delay += delay;
8607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008609
NeilBrown29ac4aa2008-02-06 01:39:58 -08008610 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008611 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008612 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008614 destroy_workqueue(md_misc_wq);
8615 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008616}
8617
Dan Williams685784a2007-07-09 11:56:42 -07008618subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008619module_exit(md_exit)
8620
NeilBrownf91de922005-11-08 21:39:36 -08008621static int get_ro(char *buffer, struct kernel_param *kp)
8622{
8623 return sprintf(buffer, "%d", start_readonly);
8624}
8625static int set_ro(const char *val, struct kernel_param *kp)
8626{
8627 char *e;
8628 int num = simple_strtoul(val, &e, 10);
8629 if (*val && (*e == '\0' || *e == '\n')) {
8630 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008631 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008632 }
8633 return -EINVAL;
8634}
8635
NeilBrown80ca3a42006-07-10 04:44:18 -07008636module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8637module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11008638module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008639
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008641MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008642MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008643MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);