blob: fe0484648de4c0df62b1ede2f409871bbe0fce6c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
NeilBrownf72ffdd2014-09-30 14:23:59 +10003 Copyright (C) 1998, 1999, 2000 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050056#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110059static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
NeilBrown01f96c02011-09-21 15:30:20 +100062/* pers_list is a list of registered personalities protected
63 * by pers_lock.
64 * pers_lock does extra service to protect accesses to
65 * mddev->thread when the mutex cannot be held.
66 */
NeilBrown2604b702006-01-06 00:20:36 -080067static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static DEFINE_SPINLOCK(pers_lock);
69
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050070struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050071EXPORT_SYMBOL(md_cluster_ops);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050072struct module *md_cluster_mod;
73EXPORT_SYMBOL(md_cluster_mod);
74
Bernd Schubert90b08712008-05-23 13:04:38 -070075static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020076static struct workqueue_struct *md_wq;
77static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070078
NeilBrown746d3202013-04-24 11:42:41 +100079static int remove_and_add_spares(struct mddev *mddev,
80 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110081static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
Robert Becker1e509152009-12-14 12:49:58 +110084 * Default number of read corrections we'll attempt on an rdev
85 * before ejecting it from the array. We divide the read error
86 * count by 2 for every hour elapsed between read errors.
87 */
88#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
89/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
91 * is 1000 KB/sec, so the extra system load does not show up that much.
92 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070093 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * subsystem is idle. There is also an 'absolute maximum' reconstruction
95 * speed limit - in case reconstruction slows down your system despite
96 * idle IO detection.
97 *
98 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080099 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
101
102static int sysctl_speed_limit_min = 1000;
103static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100104static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800105{
106 return mddev->sync_speed_min ?
107 mddev->sync_speed_min : sysctl_speed_limit_min;
108}
109
NeilBrownfd01b882011-10-11 16:47:53 +1100110static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800111{
112 return mddev->sync_speed_max ?
113 mddev->sync_speed_max : sysctl_speed_limit_max;
114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116static struct ctl_table_header *raid_table_header;
117
Joe Perches82592c32013-11-14 15:16:18 +1100118static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .procname = "speed_limit_min",
121 .data = &sysctl_speed_limit_min,
122 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700123 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800124 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 },
126 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .procname = "speed_limit_max",
128 .data = &sysctl_speed_limit_max,
129 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700130 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800131 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800133 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
Joe Perches82592c32013-11-14 15:16:18 +1100136static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .procname = "raid",
139 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700140 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .child = raid_table,
142 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800143 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
Joe Perches82592c32013-11-14 15:16:18 +1100146static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .procname = "dev",
149 .maxlen = 0,
150 .mode = 0555,
151 .child = raid_dir_table,
152 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800153 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700156static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
NeilBrownf91de922005-11-08 21:39:36 -0800158static int start_readonly;
159
NeilBrowna167f662010-10-26 18:31:13 +1100160/* bio_clone_mddev
161 * like bio_clone, but with a local bio set
162 */
163
NeilBrowna167f662010-10-26 18:31:13 +1100164struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100165 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100166{
167 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100168
169 if (!mddev || !mddev->bio_set)
170 return bio_alloc(gfp_mask, nr_iovecs);
171
Kent Overstreet395c72a2012-09-06 15:34:55 -0700172 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100173 if (!b)
174 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100175 return b;
176}
177EXPORT_SYMBOL_GPL(bio_alloc_mddev);
178
179struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100180 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100181{
NeilBrowna167f662010-10-26 18:31:13 +1100182 if (!mddev || !mddev->bio_set)
183 return bio_clone(bio, gfp_mask);
184
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700185 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100186}
187EXPORT_SYMBOL_GPL(bio_clone_mddev);
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
NeilBrownd7603b72006-01-06 00:20:30 -0800190 * We have a system wide 'event count' that is incremented
191 * on any 'interesting' event, and readers of /proc/mdstat
192 * can use 'poll' or 'select' to find out when the event
193 * count increases.
194 *
195 * Events are:
196 * start array, stop array, error, add device, remove device,
197 * start build, activate spare
198 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800199static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800200static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100201void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800202{
203 atomic_inc(&md_event_count);
204 wake_up(&md_event_waiters);
205}
NeilBrown29269552006-03-27 01:18:10 -0800206EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800207
NeilBrownc331eb02006-05-30 21:27:13 -0700208/* Alternate version that can be called from interrupts
209 * when calling sysfs_notify isn't needed.
210 */
NeilBrownfd01b882011-10-11 16:47:53 +1100211static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700212{
213 atomic_inc(&md_event_count);
214 wake_up(&md_event_waiters);
215}
216
NeilBrownd7603b72006-01-06 00:20:30 -0800217/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * Enables to iterate over all existing md arrays
219 * all_mddevs_lock protects this list.
220 */
221static LIST_HEAD(all_mddevs);
222static DEFINE_SPINLOCK(all_mddevs_lock);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/*
225 * iterates through all used mddevs in the system.
226 * We take care to grab the all_mddevs_lock whenever navigating
227 * the list, and to always hold a refcount when unlocked.
228 * Any code which breaks out of this loop while own
229 * a reference to the current mddev and must mddev_put it.
230 */
NeilBrownfd01b882011-10-11 16:47:53 +1100231#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000233 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100234 _tmp = all_mddevs.next; \
235 _mddev = NULL;}); \
236 ({ if (_tmp != &all_mddevs) \
237 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100239 if (_mddev) mddev_put(_mddev); \
240 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
241 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100243 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 )
245
NeilBrown409c57f2009-03-31 14:39:39 +1100246/* Rather than calling directly into the personality make_request function,
247 * IO requests come here first so that we can check if the device is
248 * being suspended pending a reconfiguration.
249 * We hold a refcount over the call to ->make_request. By the time that
250 * call has finished, the bio has been linked into some internal structure
251 * and so is visible to ->quiesce(), so we don't need the refcount any more.
252 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200253static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
NeilBrown49077322010-03-25 16:20:56 +1100255 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100256 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500257 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100258
NeilBrown0ca69882011-01-14 09:14:33 +1100259 if (mddev == NULL || mddev->pers == NULL
260 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100261 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200262 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100263 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100264 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
265 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
266 return;
267 }
NeilBrown0ca69882011-01-14 09:14:33 +1100268 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100269 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200270 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100271 DEFINE_WAIT(__wait);
272 for (;;) {
273 prepare_to_wait(&mddev->sb_wait, &__wait,
274 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200275 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100276 break;
277 rcu_read_unlock();
278 schedule();
279 rcu_read_lock();
280 }
281 finish_wait(&mddev->sb_wait, &__wait);
282 }
283 atomic_inc(&mddev->active_io);
284 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100285
Chris Masone91ece52011-02-07 19:21:48 -0500286 /*
287 * save the sectors now since our bio can
288 * go away inside make_request
289 */
290 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200291 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100292
Gu Zheng18c0b222014-11-24 11:05:26 +0800293 generic_start_io_acct(rw, sectors, &mddev->gendisk->part0);
NeilBrown49077322010-03-25 16:20:56 +1100294
NeilBrown409c57f2009-03-31 14:39:39 +1100295 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
296 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
NeilBrown9e35b992010-04-06 14:23:02 +1000299/* mddev_suspend makes sure no new requests are submitted
300 * to the device, and that any requests that have been submitted
301 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100302 * Once mddev_detach() is called and completes, the module will be
303 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000304 */
NeilBrownfd01b882011-10-11 16:47:53 +1100305void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100306{
307 BUG_ON(mddev->suspended);
308 mddev->suspended = 1;
309 synchronize_rcu();
310 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
311 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500312
313 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100314}
NeilBrown390ee602010-06-01 19:37:27 +1000315EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100316
NeilBrownfd01b882011-10-11 16:47:53 +1100317void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100318{
319 mddev->suspended = 0;
320 wake_up(&mddev->sb_wait);
321 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500322
Jonathan Brassow47525e52012-05-22 13:55:29 +1000323 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500324 md_wakeup_thread(mddev->thread);
325 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100326}
NeilBrown390ee602010-06-01 19:37:27 +1000327EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100328
NeilBrownfd01b882011-10-11 16:47:53 +1100329int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000330{
NeilBrown5c675f82014-12-15 12:56:56 +1100331 struct md_personality *pers = mddev->pers;
332 int ret = 0;
333
334 rcu_read_lock();
335 if (mddev->suspended)
336 ret = 1;
337 else if (pers && pers->congested)
338 ret = pers->congested(mddev, bits);
339 rcu_read_unlock();
340 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000341}
NeilBrown5c675f82014-12-15 12:56:56 +1100342EXPORT_SYMBOL_GPL(mddev_congested);
343static int md_congested(void *data, int bits)
344{
345 struct mddev *mddev = data;
346 return mddev_congested(mddev, bits);
347}
NeilBrown3fa841d2009-09-23 18:10:29 +1000348
NeilBrown64590f42014-12-15 12:56:57 +1100349static int md_mergeable_bvec(struct request_queue *q,
350 struct bvec_merge_data *bvm,
351 struct bio_vec *biovec)
352{
353 struct mddev *mddev = q->queuedata;
354 int ret;
355 rcu_read_lock();
356 if (mddev->suspended) {
357 /* Must always allow one vec */
358 if (bvm->bi_size == 0)
359 ret = biovec->bv_len;
360 else
361 ret = 0;
362 } else {
363 struct md_personality *pers = mddev->pers;
364 if (pers && pers->mergeable_bvec)
365 ret = pers->mergeable_bvec(mddev, bvm, biovec);
366 else
367 ret = biovec->bv_len;
368 }
369 rcu_read_unlock();
370 return ret;
371}
NeilBrowna2826aa2009-12-14 12:49:49 +1100372/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200373 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100374 */
375
Tejun Heoe9c74692010-09-03 11:56:18 +0200376static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100377{
NeilBrown3cb03002011-10-11 16:45:26 +1100378 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100379 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100380
381 rdev_dec_pending(rdev, mddev);
382
383 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200384 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200385 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100386 }
387 bio_put(bio);
388}
389
NeilBrowna7a07e62010-12-09 16:04:25 +1100390static void md_submit_flush_data(struct work_struct *ws);
391
NeilBrowna035fc32010-12-09 16:17:51 +1100392static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100393{
NeilBrownfd01b882011-10-11 16:47:53 +1100394 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100395 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100396
NeilBrowna7a07e62010-12-09 16:04:25 +1100397 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
398 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100399 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100400 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100401 if (rdev->raid_disk >= 0 &&
402 !test_bit(Faulty, &rdev->flags)) {
403 /* Take two references, one is dropped
404 * when request finishes, one after
405 * we reclaim rcu_read_lock
406 */
407 struct bio *bi;
408 atomic_inc(&rdev->nr_pending);
409 atomic_inc(&rdev->nr_pending);
410 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000411 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200412 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100413 bi->bi_private = rdev;
414 bi->bi_bdev = rdev->bdev;
415 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200416 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100417 rcu_read_lock();
418 rdev_dec_pending(rdev, mddev);
419 }
420 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100421 if (atomic_dec_and_test(&mddev->flush_pending))
422 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100423}
424
Tejun Heoe9c74692010-09-03 11:56:18 +0200425static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100426{
NeilBrownfd01b882011-10-11 16:47:53 +1100427 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200428 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100429
Kent Overstreet4f024f32013-10-11 15:44:27 -0700430 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100431 /* an empty barrier - all done */
432 bio_endio(bio, 0);
433 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200434 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200435 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100436 }
NeilBrown2b74e122010-12-09 15:59:01 +1100437
438 mddev->flush_bio = NULL;
439 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100440}
441
NeilBrownfd01b882011-10-11 16:47:53 +1100442void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100443{
NeilBrown85572d72014-12-15 12:56:56 +1100444 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100445 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200446 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100447 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200448 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100449 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100450
NeilBrowna035fc32010-12-09 16:17:51 +1100451 INIT_WORK(&mddev->flush_work, submit_flushes);
452 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100453}
Tejun Heoe9c74692010-09-03 11:56:18 +0200454EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100455
NeilBrown74018dc2012-07-31 09:08:15 +0200456void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000457{
NeilBrown9cbb1752012-07-31 09:08:14 +0200458 struct mddev *mddev = cb->data;
459 md_wakeup_thread(mddev->thread);
460 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000461}
NeilBrown9cbb1752012-07-31 09:08:14 +0200462EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000463
NeilBrownfd01b882011-10-11 16:47:53 +1100464static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 atomic_inc(&mddev->active);
467 return mddev;
468}
469
Dan Williams5fd3a172009-03-04 00:57:25 -0700470static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100471
NeilBrownfd01b882011-10-11 16:47:53 +1100472static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
NeilBrowna167f662010-10-26 18:31:13 +1100474 struct bio_set *bs = NULL;
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
477 return;
NeilBrownd3374822009-01-09 08:31:10 +1100478 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100479 mddev->ctime == 0 && !mddev->hold_active) {
480 /* Array is not configured at all, and not held active,
481 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100482 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100483 bs = mddev->bio_set;
484 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100485 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200486 /* We did a probe so need to clean up. Call
487 * queue_work inside the spinlock so that
488 * flush_workqueue() after mddev_find will
489 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100490 */
491 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200492 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100493 } else
494 kfree(mddev);
495 }
496 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100497 if (bs)
498 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
NeilBrownfd01b882011-10-11 16:47:53 +1100501void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100502{
503 mutex_init(&mddev->open_mutex);
504 mutex_init(&mddev->reconfig_mutex);
505 mutex_init(&mddev->bitmap_info.mutex);
506 INIT_LIST_HEAD(&mddev->disks);
507 INIT_LIST_HEAD(&mddev->all_mddevs);
508 init_timer(&mddev->safemode_timer);
509 atomic_set(&mddev->active, 1);
510 atomic_set(&mddev->openers, 0);
511 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100512 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100513 atomic_set(&mddev->flush_pending, 0);
514 init_waitqueue_head(&mddev->sb_wait);
515 init_waitqueue_head(&mddev->recovery_wait);
516 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000517 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500518 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100519 mddev->resync_min = 0;
520 mddev->resync_max = MaxSector;
521 mddev->level = LEVEL_NONE;
522}
NeilBrown390ee602010-06-01 19:37:27 +1000523EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100524
NeilBrownf72ffdd2014-09-30 14:23:59 +1000525static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
NeilBrownfd01b882011-10-11 16:47:53 +1100527 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
NeilBrown8f5f02c2011-02-16 13:58:51 +1100529 if (unit && MAJOR(unit) != MD_MAJOR)
530 unit &= ~((1<<MdpMinorShift)-1);
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 retry:
533 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
NeilBrownefeb53c2009-01-09 08:31:10 +1100535 if (unit) {
536 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
537 if (mddev->unit == unit) {
538 mddev_get(mddev);
539 spin_unlock(&all_mddevs_lock);
540 kfree(new);
541 return mddev;
542 }
543
544 if (new) {
545 list_add(&new->all_mddevs, &all_mddevs);
546 spin_unlock(&all_mddevs_lock);
547 new->hold_active = UNTIL_IOCTL;
548 return new;
549 }
550 } else if (new) {
551 /* find an unused unit number */
552 static int next_minor = 512;
553 int start = next_minor;
554 int is_free = 0;
555 int dev = 0;
556 while (!is_free) {
557 dev = MKDEV(MD_MAJOR, next_minor);
558 next_minor++;
559 if (next_minor > MINORMASK)
560 next_minor = 0;
561 if (next_minor == start) {
562 /* Oh dear, all in use. */
563 spin_unlock(&all_mddevs_lock);
564 kfree(new);
565 return NULL;
566 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000567
NeilBrownefeb53c2009-01-09 08:31:10 +1100568 is_free = 1;
569 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
570 if (mddev->unit == dev) {
571 is_free = 0;
572 break;
573 }
574 }
575 new->unit = dev;
576 new->md_minor = MINOR(dev);
577 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 list_add(&new->all_mddevs, &all_mddevs);
579 spin_unlock(&all_mddevs_lock);
580 return new;
581 }
582 spin_unlock(&all_mddevs_lock);
583
NeilBrown9ffae0c2006-01-06 00:20:32 -0800584 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (!new)
586 return NULL;
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 new->unit = unit;
589 if (MAJOR(unit) == MD_MAJOR)
590 new->md_minor = MINOR(unit);
591 else
592 new->md_minor = MINOR(unit) >> MdpMinorShift;
593
NeilBrownfafd7fb2010-04-01 15:55:30 +1100594 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 goto retry;
597}
598
NeilBrownb6eb1272010-04-15 10:13:47 +1000599static struct attribute_group md_redundancy_group;
600
NeilBrown5c47daf2014-12-15 12:57:01 +1100601void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
NeilBrowna64c8762010-04-14 17:15:37 +1000603 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000604 /* These cannot be removed under reconfig_mutex as
605 * an access to the files will try to take reconfig_mutex
606 * while holding the file unremovable, which leads to
607 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000608 * So hold set sysfs_active while the remove in happeing,
609 * and anything else which might set ->to_remove or my
610 * otherwise change the sysfs namespace will fail with
611 * -EBUSY if sysfs_active is still set.
612 * We set sysfs_active under reconfig_mutex and elsewhere
613 * test it under the same mutex to ensure its correct value
614 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000615 */
NeilBrowna64c8762010-04-14 17:15:37 +1000616 struct attribute_group *to_remove = mddev->to_remove;
617 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000618 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000619 mutex_unlock(&mddev->reconfig_mutex);
620
NeilBrown00bcb4a2010-06-01 19:37:23 +1000621 if (mddev->kobj.sd) {
622 if (to_remove != &md_redundancy_group)
623 sysfs_remove_group(&mddev->kobj, to_remove);
624 if (mddev->pers == NULL ||
625 mddev->pers->sync_request == NULL) {
626 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
627 if (mddev->sysfs_action)
628 sysfs_put(mddev->sysfs_action);
629 mddev->sysfs_action = NULL;
630 }
NeilBrowna64c8762010-04-14 17:15:37 +1000631 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000632 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000633 } else
634 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Chris Dunlop751e67c2011-10-19 16:48:26 +1100636 /* As we've dropped the mutex we need a spinlock to
637 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000638 */
639 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700640 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000641 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
NeilBrown5c47daf2014-12-15 12:57:01 +1100643EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
NeilBrown1ca69c42012-10-11 13:37:33 +1100645static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
646{
647 struct md_rdev *rdev;
648
649 rdev_for_each_rcu(rdev, mddev)
650 if (rdev->desc_nr == nr)
651 return rdev;
652
653 return NULL;
654}
655
656static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
NeilBrown3cb03002011-10-11 16:45:26 +1100658 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
NeilBrowndafb20f2012-03-19 12:46:39 +1100660 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (rdev->bdev->bd_dev == dev)
662 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return NULL;
665}
666
NeilBrown1ca69c42012-10-11 13:37:33 +1100667static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
668{
669 struct md_rdev *rdev;
670
671 rdev_for_each_rcu(rdev, mddev)
672 if (rdev->bdev->bd_dev == dev)
673 return rdev;
674
675 return NULL;
676}
677
NeilBrown84fc4b52011-10-11 16:49:58 +1100678static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800679{
NeilBrown84fc4b52011-10-11 16:49:58 +1100680 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800681 list_for_each_entry(pers, &pers_list, list) {
682 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800683 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800684 if (strcmp(pers->name, clevel)==0)
685 return pers;
686 }
NeilBrown2604b702006-01-06 00:20:36 -0800687 return NULL;
688}
689
Andre Nollb73df2d2008-07-11 22:02:23 +1000690/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100691static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100693 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000694 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
NeilBrownf72ffdd2014-09-30 14:23:59 +1000697static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 rdev->sb_page = alloc_page(GFP_KERNEL);
700 if (!rdev->sb_page) {
701 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000702 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
705 return 0;
706}
707
NeilBrown545c8792012-05-22 13:54:30 +1000708void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800711 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 rdev->sb_loaded = 0;
713 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000714 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100715 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
NeilBrown2699b672011-07-28 11:31:47 +1000717 if (rdev->bb_page) {
718 put_page(rdev->bb_page);
719 rdev->bb_page = NULL;
720 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000721 kfree(rdev->badblocks.page);
722 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
NeilBrown545c8792012-05-22 13:54:30 +1000724EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
NeilBrown6712ecf2007-09-27 12:47:43 +0200726static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700727{
NeilBrown3cb03002011-10-11 16:45:26 +1100728 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100729 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700730
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700731 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
732 printk("md: super_written gets error=%d, uptodate=%d\n",
733 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
734 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800735 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700736 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700737
NeilBrowna9701a32005-11-08 21:39:34 -0800738 if (atomic_dec_and_test(&mddev->pending_writes))
739 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700740 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700741}
742
NeilBrownfd01b882011-10-11 16:47:53 +1100743void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700744 sector_t sector, int size, struct page *page)
745{
746 /* write first size bytes of page to sector of rdev
747 * Increment mddev->pending_writes before returning
748 * and decrement it on completion, waking up sb_wait
749 * if zero is reached.
750 * If an error occurred, call md_error
751 */
NeilBrowna167f662010-10-26 18:31:13 +1100752 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700753
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100754 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700755 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700756 bio_add_page(bio, page, size, 0);
757 bio->bi_private = rdev;
758 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800759
NeilBrown7bfa19f2005-06-21 17:17:28 -0700760 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000761 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800762}
763
NeilBrownfd01b882011-10-11 16:47:53 +1100764void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800765{
Tejun Heoe9c74692010-09-03 11:56:18 +0200766 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000767 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700768}
769
NeilBrown3cb03002011-10-11 16:45:26 +1100770int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100771 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
NeilBrowna167f662010-10-26 18:31:13 +1100773 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 int ret;
775
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100776 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
777 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100778 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700779 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000780 else if (rdev->mddev->reshape_position != MaxSector &&
781 (rdev->mddev->reshape_backwards ==
782 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700783 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100784 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700785 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700787 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
790 bio_put(bio);
791 return ret;
792}
NeilBrowna8745db2006-01-06 00:20:34 -0800793EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
NeilBrownf72ffdd2014-09-30 14:23:59 +1000795static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (rdev->sb_loaded)
800 return 0;
801
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100802 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 goto fail;
804 rdev->sb_loaded = 1;
805 return 0;
806
807fail:
808 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
809 bdevname(rdev->bdev,b));
810 return -EINVAL;
811}
812
813static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
814{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000815 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000816 sb1->set_uuid1 == sb2->set_uuid1 &&
817 sb1->set_uuid2 == sb2->set_uuid2 &&
818 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
822{
823 int ret;
824 mdp_super_t *tmp1, *tmp2;
825
826 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
827 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
828
829 if (!tmp1 || !tmp2) {
830 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100831 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 goto abort;
833 }
834
835 *tmp1 = *sb1;
836 *tmp2 = *sb2;
837
838 /*
839 * nr_disks is not constant
840 */
841 tmp1->nr_disks = 0;
842 tmp2->nr_disks = 0;
843
Andre Nollce0c8e02008-07-11 22:02:20 +1000844 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700846 kfree(tmp1);
847 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return ret;
849}
850
NeilBrown4d167f02007-05-09 02:35:37 -0700851static u32 md_csum_fold(u32 csum)
852{
853 csum = (csum & 0xffff) + (csum >> 16);
854 return (csum & 0xffff) + (csum >> 16);
855}
856
NeilBrownf72ffdd2014-09-30 14:23:59 +1000857static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
NeilBrown4d167f02007-05-09 02:35:37 -0700859 u64 newcsum = 0;
860 u32 *sb32 = (u32*)sb;
861 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 unsigned int disk_csum, csum;
863
864 disk_csum = sb->sb_csum;
865 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700866
867 for (i = 0; i < MD_SB_BYTES/4 ; i++)
868 newcsum += sb32[i];
869 csum = (newcsum & 0xffffffff) + (newcsum>>32);
870
NeilBrown4d167f02007-05-09 02:35:37 -0700871#ifdef CONFIG_ALPHA
872 /* This used to use csum_partial, which was wrong for several
873 * reasons including that different results are returned on
874 * different architectures. It isn't critical that we get exactly
875 * the same return value as before (we always csum_fold before
876 * testing, and that removes any differences). However as we
877 * know that csum_partial always returned a 16bit value on
878 * alphas, do a fold to maximise conformity to previous behaviour.
879 */
880 sb->sb_csum = md_csum_fold(disk_csum);
881#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700883#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return csum;
885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887/*
888 * Handle superblock details.
889 * We want to be able to handle multiple superblock formats
890 * so we have a common interface to them all, and an array of
891 * different handlers.
892 * We rely on user-space to write the initial superblock, and support
893 * reading and updating of superblocks.
894 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100895 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * loads and validates a superblock on dev.
897 * if refdev != NULL, compare superblocks on both devices
898 * Return:
899 * 0 - dev has a superblock that is compatible with refdev
900 * 1 - dev has a superblock that is compatible and newer than refdev
901 * so dev should be used as the refdev in future
902 * -EINVAL superblock incompatible or invalid
903 * -othererror e.g. -EIO
904 *
NeilBrownfd01b882011-10-11 16:47:53 +1100905 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 * Verify that dev is acceptable into mddev.
907 * The first time, mddev->raid_disks will be 0, and data from
908 * dev should be merged in. Subsequent calls check that dev
909 * is new enough. Return 0 or -EINVAL
910 *
NeilBrownfd01b882011-10-11 16:47:53 +1100911 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 * Update the superblock for rdev with data in mddev
913 * This does not write to disc.
914 *
915 */
916
917struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000918 char *name;
919 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000920 int (*load_super)(struct md_rdev *rdev,
921 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000922 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000923 int (*validate_super)(struct mddev *mddev,
924 struct md_rdev *rdev);
925 void (*sync_super)(struct mddev *mddev,
926 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100927 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000928 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000929 int (*allow_new_offset)(struct md_rdev *rdev,
930 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931};
932
933/*
Andre Noll0894cc32009-06-18 08:49:23 +1000934 * Check that the given mddev has no bitmap.
935 *
936 * This function is called from the run method of all personalities that do not
937 * support bitmaps. It prints an error message and returns non-zero if mddev
938 * has a bitmap. Otherwise, it returns 0.
939 *
940 */
NeilBrownfd01b882011-10-11 16:47:53 +1100941int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000942{
NeilBrownc3d97142009-12-14 12:49:52 +1100943 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000944 return 0;
945 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
946 mdname(mddev), mddev->pers->name);
947 return 1;
948}
949EXPORT_SYMBOL(md_check_no_bitmap);
950
951/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000952 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 */
NeilBrown3cb03002011-10-11 16:45:26 +1100954static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
957 mdp_super_t *sb;
958 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 /*
Andre Noll0f420352008-07-11 22:02:23 +1000961 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 * it's at the end of the disk.
963 *
964 * It also happens to be a multiple of 4Kb.
965 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100966 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
NeilBrown0002b272005-09-09 16:23:53 -0700968 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (ret) return ret;
970
971 ret = -EINVAL;
972
973 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000974 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 if (sb->md_magic != MD_SB_MAGIC) {
977 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
978 b);
979 goto abort;
980 }
981
982 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800983 sb->minor_version < 90 ||
984 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
986 sb->major_version, sb->minor_version,
987 b);
988 goto abort;
989 }
990
991 if (sb->raid_disks <= 0)
992 goto abort;
993
NeilBrown4d167f02007-05-09 02:35:37 -0700994 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
996 b);
997 goto abort;
998 }
999
1000 rdev->preferred_minor = sb->md_minor;
1001 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001002 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001003 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001004 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 if (sb->level == LEVEL_MULTIPATH)
1007 rdev->desc_nr = -1;
1008 else
1009 rdev->desc_nr = sb->this_disk.number;
1010
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001011 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001013 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001015 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (!uuid_equal(refsb, sb)) {
1017 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1018 b, bdevname(refdev->bdev,b2));
1019 goto abort;
1020 }
1021 if (!sb_equal(refsb, sb)) {
1022 printk(KERN_WARNING "md: %s has same UUID"
1023 " but different superblock to %s\n",
1024 b, bdevname(refdev->bdev, b2));
1025 goto abort;
1026 }
1027 ev1 = md_event(sb);
1028 ev2 = md_event(refsb);
1029 if (ev1 > ev2)
1030 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001031 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 ret = 0;
1033 }
NeilBrown8190e752009-06-18 08:48:58 +10001034 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001035 /* Limit to 4TB as metadata cannot record more than that.
1036 * (not needed for Linear and RAID0 as metadata doesn't
1037 * record this size)
1038 */
1039 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001040 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
NeilBrown27a7b262011-09-10 17:21:28 +10001042 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001043 /* "this cannot possibly happen" ... */
1044 ret = -EINVAL;
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 abort:
1047 return ret;
1048}
1049
1050/*
1051 * validate_super for 0.90.0
1052 */
NeilBrownfd01b882011-10-11 16:47:53 +11001053static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001056 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001057 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
NeilBrown41158c72005-06-21 17:17:25 -07001059 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001060 clear_bit(Faulty, &rdev->flags);
1061 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001062 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001063 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (mddev->raid_disks == 0) {
1066 mddev->major_version = 0;
1067 mddev->minor_version = sb->minor_version;
1068 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001069 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001070 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 mddev->ctime = sb->ctime;
1072 mddev->utime = sb->utime;
1073 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001074 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 mddev->layout = sb->layout;
1076 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001077 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001078 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001079 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001080 mddev->bitmap_info.space = 0;
1081 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001082 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001083 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001084 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
NeilBrownf6705572006-03-27 01:18:11 -08001086 if (mddev->minor_version >= 91) {
1087 mddev->reshape_position = sb->reshape_position;
1088 mddev->delta_disks = sb->delta_disks;
1089 mddev->new_level = sb->new_level;
1090 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001091 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001092 if (mddev->delta_disks < 0)
1093 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001094 } else {
1095 mddev->reshape_position = MaxSector;
1096 mddev->delta_disks = 0;
1097 mddev->new_level = mddev->level;
1098 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001099 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001100 }
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (sb->state & (1<<MD_SB_CLEAN))
1103 mddev->recovery_cp = MaxSector;
1104 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001105 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 sb->events_lo == sb->cp_events_lo) {
1107 mddev->recovery_cp = sb->recovery_cp;
1108 } else
1109 mddev->recovery_cp = 0;
1110 }
1111
1112 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1113 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1114 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1115 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1116
1117 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001118
1119 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001120 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001121 mddev->bitmap_info.offset =
1122 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001123 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001124 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001125 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001126
NeilBrown41158c72005-06-21 17:17:25 -07001127 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001128 /* Insist on good event counter while assembling, except
1129 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001131 if (sb->disks[rdev->desc_nr].state & (
1132 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001133 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001134 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001135 } else if (mddev->bitmap) {
1136 /* if adding to array with a bitmap, then we can accept an
1137 * older device ... but not too old.
1138 */
NeilBrown41158c72005-06-21 17:17:25 -07001139 if (ev1 < mddev->bitmap->events_cleared)
1140 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001141 if (ev1 < mddev->events)
1142 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001143 } else {
1144 if (ev1 < mddev->events)
1145 /* just a hot-add of a new device, leave raid_disk at -1 */
1146 return 0;
1147 }
NeilBrown41158c72005-06-21 17:17:25 -07001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 desc = sb->disks + rdev->desc_nr;
1151
1152 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001153 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001154 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1155 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001156 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001158 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001159 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1160 /* active but not in sync implies recovery up to
1161 * reshape position. We don't know exactly where
1162 * that is, so set to zero for now */
1163 if (mddev->minor_version >= 91) {
1164 rdev->recovery_offset = 0;
1165 rdev->raid_disk = desc->raid_disk;
1166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001168 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1169 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001170 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001171 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return 0;
1173}
1174
1175/*
1176 * sync_super for 0.90.0
1177 */
NeilBrownfd01b882011-10-11 16:47:53 +11001178static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001181 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /* make rdev->sb match mddev data..
1185 *
1186 * 1/ zero out disks
1187 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1188 * 3/ any empty disks < next_spare become removed
1189 *
1190 * disks[0] gets initialised to REMOVED because
1191 * we cannot be sure from other fields if it has
1192 * been initialised or not.
1193 */
1194 int i;
1195 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1196
NeilBrown61181562005-09-09 16:24:02 -07001197 rdev->sb_size = MD_SB_BYTES;
1198
Namhyung Kim65a06f062011-07-27 11:00:36 +10001199 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 memset(sb, 0, sizeof(*sb));
1202
1203 sb->md_magic = MD_SB_MAGIC;
1204 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 sb->patch_version = mddev->patch_version;
1206 sb->gvalid_words = 0; /* ignored */
1207 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1208 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1209 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1210 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1211
1212 sb->ctime = mddev->ctime;
1213 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001214 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 sb->raid_disks = mddev->raid_disks;
1216 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001217 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 sb->utime = mddev->utime;
1219 sb->state = 0;
1220 sb->events_hi = (mddev->events>>32);
1221 sb->events_lo = (u32)mddev->events;
1222
NeilBrownf6705572006-03-27 01:18:11 -08001223 if (mddev->reshape_position == MaxSector)
1224 sb->minor_version = 90;
1225 else {
1226 sb->minor_version = 91;
1227 sb->reshape_position = mddev->reshape_position;
1228 sb->new_level = mddev->new_level;
1229 sb->delta_disks = mddev->delta_disks;
1230 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001231 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001232 }
1233 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (mddev->in_sync)
1235 {
1236 sb->recovery_cp = mddev->recovery_cp;
1237 sb->cp_events_hi = (mddev->events>>32);
1238 sb->cp_events_lo = (u32)mddev->events;
1239 if (mddev->recovery_cp == MaxSector)
1240 sb->state = (1<< MD_SB_CLEAN);
1241 } else
1242 sb->recovery_cp = 0;
1243
1244 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001245 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
NeilBrownc3d97142009-12-14 12:49:52 +11001247 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001248 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001251 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001253 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001254 int is_active = test_bit(In_sync, &rdev2->flags);
1255
1256 if (rdev2->raid_disk >= 0 &&
1257 sb->minor_version >= 91)
1258 /* we have nowhere to store the recovery_offset,
1259 * but if it is not below the reshape_position,
1260 * we can piggy-back on that.
1261 */
1262 is_active = 1;
1263 if (rdev2->raid_disk < 0 ||
1264 test_bit(Faulty, &rdev2->flags))
1265 is_active = 0;
1266 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001267 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001269 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001270 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 d = &sb->disks[rdev2->desc_nr];
1272 nr_disks++;
1273 d->number = rdev2->desc_nr;
1274 d->major = MAJOR(rdev2->bdev->bd_dev);
1275 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001276 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 d->raid_disk = rdev2->raid_disk;
1278 else
1279 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001280 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001282 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001284 if (test_bit(In_sync, &rdev2->flags))
1285 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 active++;
1287 working++;
1288 } else {
1289 d->state = 0;
1290 spare++;
1291 working++;
1292 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001293 if (test_bit(WriteMostly, &rdev2->flags))
1294 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 /* now set the "removed" and "faulty" bits on any missing devices */
1297 for (i=0 ; i < mddev->raid_disks ; i++) {
1298 mdp_disk_t *d = &sb->disks[i];
1299 if (d->state == 0 && d->number == 0) {
1300 d->number = i;
1301 d->raid_disk = i;
1302 d->state = (1<<MD_DISK_REMOVED);
1303 d->state |= (1<<MD_DISK_FAULTY);
1304 failed++;
1305 }
1306 }
1307 sb->nr_disks = nr_disks;
1308 sb->active_disks = active;
1309 sb->working_disks = working;
1310 sb->failed_disks = failed;
1311 sb->spare_disks = spare;
1312
1313 sb->this_disk = sb->disks[rdev->desc_nr];
1314 sb->sb_csum = calc_sb_csum(sb);
1315}
1316
1317/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001318 * rdev_size_change for 0.90.0
1319 */
1320static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001321super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001322{
Andre Noll58c0fed2009-03-31 14:33:13 +11001323 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001324 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001325 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001326 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001327 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001328 if (!num_sectors || num_sectors > rdev->sb_start)
1329 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001330 /* Limit to 4TB as metadata cannot record more than that.
1331 * 4TB == 2^32 KB, or 2*2^32 sectors.
1332 */
NeilBrown667a5312012-08-16 16:46:12 +10001333 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001334 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001335 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001336 rdev->sb_page);
1337 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001338 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001339}
1340
NeilBrownc6563a82012-05-21 09:27:00 +10001341static int
1342super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1343{
1344 /* non-zero offset changes not possible with v0.90 */
1345 return new_offset == 0;
1346}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001347
1348/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * version 1 superblock
1350 */
1351
NeilBrownf72ffdd2014-09-30 14:23:59 +10001352static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001354 __le32 disk_csum;
1355 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 unsigned long long newcsum;
1357 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001358 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 disk_csum = sb->sb_csum;
1361 sb->sb_csum = 0;
1362 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001363 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 newcsum += le32_to_cpu(*isuper++);
1365
1366 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001367 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1370 sb->sb_csum = disk_csum;
1371 return cpu_to_le32(csum);
1372}
1373
NeilBrown2699b672011-07-28 11:31:47 +10001374static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1375 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001376static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 struct mdp_superblock_1 *sb;
1379 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001380 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001381 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001383 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /*
Andre Noll0f420352008-07-11 22:02:23 +10001386 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 * It is always aligned to a 4K boundary and
1388 * depeding on minor_version, it can be:
1389 * 0: At least 8K, but less than 12K, from end of device
1390 * 1: At start of device
1391 * 2: 4K from start of device.
1392 */
1393 switch(minor_version) {
1394 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001395 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001396 sb_start -= 8*2;
1397 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 break;
1399 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001400 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 break;
1402 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001403 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 break;
1405 default:
1406 return -EINVAL;
1407 }
Andre Noll0f420352008-07-11 22:02:23 +10001408 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
NeilBrown0002b272005-09-09 16:23:53 -07001410 /* superblock is rarely larger than 1K, but it can be larger,
1411 * and it is safe to read 4k, so we do that
1412 */
1413 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (ret) return ret;
1415
Namhyung Kim65a06f062011-07-27 11:00:36 +10001416 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1419 sb->major_version != cpu_to_le32(1) ||
1420 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001421 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001422 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return -EINVAL;
1424
1425 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1426 printk("md: invalid superblock checksum on %s\n",
1427 bdevname(rdev->bdev,b));
1428 return -EINVAL;
1429 }
1430 if (le64_to_cpu(sb->data_size) < 10) {
1431 printk("md: data_size too small on %s\n",
1432 bdevname(rdev->bdev,b));
1433 return -EINVAL;
1434 }
NeilBrownc6563a82012-05-21 09:27:00 +10001435 if (sb->pad0 ||
1436 sb->pad3[0] ||
1437 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1438 /* Some padding is non-zero, might be a new feature */
1439 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 rdev->preferred_minor = 0xffff;
1442 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001443 rdev->new_data_offset = rdev->data_offset;
1444 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1445 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1446 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001447 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
NeilBrown0002b272005-09-09 16:23:53 -07001449 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001450 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001451 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001452 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1453
1454 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001455 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001456 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001457 if (minor_version
1458 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1459 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001460
NeilBrown31b65a02006-07-10 04:44:14 -07001461 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1462 rdev->desc_nr = -1;
1463 else
1464 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1465
NeilBrown2699b672011-07-28 11:31:47 +10001466 if (!rdev->bb_page) {
1467 rdev->bb_page = alloc_page(GFP_KERNEL);
1468 if (!rdev->bb_page)
1469 return -ENOMEM;
1470 }
1471 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1472 rdev->badblocks.count == 0) {
1473 /* need to load the bad block list.
1474 * Currently we limit it to one page.
1475 */
1476 s32 offset;
1477 sector_t bb_sector;
1478 u64 *bbp;
1479 int i;
1480 int sectors = le16_to_cpu(sb->bblog_size);
1481 if (sectors > (PAGE_SIZE / 512))
1482 return -EINVAL;
1483 offset = le32_to_cpu(sb->bblog_offset);
1484 if (offset == 0)
1485 return -EINVAL;
1486 bb_sector = (long long)offset;
1487 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1488 rdev->bb_page, READ, true))
1489 return -EIO;
1490 bbp = (u64 *)page_address(rdev->bb_page);
1491 rdev->badblocks.shift = sb->bblog_shift;
1492 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1493 u64 bb = le64_to_cpu(*bbp);
1494 int count = bb & (0x3ff);
1495 u64 sector = bb >> 10;
1496 sector <<= sb->bblog_shift;
1497 count <<= sb->bblog_shift;
1498 if (bb + 1 == 0)
1499 break;
1500 if (md_set_badblocks(&rdev->badblocks,
1501 sector, count, 1) == 0)
1502 return -EINVAL;
1503 }
NeilBrown486adf72013-04-24 11:42:44 +10001504 } else if (sb->bblog_offset != 0)
1505 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001506
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001507 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001508 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001509 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001511 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1514 sb->level != refsb->level ||
1515 sb->layout != refsb->layout ||
1516 sb->chunksize != refsb->chunksize) {
1517 printk(KERN_WARNING "md: %s has strangely different"
1518 " superblock to %s\n",
1519 bdevname(rdev->bdev,b),
1520 bdevname(refdev->bdev,b2));
1521 return -EINVAL;
1522 }
1523 ev1 = le64_to_cpu(sb->events);
1524 ev2 = le64_to_cpu(refsb->events);
1525
1526 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001527 ret = 1;
1528 else
1529 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
NeilBrownc6563a82012-05-21 09:27:00 +10001531 if (minor_version) {
1532 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1533 sectors -= rdev->data_offset;
1534 } else
1535 sectors = rdev->sb_start;
1536 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001538 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001539 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
NeilBrownfd01b882011-10-11 16:47:53 +11001542static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001544 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001545 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
NeilBrown41158c72005-06-21 17:17:25 -07001547 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001548 clear_bit(Faulty, &rdev->flags);
1549 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001550 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001551 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (mddev->raid_disks == 0) {
1554 mddev->major_version = 1;
1555 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001556 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001557 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1559 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1560 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001561 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 mddev->layout = le32_to_cpu(sb->layout);
1563 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001564 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001565 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001566 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001567 mddev->bitmap_info.space = 0;
1568 /* Default location for bitmap is 1K after superblock
1569 * using 3K - total of 4K
1570 */
NeilBrownc3d97142009-12-14 12:49:52 +11001571 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001572 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001573 mddev->reshape_backwards = 0;
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1576 memcpy(mddev->uuid, sb->set_uuid, 16);
1577
1578 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001579
NeilBrown71c08052005-09-09 16:23:51 -07001580 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001581 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001582 mddev->bitmap_info.offset =
1583 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001584 /* Metadata doesn't record how much space is available.
1585 * For 1.0, we assume we can use up to the superblock
1586 * if before, else to 4K beyond superblock.
1587 * For others, assume no change is possible.
1588 */
1589 if (mddev->minor_version > 0)
1590 mddev->bitmap_info.space = 0;
1591 else if (mddev->bitmap_info.offset > 0)
1592 mddev->bitmap_info.space =
1593 8 - mddev->bitmap_info.offset;
1594 else
1595 mddev->bitmap_info.space =
1596 -mddev->bitmap_info.offset;
1597 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001598
NeilBrownf6705572006-03-27 01:18:11 -08001599 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1600 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1601 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1602 mddev->new_level = le32_to_cpu(sb->new_level);
1603 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001604 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001605 if (mddev->delta_disks < 0 ||
1606 (mddev->delta_disks == 0 &&
1607 (le32_to_cpu(sb->feature_map)
1608 & MD_FEATURE_RESHAPE_BACKWARDS)))
1609 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001610 } else {
1611 mddev->reshape_position = MaxSector;
1612 mddev->delta_disks = 0;
1613 mddev->new_level = mddev->level;
1614 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001615 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001616 }
1617
NeilBrown41158c72005-06-21 17:17:25 -07001618 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001619 /* Insist of good event counter while assembling, except for
1620 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001622 if (rdev->desc_nr >= 0 &&
1623 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1624 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1625 if (ev1 < mddev->events)
1626 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001627 } else if (mddev->bitmap) {
1628 /* If adding to array with a bitmap, then we can accept an
1629 * older device, but not too old.
1630 */
NeilBrown41158c72005-06-21 17:17:25 -07001631 if (ev1 < mddev->bitmap->events_cleared)
1632 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001633 if (ev1 < mddev->events)
1634 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001635 } else {
1636 if (ev1 < mddev->events)
1637 /* just a hot-add of a new device, leave raid_disk at -1 */
1638 return 0;
1639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (mddev->level != LEVEL_MULTIPATH) {
1641 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001642 if (rdev->desc_nr < 0 ||
1643 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1644 role = 0xffff;
1645 rdev->desc_nr = -1;
1646 } else
1647 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 switch(role) {
1649 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 break;
1651 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001652 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 break;
1654 default:
NeilBrownf4667222013-12-09 12:04:56 +11001655 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001656 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001657 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001658 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001659 if (!(le32_to_cpu(sb->feature_map) &
1660 MD_FEATURE_RECOVERY_BITMAP))
1661 rdev->saved_raid_disk = -1;
1662 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001663 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 rdev->raid_disk = role;
1665 break;
1666 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001667 if (sb->devflags & WriteMostly1)
1668 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001669 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1670 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001671 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001672 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return 0;
1675}
1676
NeilBrownfd01b882011-10-11 16:47:53 +11001677static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001680 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 int max_dev, i;
1682 /* make rdev->sb match mddev and rdev data. */
1683
Namhyung Kim65a06f062011-07-27 11:00:36 +10001684 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 sb->feature_map = 0;
1687 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001688 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 memset(sb->pad3, 0, sizeof(sb->pad3));
1690
1691 sb->utime = cpu_to_le64((__u64)mddev->utime);
1692 sb->events = cpu_to_le64(mddev->events);
1693 if (mddev->in_sync)
1694 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1695 else
1696 sb->resync_offset = cpu_to_le64(0);
1697
NeilBrown1c05b4b2006-10-21 10:24:08 -07001698 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001699
NeilBrownf0ca3402006-02-02 14:28:04 -08001700 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001701 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001702 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001703 sb->level = cpu_to_le32(mddev->level);
1704 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001705
NeilBrownaeb9b2112011-08-25 14:43:08 +10001706 if (test_bit(WriteMostly, &rdev->flags))
1707 sb->devflags |= WriteMostly1;
1708 else
1709 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001710 sb->data_offset = cpu_to_le64(rdev->data_offset);
1711 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001712
NeilBrownc3d97142009-12-14 12:49:52 +11001713 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1714 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001715 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001716 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001717
1718 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001719 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001720 sb->feature_map |=
1721 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1722 sb->recovery_offset =
1723 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001724 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1725 sb->feature_map |=
1726 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001727 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001728 if (test_bit(Replacement, &rdev->flags))
1729 sb->feature_map |=
1730 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001731
NeilBrownf6705572006-03-27 01:18:11 -08001732 if (mddev->reshape_position != MaxSector) {
1733 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1734 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1735 sb->new_layout = cpu_to_le32(mddev->new_layout);
1736 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1737 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001738 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001739 if (mddev->delta_disks == 0 &&
1740 mddev->reshape_backwards)
1741 sb->feature_map
1742 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001743 if (rdev->new_data_offset != rdev->data_offset) {
1744 sb->feature_map
1745 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1746 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1747 - rdev->data_offset));
1748 }
NeilBrownf6705572006-03-27 01:18:11 -08001749 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001750
NeilBrown2699b672011-07-28 11:31:47 +10001751 if (rdev->badblocks.count == 0)
1752 /* Nothing to do for bad blocks*/ ;
1753 else if (sb->bblog_offset == 0)
1754 /* Cannot record bad blocks on this device */
1755 md_error(mddev, rdev);
1756 else {
1757 struct badblocks *bb = &rdev->badblocks;
1758 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1759 u64 *p = bb->page;
1760 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1761 if (bb->changed) {
1762 unsigned seq;
1763
1764retry:
1765 seq = read_seqbegin(&bb->lock);
1766
1767 memset(bbp, 0xff, PAGE_SIZE);
1768
1769 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001770 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001771 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1772 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001773 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001774 }
NeilBrownd0962932012-03-19 12:46:41 +11001775 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001776 if (read_seqretry(&bb->lock, seq))
1777 goto retry;
1778
1779 bb->sector = (rdev->sb_start +
1780 (int)le32_to_cpu(sb->bblog_offset));
1781 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001782 }
1783 }
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001786 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (rdev2->desc_nr+1 > max_dev)
1788 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001789
NeilBrown70471da2009-08-03 10:59:57 +10001790 if (max_dev > le32_to_cpu(sb->max_dev)) {
1791 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001792 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001793 rdev->sb_size = max_dev * 2 + 256;
1794 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1795 if (rdev->sb_size & bmask)
1796 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001797 } else
1798 max_dev = le32_to_cpu(sb->max_dev);
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 for (i=0; i<max_dev;i++)
1801 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001802
NeilBrowndafb20f2012-03-19 12:46:39 +11001803 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001805 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001807 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001809 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001810 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 else
1812 sb->dev_roles[i] = cpu_to_le16(0xffff);
1813 }
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 sb->sb_csum = calc_sb_1_csum(sb);
1816}
1817
Chris Webb0cd17fe2008-06-28 08:31:46 +10001818static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001819super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001820{
1821 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001822 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001823 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001824 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001825 if (rdev->data_offset != rdev->new_data_offset)
1826 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001827 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001828 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001829 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001830 max_sectors -= rdev->data_offset;
1831 if (!num_sectors || num_sectors > max_sectors)
1832 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001833 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001834 /* minor version 0 with bitmap we can't move */
1835 return 0;
1836 } else {
1837 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001838 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001839 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001840 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001841 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001842 if (!num_sectors || num_sectors > max_sectors)
1843 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001844 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001845 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001846 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001847 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001848 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001849 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001850 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001851 rdev->sb_page);
1852 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001853 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001854
1855}
1856
1857static int
1858super_1_allow_new_offset(struct md_rdev *rdev,
1859 unsigned long long new_offset)
1860{
1861 /* All necessary checks on new >= old have been done */
1862 struct bitmap *bitmap;
1863 if (new_offset >= rdev->data_offset)
1864 return 1;
1865
1866 /* with 1.0 metadata, there is no metadata to tread on
1867 * so we can always move back */
1868 if (rdev->mddev->minor_version == 0)
1869 return 1;
1870
1871 /* otherwise we must be sure not to step on
1872 * any metadata, so stay:
1873 * 36K beyond start of superblock
1874 * beyond end of badblocks
1875 * beyond write-intent bitmap
1876 */
1877 if (rdev->sb_start + (32+4)*2 > new_offset)
1878 return 0;
1879 bitmap = rdev->mddev->bitmap;
1880 if (bitmap && !rdev->mddev->bitmap_info.file &&
1881 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001882 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001883 return 0;
1884 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1885 return 0;
1886
1887 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001888}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Adrian Bunk75c96f82005-05-05 16:16:09 -07001890static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 [0] = {
1892 .name = "0.90.0",
1893 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001894 .load_super = super_90_load,
1895 .validate_super = super_90_validate,
1896 .sync_super = super_90_sync,
1897 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001898 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 },
1900 [1] = {
1901 .name = "md-1",
1902 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001903 .load_super = super_1_load,
1904 .validate_super = super_1_validate,
1905 .sync_super = super_1_sync,
1906 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001907 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 },
1909};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
NeilBrownfd01b882011-10-11 16:47:53 +11001911static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001912{
1913 if (mddev->sync_super) {
1914 mddev->sync_super(mddev, rdev);
1915 return;
1916 }
1917
1918 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1919
1920 super_types[mddev->major_version].sync_super(mddev, rdev);
1921}
1922
NeilBrownfd01b882011-10-11 16:47:53 +11001923static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
NeilBrown3cb03002011-10-11 16:45:26 +11001925 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
NeilBrown4b809912008-07-21 17:05:25 +10001927 rcu_read_lock();
1928 rdev_for_each_rcu(rdev, mddev1)
1929 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001930 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001931 rdev2->bdev->bd_contains) {
1932 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001933 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001934 }
1935 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return 0;
1937}
1938
1939static LIST_HEAD(pending_raid_disks);
1940
Andre Nollac5e7112009-08-03 10:59:47 +10001941/*
1942 * Try to register data integrity profile for an mddev
1943 *
1944 * This is called when an array is started and after a disk has been kicked
1945 * from the array. It only succeeds if all working and active component devices
1946 * are integrity capable with matching profiles.
1947 */
NeilBrownfd01b882011-10-11 16:47:53 +11001948int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001949{
NeilBrown3cb03002011-10-11 16:45:26 +11001950 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001951
Andre Nollac5e7112009-08-03 10:59:47 +10001952 if (list_empty(&mddev->disks))
1953 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001954 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1955 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001956 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001957 /* skip spares and non-functional disks */
1958 if (test_bit(Faulty, &rdev->flags))
1959 continue;
1960 if (rdev->raid_disk < 0)
1961 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001962 if (!reference) {
1963 /* Use the first rdev as the reference */
1964 reference = rdev;
1965 continue;
1966 }
1967 /* does this rdev's profile match the reference profile? */
1968 if (blk_integrity_compare(reference->bdev->bd_disk,
1969 rdev->bdev->bd_disk) < 0)
1970 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001971 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001972 if (!reference || !bdev_get_integrity(reference->bdev))
1973 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001974 /*
1975 * All component devices are integrity capable and have matching
1976 * profiles, register the common profile for the md device.
1977 */
1978 if (blk_integrity_register(mddev->gendisk,
1979 bdev_get_integrity(reference->bdev)) != 0) {
1980 printk(KERN_ERR "md: failed to register integrity for %s\n",
1981 mdname(mddev));
1982 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001983 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001984 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1985 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1986 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1987 mdname(mddev));
1988 return -EINVAL;
1989 }
Andre Nollac5e7112009-08-03 10:59:47 +10001990 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001991}
Andre Nollac5e7112009-08-03 10:59:47 +10001992EXPORT_SYMBOL(md_integrity_register);
1993
1994/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001995void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10001996{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001997 struct blk_integrity *bi_rdev;
1998 struct blk_integrity *bi_mddev;
1999
2000 if (!mddev->gendisk)
2001 return;
2002
2003 bi_rdev = bdev_get_integrity(rdev->bdev);
2004 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002005
2006 if (!bi_mddev) /* nothing to do */
2007 return;
2008 if (rdev->raid_disk < 0) /* skip spares */
2009 return;
2010 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2011 rdev->bdev->bd_disk) >= 0)
2012 return;
2013 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2014 blk_integrity_unregister(mddev->gendisk);
2015}
2016EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002017
NeilBrownf72ffdd2014-09-30 14:23:59 +10002018static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002020 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002021 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002022 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002023 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Dan Williams11e2ede2008-04-30 00:52:32 -07002025 /* prevent duplicates */
2026 if (find_rdev(mddev, rdev->bdev->bd_dev))
2027 return -EEXIST;
2028
Andre Nolldd8ac332009-03-31 14:33:13 +11002029 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2030 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2031 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002032 if (mddev->pers) {
2033 /* Cannot change size, so fail
2034 * If mddev->level <= 0, then we don't care
2035 * about aligning sizes (e.g. linear)
2036 */
2037 if (mddev->level > 0)
2038 return -ENOSPC;
2039 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002040 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 /* Verify rdev->desc_nr is unique.
2044 * If it is -1, assign a free number, else
2045 * check number is not in use
2046 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002047 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (rdev->desc_nr < 0) {
2049 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002050 if (mddev->pers)
2051 choice = mddev->raid_disks;
2052 while (find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 choice++;
2054 rdev->desc_nr = choice;
2055 } else {
NeilBrown4878e9e2014-09-25 17:00:11 +10002056 if (find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2057 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002061 rcu_read_unlock();
NeilBrownde01dfa2009-02-06 18:02:46 +11002062 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2063 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2064 mdname(mddev), mddev->max_disks);
2065 return -EBUSY;
2066 }
NeilBrown19133a42005-11-08 21:39:35 -08002067 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002068 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002069 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002072 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002073
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002074 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002075 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002076
Tejun Heo0762b8b2008-08-25 19:56:12 +09002077 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002078 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2079 /* failure here is OK */;
2080 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002081
NeilBrown4b809912008-07-21 17:05:25 +10002082 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002083 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002084
2085 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002086 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002089
2090 fail:
2091 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2092 b, mdname(mddev));
2093 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
NeilBrown177a99b2008-02-06 01:39:56 -08002096static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002097{
NeilBrown3cb03002011-10-11 16:45:26 +11002098 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002099 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002100 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002101}
2102
NeilBrownf72ffdd2014-09-30 14:23:59 +10002103static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
2105 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002106
Tejun Heo49731ba2011-01-14 18:43:57 +01002107 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002108 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2110 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002111 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002112 sysfs_put(rdev->sysfs_state);
2113 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002114 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002115 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002116 * writing to 'remove' to "dev/state". We also need
2117 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002118 */
NeilBrown4b809912008-07-21 17:05:25 +10002119 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002120 INIT_WORK(&rdev->del_work, md_delayed_delete);
2121 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002122 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
2125/*
2126 * prevent the device from being mounted, repartitioned or
2127 * otherwise reused by a RAID array (or any other kernel
2128 * subsystem), by bd_claiming the device.
2129 */
NeilBrown3cb03002011-10-11 16:45:26 +11002130static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
2132 int err = 0;
2133 struct block_device *bdev;
2134 char b[BDEVNAME_SIZE];
2135
Tejun Heod4d77622010-11-13 11:55:18 +01002136 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002137 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 if (IS_ERR(bdev)) {
2139 printk(KERN_ERR "md: could not open %s.\n",
2140 __bdevname(dev, b));
2141 return PTR_ERR(bdev);
2142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 rdev->bdev = bdev;
2144 return err;
2145}
2146
NeilBrown3cb03002011-10-11 16:45:26 +11002147static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
2149 struct block_device *bdev = rdev->bdev;
2150 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002151 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
2154void md_autodetect_dev(dev_t dev);
2155
NeilBrownf72ffdd2014-09-30 14:23:59 +10002156static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
2158 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 printk(KERN_INFO "md: export_rdev(%s)\n",
2161 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002162 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002164 if (test_bit(AutoDetected, &rdev->flags))
2165 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166#endif
2167 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002168 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
NeilBrownf72ffdd2014-09-30 14:23:59 +10002171static void kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
2173 unbind_rdev_from_array(rdev);
2174 export_rdev(rdev);
2175}
2176
NeilBrownfd01b882011-10-11 16:47:53 +11002177static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
NeilBrown0638bb02014-09-25 17:43:47 +10002179 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
NeilBrown0638bb02014-09-25 17:43:47 +10002181 while (!list_empty(&mddev->disks)) {
2182 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2183 same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 kick_rdev_from_array(rdev);
2185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 mddev->raid_disks = 0;
2187 mddev->major_version = 0;
2188}
2189
NeilBrownf72ffdd2014-09-30 14:23:59 +10002190static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
NeilBrown42543762006-06-26 00:27:57 -07002192 /* Update each superblock (in-memory image), but
2193 * if we are allowed to, skip spares which already
2194 * have the right event counter, or have one earlier
2195 * (which would mean they aren't being marked as dirty
2196 * with the rest of the array)
2197 */
NeilBrown3cb03002011-10-11 16:45:26 +11002198 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002199 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002200 if (rdev->sb_events == mddev->events ||
2201 (nospares &&
2202 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002203 rdev->sb_events+1 == mddev->events)) {
2204 /* Don't update this superblock */
2205 rdev->sb_loaded = 2;
2206 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002207 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002208 rdev->sb_loaded = 1;
2209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
2211}
2212
NeilBrownf72ffdd2014-09-30 14:23:59 +10002213static void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
NeilBrown3cb03002011-10-11 16:45:26 +11002215 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002216 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002217 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002218 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
NeilBrownd87f0642013-04-24 11:42:40 +10002220 if (mddev->ro) {
2221 if (force_change)
2222 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2223 return;
2224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002226 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002227 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002228 if (rdev->raid_disk >= 0 &&
2229 mddev->delta_disks >= 0 &&
2230 !test_bit(In_sync, &rdev->flags) &&
2231 mddev->curr_resync_completed > rdev->recovery_offset)
2232 rdev->recovery_offset = mddev->curr_resync_completed;
2233
NeilBrownf72ffdd2014-09-30 14:23:59 +10002234 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002235 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002236 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002237 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002238 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002239 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002240 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002241 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002242 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002243 md_ack_all_badblocks(&rdev->badblocks);
2244 md_error(mddev, rdev);
2245 }
2246 clear_bit(Blocked, &rdev->flags);
2247 clear_bit(BlockedBadBlocks, &rdev->flags);
2248 wake_up(&rdev->blocked_wait);
2249 }
2250 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002251 wake_up(&mddev->sb_wait);
2252 return;
2253 }
2254
NeilBrown85572d72014-12-15 12:56:56 +11002255 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002256
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002257 mddev->utime = get_seconds();
2258
NeilBrown850b2b422006-10-03 01:15:46 -07002259 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2260 force_change = 1;
2261 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2262 /* just a clean<-> dirty transition, possibly leave spares alone,
2263 * though if events isn't the right even/odd, we will have to do
2264 * spares after all
2265 */
2266 nospares = 1;
2267 if (force_change)
2268 nospares = 0;
2269 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002270 /* If the array is degraded, then skipping spares is both
2271 * dangerous and fairly pointless.
2272 * Dangerous because a device that was removed from the array
2273 * might have a event_count that still looks up-to-date,
2274 * so it can be re-added without a resync.
2275 * Pointless because if there are any spares to skip,
2276 * then a recovery will happen and soon that array won't
2277 * be degraded any more and the spare can go back to sleep then.
2278 */
NeilBrown850b2b422006-10-03 01:15:46 -07002279 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002280
NeilBrown06d91a52005-06-21 17:17:12 -07002281 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002282
2283 /* If this is just a dirty<->clean transition, and the array is clean
2284 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002285 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002286 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002287 && mddev->can_decrease_events
2288 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002289 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002290 mddev->can_decrease_events = 0;
2291 } else {
NeilBrown42543762006-06-26 00:27:57 -07002292 /* otherwise we have to go forward and ... */
2293 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002294 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
NeilBrown403df472014-09-30 15:52:29 +10002297 /*
2298 * This 64-bit counter should never wrap.
2299 * Either we are in around ~1 trillion A.C., assuming
2300 * 1 reboot per second, or we have a bug...
2301 */
2302 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002303
NeilBrowndafb20f2012-03-19 12:46:39 +11002304 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002305 if (rdev->badblocks.changed)
2306 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002307 if (test_bit(Faulty, &rdev->flags))
2308 set_bit(FaultRecorded, &rdev->flags);
2309 }
NeilBrown2699b672011-07-28 11:31:47 +10002310
NeilBrowne6910632008-02-06 01:39:51 -08002311 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002312 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
NeilBrown36a4e1f2011-10-07 14:23:17 +11002314 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2315 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
NeilBrown4ad13662007-07-17 04:06:13 -07002317 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002318 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002320
NeilBrown42543762006-06-26 00:27:57 -07002321 if (rdev->sb_loaded != 1)
2322 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
NeilBrownf4667222013-12-09 12:04:56 +11002324 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002325 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002326 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002327 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002328 pr_debug("md: (write) %s's sb offset: %llu\n",
2329 bdevname(rdev->bdev, b),
2330 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002331 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002332 if (rdev->badblocks.size) {
2333 md_super_write(mddev, rdev,
2334 rdev->badblocks.sector,
2335 rdev->badblocks.size << 9,
2336 rdev->bb_page);
2337 rdev->badblocks.size = 0;
2338 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002339
NeilBrownf4667222013-12-09 12:04:56 +11002340 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002341 pr_debug("md: %s (skipping faulty)\n",
2342 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002343
NeilBrown7bfa19f2005-06-21 17:17:28 -07002344 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 /* only need to write one superblock... */
2346 break;
2347 }
NeilBrowna9701a32005-11-08 21:39:34 -08002348 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002349 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002350
NeilBrown85572d72014-12-15 12:56:56 +11002351 spin_lock(&mddev->lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002352 if (mddev->in_sync != sync_req ||
2353 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002354 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002355 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002356 goto repeat;
2357 }
NeilBrown850b2b422006-10-03 01:15:46 -07002358 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002359 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002360 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002361 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2362 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002363
NeilBrowndafb20f2012-03-19 12:46:39 +11002364 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002365 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2366 clear_bit(Blocked, &rdev->flags);
2367
2368 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002369 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002370 clear_bit(BlockedBadBlocks, &rdev->flags);
2371 wake_up(&rdev->blocked_wait);
2372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374
Andre Noll7f6ce762008-03-23 18:34:54 +01002375/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002376 * We want to accept with case. For this we use cmd_match.
2377 */
2378static int cmd_match(const char *cmd, const char *str)
2379{
2380 /* See if cmd, written into a sysfs file, matches
2381 * str. They must either be the same, or cmd can
2382 * have a trailing newline
2383 */
2384 while (*cmd && *str && *cmd == *str) {
2385 cmd++;
2386 str++;
2387 }
2388 if (*cmd == '\n')
2389 cmd++;
2390 if (*str || *cmd)
2391 return 0;
2392 return 1;
2393}
2394
NeilBrown86e6ffd2005-11-08 21:39:24 -08002395struct rdev_sysfs_entry {
2396 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002397 ssize_t (*show)(struct md_rdev *, char *);
2398 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002399};
2400
2401static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002402state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002403{
2404 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002405 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002406 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002407
NeilBrown758bfc82014-12-15 12:56:59 +11002408 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002409 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002410 len+= sprintf(page+len, "%sfaulty",sep);
2411 sep = ",";
2412 }
NeilBrown758bfc82014-12-15 12:56:59 +11002413 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002414 len += sprintf(page+len, "%sin_sync",sep);
2415 sep = ",";
2416 }
NeilBrown758bfc82014-12-15 12:56:59 +11002417 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002418 len += sprintf(page+len, "%swrite_mostly",sep);
2419 sep = ",";
2420 }
NeilBrown758bfc82014-12-15 12:56:59 +11002421 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002422 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002423 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002424 len += sprintf(page+len, "%sblocked", sep);
2425 sep = ",";
2426 }
NeilBrown758bfc82014-12-15 12:56:59 +11002427 if (!test_bit(Faulty, &flags) &&
2428 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002429 len += sprintf(page+len, "%sspare", sep);
2430 sep = ",";
2431 }
NeilBrown758bfc82014-12-15 12:56:59 +11002432 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002433 len += sprintf(page+len, "%swrite_error", sep);
2434 sep = ",";
2435 }
NeilBrown758bfc82014-12-15 12:56:59 +11002436 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002437 len += sprintf(page+len, "%swant_replacement", sep);
2438 sep = ",";
2439 }
NeilBrown758bfc82014-12-15 12:56:59 +11002440 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002441 len += sprintf(page+len, "%sreplacement", sep);
2442 sep = ",";
2443 }
2444
NeilBrown86e6ffd2005-11-08 21:39:24 -08002445 return len+sprintf(page+len, "\n");
2446}
2447
NeilBrown45dc2de2006-06-26 00:27:58 -07002448static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002449state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002450{
2451 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002452 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002453 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002454 * writemostly - sets write_mostly
2455 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002456 * blocked - sets the Blocked flags
2457 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002458 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002459 * -insync - clear Insync for a device with a slot assigned,
2460 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002461 * write_error - sets WriteErrorSeen
2462 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002463 */
2464 int err = -EINVAL;
2465 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2466 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002467 if (test_bit(Faulty, &rdev->flags))
2468 err = 0;
2469 else
2470 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002471 } else if (cmd_match(buf, "remove")) {
2472 if (rdev->raid_disk >= 0)
2473 err = -EBUSY;
2474 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002475 struct mddev *mddev = rdev->mddev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05002476 if (mddev_is_clustered(mddev))
2477 md_cluster_ops->metadata_update_start(mddev);
NeilBrown45dc2de2006-06-26 00:27:58 -07002478 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002479 if (mddev->pers)
2480 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002481 md_new_event(mddev);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05002482 if (mddev_is_clustered(mddev))
2483 md_cluster_ops->metadata_update_finish(mddev);
NeilBrown45dc2de2006-06-26 00:27:58 -07002484 err = 0;
2485 }
NeilBrownf6556752006-06-26 00:28:01 -07002486 } else if (cmd_match(buf, "writemostly")) {
2487 set_bit(WriteMostly, &rdev->flags);
2488 err = 0;
2489 } else if (cmd_match(buf, "-writemostly")) {
2490 clear_bit(WriteMostly, &rdev->flags);
2491 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002492 } else if (cmd_match(buf, "blocked")) {
2493 set_bit(Blocked, &rdev->flags);
2494 err = 0;
2495 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002496 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002497 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002498 /* metadata handler doesn't understand badblocks,
2499 * so we need to fail the device
2500 */
2501 md_error(rdev->mddev, rdev);
2502 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002503 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002504 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002505 wake_up(&rdev->blocked_wait);
2506 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2507 md_wakeup_thread(rdev->mddev->thread);
2508
2509 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002510 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2511 set_bit(In_sync, &rdev->flags);
2512 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002513 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
NeilBrowne1960f82014-09-30 15:24:25 +10002514 if (rdev->mddev->pers == NULL) {
2515 clear_bit(In_sync, &rdev->flags);
2516 rdev->saved_raid_disk = rdev->raid_disk;
2517 rdev->raid_disk = -1;
2518 err = 0;
2519 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002520 } else if (cmd_match(buf, "write_error")) {
2521 set_bit(WriteErrorSeen, &rdev->flags);
2522 err = 0;
2523 } else if (cmd_match(buf, "-write_error")) {
2524 clear_bit(WriteErrorSeen, &rdev->flags);
2525 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002526 } else if (cmd_match(buf, "want_replacement")) {
2527 /* Any non-spare device that is not a replacement can
2528 * become want_replacement at any time, but we then need to
2529 * check if recovery is needed.
2530 */
2531 if (rdev->raid_disk >= 0 &&
2532 !test_bit(Replacement, &rdev->flags))
2533 set_bit(WantReplacement, &rdev->flags);
2534 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2535 md_wakeup_thread(rdev->mddev->thread);
2536 err = 0;
2537 } else if (cmd_match(buf, "-want_replacement")) {
2538 /* Clearing 'want_replacement' is always allowed.
2539 * Once replacements starts it is too late though.
2540 */
2541 err = 0;
2542 clear_bit(WantReplacement, &rdev->flags);
2543 } else if (cmd_match(buf, "replacement")) {
2544 /* Can only set a device as a replacement when array has not
2545 * yet been started. Once running, replacement is automatic
2546 * from spares, or by assigning 'slot'.
2547 */
2548 if (rdev->mddev->pers)
2549 err = -EBUSY;
2550 else {
2551 set_bit(Replacement, &rdev->flags);
2552 err = 0;
2553 }
2554 } else if (cmd_match(buf, "-replacement")) {
2555 /* Similarly, can only clear Replacement before start */
2556 if (rdev->mddev->pers)
2557 err = -EBUSY;
2558 else {
2559 clear_bit(Replacement, &rdev->flags);
2560 err = 0;
2561 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002562 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002563 if (!err)
2564 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002565 return err ? err : len;
2566}
NeilBrown80ca3a42006-07-10 04:44:18 -07002567static struct rdev_sysfs_entry rdev_state =
2568__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002569
2570static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002571errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002572{
2573 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2574}
2575
2576static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002577errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002578{
2579 char *e;
2580 unsigned long n = simple_strtoul(buf, &e, 10);
2581 if (*buf && (*e == 0 || *e == '\n')) {
2582 atomic_set(&rdev->corrected_errors, n);
2583 return len;
2584 }
2585 return -EINVAL;
2586}
2587static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002588__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002589
NeilBrown014236d2006-01-06 00:20:55 -08002590static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002591slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002592{
2593 if (rdev->raid_disk < 0)
2594 return sprintf(page, "none\n");
2595 else
2596 return sprintf(page, "%d\n", rdev->raid_disk);
2597}
2598
2599static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002600slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002601{
2602 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002603 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002604 int slot = simple_strtoul(buf, &e, 10);
2605 if (strncmp(buf, "none", 4)==0)
2606 slot = -1;
2607 else if (e==buf || (*e && *e!= '\n'))
2608 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002609 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002610 /* Setting 'slot' on an active array requires also
2611 * updating the 'rd%d' link, and communicating
2612 * with the personality with ->hot_*_disk.
2613 * For now we only support removing
2614 * failed/spare devices. This normally happens automatically,
2615 * but not when the metadata is externally managed.
2616 */
NeilBrownc303da62008-02-06 01:39:51 -08002617 if (rdev->raid_disk == -1)
2618 return -EEXIST;
2619 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002620 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002621 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002622 clear_bit(Blocked, &rdev->flags);
2623 remove_and_add_spares(rdev->mddev, rdev);
2624 if (rdev->raid_disk >= 0)
2625 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002626 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2627 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002628 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002629 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002630 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002631 */
2632
2633 if (rdev->raid_disk != -1)
2634 return -EBUSY;
2635
NeilBrownc6751b22011-02-02 11:57:13 +11002636 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2637 return -EBUSY;
2638
Neil Brown6c2fce22008-06-28 08:31:31 +10002639 if (rdev->mddev->pers->hot_add_disk == NULL)
2640 return -EINVAL;
2641
NeilBrownba1b41b2011-01-14 09:14:34 +11002642 if (slot >= rdev->mddev->raid_disks &&
2643 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2644 return -ENOSPC;
2645
Neil Brown6c2fce22008-06-28 08:31:31 +10002646 rdev->raid_disk = slot;
2647 if (test_bit(In_sync, &rdev->flags))
2648 rdev->saved_raid_disk = slot;
2649 else
2650 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002651 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002652 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002653 err = rdev->mddev->pers->
2654 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002655 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002656 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002657 return err;
Neil Brown52664732008-06-28 08:31:44 +10002658 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002659 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002660 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002661 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002662 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002663 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002664 if (slot >= rdev->mddev->raid_disks &&
2665 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002666 return -ENOSPC;
2667 rdev->raid_disk = slot;
2668 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002669 clear_bit(Faulty, &rdev->flags);
2670 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002671 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002672 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002673 }
NeilBrown014236d2006-01-06 00:20:55 -08002674 return len;
2675}
2676
NeilBrown014236d2006-01-06 00:20:55 -08002677static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002678__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002679
NeilBrown93c8cad2006-01-06 00:20:56 -08002680static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002681offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002682{
Andrew Morton6961ece2006-01-06 00:20:59 -08002683 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002684}
2685
2686static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002687offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002688{
NeilBrownc6563a82012-05-21 09:27:00 +10002689 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002690 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002691 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002692 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002693 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002694 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002695 /* Must set offset before size, so overlap checks
2696 * can be sane */
2697 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002698 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002699 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002700 return len;
2701}
2702
2703static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002704__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002705
NeilBrownc6563a82012-05-21 09:27:00 +10002706static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2707{
2708 return sprintf(page, "%llu\n",
2709 (unsigned long long)rdev->new_data_offset);
2710}
2711
2712static ssize_t new_offset_store(struct md_rdev *rdev,
2713 const char *buf, size_t len)
2714{
2715 unsigned long long new_offset;
2716 struct mddev *mddev = rdev->mddev;
2717
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002718 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002719 return -EINVAL;
2720
NeilBrownf851b602014-12-11 10:02:10 +11002721 if (mddev->sync_thread ||
2722 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002723 return -EBUSY;
2724 if (new_offset == rdev->data_offset)
2725 /* reset is always permitted */
2726 ;
2727 else if (new_offset > rdev->data_offset) {
2728 /* must not push array size beyond rdev_sectors */
2729 if (new_offset - rdev->data_offset
2730 + mddev->dev_sectors > rdev->sectors)
2731 return -E2BIG;
2732 }
2733 /* Metadata worries about other space details. */
2734
2735 /* decreasing the offset is inconsistent with a backwards
2736 * reshape.
2737 */
2738 if (new_offset < rdev->data_offset &&
2739 mddev->reshape_backwards)
2740 return -EINVAL;
2741 /* Increasing offset is inconsistent with forwards
2742 * reshape. reshape_direction should be set to
2743 * 'backwards' first.
2744 */
2745 if (new_offset > rdev->data_offset &&
2746 !mddev->reshape_backwards)
2747 return -EINVAL;
2748
2749 if (mddev->pers && mddev->persistent &&
2750 !super_types[mddev->major_version]
2751 .allow_new_offset(rdev, new_offset))
2752 return -E2BIG;
2753 rdev->new_data_offset = new_offset;
2754 if (new_offset > rdev->data_offset)
2755 mddev->reshape_backwards = 1;
2756 else if (new_offset < rdev->data_offset)
2757 mddev->reshape_backwards = 0;
2758
2759 return len;
2760}
2761static struct rdev_sysfs_entry rdev_new_offset =
2762__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2763
NeilBrown83303b62006-01-06 00:21:06 -08002764static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002765rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002766{
Andre Nolldd8ac332009-03-31 14:33:13 +11002767 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002768}
2769
NeilBrownc5d79ad2008-02-06 01:39:54 -08002770static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2771{
2772 /* check if two start/length pairs overlap */
2773 if (s1+l1 <= s2)
2774 return 0;
2775 if (s2+l2 <= s1)
2776 return 0;
2777 return 1;
2778}
2779
Dan Williamsb522adc2009-03-31 15:00:31 +11002780static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2781{
2782 unsigned long long blocks;
2783 sector_t new;
2784
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002785 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002786 return -EINVAL;
2787
2788 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2789 return -EINVAL; /* sector conversion overflow */
2790
2791 new = blocks * 2;
2792 if (new != blocks * 2)
2793 return -EINVAL; /* unsigned long long to sector_t overflow */
2794
2795 *sectors = new;
2796 return 0;
2797}
2798
NeilBrown83303b62006-01-06 00:21:06 -08002799static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002800rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002801{
NeilBrownfd01b882011-10-11 16:47:53 +11002802 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002803 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002804 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002805
Dan Williamsb522adc2009-03-31 15:00:31 +11002806 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002807 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002808 if (rdev->data_offset != rdev->new_data_offset)
2809 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002810 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002811 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002812 sectors = super_types[my_mddev->major_version].
2813 rdev_size_change(rdev, sectors);
2814 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002815 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002816 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002817 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002818 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002819 if (!my_mddev->pers->resize)
2820 /* Cannot change size for RAID0 or Linear etc */
2821 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002822 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002823 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002824 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002825
Andre Nolldd8ac332009-03-31 14:33:13 +11002826 rdev->sectors = sectors;
2827 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002828 /* Need to check that all other rdevs with the same
2829 * ->bdev do not overlap. 'rcu' is sufficient to walk
2830 * the rdev lists safely.
2831 * This check does not provide a hard guarantee, it
2832 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002833 */
NeilBrownfd01b882011-10-11 16:47:53 +11002834 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002835 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002836 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002837
NeilBrown8b1afc32014-09-29 15:33:20 +10002838 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002839 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002840 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002841
NeilBrowndafb20f2012-03-19 12:46:39 +11002842 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002843 if (rdev->bdev == rdev2->bdev &&
2844 rdev != rdev2 &&
2845 overlaps(rdev->data_offset, rdev->sectors,
2846 rdev2->data_offset,
2847 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002848 overlap = 1;
2849 break;
2850 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002851 if (overlap) {
2852 mddev_put(mddev);
2853 break;
2854 }
2855 }
NeilBrown8b1afc32014-09-29 15:33:20 +10002856 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08002857 if (overlap) {
2858 /* Someone else could have slipped in a size
2859 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002860 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002861 * safe, and trust userspace not to race with
2862 * itself
2863 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002864 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002865 return -EBUSY;
2866 }
2867 }
NeilBrown83303b62006-01-06 00:21:06 -08002868 return len;
2869}
2870
2871static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002872__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002873
NeilBrown3cb03002011-10-11 16:45:26 +11002874static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002875{
2876 unsigned long long recovery_start = rdev->recovery_offset;
2877
2878 if (test_bit(In_sync, &rdev->flags) ||
2879 recovery_start == MaxSector)
2880 return sprintf(page, "none\n");
2881
2882 return sprintf(page, "%llu\n", recovery_start);
2883}
2884
NeilBrown3cb03002011-10-11 16:45:26 +11002885static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002886{
2887 unsigned long long recovery_start;
2888
2889 if (cmd_match(buf, "none"))
2890 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002891 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07002892 return -EINVAL;
2893
2894 if (rdev->mddev->pers &&
2895 rdev->raid_disk >= 0)
2896 return -EBUSY;
2897
2898 rdev->recovery_offset = recovery_start;
2899 if (recovery_start == MaxSector)
2900 set_bit(In_sync, &rdev->flags);
2901 else
2902 clear_bit(In_sync, &rdev->flags);
2903 return len;
2904}
2905
2906static struct rdev_sysfs_entry rdev_recovery_start =
2907__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2908
NeilBrown16c791a2011-07-28 11:31:47 +10002909static ssize_t
2910badblocks_show(struct badblocks *bb, char *page, int unack);
2911static ssize_t
2912badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2913
NeilBrown3cb03002011-10-11 16:45:26 +11002914static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002915{
2916 return badblocks_show(&rdev->badblocks, page, 0);
2917}
NeilBrown3cb03002011-10-11 16:45:26 +11002918static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002919{
NeilBrownde393cd2011-07-28 11:31:48 +10002920 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2921 /* Maybe that ack was all we needed */
2922 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2923 wake_up(&rdev->blocked_wait);
2924 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002925}
2926static struct rdev_sysfs_entry rdev_bad_blocks =
2927__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2928
NeilBrown3cb03002011-10-11 16:45:26 +11002929static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002930{
2931 return badblocks_show(&rdev->badblocks, page, 1);
2932}
NeilBrown3cb03002011-10-11 16:45:26 +11002933static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002934{
2935 return badblocks_store(&rdev->badblocks, page, len, 1);
2936}
2937static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2938__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2939
NeilBrown86e6ffd2005-11-08 21:39:24 -08002940static struct attribute *rdev_default_attrs[] = {
2941 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002942 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002943 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002944 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10002945 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002946 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002947 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002948 &rdev_bad_blocks.attr,
2949 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002950 NULL,
2951};
2952static ssize_t
2953rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2954{
2955 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002956 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002957
2958 if (!entry->show)
2959 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11002960 if (!rdev->mddev)
2961 return -EBUSY;
2962 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002963}
2964
2965static ssize_t
2966rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2967 const char *page, size_t length)
2968{
2969 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002970 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002971 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11002972 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002973
2974 if (!entry->store)
2975 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002976 if (!capable(CAP_SYS_ADMIN))
2977 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002978 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002979 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002980 if (rdev->mddev == NULL)
2981 rv = -EBUSY;
2982 else
2983 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002984 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002985 }
2986 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002987}
2988
2989static void rdev_free(struct kobject *ko)
2990{
NeilBrown3cb03002011-10-11 16:45:26 +11002991 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002992 kfree(rdev);
2993}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002994static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002995 .show = rdev_attr_show,
2996 .store = rdev_attr_store,
2997};
2998static struct kobj_type rdev_ktype = {
2999 .release = rdev_free,
3000 .sysfs_ops = &rdev_sysfs_ops,
3001 .default_attrs = rdev_default_attrs,
3002};
3003
NeilBrown3cb03002011-10-11 16:45:26 +11003004int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003005{
3006 rdev->desc_nr = -1;
3007 rdev->saved_raid_disk = -1;
3008 rdev->raid_disk = -1;
3009 rdev->flags = 0;
3010 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003011 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003012 rdev->sb_events = 0;
3013 rdev->last_read_error.tv_sec = 0;
3014 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003015 rdev->sb_loaded = 0;
3016 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003017 atomic_set(&rdev->nr_pending, 0);
3018 atomic_set(&rdev->read_errors, 0);
3019 atomic_set(&rdev->corrected_errors, 0);
3020
3021 INIT_LIST_HEAD(&rdev->same_set);
3022 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003023
3024 /* Add space to store bad block list.
3025 * This reserves the space even on arrays where it cannot
3026 * be used - I wonder if that matters
3027 */
3028 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003029 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003030 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3031 seqlock_init(&rdev->badblocks.lock);
3032 if (rdev->badblocks.page == NULL)
3033 return -ENOMEM;
3034
3035 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003036}
3037EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038/*
3039 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3040 *
3041 * mark the device faulty if:
3042 *
3043 * - the device is nonexistent (zero size)
3044 * - the device has no valid superblock
3045 *
3046 * a faulty rdev _never_ has rdev->sb set.
3047 */
NeilBrown3cb03002011-10-11 16:45:26 +11003048static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
3050 char b[BDEVNAME_SIZE];
3051 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003052 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 sector_t size;
3054
NeilBrown9ffae0c2006-01-06 00:20:32 -08003055 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 if (!rdev) {
3057 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3058 return ERR_PTR(-ENOMEM);
3059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
NeilBrown2230dfe2011-07-28 11:31:46 +10003061 err = md_rdev_init(rdev);
3062 if (err)
3063 goto abort_free;
3064 err = alloc_disk_sb(rdev);
3065 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 goto abort_free;
3067
NeilBrownc5d79ad2008-02-06 01:39:54 -08003068 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 if (err)
3070 goto abort_free;
3071
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003072 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003073
Mike Snitzer77304d22010-11-08 14:39:12 +01003074 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003076 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 "md: %s has zero or unknown size, marking faulty!\n",
3078 bdevname(rdev->bdev,b));
3079 err = -EINVAL;
3080 goto abort_free;
3081 }
3082
3083 if (super_format >= 0) {
3084 err = super_types[super_format].
3085 load_super(rdev, NULL, super_minor);
3086 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003087 printk(KERN_WARNING
3088 "md: %s does not have a valid v%d.%d "
3089 "superblock, not importing!\n",
3090 bdevname(rdev->bdev,b),
3091 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 goto abort_free;
3093 }
3094 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003095 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 "md: could not read %s's sb, not importing!\n",
3097 bdevname(rdev->bdev,b));
3098 goto abort_free;
3099 }
3100 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 return rdev;
3103
3104abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003105 if (rdev->bdev)
3106 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003107 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 kfree(rdev);
3109 return ERR_PTR(err);
3110}
3111
3112/*
3113 * Check a full RAID array for plausibility
3114 */
3115
NeilBrownf72ffdd2014-09-30 14:23:59 +10003116static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117{
3118 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003119 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 char b[BDEVNAME_SIZE];
3121
3122 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003123 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 switch (super_types[mddev->major_version].
3125 load_super(rdev, freshest, mddev->minor_version)) {
3126 case 1:
3127 freshest = rdev;
3128 break;
3129 case 0:
3130 break;
3131 default:
3132 printk( KERN_ERR \
3133 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003134 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 bdevname(rdev->bdev,b));
3136 kick_rdev_from_array(rdev);
3137 }
3138
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 super_types[mddev->major_version].
3140 validate_super(mddev, freshest);
3141
3142 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003143 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003144 if (mddev->max_disks &&
3145 (rdev->desc_nr >= mddev->max_disks ||
3146 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003147 printk(KERN_WARNING
3148 "md: %s: %s: only %d devices permitted\n",
3149 mdname(mddev), bdevname(rdev->bdev, b),
3150 mddev->max_disks);
3151 kick_rdev_from_array(rdev);
3152 continue;
3153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 if (rdev != freshest)
3155 if (super_types[mddev->major_version].
3156 validate_super(mddev, rdev)) {
3157 printk(KERN_WARNING "md: kicking non-fresh %s"
3158 " from array!\n",
3159 bdevname(rdev->bdev,b));
3160 kick_rdev_from_array(rdev);
3161 continue;
3162 }
3163 if (mddev->level == LEVEL_MULTIPATH) {
3164 rdev->desc_nr = i++;
3165 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003166 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003167 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003168 rdev->raid_disk = -1;
3169 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 }
3171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172}
3173
NeilBrown72e02072009-12-14 12:49:55 +11003174/* Read a fixed-point number.
3175 * Numbers in sysfs attributes should be in "standard" units where
3176 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003177 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003178 * milliseconds or jiffies.
3179 * This function takes a decimal number with a possible fractional
3180 * component, and produces an integer which is the result of
3181 * multiplying that number by 10^'scale'.
3182 * all without any floating-point arithmetic.
3183 */
3184int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3185{
3186 unsigned long result = 0;
3187 long decimals = -1;
3188 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3189 if (*cp == '.')
3190 decimals = 0;
3191 else if (decimals < scale) {
3192 unsigned int value;
3193 value = *cp - '0';
3194 result = result * 10 + value;
3195 if (decimals >= 0)
3196 decimals++;
3197 }
3198 cp++;
3199 }
3200 if (*cp == '\n')
3201 cp++;
3202 if (*cp)
3203 return -EINVAL;
3204 if (decimals < 0)
3205 decimals = 0;
3206 while (decimals < scale) {
3207 result *= 10;
3208 decimals ++;
3209 }
3210 *res = result;
3211 return 0;
3212}
3213
NeilBrown19052c02008-08-05 15:54:13 +10003214static void md_safemode_timeout(unsigned long data);
3215
NeilBrowneae17012005-11-08 21:39:23 -08003216static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003217safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003218{
3219 int msec = (mddev->safemode_delay*1000)/HZ;
3220 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3221}
3222static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003223safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003224{
NeilBrown16f17b32006-06-26 00:27:37 -07003225 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003226
NeilBrown72e02072009-12-14 12:49:55 +11003227 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003228 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003229 if (msec == 0)
3230 mddev->safemode_delay = 0;
3231 else {
NeilBrown19052c02008-08-05 15:54:13 +10003232 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003233 unsigned long new_delay = (msec*HZ)/1000;
3234
3235 if (new_delay == 0)
3236 new_delay = 1;
3237 mddev->safemode_delay = new_delay;
3238 if (new_delay < old_delay || old_delay == 0)
3239 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003240 }
3241 return len;
3242}
3243static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003244__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003245
3246static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003247level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003248{
NeilBrown36d091f2014-12-15 12:56:58 +11003249 struct md_personality *p;
3250 int ret;
3251 spin_lock(&mddev->lock);
3252 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003253 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003254 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003255 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003256 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003257 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003258 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003259 else
NeilBrown36d091f2014-12-15 12:56:58 +11003260 ret = 0;
3261 spin_unlock(&mddev->lock);
3262 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003263}
3264
NeilBrownd9d166c2006-01-06 00:20:51 -08003265static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003266level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003267{
Dan Williamsf2859af2010-05-02 10:04:16 -07003268 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003269 ssize_t rv;
3270 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003271 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003272 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003273 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003274 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003275
NeilBrown67918752014-12-15 12:57:01 +11003276 if (slen == 0 || slen >= sizeof(clevel))
3277 return -EINVAL;
3278
3279 rv = mddev_lock(mddev);
3280 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003281 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003282
3283 if (mddev->pers == NULL) {
3284 strncpy(mddev->clevel, buf, slen);
3285 if (mddev->clevel[slen-1] == '\n')
3286 slen--;
3287 mddev->clevel[slen] = 0;
3288 mddev->level = LEVEL_NONE;
3289 rv = len;
3290 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003291 }
NeilBrown67918752014-12-15 12:57:01 +11003292 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003293 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003294 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003295
3296 /* request to change the personality. Need to ensure:
3297 * - array is not engaged in resync/recovery/reshape
3298 * - old personality can be suspended
3299 * - new personality will access other array.
3300 */
3301
NeilBrown67918752014-12-15 12:57:01 +11003302 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003303 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003304 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003305 mddev->reshape_position != MaxSector ||
3306 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003307 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003308
NeilBrown67918752014-12-15 12:57:01 +11003309 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003310 if (!mddev->pers->quiesce) {
3311 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3312 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003313 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003314 }
3315
3316 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003317 strncpy(clevel, buf, slen);
3318 if (clevel[slen-1] == '\n')
3319 slen--;
3320 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003321 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003322 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003323
Dan Williamsf2859af2010-05-02 10:04:16 -07003324 if (request_module("md-%s", clevel) != 0)
3325 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003326 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003327 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003328 if (!pers || !try_module_get(pers->owner)) {
3329 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003330 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003331 rv = -EINVAL;
3332 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003333 }
3334 spin_unlock(&pers_lock);
3335
3336 if (pers == mddev->pers) {
3337 /* Nothing to do! */
3338 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003339 rv = len;
3340 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003341 }
3342 if (!pers->takeover) {
3343 module_put(pers->owner);
3344 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003345 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003346 rv = -EINVAL;
3347 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003348 }
3349
NeilBrowndafb20f2012-03-19 12:46:39 +11003350 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003351 rdev->new_raid_disk = rdev->raid_disk;
3352
NeilBrown245f46c2009-03-31 14:39:39 +11003353 /* ->takeover must set new_* and/or delta_disks
3354 * if it succeeds, and may set them when it fails.
3355 */
3356 priv = pers->takeover(mddev);
3357 if (IS_ERR(priv)) {
3358 mddev->new_level = mddev->level;
3359 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003360 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003361 mddev->raid_disks -= mddev->delta_disks;
3362 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003363 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003364 module_put(pers->owner);
3365 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003366 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003367 rv = PTR_ERR(priv);
3368 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003369 }
3370
3371 /* Looks like we have a winner */
3372 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003373 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003374
3375 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003376 oldpers = mddev->pers;
3377 oldpriv = mddev->private;
3378 mddev->pers = pers;
3379 mddev->private = priv;
3380 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3381 mddev->level = mddev->new_level;
3382 mddev->layout = mddev->new_layout;
3383 mddev->chunk_sectors = mddev->new_chunk_sectors;
3384 mddev->delta_disks = 0;
3385 mddev->reshape_backwards = 0;
3386 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003387 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003388
NeilBrowndb721d32014-12-15 12:56:58 +11003389 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003390 mddev->external) {
3391 /* We are converting from a no-redundancy array
3392 * to a redundancy array and metadata is managed
3393 * externally so we need to be sure that writes
3394 * won't block due to a need to transition
3395 * clean->dirty
3396 * until external management is started.
3397 */
3398 mddev->in_sync = 0;
3399 mddev->safemode_delay = 0;
3400 mddev->safemode = 0;
3401 }
3402
NeilBrowndb721d32014-12-15 12:56:58 +11003403 oldpers->free(mddev, oldpriv);
3404
3405 if (oldpers->sync_request == NULL &&
3406 pers->sync_request != NULL) {
3407 /* need to add the md_redundancy_group */
3408 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3409 printk(KERN_WARNING
3410 "md: cannot register extra attributes for %s\n",
3411 mdname(mddev));
3412 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3413 }
3414 if (oldpers->sync_request != NULL &&
3415 pers->sync_request == NULL) {
3416 /* need to remove the md_redundancy_group */
3417 if (mddev->to_remove == NULL)
3418 mddev->to_remove = &md_redundancy_group;
3419 }
3420
NeilBrowndafb20f2012-03-19 12:46:39 +11003421 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003422 if (rdev->raid_disk < 0)
3423 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003424 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003425 rdev->new_raid_disk = -1;
3426 if (rdev->new_raid_disk == rdev->raid_disk)
3427 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003428 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003429 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003430 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003431 if (rdev->raid_disk < 0)
3432 continue;
3433 if (rdev->new_raid_disk == rdev->raid_disk)
3434 continue;
3435 rdev->raid_disk = rdev->new_raid_disk;
3436 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003437 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003438 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003439 if (sysfs_link_rdev(mddev, rdev))
3440 printk(KERN_WARNING "md: cannot register rd%d"
3441 " for %s after level change\n",
3442 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003443 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003444 }
3445
NeilBrowndb721d32014-12-15 12:56:58 +11003446 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003447 /* this is now an array without redundancy, so
3448 * it must always be in_sync
3449 */
3450 mddev->in_sync = 1;
3451 del_timer_sync(&mddev->safemode_timer);
3452 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003453 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003454 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003455 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003456 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003457 if (!mddev->thread)
3458 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003459 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003460 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003461 rv = len;
3462out_unlock:
3463 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003464 return rv;
3465}
3466
3467static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003468__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003469
NeilBrownd4dbd022006-06-26 00:27:59 -07003470static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003471layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003472{
3473 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003474 if (mddev->reshape_position != MaxSector &&
3475 mddev->layout != mddev->new_layout)
3476 return sprintf(page, "%d (%d)\n",
3477 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003478 return sprintf(page, "%d\n", mddev->layout);
3479}
3480
3481static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003482layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003483{
3484 char *e;
3485 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11003486 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003487
3488 if (!*buf || (*e && *e != '\n'))
3489 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11003490 err = mddev_lock(mddev);
3491 if (err)
3492 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003493
NeilBrownb3546032009-03-31 14:56:41 +11003494 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003495 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003496 err = -EBUSY;
3497 else if (mddev->ro)
3498 err = -EROFS;
3499 else {
3500 mddev->new_layout = n;
3501 err = mddev->pers->check_reshape(mddev);
3502 if (err)
3503 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003504 }
NeilBrownb3546032009-03-31 14:56:41 +11003505 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003506 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003507 if (mddev->reshape_position == MaxSector)
3508 mddev->layout = n;
3509 }
NeilBrown67918752014-12-15 12:57:01 +11003510 mddev_unlock(mddev);
3511 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003512}
3513static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003514__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003515
NeilBrowneae17012005-11-08 21:39:23 -08003516static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003517raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003518{
NeilBrownbb636542005-11-08 21:39:45 -08003519 if (mddev->raid_disks == 0)
3520 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003521 if (mddev->reshape_position != MaxSector &&
3522 mddev->delta_disks != 0)
3523 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3524 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003525 return sprintf(page, "%d\n", mddev->raid_disks);
3526}
3527
NeilBrownfd01b882011-10-11 16:47:53 +11003528static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003529
3530static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003531raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003532{
NeilBrownda943b992006-01-06 00:20:54 -08003533 char *e;
NeilBrown67918752014-12-15 12:57:01 +11003534 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003535 unsigned long n = simple_strtoul(buf, &e, 10);
3536
3537 if (!*buf || (*e && *e != '\n'))
3538 return -EINVAL;
3539
NeilBrown67918752014-12-15 12:57:01 +11003540 err = mddev_lock(mddev);
3541 if (err)
3542 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003543 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003544 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003545 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003546 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003547 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003548
NeilBrown67918752014-12-15 12:57:01 +11003549 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003550 rdev_for_each(rdev, mddev) {
3551 if (olddisks < n &&
3552 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003553 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003554 if (olddisks > n &&
3555 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003556 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003557 }
NeilBrown67918752014-12-15 12:57:01 +11003558 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003559 mddev->delta_disks = n - olddisks;
3560 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003561 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003562 } else
NeilBrownda943b992006-01-06 00:20:54 -08003563 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003564out_unlock:
3565 mddev_unlock(mddev);
3566 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003567}
3568static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003569__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003570
NeilBrown24dd4692005-11-08 21:39:26 -08003571static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003572chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003573{
NeilBrown08a02ec2007-05-09 02:35:38 -07003574 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003575 mddev->chunk_sectors != mddev->new_chunk_sectors)
3576 return sprintf(page, "%d (%d)\n",
3577 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003578 mddev->chunk_sectors << 9);
3579 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003580}
3581
3582static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003583chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003584{
NeilBrown67918752014-12-15 12:57:01 +11003585 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003586 char *e;
3587 unsigned long n = simple_strtoul(buf, &e, 10);
3588
NeilBrown3b343802006-01-06 00:20:47 -08003589 if (!*buf || (*e && *e != '\n'))
3590 return -EINVAL;
3591
NeilBrown67918752014-12-15 12:57:01 +11003592 err = mddev_lock(mddev);
3593 if (err)
3594 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003595 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003596 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003597 err = -EBUSY;
3598 else if (mddev->ro)
3599 err = -EROFS;
3600 else {
3601 mddev->new_chunk_sectors = n >> 9;
3602 err = mddev->pers->check_reshape(mddev);
3603 if (err)
3604 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003605 }
NeilBrownb3546032009-03-31 14:56:41 +11003606 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003607 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003608 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003609 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003610 }
NeilBrown67918752014-12-15 12:57:01 +11003611 mddev_unlock(mddev);
3612 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003613}
3614static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003615__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003616
NeilBrowna94213b2006-06-26 00:28:00 -07003617static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003618resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003619{
NeilBrownd1a7c502009-03-31 15:24:32 +11003620 if (mddev->recovery_cp == MaxSector)
3621 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003622 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3623}
3624
3625static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003626resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003627{
NeilBrown67918752014-12-15 12:57:01 +11003628 int err;
NeilBrowna94213b2006-06-26 00:28:00 -07003629 char *e;
3630 unsigned long long n = simple_strtoull(buf, &e, 10);
3631
NeilBrown67918752014-12-15 12:57:01 +11003632 err = mddev_lock(mddev);
3633 if (err)
3634 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003635 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003636 err = -EBUSY;
3637 else if (cmd_match(buf, "none"))
Dan Williams06e3c812009-12-12 21:17:12 -07003638 n = MaxSector;
3639 else if (!*buf || (*e && *e != '\n'))
NeilBrown67918752014-12-15 12:57:01 +11003640 err = -EINVAL;
NeilBrowna94213b2006-06-26 00:28:00 -07003641
NeilBrown67918752014-12-15 12:57:01 +11003642 if (!err) {
3643 mddev->recovery_cp = n;
3644 if (mddev->pers)
3645 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3646 }
3647 mddev_unlock(mddev);
3648 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003649}
3650static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003651__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003652
NeilBrown9e653b62006-06-26 00:27:58 -07003653/*
3654 * The array state can be:
3655 *
3656 * clear
3657 * No devices, no size, no level
3658 * Equivalent to STOP_ARRAY ioctl
3659 * inactive
3660 * May have some settings, but array is not active
3661 * all IO results in error
3662 * When written, doesn't tear down array, but just stops it
3663 * suspended (not supported yet)
3664 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003665 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003666 * readonly
3667 * no resync can happen. no superblocks get written.
3668 * write requests fail
3669 * read-auto
3670 * like readonly, but behaves like 'clean' on a write request.
3671 *
3672 * clean - no pending writes, but otherwise active.
3673 * When written to inactive array, starts without resync
3674 * If a write request arrives then
3675 * if metadata is known, mark 'dirty' and switch to 'active'.
3676 * if not known, block and switch to write-pending
3677 * If written to an active array that has pending writes, then fails.
3678 * active
3679 * fully active: IO and resync can be happening.
3680 * When written to inactive array, starts with resync
3681 *
3682 * write-pending
3683 * clean, but writes are blocked waiting for 'active' to be written.
3684 *
3685 * active-idle
3686 * like active, but no writes have been seen for a while (100msec).
3687 *
3688 */
3689enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3690 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003691static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003692 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3693 "write-pending", "active-idle", NULL };
3694
3695static int match_word(const char *word, char **list)
3696{
3697 int n;
3698 for (n=0; list[n]; n++)
3699 if (cmd_match(word, list[n]))
3700 break;
3701 return n;
3702}
3703
3704static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003705array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003706{
3707 enum array_state st = inactive;
3708
3709 if (mddev->pers)
3710 switch(mddev->ro) {
3711 case 1:
3712 st = readonly;
3713 break;
3714 case 2:
3715 st = read_auto;
3716 break;
3717 case 0:
3718 if (mddev->in_sync)
3719 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003720 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003721 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003722 else if (mddev->safemode)
3723 st = active_idle;
3724 else
3725 st = active;
3726 }
3727 else {
3728 if (list_empty(&mddev->disks) &&
3729 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003730 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003731 st = clear;
3732 else
3733 st = inactive;
3734 }
3735 return sprintf(page, "%s\n", array_states[st]);
3736}
3737
NeilBrownf72ffdd2014-09-30 14:23:59 +10003738static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3739static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3740static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003741static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003742
3743static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003744array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003745{
NeilBrown67918752014-12-15 12:57:01 +11003746 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003747 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003748
3749 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3750 /* don't take reconfig_mutex when toggling between
3751 * clean and active
3752 */
3753 spin_lock(&mddev->lock);
3754 if (st == active) {
3755 restart_array(mddev);
3756 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3757 wake_up(&mddev->sb_wait);
3758 err = 0;
3759 } else /* st == clean */ {
3760 restart_array(mddev);
3761 if (atomic_read(&mddev->writes_pending) == 0) {
3762 if (mddev->in_sync == 0) {
3763 mddev->in_sync = 1;
3764 if (mddev->safemode == 1)
3765 mddev->safemode = 0;
3766 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3767 }
3768 err = 0;
3769 } else
3770 err = -EBUSY;
3771 }
3772 spin_unlock(&mddev->lock);
3773 return err;
3774 }
3775 err = mddev_lock(mddev);
3776 if (err)
3777 return err;
3778 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003779 switch(st) {
3780 case bad_word:
3781 break;
3782 case clear:
3783 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003784 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003785 break;
3786 case inactive:
3787 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003788 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003789 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003790 else
NeilBrowne6910632008-02-06 01:39:51 -08003791 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003792 break;
3793 case suspended:
3794 break; /* not supported yet */
3795 case readonly:
3796 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003797 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003798 else {
3799 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003800 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003801 err = do_md_run(mddev);
3802 }
3803 break;
3804 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003805 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003806 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003807 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003808 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003809 err = restart_array(mddev);
3810 if (err == 0) {
3811 mddev->ro = 2;
3812 set_disk_ro(mddev->gendisk, 0);
3813 }
NeilBrown9e653b62006-06-26 00:27:58 -07003814 } else {
3815 mddev->ro = 2;
3816 err = do_md_run(mddev);
3817 }
3818 break;
3819 case clean:
3820 if (mddev->pers) {
3821 restart_array(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11003822 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003823 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003824 if (mddev->in_sync == 0) {
3825 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003826 if (mddev->safemode == 1)
3827 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003828 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003829 }
3830 err = 0;
3831 } else
3832 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11003833 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003834 } else
3835 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003836 break;
3837 case active:
3838 if (mddev->pers) {
3839 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003840 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003841 wake_up(&mddev->sb_wait);
3842 err = 0;
3843 } else {
3844 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003845 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003846 err = do_md_run(mddev);
3847 }
3848 break;
3849 case write_pending:
3850 case active_idle:
3851 /* these cannot be set */
3852 break;
3853 }
NeilBrown67918752014-12-15 12:57:01 +11003854
3855 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11003856 if (mddev->hold_active == UNTIL_IOCTL)
3857 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003858 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10003859 }
NeilBrown67918752014-12-15 12:57:01 +11003860 mddev_unlock(mddev);
3861 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07003862}
NeilBrown80ca3a42006-07-10 04:44:18 -07003863static struct md_sysfs_entry md_array_state =
3864__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003865
NeilBrown6d7ff7382006-01-06 00:21:16 -08003866static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003867max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003868 return sprintf(page, "%d\n",
3869 atomic_read(&mddev->max_corr_read_errors));
3870}
3871
3872static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003873max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003874{
3875 char *e;
3876 unsigned long n = simple_strtoul(buf, &e, 10);
3877
3878 if (*buf && (*e == 0 || *e == '\n')) {
3879 atomic_set(&mddev->max_corr_read_errors, n);
3880 return len;
3881 }
3882 return -EINVAL;
3883}
3884
3885static struct md_sysfs_entry max_corr_read_errors =
3886__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3887 max_corrected_read_errors_store);
3888
3889static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003890null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003891{
3892 return -EINVAL;
3893}
3894
3895static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003896new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003897{
3898 /* buf must be %d:%d\n? giving major and minor numbers */
3899 /* The new device is added to the array.
3900 * If the array has a persistent superblock, we read the
3901 * superblock to initialise info and check validity.
3902 * Otherwise, only checking done is that in bind_rdev_to_array,
3903 * which mainly checks size.
3904 */
3905 char *e;
3906 int major = simple_strtoul(buf, &e, 10);
3907 int minor;
3908 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003909 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003910 int err;
3911
3912 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3913 return -EINVAL;
3914 minor = simple_strtoul(e+1, &e, 10);
3915 if (*e && *e != '\n')
3916 return -EINVAL;
3917 dev = MKDEV(major, minor);
3918 if (major != MAJOR(dev) ||
3919 minor != MINOR(dev))
3920 return -EOVERFLOW;
3921
NeilBrown67918752014-12-15 12:57:01 +11003922 flush_workqueue(md_misc_wq);
3923
3924 err = mddev_lock(mddev);
3925 if (err)
3926 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003927 if (mddev->persistent) {
3928 rdev = md_import_device(dev, mddev->major_version,
3929 mddev->minor_version);
3930 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003931 struct md_rdev *rdev0
3932 = list_entry(mddev->disks.next,
3933 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08003934 err = super_types[mddev->major_version]
3935 .load_super(rdev, rdev0, mddev->minor_version);
3936 if (err < 0)
3937 goto out;
3938 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003939 } else if (mddev->external)
3940 rdev = md_import_device(dev, -2, -1);
3941 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003942 rdev = md_import_device(dev, -1, -1);
3943
3944 if (IS_ERR(rdev))
3945 return PTR_ERR(rdev);
3946 err = bind_rdev_to_array(rdev, mddev);
3947 out:
3948 if (err)
3949 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11003950 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08003951 return err ? err : len;
3952}
3953
3954static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003955__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003956
3957static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003958bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07003959{
3960 char *end;
3961 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11003962 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07003963
NeilBrown67918752014-12-15 12:57:01 +11003964 err = mddev_lock(mddev);
3965 if (err)
3966 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07003967 if (!mddev->bitmap)
3968 goto out;
3969 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3970 while (*buf) {
3971 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3972 if (buf == end) break;
3973 if (*end == '-') { /* range */
3974 buf = end + 1;
3975 end_chunk = simple_strtoul(buf, &end, 0);
3976 if (buf == end) break;
3977 }
3978 if (*end && !isspace(*end)) break;
3979 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003980 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003981 }
3982 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3983out:
NeilBrown67918752014-12-15 12:57:01 +11003984 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003985 return len;
3986}
3987
3988static struct md_sysfs_entry md_bitmap =
3989__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3990
3991static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003992size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08003993{
Andre Noll58c0fed2009-03-31 14:33:13 +11003994 return sprintf(page, "%llu\n",
3995 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003996}
3997
NeilBrownfd01b882011-10-11 16:47:53 +11003998static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003999
4000static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004001size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004002{
4003 /* If array is inactive, we can reduce the component size, but
4004 * not increase it (except from 0).
4005 * If array is active, we can try an on-line resize
4006 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004007 sector_t sectors;
4008 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004009
Andre Noll58c0fed2009-03-31 14:33:13 +11004010 if (err < 0)
4011 return err;
NeilBrown67918752014-12-15 12:57:01 +11004012 err = mddev_lock(mddev);
4013 if (err)
4014 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004015 if (mddev->pers) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05004016 if (mddev_is_clustered(mddev))
4017 md_cluster_ops->metadata_update_start(mddev);
Andre Noll58c0fed2009-03-31 14:33:13 +11004018 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004019 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05004020 if (mddev_is_clustered(mddev))
4021 md_cluster_ops->metadata_update_finish(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004022 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004023 if (mddev->dev_sectors == 0 ||
4024 mddev->dev_sectors > sectors)
4025 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004026 else
4027 err = -ENOSPC;
4028 }
NeilBrown67918752014-12-15 12:57:01 +11004029 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004030 return err ? err : len;
4031}
4032
4033static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004034__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004035
Masanari Iida83f0d772012-10-30 00:18:08 +09004036/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004037 * This is one of
4038 * 'none' for arrays with no metadata (good luck...)
4039 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004040 * or N.M for internally known formats
4041 */
4042static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004043metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004044{
4045 if (mddev->persistent)
4046 return sprintf(page, "%d.%d\n",
4047 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004048 else if (mddev->external)
4049 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004050 else
4051 return sprintf(page, "none\n");
4052}
4053
4054static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004055metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004056{
4057 int major, minor;
4058 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004059 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004060 /* Changing the details of 'external' metadata is
4061 * always permitted. Otherwise there must be
4062 * no devices attached to the array.
4063 */
NeilBrown67918752014-12-15 12:57:01 +11004064
4065 err = mddev_lock(mddev);
4066 if (err)
4067 return err;
4068 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004069 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4070 ;
4071 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004072 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004073
NeilBrown67918752014-12-15 12:57:01 +11004074 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004075 if (cmd_match(buf, "none")) {
4076 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004077 mddev->external = 0;
4078 mddev->major_version = 0;
4079 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004080 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004081 }
4082 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004083 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004084 if (namelen >= sizeof(mddev->metadata_type))
4085 namelen = sizeof(mddev->metadata_type)-1;
4086 strncpy(mddev->metadata_type, buf+9, namelen);
4087 mddev->metadata_type[namelen] = 0;
4088 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4089 mddev->metadata_type[--namelen] = 0;
4090 mddev->persistent = 0;
4091 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004092 mddev->major_version = 0;
4093 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004094 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004095 }
4096 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004097 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004098 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004099 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004100 buf = e+1;
4101 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004102 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004103 goto out_unlock;
4104 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004105 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004106 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004107 mddev->major_version = major;
4108 mddev->minor_version = minor;
4109 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004110 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004111 err = 0;
4112out_unlock:
4113 mddev_unlock(mddev);
4114 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004115}
4116
4117static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004118__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004119
NeilBrowna35b0d62006-01-06 00:20:49 -08004120static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004121action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004122{
NeilBrown7eec3142005-11-08 21:39:44 -08004123 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004124 unsigned long recovery = mddev->recovery;
4125 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004126 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004127 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4128 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4129 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004130 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004131 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4132 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004133 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004134 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004135 type = "check";
4136 else
4137 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004138 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004139 type = "recover";
4140 }
4141 return sprintf(page, "%s\n", type);
4142}
4143
4144static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004145action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004146{
NeilBrown7eec3142005-11-08 21:39:44 -08004147 if (!mddev->pers || !mddev->pers->sync_request)
4148 return -EINVAL;
4149
NeilBrownb6a9ce62009-05-26 09:41:17 +10004150 if (cmd_match(page, "frozen"))
4151 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4152 else
4153 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4154
4155 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrownf851b602014-12-11 10:02:10 +11004156 flush_workqueue(md_misc_wq);
NeilBrown7eec3142005-11-08 21:39:44 -08004157 if (mddev->sync_thread) {
4158 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004159 if (mddev_lock(mddev) == 0) {
4160 md_reap_sync_thread(mddev);
4161 mddev_unlock(mddev);
4162 }
NeilBrown7eec3142005-11-08 21:39:44 -08004163 }
NeilBrown03c902e2006-01-06 00:20:46 -08004164 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4165 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004166 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004167 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004168 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004169 else if (cmd_match(page, "recover")) {
4170 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4171 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4172 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004173 int err;
4174 if (mddev->pers->start_reshape == NULL)
4175 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004176 err = mddev_lock(mddev);
4177 if (!err) {
4178 err = mddev->pers->start_reshape(mddev);
4179 mddev_unlock(mddev);
4180 }
NeilBrown16484bf2006-03-27 01:18:13 -08004181 if (err)
4182 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004183 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004184 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004185 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004186 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004187 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004188 return -EINVAL;
4189 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4190 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004191 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004192 if (mddev->ro == 2) {
4193 /* A write to sync_action is enough to justify
4194 * canceling read-auto mode
4195 */
4196 mddev->ro = 0;
4197 md_wakeup_thread(mddev->sync_thread);
4198 }
NeilBrown03c902e2006-01-06 00:20:46 -08004199 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004200 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004201 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004202 return len;
4203}
4204
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004205static struct md_sysfs_entry md_scan_mode =
4206__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4207
4208static ssize_t
4209last_sync_action_show(struct mddev *mddev, char *page)
4210{
4211 return sprintf(page, "%s\n", mddev->last_sync_action);
4212}
4213
4214static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4215
NeilBrown9d888832005-11-08 21:39:26 -08004216static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004217mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004218{
4219 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004220 (unsigned long long)
4221 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004222}
4223
NeilBrown80ca3a42006-07-10 04:44:18 -07004224static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004225
NeilBrown88202a02006-01-06 00:21:36 -08004226static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004227sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004228{
4229 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4230 mddev->sync_speed_min ? "local": "system");
4231}
4232
4233static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004234sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004235{
4236 int min;
4237 char *e;
4238 if (strncmp(buf, "system", 6)==0) {
4239 mddev->sync_speed_min = 0;
4240 return len;
4241 }
4242 min = simple_strtoul(buf, &e, 10);
4243 if (buf == e || (*e && *e != '\n') || min <= 0)
4244 return -EINVAL;
4245 mddev->sync_speed_min = min;
4246 return len;
4247}
4248
4249static struct md_sysfs_entry md_sync_min =
4250__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4251
4252static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004253sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004254{
4255 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4256 mddev->sync_speed_max ? "local": "system");
4257}
4258
4259static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004260sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004261{
4262 int max;
4263 char *e;
4264 if (strncmp(buf, "system", 6)==0) {
4265 mddev->sync_speed_max = 0;
4266 return len;
4267 }
4268 max = simple_strtoul(buf, &e, 10);
4269 if (buf == e || (*e && *e != '\n') || max <= 0)
4270 return -EINVAL;
4271 mddev->sync_speed_max = max;
4272 return len;
4273}
4274
4275static struct md_sysfs_entry md_sync_max =
4276__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4277
Iustin Popd7f3d292007-10-16 23:30:54 -07004278static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004279degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004280{
4281 return sprintf(page, "%d\n", mddev->degraded);
4282}
4283static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004284
4285static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004286sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004287{
4288 return sprintf(page, "%d\n", mddev->parallel_resync);
4289}
4290
4291static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004292sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004293{
4294 long n;
4295
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004296 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004297 return -EINVAL;
4298
4299 if (n != 0 && n != 1)
4300 return -EINVAL;
4301
4302 mddev->parallel_resync = n;
4303
4304 if (mddev->sync_thread)
4305 wake_up(&resync_wait);
4306
4307 return len;
4308}
4309
4310/* force parallel resync, even with shared block devices */
4311static struct md_sysfs_entry md_sync_force_parallel =
4312__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4313 sync_force_parallel_show, sync_force_parallel_store);
4314
4315static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004316sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004317{
4318 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004319 if (mddev->curr_resync == 0)
4320 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004321 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4322 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004323 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004324 db = resync - mddev->resync_mark_cnt;
4325 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004326}
4327
NeilBrown80ca3a42006-07-10 04:44:18 -07004328static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004329
4330static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004331sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004332{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004333 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004334
NeilBrownacb180b2009-04-14 16:28:34 +10004335 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4336 return sprintf(page, "none\n");
4337
NeilBrown72f36d52012-10-11 14:25:57 +11004338 if (mddev->curr_resync == 1 ||
4339 mddev->curr_resync == 2)
4340 return sprintf(page, "delayed\n");
4341
NeilBrownc804cde2012-05-21 09:28:33 +10004342 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4343 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004344 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004345 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004346 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004347
NeilBrownacb180b2009-04-14 16:28:34 +10004348 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004349 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004350}
4351
NeilBrown80ca3a42006-07-10 04:44:18 -07004352static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004353
NeilBrowne464eaf2006-03-27 01:18:14 -08004354static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004355min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004356{
4357 return sprintf(page, "%llu\n",
4358 (unsigned long long)mddev->resync_min);
4359}
4360static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004361min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004362{
4363 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004364 int err;
4365 int chunk;
4366
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004367 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004368 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004369
4370 spin_lock(&mddev->lock);
4371 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004372 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004373 goto out_unlock;
4374
4375 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004376 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004377 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004378
4379 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004380 chunk = mddev->chunk_sectors;
4381 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004382 sector_t temp = min;
NeilBrown23da4222014-12-15 12:57:01 +11004383
4384 err = -EINVAL;
4385 if (sector_div(temp, chunk))
4386 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004387 }
4388 mddev->resync_min = min;
NeilBrown23da4222014-12-15 12:57:01 +11004389 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004390
NeilBrown23da4222014-12-15 12:57:01 +11004391out_unlock:
4392 spin_unlock(&mddev->lock);
4393 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004394}
4395
4396static struct md_sysfs_entry md_min_sync =
4397__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4398
4399static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004400max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004401{
4402 if (mddev->resync_max == MaxSector)
4403 return sprintf(page, "max\n");
4404 else
4405 return sprintf(page, "%llu\n",
4406 (unsigned long long)mddev->resync_max);
4407}
4408static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004409max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004410{
NeilBrown23da4222014-12-15 12:57:01 +11004411 int err;
4412 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004413 if (strncmp(buf, "max", 3) == 0)
4414 mddev->resync_max = MaxSector;
4415 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004416 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004417 int chunk;
4418
4419 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004420 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004421 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004422 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004423 goto out_unlock;
4424
4425 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004426 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004427 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004428 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004429 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004430
4431 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004432 chunk = mddev->chunk_sectors;
4433 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004434 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004435
4436 err = -EINVAL;
4437 if (sector_div(temp, chunk))
4438 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004439 }
4440 mddev->resync_max = max;
4441 }
4442 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004443 err = 0;
4444out_unlock:
4445 spin_unlock(&mddev->lock);
4446 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004447}
4448
4449static struct md_sysfs_entry md_max_sync =
4450__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4451
4452static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004453suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004454{
4455 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4456}
4457
4458static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004459suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004460{
4461 char *e;
4462 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004463 unsigned long long old;
4464 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004465
NeilBrowne464eaf2006-03-27 01:18:14 -08004466 if (buf == e || (*e && *e != '\n'))
4467 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004468
NeilBrown67918752014-12-15 12:57:01 +11004469 err = mddev_lock(mddev);
4470 if (err)
4471 return err;
4472 err = -EINVAL;
4473 if (mddev->pers == NULL ||
4474 mddev->pers->quiesce == NULL)
4475 goto unlock;
4476 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004477 mddev->suspend_lo = new;
4478 if (new >= old)
4479 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004480 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004481 else {
4482 /* Expanding suspended region - need to wait */
4483 mddev->pers->quiesce(mddev, 1);
4484 mddev->pers->quiesce(mddev, 0);
4485 }
NeilBrown67918752014-12-15 12:57:01 +11004486 err = 0;
4487unlock:
4488 mddev_unlock(mddev);
4489 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004490}
4491static struct md_sysfs_entry md_suspend_lo =
4492__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4493
NeilBrowne464eaf2006-03-27 01:18:14 -08004494static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004495suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004496{
4497 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4498}
4499
4500static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004501suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004502{
4503 char *e;
4504 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004505 unsigned long long old;
4506 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004507
NeilBrowne464eaf2006-03-27 01:18:14 -08004508 if (buf == e || (*e && *e != '\n'))
4509 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004510
NeilBrown67918752014-12-15 12:57:01 +11004511 err = mddev_lock(mddev);
4512 if (err)
4513 return err;
4514 err = -EINVAL;
4515 if (mddev->pers == NULL ||
4516 mddev->pers->quiesce == NULL)
4517 goto unlock;
4518 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004519 mddev->suspend_hi = new;
4520 if (new <= old)
4521 /* Shrinking suspended region */
4522 mddev->pers->quiesce(mddev, 2);
4523 else {
4524 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004525 mddev->pers->quiesce(mddev, 1);
4526 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004527 }
NeilBrown67918752014-12-15 12:57:01 +11004528 err = 0;
4529unlock:
4530 mddev_unlock(mddev);
4531 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004532}
4533static struct md_sysfs_entry md_suspend_hi =
4534__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4535
NeilBrown08a02ec2007-05-09 02:35:38 -07004536static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004537reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004538{
4539 if (mddev->reshape_position != MaxSector)
4540 return sprintf(page, "%llu\n",
4541 (unsigned long long)mddev->reshape_position);
4542 strcpy(page, "none\n");
4543 return 5;
4544}
4545
4546static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004547reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004548{
NeilBrownc6563a82012-05-21 09:27:00 +10004549 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004550 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004551 int err;
NeilBrown08a02ec2007-05-09 02:35:38 -07004552 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004553
NeilBrown08a02ec2007-05-09 02:35:38 -07004554 if (buf == e || (*e && *e != '\n'))
4555 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004556 err = mddev_lock(mddev);
4557 if (err)
4558 return err;
4559 err = -EBUSY;
4560 if (mddev->pers)
4561 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004562 mddev->reshape_position = new;
4563 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004564 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004565 mddev->new_level = mddev->level;
4566 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004567 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004568 rdev_for_each(rdev, mddev)
4569 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004570 err = 0;
4571unlock:
4572 mddev_unlock(mddev);
4573 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004574}
4575
4576static struct md_sysfs_entry md_reshape_position =
4577__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4578 reshape_position_store);
4579
Dan Williamsb522adc2009-03-31 15:00:31 +11004580static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004581reshape_direction_show(struct mddev *mddev, char *page)
4582{
4583 return sprintf(page, "%s\n",
4584 mddev->reshape_backwards ? "backwards" : "forwards");
4585}
4586
4587static ssize_t
4588reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4589{
4590 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004591 int err;
4592
NeilBrown2c810cd2012-05-21 09:27:00 +10004593 if (cmd_match(buf, "forwards"))
4594 backwards = 0;
4595 else if (cmd_match(buf, "backwards"))
4596 backwards = 1;
4597 else
4598 return -EINVAL;
4599 if (mddev->reshape_backwards == backwards)
4600 return len;
4601
NeilBrown67918752014-12-15 12:57:01 +11004602 err = mddev_lock(mddev);
4603 if (err)
4604 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004605 /* check if we are allowed to change */
4606 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004607 err = -EBUSY;
4608 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004609 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004610 err = -EINVAL;
4611 else
4612 mddev->reshape_backwards = backwards;
4613 mddev_unlock(mddev);
4614 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004615}
4616
4617static struct md_sysfs_entry md_reshape_direction =
4618__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4619 reshape_direction_store);
4620
4621static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004622array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004623{
4624 if (mddev->external_size)
4625 return sprintf(page, "%llu\n",
4626 (unsigned long long)mddev->array_sectors/2);
4627 else
4628 return sprintf(page, "default\n");
4629}
4630
4631static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004632array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004633{
4634 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004635 int err;
4636
4637 err = mddev_lock(mddev);
4638 if (err)
4639 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004640
4641 if (strncmp(buf, "default", 7) == 0) {
4642 if (mddev->pers)
4643 sectors = mddev->pers->size(mddev, 0, 0);
4644 else
4645 sectors = mddev->array_sectors;
4646
4647 mddev->external_size = 0;
4648 } else {
4649 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004650 err = -EINVAL;
4651 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4652 err = -E2BIG;
4653 else
4654 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004655 }
4656
NeilBrown67918752014-12-15 12:57:01 +11004657 if (!err) {
4658 mddev->array_sectors = sectors;
4659 if (mddev->pers) {
4660 set_capacity(mddev->gendisk, mddev->array_sectors);
4661 revalidate_disk(mddev->gendisk);
4662 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004663 }
NeilBrown67918752014-12-15 12:57:01 +11004664 mddev_unlock(mddev);
4665 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004666}
4667
4668static struct md_sysfs_entry md_array_size =
4669__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4670 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004671
NeilBrowneae17012005-11-08 21:39:23 -08004672static struct attribute *md_default_attrs[] = {
4673 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004674 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004675 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004676 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004677 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004678 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004679 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004680 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004681 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004682 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004683 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004684 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004685 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004686 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004687 NULL,
4688};
4689
4690static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004691 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004692 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004693 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004694 &md_sync_min.attr,
4695 &md_sync_max.attr,
4696 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004697 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004698 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004699 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004700 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004701 &md_suspend_lo.attr,
4702 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004703 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004704 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004705 NULL,
4706};
NeilBrown411036f2005-11-08 21:39:40 -08004707static struct attribute_group md_redundancy_group = {
4708 .name = NULL,
4709 .attrs = md_redundancy_attrs,
4710};
4711
NeilBrowneae17012005-11-08 21:39:23 -08004712static ssize_t
4713md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4714{
4715 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004716 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004717 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004718
4719 if (!entry->show)
4720 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004721 spin_lock(&all_mddevs_lock);
4722 if (list_empty(&mddev->all_mddevs)) {
4723 spin_unlock(&all_mddevs_lock);
4724 return -EBUSY;
4725 }
4726 mddev_get(mddev);
4727 spin_unlock(&all_mddevs_lock);
4728
NeilBrownb7b17c92014-12-15 12:56:59 +11004729 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004730 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004731 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004732}
4733
4734static ssize_t
4735md_attr_store(struct kobject *kobj, struct attribute *attr,
4736 const char *page, size_t length)
4737{
4738 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004739 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004740 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004741
4742 if (!entry->store)
4743 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004744 if (!capable(CAP_SYS_ADMIN))
4745 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004746 spin_lock(&all_mddevs_lock);
4747 if (list_empty(&mddev->all_mddevs)) {
4748 spin_unlock(&all_mddevs_lock);
4749 return -EBUSY;
4750 }
4751 mddev_get(mddev);
4752 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004753 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004754 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004755 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004756}
4757
4758static void md_free(struct kobject *ko)
4759{
NeilBrownfd01b882011-10-11 16:47:53 +11004760 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004761
4762 if (mddev->sysfs_state)
4763 sysfs_put(mddev->sysfs_state);
4764
4765 if (mddev->gendisk) {
4766 del_gendisk(mddev->gendisk);
4767 put_disk(mddev->gendisk);
4768 }
4769 if (mddev->queue)
4770 blk_cleanup_queue(mddev->queue);
4771
NeilBrowneae17012005-11-08 21:39:23 -08004772 kfree(mddev);
4773}
4774
Emese Revfy52cf25d2010-01-19 02:58:23 +01004775static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004776 .show = md_attr_show,
4777 .store = md_attr_store,
4778};
4779static struct kobj_type md_ktype = {
4780 .release = md_free,
4781 .sysfs_ops = &md_sysfs_ops,
4782 .default_attrs = md_default_attrs,
4783};
4784
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785int mdp_major = 0;
4786
Dan Williams5fd3a172009-03-04 00:57:25 -07004787static void mddev_delayed_delete(struct work_struct *ws)
4788{
NeilBrownfd01b882011-10-11 16:47:53 +11004789 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004790
NeilBrown43a70502009-12-14 12:49:55 +11004791 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004792 kobject_del(&mddev->kobj);
4793 kobject_put(&mddev->kobj);
4794}
4795
NeilBrownefeb53c2009-01-09 08:31:10 +11004796static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004798 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004799 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004801 int partitioned;
4802 int shift;
4803 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004804 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
4806 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004807 return -ENODEV;
4808
4809 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4810 shift = partitioned ? MdpMinorShift : 0;
4811 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
Tejun Heoe804ac72010-10-15 15:36:08 +02004813 /* wait for any previous instance of this device to be
4814 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004815 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004816 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004817
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004818 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004819 error = -EEXIST;
4820 if (mddev->gendisk)
4821 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004822
4823 if (name) {
4824 /* Need to ensure that 'name' is not a duplicate.
4825 */
NeilBrownfd01b882011-10-11 16:47:53 +11004826 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004827 spin_lock(&all_mddevs_lock);
4828
4829 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4830 if (mddev2->gendisk &&
4831 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4832 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004833 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004834 }
4835 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 }
NeilBrown8b765392009-01-09 08:31:08 +11004837
NeilBrown0909dc42009-07-01 12:27:21 +10004838 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004839 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004840 if (!mddev->queue)
4841 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004842 mddev->queue->queuedata = mddev;
4843
NeilBrown409c57f2009-03-31 14:39:39 +11004844 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004845 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004846
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 disk = alloc_disk(1 << shift);
4848 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004849 blk_cleanup_queue(mddev->queue);
4850 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004851 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004853 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004855 if (name)
4856 strcpy(disk->disk_name, name);
4857 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004859 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 disk->fops = &md_fops;
4862 disk->private_data = mddev;
4863 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004864 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004865 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004866 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004867 * remove it now.
4868 */
4869 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004871 /* As soon as we call add_disk(), another thread could get
4872 * through to md_open, so make sure it doesn't get too far
4873 */
4874 mutex_lock(&mddev->open_mutex);
4875 add_disk(disk);
4876
Tejun Heoed9e1982008-08-25 19:56:05 +09004877 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4878 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004879 if (error) {
4880 /* This isn't possible, but as kobject_init_and_add is marked
4881 * __must_check, we must do something with the result
4882 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004883 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4884 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004885 error = 0;
4886 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004887 if (mddev->kobj.sd &&
4888 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004889 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004890 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004891 abort:
4892 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004893 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004894 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004895 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004896 }
NeilBrownd3374822009-01-09 08:31:10 +11004897 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004898 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004899}
4900
4901static struct kobject *md_probe(dev_t dev, int *part, void *data)
4902{
4903 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 return NULL;
4905}
4906
NeilBrownefeb53c2009-01-09 08:31:10 +11004907static int add_named_array(const char *val, struct kernel_param *kp)
4908{
4909 /* val must be "md_*" where * is not all digits.
4910 * We allocate an array with a large free minor number, and
4911 * set the name to val. val must not already be an active name.
4912 */
4913 int len = strlen(val);
4914 char buf[DISK_NAME_LEN];
4915
4916 while (len && val[len-1] == '\n')
4917 len--;
4918 if (len >= DISK_NAME_LEN)
4919 return -E2BIG;
4920 strlcpy(buf, val, len+1);
4921 if (strncmp(buf, "md_", 3) != 0)
4922 return -EINVAL;
4923 return md_alloc(0, buf);
4924}
4925
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926static void md_safemode_timeout(unsigned long data)
4927{
NeilBrownfd01b882011-10-11 16:47:53 +11004928 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
Neil Brown0fd62b82008-06-28 08:31:36 +10004930 if (!atomic_read(&mddev->writes_pending)) {
4931 mddev->safemode = 1;
4932 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004933 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 md_wakeup_thread(mddev->thread);
4936}
4937
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004938static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
NeilBrownfd01b882011-10-11 16:47:53 +11004940int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941{
NeilBrown2604b702006-01-06 00:20:36 -08004942 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004943 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004944 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
NeilBrowna757e642005-04-16 15:26:42 -07004946 if (list_empty(&mddev->disks))
4947 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
4950 if (mddev->pers)
4951 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004952 /* Cannot run until previous stop completes properly */
4953 if (mddev->sysfs_active)
4954 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004955
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 /*
4957 * Analyze all RAID superblock(s)
4958 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004959 if (!mddev->raid_disks) {
4960 if (!mddev->persistent)
4961 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004962 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
NeilBrownd9d166c2006-01-06 00:20:51 -08004965 if (mddev->level != LEVEL_NONE)
4966 request_module("md-level-%d", mddev->level);
4967 else if (mddev->clevel[0])
4968 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
4970 /*
4971 * Drop all container device buffers, from now on
4972 * the only valid external interface is through the md
4973 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004975 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004976 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 continue;
4978 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004979 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004980
4981 /* perform some consistency tests on the device.
4982 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004983 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004984 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004985 if (rdev->meta_bdev) {
4986 /* Nothing to check */;
4987 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004988 if (mddev->dev_sectors &&
4989 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004990 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004991 printk("md: %s: data overlaps metadata\n",
4992 mdname(mddev));
4993 return -EINVAL;
4994 }
4995 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004996 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004997 > rdev->data_offset) {
4998 printk("md: %s: metadata overlaps data\n",
4999 mdname(mddev));
5000 return -EINVAL;
5001 }
5002 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005003 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 }
5005
NeilBrowna167f662010-10-26 18:31:13 +11005006 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005007 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005010 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005011 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005013 if (mddev->level != LEVEL_NONE)
5014 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5015 mddev->level);
5016 else
5017 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5018 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 return -EINVAL;
5020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005022 if (mddev->level != pers->level) {
5023 mddev->level = pers->level;
5024 mddev->new_level = pers->level;
5025 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005026 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
NeilBrownf6705572006-03-27 01:18:11 -08005028 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005029 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005030 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005031 module_put(pers->owner);
5032 return -EINVAL;
5033 }
5034
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005035 if (pers->sync_request) {
5036 /* Warn if this is a potentially silly
5037 * configuration.
5038 */
5039 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005040 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005041 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005042
NeilBrowndafb20f2012-03-19 12:46:39 +11005043 rdev_for_each(rdev, mddev)
5044 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005045 if (rdev < rdev2 &&
5046 rdev->bdev->bd_contains ==
5047 rdev2->bdev->bd_contains) {
5048 printk(KERN_WARNING
5049 "%s: WARNING: %s appears to be"
5050 " on the same physical disk as"
5051 " %s.\n",
5052 mdname(mddev),
5053 bdevname(rdev->bdev,b),
5054 bdevname(rdev2->bdev,b2));
5055 warned = 1;
5056 }
5057 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005058
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005059 if (warned)
5060 printk(KERN_WARNING
5061 "True protection against single-disk"
5062 " failure might be compromised.\n");
5063 }
5064
NeilBrown657390d2005-08-26 18:34:16 -07005065 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005066 /* may be over-ridden by personality */
5067 mddev->resync_max_sectors = mddev->dev_sectors;
5068
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005069 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
NeilBrown0f9552b52009-12-30 12:08:50 +11005071 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005072 mddev->ro = 2; /* read-only, but switch on first write */
5073
NeilBrown36d091f2014-12-15 12:56:58 +11005074 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005075 if (err)
5076 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005077 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005078 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5079 " but 'external_size' not in effect?\n", __func__);
5080 printk(KERN_ERR
5081 "md: invalid array_size %llu > default size %llu\n",
5082 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005083 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005084 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005085 }
NeilBrown36d091f2014-12-15 12:56:58 +11005086 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005087 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005088 struct bitmap *bitmap;
5089
5090 bitmap = bitmap_create(mddev, -1);
5091 if (IS_ERR(bitmap)) {
5092 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005093 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5094 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005095 } else
5096 mddev->bitmap = bitmap;
5097
NeilBrownb15c2e52006-01-06 00:20:16 -08005098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005100 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005101 pers->free(mddev, mddev->private);
5102 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005103 bitmap_destroy(mddev);
5104 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 }
NeilBrown5c675f82014-12-15 12:56:56 +11005106 if (mddev->queue) {
5107 mddev->queue->backing_dev_info.congested_data = mddev;
5108 mddev->queue->backing_dev_info.congested_fn = md_congested;
NeilBrown64590f42014-12-15 12:56:57 +11005109 blk_queue_merge_bvec(mddev->queue, md_mergeable_bvec);
NeilBrown5c675f82014-12-15 12:56:56 +11005110 }
NeilBrown36d091f2014-12-15 12:56:58 +11005111 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005112 if (mddev->kobj.sd &&
5113 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005114 printk(KERN_WARNING
5115 "md: cannot register extra attributes for %s\n",
5116 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005117 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005118 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005119 mddev->ro = 0;
5120
NeilBrownf72ffdd2014-09-30 14:23:59 +10005121 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005122 atomic_set(&mddev->max_corr_read_errors,
5123 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 mddev->safemode = 0;
5125 mddev->safemode_timer.function = md_safemode_timeout;
5126 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005127 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005129 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005130 spin_lock(&mddev->lock);
5131 mddev->pers = pers;
NeilBrown0ca69882011-01-14 09:14:33 +11005132 mddev->ready = 1;
NeilBrown36d091f2014-12-15 12:56:58 +11005133 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005134 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005135 if (rdev->raid_disk >= 0)
5136 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005137 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005138
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005140
NeilBrown7a0a5352013-08-27 16:28:23 +10005141 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07005142 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
NeilBrownd7603b72006-01-06 00:20:30 -08005144 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005145 sysfs_notify_dirent_safe(mddev->sysfs_state);
5146 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005147 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 return 0;
5149}
NeilBrown390ee602010-06-01 19:37:27 +10005150EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151
NeilBrownfd01b882011-10-11 16:47:53 +11005152static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005153{
5154 int err;
5155
5156 err = md_run(mddev);
5157 if (err)
5158 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005159 err = bitmap_load(mddev);
5160 if (err) {
5161 bitmap_destroy(mddev);
5162 goto out;
5163 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005164
5165 md_wakeup_thread(mddev->thread);
5166 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5167
NeilBrownfe60b012010-03-29 11:10:42 +11005168 set_capacity(mddev->gendisk, mddev->array_sectors);
5169 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005170 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005171 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5172out:
5173 return err;
5174}
5175
NeilBrownfd01b882011-10-11 16:47:53 +11005176static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177{
5178 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179
Andre Noll80fab1d2008-07-11 22:02:21 +10005180 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005182 return -ENXIO;
5183 if (!mddev->pers)
5184 return -EINVAL;
5185 if (!mddev->ro)
5186 return -EBUSY;
5187 mddev->safemode = 0;
5188 mddev->ro = 0;
5189 set_disk_ro(disk, 0);
5190 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5191 mdname(mddev));
5192 /* Kick recovery or resync if necessary */
5193 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5194 md_wakeup_thread(mddev->thread);
5195 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005196 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198}
5199
NeilBrownfd01b882011-10-11 16:47:53 +11005200static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005201{
5202 mddev->array_sectors = 0;
5203 mddev->external_size = 0;
5204 mddev->dev_sectors = 0;
5205 mddev->raid_disks = 0;
5206 mddev->recovery_cp = 0;
5207 mddev->resync_min = 0;
5208 mddev->resync_max = MaxSector;
5209 mddev->reshape_position = MaxSector;
5210 mddev->external = 0;
5211 mddev->persistent = 0;
5212 mddev->level = LEVEL_NONE;
5213 mddev->clevel[0] = 0;
5214 mddev->flags = 0;
5215 mddev->ro = 0;
5216 mddev->metadata_type[0] = 0;
5217 mddev->chunk_sectors = 0;
5218 mddev->ctime = mddev->utime = 0;
5219 mddev->layout = 0;
5220 mddev->max_disks = 0;
5221 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005222 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005223 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005224 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005225 mddev->new_level = LEVEL_NONE;
5226 mddev->new_layout = 0;
5227 mddev->new_chunk_sectors = 0;
5228 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005229 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005230 mddev->suspend_lo = mddev->suspend_hi = 0;
5231 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5232 mddev->recovery = 0;
5233 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005234 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005235 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005236 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005237 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005238 mddev->bitmap_info.offset = 0;
5239 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005240 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005241 mddev->bitmap_info.chunksize = 0;
5242 mddev->bitmap_info.daemon_sleep = 0;
5243 mddev->bitmap_info.max_write_behind = 0;
5244}
5245
NeilBrownfd01b882011-10-11 16:47:53 +11005246static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005247{
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005248 if (mddev_is_clustered(mddev))
5249 md_cluster_ops->metadata_update_start(mddev);
NeilBrown6b6204e2013-05-09 09:48:30 +10005250 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005251 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005252 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005253 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005254 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005255 }
5256
5257 del_timer_sync(&mddev->safemode_timer);
5258
5259 bitmap_flush(mddev);
5260 md_super_wait(mddev);
5261
NeilBrownb6d428c2013-04-24 11:42:42 +10005262 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005263 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005264 /* mark array as shutdown cleanly */
5265 mddev->in_sync = 1;
5266 md_update_sb(mddev, 1);
5267 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005268 if (mddev_is_clustered(mddev))
5269 md_cluster_ops->metadata_update_finish(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005270}
NeilBrowndefad612011-01-14 09:14:33 +11005271
NeilBrownfd01b882011-10-11 16:47:53 +11005272void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005273{
NeilBrown29f097c2013-11-14 17:54:51 +11005274 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005275 __md_stop_writes(mddev);
5276 mddev_unlock(mddev);
5277}
NeilBrown390ee602010-06-01 19:37:27 +10005278EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005279
NeilBrown5aa61f42014-12-15 12:56:57 +11005280static void mddev_detach(struct mddev *mddev)
5281{
5282 struct bitmap *bitmap = mddev->bitmap;
5283 /* wait for behind writes to complete */
5284 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5285 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5286 mdname(mddev));
5287 /* need to kick something here to make sure I/O goes? */
5288 wait_event(bitmap->behind_wait,
5289 atomic_read(&bitmap->behind_writes) == 0);
5290 }
NeilBrown36d091f2014-12-15 12:56:58 +11005291 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005292 mddev->pers->quiesce(mddev, 1);
5293 mddev->pers->quiesce(mddev, 0);
5294 }
5295 md_unregister_thread(&mddev->thread);
5296 if (mddev->queue)
5297 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5298}
5299
NeilBrown5eff3c42012-11-19 10:47:48 +11005300static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005301{
NeilBrown36d091f2014-12-15 12:56:58 +11005302 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005303 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005304 spin_lock(&mddev->lock);
5305 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005306 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005307 spin_unlock(&mddev->lock);
5308 pers->free(mddev, mddev->private);
5309 if (pers->sync_request && mddev->to_remove == NULL)
5310 mddev->to_remove = &md_redundancy_group;
5311 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005312 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005313}
NeilBrown5eff3c42012-11-19 10:47:48 +11005314
5315void md_stop(struct mddev *mddev)
5316{
5317 /* stop the array and free an attached data structures.
5318 * This is called from dm-raid
5319 */
5320 __md_stop(mddev);
5321 bitmap_destroy(mddev);
5322 if (mddev->bio_set)
5323 bioset_free(mddev->bio_set);
5324}
5325
NeilBrown390ee602010-06-01 19:37:27 +10005326EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005327
NeilBrowna05b7ea2012-07-19 15:59:18 +10005328static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005329{
5330 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005331 int did_freeze = 0;
5332
5333 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5334 did_freeze = 1;
5335 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5336 md_wakeup_thread(mddev->thread);
5337 }
NeilBrownf851b602014-12-11 10:02:10 +11005338 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005339 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005340 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005341 /* Thread might be blocked waiting for metadata update
5342 * which will now never happen */
5343 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005344
NeilBrown30b8feb2013-11-14 15:16:17 +11005345 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005346 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5347 &mddev->recovery));
NeilBrown30b8feb2013-11-14 15:16:17 +11005348 mddev_lock_nointr(mddev);
5349
NeilBrowna4bd82d2010-03-29 13:23:10 +11005350 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005351 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005352 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005353 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005354 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005355 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005356 if (did_freeze) {
5357 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005358 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005359 md_wakeup_thread(mddev->thread);
5360 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005361 err = -EBUSY;
5362 goto out;
5363 }
5364 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005365 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005366
5367 err = -ENXIO;
5368 if (mddev->ro==1)
5369 goto out;
5370 mddev->ro = 1;
5371 set_disk_ro(mddev->gendisk, 1);
5372 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005373 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5374 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005375 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005376 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005377 }
5378out:
5379 mutex_unlock(&mddev->open_mutex);
5380 return err;
5381}
5382
NeilBrown9e653b62006-06-26 00:27:58 -07005383/* mode:
5384 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005385 * 2 - stop but do not disassemble array
5386 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005387static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005388 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005391 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005392 int did_freeze = 0;
5393
5394 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5395 did_freeze = 1;
5396 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5397 md_wakeup_thread(mddev->thread);
5398 }
NeilBrownf851b602014-12-11 10:02:10 +11005399 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005400 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005401 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005402 /* Thread might be blocked waiting for metadata update
5403 * which will now never happen */
5404 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005405
NeilBrown30b8feb2013-11-14 15:16:17 +11005406 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005407 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5408 !test_bit(MD_RECOVERY_RUNNING,
5409 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005410 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411
NeilBrownc8c00a62009-08-10 12:50:52 +10005412 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005413 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005414 mddev->sysfs_active ||
5415 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005416 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005417 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005418 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005419 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005420 if (did_freeze) {
5421 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005422 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005423 md_wakeup_thread(mddev->thread);
5424 }
NeilBrown260fa032013-08-27 16:44:13 +10005425 return -EBUSY;
5426 }
NeilBrown6e17b022010-08-07 21:41:19 +10005427 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005428 if (mddev->ro)
5429 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005430
NeilBrowndefad612011-01-14 09:14:33 +11005431 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005432 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005433 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005434 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005435
NeilBrowna4bd82d2010-03-29 13:23:10 +11005436 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005437 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005438
NeilBrowndafb20f2012-03-19 12:46:39 +11005439 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005440 if (rdev->raid_disk >= 0)
5441 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005442
NeilBrowna4bd82d2010-03-29 13:23:10 +11005443 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005444 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005445 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005446 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005447
NeilBrowna4bd82d2010-03-29 13:23:10 +11005448 if (mddev->ro)
5449 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005450 } else
5451 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 /*
5453 * Free resources if final stop
5454 */
NeilBrown9e653b62006-06-26 00:27:58 -07005455 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5457
NeilBrown978f9462006-02-02 14:28:05 -08005458 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005459 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005460 struct file *f = mddev->bitmap_info.file;
5461 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005462 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005463 spin_unlock(&mddev->lock);
5464 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005465 }
NeilBrownc3d97142009-12-14 12:49:52 +11005466 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005467
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 export_array(mddev);
5469
NeilBrown6177b472010-03-29 11:37:13 +11005470 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005471 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005472 if (mddev->hold_active == UNTIL_STOP)
5473 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005474 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005475 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005476 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005477 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479}
5480
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005481#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005482static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483{
NeilBrown3cb03002011-10-11 16:45:26 +11005484 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 int err;
5486
NeilBrowna757e642005-04-16 15:26:42 -07005487 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
5490 printk(KERN_INFO "md: running: ");
5491
NeilBrowndafb20f2012-03-19 12:46:39 +11005492 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 char b[BDEVNAME_SIZE];
5494 printk("<%s>", bdevname(rdev->bdev,b));
5495 }
5496 printk("\n");
5497
NeilBrownd710e132008-10-13 11:55:12 +11005498 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 if (err) {
5500 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005501 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 }
5503}
5504
5505/*
5506 * lets try to run arrays based on all disks that have arrived
5507 * until now. (those are in pending_raid_disks)
5508 *
5509 * the method: pick the first pending disk, collect all disks with
5510 * the same UUID, remove all from the pending list and put them into
5511 * the 'same_array' list. Then order this list based on superblock
5512 * update time (freshest comes first), kick out 'old' disks and
5513 * compare superblocks. If everything's fine then run it.
5514 *
5515 * If "unit" is allocated, then bump its reference count
5516 */
5517static void autorun_devices(int part)
5518{
NeilBrown3cb03002011-10-11 16:45:26 +11005519 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005520 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 char b[BDEVNAME_SIZE];
5522
5523 printk(KERN_INFO "md: autorun ...\n");
5524 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005525 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005527 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005529 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
5531 printk(KERN_INFO "md: considering %s ...\n",
5532 bdevname(rdev0->bdev,b));
5533 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005534 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 if (super_90_load(rdev, rdev0, 0) >= 0) {
5536 printk(KERN_INFO "md: adding %s ...\n",
5537 bdevname(rdev->bdev,b));
5538 list_move(&rdev->same_set, &candidates);
5539 }
5540 /*
5541 * now we have a set of devices, with all of them having
5542 * mostly sane superblocks. It's time to allocate the
5543 * mddev.
5544 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005545 if (part) {
5546 dev = MKDEV(mdp_major,
5547 rdev0->preferred_minor << MdpMinorShift);
5548 unit = MINOR(dev) >> MdpMinorShift;
5549 } else {
5550 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5551 unit = MINOR(dev);
5552 }
5553 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5555 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5556 break;
5557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558
5559 md_probe(dev, NULL, NULL);
5560 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005561 if (!mddev || !mddev->gendisk) {
5562 if (mddev)
5563 mddev_put(mddev);
5564 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 "md: cannot allocate memory for md drive.\n");
5566 break;
5567 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005568 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 printk(KERN_WARNING "md: %s locked, cannot run\n",
5570 mdname(mddev));
5571 else if (mddev->raid_disks || mddev->major_version
5572 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005573 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 "md: %s already running, cannot run %s\n",
5575 mdname(mddev), bdevname(rdev0->bdev,b));
5576 mddev_unlock(mddev);
5577 } else {
5578 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005579 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005580 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 list_del_init(&rdev->same_set);
5582 if (bind_rdev_to_array(rdev, mddev))
5583 export_rdev(rdev);
5584 }
5585 autorun_array(mddev);
5586 mddev_unlock(mddev);
5587 }
5588 /* on success, candidates will be empty, on error
5589 * it won't...
5590 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005591 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005592 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 mddev_put(mddev);
5596 }
5597 printk(KERN_INFO "md: ... autorun DONE.\n");
5598}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005599#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
NeilBrownf72ffdd2014-09-30 14:23:59 +10005601static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602{
5603 mdu_version_t ver;
5604
5605 ver.major = MD_MAJOR_VERSION;
5606 ver.minor = MD_MINOR_VERSION;
5607 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5608
5609 if (copy_to_user(arg, &ver, sizeof(ver)))
5610 return -EFAULT;
5611
5612 return 0;
5613}
5614
NeilBrownf72ffdd2014-09-30 14:23:59 +10005615static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616{
5617 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005618 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005619 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620
NeilBrown1ca69c42012-10-11 13:37:33 +11005621 nr = working = insync = failed = spare = 0;
5622 rcu_read_lock();
5623 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005625 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 failed++;
5627 else {
5628 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005629 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005630 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 else
5632 spare++;
5633 }
5634 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005635 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636
5637 info.major_version = mddev->major_version;
5638 info.minor_version = mddev->minor_version;
5639 info.patch_version = MD_PATCHLEVEL_VERSION;
5640 info.ctime = mddev->ctime;
5641 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005642 info.size = mddev->dev_sectors / 2;
5643 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005644 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 info.nr_disks = nr;
5646 info.raid_disks = mddev->raid_disks;
5647 info.md_minor = mddev->md_minor;
5648 info.not_persistent= !mddev->persistent;
5649
5650 info.utime = mddev->utime;
5651 info.state = 0;
5652 if (mddev->in_sync)
5653 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005654 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005655 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005656 if (mddev_is_clustered(mddev))
5657 info.state |= (1<<MD_SB_CLUSTERED);
5658
NeilBrowna9f326e2009-09-23 18:06:41 +10005659 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 info.working_disks = working;
5661 info.failed_disks = failed;
5662 info.spare_disks = spare;
5663
5664 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005665 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
5667 if (copy_to_user(arg, &info, sizeof(info)))
5668 return -EFAULT;
5669
5670 return 0;
5671}
5672
NeilBrownf72ffdd2014-09-30 14:23:59 +10005673static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005674{
5675 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005676 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005677 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005678
NeilBrown60559da2013-07-16 16:44:29 +10005679 file = kmalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005680 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005681 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005682
NeilBrown32a76272005-06-21 17:17:14 -07005683 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005684 spin_lock(&mddev->lock);
5685 /* bitmap disabled, zero the first byte and copy out */
5686 if (!mddev->bitmap_info.file)
5687 file->pathname[0] = '\0';
5688 else if ((ptr = d_path(&mddev->bitmap_info.file->f_path,
5689 file->pathname, sizeof(file->pathname))),
5690 IS_ERR(ptr))
5691 err = PTR_ERR(ptr);
5692 else
5693 memmove(file->pathname, ptr,
5694 sizeof(file->pathname)-(ptr-file->pathname));
5695 spin_unlock(&mddev->lock);
5696
5697 if (err == 0 &&
5698 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005699 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005700
NeilBrown32a76272005-06-21 17:17:14 -07005701 kfree(file);
5702 return err;
5703}
5704
NeilBrownf72ffdd2014-09-30 14:23:59 +10005705static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706{
5707 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005708 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
5710 if (copy_from_user(&info, arg, sizeof(info)))
5711 return -EFAULT;
5712
NeilBrown1ca69c42012-10-11 13:37:33 +11005713 rcu_read_lock();
5714 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 if (rdev) {
5716 info.major = MAJOR(rdev->bdev->bd_dev);
5717 info.minor = MINOR(rdev->bdev->bd_dev);
5718 info.raid_disk = rdev->raid_disk;
5719 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005720 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005722 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 info.state |= (1<<MD_DISK_ACTIVE);
5724 info.state |= (1<<MD_DISK_SYNC);
5725 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005726 if (test_bit(WriteMostly, &rdev->flags))
5727 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 } else {
5729 info.major = info.minor = 0;
5730 info.raid_disk = -1;
5731 info.state = (1<<MD_DISK_REMOVED);
5732 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005733 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
5735 if (copy_to_user(arg, &info, sizeof(info)))
5736 return -EFAULT;
5737
5738 return 0;
5739}
5740
NeilBrownf72ffdd2014-09-30 14:23:59 +10005741static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742{
5743 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005744 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 dev_t dev = MKDEV(info->major,info->minor);
5746
5747 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5748 return -EOVERFLOW;
5749
5750 if (!mddev->raid_disks) {
5751 int err;
5752 /* expecting a device which has a superblock */
5753 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5754 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005755 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 "md: md_import_device returned %ld\n",
5757 PTR_ERR(rdev));
5758 return PTR_ERR(rdev);
5759 }
5760 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005761 struct md_rdev *rdev0
5762 = list_entry(mddev->disks.next,
5763 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005764 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 .load_super(rdev, rdev0, mddev->minor_version);
5766 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005767 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005769 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 bdevname(rdev0->bdev,b2));
5771 export_rdev(rdev);
5772 return -EINVAL;
5773 }
5774 }
5775 err = bind_rdev_to_array(rdev, mddev);
5776 if (err)
5777 export_rdev(rdev);
5778 return err;
5779 }
5780
5781 /*
5782 * add_new_disk can be used once the array is assembled
5783 * to add "hot spares". They must already have a superblock
5784 * written
5785 */
5786 if (mddev->pers) {
5787 int err;
5788 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005789 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 "%s: personality does not support diskops!\n",
5791 mdname(mddev));
5792 return -EINVAL;
5793 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005794 if (mddev->persistent)
5795 rdev = md_import_device(dev, mddev->major_version,
5796 mddev->minor_version);
5797 else
5798 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005800 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 "md: md_import_device returned %ld\n",
5802 PTR_ERR(rdev));
5803 return PTR_ERR(rdev);
5804 }
NeilBrown1a855a02010-12-09 16:36:28 +11005805 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005806 if (!mddev->persistent) {
5807 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005808 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005809 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005810 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005811 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005812 } else
NeilBrown41158c72005-06-21 17:17:25 -07005813 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005814 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005815 } else
5816 super_types[mddev->major_version].
5817 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005818 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005819 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005820 /* This was a hot-add request, but events doesn't
5821 * match, so reject it.
5822 */
5823 export_rdev(rdev);
5824 return -EINVAL;
5825 }
5826
NeilBrownb2d444d2005-11-08 21:39:31 -08005827 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005828 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5829 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005830 else
5831 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005832
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 rdev->raid_disk = -1;
5834 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005835 if (!err && !mddev->pers->hot_remove_disk) {
5836 /* If there is hot_add_disk but no hot_remove_disk
5837 * then added disks for geometry changes,
5838 * and should be added immediately.
5839 */
5840 super_types[mddev->major_version].
5841 validate_super(mddev, rdev);
5842 err = mddev->pers->hot_add_disk(mddev, rdev);
5843 if (err)
5844 unbind_rdev_from_array(rdev);
5845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 if (err)
5847 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005848 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005849 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005850
NeilBrown7ceb17e2013-04-24 11:42:42 +10005851 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005852 if (mddev->degraded)
5853 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005854 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005855 if (!err)
5856 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005857 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 return err;
5859 }
5860
5861 /* otherwise, add_new_disk is only allowed
5862 * for major_version==0 superblocks
5863 */
5864 if (mddev->major_version != 0) {
5865 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5866 mdname(mddev));
5867 return -EINVAL;
5868 }
5869
5870 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5871 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005872 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005874 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 "md: error, md_import_device() returned %ld\n",
5876 PTR_ERR(rdev));
5877 return PTR_ERR(rdev);
5878 }
5879 rdev->desc_nr = info->number;
5880 if (info->raid_disk < mddev->raid_disks)
5881 rdev->raid_disk = info->raid_disk;
5882 else
5883 rdev->raid_disk = -1;
5884
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005886 if (info->state & (1<<MD_DISK_SYNC))
5887 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005889 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5890 set_bit(WriteMostly, &rdev->flags);
5891
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 if (!mddev->persistent) {
5893 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005894 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5895 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005896 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005897 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898
NeilBrown2bf071b2006-01-06 00:20:55 -08005899 err = bind_rdev_to_array(rdev, mddev);
5900 if (err) {
5901 export_rdev(rdev);
5902 return err;
5903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 }
5905
5906 return 0;
5907}
5908
NeilBrownf72ffdd2014-09-30 14:23:59 +10005909static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910{
5911 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005912 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 rdev = find_rdev(mddev, dev);
5915 if (!rdev)
5916 return -ENXIO;
5917
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005918 if (mddev_is_clustered(mddev))
5919 md_cluster_ops->metadata_update_start(mddev);
5920
NeilBrown3ea8929d2013-04-24 11:42:41 +10005921 clear_bit(Blocked, &rdev->flags);
5922 remove_and_add_spares(mddev, rdev);
5923
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 if (rdev->raid_disk >= 0)
5925 goto busy;
5926
5927 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005928 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005929 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005931 if (mddev_is_clustered(mddev))
5932 md_cluster_ops->metadata_update_finish(mddev);
5933
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 return 0;
5935busy:
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005936 if (mddev_is_clustered(mddev))
5937 md_cluster_ops->metadata_update_cancel(mddev);
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005938 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 bdevname(rdev->bdev,b), mdname(mddev));
5940 return -EBUSY;
5941}
5942
NeilBrownf72ffdd2014-09-30 14:23:59 +10005943static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944{
5945 char b[BDEVNAME_SIZE];
5946 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005947 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948
5949 if (!mddev->pers)
5950 return -ENODEV;
5951
5952 if (mddev->major_version != 0) {
5953 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5954 " version-0 superblocks.\n",
5955 mdname(mddev));
5956 return -EINVAL;
5957 }
5958 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005959 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 "%s: personality does not support diskops!\n",
5961 mdname(mddev));
5962 return -EINVAL;
5963 }
5964
NeilBrownd710e132008-10-13 11:55:12 +11005965 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005967 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 "md: error, md_import_device() returned %ld\n",
5969 PTR_ERR(rdev));
5970 return -EINVAL;
5971 }
5972
5973 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005974 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005976 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977
NeilBrown8190e752009-06-18 08:48:58 +10005978 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979
NeilBrownb2d444d2005-11-08 21:39:31 -08005980 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005981 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 "md: can not hot-add faulty %s disk to %s!\n",
5983 bdevname(rdev->bdev,b), mdname(mddev));
5984 err = -EINVAL;
5985 goto abort_export;
5986 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005987
5988 if (mddev_is_clustered(mddev))
5989 md_cluster_ops->metadata_update_start(mddev);
NeilBrownb2d444d2005-11-08 21:39:31 -08005990 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005992 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005993 err = bind_rdev_to_array(rdev, mddev);
5994 if (err)
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005995 goto abort_clustered;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996
5997 /*
5998 * The rest should better be atomic, we can have disk failures
5999 * noticed in interrupt contexts ...
6000 */
6001
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 rdev->raid_disk = -1;
6003
NeilBrown850b2b422006-10-03 01:15:46 -07006004 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006006 if (mddev_is_clustered(mddev))
6007 md_cluster_ops->metadata_update_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 /*
6009 * Kick recovery, maybe this spare has to be added to the
6010 * array immediately.
6011 */
6012 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6013 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006014 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 return 0;
6016
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006017abort_clustered:
6018 if (mddev_is_clustered(mddev))
6019 md_cluster_ops->metadata_update_cancel(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020abort_export:
6021 export_rdev(rdev);
6022 return err;
6023}
6024
NeilBrownfd01b882011-10-11 16:47:53 +11006025static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006026{
NeilBrown035328c2014-04-09 12:25:40 +10006027 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006028
NeilBrown36fa3062005-09-09 16:23:45 -07006029 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006030 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006031 return -EBUSY;
6032 if (mddev->recovery || mddev->sync_thread)
6033 return -EBUSY;
6034 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006035 }
6036
NeilBrown36fa3062005-09-09 16:23:45 -07006037 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006038 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006039 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006040
NeilBrown1e594bb2014-12-15 12:57:00 +11006041 if (mddev->bitmap || mddev->bitmap_info.file)
6042 return -EEXIST; /* cannot add when bitmap is present */
6043 f = fget(fd);
6044
6045 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006046 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6047 mdname(mddev));
6048 return -EBADF;
6049 }
6050
NeilBrown1e594bb2014-12-15 12:57:00 +11006051 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006052 if (!S_ISREG(inode->i_mode)) {
6053 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6054 mdname(mddev));
6055 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006056 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006057 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6058 mdname(mddev));
6059 err = -EBADF;
6060 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006061 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6062 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006063 err = -EBUSY;
6064 }
6065 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006066 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006067 return err;
6068 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006069 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006070 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006071 } else if (mddev->bitmap == NULL)
6072 return -ENOENT; /* cannot remove what isn't there */
6073 err = 0;
6074 if (mddev->pers) {
6075 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006076 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006077 struct bitmap *bitmap;
6078
6079 bitmap = bitmap_create(mddev, -1);
6080 if (!IS_ERR(bitmap)) {
6081 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006082 err = bitmap_load(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006083 }
NeilBrown69e51b42010-06-01 19:37:35 +10006084 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006085 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006086 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006087 fd = -1; /* make sure to put the file */
6088 }
NeilBrown36fa3062005-09-09 16:23:45 -07006089 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006090 }
6091 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006092 struct file *f = mddev->bitmap_info.file;
6093 if (f) {
6094 spin_lock(&mddev->lock);
6095 mddev->bitmap_info.file = NULL;
6096 spin_unlock(&mddev->lock);
6097 fput(f);
6098 }
NeilBrown36fa3062005-09-09 16:23:45 -07006099 }
6100
NeilBrown32a76272005-06-21 17:17:14 -07006101 return err;
6102}
6103
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104/*
6105 * set_array_info is used two different ways
6106 * The original usage is when creating a new array.
6107 * In this usage, raid_disks is > 0 and it together with
6108 * level, size, not_persistent,layout,chunksize determine the
6109 * shape of the array.
6110 * This will always create an array with a type-0.90.0 superblock.
6111 * The newer usage is when assembling an array.
6112 * In this case raid_disks will be 0, and the major_version field is
6113 * use to determine which style super-blocks are to be found on the devices.
6114 * The minor and patch _version numbers are also kept incase the
6115 * super_block handler wishes to interpret them.
6116 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006117static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118{
6119
6120 if (info->raid_disks == 0) {
6121 /* just setting version number for superblock loading */
6122 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006123 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 super_types[info->major_version].name == NULL) {
6125 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006126 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 "md: superblock version %d not known\n",
6128 info->major_version);
6129 return -EINVAL;
6130 }
6131 mddev->major_version = info->major_version;
6132 mddev->minor_version = info->minor_version;
6133 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006134 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006135 /* ensure mddev_put doesn't delete this now that there
6136 * is some minimal configuration.
6137 */
6138 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 return 0;
6140 }
6141 mddev->major_version = MD_MAJOR_VERSION;
6142 mddev->minor_version = MD_MINOR_VERSION;
6143 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6144 mddev->ctime = get_seconds();
6145
6146 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006147 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006148 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 mddev->raid_disks = info->raid_disks;
6150 /* don't set md_minor, it is determined by which /dev/md* was
6151 * openned
6152 */
6153 if (info->state & (1<<MD_SB_CLEAN))
6154 mddev->recovery_cp = MaxSector;
6155 else
6156 mddev->recovery_cp = 0;
6157 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006158 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159
6160 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006161 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
6163 mddev->max_disks = MD_SB_DISKS;
6164
NeilBrowne6910632008-02-06 01:39:51 -08006165 if (mddev->persistent)
6166 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006167 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168
NeilBrownc3d97142009-12-14 12:49:52 +11006169 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006170 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006171 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006172
NeilBrownf6705572006-03-27 01:18:11 -08006173 mddev->reshape_position = MaxSector;
6174
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 /*
6176 * Generate a 128 bit UUID
6177 */
6178 get_random_bytes(mddev->uuid, 16);
6179
NeilBrownf6705572006-03-27 01:18:11 -08006180 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006181 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006182 mddev->new_layout = mddev->layout;
6183 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006184 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006185
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 return 0;
6187}
6188
NeilBrownfd01b882011-10-11 16:47:53 +11006189void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006190{
Dan Williamsb522adc2009-03-31 15:00:31 +11006191 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6192
6193 if (mddev->external_size)
6194 return;
6195
Dan Williams1f403622009-03-31 14:59:03 +11006196 mddev->array_sectors = array_sectors;
6197}
6198EXPORT_SYMBOL(md_set_array_sectors);
6199
NeilBrownfd01b882011-10-11 16:47:53 +11006200static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006201{
NeilBrown3cb03002011-10-11 16:45:26 +11006202 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006203 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006204 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006205
6206 if (mddev->pers->resize == NULL)
6207 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006208 /* The "num_sectors" is the number of sectors of each device that
6209 * is used. This can only make sense for arrays with redundancy.
6210 * linear and raid0 always use whatever space is available. We can only
6211 * consider changing this number if no resync or reconstruction is
6212 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006213 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006214 * of each device. If num_sectors is zero, we find the largest size
6215 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006216 */
NeilBrownf851b602014-12-11 10:02:10 +11006217 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6218 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006219 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006220 if (mddev->ro)
6221 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006222
NeilBrowndafb20f2012-03-19 12:46:39 +11006223 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006224 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006225
Andre Nolld71f9f82008-07-11 22:02:22 +10006226 if (fit && (num_sectors == 0 || num_sectors > avail))
6227 num_sectors = avail;
6228 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006229 return -ENOSPC;
6230 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006231 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006232 if (!rv)
6233 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006234 return rv;
6235}
6236
NeilBrownfd01b882011-10-11 16:47:53 +11006237static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006238{
6239 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006240 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006241 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006242 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006243 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006244 if (mddev->ro)
6245 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006246 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006247 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006248 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006249 if (mddev->sync_thread ||
6250 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6251 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006252 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006253
6254 rdev_for_each(rdev, mddev) {
6255 if (mddev->raid_disks < raid_disks &&
6256 rdev->data_offset < rdev->new_data_offset)
6257 return -EINVAL;
6258 if (mddev->raid_disks > raid_disks &&
6259 rdev->data_offset > rdev->new_data_offset)
6260 return -EINVAL;
6261 }
6262
NeilBrown63c70c42006-03-27 01:18:13 -08006263 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006264 if (mddev->delta_disks < 0)
6265 mddev->reshape_backwards = 1;
6266 else if (mddev->delta_disks > 0)
6267 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006268
6269 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006270 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006271 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006272 mddev->reshape_backwards = 0;
6273 }
NeilBrownda943b992006-01-06 00:20:54 -08006274 return rv;
6275}
6276
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277/*
6278 * update_array_info is used to change the configuration of an
6279 * on-line array.
6280 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6281 * fields in the info are checked against the array.
6282 * Any differences that cannot be handled will cause an error.
6283 * Normally, only one change can be managed at a time.
6284 */
NeilBrownfd01b882011-10-11 16:47:53 +11006285static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286{
6287 int rv = 0;
6288 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006289 int state = 0;
6290
6291 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006292 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006293 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
6295 if (mddev->major_version != info->major_version ||
6296 mddev->minor_version != info->minor_version ||
6297/* mddev->patch_version != info->patch_version || */
6298 mddev->ctime != info->ctime ||
6299 mddev->level != info->level ||
6300/* mddev->layout != info->layout || */
6301 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006302 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006303 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6304 ((state^info->state) & 0xfffffe00)
6305 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 return -EINVAL;
6307 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006308 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6309 cnt++;
6310 if (mddev->raid_disks != info->raid_disks)
6311 cnt++;
6312 if (mddev->layout != info->layout)
6313 cnt++;
6314 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6315 cnt++;
6316 if (cnt == 0)
6317 return 0;
6318 if (cnt > 1)
6319 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320
6321 if (mddev->layout != info->layout) {
6322 /* Change layout
6323 * we don't need to do anything at the md level, the
6324 * personality will take care of it all.
6325 */
NeilBrown50ac1682009-06-18 08:47:55 +10006326 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006328 else {
6329 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006330 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006331 if (rv)
6332 mddev->new_layout = mddev->layout;
6333 return rv;
6334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006336 if (mddev_is_clustered(mddev))
6337 md_cluster_ops->metadata_update_start(mddev);
Andre Noll58c0fed2009-03-31 14:33:13 +11006338 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006339 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340
NeilBrownda943b992006-01-06 00:20:54 -08006341 if (mddev->raid_disks != info->raid_disks)
6342 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343
NeilBrown36fa3062005-09-09 16:23:45 -07006344 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006345 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6346 rv = -EINVAL;
6347 goto err;
6348 }
6349 if (mddev->recovery || mddev->sync_thread) {
6350 rv = -EBUSY;
6351 goto err;
6352 }
NeilBrown36fa3062005-09-09 16:23:45 -07006353 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006354 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006355 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006356 if (mddev->bitmap) {
6357 rv = -EEXIST;
6358 goto err;
6359 }
6360 if (mddev->bitmap_info.default_offset == 0) {
6361 rv = -EINVAL;
6362 goto err;
6363 }
NeilBrownc3d97142009-12-14 12:49:52 +11006364 mddev->bitmap_info.offset =
6365 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006366 mddev->bitmap_info.space =
6367 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006368 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006369 bitmap = bitmap_create(mddev, -1);
6370 if (!IS_ERR(bitmap)) {
6371 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006372 rv = bitmap_load(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006373 }
NeilBrown36fa3062005-09-09 16:23:45 -07006374 if (rv)
6375 bitmap_destroy(mddev);
6376 mddev->pers->quiesce(mddev, 0);
6377 } else {
6378 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006379 if (!mddev->bitmap) {
6380 rv = -ENOENT;
6381 goto err;
6382 }
6383 if (mddev->bitmap->storage.file) {
6384 rv = -EINVAL;
6385 goto err;
6386 }
NeilBrown36fa3062005-09-09 16:23:45 -07006387 mddev->pers->quiesce(mddev, 1);
6388 bitmap_destroy(mddev);
6389 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006390 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006391 }
6392 }
NeilBrown850b2b422006-10-03 01:15:46 -07006393 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006394 if (mddev_is_clustered(mddev))
6395 md_cluster_ops->metadata_update_finish(mddev);
6396 return rv;
6397err:
6398 if (mddev_is_clustered(mddev))
6399 md_cluster_ops->metadata_update_cancel(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 return rv;
6401}
6402
NeilBrownfd01b882011-10-11 16:47:53 +11006403static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404{
NeilBrown3cb03002011-10-11 16:45:26 +11006405 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006406 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407
6408 if (mddev->pers == NULL)
6409 return -ENODEV;
6410
NeilBrown1ca69c42012-10-11 13:37:33 +11006411 rcu_read_lock();
6412 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006414 err = -ENODEV;
6415 else {
6416 md_error(mddev, rdev);
6417 if (!test_bit(Faulty, &rdev->flags))
6418 err = -EBUSY;
6419 }
6420 rcu_read_unlock();
6421 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422}
6423
Andre Noll2f9618c2008-04-25 18:57:58 +02006424/*
6425 * We have a problem here : there is no easy way to give a CHS
6426 * virtual geometry. We currently pretend that we have a 2 heads
6427 * 4 sectors (with a BIG number of cylinders...). This drives
6428 * dosfs just mad... ;-)
6429 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006430static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6431{
NeilBrownfd01b882011-10-11 16:47:53 +11006432 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006433
6434 geo->heads = 2;
6435 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006436 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006437 return 0;
6438}
6439
Nicolas Schichancb335f82014-01-15 16:58:52 +01006440static inline bool md_ioctl_valid(unsigned int cmd)
6441{
6442 switch (cmd) {
6443 case ADD_NEW_DISK:
6444 case BLKROSET:
6445 case GET_ARRAY_INFO:
6446 case GET_BITMAP_FILE:
6447 case GET_DISK_INFO:
6448 case HOT_ADD_DISK:
6449 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006450 case RAID_AUTORUN:
6451 case RAID_VERSION:
6452 case RESTART_ARRAY_RW:
6453 case RUN_ARRAY:
6454 case SET_ARRAY_INFO:
6455 case SET_BITMAP_FILE:
6456 case SET_DISK_FAULTY:
6457 case STOP_ARRAY:
6458 case STOP_ARRAY_RO:
6459 return true;
6460 default:
6461 return false;
6462 }
6463}
6464
Al Viroa39907f2008-03-02 10:31:15 -05006465static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 unsigned int cmd, unsigned long arg)
6467{
6468 int err = 0;
6469 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006470 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006471 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472
Nicolas Schichancb335f82014-01-15 16:58:52 +01006473 if (!md_ioctl_valid(cmd))
6474 return -ENOTTY;
6475
NeilBrown506c9e42011-12-23 10:17:26 +11006476 switch (cmd) {
6477 case RAID_VERSION:
6478 case GET_ARRAY_INFO:
6479 case GET_DISK_INFO:
6480 break;
6481 default:
6482 if (!capable(CAP_SYS_ADMIN))
6483 return -EACCES;
6484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485
6486 /*
6487 * Commands dealing with the RAID driver but not any
6488 * particular array:
6489 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006490 switch (cmd) {
6491 case RAID_VERSION:
6492 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006493 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006496 case RAID_AUTORUN:
6497 err = 0;
6498 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006499 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006501 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 }
6503
6504 /*
6505 * Commands creating/starting a new array:
6506 */
6507
Al Viroa39907f2008-03-02 10:31:15 -05006508 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509
6510 if (!mddev) {
6511 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006512 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513 }
6514
NeilBrown1ca69c42012-10-11 13:37:33 +11006515 /* Some actions do not requires the mutex */
6516 switch (cmd) {
6517 case GET_ARRAY_INFO:
6518 if (!mddev->raid_disks && !mddev->external)
6519 err = -ENODEV;
6520 else
6521 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006522 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006523
6524 case GET_DISK_INFO:
6525 if (!mddev->raid_disks && !mddev->external)
6526 err = -ENODEV;
6527 else
6528 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006529 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006530
6531 case SET_DISK_FAULTY:
6532 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006533 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006534
6535 case GET_BITMAP_FILE:
6536 err = get_bitmap_file(mddev, argp);
6537 goto out;
6538
NeilBrown1ca69c42012-10-11 13:37:33 +11006539 }
6540
NeilBrowna7a3f082012-12-11 13:35:54 +11006541 if (cmd == ADD_NEW_DISK)
6542 /* need to ensure md_delayed_delete() has completed */
6543 flush_workqueue(md_misc_wq);
6544
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006545 if (cmd == HOT_REMOVE_DISK)
6546 /* need to ensure recovery thread has run */
6547 wait_event_interruptible_timeout(mddev->sb_wait,
6548 !test_bit(MD_RECOVERY_NEEDED,
6549 &mddev->flags),
6550 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006551 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6552 /* Need to flush page cache, and ensure no-one else opens
6553 * and writes
6554 */
6555 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006556 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006557 mutex_unlock(&mddev->open_mutex);
6558 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006559 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006560 }
6561 set_bit(MD_STILL_CLOSED, &mddev->flags);
6562 mutex_unlock(&mddev->open_mutex);
6563 sync_blockdev(bdev);
6564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 err = mddev_lock(mddev);
6566 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006567 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6569 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006570 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 }
6572
NeilBrownc02c0ae2012-12-11 13:39:21 +11006573 if (cmd == SET_ARRAY_INFO) {
6574 mdu_array_info_t info;
6575 if (!arg)
6576 memset(&info, 0, sizeof(info));
6577 else if (copy_from_user(&info, argp, sizeof(info))) {
6578 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006579 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006580 }
6581 if (mddev->pers) {
6582 err = update_array_info(mddev, &info);
6583 if (err) {
6584 printk(KERN_WARNING "md: couldn't update"
6585 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006586 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006588 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006589 }
6590 if (!list_empty(&mddev->disks)) {
6591 printk(KERN_WARNING
6592 "md: array %s already has disks!\n",
6593 mdname(mddev));
6594 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006595 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006596 }
6597 if (mddev->raid_disks) {
6598 printk(KERN_WARNING
6599 "md: array %s already initialised!\n",
6600 mdname(mddev));
6601 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006602 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006603 }
6604 err = set_array_info(mddev, &info);
6605 if (err) {
6606 printk(KERN_WARNING "md: couldn't set"
6607 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006608 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006609 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006610 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 }
6612
6613 /*
6614 * Commands querying/configuring an existing array:
6615 */
NeilBrown32a76272005-06-21 17:17:14 -07006616 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006617 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006618 if ((!mddev->raid_disks && !mddev->external)
6619 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6620 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6621 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006623 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624 }
6625
6626 /*
6627 * Commands even a read-only array can execute:
6628 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006629 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006630 case RESTART_ARRAY_RW:
6631 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006632 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006633
6634 case STOP_ARRAY:
6635 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006636 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006637
6638 case STOP_ARRAY_RO:
6639 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006640 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006641
NeilBrown3ea8929d2013-04-24 11:42:41 +10006642 case HOT_REMOVE_DISK:
6643 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006644 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006645
NeilBrown7ceb17e2013-04-24 11:42:42 +10006646 case ADD_NEW_DISK:
6647 /* We can support ADD_NEW_DISK on read-only arrays
6648 * on if we are re-adding a preexisting device.
6649 * So require mddev->pers and MD_DISK_SYNC.
6650 */
6651 if (mddev->pers) {
6652 mdu_disk_info_t info;
6653 if (copy_from_user(&info, argp, sizeof(info)))
6654 err = -EFAULT;
6655 else if (!(info.state & (1<<MD_DISK_SYNC)))
6656 /* Need to clear read-only for this */
6657 break;
6658 else
6659 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006660 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006661 }
6662 break;
6663
NeilBrownc02c0ae2012-12-11 13:39:21 +11006664 case BLKROSET:
6665 if (get_user(ro, (int __user *)(arg))) {
6666 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006667 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006668 }
6669 err = -EINVAL;
6670
6671 /* if the bdev is going readonly the value of mddev->ro
6672 * does not matter, no writes are coming
6673 */
6674 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006675 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006676
NeilBrownc02c0ae2012-12-11 13:39:21 +11006677 /* are we are already prepared for writes? */
6678 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006679 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006680
6681 /* transitioning to readauto need only happen for
6682 * arrays that call md_write_start
6683 */
6684 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006686 if (err == 0) {
6687 mddev->ro = 2;
6688 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006689 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006690 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006691 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692 }
6693
6694 /*
6695 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006696 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 */
NeilBrown326eb172014-09-30 15:36:28 +10006698 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006699 if (mddev->ro == 2) {
6700 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006701 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006702 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006703 /* mddev_unlock will wake thread */
6704 /* If a device failed while we were read-only, we
6705 * need to make sure the metadata is updated now.
6706 */
6707 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6708 mddev_unlock(mddev);
6709 wait_event(mddev->sb_wait,
6710 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6711 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006712 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006713 }
NeilBrownf91de922005-11-08 21:39:36 -08006714 } else {
6715 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006716 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 }
6719
NeilBrownc02c0ae2012-12-11 13:39:21 +11006720 switch (cmd) {
6721 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006723 mdu_disk_info_t info;
6724 if (copy_from_user(&info, argp, sizeof(info)))
6725 err = -EFAULT;
6726 else
6727 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006728 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730
NeilBrownc02c0ae2012-12-11 13:39:21 +11006731 case HOT_ADD_DISK:
6732 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006733 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734
NeilBrownc02c0ae2012-12-11 13:39:21 +11006735 case RUN_ARRAY:
6736 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006737 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738
NeilBrownc02c0ae2012-12-11 13:39:21 +11006739 case SET_BITMAP_FILE:
6740 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006741 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006742
NeilBrownc02c0ae2012-12-11 13:39:21 +11006743 default:
6744 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006745 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 }
6747
NeilBrown3adc28d2014-09-30 15:46:41 +10006748unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006749 if (mddev->hold_active == UNTIL_IOCTL &&
6750 err != -EINVAL)
6751 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006753out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 return err;
6755}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006756#ifdef CONFIG_COMPAT
6757static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6758 unsigned int cmd, unsigned long arg)
6759{
6760 switch (cmd) {
6761 case HOT_REMOVE_DISK:
6762 case HOT_ADD_DISK:
6763 case SET_DISK_FAULTY:
6764 case SET_BITMAP_FILE:
6765 /* These take in integer arg, do not convert */
6766 break;
6767 default:
6768 arg = (unsigned long)compat_ptr(arg);
6769 break;
6770 }
6771
6772 return md_ioctl(bdev, mode, cmd, arg);
6773}
6774#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775
Al Viroa39907f2008-03-02 10:31:15 -05006776static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777{
6778 /*
6779 * Succeed if we can lock the mddev, which confirms that
6780 * it isn't being stopped right now.
6781 */
NeilBrownfd01b882011-10-11 16:47:53 +11006782 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 int err;
6784
Yuanhan Liu0c098222012-05-22 13:55:32 +10006785 if (!mddev)
6786 return -ENODEV;
6787
NeilBrownd3374822009-01-09 08:31:10 +11006788 if (mddev->gendisk != bdev->bd_disk) {
6789 /* we are racing with mddev_put which is discarding this
6790 * bd_disk.
6791 */
6792 mddev_put(mddev);
6793 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006794 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006795 /* Then retry the open from the top */
6796 return -ERESTARTSYS;
6797 }
6798 BUG_ON(mddev != bdev->bd_disk->private_data);
6799
NeilBrownc8c00a62009-08-10 12:50:52 +10006800 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 goto out;
6802
6803 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006804 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006805 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006806 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006808 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 out:
6810 return err;
6811}
6812
Al Virodb2a1442013-05-05 21:52:57 -04006813static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814{
NeilBrownf72ffdd2014-09-30 14:23:59 +10006815 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006817 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006818 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006821
6822static int md_media_changed(struct gendisk *disk)
6823{
NeilBrownfd01b882011-10-11 16:47:53 +11006824 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006825
6826 return mddev->changed;
6827}
6828
6829static int md_revalidate(struct gendisk *disk)
6830{
NeilBrownfd01b882011-10-11 16:47:53 +11006831 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006832
6833 mddev->changed = 0;
6834 return 0;
6835}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006836static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837{
6838 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006839 .open = md_open,
6840 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006841 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006842#ifdef CONFIG_COMPAT
6843 .compat_ioctl = md_compat_ioctl,
6844#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006845 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006846 .media_changed = md_media_changed,
6847 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848};
6849
NeilBrownf72ffdd2014-09-30 14:23:59 +10006850static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851{
NeilBrown2b8bf342011-10-11 16:48:23 +11006852 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 /*
6855 * md_thread is a 'system-thread', it's priority should be very
6856 * high. We avoid resource deadlocks individually in each
6857 * raid personality. (RAID5 does preallocation) We also use RR and
6858 * the very same RT priority as kswapd, thus we will never get
6859 * into a priority inversion deadlock.
6860 *
6861 * we definitely have to have equal or higher priority than
6862 * bdflush, otherwise bdflush will deadlock if there are too
6863 * many dirty RAID5 blocks.
6864 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865
NeilBrown6985c432005-10-19 21:23:47 -07006866 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006867 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868
NeilBrown93588e22005-11-15 00:09:12 -08006869 /* We need to wait INTERRUPTIBLE so that
6870 * we don't add to the load-average.
6871 * That means we need to be sure no signals are
6872 * pending
6873 */
6874 if (signal_pending(current))
6875 flush_signals(current);
6876
6877 wait_event_interruptible_timeout
6878 (thread->wqueue,
6879 test_bit(THREAD_WAKEUP, &thread->flags)
6880 || kthread_should_stop(),
6881 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882
NeilBrown6c987912011-01-14 09:13:53 +11006883 clear_bit(THREAD_WAKEUP, &thread->flags);
6884 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006885 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006887
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888 return 0;
6889}
6890
NeilBrown2b8bf342011-10-11 16:48:23 +11006891void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892{
6893 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006894 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 set_bit(THREAD_WAKEUP, &thread->flags);
6896 wake_up(&thread->wqueue);
6897 }
6898}
NeilBrown6c144d32014-09-30 16:15:38 +10006899EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900
Shaohua Li4ed87312012-10-11 13:34:00 +11006901struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6902 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903{
NeilBrown2b8bf342011-10-11 16:48:23 +11006904 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905
NeilBrown2b8bf342011-10-11 16:48:23 +11006906 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907 if (!thread)
6908 return NULL;
6909
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 init_waitqueue_head(&thread->wqueue);
6911
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912 thread->run = run;
6913 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006914 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006915 thread->tsk = kthread_run(md_thread, thread,
6916 "%s_%s",
6917 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006918 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006919 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920 kfree(thread);
6921 return NULL;
6922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923 return thread;
6924}
NeilBrown6c144d32014-09-30 16:15:38 +10006925EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926
NeilBrown2b8bf342011-10-11 16:48:23 +11006927void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928{
NeilBrown2b8bf342011-10-11 16:48:23 +11006929 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006930 if (!thread)
6931 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006932 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006933 /* Locking ensures that mddev_unlock does not wake_up a
6934 * non-existent thread
6935 */
6936 spin_lock(&pers_lock);
6937 *threadp = NULL;
6938 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006939
6940 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 kfree(thread);
6942}
NeilBrown6c144d32014-09-30 16:15:38 +10006943EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944
NeilBrownfd01b882011-10-11 16:47:53 +11006945void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946{
NeilBrownb2d444d2005-11-08 21:39:31 -08006947 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006949
NeilBrownde393cd2011-07-28 11:31:48 +10006950 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951 return;
6952 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006953 if (mddev->degraded)
6954 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006955 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6957 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6958 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006959 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006960 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006961 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962}
NeilBrown6c144d32014-09-30 16:15:38 +10006963EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964
6965/* seq_file implementation /proc/mdstat */
6966
6967static void status_unused(struct seq_file *seq)
6968{
6969 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006970 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971
6972 seq_printf(seq, "unused devices: ");
6973
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006974 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 char b[BDEVNAME_SIZE];
6976 i++;
6977 seq_printf(seq, "%s ",
6978 bdevname(rdev->bdev,b));
6979 }
6980 if (!i)
6981 seq_printf(seq, "<none>");
6982
6983 seq_printf(seq, "\n");
6984}
6985
NeilBrownf72ffdd2014-09-30 14:23:59 +10006986static void status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987{
NeilBrowndd71cf62009-05-07 12:49:35 +10006988 sector_t max_sectors, resync, res;
6989 unsigned long dt, db;
6990 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006991 int scale;
6992 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993
NeilBrown72f36d52012-10-11 14:25:57 +11006994 if (mddev->curr_resync <= 3)
6995 resync = 0;
6996 else
6997 resync = mddev->curr_resync
6998 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999
NeilBrownc804cde2012-05-21 09:28:33 +10007000 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7001 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007002 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007004 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005
NeilBrown403df472014-09-30 15:52:29 +10007006 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007007 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007008 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007009 * u32, as those are the requirements for sector_div.
7010 * Thus 'scale' must be at least 10
7011 */
7012 scale = 10;
7013 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007014 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007015 scale++;
7016 }
7017 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007018 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007019
7020 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021 {
NeilBrown4588b422006-03-27 01:18:04 -08007022 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023 seq_printf(seq, "[");
7024 for (i = 0; i < x; i++)
7025 seq_printf(seq, "=");
7026 seq_printf(seq, ">");
7027 for (i = 0; i < y; i++)
7028 seq_printf(seq, ".");
7029 seq_printf(seq, "] ");
7030 }
NeilBrown4588b422006-03-27 01:18:04 -08007031 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007032 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7033 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007034 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7035 "check" :
7036 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7037 "resync" : "recovery"))),
7038 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007039 (unsigned long long) resync/2,
7040 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041
7042 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 * dt: time from mark until now
7044 * db: blocks written from mark until now
7045 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007046 *
7047 * rt is a sector_t, so could be 32bit or 64bit.
7048 * So we divide before multiply in case it is 32bit and close
7049 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007050 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007051 * near the end of resync when the number of remaining sectors
7052 * is close to 'db'.
7053 * We then divide rt by 32 after multiplying by db to compensate.
7054 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 */
7056 dt = ((jiffies - mddev->resync_mark) / HZ);
7057 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007058 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7059 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060
NeilBrowndd71cf62009-05-07 12:49:35 +10007061 rt = max_sectors - resync; /* number of remaining sectors */
7062 sector_div(rt, db/32+1);
7063 rt *= dt;
7064 rt >>= 5;
7065
7066 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7067 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068
NeilBrownff4e8d92006-07-10 04:44:16 -07007069 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070}
7071
7072static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7073{
7074 struct list_head *tmp;
7075 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007076 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077
7078 if (l >= 0x10000)
7079 return NULL;
7080 if (!l--)
7081 /* header */
7082 return (void*)1;
7083
7084 spin_lock(&all_mddevs_lock);
7085 list_for_each(tmp,&all_mddevs)
7086 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007087 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088 mddev_get(mddev);
7089 spin_unlock(&all_mddevs_lock);
7090 return mddev;
7091 }
7092 spin_unlock(&all_mddevs_lock);
7093 if (!l--)
7094 return (void*)2;/* tail */
7095 return NULL;
7096}
7097
7098static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7099{
7100 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007101 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007102
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 ++*pos;
7104 if (v == (void*)2)
7105 return NULL;
7106
7107 spin_lock(&all_mddevs_lock);
7108 if (v == (void*)1)
7109 tmp = all_mddevs.next;
7110 else
7111 tmp = mddev->all_mddevs.next;
7112 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007113 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114 else {
7115 next_mddev = (void*)2;
7116 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 spin_unlock(&all_mddevs_lock);
7119
7120 if (v != (void*)1)
7121 mddev_put(mddev);
7122 return next_mddev;
7123
7124}
7125
7126static void md_seq_stop(struct seq_file *seq, void *v)
7127{
NeilBrownfd01b882011-10-11 16:47:53 +11007128 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129
7130 if (mddev && v != (void*)1 && v != (void*)2)
7131 mddev_put(mddev);
7132}
7133
7134static int md_seq_show(struct seq_file *seq, void *v)
7135{
NeilBrownfd01b882011-10-11 16:47:53 +11007136 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007137 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007138 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139
7140 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007141 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142 seq_printf(seq, "Personalities : ");
7143 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007144 list_for_each_entry(pers, &pers_list, list)
7145 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146
7147 spin_unlock(&pers_lock);
7148 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007149 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 return 0;
7151 }
7152 if (v == (void*)2) {
7153 status_unused(seq);
7154 return 0;
7155 }
7156
NeilBrown36d091f2014-12-15 12:56:58 +11007157 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7159 seq_printf(seq, "%s : %sactive", mdname(mddev),
7160 mddev->pers ? "" : "in");
7161 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007162 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007164 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007165 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166 seq_printf(seq, " %s", mddev->pers->name);
7167 }
7168
Andre Nolldd8ac332009-03-31 14:33:13 +11007169 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007170 rcu_read_lock();
7171 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172 char b[BDEVNAME_SIZE];
7173 seq_printf(seq, " %s[%d]",
7174 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007175 if (test_bit(WriteMostly, &rdev->flags))
7176 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007177 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178 seq_printf(seq, "(F)");
7179 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007180 }
7181 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007182 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007183 if (test_bit(Replacement, &rdev->flags))
7184 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007185 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007187 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188
7189 if (!list_empty(&mddev->disks)) {
7190 if (mddev->pers)
7191 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007192 (unsigned long long)
7193 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194 else
7195 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007196 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007198 if (mddev->persistent) {
7199 if (mddev->major_version != 0 ||
7200 mddev->minor_version != 90) {
7201 seq_printf(seq," super %d.%d",
7202 mddev->major_version,
7203 mddev->minor_version);
7204 }
NeilBrowne6910632008-02-06 01:39:51 -08007205 } else if (mddev->external)
7206 seq_printf(seq, " super external:%s",
7207 mddev->metadata_type);
7208 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007209 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210
7211 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007212 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007213 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007214 if (mddev->pers->sync_request) {
7215 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007216 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007217 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007218 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007219 seq_printf(seq, "\tresync=DELAYED\n ");
7220 else if (mddev->recovery_cp < MaxSector)
7221 seq_printf(seq, "\tresync=PENDING\n ");
7222 }
NeilBrown32a76272005-06-21 17:17:14 -07007223 } else
7224 seq_printf(seq, "\n ");
7225
NeilBrown57148962012-03-19 12:46:40 +11007226 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227
7228 seq_printf(seq, "\n");
7229 }
NeilBrown36d091f2014-12-15 12:56:58 +11007230 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007231
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232 return 0;
7233}
7234
Jan Engelhardt110518b2009-05-07 12:49:37 +10007235static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 .start = md_seq_start,
7237 .next = md_seq_next,
7238 .stop = md_seq_stop,
7239 .show = md_seq_show,
7240};
7241
7242static int md_seq_open(struct inode *inode, struct file *file)
7243{
Kay Sieversf1514632011-07-12 20:48:39 +02007244 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245 int error;
7246
7247 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007248 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007249 return error;
7250
7251 seq = file->private_data;
7252 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 return error;
7254}
7255
NeilBrowne2f23b62014-04-09 14:33:51 +10007256static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007257static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7258{
Kay Sieversf1514632011-07-12 20:48:39 +02007259 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007260 int mask;
7261
NeilBrowne2f23b62014-04-09 14:33:51 +10007262 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007263 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007264 poll_wait(filp, &md_event_waiters, wait);
7265
7266 /* always allow read */
7267 mask = POLLIN | POLLRDNORM;
7268
Kay Sieversf1514632011-07-12 20:48:39 +02007269 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007270 mask |= POLLERR | POLLPRI;
7271 return mask;
7272}
7273
Arjan van de Venfa027c22007-02-12 00:55:33 -08007274static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007275 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276 .open = md_seq_open,
7277 .read = seq_read,
7278 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007279 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007280 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281};
7282
NeilBrown84fc4b52011-10-11 16:49:58 +11007283int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284{
Chao Yu50bd3772014-09-25 15:28:34 +08007285 printk(KERN_INFO "md: %s personality registered for level %d\n",
7286 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007288 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289 spin_unlock(&pers_lock);
7290 return 0;
7291}
NeilBrown6c144d32014-09-30 16:15:38 +10007292EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293
NeilBrown84fc4b52011-10-11 16:49:58 +11007294int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295{
NeilBrown2604b702006-01-06 00:20:36 -08007296 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007298 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299 spin_unlock(&pers_lock);
7300 return 0;
7301}
NeilBrown6c144d32014-09-30 16:15:38 +10007302EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007304int register_md_cluster_operations(struct md_cluster_operations *ops, struct module *module)
7305{
7306 if (md_cluster_ops != NULL)
7307 return -EALREADY;
7308 spin_lock(&pers_lock);
7309 md_cluster_ops = ops;
7310 md_cluster_mod = module;
7311 spin_unlock(&pers_lock);
7312 return 0;
7313}
7314EXPORT_SYMBOL(register_md_cluster_operations);
7315
7316int unregister_md_cluster_operations(void)
7317{
7318 spin_lock(&pers_lock);
7319 md_cluster_ops = NULL;
7320 spin_unlock(&pers_lock);
7321 return 0;
7322}
7323EXPORT_SYMBOL(unregister_md_cluster_operations);
7324
7325int md_setup_cluster(struct mddev *mddev, int nodes)
7326{
7327 int err;
7328
7329 err = request_module("md-cluster");
7330 if (err) {
7331 pr_err("md-cluster module not found.\n");
7332 return err;
7333 }
7334
7335 spin_lock(&pers_lock);
7336 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7337 spin_unlock(&pers_lock);
7338 return -ENOENT;
7339 }
7340 spin_unlock(&pers_lock);
7341
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007342 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007343}
7344
7345void md_cluster_stop(struct mddev *mddev)
7346{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007347 if (!md_cluster_ops)
7348 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007349 md_cluster_ops->leave(mddev);
7350 module_put(md_cluster_mod);
7351}
7352
NeilBrownfd01b882011-10-11 16:47:53 +11007353static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007355 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007357 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358
7359 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007360 rcu_read_lock();
7361 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007363 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7364 (int)part_stat_read(&disk->part0, sectors[1]) -
7365 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007366 /* sync IO will cause sync_io to increase before the disk_stats
7367 * as sync_io is counted when a request starts, and
7368 * disk_stats is counted when it completes.
7369 * So resync activity will cause curr_events to be smaller than
7370 * when there was no such activity.
7371 * non-sync IO will cause disk_stat to increase without
7372 * increasing sync_io so curr_events will (eventually)
7373 * be larger than it was before. Once it becomes
7374 * substantially larger, the test below will cause
7375 * the array to appear non-idle, and resync will slow
7376 * down.
7377 * If there is a lot of outstanding resync activity when
7378 * we set last_event to curr_events, then all that activity
7379 * completing might cause the array to appear non-idle
7380 * and resync will be slowed down even though there might
7381 * not have been non-resync activity. This will only
7382 * happen once though. 'last_events' will soon reflect
7383 * the state where there is little or no outstanding
7384 * resync requests, and further resync activity will
7385 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007386 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007388 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389 rdev->last_events = curr_events;
7390 idle = 0;
7391 }
7392 }
NeilBrown4b809912008-07-21 17:05:25 +10007393 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394 return idle;
7395}
7396
NeilBrownfd01b882011-10-11 16:47:53 +11007397void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398{
7399 /* another "blocks" (512byte) blocks have been synced */
7400 atomic_sub(blocks, &mddev->recovery_active);
7401 wake_up(&mddev->recovery_wait);
7402 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007403 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007404 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405 md_wakeup_thread(mddev->thread);
7406 // stop recovery, signal do_sync ....
7407 }
7408}
NeilBrown6c144d32014-09-30 16:15:38 +10007409EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410
NeilBrown06d91a52005-06-21 17:17:12 -07007411/* md_write_start(mddev, bi)
7412 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007413 * in superblock) before writing, schedule a superblock update
7414 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007415 */
NeilBrownfd01b882011-10-11 16:47:53 +11007416void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417{
Neil Brown0fd62b82008-06-28 08:31:36 +10007418 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007419 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007420 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007421
NeilBrownf91de922005-11-08 21:39:36 -08007422 BUG_ON(mddev->ro == 1);
7423 if (mddev->ro == 2) {
7424 /* need to switch to read/write */
7425 mddev->ro = 0;
7426 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7427 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007428 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007429 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007430 }
NeilBrown06d91a52005-06-21 17:17:12 -07007431 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007432 if (mddev->safemode == 1)
7433 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007434 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007435 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007436 if (mddev->in_sync) {
7437 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007438 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007439 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007440 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007441 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007442 }
NeilBrown85572d72014-12-15 12:56:56 +11007443 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007444 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007445 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007446 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007447 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007448 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449}
NeilBrown6c144d32014-09-30 16:15:38 +10007450EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451
NeilBrownfd01b882011-10-11 16:47:53 +11007452void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453{
7454 if (atomic_dec_and_test(&mddev->writes_pending)) {
7455 if (mddev->safemode == 2)
7456 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007457 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7459 }
7460}
NeilBrown6c144d32014-09-30 16:15:38 +10007461EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462
NeilBrown2a2275d2007-01-26 00:57:11 -08007463/* md_allow_write(mddev)
7464 * Calling this ensures that the array is marked 'active' so that writes
7465 * may proceed without blocking. It is important to call this before
7466 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7467 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007468 *
7469 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7470 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007471 */
NeilBrownfd01b882011-10-11 16:47:53 +11007472int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007473{
7474 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007475 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007476 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007477 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007478 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007479 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007480
NeilBrown85572d72014-12-15 12:56:56 +11007481 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007482 if (mddev->in_sync) {
7483 mddev->in_sync = 0;
7484 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007485 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007486 if (mddev->safemode_delay &&
7487 mddev->safemode == 0)
7488 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007489 spin_unlock(&mddev->lock);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007490 if (mddev_is_clustered(mddev))
7491 md_cluster_ops->metadata_update_start(mddev);
NeilBrown2a2275d2007-01-26 00:57:11 -08007492 md_update_sb(mddev, 0);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007493 if (mddev_is_clustered(mddev))
7494 md_cluster_ops->metadata_update_finish(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007495 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007496 } else
NeilBrown85572d72014-12-15 12:56:56 +11007497 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007498
NeilBrown070dc6d2010-08-30 17:33:34 +10007499 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007500 return -EAGAIN;
7501 else
7502 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007503}
7504EXPORT_SYMBOL_GPL(md_allow_write);
7505
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506#define SYNC_MARKS 10
7507#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007508#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007509void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510{
Shaohua Li4ed87312012-10-11 13:34:00 +11007511 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007512 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 unsigned int currspeed = 0,
7514 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007515 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007517 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 sector_t mark_cnt[SYNC_MARKS];
7519 int last_mark,m;
7520 struct list_head *tmp;
7521 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007522 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007523 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007524 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007525 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526
7527 /* just incase thread restarts... */
7528 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7529 return;
NeilBrown3991b312014-05-28 13:39:23 +10007530 if (mddev->ro) {/* never try to sync a read-only array */
7531 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007532 return;
NeilBrown3991b312014-05-28 13:39:23 +10007533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534
NeilBrown61df9d92006-10-03 01:15:57 -07007535 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007536 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007537 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007538 action = "check";
7539 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007540 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007541 action = "repair";
7542 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007543 desc = "resync";
7544 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7545 desc = "reshape";
7546 else
7547 desc = "recovery";
7548
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007549 mddev->last_sync_action = action ?: desc;
7550
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 /* we overload curr_resync somewhat here.
7552 * 0 == not engaged in resync at all
7553 * 2 == checking that there is no conflict with another sync
7554 * 1 == like 2, but have yielded to allow conflicting resync to
7555 * commense
7556 * other == active in resync - this many blocks
7557 *
7558 * Before starting a resync we must have set curr_resync to
7559 * 2, and then checked that every "conflicting" array has curr_resync
7560 * less than ours. When we find one that is the same or higher
7561 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7562 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7563 * This will mean we have to start checking from the beginning again.
7564 *
7565 */
7566
7567 do {
7568 mddev->curr_resync = 2;
7569
7570 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007571 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007573 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 if (mddev2 == mddev)
7575 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007576 if (!mddev->parallel_resync
7577 && mddev2->curr_resync
7578 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579 DEFINE_WAIT(wq);
7580 if (mddev < mddev2 && mddev->curr_resync == 2) {
7581 /* arbitrarily yield */
7582 mddev->curr_resync = 1;
7583 wake_up(&resync_wait);
7584 }
7585 if (mddev > mddev2 && mddev->curr_resync == 1)
7586 /* no need to wait here, we can wait the next
7587 * time 'round when curr_resync == 2
7588 */
7589 continue;
NeilBrown97441972008-09-19 11:49:54 +10007590 /* We need to wait 'interruptible' so as not to
7591 * contribute to the load average, and not to
7592 * be caught by 'softlockup'
7593 */
7594 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007595 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007596 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007597 printk(KERN_INFO "md: delaying %s of %s"
7598 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007600 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007601 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007602 if (signal_pending(current))
7603 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604 schedule();
7605 finish_wait(&resync_wait, &wq);
7606 goto try_again;
7607 }
7608 finish_wait(&resync_wait, &wq);
7609 }
7610 }
7611 } while (mddev->curr_resync < 2);
7612
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007613 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007614 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007616 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617 */
7618 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007619 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007620 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007621 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7622 j = mddev->resync_min;
7623 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007624 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007625
NeilBrownccfcc3c2006-03-27 01:18:09 -08007626 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007627 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007628 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007630 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007631 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007632 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007633 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007634 if (rdev->raid_disk >= 0 &&
7635 !test_bit(Faulty, &rdev->flags) &&
7636 !test_bit(In_sync, &rdev->flags) &&
7637 rdev->recovery_offset < j)
7638 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007639 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007640
7641 /* If there is a bitmap, we need to make sure all
7642 * writes that started before we added a spare
7643 * complete before we start doing a recovery.
7644 * Otherwise the write might complete and (via
7645 * bitmap_endwrite) set a bit in the bitmap after the
7646 * recovery has checked that bit and skipped that
7647 * region.
7648 */
7649 if (mddev->bitmap) {
7650 mddev->pers->quiesce(mddev, 1);
7651 mddev->pers->quiesce(mddev, 0);
7652 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007654
NeilBrown61df9d92006-10-03 01:15:57 -07007655 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7656 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7657 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007658 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007659 "(but not more than %d KB/sec) for %s.\n",
7660 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661
NeilBrowneea1bf32009-03-31 14:27:02 +11007662 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007663
NeilBrown57afd892005-06-21 17:17:13 -07007664 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665 for (m = 0; m < SYNC_MARKS; m++) {
7666 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007667 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668 }
7669 last_mark = 0;
7670 mddev->resync_mark = mark[last_mark];
7671 mddev->resync_mark_cnt = mark_cnt[last_mark];
7672
7673 /*
7674 * Tune reconstruction:
7675 */
7676 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007677 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7678 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679
7680 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681 last_check = 0;
7682
7683 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007684 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007685 "md: resuming %s of %s from checkpoint.\n",
7686 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007687 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007688 } else
7689 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007690 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007691 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7692 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007693 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007695 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -05007696 md_cluster_ops->resync_start(mddev, j, max_sectors);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007697
majianpeng7c2c57c2012-07-03 12:12:26 +10007698 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007700 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007701
NeilBrown57afd892005-06-21 17:17:13 -07007702 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007703
NeilBrown7a91ee12009-05-26 12:57:21 +10007704 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7705 ((mddev->curr_resync > mddev->curr_resync_completed &&
7706 (mddev->curr_resync - mddev->curr_resync_completed)
7707 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007708 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007709 (j - mddev->curr_resync_completed)*2
7710 >= mddev->resync_max - mddev->curr_resync_completed
7711 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007712 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007713 wait_event(mddev->recovery_wait,
7714 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007715 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007716 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7717 j > mddev->recovery_cp)
7718 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007719 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007720 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007721 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007722 }
NeilBrownacb180b2009-04-14 16:28:34 +10007723
NeilBrownc91abf52013-11-19 12:02:01 +11007724 while (j >= mddev->resync_max &&
7725 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007726 /* As this condition is controlled by user-space,
7727 * we can block indefinitely, so use '_interruptible'
7728 * to avoid triggering warnings.
7729 */
7730 flush_signals(current); /* just in case */
7731 wait_event_interruptible(mddev->recovery_wait,
7732 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007733 || test_bit(MD_RECOVERY_INTR,
7734 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007735 }
NeilBrownacb180b2009-04-14 16:28:34 +10007736
NeilBrownc91abf52013-11-19 12:02:01 +11007737 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7738 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007739
NeilBrown57afd892005-06-21 17:17:13 -07007740 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007741 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007742 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007743 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007744 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745 }
NeilBrown57afd892005-06-21 17:17:13 -07007746
7747 if (!skipped) { /* actual IO requested */
7748 io_sectors += sectors;
7749 atomic_add(sectors, &mddev->recovery_active);
7750 }
7751
NeilBrowne875ece2011-07-28 11:39:24 +10007752 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7753 break;
7754
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007756 if (j > 2)
7757 mddev->curr_resync = j;
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007758 if (mddev_is_clustered(mddev))
7759 md_cluster_ops->resync_info_update(mddev, j, max_sectors);
NeilBrownff4e8d92006-07-10 04:44:16 -07007760 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007761 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007762 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007763 * visible in /proc/mdstat
7764 */
7765 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007766
7767 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007768 continue;
7769
NeilBrown57afd892005-06-21 17:17:13 -07007770 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771 repeat:
7772 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7773 /* step marks */
7774 int next = (last_mark+1) % SYNC_MARKS;
7775
7776 mddev->resync_mark = mark[next];
7777 mddev->resync_mark_cnt = mark_cnt[next];
7778 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007779 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780 last_mark = next;
7781 }
7782
NeilBrownc91abf52013-11-19 12:02:01 +11007783 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7784 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785
7786 /*
7787 * this loop exits only if either when we are slower than
7788 * the 'hard' speed limit, or the system was IO-idle for
7789 * a jiffy.
7790 * the system might be non-idle CPU-wise, but we only care
7791 * about not overloading the IO subsystem. (things like an
7792 * e2fsck being done on the RAID array should execute fast)
7793 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007794 cond_resched();
7795
Xiao Niac7e50a2014-08-07 09:37:41 -04007796 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7797 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07007798 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007799
NeilBrown88202a02006-01-06 00:21:36 -08007800 if (currspeed > speed_min(mddev)) {
7801 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007802 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007803 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804 goto repeat;
7805 }
7806 }
7807 }
NeilBrownc91abf52013-11-19 12:02:01 +11007808 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7809 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7810 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811 /*
7812 * this also signals 'finished resyncing' to md_stop
7813 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007814 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007815 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7816
7817 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007818 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007819
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007820 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -05007821 md_cluster_ops->resync_finish(mddev);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007822
NeilBrowndfc70642008-05-23 13:04:39 -07007823 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007824 mddev->curr_resync > 2) {
7825 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7826 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7827 if (mddev->curr_resync >= mddev->recovery_cp) {
7828 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007829 "md: checkpointing %s of %s.\n",
7830 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007831 if (test_bit(MD_RECOVERY_ERROR,
7832 &mddev->recovery))
7833 mddev->recovery_cp =
7834 mddev->curr_resync_completed;
7835 else
7836 mddev->recovery_cp =
7837 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007838 }
7839 } else
7840 mddev->recovery_cp = MaxSector;
7841 } else {
7842 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7843 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007844 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007845 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007846 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007847 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007848 !test_bit(Faulty, &rdev->flags) &&
7849 !test_bit(In_sync, &rdev->flags) &&
7850 rdev->recovery_offset < mddev->curr_resync)
7851 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007852 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007855 skip:
NeilBrown17571282006-12-10 02:20:52 -08007856 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857
NeilBrown23da4222014-12-15 12:57:01 +11007858 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11007859 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7860 /* We completed so min/max setting can be forgotten if used. */
7861 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7862 mddev->resync_min = 0;
7863 mddev->resync_max = MaxSector;
7864 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7865 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007866 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11007867 spin_unlock(&mddev->lock);
7868
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869 wake_up(&resync_wait);
7870 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7871 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007872 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873}
NeilBrown29269552006-03-27 01:18:10 -08007874EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875
NeilBrown746d3202013-04-24 11:42:41 +10007876static int remove_and_add_spares(struct mddev *mddev,
7877 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007878{
NeilBrown3cb03002011-10-11 16:45:26 +11007879 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007880 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007881 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007882
NeilBrowndafb20f2012-03-19 12:46:39 +11007883 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007884 if ((this == NULL || rdev == this) &&
7885 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007886 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007887 (test_bit(Faulty, &rdev->flags) ||
7888 ! test_bit(In_sync, &rdev->flags)) &&
7889 atomic_read(&rdev->nr_pending)==0) {
7890 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007891 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007892 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007893 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007894 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007895 }
7896 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007897 if (removed && mddev->kobj.sd)
7898 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007899
NeilBrown746d3202013-04-24 11:42:41 +10007900 if (this)
7901 goto no_add;
7902
NeilBrowndafb20f2012-03-19 12:46:39 +11007903 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007904 if (rdev->raid_disk >= 0 &&
7905 !test_bit(In_sync, &rdev->flags) &&
7906 !test_bit(Faulty, &rdev->flags))
7907 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007908 if (rdev->raid_disk >= 0)
7909 continue;
7910 if (test_bit(Faulty, &rdev->flags))
7911 continue;
7912 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11007913 ! (rdev->saved_raid_disk >= 0 &&
7914 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10007915 continue;
7916
NeilBrown7eb41882014-01-14 15:55:14 +11007917 if (rdev->saved_raid_disk < 0)
7918 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007919 if (mddev->pers->
7920 hot_add_disk(mddev, rdev) == 0) {
7921 if (sysfs_link_rdev(mddev, rdev))
7922 /* failure here is OK */;
7923 spares++;
7924 md_new_event(mddev);
7925 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007926 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007927 }
NeilBrown746d3202013-04-24 11:42:41 +10007928no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007929 if (removed)
7930 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007931 return spares;
7932}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007933
NeilBrownac05f252014-09-30 08:10:42 +10007934static void md_start_sync(struct work_struct *ws)
7935{
7936 struct mddev *mddev = container_of(ws, struct mddev, del_work);
7937
7938 mddev->sync_thread = md_register_thread(md_do_sync,
7939 mddev,
7940 "resync");
7941 if (!mddev->sync_thread) {
7942 printk(KERN_ERR "%s: could not start resync"
7943 " thread...\n",
7944 mdname(mddev));
7945 /* leave the spares where they are, it shouldn't hurt */
7946 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7947 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7948 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7949 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7950 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11007951 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10007952 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7953 &mddev->recovery))
7954 if (mddev->sysfs_action)
7955 sysfs_notify_dirent_safe(mddev->sysfs_action);
7956 } else
7957 md_wakeup_thread(mddev->sync_thread);
7958 sysfs_notify_dirent_safe(mddev->sysfs_action);
7959 md_new_event(mddev);
7960}
7961
Linus Torvalds1da177e2005-04-16 15:20:36 -07007962/*
7963 * This routine is regularly called by all per-raid-array threads to
7964 * deal with generic issues like resync and super-block update.
7965 * Raid personalities that don't have a thread (linear/raid0) do not
7966 * need this as they never do any recovery or update the superblock.
7967 *
7968 * It does not do any resync itself, but rather "forks" off other threads
7969 * to do that as needed.
7970 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7971 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007972 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973 * and wakeups up this thread which will reap the thread and finish up.
7974 * This thread also removes any faulty devices (with nr_pending == 0).
7975 *
7976 * The overall approach is:
7977 * 1/ if the superblock needs updating, update it.
7978 * 2/ If a recovery thread is running, don't do anything else.
7979 * 3/ If recovery has finished, clean up, possibly marking spares active.
7980 * 4/ If there are any faulty devices, remove them.
7981 * 5/ If array is degraded, try to add spares devices
7982 * 6/ If array has spares or is not in-sync, start a resync thread.
7983 */
NeilBrownfd01b882011-10-11 16:47:53 +11007984void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007986 if (mddev->suspended)
7987 return;
7988
NeilBrown5f404022005-06-21 17:17:16 -07007989 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007990 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991
NeilBrownfca4d842005-06-21 17:17:11 -07007992 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007993 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007994 printk(KERN_INFO "md: %s in immediate safe mode\n",
7995 mdname(mddev));
7996 mddev->safemode = 2;
7997 }
7998 flush_signals(current);
7999 }
8000
NeilBrownc89a8ee2008-08-05 15:54:13 +10008001 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8002 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008004 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008006 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008007 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008008 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8009 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008010 ))
8011 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008012
NeilBrowndf5b89b2006-03-27 01:18:20 -08008013 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008014 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008015
NeilBrownc89a8ee2008-08-05 15:54:13 +10008016 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10008017 /* On a read-only array we can:
8018 * - remove failed devices
8019 * - add already-in_sync devices if the array itself
8020 * is in-sync.
8021 * As we only add devices that are already in-sync,
8022 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008023 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008024 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008025 /* There is no thread, but we need to call
8026 * ->spare_active and clear saved_raid_disk
8027 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008028 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008029 md_reap_sync_thread(mddev);
8030 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008031 goto unlock;
8032 }
8033
NeilBrown31a59e32008-04-30 00:52:30 -07008034 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008035 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008036 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008037 if (mddev->safemode &&
8038 !atomic_read(&mddev->writes_pending) &&
8039 !mddev->in_sync &&
8040 mddev->recovery_cp == MaxSector) {
8041 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008042 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008043 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008044 }
8045 if (mddev->safemode == 1)
8046 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008047 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008048 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008049 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008050 }
NeilBrownfca4d842005-06-21 17:17:11 -07008051
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008052 if (mddev->flags & MD_UPDATE_SB_FLAGS) {
8053 if (mddev_is_clustered(mddev))
8054 md_cluster_ops->metadata_update_start(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07008055 md_update_sb(mddev, 0);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008056 if (mddev_is_clustered(mddev))
8057 md_cluster_ops->metadata_update_finish(mddev);
8058 }
NeilBrown06d91a52005-06-21 17:17:12 -07008059
Linus Torvalds1da177e2005-04-16 15:20:36 -07008060 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8061 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8062 /* resync/recovery still happening */
8063 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8064 goto unlock;
8065 }
8066 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008067 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068 goto unlock;
8069 }
Neil Brown72a23c22008-06-28 08:31:41 +10008070 /* Set RUNNING before clearing NEEDED to avoid
8071 * any transients in the value of "sync_action".
8072 */
NeilBrown72f36d52012-10-11 14:25:57 +11008073 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008074 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008075 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008076 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008077 /* Clear some bits that don't mean anything, but
8078 * might be left set
8079 */
NeilBrown24dd4692005-11-08 21:39:26 -08008080 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8081 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008082
NeilBrowned209582012-04-24 10:23:14 +10008083 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8084 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008085 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008086 /* no recovery is running.
8087 * remove any failed drives, then
8088 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008089 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090 * the personality to fail the re-add.
8091 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008093 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008094 if (mddev->pers->check_reshape == NULL ||
8095 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008096 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008097 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008098 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008099 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008100 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008101 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8102 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008103 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008104 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008105 } else if (mddev->recovery_cp < MaxSector) {
8106 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008107 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008108 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8109 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008110 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008111
Linus Torvalds1da177e2005-04-16 15:20:36 -07008112 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008113 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008114 /* We are adding a device or devices to an array
8115 * which has the bitmap stored on all devices.
8116 * So make sure all bitmap pages get written
8117 */
8118 bitmap_write_all(mddev->bitmap);
8119 }
NeilBrownac05f252014-09-30 08:10:42 +10008120 INIT_WORK(&mddev->del_work, md_start_sync);
8121 queue_work(md_misc_wq, &mddev->del_work);
8122 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008123 }
NeilBrownac05f252014-09-30 08:10:42 +10008124 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008125 if (!mddev->sync_thread) {
8126 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008127 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008128 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8129 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008130 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008131 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008132 }
NeilBrownac05f252014-09-30 08:10:42 +10008133 unlock:
8134 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008135 mddev_unlock(mddev);
8136 }
8137}
NeilBrown6c144d32014-09-30 16:15:38 +10008138EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008139
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008140void md_reap_sync_thread(struct mddev *mddev)
8141{
8142 struct md_rdev *rdev;
8143
8144 /* resync has finished, collect result */
8145 md_unregister_thread(&mddev->sync_thread);
8146 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8147 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8148 /* success...*/
8149 /* activate any spares */
8150 if (mddev->pers->spare_active(mddev)) {
8151 sysfs_notify(&mddev->kobj, NULL,
8152 "degraded");
8153 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8154 }
8155 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008156 if (mddev_is_clustered(mddev))
8157 md_cluster_ops->metadata_update_start(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008158 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8159 mddev->pers->finish_reshape)
8160 mddev->pers->finish_reshape(mddev);
8161
8162 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008163 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008164 */
NeilBrownf4667222013-12-09 12:04:56 +11008165 if (!mddev->degraded)
8166 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008167 rdev->saved_raid_disk = -1;
8168
8169 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008170 if (mddev_is_clustered(mddev))
8171 md_cluster_ops->metadata_update_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008172 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8173 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8174 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8175 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8176 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008177 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008178 /* flag recovery needed just to double check */
8179 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8180 sysfs_notify_dirent_safe(mddev->sysfs_action);
8181 md_new_event(mddev);
8182 if (mddev->event_work.func)
8183 queue_work(md_misc_wq, &mddev->event_work);
8184}
NeilBrown6c144d32014-09-30 16:15:38 +10008185EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008186
NeilBrownfd01b882011-10-11 16:47:53 +11008187void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008188{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008189 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008190 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008191 !test_bit(Blocked, &rdev->flags) &&
8192 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008193 msecs_to_jiffies(5000));
8194 rdev_dec_pending(rdev, mddev);
8195}
8196EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8197
NeilBrownc6563a82012-05-21 09:27:00 +10008198void md_finish_reshape(struct mddev *mddev)
8199{
8200 /* called be personality module when reshape completes. */
8201 struct md_rdev *rdev;
8202
8203 rdev_for_each(rdev, mddev) {
8204 if (rdev->data_offset > rdev->new_data_offset)
8205 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8206 else
8207 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8208 rdev->data_offset = rdev->new_data_offset;
8209 }
8210}
8211EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008212
8213/* Bad block management.
8214 * We can record which blocks on each device are 'bad' and so just
8215 * fail those blocks, or that stripe, rather than the whole device.
8216 * Entries in the bad-block table are 64bits wide. This comprises:
8217 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8218 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8219 * A 'shift' can be set so that larger blocks are tracked and
8220 * consequently larger devices can be covered.
8221 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8222 *
8223 * Locking of the bad-block table uses a seqlock so md_is_badblock
8224 * might need to retry if it is very unlucky.
8225 * We will sometimes want to check for bad blocks in a bi_end_io function,
8226 * so we use the write_seqlock_irq variant.
8227 *
8228 * When looking for a bad block we specify a range and want to
8229 * know if any block in the range is bad. So we binary-search
8230 * to the last range that starts at-or-before the given endpoint,
8231 * (or "before the sector after the target range")
8232 * then see if it ends after the given start.
8233 * We return
8234 * 0 if there are no known bad blocks in the range
8235 * 1 if there are known bad block which are all acknowledged
8236 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8237 * plus the start/length of the first bad section we overlap.
8238 */
8239int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8240 sector_t *first_bad, int *bad_sectors)
8241{
8242 int hi;
majianpengab056132012-11-06 17:13:44 +08008243 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008244 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008245 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008246 sector_t target = s + sectors;
8247 unsigned seq;
8248
8249 if (bb->shift > 0) {
8250 /* round the start down, and the end up */
8251 s >>= bb->shift;
8252 target += (1<<bb->shift) - 1;
8253 target >>= bb->shift;
8254 sectors = target - s;
8255 }
8256 /* 'target' is now the first block after the bad range */
8257
8258retry:
8259 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008260 lo = 0;
8261 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008262 hi = bb->count;
8263
8264 /* Binary search between lo and hi for 'target'
8265 * i.e. for the last range that starts before 'target'
8266 */
8267 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8268 * are known not to be the last range before target.
8269 * VARIANT: hi-lo is the number of possible
8270 * ranges, and decreases until it reaches 1
8271 */
8272 while (hi - lo > 1) {
8273 int mid = (lo + hi) / 2;
8274 sector_t a = BB_OFFSET(p[mid]);
8275 if (a < target)
8276 /* This could still be the one, earlier ranges
8277 * could not. */
8278 lo = mid;
8279 else
8280 /* This and later ranges are definitely out. */
8281 hi = mid;
8282 }
8283 /* 'lo' might be the last that started before target, but 'hi' isn't */
8284 if (hi > lo) {
8285 /* need to check all range that end after 's' to see if
8286 * any are unacknowledged.
8287 */
8288 while (lo >= 0 &&
8289 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8290 if (BB_OFFSET(p[lo]) < target) {
8291 /* starts before the end, and finishes after
8292 * the start, so they must overlap
8293 */
8294 if (rv != -1 && BB_ACK(p[lo]))
8295 rv = 1;
8296 else
8297 rv = -1;
8298 *first_bad = BB_OFFSET(p[lo]);
8299 *bad_sectors = BB_LEN(p[lo]);
8300 }
8301 lo--;
8302 }
8303 }
8304
8305 if (read_seqretry(&bb->lock, seq))
8306 goto retry;
8307
8308 return rv;
8309}
8310EXPORT_SYMBOL_GPL(md_is_badblock);
8311
8312/*
8313 * Add a range of bad blocks to the table.
8314 * This might extend the table, or might contract it
8315 * if two adjacent ranges can be merged.
8316 * We binary-search to find the 'insertion' point, then
8317 * decide how best to handle it.
8318 */
8319static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8320 int acknowledged)
8321{
8322 u64 *p;
8323 int lo, hi;
8324 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008325 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008326
8327 if (bb->shift < 0)
8328 /* badblocks are disabled */
8329 return 0;
8330
8331 if (bb->shift) {
8332 /* round the start down, and the end up */
8333 sector_t next = s + sectors;
8334 s >>= bb->shift;
8335 next += (1<<bb->shift) - 1;
8336 next >>= bb->shift;
8337 sectors = next - s;
8338 }
8339
Bian Yu905b0292013-10-12 01:10:03 -04008340 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008341
8342 p = bb->page;
8343 lo = 0;
8344 hi = bb->count;
8345 /* Find the last range that starts at-or-before 's' */
8346 while (hi - lo > 1) {
8347 int mid = (lo + hi) / 2;
8348 sector_t a = BB_OFFSET(p[mid]);
8349 if (a <= s)
8350 lo = mid;
8351 else
8352 hi = mid;
8353 }
8354 if (hi > lo && BB_OFFSET(p[lo]) > s)
8355 hi = lo;
8356
8357 if (hi > lo) {
8358 /* we found a range that might merge with the start
8359 * of our new range
8360 */
8361 sector_t a = BB_OFFSET(p[lo]);
8362 sector_t e = a + BB_LEN(p[lo]);
8363 int ack = BB_ACK(p[lo]);
8364 if (e >= s) {
8365 /* Yes, we can merge with a previous range */
8366 if (s == a && s + sectors >= e)
8367 /* new range covers old */
8368 ack = acknowledged;
8369 else
8370 ack = ack && acknowledged;
8371
8372 if (e < s + sectors)
8373 e = s + sectors;
8374 if (e - a <= BB_MAX_LEN) {
8375 p[lo] = BB_MAKE(a, e-a, ack);
8376 s = e;
8377 } else {
8378 /* does not all fit in one range,
8379 * make p[lo] maximal
8380 */
8381 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8382 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8383 s = a + BB_MAX_LEN;
8384 }
8385 sectors = e - s;
8386 }
8387 }
8388 if (sectors && hi < bb->count) {
8389 /* 'hi' points to the first range that starts after 's'.
8390 * Maybe we can merge with the start of that range */
8391 sector_t a = BB_OFFSET(p[hi]);
8392 sector_t e = a + BB_LEN(p[hi]);
8393 int ack = BB_ACK(p[hi]);
8394 if (a <= s + sectors) {
8395 /* merging is possible */
8396 if (e <= s + sectors) {
8397 /* full overlap */
8398 e = s + sectors;
8399 ack = acknowledged;
8400 } else
8401 ack = ack && acknowledged;
8402
8403 a = s;
8404 if (e - a <= BB_MAX_LEN) {
8405 p[hi] = BB_MAKE(a, e-a, ack);
8406 s = e;
8407 } else {
8408 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8409 s = a + BB_MAX_LEN;
8410 }
8411 sectors = e - s;
8412 lo = hi;
8413 hi++;
8414 }
8415 }
8416 if (sectors == 0 && hi < bb->count) {
8417 /* we might be able to combine lo and hi */
8418 /* Note: 's' is at the end of 'lo' */
8419 sector_t a = BB_OFFSET(p[hi]);
8420 int lolen = BB_LEN(p[lo]);
8421 int hilen = BB_LEN(p[hi]);
8422 int newlen = lolen + hilen - (s - a);
8423 if (s >= a && newlen < BB_MAX_LEN) {
8424 /* yes, we can combine them */
8425 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8426 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8427 memmove(p + hi, p + hi + 1,
8428 (bb->count - hi - 1) * 8);
8429 bb->count--;
8430 }
8431 }
8432 while (sectors) {
8433 /* didn't merge (it all).
8434 * Need to add a range just before 'hi' */
8435 if (bb->count >= MD_MAX_BADBLOCKS) {
8436 /* No room for more */
8437 rv = 0;
8438 break;
8439 } else {
8440 int this_sectors = sectors;
8441 memmove(p + hi + 1, p + hi,
8442 (bb->count - hi) * 8);
8443 bb->count++;
8444
8445 if (this_sectors > BB_MAX_LEN)
8446 this_sectors = BB_MAX_LEN;
8447 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8448 sectors -= this_sectors;
8449 s += this_sectors;
8450 }
8451 }
8452
8453 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008454 if (!acknowledged)
8455 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008456 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008457
8458 return rv;
8459}
8460
NeilBrown3cb03002011-10-11 16:45:26 +11008461int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008462 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008463{
NeilBrownc6563a82012-05-21 09:27:00 +10008464 int rv;
8465 if (is_new)
8466 s += rdev->new_data_offset;
8467 else
8468 s += rdev->data_offset;
8469 rv = md_set_badblocks(&rdev->badblocks,
8470 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008471 if (rv) {
8472 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008473 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008474 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8475 md_wakeup_thread(rdev->mddev->thread);
8476 }
8477 return rv;
8478}
8479EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8480
8481/*
8482 * Remove a range of bad blocks from the table.
8483 * This may involve extending the table if we spilt a region,
8484 * but it must not fail. So if the table becomes full, we just
8485 * drop the remove request.
8486 */
8487static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8488{
8489 u64 *p;
8490 int lo, hi;
8491 sector_t target = s + sectors;
8492 int rv = 0;
8493
8494 if (bb->shift > 0) {
8495 /* When clearing we round the start up and the end down.
8496 * This should not matter as the shift should align with
8497 * the block size and no rounding should ever be needed.
8498 * However it is better the think a block is bad when it
8499 * isn't than to think a block is not bad when it is.
8500 */
8501 s += (1<<bb->shift) - 1;
8502 s >>= bb->shift;
8503 target >>= bb->shift;
8504 sectors = target - s;
8505 }
8506
8507 write_seqlock_irq(&bb->lock);
8508
8509 p = bb->page;
8510 lo = 0;
8511 hi = bb->count;
8512 /* Find the last range that starts before 'target' */
8513 while (hi - lo > 1) {
8514 int mid = (lo + hi) / 2;
8515 sector_t a = BB_OFFSET(p[mid]);
8516 if (a < target)
8517 lo = mid;
8518 else
8519 hi = mid;
8520 }
8521 if (hi > lo) {
8522 /* p[lo] is the last range that could overlap the
8523 * current range. Earlier ranges could also overlap,
8524 * but only this one can overlap the end of the range.
8525 */
8526 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8527 /* Partial overlap, leave the tail of this range */
8528 int ack = BB_ACK(p[lo]);
8529 sector_t a = BB_OFFSET(p[lo]);
8530 sector_t end = a + BB_LEN(p[lo]);
8531
8532 if (a < s) {
8533 /* we need to split this range */
8534 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008535 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008536 goto out;
8537 }
8538 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8539 bb->count++;
8540 p[lo] = BB_MAKE(a, s-a, ack);
8541 lo++;
8542 }
8543 p[lo] = BB_MAKE(target, end - target, ack);
8544 /* there is no longer an overlap */
8545 hi = lo;
8546 lo--;
8547 }
8548 while (lo >= 0 &&
8549 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8550 /* This range does overlap */
8551 if (BB_OFFSET(p[lo]) < s) {
8552 /* Keep the early parts of this range. */
8553 int ack = BB_ACK(p[lo]);
8554 sector_t start = BB_OFFSET(p[lo]);
8555 p[lo] = BB_MAKE(start, s - start, ack);
8556 /* now low doesn't overlap, so.. */
8557 break;
8558 }
8559 lo--;
8560 }
8561 /* 'lo' is strictly before, 'hi' is strictly after,
8562 * anything between needs to be discarded
8563 */
8564 if (hi - lo > 1) {
8565 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8566 bb->count -= (hi - lo - 1);
8567 }
8568 }
8569
8570 bb->changed = 1;
8571out:
8572 write_sequnlock_irq(&bb->lock);
8573 return rv;
8574}
8575
NeilBrownc6563a82012-05-21 09:27:00 +10008576int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8577 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008578{
NeilBrownc6563a82012-05-21 09:27:00 +10008579 if (is_new)
8580 s += rdev->new_data_offset;
8581 else
8582 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008583 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008584 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008585}
8586EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8587
8588/*
8589 * Acknowledge all bad blocks in a list.
8590 * This only succeeds if ->changed is clear. It is used by
8591 * in-kernel metadata updates
8592 */
8593void md_ack_all_badblocks(struct badblocks *bb)
8594{
8595 if (bb->page == NULL || bb->changed)
8596 /* no point even trying */
8597 return;
8598 write_seqlock_irq(&bb->lock);
8599
majianpengecb178b2012-03-19 12:46:42 +11008600 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008601 u64 *p = bb->page;
8602 int i;
8603 for (i = 0; i < bb->count ; i++) {
8604 if (!BB_ACK(p[i])) {
8605 sector_t start = BB_OFFSET(p[i]);
8606 int len = BB_LEN(p[i]);
8607 p[i] = BB_MAKE(start, len, 1);
8608 }
8609 }
NeilBrownde393cd2011-07-28 11:31:48 +10008610 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008611 }
8612 write_sequnlock_irq(&bb->lock);
8613}
8614EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8615
NeilBrown16c791a2011-07-28 11:31:47 +10008616/* sysfs access to bad-blocks list.
8617 * We present two files.
8618 * 'bad-blocks' lists sector numbers and lengths of ranges that
8619 * are recorded as bad. The list is truncated to fit within
8620 * the one-page limit of sysfs.
8621 * Writing "sector length" to this file adds an acknowledged
8622 * bad block list.
8623 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8624 * been acknowledged. Writing to this file adds bad blocks
8625 * without acknowledging them. This is largely for testing.
8626 */
8627
8628static ssize_t
8629badblocks_show(struct badblocks *bb, char *page, int unack)
8630{
8631 size_t len;
8632 int i;
8633 u64 *p = bb->page;
8634 unsigned seq;
8635
8636 if (bb->shift < 0)
8637 return 0;
8638
8639retry:
8640 seq = read_seqbegin(&bb->lock);
8641
8642 len = 0;
8643 i = 0;
8644
8645 while (len < PAGE_SIZE && i < bb->count) {
8646 sector_t s = BB_OFFSET(p[i]);
8647 unsigned int length = BB_LEN(p[i]);
8648 int ack = BB_ACK(p[i]);
8649 i++;
8650
8651 if (unack && ack)
8652 continue;
8653
8654 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8655 (unsigned long long)s << bb->shift,
8656 length << bb->shift);
8657 }
NeilBrownde393cd2011-07-28 11:31:48 +10008658 if (unack && len == 0)
8659 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008660
8661 if (read_seqretry(&bb->lock, seq))
8662 goto retry;
8663
8664 return len;
8665}
8666
8667#define DO_DEBUG 1
8668
8669static ssize_t
8670badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8671{
8672 unsigned long long sector;
8673 int length;
8674 char newline;
8675#ifdef DO_DEBUG
8676 /* Allow clearing via sysfs *only* for testing/debugging.
8677 * Normally only a successful write may clear a badblock
8678 */
8679 int clear = 0;
8680 if (page[0] == '-') {
8681 clear = 1;
8682 page++;
8683 }
8684#endif /* DO_DEBUG */
8685
8686 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8687 case 3:
8688 if (newline != '\n')
8689 return -EINVAL;
8690 case 2:
8691 if (length <= 0)
8692 return -EINVAL;
8693 break;
8694 default:
8695 return -EINVAL;
8696 }
8697
8698#ifdef DO_DEBUG
8699 if (clear) {
8700 md_clear_badblocks(bb, sector, length);
8701 return len;
8702 }
8703#endif /* DO_DEBUG */
8704 if (md_set_badblocks(bb, sector, length, !unack))
8705 return len;
8706 else
8707 return -ENOSPC;
8708}
8709
Adrian Bunk75c96f82005-05-05 16:16:09 -07008710static int md_notify_reboot(struct notifier_block *this,
8711 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008712{
8713 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008714 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008715 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008716
NeilBrownc744a652012-03-19 12:46:37 +11008717 for_each_mddev(mddev, tmp) {
8718 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008719 if (mddev->pers)
8720 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008721 if (mddev->persistent)
8722 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008723 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008724 }
NeilBrownc744a652012-03-19 12:46:37 +11008725 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008726 }
NeilBrownc744a652012-03-19 12:46:37 +11008727 /*
8728 * certain more exotic SCSI devices are known to be
8729 * volatile wrt too early system reboots. While the
8730 * right place to handle this issue is the given
8731 * driver, we do want to have a safe RAID driver ...
8732 */
8733 if (need_delay)
8734 mdelay(1000*1);
8735
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736 return NOTIFY_DONE;
8737}
8738
Adrian Bunk75c96f82005-05-05 16:16:09 -07008739static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740 .notifier_call = md_notify_reboot,
8741 .next = NULL,
8742 .priority = INT_MAX, /* before any real devices */
8743};
8744
8745static void md_geninit(void)
8746{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008747 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008748
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008749 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008750}
8751
Adrian Bunk75c96f82005-05-05 16:16:09 -07008752static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008753{
Tejun Heoe804ac72010-10-15 15:36:08 +02008754 int ret = -ENOMEM;
8755
Tejun Heoada609e2011-01-25 14:35:54 +01008756 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008757 if (!md_wq)
8758 goto err_wq;
8759
8760 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8761 if (!md_misc_wq)
8762 goto err_misc_wq;
8763
8764 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8765 goto err_md;
8766
8767 if ((ret = register_blkdev(0, "mdp")) < 0)
8768 goto err_mdp;
8769 mdp_major = ret;
8770
NeilBrownaf5628f2014-07-31 13:54:54 +10008771 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008772 md_probe, NULL, NULL);
8773 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008774 md_probe, NULL, NULL);
8775
Linus Torvalds1da177e2005-04-16 15:20:36 -07008776 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008777 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008778
8779 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008780 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008781
Tejun Heoe804ac72010-10-15 15:36:08 +02008782err_mdp:
8783 unregister_blkdev(MD_MAJOR, "md");
8784err_md:
8785 destroy_workqueue(md_misc_wq);
8786err_misc_wq:
8787 destroy_workqueue(md_wq);
8788err_wq:
8789 return ret;
8790}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008791
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008792void md_reload_sb(struct mddev *mddev)
8793{
8794 struct md_rdev *rdev, *tmp;
8795
8796 rdev_for_each_safe(rdev, tmp, mddev) {
8797 rdev->sb_loaded = 0;
8798 ClearPageUptodate(rdev->sb_page);
8799 }
8800 mddev->raid_disks = 0;
8801 analyze_sbs(mddev);
8802 rdev_for_each_safe(rdev, tmp, mddev) {
8803 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8804 /* since we don't write to faulty devices, we figure out if the
8805 * disk is faulty by comparing events
8806 */
8807 if (mddev->events > sb->events)
8808 set_bit(Faulty, &rdev->flags);
8809 }
8810
8811}
8812EXPORT_SYMBOL(md_reload_sb);
8813
Linus Torvalds1da177e2005-04-16 15:20:36 -07008814#ifndef MODULE
8815
8816/*
8817 * Searches all registered partitions for autorun RAID arrays
8818 * at boot time.
8819 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008820
8821static LIST_HEAD(all_detected_devices);
8822struct detected_devices_node {
8823 struct list_head list;
8824 dev_t dev;
8825};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008826
8827void md_autodetect_dev(dev_t dev)
8828{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008829 struct detected_devices_node *node_detected_dev;
8830
8831 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8832 if (node_detected_dev) {
8833 node_detected_dev->dev = dev;
8834 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8835 } else {
8836 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8837 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008839}
8840
Linus Torvalds1da177e2005-04-16 15:20:36 -07008841static void autostart_arrays(int part)
8842{
NeilBrown3cb03002011-10-11 16:45:26 +11008843 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008844 struct detected_devices_node *node_detected_dev;
8845 dev_t dev;
8846 int i_scanned, i_passed;
8847
8848 i_scanned = 0;
8849 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850
8851 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8852
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008853 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8854 i_scanned++;
8855 node_detected_dev = list_entry(all_detected_devices.next,
8856 struct detected_devices_node, list);
8857 list_del(&node_detected_dev->list);
8858 dev = node_detected_dev->dev;
8859 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008860 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008861 if (IS_ERR(rdev))
8862 continue;
8863
NeilBrown403df472014-09-30 15:52:29 +10008864 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008865 continue;
NeilBrown403df472014-09-30 15:52:29 +10008866
NeilBrownd0fae182008-03-04 14:29:31 -08008867 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008868 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008869 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008870 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008871
8872 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8873 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874
8875 autorun_devices(part);
8876}
8877
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008878#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008879
8880static __exit void md_exit(void)
8881{
NeilBrownfd01b882011-10-11 16:47:53 +11008882 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008883 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008884 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008885
NeilBrownaf5628f2014-07-31 13:54:54 +10008886 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008887 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008889 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008890 unregister_blkdev(mdp_major, "mdp");
8891 unregister_reboot_notifier(&md_notifier);
8892 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008893
8894 /* We cannot unload the modules while some process is
8895 * waiting for us in select() or poll() - wake them up
8896 */
8897 md_unloading = 1;
8898 while (waitqueue_active(&md_event_waiters)) {
8899 /* not safe to leave yet */
8900 wake_up(&md_event_waiters);
8901 msleep(delay);
8902 delay += delay;
8903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008904 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008905
NeilBrown29ac4aa2008-02-06 01:39:58 -08008906 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008907 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008908 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008909 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008910 destroy_workqueue(md_misc_wq);
8911 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008912}
8913
Dan Williams685784a2007-07-09 11:56:42 -07008914subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008915module_exit(md_exit)
8916
NeilBrownf91de922005-11-08 21:39:36 -08008917static int get_ro(char *buffer, struct kernel_param *kp)
8918{
8919 return sprintf(buffer, "%d", start_readonly);
8920}
8921static int set_ro(const char *val, struct kernel_param *kp)
8922{
8923 char *e;
8924 int num = simple_strtoul(val, &e, 10);
8925 if (*val && (*e == '\0' || *e == '\n')) {
8926 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008927 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008928 }
8929 return -EINVAL;
8930}
8931
NeilBrown80ca3a42006-07-10 04:44:18 -07008932module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8933module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11008934module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008935
Linus Torvalds1da177e2005-04-16 15:20:36 -07008936MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008937MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008938MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008939MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);