blob: 4db4e4146a35837f54dbd6f6c63e51e85f1ec85d [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
NeilBrownf72ffdd2014-09-30 14:23:59 +1000593static inline int __must_check mddev_lock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800595 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
NeilBrown29f097c2013-11-14 17:54:51 +1100598/* Sometimes we need to take the lock in a situation where
599 * failure due to interrupts is not acceptable.
600 */
NeilBrownf72ffdd2014-09-30 14:23:59 +1000601static inline void mddev_lock_nointr(struct mddev *mddev)
NeilBrown29f097c2013-11-14 17:54:51 +1100602{
603 mutex_lock(&mddev->reconfig_mutex);
604}
605
NeilBrownfd01b882011-10-11 16:47:53 +1100606static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100607{
608 return mutex_is_locked(&mddev->reconfig_mutex);
609}
610
NeilBrownf72ffdd2014-09-30 14:23:59 +1000611static inline int mddev_trylock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800613 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
NeilBrownb6eb1272010-04-15 10:13:47 +1000616static struct attribute_group md_redundancy_group;
617
NeilBrownf72ffdd2014-09-30 14:23:59 +1000618static void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
NeilBrowna64c8762010-04-14 17:15:37 +1000620 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000621 /* These cannot be removed under reconfig_mutex as
622 * an access to the files will try to take reconfig_mutex
623 * while holding the file unremovable, which leads to
624 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000625 * So hold set sysfs_active while the remove in happeing,
626 * and anything else which might set ->to_remove or my
627 * otherwise change the sysfs namespace will fail with
628 * -EBUSY if sysfs_active is still set.
629 * We set sysfs_active under reconfig_mutex and elsewhere
630 * test it under the same mutex to ensure its correct value
631 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000632 */
NeilBrowna64c8762010-04-14 17:15:37 +1000633 struct attribute_group *to_remove = mddev->to_remove;
634 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000635 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000636 mutex_unlock(&mddev->reconfig_mutex);
637
NeilBrown00bcb4a2010-06-01 19:37:23 +1000638 if (mddev->kobj.sd) {
639 if (to_remove != &md_redundancy_group)
640 sysfs_remove_group(&mddev->kobj, to_remove);
641 if (mddev->pers == NULL ||
642 mddev->pers->sync_request == NULL) {
643 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
644 if (mddev->sysfs_action)
645 sysfs_put(mddev->sysfs_action);
646 mddev->sysfs_action = NULL;
647 }
NeilBrowna64c8762010-04-14 17:15:37 +1000648 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000649 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000650 } else
651 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Chris Dunlop751e67c2011-10-19 16:48:26 +1100653 /* As we've dropped the mutex we need a spinlock to
654 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000655 */
656 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700657 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000658 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
NeilBrown1ca69c42012-10-11 13:37:33 +1100661static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
662{
663 struct md_rdev *rdev;
664
665 rdev_for_each_rcu(rdev, mddev)
666 if (rdev->desc_nr == nr)
667 return rdev;
668
669 return NULL;
670}
671
672static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
NeilBrown3cb03002011-10-11 16:45:26 +1100674 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
NeilBrowndafb20f2012-03-19 12:46:39 +1100676 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (rdev->bdev->bd_dev == dev)
678 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return NULL;
681}
682
NeilBrown1ca69c42012-10-11 13:37:33 +1100683static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
684{
685 struct md_rdev *rdev;
686
687 rdev_for_each_rcu(rdev, mddev)
688 if (rdev->bdev->bd_dev == dev)
689 return rdev;
690
691 return NULL;
692}
693
NeilBrown84fc4b52011-10-11 16:49:58 +1100694static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800695{
NeilBrown84fc4b52011-10-11 16:49:58 +1100696 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800697 list_for_each_entry(pers, &pers_list, list) {
698 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800699 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800700 if (strcmp(pers->name, clevel)==0)
701 return pers;
702 }
NeilBrown2604b702006-01-06 00:20:36 -0800703 return NULL;
704}
705
Andre Nollb73df2d2008-07-11 22:02:23 +1000706/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100707static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100709 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000710 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
NeilBrownf72ffdd2014-09-30 14:23:59 +1000713static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 rdev->sb_page = alloc_page(GFP_KERNEL);
716 if (!rdev->sb_page) {
717 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000718 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
721 return 0;
722}
723
NeilBrown545c8792012-05-22 13:54:30 +1000724void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800727 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 rdev->sb_loaded = 0;
729 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000730 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100731 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
NeilBrown2699b672011-07-28 11:31:47 +1000733 if (rdev->bb_page) {
734 put_page(rdev->bb_page);
735 rdev->bb_page = NULL;
736 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000737 kfree(rdev->badblocks.page);
738 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
NeilBrown545c8792012-05-22 13:54:30 +1000740EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
NeilBrown6712ecf2007-09-27 12:47:43 +0200742static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700743{
NeilBrown3cb03002011-10-11 16:45:26 +1100744 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100745 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700746
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700747 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
748 printk("md: super_written gets error=%d, uptodate=%d\n",
749 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
750 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800751 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700752 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700753
NeilBrowna9701a32005-11-08 21:39:34 -0800754 if (atomic_dec_and_test(&mddev->pending_writes))
755 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700756 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700757}
758
NeilBrownfd01b882011-10-11 16:47:53 +1100759void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700760 sector_t sector, int size, struct page *page)
761{
762 /* write first size bytes of page to sector of rdev
763 * Increment mddev->pending_writes before returning
764 * and decrement it on completion, waking up sb_wait
765 * if zero is reached.
766 * If an error occurred, call md_error
767 */
NeilBrowna167f662010-10-26 18:31:13 +1100768 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700769
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100770 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700771 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700772 bio_add_page(bio, page, size, 0);
773 bio->bi_private = rdev;
774 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800775
NeilBrown7bfa19f2005-06-21 17:17:28 -0700776 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000777 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800778}
779
NeilBrownfd01b882011-10-11 16:47:53 +1100780void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800781{
Tejun Heoe9c74692010-09-03 11:56:18 +0200782 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000783 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700784}
785
NeilBrown3cb03002011-10-11 16:45:26 +1100786int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100787 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
NeilBrowna167f662010-10-26 18:31:13 +1100789 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 int ret;
791
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100792 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
793 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100794 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700795 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000796 else if (rdev->mddev->reshape_position != MaxSector &&
797 (rdev->mddev->reshape_backwards ==
798 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700799 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100800 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700801 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700803 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
806 bio_put(bio);
807 return ret;
808}
NeilBrowna8745db2006-01-06 00:20:34 -0800809EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
NeilBrownf72ffdd2014-09-30 14:23:59 +1000811static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (rdev->sb_loaded)
816 return 0;
817
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100818 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 goto fail;
820 rdev->sb_loaded = 1;
821 return 0;
822
823fail:
824 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
825 bdevname(rdev->bdev,b));
826 return -EINVAL;
827}
828
829static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
830{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000831 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000832 sb1->set_uuid1 == sb2->set_uuid1 &&
833 sb1->set_uuid2 == sb2->set_uuid2 &&
834 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
838{
839 int ret;
840 mdp_super_t *tmp1, *tmp2;
841
842 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
843 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
844
845 if (!tmp1 || !tmp2) {
846 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100847 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 goto abort;
849 }
850
851 *tmp1 = *sb1;
852 *tmp2 = *sb2;
853
854 /*
855 * nr_disks is not constant
856 */
857 tmp1->nr_disks = 0;
858 tmp2->nr_disks = 0;
859
Andre Nollce0c8e02008-07-11 22:02:20 +1000860 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700862 kfree(tmp1);
863 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return ret;
865}
866
NeilBrown4d167f02007-05-09 02:35:37 -0700867static u32 md_csum_fold(u32 csum)
868{
869 csum = (csum & 0xffff) + (csum >> 16);
870 return (csum & 0xffff) + (csum >> 16);
871}
872
NeilBrownf72ffdd2014-09-30 14:23:59 +1000873static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
NeilBrown4d167f02007-05-09 02:35:37 -0700875 u64 newcsum = 0;
876 u32 *sb32 = (u32*)sb;
877 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 unsigned int disk_csum, csum;
879
880 disk_csum = sb->sb_csum;
881 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700882
883 for (i = 0; i < MD_SB_BYTES/4 ; i++)
884 newcsum += sb32[i];
885 csum = (newcsum & 0xffffffff) + (newcsum>>32);
886
NeilBrown4d167f02007-05-09 02:35:37 -0700887#ifdef CONFIG_ALPHA
888 /* This used to use csum_partial, which was wrong for several
889 * reasons including that different results are returned on
890 * different architectures. It isn't critical that we get exactly
891 * the same return value as before (we always csum_fold before
892 * testing, and that removes any differences). However as we
893 * know that csum_partial always returned a 16bit value on
894 * alphas, do a fold to maximise conformity to previous behaviour.
895 */
896 sb->sb_csum = md_csum_fold(disk_csum);
897#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700899#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return csum;
901}
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903/*
904 * Handle superblock details.
905 * We want to be able to handle multiple superblock formats
906 * so we have a common interface to them all, and an array of
907 * different handlers.
908 * We rely on user-space to write the initial superblock, and support
909 * reading and updating of superblocks.
910 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100911 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 * loads and validates a superblock on dev.
913 * if refdev != NULL, compare superblocks on both devices
914 * Return:
915 * 0 - dev has a superblock that is compatible with refdev
916 * 1 - dev has a superblock that is compatible and newer than refdev
917 * so dev should be used as the refdev in future
918 * -EINVAL superblock incompatible or invalid
919 * -othererror e.g. -EIO
920 *
NeilBrownfd01b882011-10-11 16:47:53 +1100921 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * Verify that dev is acceptable into mddev.
923 * The first time, mddev->raid_disks will be 0, and data from
924 * dev should be merged in. Subsequent calls check that dev
925 * is new enough. Return 0 or -EINVAL
926 *
NeilBrownfd01b882011-10-11 16:47:53 +1100927 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 * Update the superblock for rdev with data in mddev
929 * This does not write to disc.
930 *
931 */
932
933struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000934 char *name;
935 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000936 int (*load_super)(struct md_rdev *rdev,
937 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000938 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000939 int (*validate_super)(struct mddev *mddev,
940 struct md_rdev *rdev);
941 void (*sync_super)(struct mddev *mddev,
942 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100943 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000944 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000945 int (*allow_new_offset)(struct md_rdev *rdev,
946 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947};
948
949/*
Andre Noll0894cc32009-06-18 08:49:23 +1000950 * Check that the given mddev has no bitmap.
951 *
952 * This function is called from the run method of all personalities that do not
953 * support bitmaps. It prints an error message and returns non-zero if mddev
954 * has a bitmap. Otherwise, it returns 0.
955 *
956 */
NeilBrownfd01b882011-10-11 16:47:53 +1100957int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000958{
NeilBrownc3d97142009-12-14 12:49:52 +1100959 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000960 return 0;
961 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
962 mdname(mddev), mddev->pers->name);
963 return 1;
964}
965EXPORT_SYMBOL(md_check_no_bitmap);
966
967/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000968 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 */
NeilBrown3cb03002011-10-11 16:45:26 +1100970static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
973 mdp_super_t *sb;
974 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 /*
Andre Noll0f420352008-07-11 22:02:23 +1000977 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 * it's at the end of the disk.
979 *
980 * It also happens to be a multiple of 4Kb.
981 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100982 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
NeilBrown0002b272005-09-09 16:23:53 -0700984 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (ret) return ret;
986
987 ret = -EINVAL;
988
989 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000990 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (sb->md_magic != MD_SB_MAGIC) {
993 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
994 b);
995 goto abort;
996 }
997
998 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800999 sb->minor_version < 90 ||
1000 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1002 sb->major_version, sb->minor_version,
1003 b);
1004 goto abort;
1005 }
1006
1007 if (sb->raid_disks <= 0)
1008 goto abort;
1009
NeilBrown4d167f02007-05-09 02:35:37 -07001010 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1012 b);
1013 goto abort;
1014 }
1015
1016 rdev->preferred_minor = sb->md_minor;
1017 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001018 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001019 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001020 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 if (sb->level == LEVEL_MULTIPATH)
1023 rdev->desc_nr = -1;
1024 else
1025 rdev->desc_nr = sb->this_disk.number;
1026
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001027 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001029 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001031 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (!uuid_equal(refsb, sb)) {
1033 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1034 b, bdevname(refdev->bdev,b2));
1035 goto abort;
1036 }
1037 if (!sb_equal(refsb, sb)) {
1038 printk(KERN_WARNING "md: %s has same UUID"
1039 " but different superblock to %s\n",
1040 b, bdevname(refdev->bdev, b2));
1041 goto abort;
1042 }
1043 ev1 = md_event(sb);
1044 ev2 = md_event(refsb);
1045 if (ev1 > ev2)
1046 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001047 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 ret = 0;
1049 }
NeilBrown8190e752009-06-18 08:48:58 +10001050 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001051 /* Limit to 4TB as metadata cannot record more than that.
1052 * (not needed for Linear and RAID0 as metadata doesn't
1053 * record this size)
1054 */
1055 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001056 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
NeilBrown27a7b262011-09-10 17:21:28 +10001058 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001059 /* "this cannot possibly happen" ... */
1060 ret = -EINVAL;
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 abort:
1063 return ret;
1064}
1065
1066/*
1067 * validate_super for 0.90.0
1068 */
NeilBrownfd01b882011-10-11 16:47:53 +11001069static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001072 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001073 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
NeilBrown41158c72005-06-21 17:17:25 -07001075 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001076 clear_bit(Faulty, &rdev->flags);
1077 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001078 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001079 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (mddev->raid_disks == 0) {
1082 mddev->major_version = 0;
1083 mddev->minor_version = sb->minor_version;
1084 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001085 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001086 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 mddev->ctime = sb->ctime;
1088 mddev->utime = sb->utime;
1089 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001090 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 mddev->layout = sb->layout;
1092 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001093 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001094 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001095 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001096 mddev->bitmap_info.space = 0;
1097 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001098 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001099 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001100 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
NeilBrownf6705572006-03-27 01:18:11 -08001102 if (mddev->minor_version >= 91) {
1103 mddev->reshape_position = sb->reshape_position;
1104 mddev->delta_disks = sb->delta_disks;
1105 mddev->new_level = sb->new_level;
1106 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001107 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001108 if (mddev->delta_disks < 0)
1109 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001110 } else {
1111 mddev->reshape_position = MaxSector;
1112 mddev->delta_disks = 0;
1113 mddev->new_level = mddev->level;
1114 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001115 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001116 }
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (sb->state & (1<<MD_SB_CLEAN))
1119 mddev->recovery_cp = MaxSector;
1120 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001121 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 sb->events_lo == sb->cp_events_lo) {
1123 mddev->recovery_cp = sb->recovery_cp;
1124 } else
1125 mddev->recovery_cp = 0;
1126 }
1127
1128 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1129 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1130 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1131 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1132
1133 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001134
1135 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001136 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001137 mddev->bitmap_info.offset =
1138 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001139 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001140 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001141 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001142
NeilBrown41158c72005-06-21 17:17:25 -07001143 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001144 /* Insist on good event counter while assembling, except
1145 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001147 if (sb->disks[rdev->desc_nr].state & (
1148 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001149 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001150 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001151 } else if (mddev->bitmap) {
1152 /* if adding to array with a bitmap, then we can accept an
1153 * older device ... but not too old.
1154 */
NeilBrown41158c72005-06-21 17:17:25 -07001155 if (ev1 < mddev->bitmap->events_cleared)
1156 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001157 if (ev1 < mddev->events)
1158 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001159 } else {
1160 if (ev1 < mddev->events)
1161 /* just a hot-add of a new device, leave raid_disk at -1 */
1162 return 0;
1163 }
NeilBrown41158c72005-06-21 17:17:25 -07001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 desc = sb->disks + rdev->desc_nr;
1167
1168 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001169 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001170 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1171 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001172 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001174 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001175 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1176 /* active but not in sync implies recovery up to
1177 * reshape position. We don't know exactly where
1178 * that is, so set to zero for now */
1179 if (mddev->minor_version >= 91) {
1180 rdev->recovery_offset = 0;
1181 rdev->raid_disk = desc->raid_disk;
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001184 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1185 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001186 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001187 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return 0;
1189}
1190
1191/*
1192 * sync_super for 0.90.0
1193 */
NeilBrownfd01b882011-10-11 16:47:53 +11001194static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001197 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /* make rdev->sb match mddev data..
1201 *
1202 * 1/ zero out disks
1203 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1204 * 3/ any empty disks < next_spare become removed
1205 *
1206 * disks[0] gets initialised to REMOVED because
1207 * we cannot be sure from other fields if it has
1208 * been initialised or not.
1209 */
1210 int i;
1211 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1212
NeilBrown61181562005-09-09 16:24:02 -07001213 rdev->sb_size = MD_SB_BYTES;
1214
Namhyung Kim65a06f062011-07-27 11:00:36 +10001215 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 memset(sb, 0, sizeof(*sb));
1218
1219 sb->md_magic = MD_SB_MAGIC;
1220 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 sb->patch_version = mddev->patch_version;
1222 sb->gvalid_words = 0; /* ignored */
1223 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1224 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1225 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1226 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1227
1228 sb->ctime = mddev->ctime;
1229 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001230 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 sb->raid_disks = mddev->raid_disks;
1232 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001233 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 sb->utime = mddev->utime;
1235 sb->state = 0;
1236 sb->events_hi = (mddev->events>>32);
1237 sb->events_lo = (u32)mddev->events;
1238
NeilBrownf6705572006-03-27 01:18:11 -08001239 if (mddev->reshape_position == MaxSector)
1240 sb->minor_version = 90;
1241 else {
1242 sb->minor_version = 91;
1243 sb->reshape_position = mddev->reshape_position;
1244 sb->new_level = mddev->new_level;
1245 sb->delta_disks = mddev->delta_disks;
1246 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001247 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001248 }
1249 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (mddev->in_sync)
1251 {
1252 sb->recovery_cp = mddev->recovery_cp;
1253 sb->cp_events_hi = (mddev->events>>32);
1254 sb->cp_events_lo = (u32)mddev->events;
1255 if (mddev->recovery_cp == MaxSector)
1256 sb->state = (1<< MD_SB_CLEAN);
1257 } else
1258 sb->recovery_cp = 0;
1259
1260 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001261 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
NeilBrownc3d97142009-12-14 12:49:52 +11001263 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001264 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001267 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001269 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001270 int is_active = test_bit(In_sync, &rdev2->flags);
1271
1272 if (rdev2->raid_disk >= 0 &&
1273 sb->minor_version >= 91)
1274 /* we have nowhere to store the recovery_offset,
1275 * but if it is not below the reshape_position,
1276 * we can piggy-back on that.
1277 */
1278 is_active = 1;
1279 if (rdev2->raid_disk < 0 ||
1280 test_bit(Faulty, &rdev2->flags))
1281 is_active = 0;
1282 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001283 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001285 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001286 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 d = &sb->disks[rdev2->desc_nr];
1288 nr_disks++;
1289 d->number = rdev2->desc_nr;
1290 d->major = MAJOR(rdev2->bdev->bd_dev);
1291 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001292 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 d->raid_disk = rdev2->raid_disk;
1294 else
1295 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001296 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001298 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001300 if (test_bit(In_sync, &rdev2->flags))
1301 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 active++;
1303 working++;
1304 } else {
1305 d->state = 0;
1306 spare++;
1307 working++;
1308 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001309 if (test_bit(WriteMostly, &rdev2->flags))
1310 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 /* now set the "removed" and "faulty" bits on any missing devices */
1313 for (i=0 ; i < mddev->raid_disks ; i++) {
1314 mdp_disk_t *d = &sb->disks[i];
1315 if (d->state == 0 && d->number == 0) {
1316 d->number = i;
1317 d->raid_disk = i;
1318 d->state = (1<<MD_DISK_REMOVED);
1319 d->state |= (1<<MD_DISK_FAULTY);
1320 failed++;
1321 }
1322 }
1323 sb->nr_disks = nr_disks;
1324 sb->active_disks = active;
1325 sb->working_disks = working;
1326 sb->failed_disks = failed;
1327 sb->spare_disks = spare;
1328
1329 sb->this_disk = sb->disks[rdev->desc_nr];
1330 sb->sb_csum = calc_sb_csum(sb);
1331}
1332
1333/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001334 * rdev_size_change for 0.90.0
1335 */
1336static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001337super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001338{
Andre Noll58c0fed2009-03-31 14:33:13 +11001339 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001340 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001341 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001342 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001343 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001344 if (!num_sectors || num_sectors > rdev->sb_start)
1345 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001346 /* Limit to 4TB as metadata cannot record more than that.
1347 * 4TB == 2^32 KB, or 2*2^32 sectors.
1348 */
NeilBrown667a5312012-08-16 16:46:12 +10001349 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001350 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001351 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001352 rdev->sb_page);
1353 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001354 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001355}
1356
NeilBrownc6563a82012-05-21 09:27:00 +10001357static int
1358super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1359{
1360 /* non-zero offset changes not possible with v0.90 */
1361 return new_offset == 0;
1362}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001363
1364/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 * version 1 superblock
1366 */
1367
NeilBrownf72ffdd2014-09-30 14:23:59 +10001368static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001370 __le32 disk_csum;
1371 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 unsigned long long newcsum;
1373 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001374 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 disk_csum = sb->sb_csum;
1377 sb->sb_csum = 0;
1378 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001379 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 newcsum += le32_to_cpu(*isuper++);
1381
1382 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001383 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1386 sb->sb_csum = disk_csum;
1387 return cpu_to_le32(csum);
1388}
1389
NeilBrown2699b672011-07-28 11:31:47 +10001390static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1391 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001392static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
1394 struct mdp_superblock_1 *sb;
1395 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001396 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001397 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001399 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 /*
Andre Noll0f420352008-07-11 22:02:23 +10001402 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 * It is always aligned to a 4K boundary and
1404 * depeding on minor_version, it can be:
1405 * 0: At least 8K, but less than 12K, from end of device
1406 * 1: At start of device
1407 * 2: 4K from start of device.
1408 */
1409 switch(minor_version) {
1410 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001411 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001412 sb_start -= 8*2;
1413 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 break;
1415 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001416 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 break;
1418 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001419 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 break;
1421 default:
1422 return -EINVAL;
1423 }
Andre Noll0f420352008-07-11 22:02:23 +10001424 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
NeilBrown0002b272005-09-09 16:23:53 -07001426 /* superblock is rarely larger than 1K, but it can be larger,
1427 * and it is safe to read 4k, so we do that
1428 */
1429 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (ret) return ret;
1431
Namhyung Kim65a06f062011-07-27 11:00:36 +10001432 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1435 sb->major_version != cpu_to_le32(1) ||
1436 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001437 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001438 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return -EINVAL;
1440
1441 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1442 printk("md: invalid superblock checksum on %s\n",
1443 bdevname(rdev->bdev,b));
1444 return -EINVAL;
1445 }
1446 if (le64_to_cpu(sb->data_size) < 10) {
1447 printk("md: data_size too small on %s\n",
1448 bdevname(rdev->bdev,b));
1449 return -EINVAL;
1450 }
NeilBrownc6563a82012-05-21 09:27:00 +10001451 if (sb->pad0 ||
1452 sb->pad3[0] ||
1453 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1454 /* Some padding is non-zero, might be a new feature */
1455 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 rdev->preferred_minor = 0xffff;
1458 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001459 rdev->new_data_offset = rdev->data_offset;
1460 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1461 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1462 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001463 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
NeilBrown0002b272005-09-09 16:23:53 -07001465 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001466 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001467 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001468 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1469
1470 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001471 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001472 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001473 if (minor_version
1474 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1475 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001476
NeilBrown31b65a02006-07-10 04:44:14 -07001477 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1478 rdev->desc_nr = -1;
1479 else
1480 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1481
NeilBrown2699b672011-07-28 11:31:47 +10001482 if (!rdev->bb_page) {
1483 rdev->bb_page = alloc_page(GFP_KERNEL);
1484 if (!rdev->bb_page)
1485 return -ENOMEM;
1486 }
1487 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1488 rdev->badblocks.count == 0) {
1489 /* need to load the bad block list.
1490 * Currently we limit it to one page.
1491 */
1492 s32 offset;
1493 sector_t bb_sector;
1494 u64 *bbp;
1495 int i;
1496 int sectors = le16_to_cpu(sb->bblog_size);
1497 if (sectors > (PAGE_SIZE / 512))
1498 return -EINVAL;
1499 offset = le32_to_cpu(sb->bblog_offset);
1500 if (offset == 0)
1501 return -EINVAL;
1502 bb_sector = (long long)offset;
1503 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1504 rdev->bb_page, READ, true))
1505 return -EIO;
1506 bbp = (u64 *)page_address(rdev->bb_page);
1507 rdev->badblocks.shift = sb->bblog_shift;
1508 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1509 u64 bb = le64_to_cpu(*bbp);
1510 int count = bb & (0x3ff);
1511 u64 sector = bb >> 10;
1512 sector <<= sb->bblog_shift;
1513 count <<= sb->bblog_shift;
1514 if (bb + 1 == 0)
1515 break;
1516 if (md_set_badblocks(&rdev->badblocks,
1517 sector, count, 1) == 0)
1518 return -EINVAL;
1519 }
NeilBrown486adf72013-04-24 11:42:44 +10001520 } else if (sb->bblog_offset != 0)
1521 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001522
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001523 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001524 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001525 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001527 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1530 sb->level != refsb->level ||
1531 sb->layout != refsb->layout ||
1532 sb->chunksize != refsb->chunksize) {
1533 printk(KERN_WARNING "md: %s has strangely different"
1534 " superblock to %s\n",
1535 bdevname(rdev->bdev,b),
1536 bdevname(refdev->bdev,b2));
1537 return -EINVAL;
1538 }
1539 ev1 = le64_to_cpu(sb->events);
1540 ev2 = le64_to_cpu(refsb->events);
1541
1542 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001543 ret = 1;
1544 else
1545 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
NeilBrownc6563a82012-05-21 09:27:00 +10001547 if (minor_version) {
1548 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1549 sectors -= rdev->data_offset;
1550 } else
1551 sectors = rdev->sb_start;
1552 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001554 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001555 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
NeilBrownfd01b882011-10-11 16:47:53 +11001558static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001560 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001561 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
NeilBrown41158c72005-06-21 17:17:25 -07001563 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001564 clear_bit(Faulty, &rdev->flags);
1565 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001566 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001567 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (mddev->raid_disks == 0) {
1570 mddev->major_version = 1;
1571 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001572 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001573 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1575 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1576 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001577 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 mddev->layout = le32_to_cpu(sb->layout);
1579 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001580 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001581 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001582 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001583 mddev->bitmap_info.space = 0;
1584 /* Default location for bitmap is 1K after superblock
1585 * using 3K - total of 4K
1586 */
NeilBrownc3d97142009-12-14 12:49:52 +11001587 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001588 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001589 mddev->reshape_backwards = 0;
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1592 memcpy(mddev->uuid, sb->set_uuid, 16);
1593
1594 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001595
NeilBrown71c08052005-09-09 16:23:51 -07001596 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001597 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001598 mddev->bitmap_info.offset =
1599 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001600 /* Metadata doesn't record how much space is available.
1601 * For 1.0, we assume we can use up to the superblock
1602 * if before, else to 4K beyond superblock.
1603 * For others, assume no change is possible.
1604 */
1605 if (mddev->minor_version > 0)
1606 mddev->bitmap_info.space = 0;
1607 else if (mddev->bitmap_info.offset > 0)
1608 mddev->bitmap_info.space =
1609 8 - mddev->bitmap_info.offset;
1610 else
1611 mddev->bitmap_info.space =
1612 -mddev->bitmap_info.offset;
1613 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001614
NeilBrownf6705572006-03-27 01:18:11 -08001615 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1616 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1617 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1618 mddev->new_level = le32_to_cpu(sb->new_level);
1619 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001620 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001621 if (mddev->delta_disks < 0 ||
1622 (mddev->delta_disks == 0 &&
1623 (le32_to_cpu(sb->feature_map)
1624 & MD_FEATURE_RESHAPE_BACKWARDS)))
1625 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001626 } else {
1627 mddev->reshape_position = MaxSector;
1628 mddev->delta_disks = 0;
1629 mddev->new_level = mddev->level;
1630 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001631 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001632 }
1633
NeilBrown41158c72005-06-21 17:17:25 -07001634 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001635 /* Insist of good event counter while assembling, except for
1636 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001638 if (rdev->desc_nr >= 0 &&
1639 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1640 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1641 if (ev1 < mddev->events)
1642 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001643 } else if (mddev->bitmap) {
1644 /* If adding to array with a bitmap, then we can accept an
1645 * older device, but not too old.
1646 */
NeilBrown41158c72005-06-21 17:17:25 -07001647 if (ev1 < mddev->bitmap->events_cleared)
1648 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001649 if (ev1 < mddev->events)
1650 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001651 } else {
1652 if (ev1 < mddev->events)
1653 /* just a hot-add of a new device, leave raid_disk at -1 */
1654 return 0;
1655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (mddev->level != LEVEL_MULTIPATH) {
1657 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001658 if (rdev->desc_nr < 0 ||
1659 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1660 role = 0xffff;
1661 rdev->desc_nr = -1;
1662 } else
1663 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 switch(role) {
1665 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 break;
1667 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001668 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 break;
1670 default:
NeilBrownf4667222013-12-09 12:04:56 +11001671 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001672 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001673 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001674 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001675 if (!(le32_to_cpu(sb->feature_map) &
1676 MD_FEATURE_RECOVERY_BITMAP))
1677 rdev->saved_raid_disk = -1;
1678 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001679 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 rdev->raid_disk = role;
1681 break;
1682 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001683 if (sb->devflags & WriteMostly1)
1684 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001685 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1686 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001687 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001688 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return 0;
1691}
1692
NeilBrownfd01b882011-10-11 16:47:53 +11001693static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
1695 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001696 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 int max_dev, i;
1698 /* make rdev->sb match mddev and rdev data. */
1699
Namhyung Kim65a06f062011-07-27 11:00:36 +10001700 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 sb->feature_map = 0;
1703 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001704 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 memset(sb->pad3, 0, sizeof(sb->pad3));
1706
1707 sb->utime = cpu_to_le64((__u64)mddev->utime);
1708 sb->events = cpu_to_le64(mddev->events);
1709 if (mddev->in_sync)
1710 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1711 else
1712 sb->resync_offset = cpu_to_le64(0);
1713
NeilBrown1c05b4b2006-10-21 10:24:08 -07001714 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001715
NeilBrownf0ca3402006-02-02 14:28:04 -08001716 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001717 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001718 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001719 sb->level = cpu_to_le32(mddev->level);
1720 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001721
NeilBrownaeb9b2112011-08-25 14:43:08 +10001722 if (test_bit(WriteMostly, &rdev->flags))
1723 sb->devflags |= WriteMostly1;
1724 else
1725 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001726 sb->data_offset = cpu_to_le64(rdev->data_offset);
1727 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001728
NeilBrownc3d97142009-12-14 12:49:52 +11001729 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1730 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001731 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001732 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001733
1734 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001735 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001736 sb->feature_map |=
1737 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1738 sb->recovery_offset =
1739 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001740 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1741 sb->feature_map |=
1742 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001743 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001744 if (test_bit(Replacement, &rdev->flags))
1745 sb->feature_map |=
1746 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001747
NeilBrownf6705572006-03-27 01:18:11 -08001748 if (mddev->reshape_position != MaxSector) {
1749 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1750 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1751 sb->new_layout = cpu_to_le32(mddev->new_layout);
1752 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1753 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001754 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001755 if (mddev->delta_disks == 0 &&
1756 mddev->reshape_backwards)
1757 sb->feature_map
1758 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001759 if (rdev->new_data_offset != rdev->data_offset) {
1760 sb->feature_map
1761 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1762 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1763 - rdev->data_offset));
1764 }
NeilBrownf6705572006-03-27 01:18:11 -08001765 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001766
NeilBrown2699b672011-07-28 11:31:47 +10001767 if (rdev->badblocks.count == 0)
1768 /* Nothing to do for bad blocks*/ ;
1769 else if (sb->bblog_offset == 0)
1770 /* Cannot record bad blocks on this device */
1771 md_error(mddev, rdev);
1772 else {
1773 struct badblocks *bb = &rdev->badblocks;
1774 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1775 u64 *p = bb->page;
1776 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1777 if (bb->changed) {
1778 unsigned seq;
1779
1780retry:
1781 seq = read_seqbegin(&bb->lock);
1782
1783 memset(bbp, 0xff, PAGE_SIZE);
1784
1785 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001786 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001787 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1788 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001789 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001790 }
NeilBrownd0962932012-03-19 12:46:41 +11001791 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001792 if (read_seqretry(&bb->lock, seq))
1793 goto retry;
1794
1795 bb->sector = (rdev->sb_start +
1796 (int)le32_to_cpu(sb->bblog_offset));
1797 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001798 }
1799 }
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001802 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (rdev2->desc_nr+1 > max_dev)
1804 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001805
NeilBrown70471da2009-08-03 10:59:57 +10001806 if (max_dev > le32_to_cpu(sb->max_dev)) {
1807 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001808 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001809 rdev->sb_size = max_dev * 2 + 256;
1810 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1811 if (rdev->sb_size & bmask)
1812 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001813 } else
1814 max_dev = le32_to_cpu(sb->max_dev);
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 for (i=0; i<max_dev;i++)
1817 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001818
NeilBrowndafb20f2012-03-19 12:46:39 +11001819 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001821 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001823 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001825 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001826 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 else
1828 sb->dev_roles[i] = cpu_to_le16(0xffff);
1829 }
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 sb->sb_csum = calc_sb_1_csum(sb);
1832}
1833
Chris Webb0cd17fe2008-06-28 08:31:46 +10001834static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001835super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001836{
1837 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001838 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001839 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001840 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001841 if (rdev->data_offset != rdev->new_data_offset)
1842 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001843 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001844 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001845 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001846 max_sectors -= rdev->data_offset;
1847 if (!num_sectors || num_sectors > max_sectors)
1848 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001849 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001850 /* minor version 0 with bitmap we can't move */
1851 return 0;
1852 } else {
1853 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001854 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001855 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001856 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001857 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001858 if (!num_sectors || num_sectors > max_sectors)
1859 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001860 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001861 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001862 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001863 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001864 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001865 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001866 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001867 rdev->sb_page);
1868 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001869 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001870
1871}
1872
1873static int
1874super_1_allow_new_offset(struct md_rdev *rdev,
1875 unsigned long long new_offset)
1876{
1877 /* All necessary checks on new >= old have been done */
1878 struct bitmap *bitmap;
1879 if (new_offset >= rdev->data_offset)
1880 return 1;
1881
1882 /* with 1.0 metadata, there is no metadata to tread on
1883 * so we can always move back */
1884 if (rdev->mddev->minor_version == 0)
1885 return 1;
1886
1887 /* otherwise we must be sure not to step on
1888 * any metadata, so stay:
1889 * 36K beyond start of superblock
1890 * beyond end of badblocks
1891 * beyond write-intent bitmap
1892 */
1893 if (rdev->sb_start + (32+4)*2 > new_offset)
1894 return 0;
1895 bitmap = rdev->mddev->bitmap;
1896 if (bitmap && !rdev->mddev->bitmap_info.file &&
1897 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001898 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001899 return 0;
1900 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1901 return 0;
1902
1903 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001904}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Adrian Bunk75c96f82005-05-05 16:16:09 -07001906static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 [0] = {
1908 .name = "0.90.0",
1909 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001910 .load_super = super_90_load,
1911 .validate_super = super_90_validate,
1912 .sync_super = super_90_sync,
1913 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001914 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 },
1916 [1] = {
1917 .name = "md-1",
1918 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001919 .load_super = super_1_load,
1920 .validate_super = super_1_validate,
1921 .sync_super = super_1_sync,
1922 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001923 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 },
1925};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
NeilBrownfd01b882011-10-11 16:47:53 +11001927static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001928{
1929 if (mddev->sync_super) {
1930 mddev->sync_super(mddev, rdev);
1931 return;
1932 }
1933
1934 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1935
1936 super_types[mddev->major_version].sync_super(mddev, rdev);
1937}
1938
NeilBrownfd01b882011-10-11 16:47:53 +11001939static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
NeilBrown3cb03002011-10-11 16:45:26 +11001941 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
NeilBrown4b809912008-07-21 17:05:25 +10001943 rcu_read_lock();
1944 rdev_for_each_rcu(rdev, mddev1)
1945 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001946 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001947 rdev2->bdev->bd_contains) {
1948 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001949 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001950 }
1951 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return 0;
1953}
1954
1955static LIST_HEAD(pending_raid_disks);
1956
Andre Nollac5e7112009-08-03 10:59:47 +10001957/*
1958 * Try to register data integrity profile for an mddev
1959 *
1960 * This is called when an array is started and after a disk has been kicked
1961 * from the array. It only succeeds if all working and active component devices
1962 * are integrity capable with matching profiles.
1963 */
NeilBrownfd01b882011-10-11 16:47:53 +11001964int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001965{
NeilBrown3cb03002011-10-11 16:45:26 +11001966 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001967
Andre Nollac5e7112009-08-03 10:59:47 +10001968 if (list_empty(&mddev->disks))
1969 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001970 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1971 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001972 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001973 /* skip spares and non-functional disks */
1974 if (test_bit(Faulty, &rdev->flags))
1975 continue;
1976 if (rdev->raid_disk < 0)
1977 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001978 if (!reference) {
1979 /* Use the first rdev as the reference */
1980 reference = rdev;
1981 continue;
1982 }
1983 /* does this rdev's profile match the reference profile? */
1984 if (blk_integrity_compare(reference->bdev->bd_disk,
1985 rdev->bdev->bd_disk) < 0)
1986 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001987 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001988 if (!reference || !bdev_get_integrity(reference->bdev))
1989 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001990 /*
1991 * All component devices are integrity capable and have matching
1992 * profiles, register the common profile for the md device.
1993 */
1994 if (blk_integrity_register(mddev->gendisk,
1995 bdev_get_integrity(reference->bdev)) != 0) {
1996 printk(KERN_ERR "md: failed to register integrity for %s\n",
1997 mdname(mddev));
1998 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001999 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01002000 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2001 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2002 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2003 mdname(mddev));
2004 return -EINVAL;
2005 }
Andre Nollac5e7112009-08-03 10:59:47 +10002006 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002007}
Andre Nollac5e7112009-08-03 10:59:47 +10002008EXPORT_SYMBOL(md_integrity_register);
2009
2010/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002011void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002012{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002013 struct blk_integrity *bi_rdev;
2014 struct blk_integrity *bi_mddev;
2015
2016 if (!mddev->gendisk)
2017 return;
2018
2019 bi_rdev = bdev_get_integrity(rdev->bdev);
2020 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002021
2022 if (!bi_mddev) /* nothing to do */
2023 return;
2024 if (rdev->raid_disk < 0) /* skip spares */
2025 return;
2026 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2027 rdev->bdev->bd_disk) >= 0)
2028 return;
2029 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2030 blk_integrity_unregister(mddev->gendisk);
2031}
2032EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002033
NeilBrownf72ffdd2014-09-30 14:23:59 +10002034static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002036 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002037 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002038 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002039 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Dan Williams11e2ede2008-04-30 00:52:32 -07002041 /* prevent duplicates */
2042 if (find_rdev(mddev, rdev->bdev->bd_dev))
2043 return -EEXIST;
2044
Andre Nolldd8ac332009-03-31 14:33:13 +11002045 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2046 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2047 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002048 if (mddev->pers) {
2049 /* Cannot change size, so fail
2050 * If mddev->level <= 0, then we don't care
2051 * about aligning sizes (e.g. linear)
2052 */
2053 if (mddev->level > 0)
2054 return -ENOSPC;
2055 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002056 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 /* Verify rdev->desc_nr is unique.
2060 * If it is -1, assign a free number, else
2061 * check number is not in use
2062 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002063 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (rdev->desc_nr < 0) {
2065 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002066 if (mddev->pers)
2067 choice = mddev->raid_disks;
2068 while (find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 choice++;
2070 rdev->desc_nr = choice;
2071 } else {
NeilBrown4878e9e2014-09-25 17:00:11 +10002072 if (find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2073 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002077 rcu_read_unlock();
NeilBrownde01dfa2009-02-06 18:02:46 +11002078 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2079 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2080 mdname(mddev), mddev->max_disks);
2081 return -EBUSY;
2082 }
NeilBrown19133a42005-11-08 21:39:35 -08002083 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002084 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002085 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002088 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002089
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002090 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002091 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002092
Tejun Heo0762b8b2008-08-25 19:56:12 +09002093 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002094 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2095 /* failure here is OK */;
2096 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002097
NeilBrown4b809912008-07-21 17:05:25 +10002098 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002099 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002100
2101 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002102 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002105
2106 fail:
2107 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2108 b, mdname(mddev));
2109 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
NeilBrown177a99b2008-02-06 01:39:56 -08002112static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002113{
NeilBrown3cb03002011-10-11 16:45:26 +11002114 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002115 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002116 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002117}
2118
NeilBrownf72ffdd2014-09-30 14:23:59 +10002119static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002122
Tejun Heo49731ba2011-01-14 18:43:57 +01002123 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002124 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2126 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002127 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002128 sysfs_put(rdev->sysfs_state);
2129 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002130 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002131 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002132 * writing to 'remove' to "dev/state". We also need
2133 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002134 */
NeilBrown4b809912008-07-21 17:05:25 +10002135 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002136 INIT_WORK(&rdev->del_work, md_delayed_delete);
2137 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002138 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
2141/*
2142 * prevent the device from being mounted, repartitioned or
2143 * otherwise reused by a RAID array (or any other kernel
2144 * subsystem), by bd_claiming the device.
2145 */
NeilBrown3cb03002011-10-11 16:45:26 +11002146static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
2148 int err = 0;
2149 struct block_device *bdev;
2150 char b[BDEVNAME_SIZE];
2151
Tejun Heod4d77622010-11-13 11:55:18 +01002152 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002153 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (IS_ERR(bdev)) {
2155 printk(KERN_ERR "md: could not open %s.\n",
2156 __bdevname(dev, b));
2157 return PTR_ERR(bdev);
2158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 rdev->bdev = bdev;
2160 return err;
2161}
2162
NeilBrown3cb03002011-10-11 16:45:26 +11002163static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
2165 struct block_device *bdev = rdev->bdev;
2166 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002167 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168}
2169
2170void md_autodetect_dev(dev_t dev);
2171
NeilBrownf72ffdd2014-09-30 14:23:59 +10002172static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 printk(KERN_INFO "md: export_rdev(%s)\n",
2177 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002178 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002180 if (test_bit(AutoDetected, &rdev->flags))
2181 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182#endif
2183 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002184 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186
NeilBrownf72ffdd2014-09-30 14:23:59 +10002187static void kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
2189 unbind_rdev_from_array(rdev);
2190 export_rdev(rdev);
2191}
2192
NeilBrownfd01b882011-10-11 16:47:53 +11002193static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
NeilBrown0638bb02014-09-25 17:43:47 +10002195 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
NeilBrown0638bb02014-09-25 17:43:47 +10002197 while (!list_empty(&mddev->disks)) {
2198 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2199 same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 kick_rdev_from_array(rdev);
2201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 mddev->raid_disks = 0;
2203 mddev->major_version = 0;
2204}
2205
NeilBrownf72ffdd2014-09-30 14:23:59 +10002206static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
NeilBrown42543762006-06-26 00:27:57 -07002208 /* Update each superblock (in-memory image), but
2209 * if we are allowed to, skip spares which already
2210 * have the right event counter, or have one earlier
2211 * (which would mean they aren't being marked as dirty
2212 * with the rest of the array)
2213 */
NeilBrown3cb03002011-10-11 16:45:26 +11002214 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002215 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002216 if (rdev->sb_events == mddev->events ||
2217 (nospares &&
2218 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002219 rdev->sb_events+1 == mddev->events)) {
2220 /* Don't update this superblock */
2221 rdev->sb_loaded = 2;
2222 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002223 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002224 rdev->sb_loaded = 1;
2225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
2227}
2228
NeilBrownf72ffdd2014-09-30 14:23:59 +10002229static void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
NeilBrown3cb03002011-10-11 16:45:26 +11002231 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002232 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002233 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002234 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
NeilBrownd87f0642013-04-24 11:42:40 +10002236 if (mddev->ro) {
2237 if (force_change)
2238 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2239 return;
2240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002242 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002243 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002244 if (rdev->raid_disk >= 0 &&
2245 mddev->delta_disks >= 0 &&
2246 !test_bit(In_sync, &rdev->flags) &&
2247 mddev->curr_resync_completed > rdev->recovery_offset)
2248 rdev->recovery_offset = mddev->curr_resync_completed;
2249
NeilBrownf72ffdd2014-09-30 14:23:59 +10002250 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002251 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002252 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002253 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002254 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002255 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002256 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002257 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002258 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002259 md_ack_all_badblocks(&rdev->badblocks);
2260 md_error(mddev, rdev);
2261 }
2262 clear_bit(Blocked, &rdev->flags);
2263 clear_bit(BlockedBadBlocks, &rdev->flags);
2264 wake_up(&rdev->blocked_wait);
2265 }
2266 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002267 wake_up(&mddev->sb_wait);
2268 return;
2269 }
2270
NeilBrown85572d72014-12-15 12:56:56 +11002271 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002272
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002273 mddev->utime = get_seconds();
2274
NeilBrown850b2b42006-10-03 01:15:46 -07002275 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2276 force_change = 1;
2277 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2278 /* just a clean<-> dirty transition, possibly leave spares alone,
2279 * though if events isn't the right even/odd, we will have to do
2280 * spares after all
2281 */
2282 nospares = 1;
2283 if (force_change)
2284 nospares = 0;
2285 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002286 /* If the array is degraded, then skipping spares is both
2287 * dangerous and fairly pointless.
2288 * Dangerous because a device that was removed from the array
2289 * might have a event_count that still looks up-to-date,
2290 * so it can be re-added without a resync.
2291 * Pointless because if there are any spares to skip,
2292 * then a recovery will happen and soon that array won't
2293 * be degraded any more and the spare can go back to sleep then.
2294 */
NeilBrown850b2b42006-10-03 01:15:46 -07002295 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002296
NeilBrown06d91a52005-06-21 17:17:12 -07002297 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002298
2299 /* If this is just a dirty<->clean transition, and the array is clean
2300 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002301 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002302 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002303 && mddev->can_decrease_events
2304 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002305 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002306 mddev->can_decrease_events = 0;
2307 } else {
NeilBrown42543762006-06-26 00:27:57 -07002308 /* otherwise we have to go forward and ... */
2309 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002310 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
NeilBrown403df472014-09-30 15:52:29 +10002313 /*
2314 * This 64-bit counter should never wrap.
2315 * Either we are in around ~1 trillion A.C., assuming
2316 * 1 reboot per second, or we have a bug...
2317 */
2318 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002319
NeilBrowndafb20f2012-03-19 12:46:39 +11002320 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002321 if (rdev->badblocks.changed)
2322 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002323 if (test_bit(Faulty, &rdev->flags))
2324 set_bit(FaultRecorded, &rdev->flags);
2325 }
NeilBrown2699b672011-07-28 11:31:47 +10002326
NeilBrowne6910632008-02-06 01:39:51 -08002327 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002328 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
NeilBrown36a4e1f2011-10-07 14:23:17 +11002330 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2331 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
NeilBrown4ad13662007-07-17 04:06:13 -07002333 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002334 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002336
NeilBrown42543762006-06-26 00:27:57 -07002337 if (rdev->sb_loaded != 1)
2338 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
NeilBrownf4667222013-12-09 12:04:56 +11002340 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002341 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002342 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002343 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002344 pr_debug("md: (write) %s's sb offset: %llu\n",
2345 bdevname(rdev->bdev, b),
2346 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002347 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002348 if (rdev->badblocks.size) {
2349 md_super_write(mddev, rdev,
2350 rdev->badblocks.sector,
2351 rdev->badblocks.size << 9,
2352 rdev->bb_page);
2353 rdev->badblocks.size = 0;
2354 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002355
NeilBrownf4667222013-12-09 12:04:56 +11002356 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002357 pr_debug("md: %s (skipping faulty)\n",
2358 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002359
NeilBrown7bfa19f2005-06-21 17:17:28 -07002360 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 /* only need to write one superblock... */
2362 break;
2363 }
NeilBrowna9701a32005-11-08 21:39:34 -08002364 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002365 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002366
NeilBrown85572d72014-12-15 12:56:56 +11002367 spin_lock(&mddev->lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002368 if (mddev->in_sync != sync_req ||
2369 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002370 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002371 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002372 goto repeat;
2373 }
NeilBrown850b2b42006-10-03 01:15:46 -07002374 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002375 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002376 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002377 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2378 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002379
NeilBrowndafb20f2012-03-19 12:46:39 +11002380 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002381 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2382 clear_bit(Blocked, &rdev->flags);
2383
2384 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002385 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002386 clear_bit(BlockedBadBlocks, &rdev->flags);
2387 wake_up(&rdev->blocked_wait);
2388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
Andre Noll7f6ce762008-03-23 18:34:54 +01002391/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002392 * We want to accept with case. For this we use cmd_match.
2393 */
2394static int cmd_match(const char *cmd, const char *str)
2395{
2396 /* See if cmd, written into a sysfs file, matches
2397 * str. They must either be the same, or cmd can
2398 * have a trailing newline
2399 */
2400 while (*cmd && *str && *cmd == *str) {
2401 cmd++;
2402 str++;
2403 }
2404 if (*cmd == '\n')
2405 cmd++;
2406 if (*str || *cmd)
2407 return 0;
2408 return 1;
2409}
2410
NeilBrown86e6ffd2005-11-08 21:39:24 -08002411struct rdev_sysfs_entry {
2412 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002413 ssize_t (*show)(struct md_rdev *, char *);
2414 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002415};
2416
2417static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002418state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002419{
2420 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002421 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002422
NeilBrownde393cd2011-07-28 11:31:48 +10002423 if (test_bit(Faulty, &rdev->flags) ||
2424 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002425 len+= sprintf(page+len, "%sfaulty",sep);
2426 sep = ",";
2427 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002428 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002429 len += sprintf(page+len, "%sin_sync",sep);
2430 sep = ",";
2431 }
NeilBrownf6556752006-06-26 00:28:01 -07002432 if (test_bit(WriteMostly, &rdev->flags)) {
2433 len += sprintf(page+len, "%swrite_mostly",sep);
2434 sep = ",";
2435 }
NeilBrownde393cd2011-07-28 11:31:48 +10002436 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002437 (rdev->badblocks.unacked_exist
2438 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002439 len += sprintf(page+len, "%sblocked", sep);
2440 sep = ",";
2441 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002442 if (!test_bit(Faulty, &rdev->flags) &&
2443 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002444 len += sprintf(page+len, "%sspare", sep);
2445 sep = ",";
2446 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002447 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2448 len += sprintf(page+len, "%swrite_error", sep);
2449 sep = ",";
2450 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002451 if (test_bit(WantReplacement, &rdev->flags)) {
2452 len += sprintf(page+len, "%swant_replacement", sep);
2453 sep = ",";
2454 }
2455 if (test_bit(Replacement, &rdev->flags)) {
2456 len += sprintf(page+len, "%sreplacement", sep);
2457 sep = ",";
2458 }
2459
NeilBrown86e6ffd2005-11-08 21:39:24 -08002460 return len+sprintf(page+len, "\n");
2461}
2462
NeilBrown45dc2de2006-06-26 00:27:58 -07002463static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002464state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002465{
2466 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002467 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002468 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002469 * writemostly - sets write_mostly
2470 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002471 * blocked - sets the Blocked flags
2472 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002473 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002474 * -insync - clear Insync for a device with a slot assigned,
2475 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002476 * write_error - sets WriteErrorSeen
2477 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002478 */
2479 int err = -EINVAL;
2480 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2481 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002482 if (test_bit(Faulty, &rdev->flags))
2483 err = 0;
2484 else
2485 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002486 } else if (cmd_match(buf, "remove")) {
2487 if (rdev->raid_disk >= 0)
2488 err = -EBUSY;
2489 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002490 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002491 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002492 if (mddev->pers)
2493 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002494 md_new_event(mddev);
2495 err = 0;
2496 }
NeilBrownf6556752006-06-26 00:28:01 -07002497 } else if (cmd_match(buf, "writemostly")) {
2498 set_bit(WriteMostly, &rdev->flags);
2499 err = 0;
2500 } else if (cmd_match(buf, "-writemostly")) {
2501 clear_bit(WriteMostly, &rdev->flags);
2502 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002503 } else if (cmd_match(buf, "blocked")) {
2504 set_bit(Blocked, &rdev->flags);
2505 err = 0;
2506 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002507 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002508 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002509 /* metadata handler doesn't understand badblocks,
2510 * so we need to fail the device
2511 */
2512 md_error(rdev->mddev, rdev);
2513 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002514 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002515 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002516 wake_up(&rdev->blocked_wait);
2517 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2518 md_wakeup_thread(rdev->mddev->thread);
2519
2520 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002521 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2522 set_bit(In_sync, &rdev->flags);
2523 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002524 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
NeilBrowne1960f82014-09-30 15:24:25 +10002525 if (rdev->mddev->pers == NULL) {
2526 clear_bit(In_sync, &rdev->flags);
2527 rdev->saved_raid_disk = rdev->raid_disk;
2528 rdev->raid_disk = -1;
2529 err = 0;
2530 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002531 } else if (cmd_match(buf, "write_error")) {
2532 set_bit(WriteErrorSeen, &rdev->flags);
2533 err = 0;
2534 } else if (cmd_match(buf, "-write_error")) {
2535 clear_bit(WriteErrorSeen, &rdev->flags);
2536 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002537 } else if (cmd_match(buf, "want_replacement")) {
2538 /* Any non-spare device that is not a replacement can
2539 * become want_replacement at any time, but we then need to
2540 * check if recovery is needed.
2541 */
2542 if (rdev->raid_disk >= 0 &&
2543 !test_bit(Replacement, &rdev->flags))
2544 set_bit(WantReplacement, &rdev->flags);
2545 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2546 md_wakeup_thread(rdev->mddev->thread);
2547 err = 0;
2548 } else if (cmd_match(buf, "-want_replacement")) {
2549 /* Clearing 'want_replacement' is always allowed.
2550 * Once replacements starts it is too late though.
2551 */
2552 err = 0;
2553 clear_bit(WantReplacement, &rdev->flags);
2554 } else if (cmd_match(buf, "replacement")) {
2555 /* Can only set a device as a replacement when array has not
2556 * yet been started. Once running, replacement is automatic
2557 * from spares, or by assigning 'slot'.
2558 */
2559 if (rdev->mddev->pers)
2560 err = -EBUSY;
2561 else {
2562 set_bit(Replacement, &rdev->flags);
2563 err = 0;
2564 }
2565 } else if (cmd_match(buf, "-replacement")) {
2566 /* Similarly, can only clear Replacement before start */
2567 if (rdev->mddev->pers)
2568 err = -EBUSY;
2569 else {
2570 clear_bit(Replacement, &rdev->flags);
2571 err = 0;
2572 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002573 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002574 if (!err)
2575 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002576 return err ? err : len;
2577}
NeilBrown80ca3a42006-07-10 04:44:18 -07002578static struct rdev_sysfs_entry rdev_state =
2579__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002580
2581static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002582errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002583{
2584 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2585}
2586
2587static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002588errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002589{
2590 char *e;
2591 unsigned long n = simple_strtoul(buf, &e, 10);
2592 if (*buf && (*e == 0 || *e == '\n')) {
2593 atomic_set(&rdev->corrected_errors, n);
2594 return len;
2595 }
2596 return -EINVAL;
2597}
2598static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002599__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002600
NeilBrown014236d2006-01-06 00:20:55 -08002601static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002602slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002603{
2604 if (rdev->raid_disk < 0)
2605 return sprintf(page, "none\n");
2606 else
2607 return sprintf(page, "%d\n", rdev->raid_disk);
2608}
2609
2610static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002611slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002612{
2613 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002614 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002615 int slot = simple_strtoul(buf, &e, 10);
2616 if (strncmp(buf, "none", 4)==0)
2617 slot = -1;
2618 else if (e==buf || (*e && *e!= '\n'))
2619 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002620 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002621 /* Setting 'slot' on an active array requires also
2622 * updating the 'rd%d' link, and communicating
2623 * with the personality with ->hot_*_disk.
2624 * For now we only support removing
2625 * failed/spare devices. This normally happens automatically,
2626 * but not when the metadata is externally managed.
2627 */
NeilBrownc303da62008-02-06 01:39:51 -08002628 if (rdev->raid_disk == -1)
2629 return -EEXIST;
2630 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002631 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002632 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002633 clear_bit(Blocked, &rdev->flags);
2634 remove_and_add_spares(rdev->mddev, rdev);
2635 if (rdev->raid_disk >= 0)
2636 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002637 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2638 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002639 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002640 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002641 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002642 */
2643
2644 if (rdev->raid_disk != -1)
2645 return -EBUSY;
2646
NeilBrownc6751b22011-02-02 11:57:13 +11002647 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2648 return -EBUSY;
2649
Neil Brown6c2fce22008-06-28 08:31:31 +10002650 if (rdev->mddev->pers->hot_add_disk == NULL)
2651 return -EINVAL;
2652
NeilBrownba1b41b2011-01-14 09:14:34 +11002653 if (slot >= rdev->mddev->raid_disks &&
2654 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2655 return -ENOSPC;
2656
Neil Brown6c2fce22008-06-28 08:31:31 +10002657 rdev->raid_disk = slot;
2658 if (test_bit(In_sync, &rdev->flags))
2659 rdev->saved_raid_disk = slot;
2660 else
2661 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002662 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002663 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002664 err = rdev->mddev->pers->
2665 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002666 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002667 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002668 return err;
Neil Brown52664732008-06-28 08:31:44 +10002669 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002670 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002671 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002672 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002673 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002674 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002675 if (slot >= rdev->mddev->raid_disks &&
2676 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002677 return -ENOSPC;
2678 rdev->raid_disk = slot;
2679 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002680 clear_bit(Faulty, &rdev->flags);
2681 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002682 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002683 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002684 }
NeilBrown014236d2006-01-06 00:20:55 -08002685 return len;
2686}
2687
NeilBrown014236d2006-01-06 00:20:55 -08002688static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002689__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002690
NeilBrown93c8cad2006-01-06 00:20:56 -08002691static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002692offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002693{
Andrew Morton6961ece2006-01-06 00:20:59 -08002694 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002695}
2696
2697static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002698offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002699{
NeilBrownc6563a82012-05-21 09:27:00 +10002700 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002701 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002702 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002703 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002704 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002705 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002706 /* Must set offset before size, so overlap checks
2707 * can be sane */
2708 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002709 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002710 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002711 return len;
2712}
2713
2714static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002715__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002716
NeilBrownc6563a82012-05-21 09:27:00 +10002717static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2718{
2719 return sprintf(page, "%llu\n",
2720 (unsigned long long)rdev->new_data_offset);
2721}
2722
2723static ssize_t new_offset_store(struct md_rdev *rdev,
2724 const char *buf, size_t len)
2725{
2726 unsigned long long new_offset;
2727 struct mddev *mddev = rdev->mddev;
2728
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002729 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002730 return -EINVAL;
2731
NeilBrownf851b602014-12-11 10:02:10 +11002732 if (mddev->sync_thread ||
2733 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002734 return -EBUSY;
2735 if (new_offset == rdev->data_offset)
2736 /* reset is always permitted */
2737 ;
2738 else if (new_offset > rdev->data_offset) {
2739 /* must not push array size beyond rdev_sectors */
2740 if (new_offset - rdev->data_offset
2741 + mddev->dev_sectors > rdev->sectors)
2742 return -E2BIG;
2743 }
2744 /* Metadata worries about other space details. */
2745
2746 /* decreasing the offset is inconsistent with a backwards
2747 * reshape.
2748 */
2749 if (new_offset < rdev->data_offset &&
2750 mddev->reshape_backwards)
2751 return -EINVAL;
2752 /* Increasing offset is inconsistent with forwards
2753 * reshape. reshape_direction should be set to
2754 * 'backwards' first.
2755 */
2756 if (new_offset > rdev->data_offset &&
2757 !mddev->reshape_backwards)
2758 return -EINVAL;
2759
2760 if (mddev->pers && mddev->persistent &&
2761 !super_types[mddev->major_version]
2762 .allow_new_offset(rdev, new_offset))
2763 return -E2BIG;
2764 rdev->new_data_offset = new_offset;
2765 if (new_offset > rdev->data_offset)
2766 mddev->reshape_backwards = 1;
2767 else if (new_offset < rdev->data_offset)
2768 mddev->reshape_backwards = 0;
2769
2770 return len;
2771}
2772static struct rdev_sysfs_entry rdev_new_offset =
2773__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2774
NeilBrown83303b62006-01-06 00:21:06 -08002775static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002776rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002777{
Andre Nolldd8ac332009-03-31 14:33:13 +11002778 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002779}
2780
NeilBrownc5d79ad2008-02-06 01:39:54 -08002781static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2782{
2783 /* check if two start/length pairs overlap */
2784 if (s1+l1 <= s2)
2785 return 0;
2786 if (s2+l2 <= s1)
2787 return 0;
2788 return 1;
2789}
2790
Dan Williamsb522adc2009-03-31 15:00:31 +11002791static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2792{
2793 unsigned long long blocks;
2794 sector_t new;
2795
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002796 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002797 return -EINVAL;
2798
2799 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2800 return -EINVAL; /* sector conversion overflow */
2801
2802 new = blocks * 2;
2803 if (new != blocks * 2)
2804 return -EINVAL; /* unsigned long long to sector_t overflow */
2805
2806 *sectors = new;
2807 return 0;
2808}
2809
NeilBrown83303b62006-01-06 00:21:06 -08002810static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002811rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002812{
NeilBrownfd01b882011-10-11 16:47:53 +11002813 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002814 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002815 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002816
Dan Williamsb522adc2009-03-31 15:00:31 +11002817 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002818 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002819 if (rdev->data_offset != rdev->new_data_offset)
2820 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002821 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002822 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002823 sectors = super_types[my_mddev->major_version].
2824 rdev_size_change(rdev, sectors);
2825 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002826 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002827 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002828 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002829 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002830 if (!my_mddev->pers->resize)
2831 /* Cannot change size for RAID0 or Linear etc */
2832 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002833 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002834 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002835 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002836
Andre Nolldd8ac332009-03-31 14:33:13 +11002837 rdev->sectors = sectors;
2838 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002839 /* Need to check that all other rdevs with the same
2840 * ->bdev do not overlap. 'rcu' is sufficient to walk
2841 * the rdev lists safely.
2842 * This check does not provide a hard guarantee, it
2843 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002844 */
NeilBrownfd01b882011-10-11 16:47:53 +11002845 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002846 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002847 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002848
NeilBrown8b1afc32014-09-29 15:33:20 +10002849 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002850 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002851 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002852
NeilBrowndafb20f2012-03-19 12:46:39 +11002853 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002854 if (rdev->bdev == rdev2->bdev &&
2855 rdev != rdev2 &&
2856 overlaps(rdev->data_offset, rdev->sectors,
2857 rdev2->data_offset,
2858 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002859 overlap = 1;
2860 break;
2861 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002862 if (overlap) {
2863 mddev_put(mddev);
2864 break;
2865 }
2866 }
NeilBrown8b1afc32014-09-29 15:33:20 +10002867 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08002868 if (overlap) {
2869 /* Someone else could have slipped in a size
2870 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002871 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002872 * safe, and trust userspace not to race with
2873 * itself
2874 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002875 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002876 return -EBUSY;
2877 }
2878 }
NeilBrown83303b62006-01-06 00:21:06 -08002879 return len;
2880}
2881
2882static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002883__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002884
NeilBrown3cb03002011-10-11 16:45:26 +11002885static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002886{
2887 unsigned long long recovery_start = rdev->recovery_offset;
2888
2889 if (test_bit(In_sync, &rdev->flags) ||
2890 recovery_start == MaxSector)
2891 return sprintf(page, "none\n");
2892
2893 return sprintf(page, "%llu\n", recovery_start);
2894}
2895
NeilBrown3cb03002011-10-11 16:45:26 +11002896static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002897{
2898 unsigned long long recovery_start;
2899
2900 if (cmd_match(buf, "none"))
2901 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002902 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07002903 return -EINVAL;
2904
2905 if (rdev->mddev->pers &&
2906 rdev->raid_disk >= 0)
2907 return -EBUSY;
2908
2909 rdev->recovery_offset = recovery_start;
2910 if (recovery_start == MaxSector)
2911 set_bit(In_sync, &rdev->flags);
2912 else
2913 clear_bit(In_sync, &rdev->flags);
2914 return len;
2915}
2916
2917static struct rdev_sysfs_entry rdev_recovery_start =
2918__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2919
NeilBrown16c791a2011-07-28 11:31:47 +10002920static ssize_t
2921badblocks_show(struct badblocks *bb, char *page, int unack);
2922static ssize_t
2923badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2924
NeilBrown3cb03002011-10-11 16:45:26 +11002925static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002926{
2927 return badblocks_show(&rdev->badblocks, page, 0);
2928}
NeilBrown3cb03002011-10-11 16:45:26 +11002929static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002930{
NeilBrownde393cd2011-07-28 11:31:48 +10002931 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2932 /* Maybe that ack was all we needed */
2933 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2934 wake_up(&rdev->blocked_wait);
2935 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002936}
2937static struct rdev_sysfs_entry rdev_bad_blocks =
2938__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2939
NeilBrown3cb03002011-10-11 16:45:26 +11002940static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002941{
2942 return badblocks_show(&rdev->badblocks, page, 1);
2943}
NeilBrown3cb03002011-10-11 16:45:26 +11002944static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002945{
2946 return badblocks_store(&rdev->badblocks, page, len, 1);
2947}
2948static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2949__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2950
NeilBrown86e6ffd2005-11-08 21:39:24 -08002951static struct attribute *rdev_default_attrs[] = {
2952 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002953 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002954 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002955 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10002956 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002957 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002958 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002959 &rdev_bad_blocks.attr,
2960 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002961 NULL,
2962};
2963static ssize_t
2964rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2965{
2966 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002967 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11002968 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08002969 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002970
2971 if (!entry->show)
2972 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002973
2974 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2975 if (!rv) {
2976 if (rdev->mddev == NULL)
2977 rv = -EBUSY;
2978 else
2979 rv = entry->show(rdev, page);
2980 mddev_unlock(mddev);
2981 }
2982 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002983}
2984
2985static ssize_t
2986rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2987 const char *page, size_t length)
2988{
2989 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002990 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002991 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11002992 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002993
2994 if (!entry->store)
2995 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002996 if (!capable(CAP_SYS_ADMIN))
2997 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002998 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002999 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003000 if (rdev->mddev == NULL)
3001 rv = -EBUSY;
3002 else
3003 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003004 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003005 }
3006 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003007}
3008
3009static void rdev_free(struct kobject *ko)
3010{
NeilBrown3cb03002011-10-11 16:45:26 +11003011 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003012 kfree(rdev);
3013}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003014static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003015 .show = rdev_attr_show,
3016 .store = rdev_attr_store,
3017};
3018static struct kobj_type rdev_ktype = {
3019 .release = rdev_free,
3020 .sysfs_ops = &rdev_sysfs_ops,
3021 .default_attrs = rdev_default_attrs,
3022};
3023
NeilBrown3cb03002011-10-11 16:45:26 +11003024int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003025{
3026 rdev->desc_nr = -1;
3027 rdev->saved_raid_disk = -1;
3028 rdev->raid_disk = -1;
3029 rdev->flags = 0;
3030 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003031 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003032 rdev->sb_events = 0;
3033 rdev->last_read_error.tv_sec = 0;
3034 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003035 rdev->sb_loaded = 0;
3036 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003037 atomic_set(&rdev->nr_pending, 0);
3038 atomic_set(&rdev->read_errors, 0);
3039 atomic_set(&rdev->corrected_errors, 0);
3040
3041 INIT_LIST_HEAD(&rdev->same_set);
3042 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003043
3044 /* Add space to store bad block list.
3045 * This reserves the space even on arrays where it cannot
3046 * be used - I wonder if that matters
3047 */
3048 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003049 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003050 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3051 seqlock_init(&rdev->badblocks.lock);
3052 if (rdev->badblocks.page == NULL)
3053 return -ENOMEM;
3054
3055 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003056}
3057EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058/*
3059 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3060 *
3061 * mark the device faulty if:
3062 *
3063 * - the device is nonexistent (zero size)
3064 * - the device has no valid superblock
3065 *
3066 * a faulty rdev _never_ has rdev->sb set.
3067 */
NeilBrown3cb03002011-10-11 16:45:26 +11003068static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
3070 char b[BDEVNAME_SIZE];
3071 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003072 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 sector_t size;
3074
NeilBrown9ffae0c2006-01-06 00:20:32 -08003075 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 if (!rdev) {
3077 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3078 return ERR_PTR(-ENOMEM);
3079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
NeilBrown2230dfe2011-07-28 11:31:46 +10003081 err = md_rdev_init(rdev);
3082 if (err)
3083 goto abort_free;
3084 err = alloc_disk_sb(rdev);
3085 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 goto abort_free;
3087
NeilBrownc5d79ad2008-02-06 01:39:54 -08003088 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 if (err)
3090 goto abort_free;
3091
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003092 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003093
Mike Snitzer77304d22010-11-08 14:39:12 +01003094 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003096 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 "md: %s has zero or unknown size, marking faulty!\n",
3098 bdevname(rdev->bdev,b));
3099 err = -EINVAL;
3100 goto abort_free;
3101 }
3102
3103 if (super_format >= 0) {
3104 err = super_types[super_format].
3105 load_super(rdev, NULL, super_minor);
3106 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003107 printk(KERN_WARNING
3108 "md: %s does not have a valid v%d.%d "
3109 "superblock, not importing!\n",
3110 bdevname(rdev->bdev,b),
3111 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 goto abort_free;
3113 }
3114 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003115 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 "md: could not read %s's sb, not importing!\n",
3117 bdevname(rdev->bdev,b));
3118 goto abort_free;
3119 }
3120 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 return rdev;
3123
3124abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003125 if (rdev->bdev)
3126 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003127 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 kfree(rdev);
3129 return ERR_PTR(err);
3130}
3131
3132/*
3133 * Check a full RAID array for plausibility
3134 */
3135
NeilBrownf72ffdd2014-09-30 14:23:59 +10003136static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137{
3138 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003139 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 char b[BDEVNAME_SIZE];
3141
3142 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003143 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 switch (super_types[mddev->major_version].
3145 load_super(rdev, freshest, mddev->minor_version)) {
3146 case 1:
3147 freshest = rdev;
3148 break;
3149 case 0:
3150 break;
3151 default:
3152 printk( KERN_ERR \
3153 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003154 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 bdevname(rdev->bdev,b));
3156 kick_rdev_from_array(rdev);
3157 }
3158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 super_types[mddev->major_version].
3160 validate_super(mddev, freshest);
3161
3162 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003163 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003164 if (mddev->max_disks &&
3165 (rdev->desc_nr >= mddev->max_disks ||
3166 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003167 printk(KERN_WARNING
3168 "md: %s: %s: only %d devices permitted\n",
3169 mdname(mddev), bdevname(rdev->bdev, b),
3170 mddev->max_disks);
3171 kick_rdev_from_array(rdev);
3172 continue;
3173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 if (rdev != freshest)
3175 if (super_types[mddev->major_version].
3176 validate_super(mddev, rdev)) {
3177 printk(KERN_WARNING "md: kicking non-fresh %s"
3178 " from array!\n",
3179 bdevname(rdev->bdev,b));
3180 kick_rdev_from_array(rdev);
3181 continue;
3182 }
3183 if (mddev->level == LEVEL_MULTIPATH) {
3184 rdev->desc_nr = i++;
3185 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003186 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003187 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003188 rdev->raid_disk = -1;
3189 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 }
3191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192}
3193
NeilBrown72e02072009-12-14 12:49:55 +11003194/* Read a fixed-point number.
3195 * Numbers in sysfs attributes should be in "standard" units where
3196 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003197 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003198 * milliseconds or jiffies.
3199 * This function takes a decimal number with a possible fractional
3200 * component, and produces an integer which is the result of
3201 * multiplying that number by 10^'scale'.
3202 * all without any floating-point arithmetic.
3203 */
3204int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3205{
3206 unsigned long result = 0;
3207 long decimals = -1;
3208 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3209 if (*cp == '.')
3210 decimals = 0;
3211 else if (decimals < scale) {
3212 unsigned int value;
3213 value = *cp - '0';
3214 result = result * 10 + value;
3215 if (decimals >= 0)
3216 decimals++;
3217 }
3218 cp++;
3219 }
3220 if (*cp == '\n')
3221 cp++;
3222 if (*cp)
3223 return -EINVAL;
3224 if (decimals < 0)
3225 decimals = 0;
3226 while (decimals < scale) {
3227 result *= 10;
3228 decimals ++;
3229 }
3230 *res = result;
3231 return 0;
3232}
3233
NeilBrown19052c02008-08-05 15:54:13 +10003234static void md_safemode_timeout(unsigned long data);
3235
NeilBrowneae17012005-11-08 21:39:23 -08003236static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003237safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003238{
3239 int msec = (mddev->safemode_delay*1000)/HZ;
3240 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3241}
3242static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003243safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003244{
NeilBrown16f17b32006-06-26 00:27:37 -07003245 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003246
NeilBrown72e02072009-12-14 12:49:55 +11003247 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003248 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003249 if (msec == 0)
3250 mddev->safemode_delay = 0;
3251 else {
NeilBrown19052c02008-08-05 15:54:13 +10003252 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003253 mddev->safemode_delay = (msec*HZ)/1000;
3254 if (mddev->safemode_delay == 0)
3255 mddev->safemode_delay = 1;
NeilBrown275c51c2013-08-08 09:20:32 +10003256 if (mddev->safemode_delay < old_delay || old_delay == 0)
NeilBrown19052c02008-08-05 15:54:13 +10003257 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003258 }
3259 return len;
3260}
3261static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003262__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003263
3264static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003265level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003266{
NeilBrown36d091f2014-12-15 12:56:58 +11003267 struct md_personality *p;
3268 int ret;
3269 spin_lock(&mddev->lock);
3270 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003271 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003272 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003273 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003274 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003275 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003276 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003277 else
NeilBrown36d091f2014-12-15 12:56:58 +11003278 ret = 0;
3279 spin_unlock(&mddev->lock);
3280 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003281}
3282
NeilBrownd9d166c2006-01-06 00:20:51 -08003283static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003284level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003285{
Dan Williamsf2859af2010-05-02 10:04:16 -07003286 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003287 ssize_t rv = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003288 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003289 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003290 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003291 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003292
3293 if (mddev->pers == NULL) {
3294 if (len == 0)
3295 return 0;
3296 if (len >= sizeof(mddev->clevel))
3297 return -ENOSPC;
3298 strncpy(mddev->clevel, buf, len);
3299 if (mddev->clevel[len-1] == '\n')
3300 len--;
3301 mddev->clevel[len] = 0;
3302 mddev->level = LEVEL_NONE;
3303 return rv;
3304 }
NeilBrownbd8839e2014-05-28 13:39:21 +10003305 if (mddev->ro)
3306 return -EROFS;
NeilBrown245f46c2009-03-31 14:39:39 +11003307
3308 /* request to change the personality. Need to ensure:
3309 * - array is not engaged in resync/recovery/reshape
3310 * - old personality can be suspended
3311 * - new personality will access other array.
3312 */
3313
NeilBrownbb4f1e92010-08-08 21:18:03 +10003314 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003315 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003316 mddev->reshape_position != MaxSector ||
3317 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003318 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003319
3320 if (!mddev->pers->quiesce) {
3321 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3322 mdname(mddev), mddev->pers->name);
3323 return -EINVAL;
3324 }
3325
3326 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003327 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003328 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003329 strncpy(clevel, buf, len);
3330 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003331 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003332 clevel[len] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003333 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003334 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003335
Dan Williamsf2859af2010-05-02 10:04:16 -07003336 if (request_module("md-%s", clevel) != 0)
3337 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003338 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003339 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003340 if (!pers || !try_module_get(pers->owner)) {
3341 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003342 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003343 return -EINVAL;
3344 }
3345 spin_unlock(&pers_lock);
3346
3347 if (pers == mddev->pers) {
3348 /* Nothing to do! */
3349 module_put(pers->owner);
3350 return rv;
3351 }
3352 if (!pers->takeover) {
3353 module_put(pers->owner);
3354 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003355 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003356 return -EINVAL;
3357 }
3358
NeilBrowndafb20f2012-03-19 12:46:39 +11003359 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003360 rdev->new_raid_disk = rdev->raid_disk;
3361
NeilBrown245f46c2009-03-31 14:39:39 +11003362 /* ->takeover must set new_* and/or delta_disks
3363 * if it succeeds, and may set them when it fails.
3364 */
3365 priv = pers->takeover(mddev);
3366 if (IS_ERR(priv)) {
3367 mddev->new_level = mddev->level;
3368 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003369 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003370 mddev->raid_disks -= mddev->delta_disks;
3371 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003372 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003373 module_put(pers->owner);
3374 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003375 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003376 return PTR_ERR(priv);
3377 }
3378
3379 /* Looks like we have a winner */
3380 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003381 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003382
3383 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003384 oldpers = mddev->pers;
3385 oldpriv = mddev->private;
3386 mddev->pers = pers;
3387 mddev->private = priv;
3388 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3389 mddev->level = mddev->new_level;
3390 mddev->layout = mddev->new_layout;
3391 mddev->chunk_sectors = mddev->new_chunk_sectors;
3392 mddev->delta_disks = 0;
3393 mddev->reshape_backwards = 0;
3394 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003395 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003396
NeilBrowndb721d32014-12-15 12:56:58 +11003397 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003398 mddev->external) {
3399 /* We are converting from a no-redundancy array
3400 * to a redundancy array and metadata is managed
3401 * externally so we need to be sure that writes
3402 * won't block due to a need to transition
3403 * clean->dirty
3404 * until external management is started.
3405 */
3406 mddev->in_sync = 0;
3407 mddev->safemode_delay = 0;
3408 mddev->safemode = 0;
3409 }
3410
NeilBrowndb721d32014-12-15 12:56:58 +11003411 oldpers->free(mddev, oldpriv);
3412
3413 if (oldpers->sync_request == NULL &&
3414 pers->sync_request != NULL) {
3415 /* need to add the md_redundancy_group */
3416 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3417 printk(KERN_WARNING
3418 "md: cannot register extra attributes for %s\n",
3419 mdname(mddev));
3420 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3421 }
3422 if (oldpers->sync_request != NULL &&
3423 pers->sync_request == NULL) {
3424 /* need to remove the md_redundancy_group */
3425 if (mddev->to_remove == NULL)
3426 mddev->to_remove = &md_redundancy_group;
3427 }
3428
NeilBrowndafb20f2012-03-19 12:46:39 +11003429 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003430 if (rdev->raid_disk < 0)
3431 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003432 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003433 rdev->new_raid_disk = -1;
3434 if (rdev->new_raid_disk == rdev->raid_disk)
3435 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003436 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003437 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003438 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003439 if (rdev->raid_disk < 0)
3440 continue;
3441 if (rdev->new_raid_disk == rdev->raid_disk)
3442 continue;
3443 rdev->raid_disk = rdev->new_raid_disk;
3444 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003445 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003446 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003447 if (sysfs_link_rdev(mddev, rdev))
3448 printk(KERN_WARNING "md: cannot register rd%d"
3449 " for %s after level change\n",
3450 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003451 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003452 }
3453
NeilBrowndb721d32014-12-15 12:56:58 +11003454 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003455 /* this is now an array without redundancy, so
3456 * it must always be in_sync
3457 */
3458 mddev->in_sync = 1;
3459 del_timer_sync(&mddev->safemode_timer);
3460 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003461 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003462 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003463 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003464 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003465 if (!mddev->thread)
3466 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003467 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003468 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003469 return rv;
3470}
3471
3472static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003473__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003474
NeilBrownd4dbd022006-06-26 00:27:59 -07003475static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003476layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003477{
3478 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003479 if (mddev->reshape_position != MaxSector &&
3480 mddev->layout != mddev->new_layout)
3481 return sprintf(page, "%d (%d)\n",
3482 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003483 return sprintf(page, "%d\n", mddev->layout);
3484}
3485
3486static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003487layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003488{
3489 char *e;
3490 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003491
3492 if (!*buf || (*e && *e != '\n'))
3493 return -EINVAL;
3494
NeilBrownb3546032009-03-31 14:56:41 +11003495 if (mddev->pers) {
3496 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003497 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003498 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10003499 if (mddev->ro)
3500 return -EROFS;
NeilBrown597a7112009-06-18 08:47:42 +10003501 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003502 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003503 if (err) {
3504 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003505 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003506 }
NeilBrownb3546032009-03-31 14:56:41 +11003507 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003508 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003509 if (mddev->reshape_position == MaxSector)
3510 mddev->layout = n;
3511 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003512 return len;
3513}
3514static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003515__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003516
NeilBrowneae17012005-11-08 21:39:23 -08003517static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003518raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003519{
NeilBrownbb636542005-11-08 21:39:45 -08003520 if (mddev->raid_disks == 0)
3521 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003522 if (mddev->reshape_position != MaxSector &&
3523 mddev->delta_disks != 0)
3524 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3525 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003526 return sprintf(page, "%d\n", mddev->raid_disks);
3527}
3528
NeilBrownfd01b882011-10-11 16:47:53 +11003529static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003530
3531static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003532raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003533{
NeilBrownda943b992006-01-06 00:20:54 -08003534 char *e;
3535 int rv = 0;
3536 unsigned long n = simple_strtoul(buf, &e, 10);
3537
3538 if (!*buf || (*e && *e != '\n'))
3539 return -EINVAL;
3540
3541 if (mddev->pers)
3542 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003543 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003544 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003545 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003546
3547 rdev_for_each(rdev, mddev) {
3548 if (olddisks < n &&
3549 rdev->data_offset < rdev->new_data_offset)
3550 return -EINVAL;
3551 if (olddisks > n &&
3552 rdev->data_offset > rdev->new_data_offset)
3553 return -EINVAL;
3554 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003555 mddev->delta_disks = n - olddisks;
3556 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003557 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003558 } else
NeilBrownda943b992006-01-06 00:20:54 -08003559 mddev->raid_disks = n;
3560 return rv ? rv : len;
3561}
3562static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003563__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003564
NeilBrown24dd4692005-11-08 21:39:26 -08003565static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003566chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003567{
NeilBrown08a02ec2007-05-09 02:35:38 -07003568 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003569 mddev->chunk_sectors != mddev->new_chunk_sectors)
3570 return sprintf(page, "%d (%d)\n",
3571 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003572 mddev->chunk_sectors << 9);
3573 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003574}
3575
3576static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003577chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003578{
NeilBrown3b343802006-01-06 00:20:47 -08003579 char *e;
3580 unsigned long n = simple_strtoul(buf, &e, 10);
3581
NeilBrown3b343802006-01-06 00:20:47 -08003582 if (!*buf || (*e && *e != '\n'))
3583 return -EINVAL;
3584
NeilBrownb3546032009-03-31 14:56:41 +11003585 if (mddev->pers) {
3586 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003587 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003588 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10003589 if (mddev->ro)
3590 return -EROFS;
NeilBrown597a7112009-06-18 08:47:42 +10003591 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003592 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003593 if (err) {
3594 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003595 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003596 }
NeilBrownb3546032009-03-31 14:56:41 +11003597 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003598 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003599 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003600 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003601 }
NeilBrown3b343802006-01-06 00:20:47 -08003602 return len;
3603}
3604static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003605__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003606
NeilBrowna94213b2006-06-26 00:28:00 -07003607static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003608resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003609{
NeilBrownd1a7c502009-03-31 15:24:32 +11003610 if (mddev->recovery_cp == MaxSector)
3611 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003612 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3613}
3614
3615static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003616resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003617{
NeilBrowna94213b2006-06-26 00:28:00 -07003618 char *e;
3619 unsigned long long n = simple_strtoull(buf, &e, 10);
3620
NeilBrownb0986362011-05-11 15:52:21 +10003621 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003622 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003623 if (cmd_match(buf, "none"))
3624 n = MaxSector;
3625 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003626 return -EINVAL;
3627
3628 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003629 if (mddev->pers)
3630 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003631 return len;
3632}
3633static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003634__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003635
NeilBrown9e653b62006-06-26 00:27:58 -07003636/*
3637 * The array state can be:
3638 *
3639 * clear
3640 * No devices, no size, no level
3641 * Equivalent to STOP_ARRAY ioctl
3642 * inactive
3643 * May have some settings, but array is not active
3644 * all IO results in error
3645 * When written, doesn't tear down array, but just stops it
3646 * suspended (not supported yet)
3647 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003648 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003649 * readonly
3650 * no resync can happen. no superblocks get written.
3651 * write requests fail
3652 * read-auto
3653 * like readonly, but behaves like 'clean' on a write request.
3654 *
3655 * clean - no pending writes, but otherwise active.
3656 * When written to inactive array, starts without resync
3657 * If a write request arrives then
3658 * if metadata is known, mark 'dirty' and switch to 'active'.
3659 * if not known, block and switch to write-pending
3660 * If written to an active array that has pending writes, then fails.
3661 * active
3662 * fully active: IO and resync can be happening.
3663 * When written to inactive array, starts with resync
3664 *
3665 * write-pending
3666 * clean, but writes are blocked waiting for 'active' to be written.
3667 *
3668 * active-idle
3669 * like active, but no writes have been seen for a while (100msec).
3670 *
3671 */
3672enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3673 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003674static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003675 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3676 "write-pending", "active-idle", NULL };
3677
3678static int match_word(const char *word, char **list)
3679{
3680 int n;
3681 for (n=0; list[n]; n++)
3682 if (cmd_match(word, list[n]))
3683 break;
3684 return n;
3685}
3686
3687static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003688array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003689{
3690 enum array_state st = inactive;
3691
3692 if (mddev->pers)
3693 switch(mddev->ro) {
3694 case 1:
3695 st = readonly;
3696 break;
3697 case 2:
3698 st = read_auto;
3699 break;
3700 case 0:
3701 if (mddev->in_sync)
3702 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003703 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003704 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003705 else if (mddev->safemode)
3706 st = active_idle;
3707 else
3708 st = active;
3709 }
3710 else {
3711 if (list_empty(&mddev->disks) &&
3712 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003713 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003714 st = clear;
3715 else
3716 st = inactive;
3717 }
3718 return sprintf(page, "%s\n", array_states[st]);
3719}
3720
NeilBrownf72ffdd2014-09-30 14:23:59 +10003721static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3722static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3723static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003724static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003725
3726static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003727array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003728{
3729 int err = -EINVAL;
3730 enum array_state st = match_word(buf, array_states);
3731 switch(st) {
3732 case bad_word:
3733 break;
3734 case clear:
3735 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003736 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003737 break;
3738 case inactive:
3739 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003740 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003741 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003742 else
NeilBrowne6910632008-02-06 01:39:51 -08003743 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003744 break;
3745 case suspended:
3746 break; /* not supported yet */
3747 case readonly:
3748 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003749 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003750 else {
3751 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003752 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003753 err = do_md_run(mddev);
3754 }
3755 break;
3756 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003757 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003758 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003759 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003760 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003761 err = restart_array(mddev);
3762 if (err == 0) {
3763 mddev->ro = 2;
3764 set_disk_ro(mddev->gendisk, 0);
3765 }
NeilBrown9e653b62006-06-26 00:27:58 -07003766 } else {
3767 mddev->ro = 2;
3768 err = do_md_run(mddev);
3769 }
3770 break;
3771 case clean:
3772 if (mddev->pers) {
3773 restart_array(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11003774 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003775 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003776 if (mddev->in_sync == 0) {
3777 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003778 if (mddev->safemode == 1)
3779 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003780 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003781 }
3782 err = 0;
3783 } else
3784 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11003785 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003786 } else
3787 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003788 break;
3789 case active:
3790 if (mddev->pers) {
3791 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003792 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003793 wake_up(&mddev->sb_wait);
3794 err = 0;
3795 } else {
3796 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003797 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003798 err = do_md_run(mddev);
3799 }
3800 break;
3801 case write_pending:
3802 case active_idle:
3803 /* these cannot be set */
3804 break;
3805 }
3806 if (err)
3807 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003808 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003809 if (mddev->hold_active == UNTIL_IOCTL)
3810 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003811 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003812 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003813 }
NeilBrown9e653b62006-06-26 00:27:58 -07003814}
NeilBrown80ca3a42006-07-10 04:44:18 -07003815static struct md_sysfs_entry md_array_state =
3816__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003817
NeilBrown6d7ff7382006-01-06 00:21:16 -08003818static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003819max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003820 return sprintf(page, "%d\n",
3821 atomic_read(&mddev->max_corr_read_errors));
3822}
3823
3824static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003825max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003826{
3827 char *e;
3828 unsigned long n = simple_strtoul(buf, &e, 10);
3829
3830 if (*buf && (*e == 0 || *e == '\n')) {
3831 atomic_set(&mddev->max_corr_read_errors, n);
3832 return len;
3833 }
3834 return -EINVAL;
3835}
3836
3837static struct md_sysfs_entry max_corr_read_errors =
3838__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3839 max_corrected_read_errors_store);
3840
3841static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003842null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003843{
3844 return -EINVAL;
3845}
3846
3847static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003848new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003849{
3850 /* buf must be %d:%d\n? giving major and minor numbers */
3851 /* The new device is added to the array.
3852 * If the array has a persistent superblock, we read the
3853 * superblock to initialise info and check validity.
3854 * Otherwise, only checking done is that in bind_rdev_to_array,
3855 * which mainly checks size.
3856 */
3857 char *e;
3858 int major = simple_strtoul(buf, &e, 10);
3859 int minor;
3860 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003861 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003862 int err;
3863
3864 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3865 return -EINVAL;
3866 minor = simple_strtoul(e+1, &e, 10);
3867 if (*e && *e != '\n')
3868 return -EINVAL;
3869 dev = MKDEV(major, minor);
3870 if (major != MAJOR(dev) ||
3871 minor != MINOR(dev))
3872 return -EOVERFLOW;
3873
NeilBrown6d7ff7382006-01-06 00:21:16 -08003874 if (mddev->persistent) {
3875 rdev = md_import_device(dev, mddev->major_version,
3876 mddev->minor_version);
3877 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003878 struct md_rdev *rdev0
3879 = list_entry(mddev->disks.next,
3880 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08003881 err = super_types[mddev->major_version]
3882 .load_super(rdev, rdev0, mddev->minor_version);
3883 if (err < 0)
3884 goto out;
3885 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003886 } else if (mddev->external)
3887 rdev = md_import_device(dev, -2, -1);
3888 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003889 rdev = md_import_device(dev, -1, -1);
3890
3891 if (IS_ERR(rdev))
3892 return PTR_ERR(rdev);
3893 err = bind_rdev_to_array(rdev, mddev);
3894 out:
3895 if (err)
3896 export_rdev(rdev);
3897 return err ? err : len;
3898}
3899
3900static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003901__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003902
3903static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003904bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07003905{
3906 char *end;
3907 unsigned long chunk, end_chunk;
3908
3909 if (!mddev->bitmap)
3910 goto out;
3911 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3912 while (*buf) {
3913 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3914 if (buf == end) break;
3915 if (*end == '-') { /* range */
3916 buf = end + 1;
3917 end_chunk = simple_strtoul(buf, &end, 0);
3918 if (buf == end) break;
3919 }
3920 if (*end && !isspace(*end)) break;
3921 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003922 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003923 }
3924 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3925out:
3926 return len;
3927}
3928
3929static struct md_sysfs_entry md_bitmap =
3930__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3931
3932static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003933size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08003934{
Andre Noll58c0fed2009-03-31 14:33:13 +11003935 return sprintf(page, "%llu\n",
3936 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003937}
3938
NeilBrownfd01b882011-10-11 16:47:53 +11003939static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003940
3941static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003942size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08003943{
3944 /* If array is inactive, we can reduce the component size, but
3945 * not increase it (except from 0).
3946 * If array is active, we can try an on-line resize
3947 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003948 sector_t sectors;
3949 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003950
Andre Noll58c0fed2009-03-31 14:33:13 +11003951 if (err < 0)
3952 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003953 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003954 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003955 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003956 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003957 if (mddev->dev_sectors == 0 ||
3958 mddev->dev_sectors > sectors)
3959 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003960 else
3961 err = -ENOSPC;
3962 }
3963 return err ? err : len;
3964}
3965
3966static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003967__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003968
Masanari Iida83f0d772012-10-30 00:18:08 +09003969/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08003970 * This is one of
3971 * 'none' for arrays with no metadata (good luck...)
3972 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003973 * or N.M for internally known formats
3974 */
3975static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003976metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003977{
3978 if (mddev->persistent)
3979 return sprintf(page, "%d.%d\n",
3980 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003981 else if (mddev->external)
3982 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003983 else
3984 return sprintf(page, "none\n");
3985}
3986
3987static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003988metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003989{
3990 int major, minor;
3991 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003992 /* Changing the details of 'external' metadata is
3993 * always permitted. Otherwise there must be
3994 * no devices attached to the array.
3995 */
3996 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3997 ;
3998 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003999 return -EBUSY;
4000
4001 if (cmd_match(buf, "none")) {
4002 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004003 mddev->external = 0;
4004 mddev->major_version = 0;
4005 mddev->minor_version = 90;
4006 return len;
4007 }
4008 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004009 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004010 if (namelen >= sizeof(mddev->metadata_type))
4011 namelen = sizeof(mddev->metadata_type)-1;
4012 strncpy(mddev->metadata_type, buf+9, namelen);
4013 mddev->metadata_type[namelen] = 0;
4014 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4015 mddev->metadata_type[--namelen] = 0;
4016 mddev->persistent = 0;
4017 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004018 mddev->major_version = 0;
4019 mddev->minor_version = 90;
4020 return len;
4021 }
4022 major = simple_strtoul(buf, &e, 10);
4023 if (e==buf || *e != '.')
4024 return -EINVAL;
4025 buf = e+1;
4026 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004027 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004028 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004029 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004030 return -ENOENT;
4031 mddev->major_version = major;
4032 mddev->minor_version = minor;
4033 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004034 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004035 return len;
4036}
4037
4038static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004039__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004040
NeilBrowna35b0d62006-01-06 00:20:49 -08004041static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004042action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004043{
NeilBrown7eec3142005-11-08 21:39:44 -08004044 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004045 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4046 type = "frozen";
4047 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004048 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004049 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4050 type = "reshape";
4051 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004052 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4053 type = "resync";
4054 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4055 type = "check";
4056 else
4057 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004058 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004059 type = "recover";
4060 }
4061 return sprintf(page, "%s\n", type);
4062}
4063
4064static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004065action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004066{
NeilBrown7eec3142005-11-08 21:39:44 -08004067 if (!mddev->pers || !mddev->pers->sync_request)
4068 return -EINVAL;
4069
NeilBrownb6a9ce62009-05-26 09:41:17 +10004070 if (cmd_match(page, "frozen"))
4071 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4072 else
4073 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4074
4075 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrownf851b602014-12-11 10:02:10 +11004076 flush_workqueue(md_misc_wq);
NeilBrown7eec3142005-11-08 21:39:44 -08004077 if (mddev->sync_thread) {
4078 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10004079 md_reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004080 }
NeilBrown03c902e2006-01-06 00:20:46 -08004081 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4082 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004083 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004084 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004085 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004086 else if (cmd_match(page, "recover")) {
4087 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4088 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4089 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004090 int err;
4091 if (mddev->pers->start_reshape == NULL)
4092 return -EINVAL;
4093 err = mddev->pers->start_reshape(mddev);
4094 if (err)
4095 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004096 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004097 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004098 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004099 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004100 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004101 return -EINVAL;
4102 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4103 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004104 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004105 if (mddev->ro == 2) {
4106 /* A write to sync_action is enough to justify
4107 * canceling read-auto mode
4108 */
4109 mddev->ro = 0;
4110 md_wakeup_thread(mddev->sync_thread);
4111 }
NeilBrown03c902e2006-01-06 00:20:46 -08004112 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004113 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004114 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004115 return len;
4116}
4117
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004118static struct md_sysfs_entry md_scan_mode =
4119__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4120
4121static ssize_t
4122last_sync_action_show(struct mddev *mddev, char *page)
4123{
4124 return sprintf(page, "%s\n", mddev->last_sync_action);
4125}
4126
4127static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4128
NeilBrown9d888832005-11-08 21:39:26 -08004129static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004130mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004131{
4132 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004133 (unsigned long long)
4134 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004135}
4136
NeilBrown80ca3a42006-07-10 04:44:18 -07004137static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004138
NeilBrown88202a02006-01-06 00:21:36 -08004139static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004140sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004141{
4142 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4143 mddev->sync_speed_min ? "local": "system");
4144}
4145
4146static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004147sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004148{
4149 int min;
4150 char *e;
4151 if (strncmp(buf, "system", 6)==0) {
4152 mddev->sync_speed_min = 0;
4153 return len;
4154 }
4155 min = simple_strtoul(buf, &e, 10);
4156 if (buf == e || (*e && *e != '\n') || min <= 0)
4157 return -EINVAL;
4158 mddev->sync_speed_min = min;
4159 return len;
4160}
4161
4162static struct md_sysfs_entry md_sync_min =
4163__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4164
4165static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004166sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004167{
4168 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4169 mddev->sync_speed_max ? "local": "system");
4170}
4171
4172static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004173sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004174{
4175 int max;
4176 char *e;
4177 if (strncmp(buf, "system", 6)==0) {
4178 mddev->sync_speed_max = 0;
4179 return len;
4180 }
4181 max = simple_strtoul(buf, &e, 10);
4182 if (buf == e || (*e && *e != '\n') || max <= 0)
4183 return -EINVAL;
4184 mddev->sync_speed_max = max;
4185 return len;
4186}
4187
4188static struct md_sysfs_entry md_sync_max =
4189__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4190
Iustin Popd7f3d292007-10-16 23:30:54 -07004191static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004192degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004193{
4194 return sprintf(page, "%d\n", mddev->degraded);
4195}
4196static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004197
4198static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004199sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004200{
4201 return sprintf(page, "%d\n", mddev->parallel_resync);
4202}
4203
4204static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004205sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004206{
4207 long n;
4208
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004209 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004210 return -EINVAL;
4211
4212 if (n != 0 && n != 1)
4213 return -EINVAL;
4214
4215 mddev->parallel_resync = n;
4216
4217 if (mddev->sync_thread)
4218 wake_up(&resync_wait);
4219
4220 return len;
4221}
4222
4223/* force parallel resync, even with shared block devices */
4224static struct md_sysfs_entry md_sync_force_parallel =
4225__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4226 sync_force_parallel_show, sync_force_parallel_store);
4227
4228static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004229sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004230{
4231 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004232 if (mddev->curr_resync == 0)
4233 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004234 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4235 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004236 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004237 db = resync - mddev->resync_mark_cnt;
4238 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004239}
4240
NeilBrown80ca3a42006-07-10 04:44:18 -07004241static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004242
4243static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004244sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004245{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004246 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004247
NeilBrownacb180b2009-04-14 16:28:34 +10004248 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4249 return sprintf(page, "none\n");
4250
NeilBrown72f36d52012-10-11 14:25:57 +11004251 if (mddev->curr_resync == 1 ||
4252 mddev->curr_resync == 2)
4253 return sprintf(page, "delayed\n");
4254
NeilBrownc804cde2012-05-21 09:28:33 +10004255 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4256 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004257 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004258 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004259 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004260
NeilBrownacb180b2009-04-14 16:28:34 +10004261 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004262 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004263}
4264
NeilBrown80ca3a42006-07-10 04:44:18 -07004265static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004266
NeilBrowne464eaf2006-03-27 01:18:14 -08004267static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004268min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004269{
4270 return sprintf(page, "%llu\n",
4271 (unsigned long long)mddev->resync_min);
4272}
4273static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004274min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004275{
4276 unsigned long long min;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004277 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004278 return -EINVAL;
4279 if (min > mddev->resync_max)
4280 return -EINVAL;
4281 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4282 return -EBUSY;
4283
4284 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004285 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004286 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004287 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004288 return -EINVAL;
4289 }
4290 mddev->resync_min = min;
4291
4292 return len;
4293}
4294
4295static struct md_sysfs_entry md_min_sync =
4296__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4297
4298static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004299max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004300{
4301 if (mddev->resync_max == MaxSector)
4302 return sprintf(page, "max\n");
4303 else
4304 return sprintf(page, "%llu\n",
4305 (unsigned long long)mddev->resync_max);
4306}
4307static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004308max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004309{
4310 if (strncmp(buf, "max", 3) == 0)
4311 mddev->resync_max = MaxSector;
4312 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004313 unsigned long long max;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004314 if (kstrtoull(buf, 10, &max))
Neil Brown5e96ee62008-06-28 08:31:24 +10004315 return -EINVAL;
4316 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004317 return -EINVAL;
4318 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004319 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004320 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4321 return -EBUSY;
4322
4323 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004324 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004325 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004326 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004327 return -EINVAL;
4328 }
4329 mddev->resync_max = max;
4330 }
4331 wake_up(&mddev->recovery_wait);
4332 return len;
4333}
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
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004575 rv = mddev_lock(mddev);
4576 if (!rv) {
4577 rv = entry->show(mddev, page);
4578 mddev_unlock(mddev);
4579 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004580 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004581 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004582}
4583
4584static ssize_t
4585md_attr_store(struct kobject *kobj, struct attribute *attr,
4586 const char *page, size_t length)
4587{
4588 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004589 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004590 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004591
4592 if (!entry->store)
4593 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004594 if (!capable(CAP_SYS_ADMIN))
4595 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004596 spin_lock(&all_mddevs_lock);
4597 if (list_empty(&mddev->all_mddevs)) {
4598 spin_unlock(&all_mddevs_lock);
4599 return -EBUSY;
4600 }
4601 mddev_get(mddev);
4602 spin_unlock(&all_mddevs_lock);
NeilBrowna7a3f082012-12-11 13:35:54 +11004603 if (entry->store == new_dev_store)
4604 flush_workqueue(md_misc_wq);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004605 rv = mddev_lock(mddev);
4606 if (!rv) {
4607 rv = entry->store(mddev, page, length);
4608 mddev_unlock(mddev);
4609 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004610 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004611 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004612}
4613
4614static void md_free(struct kobject *ko)
4615{
NeilBrownfd01b882011-10-11 16:47:53 +11004616 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004617
4618 if (mddev->sysfs_state)
4619 sysfs_put(mddev->sysfs_state);
4620
4621 if (mddev->gendisk) {
4622 del_gendisk(mddev->gendisk);
4623 put_disk(mddev->gendisk);
4624 }
4625 if (mddev->queue)
4626 blk_cleanup_queue(mddev->queue);
4627
NeilBrowneae17012005-11-08 21:39:23 -08004628 kfree(mddev);
4629}
4630
Emese Revfy52cf25d2010-01-19 02:58:23 +01004631static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004632 .show = md_attr_show,
4633 .store = md_attr_store,
4634};
4635static struct kobj_type md_ktype = {
4636 .release = md_free,
4637 .sysfs_ops = &md_sysfs_ops,
4638 .default_attrs = md_default_attrs,
4639};
4640
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641int mdp_major = 0;
4642
Dan Williams5fd3a172009-03-04 00:57:25 -07004643static void mddev_delayed_delete(struct work_struct *ws)
4644{
NeilBrownfd01b882011-10-11 16:47:53 +11004645 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004646
NeilBrown43a70502009-12-14 12:49:55 +11004647 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004648 kobject_del(&mddev->kobj);
4649 kobject_put(&mddev->kobj);
4650}
4651
NeilBrownefeb53c2009-01-09 08:31:10 +11004652static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004654 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004655 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004657 int partitioned;
4658 int shift;
4659 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004660 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
4662 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004663 return -ENODEV;
4664
4665 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4666 shift = partitioned ? MdpMinorShift : 0;
4667 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
Tejun Heoe804ac72010-10-15 15:36:08 +02004669 /* wait for any previous instance of this device to be
4670 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004671 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004672 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004673
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004674 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004675 error = -EEXIST;
4676 if (mddev->gendisk)
4677 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004678
4679 if (name) {
4680 /* Need to ensure that 'name' is not a duplicate.
4681 */
NeilBrownfd01b882011-10-11 16:47:53 +11004682 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004683 spin_lock(&all_mddevs_lock);
4684
4685 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4686 if (mddev2->gendisk &&
4687 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4688 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004689 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004690 }
4691 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 }
NeilBrown8b765392009-01-09 08:31:08 +11004693
NeilBrown0909dc42009-07-01 12:27:21 +10004694 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004695 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004696 if (!mddev->queue)
4697 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004698 mddev->queue->queuedata = mddev;
4699
NeilBrown409c57f2009-03-31 14:39:39 +11004700 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004701 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004702
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 disk = alloc_disk(1 << shift);
4704 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004705 blk_cleanup_queue(mddev->queue);
4706 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004707 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004709 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004711 if (name)
4712 strcpy(disk->disk_name, name);
4713 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004715 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 disk->fops = &md_fops;
4718 disk->private_data = mddev;
4719 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004720 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004721 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004722 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004723 * remove it now.
4724 */
4725 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004727 /* As soon as we call add_disk(), another thread could get
4728 * through to md_open, so make sure it doesn't get too far
4729 */
4730 mutex_lock(&mddev->open_mutex);
4731 add_disk(disk);
4732
Tejun Heoed9e1982008-08-25 19:56:05 +09004733 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4734 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004735 if (error) {
4736 /* This isn't possible, but as kobject_init_and_add is marked
4737 * __must_check, we must do something with the result
4738 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004739 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4740 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004741 error = 0;
4742 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004743 if (mddev->kobj.sd &&
4744 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004745 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004746 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004747 abort:
4748 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004749 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004750 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004751 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004752 }
NeilBrownd3374822009-01-09 08:31:10 +11004753 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004754 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004755}
4756
4757static struct kobject *md_probe(dev_t dev, int *part, void *data)
4758{
4759 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 return NULL;
4761}
4762
NeilBrownefeb53c2009-01-09 08:31:10 +11004763static int add_named_array(const char *val, struct kernel_param *kp)
4764{
4765 /* val must be "md_*" where * is not all digits.
4766 * We allocate an array with a large free minor number, and
4767 * set the name to val. val must not already be an active name.
4768 */
4769 int len = strlen(val);
4770 char buf[DISK_NAME_LEN];
4771
4772 while (len && val[len-1] == '\n')
4773 len--;
4774 if (len >= DISK_NAME_LEN)
4775 return -E2BIG;
4776 strlcpy(buf, val, len+1);
4777 if (strncmp(buf, "md_", 3) != 0)
4778 return -EINVAL;
4779 return md_alloc(0, buf);
4780}
4781
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782static void md_safemode_timeout(unsigned long data)
4783{
NeilBrownfd01b882011-10-11 16:47:53 +11004784 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
Neil Brown0fd62b82008-06-28 08:31:36 +10004786 if (!atomic_read(&mddev->writes_pending)) {
4787 mddev->safemode = 1;
4788 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004789 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 md_wakeup_thread(mddev->thread);
4792}
4793
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004794static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
NeilBrownfd01b882011-10-11 16:47:53 +11004796int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
NeilBrown2604b702006-01-06 00:20:36 -08004798 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004799 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004800 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
NeilBrowna757e642005-04-16 15:26:42 -07004802 if (list_empty(&mddev->disks))
4803 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
4806 if (mddev->pers)
4807 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004808 /* Cannot run until previous stop completes properly */
4809 if (mddev->sysfs_active)
4810 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004811
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 /*
4813 * Analyze all RAID superblock(s)
4814 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004815 if (!mddev->raid_disks) {
4816 if (!mddev->persistent)
4817 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004818 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
NeilBrownd9d166c2006-01-06 00:20:51 -08004821 if (mddev->level != LEVEL_NONE)
4822 request_module("md-level-%d", mddev->level);
4823 else if (mddev->clevel[0])
4824 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
4826 /*
4827 * Drop all container device buffers, from now on
4828 * the only valid external interface is through the md
4829 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004831 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004832 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 continue;
4834 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004835 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004836
4837 /* perform some consistency tests on the device.
4838 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004839 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004840 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004841 if (rdev->meta_bdev) {
4842 /* Nothing to check */;
4843 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004844 if (mddev->dev_sectors &&
4845 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004846 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004847 printk("md: %s: data overlaps metadata\n",
4848 mdname(mddev));
4849 return -EINVAL;
4850 }
4851 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004852 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004853 > rdev->data_offset) {
4854 printk("md: %s: metadata overlaps data\n",
4855 mdname(mddev));
4856 return -EINVAL;
4857 }
4858 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004859 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 }
4861
NeilBrowna167f662010-10-26 18:31:13 +11004862 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07004863 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11004864
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004866 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004867 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004869 if (mddev->level != LEVEL_NONE)
4870 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4871 mddev->level);
4872 else
4873 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4874 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 return -EINVAL;
4876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004878 if (mddev->level != pers->level) {
4879 mddev->level = pers->level;
4880 mddev->new_level = pers->level;
4881 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004882 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
NeilBrownf6705572006-03-27 01:18:11 -08004884 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004885 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004886 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08004887 module_put(pers->owner);
4888 return -EINVAL;
4889 }
4890
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004891 if (pers->sync_request) {
4892 /* Warn if this is a potentially silly
4893 * configuration.
4894 */
4895 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11004896 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004897 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004898
NeilBrowndafb20f2012-03-19 12:46:39 +11004899 rdev_for_each(rdev, mddev)
4900 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004901 if (rdev < rdev2 &&
4902 rdev->bdev->bd_contains ==
4903 rdev2->bdev->bd_contains) {
4904 printk(KERN_WARNING
4905 "%s: WARNING: %s appears to be"
4906 " on the same physical disk as"
4907 " %s.\n",
4908 mdname(mddev),
4909 bdevname(rdev->bdev,b),
4910 bdevname(rdev2->bdev,b2));
4911 warned = 1;
4912 }
4913 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004914
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004915 if (warned)
4916 printk(KERN_WARNING
4917 "True protection against single-disk"
4918 " failure might be compromised.\n");
4919 }
4920
NeilBrown657390d2005-08-26 18:34:16 -07004921 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004922 /* may be over-ridden by personality */
4923 mddev->resync_max_sectors = mddev->dev_sectors;
4924
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004925 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
NeilBrown0f9552b52009-12-30 12:08:50 +11004927 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004928 mddev->ro = 2; /* read-only, but switch on first write */
4929
NeilBrown36d091f2014-12-15 12:56:58 +11004930 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004931 if (err)
4932 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11004933 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11004934 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4935 " but 'external_size' not in effect?\n", __func__);
4936 printk(KERN_ERR
4937 "md: invalid array_size %llu > default size %llu\n",
4938 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11004939 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11004940 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11004941 }
NeilBrown36d091f2014-12-15 12:56:58 +11004942 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10004943 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004944 err = bitmap_create(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11004945 if (err)
NeilBrownb15c2e52006-01-06 00:20:16 -08004946 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4947 mdname(mddev), err);
NeilBrownb15c2e52006-01-06 00:20:16 -08004948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11004950 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11004951 pers->free(mddev, mddev->private);
4952 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07004953 bitmap_destroy(mddev);
4954 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 }
NeilBrown5c675f82014-12-15 12:56:56 +11004956 if (mddev->queue) {
4957 mddev->queue->backing_dev_info.congested_data = mddev;
4958 mddev->queue->backing_dev_info.congested_fn = md_congested;
NeilBrown64590f42014-12-15 12:56:57 +11004959 blk_queue_merge_bvec(mddev->queue, md_mergeable_bvec);
NeilBrown5c675f82014-12-15 12:56:56 +11004960 }
NeilBrown36d091f2014-12-15 12:56:58 +11004961 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004962 if (mddev->kobj.sd &&
4963 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004964 printk(KERN_WARNING
4965 "md: cannot register extra attributes for %s\n",
4966 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004967 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004968 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004969 mddev->ro = 0;
4970
NeilBrownf72ffdd2014-09-30 14:23:59 +10004971 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004972 atomic_set(&mddev->max_corr_read_errors,
4973 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 mddev->safemode = 0;
4975 mddev->safemode_timer.function = md_safemode_timeout;
4976 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004977 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004979 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11004980 spin_lock(&mddev->lock);
4981 mddev->pers = pers;
NeilBrown0ca69882011-01-14 09:14:33 +11004982 mddev->ready = 1;
NeilBrown36d091f2014-12-15 12:56:58 +11004983 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11004984 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10004985 if (rdev->raid_disk >= 0)
4986 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004987 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10004988
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10004990
NeilBrown7a0a5352013-08-27 16:28:23 +10004991 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07004992 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
NeilBrownd7603b72006-01-06 00:20:30 -08004994 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004995 sysfs_notify_dirent_safe(mddev->sysfs_state);
4996 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004997 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 return 0;
4999}
NeilBrown390ee602010-06-01 19:37:27 +10005000EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
NeilBrownfd01b882011-10-11 16:47:53 +11005002static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005003{
5004 int err;
5005
5006 err = md_run(mddev);
5007 if (err)
5008 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005009 err = bitmap_load(mddev);
5010 if (err) {
5011 bitmap_destroy(mddev);
5012 goto out;
5013 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005014
5015 md_wakeup_thread(mddev->thread);
5016 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5017
NeilBrownfe60b012010-03-29 11:10:42 +11005018 set_capacity(mddev->gendisk, mddev->array_sectors);
5019 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005020 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005021 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5022out:
5023 return err;
5024}
5025
NeilBrownfd01b882011-10-11 16:47:53 +11005026static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027{
5028 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
Andre Noll80fab1d2008-07-11 22:02:21 +10005030 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005032 return -ENXIO;
5033 if (!mddev->pers)
5034 return -EINVAL;
5035 if (!mddev->ro)
5036 return -EBUSY;
5037 mddev->safemode = 0;
5038 mddev->ro = 0;
5039 set_disk_ro(disk, 0);
5040 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5041 mdname(mddev));
5042 /* Kick recovery or resync if necessary */
5043 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5044 md_wakeup_thread(mddev->thread);
5045 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005046 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048}
5049
NeilBrownfd01b882011-10-11 16:47:53 +11005050static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005051{
5052 mddev->array_sectors = 0;
5053 mddev->external_size = 0;
5054 mddev->dev_sectors = 0;
5055 mddev->raid_disks = 0;
5056 mddev->recovery_cp = 0;
5057 mddev->resync_min = 0;
5058 mddev->resync_max = MaxSector;
5059 mddev->reshape_position = MaxSector;
5060 mddev->external = 0;
5061 mddev->persistent = 0;
5062 mddev->level = LEVEL_NONE;
5063 mddev->clevel[0] = 0;
5064 mddev->flags = 0;
5065 mddev->ro = 0;
5066 mddev->metadata_type[0] = 0;
5067 mddev->chunk_sectors = 0;
5068 mddev->ctime = mddev->utime = 0;
5069 mddev->layout = 0;
5070 mddev->max_disks = 0;
5071 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005072 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005073 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005074 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005075 mddev->new_level = LEVEL_NONE;
5076 mddev->new_layout = 0;
5077 mddev->new_chunk_sectors = 0;
5078 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005079 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005080 mddev->suspend_lo = mddev->suspend_hi = 0;
5081 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5082 mddev->recovery = 0;
5083 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005084 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005085 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005086 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005087 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005088 mddev->bitmap_info.offset = 0;
5089 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005090 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005091 mddev->bitmap_info.chunksize = 0;
5092 mddev->bitmap_info.daemon_sleep = 0;
5093 mddev->bitmap_info.max_write_behind = 0;
5094}
5095
NeilBrownfd01b882011-10-11 16:47:53 +11005096static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005097{
NeilBrown6b6204e2013-05-09 09:48:30 +10005098 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005099 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005100 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005101 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005102 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005103 }
5104
5105 del_timer_sync(&mddev->safemode_timer);
5106
5107 bitmap_flush(mddev);
5108 md_super_wait(mddev);
5109
NeilBrownb6d428c2013-04-24 11:42:42 +10005110 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005111 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005112 /* mark array as shutdown cleanly */
5113 mddev->in_sync = 1;
5114 md_update_sb(mddev, 1);
5115 }
5116}
NeilBrowndefad612011-01-14 09:14:33 +11005117
NeilBrownfd01b882011-10-11 16:47:53 +11005118void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005119{
NeilBrown29f097c2013-11-14 17:54:51 +11005120 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005121 __md_stop_writes(mddev);
5122 mddev_unlock(mddev);
5123}
NeilBrown390ee602010-06-01 19:37:27 +10005124EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005125
NeilBrown5aa61f42014-12-15 12:56:57 +11005126static void mddev_detach(struct mddev *mddev)
5127{
5128 struct bitmap *bitmap = mddev->bitmap;
5129 /* wait for behind writes to complete */
5130 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5131 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5132 mdname(mddev));
5133 /* need to kick something here to make sure I/O goes? */
5134 wait_event(bitmap->behind_wait,
5135 atomic_read(&bitmap->behind_writes) == 0);
5136 }
NeilBrown36d091f2014-12-15 12:56:58 +11005137 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005138 mddev->pers->quiesce(mddev, 1);
5139 mddev->pers->quiesce(mddev, 0);
5140 }
5141 md_unregister_thread(&mddev->thread);
5142 if (mddev->queue)
5143 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5144}
5145
NeilBrown5eff3c42012-11-19 10:47:48 +11005146static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005147{
NeilBrown36d091f2014-12-15 12:56:58 +11005148 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005149 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005150 spin_lock(&mddev->lock);
5151 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005152 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005153 spin_unlock(&mddev->lock);
5154 pers->free(mddev, mddev->private);
5155 if (pers->sync_request && mddev->to_remove == NULL)
5156 mddev->to_remove = &md_redundancy_group;
5157 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005158 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005159}
NeilBrown5eff3c42012-11-19 10:47:48 +11005160
5161void md_stop(struct mddev *mddev)
5162{
5163 /* stop the array and free an attached data structures.
5164 * This is called from dm-raid
5165 */
5166 __md_stop(mddev);
5167 bitmap_destroy(mddev);
5168 if (mddev->bio_set)
5169 bioset_free(mddev->bio_set);
5170}
5171
NeilBrown390ee602010-06-01 19:37:27 +10005172EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005173
NeilBrowna05b7ea2012-07-19 15:59:18 +10005174static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005175{
5176 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005177 int did_freeze = 0;
5178
5179 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5180 did_freeze = 1;
5181 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5182 md_wakeup_thread(mddev->thread);
5183 }
NeilBrownf851b602014-12-11 10:02:10 +11005184 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005185 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005186 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005187 /* Thread might be blocked waiting for metadata update
5188 * which will now never happen */
5189 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005190
NeilBrown30b8feb2013-11-14 15:16:17 +11005191 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005192 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5193 &mddev->recovery));
NeilBrown30b8feb2013-11-14 15:16:17 +11005194 mddev_lock_nointr(mddev);
5195
NeilBrowna4bd82d2010-03-29 13:23:10 +11005196 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005197 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005198 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005199 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005200 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005201 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005202 if (did_freeze) {
5203 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005204 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005205 md_wakeup_thread(mddev->thread);
5206 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005207 err = -EBUSY;
5208 goto out;
5209 }
5210 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005211 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005212
5213 err = -ENXIO;
5214 if (mddev->ro==1)
5215 goto out;
5216 mddev->ro = 1;
5217 set_disk_ro(mddev->gendisk, 1);
5218 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005219 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5220 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005221 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005222 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005223 }
5224out:
5225 mutex_unlock(&mddev->open_mutex);
5226 return err;
5227}
5228
NeilBrown9e653b62006-06-26 00:27:58 -07005229/* mode:
5230 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005231 * 2 - stop but do not disassemble array
5232 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005233static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005234 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005237 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005238 int did_freeze = 0;
5239
5240 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5241 did_freeze = 1;
5242 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5243 md_wakeup_thread(mddev->thread);
5244 }
NeilBrownf851b602014-12-11 10:02:10 +11005245 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005246 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005247 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005248 /* Thread might be blocked waiting for metadata update
5249 * which will now never happen */
5250 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005251
NeilBrown30b8feb2013-11-14 15:16:17 +11005252 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005253 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5254 !test_bit(MD_RECOVERY_RUNNING,
5255 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005256 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
NeilBrownc8c00a62009-08-10 12:50:52 +10005258 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005259 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005260 mddev->sysfs_active ||
5261 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005262 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005263 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005264 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005265 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005266 if (did_freeze) {
5267 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005268 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005269 md_wakeup_thread(mddev->thread);
5270 }
NeilBrown260fa032013-08-27 16:44:13 +10005271 return -EBUSY;
5272 }
NeilBrown6e17b022010-08-07 21:41:19 +10005273 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005274 if (mddev->ro)
5275 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005276
NeilBrowndefad612011-01-14 09:14:33 +11005277 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005278 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005279 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005280 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005281
NeilBrowna4bd82d2010-03-29 13:23:10 +11005282 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005283 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005284
NeilBrowndafb20f2012-03-19 12:46:39 +11005285 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005286 if (rdev->raid_disk >= 0)
5287 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005288
NeilBrowna4bd82d2010-03-29 13:23:10 +11005289 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005290 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005291 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005292 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005293
NeilBrowna4bd82d2010-03-29 13:23:10 +11005294 if (mddev->ro)
5295 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005296 } else
5297 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 /*
5299 * Free resources if final stop
5300 */
NeilBrown9e653b62006-06-26 00:27:58 -07005301 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5303
NeilBrown978f9462006-02-02 14:28:05 -08005304 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005305 if (mddev->bitmap_info.file) {
NeilBrownc3d97142009-12-14 12:49:52 +11005306 fput(mddev->bitmap_info.file);
5307 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005308 }
NeilBrownc3d97142009-12-14 12:49:52 +11005309 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005310
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 export_array(mddev);
5312
NeilBrown6177b472010-03-29 11:37:13 +11005313 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005314 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005315 if (mddev->hold_active == UNTIL_STOP)
5316 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005317 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005318 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005319 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005320 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322}
5323
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005324#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005325static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326{
NeilBrown3cb03002011-10-11 16:45:26 +11005327 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 int err;
5329
NeilBrowna757e642005-04-16 15:26:42 -07005330 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332
5333 printk(KERN_INFO "md: running: ");
5334
NeilBrowndafb20f2012-03-19 12:46:39 +11005335 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 char b[BDEVNAME_SIZE];
5337 printk("<%s>", bdevname(rdev->bdev,b));
5338 }
5339 printk("\n");
5340
NeilBrownd710e132008-10-13 11:55:12 +11005341 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 if (err) {
5343 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005344 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 }
5346}
5347
5348/*
5349 * lets try to run arrays based on all disks that have arrived
5350 * until now. (those are in pending_raid_disks)
5351 *
5352 * the method: pick the first pending disk, collect all disks with
5353 * the same UUID, remove all from the pending list and put them into
5354 * the 'same_array' list. Then order this list based on superblock
5355 * update time (freshest comes first), kick out 'old' disks and
5356 * compare superblocks. If everything's fine then run it.
5357 *
5358 * If "unit" is allocated, then bump its reference count
5359 */
5360static void autorun_devices(int part)
5361{
NeilBrown3cb03002011-10-11 16:45:26 +11005362 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005363 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 char b[BDEVNAME_SIZE];
5365
5366 printk(KERN_INFO "md: autorun ...\n");
5367 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005368 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005370 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005372 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373
5374 printk(KERN_INFO "md: considering %s ...\n",
5375 bdevname(rdev0->bdev,b));
5376 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005377 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 if (super_90_load(rdev, rdev0, 0) >= 0) {
5379 printk(KERN_INFO "md: adding %s ...\n",
5380 bdevname(rdev->bdev,b));
5381 list_move(&rdev->same_set, &candidates);
5382 }
5383 /*
5384 * now we have a set of devices, with all of them having
5385 * mostly sane superblocks. It's time to allocate the
5386 * mddev.
5387 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005388 if (part) {
5389 dev = MKDEV(mdp_major,
5390 rdev0->preferred_minor << MdpMinorShift);
5391 unit = MINOR(dev) >> MdpMinorShift;
5392 } else {
5393 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5394 unit = MINOR(dev);
5395 }
5396 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5398 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5399 break;
5400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
5402 md_probe(dev, NULL, NULL);
5403 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005404 if (!mddev || !mddev->gendisk) {
5405 if (mddev)
5406 mddev_put(mddev);
5407 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 "md: cannot allocate memory for md drive.\n");
5409 break;
5410 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005411 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 printk(KERN_WARNING "md: %s locked, cannot run\n",
5413 mdname(mddev));
5414 else if (mddev->raid_disks || mddev->major_version
5415 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005416 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 "md: %s already running, cannot run %s\n",
5418 mdname(mddev), bdevname(rdev0->bdev,b));
5419 mddev_unlock(mddev);
5420 } else {
5421 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005422 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005423 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 list_del_init(&rdev->same_set);
5425 if (bind_rdev_to_array(rdev, mddev))
5426 export_rdev(rdev);
5427 }
5428 autorun_array(mddev);
5429 mddev_unlock(mddev);
5430 }
5431 /* on success, candidates will be empty, on error
5432 * it won't...
5433 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005434 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005435 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 mddev_put(mddev);
5439 }
5440 printk(KERN_INFO "md: ... autorun DONE.\n");
5441}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005442#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443
NeilBrownf72ffdd2014-09-30 14:23:59 +10005444static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445{
5446 mdu_version_t ver;
5447
5448 ver.major = MD_MAJOR_VERSION;
5449 ver.minor = MD_MINOR_VERSION;
5450 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5451
5452 if (copy_to_user(arg, &ver, sizeof(ver)))
5453 return -EFAULT;
5454
5455 return 0;
5456}
5457
NeilBrownf72ffdd2014-09-30 14:23:59 +10005458static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459{
5460 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005461 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005462 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463
NeilBrown1ca69c42012-10-11 13:37:33 +11005464 nr = working = insync = failed = spare = 0;
5465 rcu_read_lock();
5466 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005468 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 failed++;
5470 else {
5471 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005472 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005473 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 else
5475 spare++;
5476 }
5477 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005478 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479
5480 info.major_version = mddev->major_version;
5481 info.minor_version = mddev->minor_version;
5482 info.patch_version = MD_PATCHLEVEL_VERSION;
5483 info.ctime = mddev->ctime;
5484 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005485 info.size = mddev->dev_sectors / 2;
5486 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005487 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 info.nr_disks = nr;
5489 info.raid_disks = mddev->raid_disks;
5490 info.md_minor = mddev->md_minor;
5491 info.not_persistent= !mddev->persistent;
5492
5493 info.utime = mddev->utime;
5494 info.state = 0;
5495 if (mddev->in_sync)
5496 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005497 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005498 info.state |= (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005499 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 info.working_disks = working;
5501 info.failed_disks = failed;
5502 info.spare_disks = spare;
5503
5504 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005505 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506
5507 if (copy_to_user(arg, &info, sizeof(info)))
5508 return -EFAULT;
5509
5510 return 0;
5511}
5512
NeilBrownf72ffdd2014-09-30 14:23:59 +10005513static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005514{
5515 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5516 char *ptr, *buf = NULL;
5517 int err = -ENOMEM;
5518
NeilBrown60559da2013-07-16 16:44:29 +10005519 file = kmalloc(sizeof(*file), GFP_NOIO);
NeilBrown2a2275d2007-01-26 00:57:11 -08005520
NeilBrown32a76272005-06-21 17:17:14 -07005521 if (!file)
5522 goto out;
5523
5524 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005525 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005526 file->pathname[0] = '\0';
5527 goto copy_out;
5528 }
5529
5530 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5531 if (!buf)
5532 goto out;
5533
NeilBrown1ec885c2012-05-22 13:55:10 +10005534 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5535 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005536 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005537 goto out;
5538
5539 strcpy(file->pathname, ptr);
5540
5541copy_out:
5542 err = 0;
5543 if (copy_to_user(arg, file, sizeof(*file)))
5544 err = -EFAULT;
5545out:
5546 kfree(buf);
5547 kfree(file);
5548 return err;
5549}
5550
NeilBrownf72ffdd2014-09-30 14:23:59 +10005551static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552{
5553 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005554 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
5556 if (copy_from_user(&info, arg, sizeof(info)))
5557 return -EFAULT;
5558
NeilBrown1ca69c42012-10-11 13:37:33 +11005559 rcu_read_lock();
5560 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 if (rdev) {
5562 info.major = MAJOR(rdev->bdev->bd_dev);
5563 info.minor = MINOR(rdev->bdev->bd_dev);
5564 info.raid_disk = rdev->raid_disk;
5565 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005566 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005568 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 info.state |= (1<<MD_DISK_ACTIVE);
5570 info.state |= (1<<MD_DISK_SYNC);
5571 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005572 if (test_bit(WriteMostly, &rdev->flags))
5573 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 } else {
5575 info.major = info.minor = 0;
5576 info.raid_disk = -1;
5577 info.state = (1<<MD_DISK_REMOVED);
5578 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005579 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580
5581 if (copy_to_user(arg, &info, sizeof(info)))
5582 return -EFAULT;
5583
5584 return 0;
5585}
5586
NeilBrownf72ffdd2014-09-30 14:23:59 +10005587static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588{
5589 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005590 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 dev_t dev = MKDEV(info->major,info->minor);
5592
5593 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5594 return -EOVERFLOW;
5595
5596 if (!mddev->raid_disks) {
5597 int err;
5598 /* expecting a device which has a superblock */
5599 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5600 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005601 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 "md: md_import_device returned %ld\n",
5603 PTR_ERR(rdev));
5604 return PTR_ERR(rdev);
5605 }
5606 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005607 struct md_rdev *rdev0
5608 = list_entry(mddev->disks.next,
5609 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005610 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 .load_super(rdev, rdev0, mddev->minor_version);
5612 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005613 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005615 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 bdevname(rdev0->bdev,b2));
5617 export_rdev(rdev);
5618 return -EINVAL;
5619 }
5620 }
5621 err = bind_rdev_to_array(rdev, mddev);
5622 if (err)
5623 export_rdev(rdev);
5624 return err;
5625 }
5626
5627 /*
5628 * add_new_disk can be used once the array is assembled
5629 * to add "hot spares". They must already have a superblock
5630 * written
5631 */
5632 if (mddev->pers) {
5633 int err;
5634 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005635 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 "%s: personality does not support diskops!\n",
5637 mdname(mddev));
5638 return -EINVAL;
5639 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005640 if (mddev->persistent)
5641 rdev = md_import_device(dev, mddev->major_version,
5642 mddev->minor_version);
5643 else
5644 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005646 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 "md: md_import_device returned %ld\n",
5648 PTR_ERR(rdev));
5649 return PTR_ERR(rdev);
5650 }
NeilBrown1a855a02010-12-09 16:36:28 +11005651 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005652 if (!mddev->persistent) {
5653 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005654 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005655 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005656 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005657 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005658 } else
NeilBrown41158c72005-06-21 17:17:25 -07005659 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005660 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005661 } else
5662 super_types[mddev->major_version].
5663 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005664 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005665 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005666 /* This was a hot-add request, but events doesn't
5667 * match, so reject it.
5668 */
5669 export_rdev(rdev);
5670 return -EINVAL;
5671 }
5672
NeilBrownb2d444d2005-11-08 21:39:31 -08005673 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005674 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5675 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005676 else
5677 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005678
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 rdev->raid_disk = -1;
5680 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005681 if (!err && !mddev->pers->hot_remove_disk) {
5682 /* If there is hot_add_disk but no hot_remove_disk
5683 * then added disks for geometry changes,
5684 * and should be added immediately.
5685 */
5686 super_types[mddev->major_version].
5687 validate_super(mddev, rdev);
5688 err = mddev->pers->hot_add_disk(mddev, rdev);
5689 if (err)
5690 unbind_rdev_from_array(rdev);
5691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 if (err)
5693 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005694 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005695 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005696
NeilBrown7ceb17e2013-04-24 11:42:42 +10005697 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005698 if (mddev->degraded)
5699 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005700 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005701 if (!err)
5702 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005703 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 return err;
5705 }
5706
5707 /* otherwise, add_new_disk is only allowed
5708 * for major_version==0 superblocks
5709 */
5710 if (mddev->major_version != 0) {
5711 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5712 mdname(mddev));
5713 return -EINVAL;
5714 }
5715
5716 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5717 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005718 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005720 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 "md: error, md_import_device() returned %ld\n",
5722 PTR_ERR(rdev));
5723 return PTR_ERR(rdev);
5724 }
5725 rdev->desc_nr = info->number;
5726 if (info->raid_disk < mddev->raid_disks)
5727 rdev->raid_disk = info->raid_disk;
5728 else
5729 rdev->raid_disk = -1;
5730
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005732 if (info->state & (1<<MD_DISK_SYNC))
5733 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005735 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5736 set_bit(WriteMostly, &rdev->flags);
5737
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 if (!mddev->persistent) {
5739 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005740 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5741 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005742 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005743 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744
NeilBrown2bf071b2006-01-06 00:20:55 -08005745 err = bind_rdev_to_array(rdev, mddev);
5746 if (err) {
5747 export_rdev(rdev);
5748 return err;
5749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 }
5751
5752 return 0;
5753}
5754
NeilBrownf72ffdd2014-09-30 14:23:59 +10005755static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756{
5757 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005758 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 rdev = find_rdev(mddev, dev);
5761 if (!rdev)
5762 return -ENXIO;
5763
NeilBrown3ea8929d2013-04-24 11:42:41 +10005764 clear_bit(Blocked, &rdev->flags);
5765 remove_and_add_spares(mddev, rdev);
5766
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 if (rdev->raid_disk >= 0)
5768 goto busy;
5769
5770 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005771 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005772 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773
5774 return 0;
5775busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005776 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 bdevname(rdev->bdev,b), mdname(mddev));
5778 return -EBUSY;
5779}
5780
NeilBrownf72ffdd2014-09-30 14:23:59 +10005781static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782{
5783 char b[BDEVNAME_SIZE];
5784 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005785 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786
5787 if (!mddev->pers)
5788 return -ENODEV;
5789
5790 if (mddev->major_version != 0) {
5791 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5792 " version-0 superblocks.\n",
5793 mdname(mddev));
5794 return -EINVAL;
5795 }
5796 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005797 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 "%s: personality does not support diskops!\n",
5799 mdname(mddev));
5800 return -EINVAL;
5801 }
5802
NeilBrownd710e132008-10-13 11:55:12 +11005803 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005805 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 "md: error, md_import_device() returned %ld\n",
5807 PTR_ERR(rdev));
5808 return -EINVAL;
5809 }
5810
5811 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005812 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005814 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815
NeilBrown8190e752009-06-18 08:48:58 +10005816 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
NeilBrownb2d444d2005-11-08 21:39:31 -08005818 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005819 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 "md: can not hot-add faulty %s disk to %s!\n",
5821 bdevname(rdev->bdev,b), mdname(mddev));
5822 err = -EINVAL;
5823 goto abort_export;
5824 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005825 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005827 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005828 err = bind_rdev_to_array(rdev, mddev);
5829 if (err)
5830 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
5832 /*
5833 * The rest should better be atomic, we can have disk failures
5834 * noticed in interrupt contexts ...
5835 */
5836
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 rdev->raid_disk = -1;
5838
NeilBrown850b2b42006-10-03 01:15:46 -07005839 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840
5841 /*
5842 * Kick recovery, maybe this spare has to be added to the
5843 * array immediately.
5844 */
5845 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5846 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005847 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 return 0;
5849
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850abort_export:
5851 export_rdev(rdev);
5852 return err;
5853}
5854
NeilBrownfd01b882011-10-11 16:47:53 +11005855static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005856{
NeilBrown035328c2014-04-09 12:25:40 +10005857 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07005858
NeilBrown36fa3062005-09-09 16:23:45 -07005859 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10005860 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07005861 return -EBUSY;
5862 if (mddev->recovery || mddev->sync_thread)
5863 return -EBUSY;
5864 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005865 }
5866
NeilBrown36fa3062005-09-09 16:23:45 -07005867 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10005868 struct inode *inode;
NeilBrown36fa3062005-09-09 16:23:45 -07005869 if (mddev->bitmap)
5870 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005871 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005872
NeilBrownc3d97142009-12-14 12:49:52 +11005873 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005874 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5875 mdname(mddev));
5876 return -EBADF;
5877 }
5878
NeilBrown035328c2014-04-09 12:25:40 +10005879 inode = mddev->bitmap_info.file->f_mapping->host;
5880 if (!S_ISREG(inode->i_mode)) {
5881 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
5882 mdname(mddev));
5883 err = -EBADF;
5884 } else if (!(mddev->bitmap_info.file->f_mode & FMODE_WRITE)) {
5885 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
5886 mdname(mddev));
5887 err = -EBADF;
5888 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07005889 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5890 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10005891 err = -EBUSY;
5892 }
5893 if (err) {
NeilBrownc3d97142009-12-14 12:49:52 +11005894 fput(mddev->bitmap_info.file);
5895 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005896 return err;
5897 }
NeilBrownc3d97142009-12-14 12:49:52 +11005898 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005899 } else if (mddev->bitmap == NULL)
5900 return -ENOENT; /* cannot remove what isn't there */
5901 err = 0;
5902 if (mddev->pers) {
5903 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005904 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005905 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005906 if (!err)
5907 err = bitmap_load(mddev);
5908 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005909 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005910 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005911 fd = -1; /* make sure to put the file */
5912 }
NeilBrown36fa3062005-09-09 16:23:45 -07005913 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005914 }
5915 if (fd < 0) {
NeilBrown035328c2014-04-09 12:25:40 +10005916 if (mddev->bitmap_info.file)
NeilBrownc3d97142009-12-14 12:49:52 +11005917 fput(mddev->bitmap_info.file);
NeilBrownc3d97142009-12-14 12:49:52 +11005918 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005919 }
5920
NeilBrown32a76272005-06-21 17:17:14 -07005921 return err;
5922}
5923
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924/*
5925 * set_array_info is used two different ways
5926 * The original usage is when creating a new array.
5927 * In this usage, raid_disks is > 0 and it together with
5928 * level, size, not_persistent,layout,chunksize determine the
5929 * shape of the array.
5930 * This will always create an array with a type-0.90.0 superblock.
5931 * The newer usage is when assembling an array.
5932 * In this case raid_disks will be 0, and the major_version field is
5933 * use to determine which style super-blocks are to be found on the devices.
5934 * The minor and patch _version numbers are also kept incase the
5935 * super_block handler wishes to interpret them.
5936 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005937static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938{
5939
5940 if (info->raid_disks == 0) {
5941 /* just setting version number for superblock loading */
5942 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005943 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 super_types[info->major_version].name == NULL) {
5945 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005946 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 "md: superblock version %d not known\n",
5948 info->major_version);
5949 return -EINVAL;
5950 }
5951 mddev->major_version = info->major_version;
5952 mddev->minor_version = info->minor_version;
5953 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005954 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005955 /* ensure mddev_put doesn't delete this now that there
5956 * is some minimal configuration.
5957 */
5958 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 return 0;
5960 }
5961 mddev->major_version = MD_MAJOR_VERSION;
5962 mddev->minor_version = MD_MINOR_VERSION;
5963 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5964 mddev->ctime = get_seconds();
5965
5966 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005967 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005968 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 mddev->raid_disks = info->raid_disks;
5970 /* don't set md_minor, it is determined by which /dev/md* was
5971 * openned
5972 */
5973 if (info->state & (1<<MD_SB_CLEAN))
5974 mddev->recovery_cp = MaxSector;
5975 else
5976 mddev->recovery_cp = 0;
5977 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005978 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979
5980 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005981 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
5983 mddev->max_disks = MD_SB_DISKS;
5984
NeilBrowne6910632008-02-06 01:39:51 -08005985 if (mddev->persistent)
5986 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005987 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988
NeilBrownc3d97142009-12-14 12:49:52 +11005989 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10005990 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11005991 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005992
NeilBrownf6705572006-03-27 01:18:11 -08005993 mddev->reshape_position = MaxSector;
5994
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 /*
5996 * Generate a 128 bit UUID
5997 */
5998 get_random_bytes(mddev->uuid, 16);
5999
NeilBrownf6705572006-03-27 01:18:11 -08006000 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006001 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006002 mddev->new_layout = mddev->layout;
6003 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006004 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006005
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 return 0;
6007}
6008
NeilBrownfd01b882011-10-11 16:47:53 +11006009void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006010{
Dan Williamsb522adc2009-03-31 15:00:31 +11006011 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6012
6013 if (mddev->external_size)
6014 return;
6015
Dan Williams1f403622009-03-31 14:59:03 +11006016 mddev->array_sectors = array_sectors;
6017}
6018EXPORT_SYMBOL(md_set_array_sectors);
6019
NeilBrownfd01b882011-10-11 16:47:53 +11006020static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006021{
NeilBrown3cb03002011-10-11 16:45:26 +11006022 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006023 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006024 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006025
6026 if (mddev->pers->resize == NULL)
6027 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006028 /* The "num_sectors" is the number of sectors of each device that
6029 * is used. This can only make sense for arrays with redundancy.
6030 * linear and raid0 always use whatever space is available. We can only
6031 * consider changing this number if no resync or reconstruction is
6032 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006033 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006034 * of each device. If num_sectors is zero, we find the largest size
6035 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006036 */
NeilBrownf851b602014-12-11 10:02:10 +11006037 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6038 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006039 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006040 if (mddev->ro)
6041 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006042
NeilBrowndafb20f2012-03-19 12:46:39 +11006043 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006044 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006045
Andre Nolld71f9f82008-07-11 22:02:22 +10006046 if (fit && (num_sectors == 0 || num_sectors > avail))
6047 num_sectors = avail;
6048 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006049 return -ENOSPC;
6050 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006051 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006052 if (!rv)
6053 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006054 return rv;
6055}
6056
NeilBrownfd01b882011-10-11 16:47:53 +11006057static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006058{
6059 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006060 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006061 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006062 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006063 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006064 if (mddev->ro)
6065 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006066 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006067 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006068 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006069 if (mddev->sync_thread ||
6070 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6071 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006072 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006073
6074 rdev_for_each(rdev, mddev) {
6075 if (mddev->raid_disks < raid_disks &&
6076 rdev->data_offset < rdev->new_data_offset)
6077 return -EINVAL;
6078 if (mddev->raid_disks > raid_disks &&
6079 rdev->data_offset > rdev->new_data_offset)
6080 return -EINVAL;
6081 }
6082
NeilBrown63c70c42006-03-27 01:18:13 -08006083 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006084 if (mddev->delta_disks < 0)
6085 mddev->reshape_backwards = 1;
6086 else if (mddev->delta_disks > 0)
6087 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006088
6089 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006090 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006091 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006092 mddev->reshape_backwards = 0;
6093 }
NeilBrownda943b992006-01-06 00:20:54 -08006094 return rv;
6095}
6096
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097/*
6098 * update_array_info is used to change the configuration of an
6099 * on-line array.
6100 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6101 * fields in the info are checked against the array.
6102 * Any differences that cannot be handled will cause an error.
6103 * Normally, only one change can be managed at a time.
6104 */
NeilBrownfd01b882011-10-11 16:47:53 +11006105static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106{
6107 int rv = 0;
6108 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006109 int state = 0;
6110
6111 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006112 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006113 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114
6115 if (mddev->major_version != info->major_version ||
6116 mddev->minor_version != info->minor_version ||
6117/* mddev->patch_version != info->patch_version || */
6118 mddev->ctime != info->ctime ||
6119 mddev->level != info->level ||
6120/* mddev->layout != info->layout || */
6121 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006122 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006123 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6124 ((state^info->state) & 0xfffffe00)
6125 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 return -EINVAL;
6127 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006128 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6129 cnt++;
6130 if (mddev->raid_disks != info->raid_disks)
6131 cnt++;
6132 if (mddev->layout != info->layout)
6133 cnt++;
6134 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6135 cnt++;
6136 if (cnt == 0)
6137 return 0;
6138 if (cnt > 1)
6139 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140
6141 if (mddev->layout != info->layout) {
6142 /* Change layout
6143 * we don't need to do anything at the md level, the
6144 * personality will take care of it all.
6145 */
NeilBrown50ac1682009-06-18 08:47:55 +10006146 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006148 else {
6149 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006150 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006151 if (rv)
6152 mddev->new_layout = mddev->layout;
6153 return rv;
6154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006156 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006157 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158
NeilBrownda943b992006-01-06 00:20:54 -08006159 if (mddev->raid_disks != info->raid_disks)
6160 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161
NeilBrown36fa3062005-09-09 16:23:45 -07006162 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006163 if (mddev->pers->quiesce == NULL || mddev->thread == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -07006164 return -EINVAL;
6165 if (mddev->recovery || mddev->sync_thread)
6166 return -EBUSY;
6167 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6168 /* add the bitmap */
6169 if (mddev->bitmap)
6170 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006171 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006172 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006173 mddev->bitmap_info.offset =
6174 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006175 mddev->bitmap_info.space =
6176 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006177 mddev->pers->quiesce(mddev, 1);
6178 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006179 if (!rv)
6180 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006181 if (rv)
6182 bitmap_destroy(mddev);
6183 mddev->pers->quiesce(mddev, 0);
6184 } else {
6185 /* remove the bitmap */
6186 if (!mddev->bitmap)
6187 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006188 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006189 return -EINVAL;
6190 mddev->pers->quiesce(mddev, 1);
6191 bitmap_destroy(mddev);
6192 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006193 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006194 }
6195 }
NeilBrown850b2b42006-10-03 01:15:46 -07006196 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 return rv;
6198}
6199
NeilBrownfd01b882011-10-11 16:47:53 +11006200static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201{
NeilBrown3cb03002011-10-11 16:45:26 +11006202 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006203 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204
6205 if (mddev->pers == NULL)
6206 return -ENODEV;
6207
NeilBrown1ca69c42012-10-11 13:37:33 +11006208 rcu_read_lock();
6209 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006211 err = -ENODEV;
6212 else {
6213 md_error(mddev, rdev);
6214 if (!test_bit(Faulty, &rdev->flags))
6215 err = -EBUSY;
6216 }
6217 rcu_read_unlock();
6218 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219}
6220
Andre Noll2f9618c2008-04-25 18:57:58 +02006221/*
6222 * We have a problem here : there is no easy way to give a CHS
6223 * virtual geometry. We currently pretend that we have a 2 heads
6224 * 4 sectors (with a BIG number of cylinders...). This drives
6225 * dosfs just mad... ;-)
6226 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006227static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6228{
NeilBrownfd01b882011-10-11 16:47:53 +11006229 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006230
6231 geo->heads = 2;
6232 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006233 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006234 return 0;
6235}
6236
Nicolas Schichancb335f82014-01-15 16:58:52 +01006237static inline bool md_ioctl_valid(unsigned int cmd)
6238{
6239 switch (cmd) {
6240 case ADD_NEW_DISK:
6241 case BLKROSET:
6242 case GET_ARRAY_INFO:
6243 case GET_BITMAP_FILE:
6244 case GET_DISK_INFO:
6245 case HOT_ADD_DISK:
6246 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006247 case RAID_AUTORUN:
6248 case RAID_VERSION:
6249 case RESTART_ARRAY_RW:
6250 case RUN_ARRAY:
6251 case SET_ARRAY_INFO:
6252 case SET_BITMAP_FILE:
6253 case SET_DISK_FAULTY:
6254 case STOP_ARRAY:
6255 case STOP_ARRAY_RO:
6256 return true;
6257 default:
6258 return false;
6259 }
6260}
6261
Al Viroa39907f2008-03-02 10:31:15 -05006262static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 unsigned int cmd, unsigned long arg)
6264{
6265 int err = 0;
6266 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006267 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006268 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269
Nicolas Schichancb335f82014-01-15 16:58:52 +01006270 if (!md_ioctl_valid(cmd))
6271 return -ENOTTY;
6272
NeilBrown506c9e42011-12-23 10:17:26 +11006273 switch (cmd) {
6274 case RAID_VERSION:
6275 case GET_ARRAY_INFO:
6276 case GET_DISK_INFO:
6277 break;
6278 default:
6279 if (!capable(CAP_SYS_ADMIN))
6280 return -EACCES;
6281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282
6283 /*
6284 * Commands dealing with the RAID driver but not any
6285 * particular array:
6286 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006287 switch (cmd) {
6288 case RAID_VERSION:
6289 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006290 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006293 case RAID_AUTORUN:
6294 err = 0;
6295 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006296 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006298 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 }
6300
6301 /*
6302 * Commands creating/starting a new array:
6303 */
6304
Al Viroa39907f2008-03-02 10:31:15 -05006305 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306
6307 if (!mddev) {
6308 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006309 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 }
6311
NeilBrown1ca69c42012-10-11 13:37:33 +11006312 /* Some actions do not requires the mutex */
6313 switch (cmd) {
6314 case GET_ARRAY_INFO:
6315 if (!mddev->raid_disks && !mddev->external)
6316 err = -ENODEV;
6317 else
6318 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006319 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006320
6321 case GET_DISK_INFO:
6322 if (!mddev->raid_disks && !mddev->external)
6323 err = -ENODEV;
6324 else
6325 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006326 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006327
6328 case SET_DISK_FAULTY:
6329 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006330 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006331 }
6332
NeilBrowna7a3f082012-12-11 13:35:54 +11006333 if (cmd == ADD_NEW_DISK)
6334 /* need to ensure md_delayed_delete() has completed */
6335 flush_workqueue(md_misc_wq);
6336
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006337 if (cmd == HOT_REMOVE_DISK)
6338 /* need to ensure recovery thread has run */
6339 wait_event_interruptible_timeout(mddev->sb_wait,
6340 !test_bit(MD_RECOVERY_NEEDED,
6341 &mddev->flags),
6342 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006343 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6344 /* Need to flush page cache, and ensure no-one else opens
6345 * and writes
6346 */
6347 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006348 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006349 mutex_unlock(&mddev->open_mutex);
6350 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006351 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006352 }
6353 set_bit(MD_STILL_CLOSED, &mddev->flags);
6354 mutex_unlock(&mddev->open_mutex);
6355 sync_blockdev(bdev);
6356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 err = mddev_lock(mddev);
6358 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006359 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6361 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006362 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 }
6364
NeilBrownc02c0ae2012-12-11 13:39:21 +11006365 if (cmd == SET_ARRAY_INFO) {
6366 mdu_array_info_t info;
6367 if (!arg)
6368 memset(&info, 0, sizeof(info));
6369 else if (copy_from_user(&info, argp, sizeof(info))) {
6370 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006371 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006372 }
6373 if (mddev->pers) {
6374 err = update_array_info(mddev, &info);
6375 if (err) {
6376 printk(KERN_WARNING "md: couldn't update"
6377 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006378 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006380 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006381 }
6382 if (!list_empty(&mddev->disks)) {
6383 printk(KERN_WARNING
6384 "md: array %s already has disks!\n",
6385 mdname(mddev));
6386 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006387 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006388 }
6389 if (mddev->raid_disks) {
6390 printk(KERN_WARNING
6391 "md: array %s already initialised!\n",
6392 mdname(mddev));
6393 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006394 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006395 }
6396 err = set_array_info(mddev, &info);
6397 if (err) {
6398 printk(KERN_WARNING "md: couldn't set"
6399 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006400 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006401 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006402 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403 }
6404
6405 /*
6406 * Commands querying/configuring an existing array:
6407 */
NeilBrown32a76272005-06-21 17:17:14 -07006408 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006409 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006410 if ((!mddev->raid_disks && !mddev->external)
6411 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6412 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6413 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006415 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 }
6417
6418 /*
6419 * Commands even a read-only array can execute:
6420 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006421 switch (cmd) {
6422 case GET_BITMAP_FILE:
6423 err = get_bitmap_file(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006424 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006425
6426 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
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006953 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006955
NeilBrown36d091f2014-12-15 12:56:58 +11006956 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6958 seq_printf(seq, "%s : %sactive", mdname(mddev),
6959 mddev->pers ? "" : "in");
6960 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006961 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006963 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006964 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 seq_printf(seq, " %s", mddev->pers->name);
6966 }
6967
Andre Nolldd8ac332009-03-31 14:33:13 +11006968 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11006969 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 char b[BDEVNAME_SIZE];
6971 seq_printf(seq, " %s[%d]",
6972 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006973 if (test_bit(WriteMostly, &rdev->flags))
6974 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006975 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 seq_printf(seq, "(F)");
6977 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11006978 }
6979 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07006980 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11006981 if (test_bit(Replacement, &rdev->flags))
6982 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11006983 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984 }
6985
6986 if (!list_empty(&mddev->disks)) {
6987 if (mddev->pers)
6988 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006989 (unsigned long long)
6990 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991 else
6992 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006993 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006995 if (mddev->persistent) {
6996 if (mddev->major_version != 0 ||
6997 mddev->minor_version != 90) {
6998 seq_printf(seq," super %d.%d",
6999 mddev->major_version,
7000 mddev->minor_version);
7001 }
NeilBrowne6910632008-02-06 01:39:51 -08007002 } else if (mddev->external)
7003 seq_printf(seq, " super external:%s",
7004 mddev->metadata_type);
7005 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007006 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007
7008 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007009 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007010 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007011 if (mddev->pers->sync_request) {
7012 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007013 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007014 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007015 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007016 seq_printf(seq, "\tresync=DELAYED\n ");
7017 else if (mddev->recovery_cp < MaxSector)
7018 seq_printf(seq, "\tresync=PENDING\n ");
7019 }
NeilBrown32a76272005-06-21 17:17:14 -07007020 } else
7021 seq_printf(seq, "\n ");
7022
NeilBrown57148962012-03-19 12:46:40 +11007023 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024
7025 seq_printf(seq, "\n");
7026 }
NeilBrown36d091f2014-12-15 12:56:58 +11007027 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028 mddev_unlock(mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007029
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030 return 0;
7031}
7032
Jan Engelhardt110518b2009-05-07 12:49:37 +10007033static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 .start = md_seq_start,
7035 .next = md_seq_next,
7036 .stop = md_seq_stop,
7037 .show = md_seq_show,
7038};
7039
7040static int md_seq_open(struct inode *inode, struct file *file)
7041{
Kay Sieversf1514632011-07-12 20:48:39 +02007042 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 int error;
7044
7045 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007046 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007047 return error;
7048
7049 seq = file->private_data;
7050 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 return error;
7052}
7053
NeilBrowne2f23b62014-04-09 14:33:51 +10007054static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007055static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7056{
Kay Sieversf1514632011-07-12 20:48:39 +02007057 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007058 int mask;
7059
NeilBrowne2f23b62014-04-09 14:33:51 +10007060 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007061 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007062 poll_wait(filp, &md_event_waiters, wait);
7063
7064 /* always allow read */
7065 mask = POLLIN | POLLRDNORM;
7066
Kay Sieversf1514632011-07-12 20:48:39 +02007067 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007068 mask |= POLLERR | POLLPRI;
7069 return mask;
7070}
7071
Arjan van de Venfa027c22007-02-12 00:55:33 -08007072static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007073 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074 .open = md_seq_open,
7075 .read = seq_read,
7076 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007077 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007078 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079};
7080
NeilBrown84fc4b52011-10-11 16:49:58 +11007081int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082{
Chao Yu50bd3772014-09-25 15:28:34 +08007083 printk(KERN_INFO "md: %s personality registered for level %d\n",
7084 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007086 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087 spin_unlock(&pers_lock);
7088 return 0;
7089}
NeilBrown6c144d32014-09-30 16:15:38 +10007090EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091
NeilBrown84fc4b52011-10-11 16:49:58 +11007092int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093{
NeilBrown2604b702006-01-06 00:20:36 -08007094 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007096 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 spin_unlock(&pers_lock);
7098 return 0;
7099}
NeilBrown6c144d32014-09-30 16:15:38 +10007100EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101
NeilBrownfd01b882011-10-11 16:47:53 +11007102static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007104 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007106 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107
7108 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007109 rcu_read_lock();
7110 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007112 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7113 (int)part_stat_read(&disk->part0, sectors[1]) -
7114 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007115 /* sync IO will cause sync_io to increase before the disk_stats
7116 * as sync_io is counted when a request starts, and
7117 * disk_stats is counted when it completes.
7118 * So resync activity will cause curr_events to be smaller than
7119 * when there was no such activity.
7120 * non-sync IO will cause disk_stat to increase without
7121 * increasing sync_io so curr_events will (eventually)
7122 * be larger than it was before. Once it becomes
7123 * substantially larger, the test below will cause
7124 * the array to appear non-idle, and resync will slow
7125 * down.
7126 * If there is a lot of outstanding resync activity when
7127 * we set last_event to curr_events, then all that activity
7128 * completing might cause the array to appear non-idle
7129 * and resync will be slowed down even though there might
7130 * not have been non-resync activity. This will only
7131 * happen once though. 'last_events' will soon reflect
7132 * the state where there is little or no outstanding
7133 * resync requests, and further resync activity will
7134 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007135 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007137 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 rdev->last_events = curr_events;
7139 idle = 0;
7140 }
7141 }
NeilBrown4b809912008-07-21 17:05:25 +10007142 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143 return idle;
7144}
7145
NeilBrownfd01b882011-10-11 16:47:53 +11007146void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147{
7148 /* another "blocks" (512byte) blocks have been synced */
7149 atomic_sub(blocks, &mddev->recovery_active);
7150 wake_up(&mddev->recovery_wait);
7151 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007152 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007153 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154 md_wakeup_thread(mddev->thread);
7155 // stop recovery, signal do_sync ....
7156 }
7157}
NeilBrown6c144d32014-09-30 16:15:38 +10007158EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159
NeilBrown06d91a52005-06-21 17:17:12 -07007160/* md_write_start(mddev, bi)
7161 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007162 * in superblock) before writing, schedule a superblock update
7163 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007164 */
NeilBrownfd01b882011-10-11 16:47:53 +11007165void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166{
Neil Brown0fd62b82008-06-28 08:31:36 +10007167 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007168 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007169 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007170
NeilBrownf91de922005-11-08 21:39:36 -08007171 BUG_ON(mddev->ro == 1);
7172 if (mddev->ro == 2) {
7173 /* need to switch to read/write */
7174 mddev->ro = 0;
7175 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7176 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007177 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007178 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007179 }
NeilBrown06d91a52005-06-21 17:17:12 -07007180 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007181 if (mddev->safemode == 1)
7182 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007183 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007184 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007185 if (mddev->in_sync) {
7186 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007187 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007188 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007189 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007190 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007191 }
NeilBrown85572d72014-12-15 12:56:56 +11007192 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007193 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007194 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007195 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007196 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007197 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198}
NeilBrown6c144d32014-09-30 16:15:38 +10007199EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200
NeilBrownfd01b882011-10-11 16:47:53 +11007201void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202{
7203 if (atomic_dec_and_test(&mddev->writes_pending)) {
7204 if (mddev->safemode == 2)
7205 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007206 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7208 }
7209}
NeilBrown6c144d32014-09-30 16:15:38 +10007210EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211
NeilBrown2a2275d2007-01-26 00:57:11 -08007212/* md_allow_write(mddev)
7213 * Calling this ensures that the array is marked 'active' so that writes
7214 * may proceed without blocking. It is important to call this before
7215 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7216 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007217 *
7218 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7219 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007220 */
NeilBrownfd01b882011-10-11 16:47:53 +11007221int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007222{
7223 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007224 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007225 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007226 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007227 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007228 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007229
NeilBrown85572d72014-12-15 12:56:56 +11007230 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007231 if (mddev->in_sync) {
7232 mddev->in_sync = 0;
7233 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007234 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007235 if (mddev->safemode_delay &&
7236 mddev->safemode == 0)
7237 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007238 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007239 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007240 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007241 } else
NeilBrown85572d72014-12-15 12:56:56 +11007242 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007243
NeilBrown070dc6d2010-08-30 17:33:34 +10007244 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007245 return -EAGAIN;
7246 else
7247 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007248}
7249EXPORT_SYMBOL_GPL(md_allow_write);
7250
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251#define SYNC_MARKS 10
7252#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007253#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007254void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255{
Shaohua Li4ed87312012-10-11 13:34:00 +11007256 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007257 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258 unsigned int currspeed = 0,
7259 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007260 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007262 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263 sector_t mark_cnt[SYNC_MARKS];
7264 int last_mark,m;
7265 struct list_head *tmp;
7266 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007267 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007268 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007269 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007270 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007271
7272 /* just incase thread restarts... */
7273 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7274 return;
NeilBrown3991b312014-05-28 13:39:23 +10007275 if (mddev->ro) {/* never try to sync a read-only array */
7276 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007277 return;
NeilBrown3991b312014-05-28 13:39:23 +10007278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279
NeilBrown61df9d92006-10-03 01:15:57 -07007280 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007281 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007282 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007283 action = "check";
7284 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007285 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007286 action = "repair";
7287 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007288 desc = "resync";
7289 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7290 desc = "reshape";
7291 else
7292 desc = "recovery";
7293
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007294 mddev->last_sync_action = action ?: desc;
7295
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296 /* we overload curr_resync somewhat here.
7297 * 0 == not engaged in resync at all
7298 * 2 == checking that there is no conflict with another sync
7299 * 1 == like 2, but have yielded to allow conflicting resync to
7300 * commense
7301 * other == active in resync - this many blocks
7302 *
7303 * Before starting a resync we must have set curr_resync to
7304 * 2, and then checked that every "conflicting" array has curr_resync
7305 * less than ours. When we find one that is the same or higher
7306 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7307 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7308 * This will mean we have to start checking from the beginning again.
7309 *
7310 */
7311
7312 do {
7313 mddev->curr_resync = 2;
7314
7315 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007316 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007318 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319 if (mddev2 == mddev)
7320 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007321 if (!mddev->parallel_resync
7322 && mddev2->curr_resync
7323 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324 DEFINE_WAIT(wq);
7325 if (mddev < mddev2 && mddev->curr_resync == 2) {
7326 /* arbitrarily yield */
7327 mddev->curr_resync = 1;
7328 wake_up(&resync_wait);
7329 }
7330 if (mddev > mddev2 && mddev->curr_resync == 1)
7331 /* no need to wait here, we can wait the next
7332 * time 'round when curr_resync == 2
7333 */
7334 continue;
NeilBrown97441972008-09-19 11:49:54 +10007335 /* We need to wait 'interruptible' so as not to
7336 * contribute to the load average, and not to
7337 * be caught by 'softlockup'
7338 */
7339 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007340 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007341 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007342 printk(KERN_INFO "md: delaying %s of %s"
7343 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007345 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007347 if (signal_pending(current))
7348 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 schedule();
7350 finish_wait(&resync_wait, &wq);
7351 goto try_again;
7352 }
7353 finish_wait(&resync_wait, &wq);
7354 }
7355 }
7356 } while (mddev->curr_resync < 2);
7357
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007358 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007359 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007361 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 */
7363 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007364 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007365 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007366 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7367 j = mddev->resync_min;
7368 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007369 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007370
NeilBrownccfcc3c2006-03-27 01:18:09 -08007371 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007372 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007373 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007375 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007376 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007377 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007378 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007379 if (rdev->raid_disk >= 0 &&
7380 !test_bit(Faulty, &rdev->flags) &&
7381 !test_bit(In_sync, &rdev->flags) &&
7382 rdev->recovery_offset < j)
7383 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007384 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007385
7386 /* If there is a bitmap, we need to make sure all
7387 * writes that started before we added a spare
7388 * complete before we start doing a recovery.
7389 * Otherwise the write might complete and (via
7390 * bitmap_endwrite) set a bit in the bitmap after the
7391 * recovery has checked that bit and skipped that
7392 * region.
7393 */
7394 if (mddev->bitmap) {
7395 mddev->pers->quiesce(mddev, 1);
7396 mddev->pers->quiesce(mddev, 0);
7397 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399
NeilBrown61df9d92006-10-03 01:15:57 -07007400 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7401 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7402 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007403 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007404 "(but not more than %d KB/sec) for %s.\n",
7405 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406
NeilBrowneea1bf32009-03-31 14:27:02 +11007407 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007408
NeilBrown57afd892005-06-21 17:17:13 -07007409 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 for (m = 0; m < SYNC_MARKS; m++) {
7411 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007412 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413 }
7414 last_mark = 0;
7415 mddev->resync_mark = mark[last_mark];
7416 mddev->resync_mark_cnt = mark_cnt[last_mark];
7417
7418 /*
7419 * Tune reconstruction:
7420 */
7421 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007422 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7423 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424
7425 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 last_check = 0;
7427
7428 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007429 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007430 "md: resuming %s of %s from checkpoint.\n",
7431 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007433 } else
7434 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007435 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007436 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7437 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007438 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439
majianpeng7c2c57c2012-07-03 12:12:26 +10007440 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007442 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443
NeilBrown57afd892005-06-21 17:17:13 -07007444 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007445
NeilBrown7a91ee12009-05-26 12:57:21 +10007446 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7447 ((mddev->curr_resync > mddev->curr_resync_completed &&
7448 (mddev->curr_resync - mddev->curr_resync_completed)
7449 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007450 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007451 (j - mddev->curr_resync_completed)*2
7452 >= mddev->resync_max - mddev->curr_resync_completed
7453 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007454 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007455 wait_event(mddev->recovery_wait,
7456 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007457 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007458 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7459 j > mddev->recovery_cp)
7460 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007461 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007462 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007463 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007464 }
NeilBrownacb180b2009-04-14 16:28:34 +10007465
NeilBrownc91abf52013-11-19 12:02:01 +11007466 while (j >= mddev->resync_max &&
7467 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007468 /* As this condition is controlled by user-space,
7469 * we can block indefinitely, so use '_interruptible'
7470 * to avoid triggering warnings.
7471 */
7472 flush_signals(current); /* just in case */
7473 wait_event_interruptible(mddev->recovery_wait,
7474 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007475 || test_bit(MD_RECOVERY_INTR,
7476 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007477 }
NeilBrownacb180b2009-04-14 16:28:34 +10007478
NeilBrownc91abf52013-11-19 12:02:01 +11007479 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7480 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007481
NeilBrown57afd892005-06-21 17:17:13 -07007482 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007483 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007484 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007485 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007486 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487 }
NeilBrown57afd892005-06-21 17:17:13 -07007488
7489 if (!skipped) { /* actual IO requested */
7490 io_sectors += sectors;
7491 atomic_add(sectors, &mddev->recovery_active);
7492 }
7493
NeilBrowne875ece2011-07-28 11:39:24 +10007494 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7495 break;
7496
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007498 if (j > 2)
7499 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007500 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007501 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007502 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007503 * visible in /proc/mdstat
7504 */
7505 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007506
7507 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508 continue;
7509
NeilBrown57afd892005-06-21 17:17:13 -07007510 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511 repeat:
7512 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7513 /* step marks */
7514 int next = (last_mark+1) % SYNC_MARKS;
7515
7516 mddev->resync_mark = mark[next];
7517 mddev->resync_mark_cnt = mark_cnt[next];
7518 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007519 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520 last_mark = next;
7521 }
7522
NeilBrownc91abf52013-11-19 12:02:01 +11007523 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525
7526 /*
7527 * this loop exits only if either when we are slower than
7528 * the 'hard' speed limit, or the system was IO-idle for
7529 * a jiffy.
7530 * the system might be non-idle CPU-wise, but we only care
7531 * about not overloading the IO subsystem. (things like an
7532 * e2fsck being done on the RAID array should execute fast)
7533 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534 cond_resched();
7535
Xiao Niac7e50a2014-08-07 09:37:41 -04007536 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7537 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07007538 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539
NeilBrown88202a02006-01-06 00:21:36 -08007540 if (currspeed > speed_min(mddev)) {
7541 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007542 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007543 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544 goto repeat;
7545 }
7546 }
7547 }
NeilBrownc91abf52013-11-19 12:02:01 +11007548 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7549 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7550 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 /*
7552 * this also signals 'finished resyncing' to md_stop
7553 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007554 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7556
7557 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007558 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559
NeilBrowndfc70642008-05-23 13:04:39 -07007560 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007561 mddev->curr_resync > 2) {
7562 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7563 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7564 if (mddev->curr_resync >= mddev->recovery_cp) {
7565 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007566 "md: checkpointing %s of %s.\n",
7567 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007568 if (test_bit(MD_RECOVERY_ERROR,
7569 &mddev->recovery))
7570 mddev->recovery_cp =
7571 mddev->curr_resync_completed;
7572 else
7573 mddev->recovery_cp =
7574 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007575 }
7576 } else
7577 mddev->recovery_cp = MaxSector;
7578 } else {
7579 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7580 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007581 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007582 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007583 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007584 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007585 !test_bit(Faulty, &rdev->flags) &&
7586 !test_bit(In_sync, &rdev->flags) &&
7587 rdev->recovery_offset < mddev->curr_resync)
7588 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007589 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007592 skip:
NeilBrown17571282006-12-10 02:20:52 -08007593 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594
NeilBrownc07b70a2009-12-14 12:49:48 +11007595 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7596 /* We completed so min/max setting can be forgotten if used. */
7597 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7598 mddev->resync_min = 0;
7599 mddev->resync_max = MaxSector;
7600 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7601 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007602 mddev->curr_resync = 0;
7603 wake_up(&resync_wait);
7604 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7605 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007606 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607}
NeilBrown29269552006-03-27 01:18:10 -08007608EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609
NeilBrown746d3202013-04-24 11:42:41 +10007610static int remove_and_add_spares(struct mddev *mddev,
7611 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007612{
NeilBrown3cb03002011-10-11 16:45:26 +11007613 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007614 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007615 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007616
NeilBrowndafb20f2012-03-19 12:46:39 +11007617 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007618 if ((this == NULL || rdev == this) &&
7619 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007620 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007621 (test_bit(Faulty, &rdev->flags) ||
7622 ! test_bit(In_sync, &rdev->flags)) &&
7623 atomic_read(&rdev->nr_pending)==0) {
7624 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007625 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007626 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007627 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007628 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007629 }
7630 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007631 if (removed && mddev->kobj.sd)
7632 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007633
NeilBrown746d3202013-04-24 11:42:41 +10007634 if (this)
7635 goto no_add;
7636
NeilBrowndafb20f2012-03-19 12:46:39 +11007637 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007638 if (rdev->raid_disk >= 0 &&
7639 !test_bit(In_sync, &rdev->flags) &&
7640 !test_bit(Faulty, &rdev->flags))
7641 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007642 if (rdev->raid_disk >= 0)
7643 continue;
7644 if (test_bit(Faulty, &rdev->flags))
7645 continue;
7646 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11007647 ! (rdev->saved_raid_disk >= 0 &&
7648 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10007649 continue;
7650
NeilBrown7eb41882014-01-14 15:55:14 +11007651 if (rdev->saved_raid_disk < 0)
7652 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007653 if (mddev->pers->
7654 hot_add_disk(mddev, rdev) == 0) {
7655 if (sysfs_link_rdev(mddev, rdev))
7656 /* failure here is OK */;
7657 spares++;
7658 md_new_event(mddev);
7659 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007660 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007661 }
NeilBrown746d3202013-04-24 11:42:41 +10007662no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007663 if (removed)
7664 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007665 return spares;
7666}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007667
NeilBrownac05f252014-09-30 08:10:42 +10007668static void md_start_sync(struct work_struct *ws)
7669{
7670 struct mddev *mddev = container_of(ws, struct mddev, del_work);
7671
7672 mddev->sync_thread = md_register_thread(md_do_sync,
7673 mddev,
7674 "resync");
7675 if (!mddev->sync_thread) {
7676 printk(KERN_ERR "%s: could not start resync"
7677 " thread...\n",
7678 mdname(mddev));
7679 /* leave the spares where they are, it shouldn't hurt */
7680 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7681 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7682 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7683 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7684 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11007685 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10007686 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7687 &mddev->recovery))
7688 if (mddev->sysfs_action)
7689 sysfs_notify_dirent_safe(mddev->sysfs_action);
7690 } else
7691 md_wakeup_thread(mddev->sync_thread);
7692 sysfs_notify_dirent_safe(mddev->sysfs_action);
7693 md_new_event(mddev);
7694}
7695
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696/*
7697 * This routine is regularly called by all per-raid-array threads to
7698 * deal with generic issues like resync and super-block update.
7699 * Raid personalities that don't have a thread (linear/raid0) do not
7700 * need this as they never do any recovery or update the superblock.
7701 *
7702 * It does not do any resync itself, but rather "forks" off other threads
7703 * to do that as needed.
7704 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7705 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007706 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007707 * and wakeups up this thread which will reap the thread and finish up.
7708 * This thread also removes any faulty devices (with nr_pending == 0).
7709 *
7710 * The overall approach is:
7711 * 1/ if the superblock needs updating, update it.
7712 * 2/ If a recovery thread is running, don't do anything else.
7713 * 3/ If recovery has finished, clean up, possibly marking spares active.
7714 * 4/ If there are any faulty devices, remove them.
7715 * 5/ If array is degraded, try to add spares devices
7716 * 6/ If array has spares or is not in-sync, start a resync thread.
7717 */
NeilBrownfd01b882011-10-11 16:47:53 +11007718void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007719{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007720 if (mddev->suspended)
7721 return;
7722
NeilBrown5f404022005-06-21 17:17:16 -07007723 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007724 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725
NeilBrownfca4d842005-06-21 17:17:11 -07007726 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007727 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007728 printk(KERN_INFO "md: %s in immediate safe mode\n",
7729 mdname(mddev));
7730 mddev->safemode = 2;
7731 }
7732 flush_signals(current);
7733 }
7734
NeilBrownc89a8ee2008-08-05 15:54:13 +10007735 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7736 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007737 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11007738 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007739 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007740 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007741 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007742 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7743 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007744 ))
7745 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007746
NeilBrowndf5b89b2006-03-27 01:18:20 -08007747 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007748 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007749
NeilBrownc89a8ee2008-08-05 15:54:13 +10007750 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10007751 /* On a read-only array we can:
7752 * - remove failed devices
7753 * - add already-in_sync devices if the array itself
7754 * is in-sync.
7755 * As we only add devices that are already in-sync,
7756 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10007757 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10007758 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11007759 /* There is no thread, but we need to call
7760 * ->spare_active and clear saved_raid_disk
7761 */
NeilBrown2ac295a2014-05-29 11:40:03 +10007762 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11007763 md_reap_sync_thread(mddev);
7764 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownc89a8ee2008-08-05 15:54:13 +10007765 goto unlock;
7766 }
7767
NeilBrown31a59e32008-04-30 00:52:30 -07007768 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007769 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11007770 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07007771 if (mddev->safemode &&
7772 !atomic_read(&mddev->writes_pending) &&
7773 !mddev->in_sync &&
7774 mddev->recovery_cp == MaxSector) {
7775 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007776 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007777 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007778 }
7779 if (mddev->safemode == 1)
7780 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11007781 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007782 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007783 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007784 }
NeilBrownfca4d842005-06-21 17:17:11 -07007785
NeilBrown7a0a5352013-08-27 16:28:23 +10007786 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07007787 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007788
Linus Torvalds1da177e2005-04-16 15:20:36 -07007789 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7790 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7791 /* resync/recovery still happening */
7792 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7793 goto unlock;
7794 }
7795 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007796 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007797 goto unlock;
7798 }
Neil Brown72a23c22008-06-28 08:31:41 +10007799 /* Set RUNNING before clearing NEEDED to avoid
7800 * any transients in the value of "sync_action".
7801 */
NeilBrown72f36d52012-10-11 14:25:57 +11007802 mddev->curr_resync_completed = 0;
Neil Brown72a23c22008-06-28 08:31:41 +10007803 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007804 /* Clear some bits that don't mean anything, but
7805 * might be left set
7806 */
NeilBrown24dd4692005-11-08 21:39:26 -08007807 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7808 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007809
NeilBrowned209582012-04-24 10:23:14 +10007810 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7811 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10007812 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813 /* no recovery is running.
7814 * remove any failed drives, then
7815 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007816 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007817 * the personality to fail the re-add.
7818 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007819
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007820 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007821 if (mddev->pers->check_reshape == NULL ||
7822 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007823 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10007824 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007825 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007826 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10007827 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007828 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7829 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007830 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007831 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007832 } else if (mddev->recovery_cp < MaxSector) {
7833 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007834 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007835 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7836 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10007837 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08007838
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007840 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007841 /* We are adding a device or devices to an array
7842 * which has the bitmap stored on all devices.
7843 * So make sure all bitmap pages get written
7844 */
7845 bitmap_write_all(mddev->bitmap);
7846 }
NeilBrownac05f252014-09-30 08:10:42 +10007847 INIT_WORK(&mddev->del_work, md_start_sync);
7848 queue_work(md_misc_wq, &mddev->del_work);
7849 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007850 }
NeilBrownac05f252014-09-30 08:10:42 +10007851 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10007852 if (!mddev->sync_thread) {
7853 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11007854 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10007855 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7856 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007857 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007858 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007859 }
NeilBrownac05f252014-09-30 08:10:42 +10007860 unlock:
7861 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007862 mddev_unlock(mddev);
7863 }
7864}
NeilBrown6c144d32014-09-30 16:15:38 +10007865EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007866
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007867void md_reap_sync_thread(struct mddev *mddev)
7868{
7869 struct md_rdev *rdev;
7870
7871 /* resync has finished, collect result */
7872 md_unregister_thread(&mddev->sync_thread);
7873 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7874 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7875 /* success...*/
7876 /* activate any spares */
7877 if (mddev->pers->spare_active(mddev)) {
7878 sysfs_notify(&mddev->kobj, NULL,
7879 "degraded");
7880 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7881 }
7882 }
7883 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7884 mddev->pers->finish_reshape)
7885 mddev->pers->finish_reshape(mddev);
7886
7887 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11007888 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007889 */
NeilBrownf4667222013-12-09 12:04:56 +11007890 if (!mddev->degraded)
7891 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007892 rdev->saved_raid_disk = -1;
7893
7894 md_update_sb(mddev, 1);
7895 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7896 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7897 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7898 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7899 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11007900 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007901 /* flag recovery needed just to double check */
7902 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7903 sysfs_notify_dirent_safe(mddev->sysfs_action);
7904 md_new_event(mddev);
7905 if (mddev->event_work.func)
7906 queue_work(md_misc_wq, &mddev->event_work);
7907}
NeilBrown6c144d32014-09-30 16:15:38 +10007908EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007909
NeilBrownfd01b882011-10-11 16:47:53 +11007910void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007911{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007912 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007913 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007914 !test_bit(Blocked, &rdev->flags) &&
7915 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007916 msecs_to_jiffies(5000));
7917 rdev_dec_pending(rdev, mddev);
7918}
7919EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7920
NeilBrownc6563a82012-05-21 09:27:00 +10007921void md_finish_reshape(struct mddev *mddev)
7922{
7923 /* called be personality module when reshape completes. */
7924 struct md_rdev *rdev;
7925
7926 rdev_for_each(rdev, mddev) {
7927 if (rdev->data_offset > rdev->new_data_offset)
7928 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7929 else
7930 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7931 rdev->data_offset = rdev->new_data_offset;
7932 }
7933}
7934EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10007935
7936/* Bad block management.
7937 * We can record which blocks on each device are 'bad' and so just
7938 * fail those blocks, or that stripe, rather than the whole device.
7939 * Entries in the bad-block table are 64bits wide. This comprises:
7940 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7941 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7942 * A 'shift' can be set so that larger blocks are tracked and
7943 * consequently larger devices can be covered.
7944 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7945 *
7946 * Locking of the bad-block table uses a seqlock so md_is_badblock
7947 * might need to retry if it is very unlucky.
7948 * We will sometimes want to check for bad blocks in a bi_end_io function,
7949 * so we use the write_seqlock_irq variant.
7950 *
7951 * When looking for a bad block we specify a range and want to
7952 * know if any block in the range is bad. So we binary-search
7953 * to the last range that starts at-or-before the given endpoint,
7954 * (or "before the sector after the target range")
7955 * then see if it ends after the given start.
7956 * We return
7957 * 0 if there are no known bad blocks in the range
7958 * 1 if there are known bad block which are all acknowledged
7959 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7960 * plus the start/length of the first bad section we overlap.
7961 */
7962int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7963 sector_t *first_bad, int *bad_sectors)
7964{
7965 int hi;
majianpengab056132012-11-06 17:13:44 +08007966 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10007967 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08007968 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10007969 sector_t target = s + sectors;
7970 unsigned seq;
7971
7972 if (bb->shift > 0) {
7973 /* round the start down, and the end up */
7974 s >>= bb->shift;
7975 target += (1<<bb->shift) - 1;
7976 target >>= bb->shift;
7977 sectors = target - s;
7978 }
7979 /* 'target' is now the first block after the bad range */
7980
7981retry:
7982 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08007983 lo = 0;
7984 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10007985 hi = bb->count;
7986
7987 /* Binary search between lo and hi for 'target'
7988 * i.e. for the last range that starts before 'target'
7989 */
7990 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7991 * are known not to be the last range before target.
7992 * VARIANT: hi-lo is the number of possible
7993 * ranges, and decreases until it reaches 1
7994 */
7995 while (hi - lo > 1) {
7996 int mid = (lo + hi) / 2;
7997 sector_t a = BB_OFFSET(p[mid]);
7998 if (a < target)
7999 /* This could still be the one, earlier ranges
8000 * could not. */
8001 lo = mid;
8002 else
8003 /* This and later ranges are definitely out. */
8004 hi = mid;
8005 }
8006 /* 'lo' might be the last that started before target, but 'hi' isn't */
8007 if (hi > lo) {
8008 /* need to check all range that end after 's' to see if
8009 * any are unacknowledged.
8010 */
8011 while (lo >= 0 &&
8012 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8013 if (BB_OFFSET(p[lo]) < target) {
8014 /* starts before the end, and finishes after
8015 * the start, so they must overlap
8016 */
8017 if (rv != -1 && BB_ACK(p[lo]))
8018 rv = 1;
8019 else
8020 rv = -1;
8021 *first_bad = BB_OFFSET(p[lo]);
8022 *bad_sectors = BB_LEN(p[lo]);
8023 }
8024 lo--;
8025 }
8026 }
8027
8028 if (read_seqretry(&bb->lock, seq))
8029 goto retry;
8030
8031 return rv;
8032}
8033EXPORT_SYMBOL_GPL(md_is_badblock);
8034
8035/*
8036 * Add a range of bad blocks to the table.
8037 * This might extend the table, or might contract it
8038 * if two adjacent ranges can be merged.
8039 * We binary-search to find the 'insertion' point, then
8040 * decide how best to handle it.
8041 */
8042static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8043 int acknowledged)
8044{
8045 u64 *p;
8046 int lo, hi;
8047 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008048 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008049
8050 if (bb->shift < 0)
8051 /* badblocks are disabled */
8052 return 0;
8053
8054 if (bb->shift) {
8055 /* round the start down, and the end up */
8056 sector_t next = s + sectors;
8057 s >>= bb->shift;
8058 next += (1<<bb->shift) - 1;
8059 next >>= bb->shift;
8060 sectors = next - s;
8061 }
8062
Bian Yu905b0292013-10-12 01:10:03 -04008063 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008064
8065 p = bb->page;
8066 lo = 0;
8067 hi = bb->count;
8068 /* Find the last range that starts at-or-before 's' */
8069 while (hi - lo > 1) {
8070 int mid = (lo + hi) / 2;
8071 sector_t a = BB_OFFSET(p[mid]);
8072 if (a <= s)
8073 lo = mid;
8074 else
8075 hi = mid;
8076 }
8077 if (hi > lo && BB_OFFSET(p[lo]) > s)
8078 hi = lo;
8079
8080 if (hi > lo) {
8081 /* we found a range that might merge with the start
8082 * of our new range
8083 */
8084 sector_t a = BB_OFFSET(p[lo]);
8085 sector_t e = a + BB_LEN(p[lo]);
8086 int ack = BB_ACK(p[lo]);
8087 if (e >= s) {
8088 /* Yes, we can merge with a previous range */
8089 if (s == a && s + sectors >= e)
8090 /* new range covers old */
8091 ack = acknowledged;
8092 else
8093 ack = ack && acknowledged;
8094
8095 if (e < s + sectors)
8096 e = s + sectors;
8097 if (e - a <= BB_MAX_LEN) {
8098 p[lo] = BB_MAKE(a, e-a, ack);
8099 s = e;
8100 } else {
8101 /* does not all fit in one range,
8102 * make p[lo] maximal
8103 */
8104 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8105 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8106 s = a + BB_MAX_LEN;
8107 }
8108 sectors = e - s;
8109 }
8110 }
8111 if (sectors && hi < bb->count) {
8112 /* 'hi' points to the first range that starts after 's'.
8113 * Maybe we can merge with the start of that range */
8114 sector_t a = BB_OFFSET(p[hi]);
8115 sector_t e = a + BB_LEN(p[hi]);
8116 int ack = BB_ACK(p[hi]);
8117 if (a <= s + sectors) {
8118 /* merging is possible */
8119 if (e <= s + sectors) {
8120 /* full overlap */
8121 e = s + sectors;
8122 ack = acknowledged;
8123 } else
8124 ack = ack && acknowledged;
8125
8126 a = s;
8127 if (e - a <= BB_MAX_LEN) {
8128 p[hi] = BB_MAKE(a, e-a, ack);
8129 s = e;
8130 } else {
8131 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8132 s = a + BB_MAX_LEN;
8133 }
8134 sectors = e - s;
8135 lo = hi;
8136 hi++;
8137 }
8138 }
8139 if (sectors == 0 && hi < bb->count) {
8140 /* we might be able to combine lo and hi */
8141 /* Note: 's' is at the end of 'lo' */
8142 sector_t a = BB_OFFSET(p[hi]);
8143 int lolen = BB_LEN(p[lo]);
8144 int hilen = BB_LEN(p[hi]);
8145 int newlen = lolen + hilen - (s - a);
8146 if (s >= a && newlen < BB_MAX_LEN) {
8147 /* yes, we can combine them */
8148 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8149 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8150 memmove(p + hi, p + hi + 1,
8151 (bb->count - hi - 1) * 8);
8152 bb->count--;
8153 }
8154 }
8155 while (sectors) {
8156 /* didn't merge (it all).
8157 * Need to add a range just before 'hi' */
8158 if (bb->count >= MD_MAX_BADBLOCKS) {
8159 /* No room for more */
8160 rv = 0;
8161 break;
8162 } else {
8163 int this_sectors = sectors;
8164 memmove(p + hi + 1, p + hi,
8165 (bb->count - hi) * 8);
8166 bb->count++;
8167
8168 if (this_sectors > BB_MAX_LEN)
8169 this_sectors = BB_MAX_LEN;
8170 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8171 sectors -= this_sectors;
8172 s += this_sectors;
8173 }
8174 }
8175
8176 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008177 if (!acknowledged)
8178 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008179 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008180
8181 return rv;
8182}
8183
NeilBrown3cb03002011-10-11 16:45:26 +11008184int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008185 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008186{
NeilBrownc6563a82012-05-21 09:27:00 +10008187 int rv;
8188 if (is_new)
8189 s += rdev->new_data_offset;
8190 else
8191 s += rdev->data_offset;
8192 rv = md_set_badblocks(&rdev->badblocks,
8193 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008194 if (rv) {
8195 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008196 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008197 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8198 md_wakeup_thread(rdev->mddev->thread);
8199 }
8200 return rv;
8201}
8202EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8203
8204/*
8205 * Remove a range of bad blocks from the table.
8206 * This may involve extending the table if we spilt a region,
8207 * but it must not fail. So if the table becomes full, we just
8208 * drop the remove request.
8209 */
8210static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8211{
8212 u64 *p;
8213 int lo, hi;
8214 sector_t target = s + sectors;
8215 int rv = 0;
8216
8217 if (bb->shift > 0) {
8218 /* When clearing we round the start up and the end down.
8219 * This should not matter as the shift should align with
8220 * the block size and no rounding should ever be needed.
8221 * However it is better the think a block is bad when it
8222 * isn't than to think a block is not bad when it is.
8223 */
8224 s += (1<<bb->shift) - 1;
8225 s >>= bb->shift;
8226 target >>= bb->shift;
8227 sectors = target - s;
8228 }
8229
8230 write_seqlock_irq(&bb->lock);
8231
8232 p = bb->page;
8233 lo = 0;
8234 hi = bb->count;
8235 /* Find the last range that starts before 'target' */
8236 while (hi - lo > 1) {
8237 int mid = (lo + hi) / 2;
8238 sector_t a = BB_OFFSET(p[mid]);
8239 if (a < target)
8240 lo = mid;
8241 else
8242 hi = mid;
8243 }
8244 if (hi > lo) {
8245 /* p[lo] is the last range that could overlap the
8246 * current range. Earlier ranges could also overlap,
8247 * but only this one can overlap the end of the range.
8248 */
8249 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8250 /* Partial overlap, leave the tail of this range */
8251 int ack = BB_ACK(p[lo]);
8252 sector_t a = BB_OFFSET(p[lo]);
8253 sector_t end = a + BB_LEN(p[lo]);
8254
8255 if (a < s) {
8256 /* we need to split this range */
8257 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008258 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008259 goto out;
8260 }
8261 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8262 bb->count++;
8263 p[lo] = BB_MAKE(a, s-a, ack);
8264 lo++;
8265 }
8266 p[lo] = BB_MAKE(target, end - target, ack);
8267 /* there is no longer an overlap */
8268 hi = lo;
8269 lo--;
8270 }
8271 while (lo >= 0 &&
8272 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8273 /* This range does overlap */
8274 if (BB_OFFSET(p[lo]) < s) {
8275 /* Keep the early parts of this range. */
8276 int ack = BB_ACK(p[lo]);
8277 sector_t start = BB_OFFSET(p[lo]);
8278 p[lo] = BB_MAKE(start, s - start, ack);
8279 /* now low doesn't overlap, so.. */
8280 break;
8281 }
8282 lo--;
8283 }
8284 /* 'lo' is strictly before, 'hi' is strictly after,
8285 * anything between needs to be discarded
8286 */
8287 if (hi - lo > 1) {
8288 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8289 bb->count -= (hi - lo - 1);
8290 }
8291 }
8292
8293 bb->changed = 1;
8294out:
8295 write_sequnlock_irq(&bb->lock);
8296 return rv;
8297}
8298
NeilBrownc6563a82012-05-21 09:27:00 +10008299int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8300 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008301{
NeilBrownc6563a82012-05-21 09:27:00 +10008302 if (is_new)
8303 s += rdev->new_data_offset;
8304 else
8305 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008306 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008307 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008308}
8309EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8310
8311/*
8312 * Acknowledge all bad blocks in a list.
8313 * This only succeeds if ->changed is clear. It is used by
8314 * in-kernel metadata updates
8315 */
8316void md_ack_all_badblocks(struct badblocks *bb)
8317{
8318 if (bb->page == NULL || bb->changed)
8319 /* no point even trying */
8320 return;
8321 write_seqlock_irq(&bb->lock);
8322
majianpengecb178b2012-03-19 12:46:42 +11008323 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008324 u64 *p = bb->page;
8325 int i;
8326 for (i = 0; i < bb->count ; i++) {
8327 if (!BB_ACK(p[i])) {
8328 sector_t start = BB_OFFSET(p[i]);
8329 int len = BB_LEN(p[i]);
8330 p[i] = BB_MAKE(start, len, 1);
8331 }
8332 }
NeilBrownde393cd2011-07-28 11:31:48 +10008333 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008334 }
8335 write_sequnlock_irq(&bb->lock);
8336}
8337EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8338
NeilBrown16c791a2011-07-28 11:31:47 +10008339/* sysfs access to bad-blocks list.
8340 * We present two files.
8341 * 'bad-blocks' lists sector numbers and lengths of ranges that
8342 * are recorded as bad. The list is truncated to fit within
8343 * the one-page limit of sysfs.
8344 * Writing "sector length" to this file adds an acknowledged
8345 * bad block list.
8346 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8347 * been acknowledged. Writing to this file adds bad blocks
8348 * without acknowledging them. This is largely for testing.
8349 */
8350
8351static ssize_t
8352badblocks_show(struct badblocks *bb, char *page, int unack)
8353{
8354 size_t len;
8355 int i;
8356 u64 *p = bb->page;
8357 unsigned seq;
8358
8359 if (bb->shift < 0)
8360 return 0;
8361
8362retry:
8363 seq = read_seqbegin(&bb->lock);
8364
8365 len = 0;
8366 i = 0;
8367
8368 while (len < PAGE_SIZE && i < bb->count) {
8369 sector_t s = BB_OFFSET(p[i]);
8370 unsigned int length = BB_LEN(p[i]);
8371 int ack = BB_ACK(p[i]);
8372 i++;
8373
8374 if (unack && ack)
8375 continue;
8376
8377 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8378 (unsigned long long)s << bb->shift,
8379 length << bb->shift);
8380 }
NeilBrownde393cd2011-07-28 11:31:48 +10008381 if (unack && len == 0)
8382 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008383
8384 if (read_seqretry(&bb->lock, seq))
8385 goto retry;
8386
8387 return len;
8388}
8389
8390#define DO_DEBUG 1
8391
8392static ssize_t
8393badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8394{
8395 unsigned long long sector;
8396 int length;
8397 char newline;
8398#ifdef DO_DEBUG
8399 /* Allow clearing via sysfs *only* for testing/debugging.
8400 * Normally only a successful write may clear a badblock
8401 */
8402 int clear = 0;
8403 if (page[0] == '-') {
8404 clear = 1;
8405 page++;
8406 }
8407#endif /* DO_DEBUG */
8408
8409 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8410 case 3:
8411 if (newline != '\n')
8412 return -EINVAL;
8413 case 2:
8414 if (length <= 0)
8415 return -EINVAL;
8416 break;
8417 default:
8418 return -EINVAL;
8419 }
8420
8421#ifdef DO_DEBUG
8422 if (clear) {
8423 md_clear_badblocks(bb, sector, length);
8424 return len;
8425 }
8426#endif /* DO_DEBUG */
8427 if (md_set_badblocks(bb, sector, length, !unack))
8428 return len;
8429 else
8430 return -ENOSPC;
8431}
8432
Adrian Bunk75c96f82005-05-05 16:16:09 -07008433static int md_notify_reboot(struct notifier_block *this,
8434 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008435{
8436 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008437 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008438 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439
NeilBrownc744a652012-03-19 12:46:37 +11008440 for_each_mddev(mddev, tmp) {
8441 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008442 if (mddev->pers)
8443 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008444 if (mddev->persistent)
8445 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008446 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008447 }
NeilBrownc744a652012-03-19 12:46:37 +11008448 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008449 }
NeilBrownc744a652012-03-19 12:46:37 +11008450 /*
8451 * certain more exotic SCSI devices are known to be
8452 * volatile wrt too early system reboots. While the
8453 * right place to handle this issue is the given
8454 * driver, we do want to have a safe RAID driver ...
8455 */
8456 if (need_delay)
8457 mdelay(1000*1);
8458
Linus Torvalds1da177e2005-04-16 15:20:36 -07008459 return NOTIFY_DONE;
8460}
8461
Adrian Bunk75c96f82005-05-05 16:16:09 -07008462static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463 .notifier_call = md_notify_reboot,
8464 .next = NULL,
8465 .priority = INT_MAX, /* before any real devices */
8466};
8467
8468static void md_geninit(void)
8469{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008470 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008472 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008473}
8474
Adrian Bunk75c96f82005-05-05 16:16:09 -07008475static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008476{
Tejun Heoe804ac72010-10-15 15:36:08 +02008477 int ret = -ENOMEM;
8478
Tejun Heoada609e2011-01-25 14:35:54 +01008479 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008480 if (!md_wq)
8481 goto err_wq;
8482
8483 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8484 if (!md_misc_wq)
8485 goto err_misc_wq;
8486
8487 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8488 goto err_md;
8489
8490 if ((ret = register_blkdev(0, "mdp")) < 0)
8491 goto err_mdp;
8492 mdp_major = ret;
8493
NeilBrownaf5628f2014-07-31 13:54:54 +10008494 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008495 md_probe, NULL, NULL);
8496 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008497 md_probe, NULL, NULL);
8498
Linus Torvalds1da177e2005-04-16 15:20:36 -07008499 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008500 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501
8502 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504
Tejun Heoe804ac72010-10-15 15:36:08 +02008505err_mdp:
8506 unregister_blkdev(MD_MAJOR, "md");
8507err_md:
8508 destroy_workqueue(md_misc_wq);
8509err_misc_wq:
8510 destroy_workqueue(md_wq);
8511err_wq:
8512 return ret;
8513}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514
8515#ifndef MODULE
8516
8517/*
8518 * Searches all registered partitions for autorun RAID arrays
8519 * at boot time.
8520 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008521
8522static LIST_HEAD(all_detected_devices);
8523struct detected_devices_node {
8524 struct list_head list;
8525 dev_t dev;
8526};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527
8528void md_autodetect_dev(dev_t dev)
8529{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008530 struct detected_devices_node *node_detected_dev;
8531
8532 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8533 if (node_detected_dev) {
8534 node_detected_dev->dev = dev;
8535 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8536 } else {
8537 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8538 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540}
8541
Linus Torvalds1da177e2005-04-16 15:20:36 -07008542static void autostart_arrays(int part)
8543{
NeilBrown3cb03002011-10-11 16:45:26 +11008544 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008545 struct detected_devices_node *node_detected_dev;
8546 dev_t dev;
8547 int i_scanned, i_passed;
8548
8549 i_scanned = 0;
8550 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551
8552 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8553
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008554 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8555 i_scanned++;
8556 node_detected_dev = list_entry(all_detected_devices.next,
8557 struct detected_devices_node, list);
8558 list_del(&node_detected_dev->list);
8559 dev = node_detected_dev->dev;
8560 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008561 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008562 if (IS_ERR(rdev))
8563 continue;
8564
NeilBrown403df472014-09-30 15:52:29 +10008565 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008566 continue;
NeilBrown403df472014-09-30 15:52:29 +10008567
NeilBrownd0fae182008-03-04 14:29:31 -08008568 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008569 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008570 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008571 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008572
8573 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8574 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008575
8576 autorun_devices(part);
8577}
8578
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008579#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008580
8581static __exit void md_exit(void)
8582{
NeilBrownfd01b882011-10-11 16:47:53 +11008583 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008584 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008585 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008586
NeilBrownaf5628f2014-07-31 13:54:54 +10008587 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008588 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008589
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008590 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008591 unregister_blkdev(mdp_major, "mdp");
8592 unregister_reboot_notifier(&md_notifier);
8593 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008594
8595 /* We cannot unload the modules while some process is
8596 * waiting for us in select() or poll() - wake them up
8597 */
8598 md_unloading = 1;
8599 while (waitqueue_active(&md_event_waiters)) {
8600 /* not safe to leave yet */
8601 wake_up(&md_event_waiters);
8602 msleep(delay);
8603 delay += delay;
8604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008605 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008606
NeilBrown29ac4aa2008-02-06 01:39:58 -08008607 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008609 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008610 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008611 destroy_workqueue(md_misc_wq);
8612 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613}
8614
Dan Williams685784a2007-07-09 11:56:42 -07008615subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008616module_exit(md_exit)
8617
NeilBrownf91de922005-11-08 21:39:36 -08008618static int get_ro(char *buffer, struct kernel_param *kp)
8619{
8620 return sprintf(buffer, "%d", start_readonly);
8621}
8622static int set_ro(const char *val, struct kernel_param *kp)
8623{
8624 char *e;
8625 int num = simple_strtoul(val, &e, 10);
8626 if (*val && (*e == '\0' || *e == '\n')) {
8627 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008628 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008629 }
8630 return -EINVAL;
8631}
8632
NeilBrown80ca3a42006-07-10 04:44:18 -07008633module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8634module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11008635module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008636
Linus Torvalds1da177e2005-04-16 15:20:36 -07008637MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008638MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008639MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008640MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);