blob: ca011d1d1de728ac65aa97fc3a14421c4cbe0ab6 [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
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500645struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100646{
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}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500655EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100656
657static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
NeilBrown3cb03002011-10-11 16:45:26 +1100659 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
NeilBrowndafb20f2012-03-19 12:46:39 +1100661 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (rdev->bdev->bd_dev == dev)
663 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return NULL;
666}
667
NeilBrown1ca69c42012-10-11 13:37:33 +1100668static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
669{
670 struct md_rdev *rdev;
671
672 rdev_for_each_rcu(rdev, mddev)
673 if (rdev->bdev->bd_dev == dev)
674 return rdev;
675
676 return NULL;
677}
678
NeilBrown84fc4b52011-10-11 16:49:58 +1100679static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800680{
NeilBrown84fc4b52011-10-11 16:49:58 +1100681 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800682 list_for_each_entry(pers, &pers_list, list) {
683 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800684 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800685 if (strcmp(pers->name, clevel)==0)
686 return pers;
687 }
NeilBrown2604b702006-01-06 00:20:36 -0800688 return NULL;
689}
690
Andre Nollb73df2d2008-07-11 22:02:23 +1000691/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100692static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100694 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000695 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
NeilBrownf72ffdd2014-09-30 14:23:59 +1000698static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 rdev->sb_page = alloc_page(GFP_KERNEL);
701 if (!rdev->sb_page) {
702 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000703 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705
706 return 0;
707}
708
NeilBrown545c8792012-05-22 13:54:30 +1000709void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800712 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 rdev->sb_loaded = 0;
714 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000715 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100716 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
NeilBrown2699b672011-07-28 11:31:47 +1000718 if (rdev->bb_page) {
719 put_page(rdev->bb_page);
720 rdev->bb_page = NULL;
721 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000722 kfree(rdev->badblocks.page);
723 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
NeilBrown545c8792012-05-22 13:54:30 +1000725EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
NeilBrown6712ecf2007-09-27 12:47:43 +0200727static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700728{
NeilBrown3cb03002011-10-11 16:45:26 +1100729 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100730 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700731
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700732 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
733 printk("md: super_written gets error=%d, uptodate=%d\n",
734 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
735 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800736 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700737 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700738
NeilBrowna9701a32005-11-08 21:39:34 -0800739 if (atomic_dec_and_test(&mddev->pending_writes))
740 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700741 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700742}
743
NeilBrownfd01b882011-10-11 16:47:53 +1100744void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700745 sector_t sector, int size, struct page *page)
746{
747 /* write first size bytes of page to sector of rdev
748 * Increment mddev->pending_writes before returning
749 * and decrement it on completion, waking up sb_wait
750 * if zero is reached.
751 * If an error occurred, call md_error
752 */
NeilBrowna167f662010-10-26 18:31:13 +1100753 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700754
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100755 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700756 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700757 bio_add_page(bio, page, size, 0);
758 bio->bi_private = rdev;
759 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800760
NeilBrown7bfa19f2005-06-21 17:17:28 -0700761 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000762 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800763}
764
NeilBrownfd01b882011-10-11 16:47:53 +1100765void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800766{
Tejun Heoe9c74692010-09-03 11:56:18 +0200767 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000768 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700769}
770
NeilBrown3cb03002011-10-11 16:45:26 +1100771int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100772 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
NeilBrowna167f662010-10-26 18:31:13 +1100774 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 int ret;
776
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100777 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
778 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100779 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700780 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000781 else if (rdev->mddev->reshape_position != MaxSector &&
782 (rdev->mddev->reshape_backwards ==
783 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700784 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100785 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700786 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700788 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
791 bio_put(bio);
792 return ret;
793}
NeilBrowna8745db2006-01-06 00:20:34 -0800794EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
NeilBrownf72ffdd2014-09-30 14:23:59 +1000796static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (rdev->sb_loaded)
801 return 0;
802
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100803 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 goto fail;
805 rdev->sb_loaded = 1;
806 return 0;
807
808fail:
809 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
810 bdevname(rdev->bdev,b));
811 return -EINVAL;
812}
813
814static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
815{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000816 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000817 sb1->set_uuid1 == sb2->set_uuid1 &&
818 sb1->set_uuid2 == sb2->set_uuid2 &&
819 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
823{
824 int ret;
825 mdp_super_t *tmp1, *tmp2;
826
827 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
828 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
829
830 if (!tmp1 || !tmp2) {
831 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100832 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 goto abort;
834 }
835
836 *tmp1 = *sb1;
837 *tmp2 = *sb2;
838
839 /*
840 * nr_disks is not constant
841 */
842 tmp1->nr_disks = 0;
843 tmp2->nr_disks = 0;
844
Andre Nollce0c8e02008-07-11 22:02:20 +1000845 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700847 kfree(tmp1);
848 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return ret;
850}
851
NeilBrown4d167f02007-05-09 02:35:37 -0700852static u32 md_csum_fold(u32 csum)
853{
854 csum = (csum & 0xffff) + (csum >> 16);
855 return (csum & 0xffff) + (csum >> 16);
856}
857
NeilBrownf72ffdd2014-09-30 14:23:59 +1000858static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
NeilBrown4d167f02007-05-09 02:35:37 -0700860 u64 newcsum = 0;
861 u32 *sb32 = (u32*)sb;
862 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 unsigned int disk_csum, csum;
864
865 disk_csum = sb->sb_csum;
866 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700867
868 for (i = 0; i < MD_SB_BYTES/4 ; i++)
869 newcsum += sb32[i];
870 csum = (newcsum & 0xffffffff) + (newcsum>>32);
871
NeilBrown4d167f02007-05-09 02:35:37 -0700872#ifdef CONFIG_ALPHA
873 /* This used to use csum_partial, which was wrong for several
874 * reasons including that different results are returned on
875 * different architectures. It isn't critical that we get exactly
876 * the same return value as before (we always csum_fold before
877 * testing, and that removes any differences). However as we
878 * know that csum_partial always returned a 16bit value on
879 * alphas, do a fold to maximise conformity to previous behaviour.
880 */
881 sb->sb_csum = md_csum_fold(disk_csum);
882#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700884#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return csum;
886}
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888/*
889 * Handle superblock details.
890 * We want to be able to handle multiple superblock formats
891 * so we have a common interface to them all, and an array of
892 * different handlers.
893 * We rely on user-space to write the initial superblock, and support
894 * reading and updating of superblocks.
895 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100896 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 * loads and validates a superblock on dev.
898 * if refdev != NULL, compare superblocks on both devices
899 * Return:
900 * 0 - dev has a superblock that is compatible with refdev
901 * 1 - dev has a superblock that is compatible and newer than refdev
902 * so dev should be used as the refdev in future
903 * -EINVAL superblock incompatible or invalid
904 * -othererror e.g. -EIO
905 *
NeilBrownfd01b882011-10-11 16:47:53 +1100906 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 * Verify that dev is acceptable into mddev.
908 * The first time, mddev->raid_disks will be 0, and data from
909 * dev should be merged in. Subsequent calls check that dev
910 * is new enough. Return 0 or -EINVAL
911 *
NeilBrownfd01b882011-10-11 16:47:53 +1100912 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 * Update the superblock for rdev with data in mddev
914 * This does not write to disc.
915 *
916 */
917
918struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000919 char *name;
920 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000921 int (*load_super)(struct md_rdev *rdev,
922 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000923 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000924 int (*validate_super)(struct mddev *mddev,
925 struct md_rdev *rdev);
926 void (*sync_super)(struct mddev *mddev,
927 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100928 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000929 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000930 int (*allow_new_offset)(struct md_rdev *rdev,
931 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932};
933
934/*
Andre Noll0894cc32009-06-18 08:49:23 +1000935 * Check that the given mddev has no bitmap.
936 *
937 * This function is called from the run method of all personalities that do not
938 * support bitmaps. It prints an error message and returns non-zero if mddev
939 * has a bitmap. Otherwise, it returns 0.
940 *
941 */
NeilBrownfd01b882011-10-11 16:47:53 +1100942int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000943{
NeilBrownc3d97142009-12-14 12:49:52 +1100944 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000945 return 0;
946 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
947 mdname(mddev), mddev->pers->name);
948 return 1;
949}
950EXPORT_SYMBOL(md_check_no_bitmap);
951
952/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000953 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 */
NeilBrown3cb03002011-10-11 16:45:26 +1100955static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
958 mdp_super_t *sb;
959 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /*
Andre Noll0f420352008-07-11 22:02:23 +1000962 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 * it's at the end of the disk.
964 *
965 * It also happens to be a multiple of 4Kb.
966 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100967 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
NeilBrown0002b272005-09-09 16:23:53 -0700969 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (ret) return ret;
971
972 ret = -EINVAL;
973
974 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000975 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 if (sb->md_magic != MD_SB_MAGIC) {
978 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
979 b);
980 goto abort;
981 }
982
983 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800984 sb->minor_version < 90 ||
985 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
987 sb->major_version, sb->minor_version,
988 b);
989 goto abort;
990 }
991
992 if (sb->raid_disks <= 0)
993 goto abort;
994
NeilBrown4d167f02007-05-09 02:35:37 -0700995 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
997 b);
998 goto abort;
999 }
1000
1001 rdev->preferred_minor = sb->md_minor;
1002 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001003 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001004 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001005 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 if (sb->level == LEVEL_MULTIPATH)
1008 rdev->desc_nr = -1;
1009 else
1010 rdev->desc_nr = sb->this_disk.number;
1011
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001012 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001014 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001016 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (!uuid_equal(refsb, sb)) {
1018 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1019 b, bdevname(refdev->bdev,b2));
1020 goto abort;
1021 }
1022 if (!sb_equal(refsb, sb)) {
1023 printk(KERN_WARNING "md: %s has same UUID"
1024 " but different superblock to %s\n",
1025 b, bdevname(refdev->bdev, b2));
1026 goto abort;
1027 }
1028 ev1 = md_event(sb);
1029 ev2 = md_event(refsb);
1030 if (ev1 > ev2)
1031 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001032 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 ret = 0;
1034 }
NeilBrown8190e752009-06-18 08:48:58 +10001035 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001036 /* Limit to 4TB as metadata cannot record more than that.
1037 * (not needed for Linear and RAID0 as metadata doesn't
1038 * record this size)
1039 */
1040 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001041 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
NeilBrown27a7b262011-09-10 17:21:28 +10001043 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001044 /* "this cannot possibly happen" ... */
1045 ret = -EINVAL;
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 abort:
1048 return ret;
1049}
1050
1051/*
1052 * validate_super for 0.90.0
1053 */
NeilBrownfd01b882011-10-11 16:47:53 +11001054static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001057 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001058 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
NeilBrown41158c72005-06-21 17:17:25 -07001060 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001061 clear_bit(Faulty, &rdev->flags);
1062 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001063 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001064 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (mddev->raid_disks == 0) {
1067 mddev->major_version = 0;
1068 mddev->minor_version = sb->minor_version;
1069 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001070 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001071 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 mddev->ctime = sb->ctime;
1073 mddev->utime = sb->utime;
1074 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001075 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 mddev->layout = sb->layout;
1077 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001078 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001079 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001080 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001081 mddev->bitmap_info.space = 0;
1082 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001083 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001084 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001085 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
NeilBrownf6705572006-03-27 01:18:11 -08001087 if (mddev->minor_version >= 91) {
1088 mddev->reshape_position = sb->reshape_position;
1089 mddev->delta_disks = sb->delta_disks;
1090 mddev->new_level = sb->new_level;
1091 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001092 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001093 if (mddev->delta_disks < 0)
1094 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001095 } else {
1096 mddev->reshape_position = MaxSector;
1097 mddev->delta_disks = 0;
1098 mddev->new_level = mddev->level;
1099 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001100 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001101 }
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (sb->state & (1<<MD_SB_CLEAN))
1104 mddev->recovery_cp = MaxSector;
1105 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001106 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 sb->events_lo == sb->cp_events_lo) {
1108 mddev->recovery_cp = sb->recovery_cp;
1109 } else
1110 mddev->recovery_cp = 0;
1111 }
1112
1113 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1114 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1115 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1116 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1117
1118 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001119
1120 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001121 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001122 mddev->bitmap_info.offset =
1123 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001124 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001125 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001126 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001127
NeilBrown41158c72005-06-21 17:17:25 -07001128 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001129 /* Insist on good event counter while assembling, except
1130 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001132 if (sb->disks[rdev->desc_nr].state & (
1133 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001134 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001135 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001136 } else if (mddev->bitmap) {
1137 /* if adding to array with a bitmap, then we can accept an
1138 * older device ... but not too old.
1139 */
NeilBrown41158c72005-06-21 17:17:25 -07001140 if (ev1 < mddev->bitmap->events_cleared)
1141 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001142 if (ev1 < mddev->events)
1143 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001144 } else {
1145 if (ev1 < mddev->events)
1146 /* just a hot-add of a new device, leave raid_disk at -1 */
1147 return 0;
1148 }
NeilBrown41158c72005-06-21 17:17:25 -07001149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 desc = sb->disks + rdev->desc_nr;
1152
1153 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001154 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001155 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1156 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001157 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001159 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001160 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1161 /* active but not in sync implies recovery up to
1162 * reshape position. We don't know exactly where
1163 * that is, so set to zero for now */
1164 if (mddev->minor_version >= 91) {
1165 rdev->recovery_offset = 0;
1166 rdev->raid_disk = desc->raid_disk;
1167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001169 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1170 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001171 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001172 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return 0;
1174}
1175
1176/*
1177 * sync_super for 0.90.0
1178 */
NeilBrownfd01b882011-10-11 16:47:53 +11001179static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001182 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /* make rdev->sb match mddev data..
1186 *
1187 * 1/ zero out disks
1188 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1189 * 3/ any empty disks < next_spare become removed
1190 *
1191 * disks[0] gets initialised to REMOVED because
1192 * we cannot be sure from other fields if it has
1193 * been initialised or not.
1194 */
1195 int i;
1196 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1197
NeilBrown61181562005-09-09 16:24:02 -07001198 rdev->sb_size = MD_SB_BYTES;
1199
Namhyung Kim65a06f062011-07-27 11:00:36 +10001200 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 memset(sb, 0, sizeof(*sb));
1203
1204 sb->md_magic = MD_SB_MAGIC;
1205 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 sb->patch_version = mddev->patch_version;
1207 sb->gvalid_words = 0; /* ignored */
1208 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1209 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1210 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1211 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1212
1213 sb->ctime = mddev->ctime;
1214 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001215 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 sb->raid_disks = mddev->raid_disks;
1217 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001218 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 sb->utime = mddev->utime;
1220 sb->state = 0;
1221 sb->events_hi = (mddev->events>>32);
1222 sb->events_lo = (u32)mddev->events;
1223
NeilBrownf6705572006-03-27 01:18:11 -08001224 if (mddev->reshape_position == MaxSector)
1225 sb->minor_version = 90;
1226 else {
1227 sb->minor_version = 91;
1228 sb->reshape_position = mddev->reshape_position;
1229 sb->new_level = mddev->new_level;
1230 sb->delta_disks = mddev->delta_disks;
1231 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001232 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001233 }
1234 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (mddev->in_sync)
1236 {
1237 sb->recovery_cp = mddev->recovery_cp;
1238 sb->cp_events_hi = (mddev->events>>32);
1239 sb->cp_events_lo = (u32)mddev->events;
1240 if (mddev->recovery_cp == MaxSector)
1241 sb->state = (1<< MD_SB_CLEAN);
1242 } else
1243 sb->recovery_cp = 0;
1244
1245 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001246 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
NeilBrownc3d97142009-12-14 12:49:52 +11001248 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001249 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001252 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001254 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001255 int is_active = test_bit(In_sync, &rdev2->flags);
1256
1257 if (rdev2->raid_disk >= 0 &&
1258 sb->minor_version >= 91)
1259 /* we have nowhere to store the recovery_offset,
1260 * but if it is not below the reshape_position,
1261 * we can piggy-back on that.
1262 */
1263 is_active = 1;
1264 if (rdev2->raid_disk < 0 ||
1265 test_bit(Faulty, &rdev2->flags))
1266 is_active = 0;
1267 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001268 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001270 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001271 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 d = &sb->disks[rdev2->desc_nr];
1273 nr_disks++;
1274 d->number = rdev2->desc_nr;
1275 d->major = MAJOR(rdev2->bdev->bd_dev);
1276 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001277 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 d->raid_disk = rdev2->raid_disk;
1279 else
1280 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001281 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001283 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001285 if (test_bit(In_sync, &rdev2->flags))
1286 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 active++;
1288 working++;
1289 } else {
1290 d->state = 0;
1291 spare++;
1292 working++;
1293 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001294 if (test_bit(WriteMostly, &rdev2->flags))
1295 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /* now set the "removed" and "faulty" bits on any missing devices */
1298 for (i=0 ; i < mddev->raid_disks ; i++) {
1299 mdp_disk_t *d = &sb->disks[i];
1300 if (d->state == 0 && d->number == 0) {
1301 d->number = i;
1302 d->raid_disk = i;
1303 d->state = (1<<MD_DISK_REMOVED);
1304 d->state |= (1<<MD_DISK_FAULTY);
1305 failed++;
1306 }
1307 }
1308 sb->nr_disks = nr_disks;
1309 sb->active_disks = active;
1310 sb->working_disks = working;
1311 sb->failed_disks = failed;
1312 sb->spare_disks = spare;
1313
1314 sb->this_disk = sb->disks[rdev->desc_nr];
1315 sb->sb_csum = calc_sb_csum(sb);
1316}
1317
1318/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001319 * rdev_size_change for 0.90.0
1320 */
1321static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001322super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001323{
Andre Noll58c0fed2009-03-31 14:33:13 +11001324 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001325 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001326 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001327 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001328 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001329 if (!num_sectors || num_sectors > rdev->sb_start)
1330 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001331 /* Limit to 4TB as metadata cannot record more than that.
1332 * 4TB == 2^32 KB, or 2*2^32 sectors.
1333 */
NeilBrown667a5312012-08-16 16:46:12 +10001334 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001335 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001336 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001337 rdev->sb_page);
1338 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001339 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001340}
1341
NeilBrownc6563a82012-05-21 09:27:00 +10001342static int
1343super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1344{
1345 /* non-zero offset changes not possible with v0.90 */
1346 return new_offset == 0;
1347}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001348
1349/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 * version 1 superblock
1351 */
1352
NeilBrownf72ffdd2014-09-30 14:23:59 +10001353static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001355 __le32 disk_csum;
1356 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 unsigned long long newcsum;
1358 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001359 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 disk_csum = sb->sb_csum;
1362 sb->sb_csum = 0;
1363 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001364 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 newcsum += le32_to_cpu(*isuper++);
1366
1367 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001368 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1371 sb->sb_csum = disk_csum;
1372 return cpu_to_le32(csum);
1373}
1374
NeilBrown2699b672011-07-28 11:31:47 +10001375static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1376 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001377static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 struct mdp_superblock_1 *sb;
1380 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001381 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001382 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001384 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 /*
Andre Noll0f420352008-07-11 22:02:23 +10001387 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 * It is always aligned to a 4K boundary and
1389 * depeding on minor_version, it can be:
1390 * 0: At least 8K, but less than 12K, from end of device
1391 * 1: At start of device
1392 * 2: 4K from start of device.
1393 */
1394 switch(minor_version) {
1395 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001396 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001397 sb_start -= 8*2;
1398 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 break;
1400 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001401 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 break;
1403 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001404 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 break;
1406 default:
1407 return -EINVAL;
1408 }
Andre Noll0f420352008-07-11 22:02:23 +10001409 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
NeilBrown0002b272005-09-09 16:23:53 -07001411 /* superblock is rarely larger than 1K, but it can be larger,
1412 * and it is safe to read 4k, so we do that
1413 */
1414 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (ret) return ret;
1416
Namhyung Kim65a06f062011-07-27 11:00:36 +10001417 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1420 sb->major_version != cpu_to_le32(1) ||
1421 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001422 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001423 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return -EINVAL;
1425
1426 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1427 printk("md: invalid superblock checksum on %s\n",
1428 bdevname(rdev->bdev,b));
1429 return -EINVAL;
1430 }
1431 if (le64_to_cpu(sb->data_size) < 10) {
1432 printk("md: data_size too small on %s\n",
1433 bdevname(rdev->bdev,b));
1434 return -EINVAL;
1435 }
NeilBrownc6563a82012-05-21 09:27:00 +10001436 if (sb->pad0 ||
1437 sb->pad3[0] ||
1438 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1439 /* Some padding is non-zero, might be a new feature */
1440 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 rdev->preferred_minor = 0xffff;
1443 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001444 rdev->new_data_offset = rdev->data_offset;
1445 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1446 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1447 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001448 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
NeilBrown0002b272005-09-09 16:23:53 -07001450 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001451 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001452 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001453 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1454
1455 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001456 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001457 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001458 if (minor_version
1459 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1460 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001461
NeilBrown31b65a02006-07-10 04:44:14 -07001462 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1463 rdev->desc_nr = -1;
1464 else
1465 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1466
NeilBrown2699b672011-07-28 11:31:47 +10001467 if (!rdev->bb_page) {
1468 rdev->bb_page = alloc_page(GFP_KERNEL);
1469 if (!rdev->bb_page)
1470 return -ENOMEM;
1471 }
1472 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1473 rdev->badblocks.count == 0) {
1474 /* need to load the bad block list.
1475 * Currently we limit it to one page.
1476 */
1477 s32 offset;
1478 sector_t bb_sector;
1479 u64 *bbp;
1480 int i;
1481 int sectors = le16_to_cpu(sb->bblog_size);
1482 if (sectors > (PAGE_SIZE / 512))
1483 return -EINVAL;
1484 offset = le32_to_cpu(sb->bblog_offset);
1485 if (offset == 0)
1486 return -EINVAL;
1487 bb_sector = (long long)offset;
1488 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1489 rdev->bb_page, READ, true))
1490 return -EIO;
1491 bbp = (u64 *)page_address(rdev->bb_page);
1492 rdev->badblocks.shift = sb->bblog_shift;
1493 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1494 u64 bb = le64_to_cpu(*bbp);
1495 int count = bb & (0x3ff);
1496 u64 sector = bb >> 10;
1497 sector <<= sb->bblog_shift;
1498 count <<= sb->bblog_shift;
1499 if (bb + 1 == 0)
1500 break;
1501 if (md_set_badblocks(&rdev->badblocks,
1502 sector, count, 1) == 0)
1503 return -EINVAL;
1504 }
NeilBrown486adf72013-04-24 11:42:44 +10001505 } else if (sb->bblog_offset != 0)
1506 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001507
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001508 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001509 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001510 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001512 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1515 sb->level != refsb->level ||
1516 sb->layout != refsb->layout ||
1517 sb->chunksize != refsb->chunksize) {
1518 printk(KERN_WARNING "md: %s has strangely different"
1519 " superblock to %s\n",
1520 bdevname(rdev->bdev,b),
1521 bdevname(refdev->bdev,b2));
1522 return -EINVAL;
1523 }
1524 ev1 = le64_to_cpu(sb->events);
1525 ev2 = le64_to_cpu(refsb->events);
1526
1527 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001528 ret = 1;
1529 else
1530 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
NeilBrownc6563a82012-05-21 09:27:00 +10001532 if (minor_version) {
1533 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1534 sectors -= rdev->data_offset;
1535 } else
1536 sectors = rdev->sb_start;
1537 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001539 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001540 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
NeilBrownfd01b882011-10-11 16:47:53 +11001543static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001545 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001546 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
NeilBrown41158c72005-06-21 17:17:25 -07001548 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001549 clear_bit(Faulty, &rdev->flags);
1550 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001551 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001552 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (mddev->raid_disks == 0) {
1555 mddev->major_version = 1;
1556 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001557 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001558 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1560 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1561 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001562 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 mddev->layout = le32_to_cpu(sb->layout);
1564 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001565 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001566 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001567 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001568 mddev->bitmap_info.space = 0;
1569 /* Default location for bitmap is 1K after superblock
1570 * using 3K - total of 4K
1571 */
NeilBrownc3d97142009-12-14 12:49:52 +11001572 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001573 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001574 mddev->reshape_backwards = 0;
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1577 memcpy(mddev->uuid, sb->set_uuid, 16);
1578
1579 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001580
NeilBrown71c08052005-09-09 16:23:51 -07001581 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001582 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001583 mddev->bitmap_info.offset =
1584 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001585 /* Metadata doesn't record how much space is available.
1586 * For 1.0, we assume we can use up to the superblock
1587 * if before, else to 4K beyond superblock.
1588 * For others, assume no change is possible.
1589 */
1590 if (mddev->minor_version > 0)
1591 mddev->bitmap_info.space = 0;
1592 else if (mddev->bitmap_info.offset > 0)
1593 mddev->bitmap_info.space =
1594 8 - mddev->bitmap_info.offset;
1595 else
1596 mddev->bitmap_info.space =
1597 -mddev->bitmap_info.offset;
1598 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001599
NeilBrownf6705572006-03-27 01:18:11 -08001600 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1601 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1602 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1603 mddev->new_level = le32_to_cpu(sb->new_level);
1604 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001605 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001606 if (mddev->delta_disks < 0 ||
1607 (mddev->delta_disks == 0 &&
1608 (le32_to_cpu(sb->feature_map)
1609 & MD_FEATURE_RESHAPE_BACKWARDS)))
1610 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001611 } else {
1612 mddev->reshape_position = MaxSector;
1613 mddev->delta_disks = 0;
1614 mddev->new_level = mddev->level;
1615 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001616 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001617 }
1618
NeilBrown41158c72005-06-21 17:17:25 -07001619 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001620 /* Insist of good event counter while assembling, except for
1621 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001623 if (rdev->desc_nr >= 0 &&
1624 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1625 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1626 if (ev1 < mddev->events)
1627 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001628 } else if (mddev->bitmap) {
1629 /* If adding to array with a bitmap, then we can accept an
1630 * older device, but not too old.
1631 */
NeilBrown41158c72005-06-21 17:17:25 -07001632 if (ev1 < mddev->bitmap->events_cleared)
1633 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001634 if (ev1 < mddev->events)
1635 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001636 } else {
1637 if (ev1 < mddev->events)
1638 /* just a hot-add of a new device, leave raid_disk at -1 */
1639 return 0;
1640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (mddev->level != LEVEL_MULTIPATH) {
1642 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001643 if (rdev->desc_nr < 0 ||
1644 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1645 role = 0xffff;
1646 rdev->desc_nr = -1;
1647 } else
1648 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 switch(role) {
1650 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 break;
1652 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001653 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 break;
1655 default:
NeilBrownf4667222013-12-09 12:04:56 +11001656 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001657 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001658 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001659 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001660 if (!(le32_to_cpu(sb->feature_map) &
1661 MD_FEATURE_RECOVERY_BITMAP))
1662 rdev->saved_raid_disk = -1;
1663 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001664 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 rdev->raid_disk = role;
1666 break;
1667 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001668 if (sb->devflags & WriteMostly1)
1669 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001670 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1671 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001672 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001673 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return 0;
1676}
1677
NeilBrownfd01b882011-10-11 16:47:53 +11001678static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
1680 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001681 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 int max_dev, i;
1683 /* make rdev->sb match mddev and rdev data. */
1684
Namhyung Kim65a06f062011-07-27 11:00:36 +10001685 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 sb->feature_map = 0;
1688 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001689 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 memset(sb->pad3, 0, sizeof(sb->pad3));
1691
1692 sb->utime = cpu_to_le64((__u64)mddev->utime);
1693 sb->events = cpu_to_le64(mddev->events);
1694 if (mddev->in_sync)
1695 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1696 else
1697 sb->resync_offset = cpu_to_le64(0);
1698
NeilBrown1c05b4b2006-10-21 10:24:08 -07001699 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001700
NeilBrownf0ca3402006-02-02 14:28:04 -08001701 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001702 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001703 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001704 sb->level = cpu_to_le32(mddev->level);
1705 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001706
NeilBrownaeb9b212011-08-25 14:43:08 +10001707 if (test_bit(WriteMostly, &rdev->flags))
1708 sb->devflags |= WriteMostly1;
1709 else
1710 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001711 sb->data_offset = cpu_to_le64(rdev->data_offset);
1712 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001713
NeilBrownc3d97142009-12-14 12:49:52 +11001714 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1715 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001716 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001717 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001718
1719 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001720 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001721 sb->feature_map |=
1722 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1723 sb->recovery_offset =
1724 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001725 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1726 sb->feature_map |=
1727 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001728 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001729 if (test_bit(Replacement, &rdev->flags))
1730 sb->feature_map |=
1731 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001732
NeilBrownf6705572006-03-27 01:18:11 -08001733 if (mddev->reshape_position != MaxSector) {
1734 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1735 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1736 sb->new_layout = cpu_to_le32(mddev->new_layout);
1737 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1738 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001739 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001740 if (mddev->delta_disks == 0 &&
1741 mddev->reshape_backwards)
1742 sb->feature_map
1743 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001744 if (rdev->new_data_offset != rdev->data_offset) {
1745 sb->feature_map
1746 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1747 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1748 - rdev->data_offset));
1749 }
NeilBrownf6705572006-03-27 01:18:11 -08001750 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001751
NeilBrown2699b672011-07-28 11:31:47 +10001752 if (rdev->badblocks.count == 0)
1753 /* Nothing to do for bad blocks*/ ;
1754 else if (sb->bblog_offset == 0)
1755 /* Cannot record bad blocks on this device */
1756 md_error(mddev, rdev);
1757 else {
1758 struct badblocks *bb = &rdev->badblocks;
1759 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1760 u64 *p = bb->page;
1761 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1762 if (bb->changed) {
1763 unsigned seq;
1764
1765retry:
1766 seq = read_seqbegin(&bb->lock);
1767
1768 memset(bbp, 0xff, PAGE_SIZE);
1769
1770 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001771 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001772 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1773 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001774 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001775 }
NeilBrownd0962932012-03-19 12:46:41 +11001776 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001777 if (read_seqretry(&bb->lock, seq))
1778 goto retry;
1779
1780 bb->sector = (rdev->sb_start +
1781 (int)le32_to_cpu(sb->bblog_offset));
1782 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001783 }
1784 }
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001787 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (rdev2->desc_nr+1 > max_dev)
1789 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001790
NeilBrown70471da2009-08-03 10:59:57 +10001791 if (max_dev > le32_to_cpu(sb->max_dev)) {
1792 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001793 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001794 rdev->sb_size = max_dev * 2 + 256;
1795 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1796 if (rdev->sb_size & bmask)
1797 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001798 } else
1799 max_dev = le32_to_cpu(sb->max_dev);
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 for (i=0; i<max_dev;i++)
1802 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001803
NeilBrowndafb20f2012-03-19 12:46:39 +11001804 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001806 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001808 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001810 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001811 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 else
1813 sb->dev_roles[i] = cpu_to_le16(0xffff);
1814 }
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 sb->sb_csum = calc_sb_1_csum(sb);
1817}
1818
Chris Webb0cd17fe2008-06-28 08:31:46 +10001819static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001820super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001821{
1822 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001823 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001824 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001825 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001826 if (rdev->data_offset != rdev->new_data_offset)
1827 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001828 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001829 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001830 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001831 max_sectors -= rdev->data_offset;
1832 if (!num_sectors || num_sectors > max_sectors)
1833 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001834 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001835 /* minor version 0 with bitmap we can't move */
1836 return 0;
1837 } else {
1838 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001839 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001840 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001841 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001842 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001843 if (!num_sectors || num_sectors > max_sectors)
1844 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001845 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001846 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001847 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001848 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001849 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001850 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001851 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001852 rdev->sb_page);
1853 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001854 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001855
1856}
1857
1858static int
1859super_1_allow_new_offset(struct md_rdev *rdev,
1860 unsigned long long new_offset)
1861{
1862 /* All necessary checks on new >= old have been done */
1863 struct bitmap *bitmap;
1864 if (new_offset >= rdev->data_offset)
1865 return 1;
1866
1867 /* with 1.0 metadata, there is no metadata to tread on
1868 * so we can always move back */
1869 if (rdev->mddev->minor_version == 0)
1870 return 1;
1871
1872 /* otherwise we must be sure not to step on
1873 * any metadata, so stay:
1874 * 36K beyond start of superblock
1875 * beyond end of badblocks
1876 * beyond write-intent bitmap
1877 */
1878 if (rdev->sb_start + (32+4)*2 > new_offset)
1879 return 0;
1880 bitmap = rdev->mddev->bitmap;
1881 if (bitmap && !rdev->mddev->bitmap_info.file &&
1882 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001883 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001884 return 0;
1885 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1886 return 0;
1887
1888 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001889}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Adrian Bunk75c96f82005-05-05 16:16:09 -07001891static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 [0] = {
1893 .name = "0.90.0",
1894 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001895 .load_super = super_90_load,
1896 .validate_super = super_90_validate,
1897 .sync_super = super_90_sync,
1898 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001899 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 },
1901 [1] = {
1902 .name = "md-1",
1903 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001904 .load_super = super_1_load,
1905 .validate_super = super_1_validate,
1906 .sync_super = super_1_sync,
1907 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001908 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 },
1910};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
NeilBrownfd01b882011-10-11 16:47:53 +11001912static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001913{
1914 if (mddev->sync_super) {
1915 mddev->sync_super(mddev, rdev);
1916 return;
1917 }
1918
1919 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1920
1921 super_types[mddev->major_version].sync_super(mddev, rdev);
1922}
1923
NeilBrownfd01b882011-10-11 16:47:53 +11001924static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
NeilBrown3cb03002011-10-11 16:45:26 +11001926 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
NeilBrown4b809912008-07-21 17:05:25 +10001928 rcu_read_lock();
1929 rdev_for_each_rcu(rdev, mddev1)
1930 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001931 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001932 rdev2->bdev->bd_contains) {
1933 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001934 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001935 }
1936 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return 0;
1938}
1939
1940static LIST_HEAD(pending_raid_disks);
1941
Andre Nollac5e7112009-08-03 10:59:47 +10001942/*
1943 * Try to register data integrity profile for an mddev
1944 *
1945 * This is called when an array is started and after a disk has been kicked
1946 * from the array. It only succeeds if all working and active component devices
1947 * are integrity capable with matching profiles.
1948 */
NeilBrownfd01b882011-10-11 16:47:53 +11001949int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001950{
NeilBrown3cb03002011-10-11 16:45:26 +11001951 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001952
Andre Nollac5e7112009-08-03 10:59:47 +10001953 if (list_empty(&mddev->disks))
1954 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001955 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1956 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001957 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001958 /* skip spares and non-functional disks */
1959 if (test_bit(Faulty, &rdev->flags))
1960 continue;
1961 if (rdev->raid_disk < 0)
1962 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001963 if (!reference) {
1964 /* Use the first rdev as the reference */
1965 reference = rdev;
1966 continue;
1967 }
1968 /* does this rdev's profile match the reference profile? */
1969 if (blk_integrity_compare(reference->bdev->bd_disk,
1970 rdev->bdev->bd_disk) < 0)
1971 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001972 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001973 if (!reference || !bdev_get_integrity(reference->bdev))
1974 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001975 /*
1976 * All component devices are integrity capable and have matching
1977 * profiles, register the common profile for the md device.
1978 */
1979 if (blk_integrity_register(mddev->gendisk,
1980 bdev_get_integrity(reference->bdev)) != 0) {
1981 printk(KERN_ERR "md: failed to register integrity for %s\n",
1982 mdname(mddev));
1983 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001984 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001985 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1986 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1987 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1988 mdname(mddev));
1989 return -EINVAL;
1990 }
Andre Nollac5e7112009-08-03 10:59:47 +10001991 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001992}
Andre Nollac5e7112009-08-03 10:59:47 +10001993EXPORT_SYMBOL(md_integrity_register);
1994
1995/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001996void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10001997{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001998 struct blk_integrity *bi_rdev;
1999 struct blk_integrity *bi_mddev;
2000
2001 if (!mddev->gendisk)
2002 return;
2003
2004 bi_rdev = bdev_get_integrity(rdev->bdev);
2005 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002006
2007 if (!bi_mddev) /* nothing to do */
2008 return;
2009 if (rdev->raid_disk < 0) /* skip spares */
2010 return;
2011 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2012 rdev->bdev->bd_disk) >= 0)
2013 return;
2014 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2015 blk_integrity_unregister(mddev->gendisk);
2016}
2017EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002018
NeilBrownf72ffdd2014-09-30 14:23:59 +10002019static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002021 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002022 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002023 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002024 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Dan Williams11e2ede2008-04-30 00:52:32 -07002026 /* prevent duplicates */
2027 if (find_rdev(mddev, rdev->bdev->bd_dev))
2028 return -EEXIST;
2029
Andre Nolldd8ac332009-03-31 14:33:13 +11002030 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2031 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2032 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002033 if (mddev->pers) {
2034 /* Cannot change size, so fail
2035 * If mddev->level <= 0, then we don't care
2036 * about aligning sizes (e.g. linear)
2037 */
2038 if (mddev->level > 0)
2039 return -ENOSPC;
2040 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002041 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 /* Verify rdev->desc_nr is unique.
2045 * If it is -1, assign a free number, else
2046 * check number is not in use
2047 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002048 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (rdev->desc_nr < 0) {
2050 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002051 if (mddev->pers)
2052 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002053 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 choice++;
2055 rdev->desc_nr = choice;
2056 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002057 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002058 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002062 rcu_read_unlock();
NeilBrownde01dfa2009-02-06 18:02:46 +11002063 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2064 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2065 mdname(mddev), mddev->max_disks);
2066 return -EBUSY;
2067 }
NeilBrown19133a42005-11-08 21:39:35 -08002068 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002069 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002070 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002073 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002074
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002075 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002076 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002077
Tejun Heo0762b8b2008-08-25 19:56:12 +09002078 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002079 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2080 /* failure here is OK */;
2081 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002082
NeilBrown4b809912008-07-21 17:05:25 +10002083 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002084 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002085
2086 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002087 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002090
2091 fail:
2092 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2093 b, mdname(mddev));
2094 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
NeilBrown177a99b2008-02-06 01:39:56 -08002097static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002098{
NeilBrown3cb03002011-10-11 16:45:26 +11002099 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002100 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002101 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002102}
2103
NeilBrownf72ffdd2014-09-30 14:23:59 +10002104static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
2106 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002107
Tejun Heo49731ba2011-01-14 18:43:57 +01002108 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002109 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2111 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002112 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002113 sysfs_put(rdev->sysfs_state);
2114 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002115 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002116 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002117 * writing to 'remove' to "dev/state". We also need
2118 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002119 */
NeilBrown4b809912008-07-21 17:05:25 +10002120 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002121 INIT_WORK(&rdev->del_work, md_delayed_delete);
2122 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002123 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
2126/*
2127 * prevent the device from being mounted, repartitioned or
2128 * otherwise reused by a RAID array (or any other kernel
2129 * subsystem), by bd_claiming the device.
2130 */
NeilBrown3cb03002011-10-11 16:45:26 +11002131static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
2133 int err = 0;
2134 struct block_device *bdev;
2135 char b[BDEVNAME_SIZE];
2136
Tejun Heod4d77622010-11-13 11:55:18 +01002137 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002138 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (IS_ERR(bdev)) {
2140 printk(KERN_ERR "md: could not open %s.\n",
2141 __bdevname(dev, b));
2142 return PTR_ERR(bdev);
2143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 rdev->bdev = bdev;
2145 return err;
2146}
2147
NeilBrown3cb03002011-10-11 16:45:26 +11002148static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
2150 struct block_device *bdev = rdev->bdev;
2151 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002152 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
2155void md_autodetect_dev(dev_t dev);
2156
NeilBrownf72ffdd2014-09-30 14:23:59 +10002157static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
2159 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 printk(KERN_INFO "md: export_rdev(%s)\n",
2162 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002163 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002165 if (test_bit(AutoDetected, &rdev->flags))
2166 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167#endif
2168 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002169 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002172void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 unbind_rdev_from_array(rdev);
2175 export_rdev(rdev);
2176}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002177EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
NeilBrownfd01b882011-10-11 16:47:53 +11002179static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
NeilBrown0638bb02014-09-25 17:43:47 +10002181 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
NeilBrown0638bb02014-09-25 17:43:47 +10002183 while (!list_empty(&mddev->disks)) {
2184 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2185 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002186 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 mddev->raid_disks = 0;
2189 mddev->major_version = 0;
2190}
2191
NeilBrownf72ffdd2014-09-30 14:23:59 +10002192static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
NeilBrown42543762006-06-26 00:27:57 -07002194 /* Update each superblock (in-memory image), but
2195 * if we are allowed to, skip spares which already
2196 * have the right event counter, or have one earlier
2197 * (which would mean they aren't being marked as dirty
2198 * with the rest of the array)
2199 */
NeilBrown3cb03002011-10-11 16:45:26 +11002200 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002201 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002202 if (rdev->sb_events == mddev->events ||
2203 (nospares &&
2204 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002205 rdev->sb_events+1 == mddev->events)) {
2206 /* Don't update this superblock */
2207 rdev->sb_loaded = 2;
2208 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002209 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002210 rdev->sb_loaded = 1;
2211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
2213}
2214
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002215void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
NeilBrown3cb03002011-10-11 16:45:26 +11002217 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002218 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002219 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002220 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
NeilBrownd87f0642013-04-24 11:42:40 +10002222 if (mddev->ro) {
2223 if (force_change)
2224 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2225 return;
2226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002228 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002229 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002230 if (rdev->raid_disk >= 0 &&
2231 mddev->delta_disks >= 0 &&
2232 !test_bit(In_sync, &rdev->flags) &&
2233 mddev->curr_resync_completed > rdev->recovery_offset)
2234 rdev->recovery_offset = mddev->curr_resync_completed;
2235
NeilBrownf72ffdd2014-09-30 14:23:59 +10002236 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002237 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002238 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002239 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002240 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002241 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002242 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002243 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002244 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002245 md_ack_all_badblocks(&rdev->badblocks);
2246 md_error(mddev, rdev);
2247 }
2248 clear_bit(Blocked, &rdev->flags);
2249 clear_bit(BlockedBadBlocks, &rdev->flags);
2250 wake_up(&rdev->blocked_wait);
2251 }
2252 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002253 wake_up(&mddev->sb_wait);
2254 return;
2255 }
2256
NeilBrown85572d72014-12-15 12:56:56 +11002257 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002258
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002259 mddev->utime = get_seconds();
2260
NeilBrown850b2b42006-10-03 01:15:46 -07002261 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2262 force_change = 1;
2263 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2264 /* just a clean<-> dirty transition, possibly leave spares alone,
2265 * though if events isn't the right even/odd, we will have to do
2266 * spares after all
2267 */
2268 nospares = 1;
2269 if (force_change)
2270 nospares = 0;
2271 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002272 /* If the array is degraded, then skipping spares is both
2273 * dangerous and fairly pointless.
2274 * Dangerous because a device that was removed from the array
2275 * might have a event_count that still looks up-to-date,
2276 * so it can be re-added without a resync.
2277 * Pointless because if there are any spares to skip,
2278 * then a recovery will happen and soon that array won't
2279 * be degraded any more and the spare can go back to sleep then.
2280 */
NeilBrown850b2b42006-10-03 01:15:46 -07002281 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002282
NeilBrown06d91a52005-06-21 17:17:12 -07002283 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002284
2285 /* If this is just a dirty<->clean transition, and the array is clean
2286 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002287 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002288 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002289 && mddev->can_decrease_events
2290 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002291 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002292 mddev->can_decrease_events = 0;
2293 } else {
NeilBrown42543762006-06-26 00:27:57 -07002294 /* otherwise we have to go forward and ... */
2295 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002296 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
NeilBrown403df472014-09-30 15:52:29 +10002299 /*
2300 * This 64-bit counter should never wrap.
2301 * Either we are in around ~1 trillion A.C., assuming
2302 * 1 reboot per second, or we have a bug...
2303 */
2304 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002305
NeilBrowndafb20f2012-03-19 12:46:39 +11002306 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002307 if (rdev->badblocks.changed)
2308 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002309 if (test_bit(Faulty, &rdev->flags))
2310 set_bit(FaultRecorded, &rdev->flags);
2311 }
NeilBrown2699b672011-07-28 11:31:47 +10002312
NeilBrowne6910632008-02-06 01:39:51 -08002313 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002314 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
NeilBrown36a4e1f2011-10-07 14:23:17 +11002316 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2317 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
NeilBrown4ad13662007-07-17 04:06:13 -07002319 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002320 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002322
NeilBrown42543762006-06-26 00:27:57 -07002323 if (rdev->sb_loaded != 1)
2324 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
NeilBrownf4667222013-12-09 12:04:56 +11002326 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002327 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002328 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002329 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002330 pr_debug("md: (write) %s's sb offset: %llu\n",
2331 bdevname(rdev->bdev, b),
2332 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002333 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002334 if (rdev->badblocks.size) {
2335 md_super_write(mddev, rdev,
2336 rdev->badblocks.sector,
2337 rdev->badblocks.size << 9,
2338 rdev->bb_page);
2339 rdev->badblocks.size = 0;
2340 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002341
NeilBrownf4667222013-12-09 12:04:56 +11002342 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002343 pr_debug("md: %s (skipping faulty)\n",
2344 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002345
NeilBrown7bfa19f2005-06-21 17:17:28 -07002346 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 /* only need to write one superblock... */
2348 break;
2349 }
NeilBrowna9701a32005-11-08 21:39:34 -08002350 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002351 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002352
NeilBrown85572d72014-12-15 12:56:56 +11002353 spin_lock(&mddev->lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002354 if (mddev->in_sync != sync_req ||
2355 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002356 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002357 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002358 goto repeat;
2359 }
NeilBrown850b2b42006-10-03 01:15:46 -07002360 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002361 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002362 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002363 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2364 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002365
NeilBrowndafb20f2012-03-19 12:46:39 +11002366 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002367 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2368 clear_bit(Blocked, &rdev->flags);
2369
2370 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002371 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002372 clear_bit(BlockedBadBlocks, &rdev->flags);
2373 wake_up(&rdev->blocked_wait);
2374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002376EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Andre Noll7f6ce762008-03-23 18:34:54 +01002378/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002379 * We want to accept with case. For this we use cmd_match.
2380 */
2381static int cmd_match(const char *cmd, const char *str)
2382{
2383 /* See if cmd, written into a sysfs file, matches
2384 * str. They must either be the same, or cmd can
2385 * have a trailing newline
2386 */
2387 while (*cmd && *str && *cmd == *str) {
2388 cmd++;
2389 str++;
2390 }
2391 if (*cmd == '\n')
2392 cmd++;
2393 if (*str || *cmd)
2394 return 0;
2395 return 1;
2396}
2397
NeilBrown86e6ffd2005-11-08 21:39:24 -08002398struct rdev_sysfs_entry {
2399 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002400 ssize_t (*show)(struct md_rdev *, char *);
2401 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002402};
2403
2404static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002405state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002406{
2407 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002408 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002409 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002410
NeilBrown758bfc82014-12-15 12:56:59 +11002411 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002412 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002413 len+= sprintf(page+len, "%sfaulty",sep);
2414 sep = ",";
2415 }
NeilBrown758bfc82014-12-15 12:56:59 +11002416 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002417 len += sprintf(page+len, "%sin_sync",sep);
2418 sep = ",";
2419 }
NeilBrown758bfc82014-12-15 12:56:59 +11002420 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002421 len += sprintf(page+len, "%swrite_mostly",sep);
2422 sep = ",";
2423 }
NeilBrown758bfc82014-12-15 12:56:59 +11002424 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002425 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002426 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002427 len += sprintf(page+len, "%sblocked", sep);
2428 sep = ",";
2429 }
NeilBrown758bfc82014-12-15 12:56:59 +11002430 if (!test_bit(Faulty, &flags) &&
2431 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002432 len += sprintf(page+len, "%sspare", sep);
2433 sep = ",";
2434 }
NeilBrown758bfc82014-12-15 12:56:59 +11002435 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002436 len += sprintf(page+len, "%swrite_error", sep);
2437 sep = ",";
2438 }
NeilBrown758bfc82014-12-15 12:56:59 +11002439 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002440 len += sprintf(page+len, "%swant_replacement", sep);
2441 sep = ",";
2442 }
NeilBrown758bfc82014-12-15 12:56:59 +11002443 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002444 len += sprintf(page+len, "%sreplacement", sep);
2445 sep = ",";
2446 }
2447
NeilBrown86e6ffd2005-11-08 21:39:24 -08002448 return len+sprintf(page+len, "\n");
2449}
2450
NeilBrown45dc2de2006-06-26 00:27:58 -07002451static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002452state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002453{
2454 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002455 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002456 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002457 * writemostly - sets write_mostly
2458 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002459 * blocked - sets the Blocked flags
2460 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002461 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002462 * -insync - clear Insync for a device with a slot assigned,
2463 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002464 * write_error - sets WriteErrorSeen
2465 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002466 */
2467 int err = -EINVAL;
2468 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2469 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002470 if (test_bit(Faulty, &rdev->flags))
2471 err = 0;
2472 else
2473 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002474 } else if (cmd_match(buf, "remove")) {
2475 if (rdev->raid_disk >= 0)
2476 err = -EBUSY;
2477 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002478 struct mddev *mddev = rdev->mddev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05002479 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05002480 md_cluster_ops->remove_disk(mddev, rdev);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002481 md_kick_rdev_from_array(rdev);
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05002482 if (mddev_is_clustered(mddev))
2483 md_cluster_ops->metadata_update_start(mddev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002484 if (mddev->pers)
2485 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002486 md_new_event(mddev);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05002487 if (mddev_is_clustered(mddev))
2488 md_cluster_ops->metadata_update_finish(mddev);
NeilBrown45dc2de2006-06-26 00:27:58 -07002489 err = 0;
2490 }
NeilBrownf6556752006-06-26 00:28:01 -07002491 } else if (cmd_match(buf, "writemostly")) {
2492 set_bit(WriteMostly, &rdev->flags);
2493 err = 0;
2494 } else if (cmd_match(buf, "-writemostly")) {
2495 clear_bit(WriteMostly, &rdev->flags);
2496 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002497 } else if (cmd_match(buf, "blocked")) {
2498 set_bit(Blocked, &rdev->flags);
2499 err = 0;
2500 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002501 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002502 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002503 /* metadata handler doesn't understand badblocks,
2504 * so we need to fail the device
2505 */
2506 md_error(rdev->mddev, rdev);
2507 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002508 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002509 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002510 wake_up(&rdev->blocked_wait);
2511 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2512 md_wakeup_thread(rdev->mddev->thread);
2513
2514 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002515 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2516 set_bit(In_sync, &rdev->flags);
2517 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002518 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
NeilBrowne1960f82014-09-30 15:24:25 +10002519 if (rdev->mddev->pers == NULL) {
2520 clear_bit(In_sync, &rdev->flags);
2521 rdev->saved_raid_disk = rdev->raid_disk;
2522 rdev->raid_disk = -1;
2523 err = 0;
2524 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002525 } else if (cmd_match(buf, "write_error")) {
2526 set_bit(WriteErrorSeen, &rdev->flags);
2527 err = 0;
2528 } else if (cmd_match(buf, "-write_error")) {
2529 clear_bit(WriteErrorSeen, &rdev->flags);
2530 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002531 } else if (cmd_match(buf, "want_replacement")) {
2532 /* Any non-spare device that is not a replacement can
2533 * become want_replacement at any time, but we then need to
2534 * check if recovery is needed.
2535 */
2536 if (rdev->raid_disk >= 0 &&
2537 !test_bit(Replacement, &rdev->flags))
2538 set_bit(WantReplacement, &rdev->flags);
2539 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2540 md_wakeup_thread(rdev->mddev->thread);
2541 err = 0;
2542 } else if (cmd_match(buf, "-want_replacement")) {
2543 /* Clearing 'want_replacement' is always allowed.
2544 * Once replacements starts it is too late though.
2545 */
2546 err = 0;
2547 clear_bit(WantReplacement, &rdev->flags);
2548 } else if (cmd_match(buf, "replacement")) {
2549 /* Can only set a device as a replacement when array has not
2550 * yet been started. Once running, replacement is automatic
2551 * from spares, or by assigning 'slot'.
2552 */
2553 if (rdev->mddev->pers)
2554 err = -EBUSY;
2555 else {
2556 set_bit(Replacement, &rdev->flags);
2557 err = 0;
2558 }
2559 } else if (cmd_match(buf, "-replacement")) {
2560 /* Similarly, can only clear Replacement before start */
2561 if (rdev->mddev->pers)
2562 err = -EBUSY;
2563 else {
2564 clear_bit(Replacement, &rdev->flags);
2565 err = 0;
2566 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002567 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002568 if (!err)
2569 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002570 return err ? err : len;
2571}
NeilBrown80ca3a42006-07-10 04:44:18 -07002572static struct rdev_sysfs_entry rdev_state =
2573__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002574
2575static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002576errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002577{
2578 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2579}
2580
2581static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002582errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002583{
2584 char *e;
2585 unsigned long n = simple_strtoul(buf, &e, 10);
2586 if (*buf && (*e == 0 || *e == '\n')) {
2587 atomic_set(&rdev->corrected_errors, n);
2588 return len;
2589 }
2590 return -EINVAL;
2591}
2592static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002593__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002594
NeilBrown014236d2006-01-06 00:20:55 -08002595static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002596slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002597{
2598 if (rdev->raid_disk < 0)
2599 return sprintf(page, "none\n");
2600 else
2601 return sprintf(page, "%d\n", rdev->raid_disk);
2602}
2603
2604static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002605slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002606{
2607 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002608 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002609 int slot = simple_strtoul(buf, &e, 10);
2610 if (strncmp(buf, "none", 4)==0)
2611 slot = -1;
2612 else if (e==buf || (*e && *e!= '\n'))
2613 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002614 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002615 /* Setting 'slot' on an active array requires also
2616 * updating the 'rd%d' link, and communicating
2617 * with the personality with ->hot_*_disk.
2618 * For now we only support removing
2619 * failed/spare devices. This normally happens automatically,
2620 * but not when the metadata is externally managed.
2621 */
NeilBrownc303da62008-02-06 01:39:51 -08002622 if (rdev->raid_disk == -1)
2623 return -EEXIST;
2624 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002625 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002626 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002627 clear_bit(Blocked, &rdev->flags);
2628 remove_and_add_spares(rdev->mddev, rdev);
2629 if (rdev->raid_disk >= 0)
2630 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002631 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2632 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002633 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002634 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002635 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002636 */
2637
2638 if (rdev->raid_disk != -1)
2639 return -EBUSY;
2640
NeilBrownc6751b22011-02-02 11:57:13 +11002641 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2642 return -EBUSY;
2643
Neil Brown6c2fce22008-06-28 08:31:31 +10002644 if (rdev->mddev->pers->hot_add_disk == NULL)
2645 return -EINVAL;
2646
NeilBrownba1b41b2011-01-14 09:14:34 +11002647 if (slot >= rdev->mddev->raid_disks &&
2648 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2649 return -ENOSPC;
2650
Neil Brown6c2fce22008-06-28 08:31:31 +10002651 rdev->raid_disk = slot;
2652 if (test_bit(In_sync, &rdev->flags))
2653 rdev->saved_raid_disk = slot;
2654 else
2655 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002656 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002657 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002658 err = rdev->mddev->pers->
2659 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002660 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002661 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002662 return err;
Neil Brown52664732008-06-28 08:31:44 +10002663 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002664 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002665 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002666 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002667 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002668 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002669 if (slot >= rdev->mddev->raid_disks &&
2670 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002671 return -ENOSPC;
2672 rdev->raid_disk = slot;
2673 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002674 clear_bit(Faulty, &rdev->flags);
2675 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002676 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002677 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002678 }
NeilBrown014236d2006-01-06 00:20:55 -08002679 return len;
2680}
2681
NeilBrown014236d2006-01-06 00:20:55 -08002682static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002683__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002684
NeilBrown93c8cad2006-01-06 00:20:56 -08002685static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002686offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002687{
Andrew Morton6961ece2006-01-06 00:20:59 -08002688 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002689}
2690
2691static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002692offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002693{
NeilBrownc6563a82012-05-21 09:27:00 +10002694 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002695 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002696 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002697 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002698 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002699 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002700 /* Must set offset before size, so overlap checks
2701 * can be sane */
2702 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002703 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002704 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002705 return len;
2706}
2707
2708static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002709__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002710
NeilBrownc6563a82012-05-21 09:27:00 +10002711static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2712{
2713 return sprintf(page, "%llu\n",
2714 (unsigned long long)rdev->new_data_offset);
2715}
2716
2717static ssize_t new_offset_store(struct md_rdev *rdev,
2718 const char *buf, size_t len)
2719{
2720 unsigned long long new_offset;
2721 struct mddev *mddev = rdev->mddev;
2722
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002723 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002724 return -EINVAL;
2725
NeilBrownf851b602014-12-11 10:02:10 +11002726 if (mddev->sync_thread ||
2727 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002728 return -EBUSY;
2729 if (new_offset == rdev->data_offset)
2730 /* reset is always permitted */
2731 ;
2732 else if (new_offset > rdev->data_offset) {
2733 /* must not push array size beyond rdev_sectors */
2734 if (new_offset - rdev->data_offset
2735 + mddev->dev_sectors > rdev->sectors)
2736 return -E2BIG;
2737 }
2738 /* Metadata worries about other space details. */
2739
2740 /* decreasing the offset is inconsistent with a backwards
2741 * reshape.
2742 */
2743 if (new_offset < rdev->data_offset &&
2744 mddev->reshape_backwards)
2745 return -EINVAL;
2746 /* Increasing offset is inconsistent with forwards
2747 * reshape. reshape_direction should be set to
2748 * 'backwards' first.
2749 */
2750 if (new_offset > rdev->data_offset &&
2751 !mddev->reshape_backwards)
2752 return -EINVAL;
2753
2754 if (mddev->pers && mddev->persistent &&
2755 !super_types[mddev->major_version]
2756 .allow_new_offset(rdev, new_offset))
2757 return -E2BIG;
2758 rdev->new_data_offset = new_offset;
2759 if (new_offset > rdev->data_offset)
2760 mddev->reshape_backwards = 1;
2761 else if (new_offset < rdev->data_offset)
2762 mddev->reshape_backwards = 0;
2763
2764 return len;
2765}
2766static struct rdev_sysfs_entry rdev_new_offset =
2767__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2768
NeilBrown83303b62006-01-06 00:21:06 -08002769static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002770rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002771{
Andre Nolldd8ac332009-03-31 14:33:13 +11002772 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002773}
2774
NeilBrownc5d79ad2008-02-06 01:39:54 -08002775static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2776{
2777 /* check if two start/length pairs overlap */
2778 if (s1+l1 <= s2)
2779 return 0;
2780 if (s2+l2 <= s1)
2781 return 0;
2782 return 1;
2783}
2784
Dan Williamsb522adc2009-03-31 15:00:31 +11002785static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2786{
2787 unsigned long long blocks;
2788 sector_t new;
2789
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002790 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002791 return -EINVAL;
2792
2793 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2794 return -EINVAL; /* sector conversion overflow */
2795
2796 new = blocks * 2;
2797 if (new != blocks * 2)
2798 return -EINVAL; /* unsigned long long to sector_t overflow */
2799
2800 *sectors = new;
2801 return 0;
2802}
2803
NeilBrown83303b62006-01-06 00:21:06 -08002804static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002805rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002806{
NeilBrownfd01b882011-10-11 16:47:53 +11002807 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002808 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002809 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002810
Dan Williamsb522adc2009-03-31 15:00:31 +11002811 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002812 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002813 if (rdev->data_offset != rdev->new_data_offset)
2814 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002815 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002816 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002817 sectors = super_types[my_mddev->major_version].
2818 rdev_size_change(rdev, sectors);
2819 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002820 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002821 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002822 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002823 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002824 if (!my_mddev->pers->resize)
2825 /* Cannot change size for RAID0 or Linear etc */
2826 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002827 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002828 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002829 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002830
Andre Nolldd8ac332009-03-31 14:33:13 +11002831 rdev->sectors = sectors;
2832 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002833 /* Need to check that all other rdevs with the same
2834 * ->bdev do not overlap. 'rcu' is sufficient to walk
2835 * the rdev lists safely.
2836 * This check does not provide a hard guarantee, it
2837 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002838 */
NeilBrownfd01b882011-10-11 16:47:53 +11002839 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002840 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002841 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002842
NeilBrown8b1afc32014-09-29 15:33:20 +10002843 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002844 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002845 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002846
NeilBrowndafb20f2012-03-19 12:46:39 +11002847 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002848 if (rdev->bdev == rdev2->bdev &&
2849 rdev != rdev2 &&
2850 overlaps(rdev->data_offset, rdev->sectors,
2851 rdev2->data_offset,
2852 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002853 overlap = 1;
2854 break;
2855 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002856 if (overlap) {
2857 mddev_put(mddev);
2858 break;
2859 }
2860 }
NeilBrown8b1afc32014-09-29 15:33:20 +10002861 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08002862 if (overlap) {
2863 /* Someone else could have slipped in a size
2864 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002865 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002866 * safe, and trust userspace not to race with
2867 * itself
2868 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002869 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002870 return -EBUSY;
2871 }
2872 }
NeilBrown83303b62006-01-06 00:21:06 -08002873 return len;
2874}
2875
2876static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002877__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002878
NeilBrown3cb03002011-10-11 16:45:26 +11002879static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002880{
2881 unsigned long long recovery_start = rdev->recovery_offset;
2882
2883 if (test_bit(In_sync, &rdev->flags) ||
2884 recovery_start == MaxSector)
2885 return sprintf(page, "none\n");
2886
2887 return sprintf(page, "%llu\n", recovery_start);
2888}
2889
NeilBrown3cb03002011-10-11 16:45:26 +11002890static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002891{
2892 unsigned long long recovery_start;
2893
2894 if (cmd_match(buf, "none"))
2895 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002896 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07002897 return -EINVAL;
2898
2899 if (rdev->mddev->pers &&
2900 rdev->raid_disk >= 0)
2901 return -EBUSY;
2902
2903 rdev->recovery_offset = recovery_start;
2904 if (recovery_start == MaxSector)
2905 set_bit(In_sync, &rdev->flags);
2906 else
2907 clear_bit(In_sync, &rdev->flags);
2908 return len;
2909}
2910
2911static struct rdev_sysfs_entry rdev_recovery_start =
2912__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2913
NeilBrown16c791a2011-07-28 11:31:47 +10002914static ssize_t
2915badblocks_show(struct badblocks *bb, char *page, int unack);
2916static ssize_t
2917badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2918
NeilBrown3cb03002011-10-11 16:45:26 +11002919static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002920{
2921 return badblocks_show(&rdev->badblocks, page, 0);
2922}
NeilBrown3cb03002011-10-11 16:45:26 +11002923static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002924{
NeilBrownde393cd2011-07-28 11:31:48 +10002925 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2926 /* Maybe that ack was all we needed */
2927 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2928 wake_up(&rdev->blocked_wait);
2929 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002930}
2931static struct rdev_sysfs_entry rdev_bad_blocks =
2932__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2933
NeilBrown3cb03002011-10-11 16:45:26 +11002934static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002935{
2936 return badblocks_show(&rdev->badblocks, page, 1);
2937}
NeilBrown3cb03002011-10-11 16:45:26 +11002938static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002939{
2940 return badblocks_store(&rdev->badblocks, page, len, 1);
2941}
2942static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2943__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2944
NeilBrown86e6ffd2005-11-08 21:39:24 -08002945static struct attribute *rdev_default_attrs[] = {
2946 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002947 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002948 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002949 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10002950 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002951 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002952 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002953 &rdev_bad_blocks.attr,
2954 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002955 NULL,
2956};
2957static ssize_t
2958rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2959{
2960 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002961 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002962
2963 if (!entry->show)
2964 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11002965 if (!rdev->mddev)
2966 return -EBUSY;
2967 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002968}
2969
2970static ssize_t
2971rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2972 const char *page, size_t length)
2973{
2974 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002975 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002976 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11002977 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002978
2979 if (!entry->store)
2980 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002981 if (!capable(CAP_SYS_ADMIN))
2982 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002983 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002984 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002985 if (rdev->mddev == NULL)
2986 rv = -EBUSY;
2987 else
2988 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002989 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002990 }
2991 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002992}
2993
2994static void rdev_free(struct kobject *ko)
2995{
NeilBrown3cb03002011-10-11 16:45:26 +11002996 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002997 kfree(rdev);
2998}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002999static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003000 .show = rdev_attr_show,
3001 .store = rdev_attr_store,
3002};
3003static struct kobj_type rdev_ktype = {
3004 .release = rdev_free,
3005 .sysfs_ops = &rdev_sysfs_ops,
3006 .default_attrs = rdev_default_attrs,
3007};
3008
NeilBrown3cb03002011-10-11 16:45:26 +11003009int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003010{
3011 rdev->desc_nr = -1;
3012 rdev->saved_raid_disk = -1;
3013 rdev->raid_disk = -1;
3014 rdev->flags = 0;
3015 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003016 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003017 rdev->sb_events = 0;
3018 rdev->last_read_error.tv_sec = 0;
3019 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003020 rdev->sb_loaded = 0;
3021 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003022 atomic_set(&rdev->nr_pending, 0);
3023 atomic_set(&rdev->read_errors, 0);
3024 atomic_set(&rdev->corrected_errors, 0);
3025
3026 INIT_LIST_HEAD(&rdev->same_set);
3027 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003028
3029 /* Add space to store bad block list.
3030 * This reserves the space even on arrays where it cannot
3031 * be used - I wonder if that matters
3032 */
3033 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003034 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003035 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3036 seqlock_init(&rdev->badblocks.lock);
3037 if (rdev->badblocks.page == NULL)
3038 return -ENOMEM;
3039
3040 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003041}
3042EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043/*
3044 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3045 *
3046 * mark the device faulty if:
3047 *
3048 * - the device is nonexistent (zero size)
3049 * - the device has no valid superblock
3050 *
3051 * a faulty rdev _never_ has rdev->sb set.
3052 */
NeilBrown3cb03002011-10-11 16:45:26 +11003053static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
3055 char b[BDEVNAME_SIZE];
3056 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003057 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 sector_t size;
3059
NeilBrown9ffae0c2006-01-06 00:20:32 -08003060 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 if (!rdev) {
3062 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3063 return ERR_PTR(-ENOMEM);
3064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
NeilBrown2230dfe2011-07-28 11:31:46 +10003066 err = md_rdev_init(rdev);
3067 if (err)
3068 goto abort_free;
3069 err = alloc_disk_sb(rdev);
3070 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 goto abort_free;
3072
NeilBrownc5d79ad2008-02-06 01:39:54 -08003073 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 if (err)
3075 goto abort_free;
3076
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003077 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003078
Mike Snitzer77304d22010-11-08 14:39:12 +01003079 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003081 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 "md: %s has zero or unknown size, marking faulty!\n",
3083 bdevname(rdev->bdev,b));
3084 err = -EINVAL;
3085 goto abort_free;
3086 }
3087
3088 if (super_format >= 0) {
3089 err = super_types[super_format].
3090 load_super(rdev, NULL, super_minor);
3091 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003092 printk(KERN_WARNING
3093 "md: %s does not have a valid v%d.%d "
3094 "superblock, not importing!\n",
3095 bdevname(rdev->bdev,b),
3096 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 goto abort_free;
3098 }
3099 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003100 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 "md: could not read %s's sb, not importing!\n",
3102 bdevname(rdev->bdev,b));
3103 goto abort_free;
3104 }
3105 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 return rdev;
3108
3109abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003110 if (rdev->bdev)
3111 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003112 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 kfree(rdev);
3114 return ERR_PTR(err);
3115}
3116
3117/*
3118 * Check a full RAID array for plausibility
3119 */
3120
NeilBrownf72ffdd2014-09-30 14:23:59 +10003121static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122{
3123 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003124 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 char b[BDEVNAME_SIZE];
3126
3127 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003128 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 switch (super_types[mddev->major_version].
3130 load_super(rdev, freshest, mddev->minor_version)) {
3131 case 1:
3132 freshest = rdev;
3133 break;
3134 case 0:
3135 break;
3136 default:
3137 printk( KERN_ERR \
3138 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003139 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003141 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 }
3143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 super_types[mddev->major_version].
3145 validate_super(mddev, freshest);
3146
3147 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003148 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003149 if (mddev->max_disks &&
3150 (rdev->desc_nr >= mddev->max_disks ||
3151 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003152 printk(KERN_WARNING
3153 "md: %s: %s: only %d devices permitted\n",
3154 mdname(mddev), bdevname(rdev->bdev, b),
3155 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003156 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003157 continue;
3158 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003159 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 if (super_types[mddev->major_version].
3161 validate_super(mddev, rdev)) {
3162 printk(KERN_WARNING "md: kicking non-fresh %s"
3163 " from array!\n",
3164 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003165 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 continue;
3167 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003168 /* No device should have a Candidate flag
3169 * when reading devices
3170 */
3171 if (test_bit(Candidate, &rdev->flags)) {
3172 pr_info("md: kicking Cluster Candidate %s from array!\n",
3173 bdevname(rdev->bdev, b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003174 md_kick_rdev_from_array(rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003175 }
3176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 if (mddev->level == LEVEL_MULTIPATH) {
3178 rdev->desc_nr = i++;
3179 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003180 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003181 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003182 rdev->raid_disk = -1;
3183 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 }
3185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186}
3187
NeilBrown72e02072009-12-14 12:49:55 +11003188/* Read a fixed-point number.
3189 * Numbers in sysfs attributes should be in "standard" units where
3190 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003191 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003192 * milliseconds or jiffies.
3193 * This function takes a decimal number with a possible fractional
3194 * component, and produces an integer which is the result of
3195 * multiplying that number by 10^'scale'.
3196 * all without any floating-point arithmetic.
3197 */
3198int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3199{
3200 unsigned long result = 0;
3201 long decimals = -1;
3202 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3203 if (*cp == '.')
3204 decimals = 0;
3205 else if (decimals < scale) {
3206 unsigned int value;
3207 value = *cp - '0';
3208 result = result * 10 + value;
3209 if (decimals >= 0)
3210 decimals++;
3211 }
3212 cp++;
3213 }
3214 if (*cp == '\n')
3215 cp++;
3216 if (*cp)
3217 return -EINVAL;
3218 if (decimals < 0)
3219 decimals = 0;
3220 while (decimals < scale) {
3221 result *= 10;
3222 decimals ++;
3223 }
3224 *res = result;
3225 return 0;
3226}
3227
NeilBrown19052c02008-08-05 15:54:13 +10003228static void md_safemode_timeout(unsigned long data);
3229
NeilBrowneae17012005-11-08 21:39:23 -08003230static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003231safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003232{
3233 int msec = (mddev->safemode_delay*1000)/HZ;
3234 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3235}
3236static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003237safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003238{
NeilBrown16f17b32006-06-26 00:27:37 -07003239 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003240
NeilBrown72e02072009-12-14 12:49:55 +11003241 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003242 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003243 if (msec == 0)
3244 mddev->safemode_delay = 0;
3245 else {
NeilBrown19052c02008-08-05 15:54:13 +10003246 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003247 unsigned long new_delay = (msec*HZ)/1000;
3248
3249 if (new_delay == 0)
3250 new_delay = 1;
3251 mddev->safemode_delay = new_delay;
3252 if (new_delay < old_delay || old_delay == 0)
3253 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003254 }
3255 return len;
3256}
3257static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003258__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003259
3260static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003261level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003262{
NeilBrown36d091f2014-12-15 12:56:58 +11003263 struct md_personality *p;
3264 int ret;
3265 spin_lock(&mddev->lock);
3266 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003267 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003268 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003269 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003270 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003271 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003272 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003273 else
NeilBrown36d091f2014-12-15 12:56:58 +11003274 ret = 0;
3275 spin_unlock(&mddev->lock);
3276 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003277}
3278
NeilBrownd9d166c2006-01-06 00:20:51 -08003279static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003280level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003281{
Dan Williamsf2859af2010-05-02 10:04:16 -07003282 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003283 ssize_t rv;
3284 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003285 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003286 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003287 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003288 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003289
NeilBrown67918752014-12-15 12:57:01 +11003290 if (slen == 0 || slen >= sizeof(clevel))
3291 return -EINVAL;
3292
3293 rv = mddev_lock(mddev);
3294 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003295 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003296
3297 if (mddev->pers == NULL) {
3298 strncpy(mddev->clevel, buf, slen);
3299 if (mddev->clevel[slen-1] == '\n')
3300 slen--;
3301 mddev->clevel[slen] = 0;
3302 mddev->level = LEVEL_NONE;
3303 rv = len;
3304 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003305 }
NeilBrown67918752014-12-15 12:57:01 +11003306 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003307 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003308 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003309
3310 /* request to change the personality. Need to ensure:
3311 * - array is not engaged in resync/recovery/reshape
3312 * - old personality can be suspended
3313 * - new personality will access other array.
3314 */
3315
NeilBrown67918752014-12-15 12:57:01 +11003316 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003317 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003318 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003319 mddev->reshape_position != MaxSector ||
3320 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003321 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003322
NeilBrown67918752014-12-15 12:57:01 +11003323 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003324 if (!mddev->pers->quiesce) {
3325 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3326 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003327 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003328 }
3329
3330 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003331 strncpy(clevel, buf, slen);
3332 if (clevel[slen-1] == '\n')
3333 slen--;
3334 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003335 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003336 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003337
Dan Williamsf2859af2010-05-02 10:04:16 -07003338 if (request_module("md-%s", clevel) != 0)
3339 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003340 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003341 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003342 if (!pers || !try_module_get(pers->owner)) {
3343 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003344 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003345 rv = -EINVAL;
3346 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003347 }
3348 spin_unlock(&pers_lock);
3349
3350 if (pers == mddev->pers) {
3351 /* Nothing to do! */
3352 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003353 rv = len;
3354 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003355 }
3356 if (!pers->takeover) {
3357 module_put(pers->owner);
3358 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003359 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003360 rv = -EINVAL;
3361 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003362 }
3363
NeilBrowndafb20f2012-03-19 12:46:39 +11003364 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003365 rdev->new_raid_disk = rdev->raid_disk;
3366
NeilBrown245f46c2009-03-31 14:39:39 +11003367 /* ->takeover must set new_* and/or delta_disks
3368 * if it succeeds, and may set them when it fails.
3369 */
3370 priv = pers->takeover(mddev);
3371 if (IS_ERR(priv)) {
3372 mddev->new_level = mddev->level;
3373 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003374 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003375 mddev->raid_disks -= mddev->delta_disks;
3376 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003377 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003378 module_put(pers->owner);
3379 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003380 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003381 rv = PTR_ERR(priv);
3382 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003383 }
3384
3385 /* Looks like we have a winner */
3386 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003387 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003388
3389 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003390 oldpers = mddev->pers;
3391 oldpriv = mddev->private;
3392 mddev->pers = pers;
3393 mddev->private = priv;
3394 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3395 mddev->level = mddev->new_level;
3396 mddev->layout = mddev->new_layout;
3397 mddev->chunk_sectors = mddev->new_chunk_sectors;
3398 mddev->delta_disks = 0;
3399 mddev->reshape_backwards = 0;
3400 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003401 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003402
NeilBrowndb721d32014-12-15 12:56:58 +11003403 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003404 mddev->external) {
3405 /* We are converting from a no-redundancy array
3406 * to a redundancy array and metadata is managed
3407 * externally so we need to be sure that writes
3408 * won't block due to a need to transition
3409 * clean->dirty
3410 * until external management is started.
3411 */
3412 mddev->in_sync = 0;
3413 mddev->safemode_delay = 0;
3414 mddev->safemode = 0;
3415 }
3416
NeilBrowndb721d32014-12-15 12:56:58 +11003417 oldpers->free(mddev, oldpriv);
3418
3419 if (oldpers->sync_request == NULL &&
3420 pers->sync_request != NULL) {
3421 /* need to add the md_redundancy_group */
3422 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3423 printk(KERN_WARNING
3424 "md: cannot register extra attributes for %s\n",
3425 mdname(mddev));
3426 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3427 }
3428 if (oldpers->sync_request != NULL &&
3429 pers->sync_request == NULL) {
3430 /* need to remove the md_redundancy_group */
3431 if (mddev->to_remove == NULL)
3432 mddev->to_remove = &md_redundancy_group;
3433 }
3434
NeilBrowndafb20f2012-03-19 12:46:39 +11003435 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003436 if (rdev->raid_disk < 0)
3437 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003438 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003439 rdev->new_raid_disk = -1;
3440 if (rdev->new_raid_disk == rdev->raid_disk)
3441 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003442 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003443 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003444 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003445 if (rdev->raid_disk < 0)
3446 continue;
3447 if (rdev->new_raid_disk == rdev->raid_disk)
3448 continue;
3449 rdev->raid_disk = rdev->new_raid_disk;
3450 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003451 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003452 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003453 if (sysfs_link_rdev(mddev, rdev))
3454 printk(KERN_WARNING "md: cannot register rd%d"
3455 " for %s after level change\n",
3456 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003457 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003458 }
3459
NeilBrowndb721d32014-12-15 12:56:58 +11003460 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003461 /* this is now an array without redundancy, so
3462 * it must always be in_sync
3463 */
3464 mddev->in_sync = 1;
3465 del_timer_sync(&mddev->safemode_timer);
3466 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003467 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003468 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003469 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003470 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003471 if (!mddev->thread)
3472 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003473 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003474 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003475 rv = len;
3476out_unlock:
3477 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003478 return rv;
3479}
3480
3481static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003482__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003483
NeilBrownd4dbd022006-06-26 00:27:59 -07003484static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003485layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003486{
3487 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003488 if (mddev->reshape_position != MaxSector &&
3489 mddev->layout != mddev->new_layout)
3490 return sprintf(page, "%d (%d)\n",
3491 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003492 return sprintf(page, "%d\n", mddev->layout);
3493}
3494
3495static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003496layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003497{
3498 char *e;
3499 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11003500 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003501
3502 if (!*buf || (*e && *e != '\n'))
3503 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11003504 err = mddev_lock(mddev);
3505 if (err)
3506 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003507
NeilBrownb3546032009-03-31 14:56:41 +11003508 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003509 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003510 err = -EBUSY;
3511 else if (mddev->ro)
3512 err = -EROFS;
3513 else {
3514 mddev->new_layout = n;
3515 err = mddev->pers->check_reshape(mddev);
3516 if (err)
3517 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003518 }
NeilBrownb3546032009-03-31 14:56:41 +11003519 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003520 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003521 if (mddev->reshape_position == MaxSector)
3522 mddev->layout = n;
3523 }
NeilBrown67918752014-12-15 12:57:01 +11003524 mddev_unlock(mddev);
3525 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003526}
3527static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003528__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003529
NeilBrowneae17012005-11-08 21:39:23 -08003530static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003531raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003532{
NeilBrownbb636542005-11-08 21:39:45 -08003533 if (mddev->raid_disks == 0)
3534 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003535 if (mddev->reshape_position != MaxSector &&
3536 mddev->delta_disks != 0)
3537 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3538 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003539 return sprintf(page, "%d\n", mddev->raid_disks);
3540}
3541
NeilBrownfd01b882011-10-11 16:47:53 +11003542static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003543
3544static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003545raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003546{
NeilBrownda943b992006-01-06 00:20:54 -08003547 char *e;
NeilBrown67918752014-12-15 12:57:01 +11003548 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003549 unsigned long n = simple_strtoul(buf, &e, 10);
3550
3551 if (!*buf || (*e && *e != '\n'))
3552 return -EINVAL;
3553
NeilBrown67918752014-12-15 12:57:01 +11003554 err = mddev_lock(mddev);
3555 if (err)
3556 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003557 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003558 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003559 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003560 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003561 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003562
NeilBrown67918752014-12-15 12:57:01 +11003563 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003564 rdev_for_each(rdev, mddev) {
3565 if (olddisks < n &&
3566 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003567 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003568 if (olddisks > n &&
3569 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003570 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003571 }
NeilBrown67918752014-12-15 12:57:01 +11003572 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003573 mddev->delta_disks = n - olddisks;
3574 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003575 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003576 } else
NeilBrownda943b992006-01-06 00:20:54 -08003577 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003578out_unlock:
3579 mddev_unlock(mddev);
3580 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003581}
3582static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003583__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003584
NeilBrown24dd4692005-11-08 21:39:26 -08003585static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003586chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003587{
NeilBrown08a02ec2007-05-09 02:35:38 -07003588 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003589 mddev->chunk_sectors != mddev->new_chunk_sectors)
3590 return sprintf(page, "%d (%d)\n",
3591 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003592 mddev->chunk_sectors << 9);
3593 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003594}
3595
3596static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003597chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003598{
NeilBrown67918752014-12-15 12:57:01 +11003599 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003600 char *e;
3601 unsigned long n = simple_strtoul(buf, &e, 10);
3602
NeilBrown3b343802006-01-06 00:20:47 -08003603 if (!*buf || (*e && *e != '\n'))
3604 return -EINVAL;
3605
NeilBrown67918752014-12-15 12:57:01 +11003606 err = mddev_lock(mddev);
3607 if (err)
3608 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003609 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003610 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003611 err = -EBUSY;
3612 else if (mddev->ro)
3613 err = -EROFS;
3614 else {
3615 mddev->new_chunk_sectors = n >> 9;
3616 err = mddev->pers->check_reshape(mddev);
3617 if (err)
3618 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003619 }
NeilBrownb3546032009-03-31 14:56:41 +11003620 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003621 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003622 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003623 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003624 }
NeilBrown67918752014-12-15 12:57:01 +11003625 mddev_unlock(mddev);
3626 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003627}
3628static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003629__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003630
NeilBrowna94213b2006-06-26 00:28:00 -07003631static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003632resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003633{
NeilBrownd1a7c502009-03-31 15:24:32 +11003634 if (mddev->recovery_cp == MaxSector)
3635 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003636 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3637}
3638
3639static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003640resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003641{
NeilBrown67918752014-12-15 12:57:01 +11003642 int err;
NeilBrowna94213b2006-06-26 00:28:00 -07003643 char *e;
3644 unsigned long long n = simple_strtoull(buf, &e, 10);
3645
NeilBrown67918752014-12-15 12:57:01 +11003646 err = mddev_lock(mddev);
3647 if (err)
3648 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003649 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003650 err = -EBUSY;
3651 else if (cmd_match(buf, "none"))
Dan Williams06e3c812009-12-12 21:17:12 -07003652 n = MaxSector;
3653 else if (!*buf || (*e && *e != '\n'))
NeilBrown67918752014-12-15 12:57:01 +11003654 err = -EINVAL;
NeilBrowna94213b2006-06-26 00:28:00 -07003655
NeilBrown67918752014-12-15 12:57:01 +11003656 if (!err) {
3657 mddev->recovery_cp = n;
3658 if (mddev->pers)
3659 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3660 }
3661 mddev_unlock(mddev);
3662 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003663}
3664static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003665__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003666
NeilBrown9e653b62006-06-26 00:27:58 -07003667/*
3668 * The array state can be:
3669 *
3670 * clear
3671 * No devices, no size, no level
3672 * Equivalent to STOP_ARRAY ioctl
3673 * inactive
3674 * May have some settings, but array is not active
3675 * all IO results in error
3676 * When written, doesn't tear down array, but just stops it
3677 * suspended (not supported yet)
3678 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003679 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003680 * readonly
3681 * no resync can happen. no superblocks get written.
3682 * write requests fail
3683 * read-auto
3684 * like readonly, but behaves like 'clean' on a write request.
3685 *
3686 * clean - no pending writes, but otherwise active.
3687 * When written to inactive array, starts without resync
3688 * If a write request arrives then
3689 * if metadata is known, mark 'dirty' and switch to 'active'.
3690 * if not known, block and switch to write-pending
3691 * If written to an active array that has pending writes, then fails.
3692 * active
3693 * fully active: IO and resync can be happening.
3694 * When written to inactive array, starts with resync
3695 *
3696 * write-pending
3697 * clean, but writes are blocked waiting for 'active' to be written.
3698 *
3699 * active-idle
3700 * like active, but no writes have been seen for a while (100msec).
3701 *
3702 */
3703enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3704 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003705static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003706 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3707 "write-pending", "active-idle", NULL };
3708
3709static int match_word(const char *word, char **list)
3710{
3711 int n;
3712 for (n=0; list[n]; n++)
3713 if (cmd_match(word, list[n]))
3714 break;
3715 return n;
3716}
3717
3718static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003719array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003720{
3721 enum array_state st = inactive;
3722
3723 if (mddev->pers)
3724 switch(mddev->ro) {
3725 case 1:
3726 st = readonly;
3727 break;
3728 case 2:
3729 st = read_auto;
3730 break;
3731 case 0:
3732 if (mddev->in_sync)
3733 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003734 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003735 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003736 else if (mddev->safemode)
3737 st = active_idle;
3738 else
3739 st = active;
3740 }
3741 else {
3742 if (list_empty(&mddev->disks) &&
3743 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003744 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003745 st = clear;
3746 else
3747 st = inactive;
3748 }
3749 return sprintf(page, "%s\n", array_states[st]);
3750}
3751
NeilBrownf72ffdd2014-09-30 14:23:59 +10003752static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3753static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3754static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003755static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003756
3757static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003758array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003759{
NeilBrown67918752014-12-15 12:57:01 +11003760 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003761 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003762
3763 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3764 /* don't take reconfig_mutex when toggling between
3765 * clean and active
3766 */
3767 spin_lock(&mddev->lock);
3768 if (st == active) {
3769 restart_array(mddev);
3770 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3771 wake_up(&mddev->sb_wait);
3772 err = 0;
3773 } else /* st == clean */ {
3774 restart_array(mddev);
3775 if (atomic_read(&mddev->writes_pending) == 0) {
3776 if (mddev->in_sync == 0) {
3777 mddev->in_sync = 1;
3778 if (mddev->safemode == 1)
3779 mddev->safemode = 0;
3780 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3781 }
3782 err = 0;
3783 } else
3784 err = -EBUSY;
3785 }
3786 spin_unlock(&mddev->lock);
3787 return err;
3788 }
3789 err = mddev_lock(mddev);
3790 if (err)
3791 return err;
3792 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003793 switch(st) {
3794 case bad_word:
3795 break;
3796 case clear:
3797 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003798 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003799 break;
3800 case inactive:
3801 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003802 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003803 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003804 else
NeilBrowne6910632008-02-06 01:39:51 -08003805 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003806 break;
3807 case suspended:
3808 break; /* not supported yet */
3809 case readonly:
3810 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003811 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003812 else {
3813 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003814 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003815 err = do_md_run(mddev);
3816 }
3817 break;
3818 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003819 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003820 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003821 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003822 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003823 err = restart_array(mddev);
3824 if (err == 0) {
3825 mddev->ro = 2;
3826 set_disk_ro(mddev->gendisk, 0);
3827 }
NeilBrown9e653b62006-06-26 00:27:58 -07003828 } else {
3829 mddev->ro = 2;
3830 err = do_md_run(mddev);
3831 }
3832 break;
3833 case clean:
3834 if (mddev->pers) {
3835 restart_array(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11003836 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003837 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003838 if (mddev->in_sync == 0) {
3839 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003840 if (mddev->safemode == 1)
3841 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003842 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003843 }
3844 err = 0;
3845 } else
3846 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11003847 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003848 } else
3849 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003850 break;
3851 case active:
3852 if (mddev->pers) {
3853 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003854 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003855 wake_up(&mddev->sb_wait);
3856 err = 0;
3857 } else {
3858 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003859 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003860 err = do_md_run(mddev);
3861 }
3862 break;
3863 case write_pending:
3864 case active_idle:
3865 /* these cannot be set */
3866 break;
3867 }
NeilBrown67918752014-12-15 12:57:01 +11003868
3869 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11003870 if (mddev->hold_active == UNTIL_IOCTL)
3871 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003872 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10003873 }
NeilBrown67918752014-12-15 12:57:01 +11003874 mddev_unlock(mddev);
3875 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07003876}
NeilBrown80ca3a42006-07-10 04:44:18 -07003877static struct md_sysfs_entry md_array_state =
3878__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003879
NeilBrown6d7ff7382006-01-06 00:21:16 -08003880static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003881max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003882 return sprintf(page, "%d\n",
3883 atomic_read(&mddev->max_corr_read_errors));
3884}
3885
3886static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003887max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003888{
3889 char *e;
3890 unsigned long n = simple_strtoul(buf, &e, 10);
3891
3892 if (*buf && (*e == 0 || *e == '\n')) {
3893 atomic_set(&mddev->max_corr_read_errors, n);
3894 return len;
3895 }
3896 return -EINVAL;
3897}
3898
3899static struct md_sysfs_entry max_corr_read_errors =
3900__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3901 max_corrected_read_errors_store);
3902
3903static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003904null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003905{
3906 return -EINVAL;
3907}
3908
3909static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003910new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003911{
3912 /* buf must be %d:%d\n? giving major and minor numbers */
3913 /* The new device is added to the array.
3914 * If the array has a persistent superblock, we read the
3915 * superblock to initialise info and check validity.
3916 * Otherwise, only checking done is that in bind_rdev_to_array,
3917 * which mainly checks size.
3918 */
3919 char *e;
3920 int major = simple_strtoul(buf, &e, 10);
3921 int minor;
3922 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003923 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003924 int err;
3925
3926 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3927 return -EINVAL;
3928 minor = simple_strtoul(e+1, &e, 10);
3929 if (*e && *e != '\n')
3930 return -EINVAL;
3931 dev = MKDEV(major, minor);
3932 if (major != MAJOR(dev) ||
3933 minor != MINOR(dev))
3934 return -EOVERFLOW;
3935
NeilBrown67918752014-12-15 12:57:01 +11003936 flush_workqueue(md_misc_wq);
3937
3938 err = mddev_lock(mddev);
3939 if (err)
3940 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003941 if (mddev->persistent) {
3942 rdev = md_import_device(dev, mddev->major_version,
3943 mddev->minor_version);
3944 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003945 struct md_rdev *rdev0
3946 = list_entry(mddev->disks.next,
3947 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08003948 err = super_types[mddev->major_version]
3949 .load_super(rdev, rdev0, mddev->minor_version);
3950 if (err < 0)
3951 goto out;
3952 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003953 } else if (mddev->external)
3954 rdev = md_import_device(dev, -2, -1);
3955 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003956 rdev = md_import_device(dev, -1, -1);
3957
3958 if (IS_ERR(rdev))
3959 return PTR_ERR(rdev);
3960 err = bind_rdev_to_array(rdev, mddev);
3961 out:
3962 if (err)
3963 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11003964 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08003965 return err ? err : len;
3966}
3967
3968static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003969__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003970
3971static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003972bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07003973{
3974 char *end;
3975 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11003976 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07003977
NeilBrown67918752014-12-15 12:57:01 +11003978 err = mddev_lock(mddev);
3979 if (err)
3980 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07003981 if (!mddev->bitmap)
3982 goto out;
3983 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3984 while (*buf) {
3985 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3986 if (buf == end) break;
3987 if (*end == '-') { /* range */
3988 buf = end + 1;
3989 end_chunk = simple_strtoul(buf, &end, 0);
3990 if (buf == end) break;
3991 }
3992 if (*end && !isspace(*end)) break;
3993 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003994 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003995 }
3996 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3997out:
NeilBrown67918752014-12-15 12:57:01 +11003998 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003999 return len;
4000}
4001
4002static struct md_sysfs_entry md_bitmap =
4003__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4004
4005static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004006size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004007{
Andre Noll58c0fed2009-03-31 14:33:13 +11004008 return sprintf(page, "%llu\n",
4009 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004010}
4011
NeilBrownfd01b882011-10-11 16:47:53 +11004012static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004013
4014static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004015size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004016{
4017 /* If array is inactive, we can reduce the component size, but
4018 * not increase it (except from 0).
4019 * If array is active, we can try an on-line resize
4020 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004021 sector_t sectors;
4022 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004023
Andre Noll58c0fed2009-03-31 14:33:13 +11004024 if (err < 0)
4025 return err;
NeilBrown67918752014-12-15 12:57:01 +11004026 err = mddev_lock(mddev);
4027 if (err)
4028 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004029 if (mddev->pers) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05004030 if (mddev_is_clustered(mddev))
4031 md_cluster_ops->metadata_update_start(mddev);
Andre Noll58c0fed2009-03-31 14:33:13 +11004032 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004033 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05004034 if (mddev_is_clustered(mddev))
4035 md_cluster_ops->metadata_update_finish(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004036 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004037 if (mddev->dev_sectors == 0 ||
4038 mddev->dev_sectors > sectors)
4039 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004040 else
4041 err = -ENOSPC;
4042 }
NeilBrown67918752014-12-15 12:57:01 +11004043 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004044 return err ? err : len;
4045}
4046
4047static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004048__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004049
Masanari Iida83f0d772012-10-30 00:18:08 +09004050/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004051 * This is one of
4052 * 'none' for arrays with no metadata (good luck...)
4053 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004054 * or N.M for internally known formats
4055 */
4056static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004057metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004058{
4059 if (mddev->persistent)
4060 return sprintf(page, "%d.%d\n",
4061 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004062 else if (mddev->external)
4063 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004064 else
4065 return sprintf(page, "none\n");
4066}
4067
4068static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004069metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004070{
4071 int major, minor;
4072 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004073 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004074 /* Changing the details of 'external' metadata is
4075 * always permitted. Otherwise there must be
4076 * no devices attached to the array.
4077 */
NeilBrown67918752014-12-15 12:57:01 +11004078
4079 err = mddev_lock(mddev);
4080 if (err)
4081 return err;
4082 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004083 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4084 ;
4085 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004086 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004087
NeilBrown67918752014-12-15 12:57:01 +11004088 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004089 if (cmd_match(buf, "none")) {
4090 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004091 mddev->external = 0;
4092 mddev->major_version = 0;
4093 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004094 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004095 }
4096 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004097 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004098 if (namelen >= sizeof(mddev->metadata_type))
4099 namelen = sizeof(mddev->metadata_type)-1;
4100 strncpy(mddev->metadata_type, buf+9, namelen);
4101 mddev->metadata_type[namelen] = 0;
4102 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4103 mddev->metadata_type[--namelen] = 0;
4104 mddev->persistent = 0;
4105 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004106 mddev->major_version = 0;
4107 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004108 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004109 }
4110 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004111 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004112 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004113 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004114 buf = e+1;
4115 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004116 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004117 goto out_unlock;
4118 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004119 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004120 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004121 mddev->major_version = major;
4122 mddev->minor_version = minor;
4123 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004124 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004125 err = 0;
4126out_unlock:
4127 mddev_unlock(mddev);
4128 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004129}
4130
4131static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004132__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004133
NeilBrowna35b0d62006-01-06 00:20:49 -08004134static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004135action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004136{
NeilBrown7eec3142005-11-08 21:39:44 -08004137 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004138 unsigned long recovery = mddev->recovery;
4139 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004140 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004141 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4142 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4143 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004144 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004145 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4146 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004147 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004148 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004149 type = "check";
4150 else
4151 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004152 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004153 type = "recover";
4154 }
4155 return sprintf(page, "%s\n", type);
4156}
4157
4158static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004159action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004160{
NeilBrown7eec3142005-11-08 21:39:44 -08004161 if (!mddev->pers || !mddev->pers->sync_request)
4162 return -EINVAL;
4163
NeilBrownb6a9ce62009-05-26 09:41:17 +10004164 if (cmd_match(page, "frozen"))
4165 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4166 else
4167 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4168
4169 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrownf851b602014-12-11 10:02:10 +11004170 flush_workqueue(md_misc_wq);
NeilBrown7eec3142005-11-08 21:39:44 -08004171 if (mddev->sync_thread) {
4172 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004173 if (mddev_lock(mddev) == 0) {
4174 md_reap_sync_thread(mddev);
4175 mddev_unlock(mddev);
4176 }
NeilBrown7eec3142005-11-08 21:39:44 -08004177 }
NeilBrown03c902e2006-01-06 00:20:46 -08004178 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4179 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004180 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004181 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004182 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004183 else if (cmd_match(page, "recover")) {
4184 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4185 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4186 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004187 int err;
4188 if (mddev->pers->start_reshape == NULL)
4189 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004190 err = mddev_lock(mddev);
4191 if (!err) {
4192 err = mddev->pers->start_reshape(mddev);
4193 mddev_unlock(mddev);
4194 }
NeilBrown16484bf2006-03-27 01:18:13 -08004195 if (err)
4196 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004197 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004198 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004199 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004200 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004201 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004202 return -EINVAL;
4203 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4204 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004205 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004206 if (mddev->ro == 2) {
4207 /* A write to sync_action is enough to justify
4208 * canceling read-auto mode
4209 */
4210 mddev->ro = 0;
4211 md_wakeup_thread(mddev->sync_thread);
4212 }
NeilBrown03c902e2006-01-06 00:20:46 -08004213 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004214 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004215 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004216 return len;
4217}
4218
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004219static struct md_sysfs_entry md_scan_mode =
4220__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4221
4222static ssize_t
4223last_sync_action_show(struct mddev *mddev, char *page)
4224{
4225 return sprintf(page, "%s\n", mddev->last_sync_action);
4226}
4227
4228static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4229
NeilBrown9d888832005-11-08 21:39:26 -08004230static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004231mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004232{
4233 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004234 (unsigned long long)
4235 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004236}
4237
NeilBrown80ca3a42006-07-10 04:44:18 -07004238static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004239
NeilBrown88202a02006-01-06 00:21:36 -08004240static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004241sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004242{
4243 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4244 mddev->sync_speed_min ? "local": "system");
4245}
4246
4247static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004248sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004249{
4250 int min;
4251 char *e;
4252 if (strncmp(buf, "system", 6)==0) {
4253 mddev->sync_speed_min = 0;
4254 return len;
4255 }
4256 min = simple_strtoul(buf, &e, 10);
4257 if (buf == e || (*e && *e != '\n') || min <= 0)
4258 return -EINVAL;
4259 mddev->sync_speed_min = min;
4260 return len;
4261}
4262
4263static struct md_sysfs_entry md_sync_min =
4264__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4265
4266static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004267sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004268{
4269 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4270 mddev->sync_speed_max ? "local": "system");
4271}
4272
4273static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004274sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004275{
4276 int max;
4277 char *e;
4278 if (strncmp(buf, "system", 6)==0) {
4279 mddev->sync_speed_max = 0;
4280 return len;
4281 }
4282 max = simple_strtoul(buf, &e, 10);
4283 if (buf == e || (*e && *e != '\n') || max <= 0)
4284 return -EINVAL;
4285 mddev->sync_speed_max = max;
4286 return len;
4287}
4288
4289static struct md_sysfs_entry md_sync_max =
4290__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4291
Iustin Popd7f3d292007-10-16 23:30:54 -07004292static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004293degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004294{
4295 return sprintf(page, "%d\n", mddev->degraded);
4296}
4297static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004298
4299static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004300sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004301{
4302 return sprintf(page, "%d\n", mddev->parallel_resync);
4303}
4304
4305static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004306sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004307{
4308 long n;
4309
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004310 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004311 return -EINVAL;
4312
4313 if (n != 0 && n != 1)
4314 return -EINVAL;
4315
4316 mddev->parallel_resync = n;
4317
4318 if (mddev->sync_thread)
4319 wake_up(&resync_wait);
4320
4321 return len;
4322}
4323
4324/* force parallel resync, even with shared block devices */
4325static struct md_sysfs_entry md_sync_force_parallel =
4326__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4327 sync_force_parallel_show, sync_force_parallel_store);
4328
4329static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004330sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004331{
4332 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004333 if (mddev->curr_resync == 0)
4334 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004335 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4336 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004337 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004338 db = resync - mddev->resync_mark_cnt;
4339 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004340}
4341
NeilBrown80ca3a42006-07-10 04:44:18 -07004342static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004343
4344static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004345sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004346{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004347 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004348
NeilBrownacb180b2009-04-14 16:28:34 +10004349 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4350 return sprintf(page, "none\n");
4351
NeilBrown72f36d52012-10-11 14:25:57 +11004352 if (mddev->curr_resync == 1 ||
4353 mddev->curr_resync == 2)
4354 return sprintf(page, "delayed\n");
4355
NeilBrownc804cde2012-05-21 09:28:33 +10004356 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4357 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004358 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004359 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004360 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004361
NeilBrownacb180b2009-04-14 16:28:34 +10004362 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004363 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004364}
4365
NeilBrown80ca3a42006-07-10 04:44:18 -07004366static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004367
NeilBrowne464eaf2006-03-27 01:18:14 -08004368static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004369min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004370{
4371 return sprintf(page, "%llu\n",
4372 (unsigned long long)mddev->resync_min);
4373}
4374static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004375min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004376{
4377 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004378 int err;
4379 int chunk;
4380
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004381 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004382 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004383
4384 spin_lock(&mddev->lock);
4385 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004386 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004387 goto out_unlock;
4388
4389 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004390 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004391 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004392
4393 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004394 chunk = mddev->chunk_sectors;
4395 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004396 sector_t temp = min;
NeilBrown23da4222014-12-15 12:57:01 +11004397
4398 err = -EINVAL;
4399 if (sector_div(temp, chunk))
4400 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004401 }
4402 mddev->resync_min = min;
NeilBrown23da4222014-12-15 12:57:01 +11004403 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004404
NeilBrown23da4222014-12-15 12:57:01 +11004405out_unlock:
4406 spin_unlock(&mddev->lock);
4407 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004408}
4409
4410static struct md_sysfs_entry md_min_sync =
4411__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4412
4413static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004414max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004415{
4416 if (mddev->resync_max == MaxSector)
4417 return sprintf(page, "max\n");
4418 else
4419 return sprintf(page, "%llu\n",
4420 (unsigned long long)mddev->resync_max);
4421}
4422static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004423max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004424{
NeilBrown23da4222014-12-15 12:57:01 +11004425 int err;
4426 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004427 if (strncmp(buf, "max", 3) == 0)
4428 mddev->resync_max = MaxSector;
4429 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004430 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004431 int chunk;
4432
4433 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004434 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004435 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004436 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004437 goto out_unlock;
4438
4439 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004440 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004441 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004442 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004443 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004444
4445 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004446 chunk = mddev->chunk_sectors;
4447 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004448 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004449
4450 err = -EINVAL;
4451 if (sector_div(temp, chunk))
4452 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004453 }
4454 mddev->resync_max = max;
4455 }
4456 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004457 err = 0;
4458out_unlock:
4459 spin_unlock(&mddev->lock);
4460 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004461}
4462
4463static struct md_sysfs_entry md_max_sync =
4464__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4465
4466static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004467suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004468{
4469 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4470}
4471
4472static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004473suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004474{
4475 char *e;
4476 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004477 unsigned long long old;
4478 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004479
NeilBrowne464eaf2006-03-27 01:18:14 -08004480 if (buf == e || (*e && *e != '\n'))
4481 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004482
NeilBrown67918752014-12-15 12:57:01 +11004483 err = mddev_lock(mddev);
4484 if (err)
4485 return err;
4486 err = -EINVAL;
4487 if (mddev->pers == NULL ||
4488 mddev->pers->quiesce == NULL)
4489 goto unlock;
4490 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004491 mddev->suspend_lo = new;
4492 if (new >= old)
4493 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004494 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004495 else {
4496 /* Expanding suspended region - need to wait */
4497 mddev->pers->quiesce(mddev, 1);
4498 mddev->pers->quiesce(mddev, 0);
4499 }
NeilBrown67918752014-12-15 12:57:01 +11004500 err = 0;
4501unlock:
4502 mddev_unlock(mddev);
4503 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004504}
4505static struct md_sysfs_entry md_suspend_lo =
4506__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4507
NeilBrowne464eaf2006-03-27 01:18:14 -08004508static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004509suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004510{
4511 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4512}
4513
4514static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004515suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004516{
4517 char *e;
4518 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004519 unsigned long long old;
4520 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004521
NeilBrowne464eaf2006-03-27 01:18:14 -08004522 if (buf == e || (*e && *e != '\n'))
4523 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004524
NeilBrown67918752014-12-15 12:57:01 +11004525 err = mddev_lock(mddev);
4526 if (err)
4527 return err;
4528 err = -EINVAL;
4529 if (mddev->pers == NULL ||
4530 mddev->pers->quiesce == NULL)
4531 goto unlock;
4532 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004533 mddev->suspend_hi = new;
4534 if (new <= old)
4535 /* Shrinking suspended region */
4536 mddev->pers->quiesce(mddev, 2);
4537 else {
4538 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004539 mddev->pers->quiesce(mddev, 1);
4540 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004541 }
NeilBrown67918752014-12-15 12:57:01 +11004542 err = 0;
4543unlock:
4544 mddev_unlock(mddev);
4545 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004546}
4547static struct md_sysfs_entry md_suspend_hi =
4548__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4549
NeilBrown08a02ec2007-05-09 02:35:38 -07004550static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004551reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004552{
4553 if (mddev->reshape_position != MaxSector)
4554 return sprintf(page, "%llu\n",
4555 (unsigned long long)mddev->reshape_position);
4556 strcpy(page, "none\n");
4557 return 5;
4558}
4559
4560static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004561reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004562{
NeilBrownc6563a82012-05-21 09:27:00 +10004563 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004564 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004565 int err;
NeilBrown08a02ec2007-05-09 02:35:38 -07004566 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004567
NeilBrown08a02ec2007-05-09 02:35:38 -07004568 if (buf == e || (*e && *e != '\n'))
4569 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004570 err = mddev_lock(mddev);
4571 if (err)
4572 return err;
4573 err = -EBUSY;
4574 if (mddev->pers)
4575 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004576 mddev->reshape_position = new;
4577 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004578 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004579 mddev->new_level = mddev->level;
4580 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004581 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004582 rdev_for_each(rdev, mddev)
4583 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004584 err = 0;
4585unlock:
4586 mddev_unlock(mddev);
4587 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004588}
4589
4590static struct md_sysfs_entry md_reshape_position =
4591__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4592 reshape_position_store);
4593
Dan Williamsb522adc2009-03-31 15:00:31 +11004594static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004595reshape_direction_show(struct mddev *mddev, char *page)
4596{
4597 return sprintf(page, "%s\n",
4598 mddev->reshape_backwards ? "backwards" : "forwards");
4599}
4600
4601static ssize_t
4602reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4603{
4604 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004605 int err;
4606
NeilBrown2c810cd2012-05-21 09:27:00 +10004607 if (cmd_match(buf, "forwards"))
4608 backwards = 0;
4609 else if (cmd_match(buf, "backwards"))
4610 backwards = 1;
4611 else
4612 return -EINVAL;
4613 if (mddev->reshape_backwards == backwards)
4614 return len;
4615
NeilBrown67918752014-12-15 12:57:01 +11004616 err = mddev_lock(mddev);
4617 if (err)
4618 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004619 /* check if we are allowed to change */
4620 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004621 err = -EBUSY;
4622 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004623 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004624 err = -EINVAL;
4625 else
4626 mddev->reshape_backwards = backwards;
4627 mddev_unlock(mddev);
4628 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004629}
4630
4631static struct md_sysfs_entry md_reshape_direction =
4632__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4633 reshape_direction_store);
4634
4635static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004636array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004637{
4638 if (mddev->external_size)
4639 return sprintf(page, "%llu\n",
4640 (unsigned long long)mddev->array_sectors/2);
4641 else
4642 return sprintf(page, "default\n");
4643}
4644
4645static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004646array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004647{
4648 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004649 int err;
4650
4651 err = mddev_lock(mddev);
4652 if (err)
4653 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004654
4655 if (strncmp(buf, "default", 7) == 0) {
4656 if (mddev->pers)
4657 sectors = mddev->pers->size(mddev, 0, 0);
4658 else
4659 sectors = mddev->array_sectors;
4660
4661 mddev->external_size = 0;
4662 } else {
4663 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004664 err = -EINVAL;
4665 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4666 err = -E2BIG;
4667 else
4668 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004669 }
4670
NeilBrown67918752014-12-15 12:57:01 +11004671 if (!err) {
4672 mddev->array_sectors = sectors;
4673 if (mddev->pers) {
4674 set_capacity(mddev->gendisk, mddev->array_sectors);
4675 revalidate_disk(mddev->gendisk);
4676 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004677 }
NeilBrown67918752014-12-15 12:57:01 +11004678 mddev_unlock(mddev);
4679 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004680}
4681
4682static struct md_sysfs_entry md_array_size =
4683__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4684 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004685
NeilBrowneae17012005-11-08 21:39:23 -08004686static struct attribute *md_default_attrs[] = {
4687 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004688 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004689 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004690 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004691 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004692 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004693 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004694 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004695 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004696 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004697 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004698 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004699 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004700 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004701 NULL,
4702};
4703
4704static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004705 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004706 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004707 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004708 &md_sync_min.attr,
4709 &md_sync_max.attr,
4710 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004711 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004712 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004713 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004714 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004715 &md_suspend_lo.attr,
4716 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004717 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004718 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004719 NULL,
4720};
NeilBrown411036f2005-11-08 21:39:40 -08004721static struct attribute_group md_redundancy_group = {
4722 .name = NULL,
4723 .attrs = md_redundancy_attrs,
4724};
4725
NeilBrowneae17012005-11-08 21:39:23 -08004726static ssize_t
4727md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4728{
4729 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004730 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004731 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004732
4733 if (!entry->show)
4734 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004735 spin_lock(&all_mddevs_lock);
4736 if (list_empty(&mddev->all_mddevs)) {
4737 spin_unlock(&all_mddevs_lock);
4738 return -EBUSY;
4739 }
4740 mddev_get(mddev);
4741 spin_unlock(&all_mddevs_lock);
4742
NeilBrownb7b17c92014-12-15 12:56:59 +11004743 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004744 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004745 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004746}
4747
4748static ssize_t
4749md_attr_store(struct kobject *kobj, struct attribute *attr,
4750 const char *page, size_t length)
4751{
4752 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004753 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004754 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004755
4756 if (!entry->store)
4757 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004758 if (!capable(CAP_SYS_ADMIN))
4759 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004760 spin_lock(&all_mddevs_lock);
4761 if (list_empty(&mddev->all_mddevs)) {
4762 spin_unlock(&all_mddevs_lock);
4763 return -EBUSY;
4764 }
4765 mddev_get(mddev);
4766 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004767 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004768 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004769 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004770}
4771
4772static void md_free(struct kobject *ko)
4773{
NeilBrownfd01b882011-10-11 16:47:53 +11004774 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004775
4776 if (mddev->sysfs_state)
4777 sysfs_put(mddev->sysfs_state);
4778
4779 if (mddev->gendisk) {
4780 del_gendisk(mddev->gendisk);
4781 put_disk(mddev->gendisk);
4782 }
4783 if (mddev->queue)
4784 blk_cleanup_queue(mddev->queue);
4785
NeilBrowneae17012005-11-08 21:39:23 -08004786 kfree(mddev);
4787}
4788
Emese Revfy52cf25d2010-01-19 02:58:23 +01004789static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004790 .show = md_attr_show,
4791 .store = md_attr_store,
4792};
4793static struct kobj_type md_ktype = {
4794 .release = md_free,
4795 .sysfs_ops = &md_sysfs_ops,
4796 .default_attrs = md_default_attrs,
4797};
4798
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799int mdp_major = 0;
4800
Dan Williams5fd3a172009-03-04 00:57:25 -07004801static void mddev_delayed_delete(struct work_struct *ws)
4802{
NeilBrownfd01b882011-10-11 16:47:53 +11004803 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004804
NeilBrown43a70502009-12-14 12:49:55 +11004805 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004806 kobject_del(&mddev->kobj);
4807 kobject_put(&mddev->kobj);
4808}
4809
NeilBrownefeb53c2009-01-09 08:31:10 +11004810static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004812 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004813 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004815 int partitioned;
4816 int shift;
4817 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004818 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
4820 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004821 return -ENODEV;
4822
4823 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4824 shift = partitioned ? MdpMinorShift : 0;
4825 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
Tejun Heoe804ac72010-10-15 15:36:08 +02004827 /* wait for any previous instance of this device to be
4828 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004829 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004830 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004831
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004832 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004833 error = -EEXIST;
4834 if (mddev->gendisk)
4835 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004836
4837 if (name) {
4838 /* Need to ensure that 'name' is not a duplicate.
4839 */
NeilBrownfd01b882011-10-11 16:47:53 +11004840 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004841 spin_lock(&all_mddevs_lock);
4842
4843 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4844 if (mddev2->gendisk &&
4845 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4846 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004847 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004848 }
4849 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 }
NeilBrown8b765392009-01-09 08:31:08 +11004851
NeilBrown0909dc42009-07-01 12:27:21 +10004852 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004853 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004854 if (!mddev->queue)
4855 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004856 mddev->queue->queuedata = mddev;
4857
NeilBrown409c57f2009-03-31 14:39:39 +11004858 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004859 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004860
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 disk = alloc_disk(1 << shift);
4862 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004863 blk_cleanup_queue(mddev->queue);
4864 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004865 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004867 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004869 if (name)
4870 strcpy(disk->disk_name, name);
4871 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004873 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 disk->fops = &md_fops;
4876 disk->private_data = mddev;
4877 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004878 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004879 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004880 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004881 * remove it now.
4882 */
4883 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004885 /* As soon as we call add_disk(), another thread could get
4886 * through to md_open, so make sure it doesn't get too far
4887 */
4888 mutex_lock(&mddev->open_mutex);
4889 add_disk(disk);
4890
Tejun Heoed9e1982008-08-25 19:56:05 +09004891 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4892 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004893 if (error) {
4894 /* This isn't possible, but as kobject_init_and_add is marked
4895 * __must_check, we must do something with the result
4896 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004897 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4898 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004899 error = 0;
4900 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004901 if (mddev->kobj.sd &&
4902 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004903 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004904 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004905 abort:
4906 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004907 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004908 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004909 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004910 }
NeilBrownd3374822009-01-09 08:31:10 +11004911 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004912 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004913}
4914
4915static struct kobject *md_probe(dev_t dev, int *part, void *data)
4916{
4917 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 return NULL;
4919}
4920
NeilBrownefeb53c2009-01-09 08:31:10 +11004921static int add_named_array(const char *val, struct kernel_param *kp)
4922{
4923 /* val must be "md_*" where * is not all digits.
4924 * We allocate an array with a large free minor number, and
4925 * set the name to val. val must not already be an active name.
4926 */
4927 int len = strlen(val);
4928 char buf[DISK_NAME_LEN];
4929
4930 while (len && val[len-1] == '\n')
4931 len--;
4932 if (len >= DISK_NAME_LEN)
4933 return -E2BIG;
4934 strlcpy(buf, val, len+1);
4935 if (strncmp(buf, "md_", 3) != 0)
4936 return -EINVAL;
4937 return md_alloc(0, buf);
4938}
4939
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940static void md_safemode_timeout(unsigned long data)
4941{
NeilBrownfd01b882011-10-11 16:47:53 +11004942 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
Neil Brown0fd62b82008-06-28 08:31:36 +10004944 if (!atomic_read(&mddev->writes_pending)) {
4945 mddev->safemode = 1;
4946 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004947 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 md_wakeup_thread(mddev->thread);
4950}
4951
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004952static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953
NeilBrownfd01b882011-10-11 16:47:53 +11004954int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955{
NeilBrown2604b702006-01-06 00:20:36 -08004956 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004957 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004958 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
NeilBrowna757e642005-04-16 15:26:42 -07004960 if (list_empty(&mddev->disks))
4961 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963
4964 if (mddev->pers)
4965 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004966 /* Cannot run until previous stop completes properly */
4967 if (mddev->sysfs_active)
4968 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004969
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 /*
4971 * Analyze all RAID superblock(s)
4972 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004973 if (!mddev->raid_disks) {
4974 if (!mddev->persistent)
4975 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004976 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
NeilBrownd9d166c2006-01-06 00:20:51 -08004979 if (mddev->level != LEVEL_NONE)
4980 request_module("md-level-%d", mddev->level);
4981 else if (mddev->clevel[0])
4982 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
4984 /*
4985 * Drop all container device buffers, from now on
4986 * the only valid external interface is through the md
4987 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004989 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004990 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 continue;
4992 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004993 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004994
4995 /* perform some consistency tests on the device.
4996 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004997 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004998 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004999 if (rdev->meta_bdev) {
5000 /* Nothing to check */;
5001 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005002 if (mddev->dev_sectors &&
5003 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005004 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005005 printk("md: %s: data overlaps metadata\n",
5006 mdname(mddev));
5007 return -EINVAL;
5008 }
5009 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005010 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005011 > rdev->data_offset) {
5012 printk("md: %s: metadata overlaps data\n",
5013 mdname(mddev));
5014 return -EINVAL;
5015 }
5016 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005017 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 }
5019
NeilBrowna167f662010-10-26 18:31:13 +11005020 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005021 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005022
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005024 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005025 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005027 if (mddev->level != LEVEL_NONE)
5028 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5029 mddev->level);
5030 else
5031 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5032 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 return -EINVAL;
5034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005036 if (mddev->level != pers->level) {
5037 mddev->level = pers->level;
5038 mddev->new_level = pers->level;
5039 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005040 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
NeilBrownf6705572006-03-27 01:18:11 -08005042 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005043 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005044 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005045 module_put(pers->owner);
5046 return -EINVAL;
5047 }
5048
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005049 if (pers->sync_request) {
5050 /* Warn if this is a potentially silly
5051 * configuration.
5052 */
5053 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005054 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005055 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005056
NeilBrowndafb20f2012-03-19 12:46:39 +11005057 rdev_for_each(rdev, mddev)
5058 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005059 if (rdev < rdev2 &&
5060 rdev->bdev->bd_contains ==
5061 rdev2->bdev->bd_contains) {
5062 printk(KERN_WARNING
5063 "%s: WARNING: %s appears to be"
5064 " on the same physical disk as"
5065 " %s.\n",
5066 mdname(mddev),
5067 bdevname(rdev->bdev,b),
5068 bdevname(rdev2->bdev,b2));
5069 warned = 1;
5070 }
5071 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005072
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005073 if (warned)
5074 printk(KERN_WARNING
5075 "True protection against single-disk"
5076 " failure might be compromised.\n");
5077 }
5078
NeilBrown657390d2005-08-26 18:34:16 -07005079 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005080 /* may be over-ridden by personality */
5081 mddev->resync_max_sectors = mddev->dev_sectors;
5082
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005083 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
NeilBrown0f9552b52009-12-30 12:08:50 +11005085 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005086 mddev->ro = 2; /* read-only, but switch on first write */
5087
NeilBrown36d091f2014-12-15 12:56:58 +11005088 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005089 if (err)
5090 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005091 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005092 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5093 " but 'external_size' not in effect?\n", __func__);
5094 printk(KERN_ERR
5095 "md: invalid array_size %llu > default size %llu\n",
5096 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005097 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005098 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005099 }
NeilBrown36d091f2014-12-15 12:56:58 +11005100 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005101 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005102 struct bitmap *bitmap;
5103
5104 bitmap = bitmap_create(mddev, -1);
5105 if (IS_ERR(bitmap)) {
5106 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005107 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5108 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005109 } else
5110 mddev->bitmap = bitmap;
5111
NeilBrownb15c2e52006-01-06 00:20:16 -08005112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005114 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005115 pers->free(mddev, mddev->private);
5116 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005117 bitmap_destroy(mddev);
5118 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 }
NeilBrown5c675f82014-12-15 12:56:56 +11005120 if (mddev->queue) {
5121 mddev->queue->backing_dev_info.congested_data = mddev;
5122 mddev->queue->backing_dev_info.congested_fn = md_congested;
NeilBrown64590f42014-12-15 12:56:57 +11005123 blk_queue_merge_bvec(mddev->queue, md_mergeable_bvec);
NeilBrown5c675f82014-12-15 12:56:56 +11005124 }
NeilBrown36d091f2014-12-15 12:56:58 +11005125 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005126 if (mddev->kobj.sd &&
5127 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005128 printk(KERN_WARNING
5129 "md: cannot register extra attributes for %s\n",
5130 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005131 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005132 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005133 mddev->ro = 0;
5134
NeilBrownf72ffdd2014-09-30 14:23:59 +10005135 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005136 atomic_set(&mddev->max_corr_read_errors,
5137 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 mddev->safemode = 0;
5139 mddev->safemode_timer.function = md_safemode_timeout;
5140 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005141 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005143 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005144 spin_lock(&mddev->lock);
5145 mddev->pers = pers;
NeilBrown0ca69882011-01-14 09:14:33 +11005146 mddev->ready = 1;
NeilBrown36d091f2014-12-15 12:56:58 +11005147 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005148 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005149 if (rdev->raid_disk >= 0)
5150 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005151 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005152
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005154
NeilBrown7a0a5352013-08-27 16:28:23 +10005155 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07005156 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157
NeilBrownd7603b72006-01-06 00:20:30 -08005158 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005159 sysfs_notify_dirent_safe(mddev->sysfs_state);
5160 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005161 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 return 0;
5163}
NeilBrown390ee602010-06-01 19:37:27 +10005164EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
NeilBrownfd01b882011-10-11 16:47:53 +11005166static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005167{
5168 int err;
5169
5170 err = md_run(mddev);
5171 if (err)
5172 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005173 err = bitmap_load(mddev);
5174 if (err) {
5175 bitmap_destroy(mddev);
5176 goto out;
5177 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005178
5179 md_wakeup_thread(mddev->thread);
5180 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5181
NeilBrownfe60b012010-03-29 11:10:42 +11005182 set_capacity(mddev->gendisk, mddev->array_sectors);
5183 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005184 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005185 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5186out:
5187 return err;
5188}
5189
NeilBrownfd01b882011-10-11 16:47:53 +11005190static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191{
5192 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193
Andre Noll80fab1d2008-07-11 22:02:21 +10005194 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005196 return -ENXIO;
5197 if (!mddev->pers)
5198 return -EINVAL;
5199 if (!mddev->ro)
5200 return -EBUSY;
5201 mddev->safemode = 0;
5202 mddev->ro = 0;
5203 set_disk_ro(disk, 0);
5204 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5205 mdname(mddev));
5206 /* Kick recovery or resync if necessary */
5207 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5208 md_wakeup_thread(mddev->thread);
5209 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005210 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212}
5213
NeilBrownfd01b882011-10-11 16:47:53 +11005214static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005215{
5216 mddev->array_sectors = 0;
5217 mddev->external_size = 0;
5218 mddev->dev_sectors = 0;
5219 mddev->raid_disks = 0;
5220 mddev->recovery_cp = 0;
5221 mddev->resync_min = 0;
5222 mddev->resync_max = MaxSector;
5223 mddev->reshape_position = MaxSector;
5224 mddev->external = 0;
5225 mddev->persistent = 0;
5226 mddev->level = LEVEL_NONE;
5227 mddev->clevel[0] = 0;
5228 mddev->flags = 0;
5229 mddev->ro = 0;
5230 mddev->metadata_type[0] = 0;
5231 mddev->chunk_sectors = 0;
5232 mddev->ctime = mddev->utime = 0;
5233 mddev->layout = 0;
5234 mddev->max_disks = 0;
5235 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005236 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005237 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005238 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005239 mddev->new_level = LEVEL_NONE;
5240 mddev->new_layout = 0;
5241 mddev->new_chunk_sectors = 0;
5242 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005243 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005244 mddev->suspend_lo = mddev->suspend_hi = 0;
5245 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5246 mddev->recovery = 0;
5247 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005248 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005249 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005250 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005251 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005252 mddev->bitmap_info.offset = 0;
5253 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005254 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005255 mddev->bitmap_info.chunksize = 0;
5256 mddev->bitmap_info.daemon_sleep = 0;
5257 mddev->bitmap_info.max_write_behind = 0;
5258}
5259
NeilBrownfd01b882011-10-11 16:47:53 +11005260static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005261{
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005262 if (mddev_is_clustered(mddev))
5263 md_cluster_ops->metadata_update_start(mddev);
NeilBrown6b6204e2013-05-09 09:48:30 +10005264 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005265 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005266 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005267 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005268 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005269 }
5270
5271 del_timer_sync(&mddev->safemode_timer);
5272
5273 bitmap_flush(mddev);
5274 md_super_wait(mddev);
5275
NeilBrownb6d428c2013-04-24 11:42:42 +10005276 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005277 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005278 /* mark array as shutdown cleanly */
5279 mddev->in_sync = 1;
5280 md_update_sb(mddev, 1);
5281 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005282 if (mddev_is_clustered(mddev))
5283 md_cluster_ops->metadata_update_finish(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005284}
NeilBrowndefad612011-01-14 09:14:33 +11005285
NeilBrownfd01b882011-10-11 16:47:53 +11005286void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005287{
NeilBrown29f097c2013-11-14 17:54:51 +11005288 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005289 __md_stop_writes(mddev);
5290 mddev_unlock(mddev);
5291}
NeilBrown390ee602010-06-01 19:37:27 +10005292EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005293
NeilBrown5aa61f42014-12-15 12:56:57 +11005294static void mddev_detach(struct mddev *mddev)
5295{
5296 struct bitmap *bitmap = mddev->bitmap;
5297 /* wait for behind writes to complete */
5298 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5299 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5300 mdname(mddev));
5301 /* need to kick something here to make sure I/O goes? */
5302 wait_event(bitmap->behind_wait,
5303 atomic_read(&bitmap->behind_writes) == 0);
5304 }
NeilBrown36d091f2014-12-15 12:56:58 +11005305 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005306 mddev->pers->quiesce(mddev, 1);
5307 mddev->pers->quiesce(mddev, 0);
5308 }
5309 md_unregister_thread(&mddev->thread);
5310 if (mddev->queue)
5311 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5312}
5313
NeilBrown5eff3c42012-11-19 10:47:48 +11005314static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005315{
NeilBrown36d091f2014-12-15 12:56:58 +11005316 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005317 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005318 spin_lock(&mddev->lock);
5319 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005320 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005321 spin_unlock(&mddev->lock);
5322 pers->free(mddev, mddev->private);
5323 if (pers->sync_request && mddev->to_remove == NULL)
5324 mddev->to_remove = &md_redundancy_group;
5325 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005326 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005327}
NeilBrown5eff3c42012-11-19 10:47:48 +11005328
5329void md_stop(struct mddev *mddev)
5330{
5331 /* stop the array and free an attached data structures.
5332 * This is called from dm-raid
5333 */
5334 __md_stop(mddev);
5335 bitmap_destroy(mddev);
5336 if (mddev->bio_set)
5337 bioset_free(mddev->bio_set);
5338}
5339
NeilBrown390ee602010-06-01 19:37:27 +10005340EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005341
NeilBrowna05b7ea2012-07-19 15:59:18 +10005342static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d02010-03-29 13:23:10 +11005343{
5344 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005345 int did_freeze = 0;
5346
5347 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5348 did_freeze = 1;
5349 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5350 md_wakeup_thread(mddev->thread);
5351 }
NeilBrownf851b602014-12-11 10:02:10 +11005352 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005353 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005354 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005355 /* Thread might be blocked waiting for metadata update
5356 * which will now never happen */
5357 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005358
NeilBrown30b8feb2013-11-14 15:16:17 +11005359 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005360 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5361 &mddev->recovery));
NeilBrown30b8feb2013-11-14 15:16:17 +11005362 mddev_lock_nointr(mddev);
5363
NeilBrowna4bd82d02010-03-29 13:23:10 +11005364 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005365 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005366 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005367 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005368 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d02010-03-29 13:23:10 +11005369 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005370 if (did_freeze) {
5371 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005372 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005373 md_wakeup_thread(mddev->thread);
5374 }
NeilBrowna4bd82d02010-03-29 13:23:10 +11005375 err = -EBUSY;
5376 goto out;
5377 }
5378 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005379 __md_stop_writes(mddev);
NeilBrowna4bd82d02010-03-29 13:23:10 +11005380
5381 err = -ENXIO;
5382 if (mddev->ro==1)
5383 goto out;
5384 mddev->ro = 1;
5385 set_disk_ro(mddev->gendisk, 1);
5386 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005387 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5388 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005389 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005390 err = 0;
NeilBrowna4bd82d02010-03-29 13:23:10 +11005391 }
5392out:
5393 mutex_unlock(&mddev->open_mutex);
5394 return err;
5395}
5396
NeilBrown9e653b62006-06-26 00:27:58 -07005397/* mode:
5398 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005399 * 2 - stop but do not disassemble array
5400 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005401static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005402 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005405 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005406 int did_freeze = 0;
5407
5408 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5409 did_freeze = 1;
5410 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5411 md_wakeup_thread(mddev->thread);
5412 }
NeilBrownf851b602014-12-11 10:02:10 +11005413 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005414 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005415 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005416 /* Thread might be blocked waiting for metadata update
5417 * which will now never happen */
5418 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005419
NeilBrown30b8feb2013-11-14 15:16:17 +11005420 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005421 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5422 !test_bit(MD_RECOVERY_RUNNING,
5423 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005424 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
NeilBrownc8c00a62009-08-10 12:50:52 +10005426 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005427 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005428 mddev->sysfs_active ||
5429 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005430 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005431 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005432 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005433 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005434 if (did_freeze) {
5435 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005436 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005437 md_wakeup_thread(mddev->thread);
5438 }
NeilBrown260fa032013-08-27 16:44:13 +10005439 return -EBUSY;
5440 }
NeilBrown6e17b022010-08-07 21:41:19 +10005441 if (mddev->pers) {
NeilBrowna4bd82d02010-03-29 13:23:10 +11005442 if (mddev->ro)
5443 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005444
NeilBrowndefad612011-01-14 09:14:33 +11005445 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005446 __md_stop(mddev);
NeilBrowna4bd82d02010-03-29 13:23:10 +11005447 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d02010-03-29 13:23:10 +11005448 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005449
NeilBrowna4bd82d02010-03-29 13:23:10 +11005450 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005451 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005452
NeilBrowndafb20f2012-03-19 12:46:39 +11005453 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005454 if (rdev->raid_disk >= 0)
5455 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005456
NeilBrowna4bd82d02010-03-29 13:23:10 +11005457 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005458 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005459 mddev->changed = 1;
NeilBrowna4bd82d02010-03-29 13:23:10 +11005460 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005461
NeilBrowna4bd82d02010-03-29 13:23:10 +11005462 if (mddev->ro)
5463 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005464 } else
5465 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 /*
5467 * Free resources if final stop
5468 */
NeilBrown9e653b62006-06-26 00:27:58 -07005469 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5471
NeilBrown978f9462006-02-02 14:28:05 -08005472 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005473 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005474 struct file *f = mddev->bitmap_info.file;
5475 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005476 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005477 spin_unlock(&mddev->lock);
5478 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005479 }
NeilBrownc3d97142009-12-14 12:49:52 +11005480 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005481
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 export_array(mddev);
5483
NeilBrown6177b472010-03-29 11:37:13 +11005484 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005485 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005486 if (mddev->hold_active == UNTIL_STOP)
5487 mddev->hold_active = 0;
NeilBrowna4bd82d02010-03-29 13:23:10 +11005488 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005489 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005490 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005491 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493}
5494
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005495#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005496static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497{
NeilBrown3cb03002011-10-11 16:45:26 +11005498 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 int err;
5500
NeilBrowna757e642005-04-16 15:26:42 -07005501 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503
5504 printk(KERN_INFO "md: running: ");
5505
NeilBrowndafb20f2012-03-19 12:46:39 +11005506 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 char b[BDEVNAME_SIZE];
5508 printk("<%s>", bdevname(rdev->bdev,b));
5509 }
5510 printk("\n");
5511
NeilBrownd710e132008-10-13 11:55:12 +11005512 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 if (err) {
5514 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005515 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 }
5517}
5518
5519/*
5520 * lets try to run arrays based on all disks that have arrived
5521 * until now. (those are in pending_raid_disks)
5522 *
5523 * the method: pick the first pending disk, collect all disks with
5524 * the same UUID, remove all from the pending list and put them into
5525 * the 'same_array' list. Then order this list based on superblock
5526 * update time (freshest comes first), kick out 'old' disks and
5527 * compare superblocks. If everything's fine then run it.
5528 *
5529 * If "unit" is allocated, then bump its reference count
5530 */
5531static void autorun_devices(int part)
5532{
NeilBrown3cb03002011-10-11 16:45:26 +11005533 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005534 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 char b[BDEVNAME_SIZE];
5536
5537 printk(KERN_INFO "md: autorun ...\n");
5538 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005539 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005541 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005543 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544
5545 printk(KERN_INFO "md: considering %s ...\n",
5546 bdevname(rdev0->bdev,b));
5547 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005548 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 if (super_90_load(rdev, rdev0, 0) >= 0) {
5550 printk(KERN_INFO "md: adding %s ...\n",
5551 bdevname(rdev->bdev,b));
5552 list_move(&rdev->same_set, &candidates);
5553 }
5554 /*
5555 * now we have a set of devices, with all of them having
5556 * mostly sane superblocks. It's time to allocate the
5557 * mddev.
5558 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005559 if (part) {
5560 dev = MKDEV(mdp_major,
5561 rdev0->preferred_minor << MdpMinorShift);
5562 unit = MINOR(dev) >> MdpMinorShift;
5563 } else {
5564 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5565 unit = MINOR(dev);
5566 }
5567 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5569 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5570 break;
5571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
5573 md_probe(dev, NULL, NULL);
5574 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005575 if (!mddev || !mddev->gendisk) {
5576 if (mddev)
5577 mddev_put(mddev);
5578 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 "md: cannot allocate memory for md drive.\n");
5580 break;
5581 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005582 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 printk(KERN_WARNING "md: %s locked, cannot run\n",
5584 mdname(mddev));
5585 else if (mddev->raid_disks || mddev->major_version
5586 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005587 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 "md: %s already running, cannot run %s\n",
5589 mdname(mddev), bdevname(rdev0->bdev,b));
5590 mddev_unlock(mddev);
5591 } else {
5592 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005593 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005594 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 list_del_init(&rdev->same_set);
5596 if (bind_rdev_to_array(rdev, mddev))
5597 export_rdev(rdev);
5598 }
5599 autorun_array(mddev);
5600 mddev_unlock(mddev);
5601 }
5602 /* on success, candidates will be empty, on error
5603 * it won't...
5604 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005605 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005606 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 mddev_put(mddev);
5610 }
5611 printk(KERN_INFO "md: ... autorun DONE.\n");
5612}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005613#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
NeilBrownf72ffdd2014-09-30 14:23:59 +10005615static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616{
5617 mdu_version_t ver;
5618
5619 ver.major = MD_MAJOR_VERSION;
5620 ver.minor = MD_MINOR_VERSION;
5621 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5622
5623 if (copy_to_user(arg, &ver, sizeof(ver)))
5624 return -EFAULT;
5625
5626 return 0;
5627}
5628
NeilBrownf72ffdd2014-09-30 14:23:59 +10005629static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630{
5631 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005632 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005633 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634
NeilBrown1ca69c42012-10-11 13:37:33 +11005635 nr = working = insync = failed = spare = 0;
5636 rcu_read_lock();
5637 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005639 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 failed++;
5641 else {
5642 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005643 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005644 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 else
5646 spare++;
5647 }
5648 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005649 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650
5651 info.major_version = mddev->major_version;
5652 info.minor_version = mddev->minor_version;
5653 info.patch_version = MD_PATCHLEVEL_VERSION;
5654 info.ctime = mddev->ctime;
5655 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005656 info.size = mddev->dev_sectors / 2;
5657 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005658 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 info.nr_disks = nr;
5660 info.raid_disks = mddev->raid_disks;
5661 info.md_minor = mddev->md_minor;
5662 info.not_persistent= !mddev->persistent;
5663
5664 info.utime = mddev->utime;
5665 info.state = 0;
5666 if (mddev->in_sync)
5667 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005668 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005669 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005670 if (mddev_is_clustered(mddev))
5671 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005672 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 info.working_disks = working;
5674 info.failed_disks = failed;
5675 info.spare_disks = spare;
5676
5677 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005678 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679
5680 if (copy_to_user(arg, &info, sizeof(info)))
5681 return -EFAULT;
5682
5683 return 0;
5684}
5685
NeilBrownf72ffdd2014-09-30 14:23:59 +10005686static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005687{
5688 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005689 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005690 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005691
NeilBrown60559da2013-07-16 16:44:29 +10005692 file = kmalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005693 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005694 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005695
NeilBrown32a76272005-06-21 17:17:14 -07005696 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005697 spin_lock(&mddev->lock);
5698 /* bitmap disabled, zero the first byte and copy out */
5699 if (!mddev->bitmap_info.file)
5700 file->pathname[0] = '\0';
5701 else if ((ptr = d_path(&mddev->bitmap_info.file->f_path,
5702 file->pathname, sizeof(file->pathname))),
5703 IS_ERR(ptr))
5704 err = PTR_ERR(ptr);
5705 else
5706 memmove(file->pathname, ptr,
5707 sizeof(file->pathname)-(ptr-file->pathname));
5708 spin_unlock(&mddev->lock);
5709
5710 if (err == 0 &&
5711 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005712 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005713
NeilBrown32a76272005-06-21 17:17:14 -07005714 kfree(file);
5715 return err;
5716}
5717
NeilBrownf72ffdd2014-09-30 14:23:59 +10005718static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719{
5720 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005721 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
5723 if (copy_from_user(&info, arg, sizeof(info)))
5724 return -EFAULT;
5725
NeilBrown1ca69c42012-10-11 13:37:33 +11005726 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005727 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 if (rdev) {
5729 info.major = MAJOR(rdev->bdev->bd_dev);
5730 info.minor = MINOR(rdev->bdev->bd_dev);
5731 info.raid_disk = rdev->raid_disk;
5732 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005733 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005735 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 info.state |= (1<<MD_DISK_ACTIVE);
5737 info.state |= (1<<MD_DISK_SYNC);
5738 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005739 if (test_bit(WriteMostly, &rdev->flags))
5740 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 } else {
5742 info.major = info.minor = 0;
5743 info.raid_disk = -1;
5744 info.state = (1<<MD_DISK_REMOVED);
5745 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005746 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
5748 if (copy_to_user(arg, &info, sizeof(info)))
5749 return -EFAULT;
5750
5751 return 0;
5752}
5753
NeilBrownf72ffdd2014-09-30 14:23:59 +10005754static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755{
5756 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005757 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 dev_t dev = MKDEV(info->major,info->minor);
5759
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005760 if (mddev_is_clustered(mddev) &&
5761 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005762 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005763 mdname(mddev));
5764 return -EINVAL;
5765 }
5766
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5768 return -EOVERFLOW;
5769
5770 if (!mddev->raid_disks) {
5771 int err;
5772 /* expecting a device which has a superblock */
5773 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5774 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005775 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 "md: md_import_device returned %ld\n",
5777 PTR_ERR(rdev));
5778 return PTR_ERR(rdev);
5779 }
5780 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005781 struct md_rdev *rdev0
5782 = list_entry(mddev->disks.next,
5783 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005784 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 .load_super(rdev, rdev0, mddev->minor_version);
5786 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005787 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005789 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 bdevname(rdev0->bdev,b2));
5791 export_rdev(rdev);
5792 return -EINVAL;
5793 }
5794 }
5795 err = bind_rdev_to_array(rdev, mddev);
5796 if (err)
5797 export_rdev(rdev);
5798 return err;
5799 }
5800
5801 /*
5802 * add_new_disk can be used once the array is assembled
5803 * to add "hot spares". They must already have a superblock
5804 * written
5805 */
5806 if (mddev->pers) {
5807 int err;
5808 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005809 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 "%s: personality does not support diskops!\n",
5811 mdname(mddev));
5812 return -EINVAL;
5813 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005814 if (mddev->persistent)
5815 rdev = md_import_device(dev, mddev->major_version,
5816 mddev->minor_version);
5817 else
5818 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005820 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 "md: md_import_device returned %ld\n",
5822 PTR_ERR(rdev));
5823 return PTR_ERR(rdev);
5824 }
NeilBrown1a855a02010-12-09 16:36:28 +11005825 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005826 if (!mddev->persistent) {
5827 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005828 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005829 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005830 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005831 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005832 } else
NeilBrown41158c72005-06-21 17:17:25 -07005833 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005834 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005835 } else
5836 super_types[mddev->major_version].
5837 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005838 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005839 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005840 /* This was a hot-add request, but events doesn't
5841 * match, so reject it.
5842 */
5843 export_rdev(rdev);
5844 return -EINVAL;
5845 }
5846
NeilBrownb2d444d2005-11-08 21:39:31 -08005847 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005848 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5849 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005850 else
5851 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005852
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005853 /*
5854 * check whether the device shows up in other nodes
5855 */
5856 if (mddev_is_clustered(mddev)) {
5857 if (info->state & (1 << MD_DISK_CANDIDATE)) {
5858 /* Through --cluster-confirm */
5859 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005860 err = md_cluster_ops->new_disk_ack(mddev, true);
5861 if (err) {
5862 export_rdev(rdev);
5863 return err;
5864 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005865 } else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
5866 /* --add initiated by this node */
5867 err = md_cluster_ops->add_new_disk_start(mddev, rdev);
5868 if (err) {
5869 md_cluster_ops->add_new_disk_finish(mddev);
5870 export_rdev(rdev);
5871 return err;
5872 }
5873 }
5874 }
5875
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 rdev->raid_disk = -1;
5877 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005878 if (!err && !mddev->pers->hot_remove_disk) {
5879 /* If there is hot_add_disk but no hot_remove_disk
5880 * then added disks for geometry changes,
5881 * and should be added immediately.
5882 */
5883 super_types[mddev->major_version].
5884 validate_super(mddev, rdev);
5885 err = mddev->pers->hot_add_disk(mddev, rdev);
5886 if (err)
5887 unbind_rdev_from_array(rdev);
5888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 if (err)
5890 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005891 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005892 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005893
NeilBrown7ceb17e2013-04-24 11:42:42 +10005894 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005895 if (mddev->degraded)
5896 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005897 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005898 if (!err)
5899 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005900 md_wakeup_thread(mddev->thread);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005901 if (mddev_is_clustered(mddev) &&
5902 (info->state & (1 << MD_DISK_CLUSTER_ADD)))
5903 md_cluster_ops->add_new_disk_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 return err;
5905 }
5906
5907 /* otherwise, add_new_disk is only allowed
5908 * for major_version==0 superblocks
5909 */
5910 if (mddev->major_version != 0) {
5911 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5912 mdname(mddev));
5913 return -EINVAL;
5914 }
5915
5916 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5917 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005918 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005920 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 "md: error, md_import_device() returned %ld\n",
5922 PTR_ERR(rdev));
5923 return PTR_ERR(rdev);
5924 }
5925 rdev->desc_nr = info->number;
5926 if (info->raid_disk < mddev->raid_disks)
5927 rdev->raid_disk = info->raid_disk;
5928 else
5929 rdev->raid_disk = -1;
5930
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005932 if (info->state & (1<<MD_DISK_SYNC))
5933 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005935 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5936 set_bit(WriteMostly, &rdev->flags);
5937
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 if (!mddev->persistent) {
5939 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005940 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5941 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005942 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005943 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944
NeilBrown2bf071b2006-01-06 00:20:55 -08005945 err = bind_rdev_to_array(rdev, mddev);
5946 if (err) {
5947 export_rdev(rdev);
5948 return err;
5949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 }
5951
5952 return 0;
5953}
5954
NeilBrownf72ffdd2014-09-30 14:23:59 +10005955static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956{
5957 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005958 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 rdev = find_rdev(mddev, dev);
5961 if (!rdev)
5962 return -ENXIO;
5963
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005964 if (mddev_is_clustered(mddev))
5965 md_cluster_ops->metadata_update_start(mddev);
5966
NeilBrown3ea8929d2013-04-24 11:42:41 +10005967 clear_bit(Blocked, &rdev->flags);
5968 remove_and_add_spares(mddev, rdev);
5969
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 if (rdev->raid_disk >= 0)
5971 goto busy;
5972
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05005973 if (mddev_is_clustered(mddev))
5974 md_cluster_ops->remove_disk(mddev, rdev);
5975
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05005976 md_kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005977 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005978 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005980 if (mddev_is_clustered(mddev))
5981 md_cluster_ops->metadata_update_finish(mddev);
5982
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 return 0;
5984busy:
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005985 if (mddev_is_clustered(mddev))
5986 md_cluster_ops->metadata_update_cancel(mddev);
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005987 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 bdevname(rdev->bdev,b), mdname(mddev));
5989 return -EBUSY;
5990}
5991
NeilBrownf72ffdd2014-09-30 14:23:59 +10005992static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993{
5994 char b[BDEVNAME_SIZE];
5995 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005996 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997
5998 if (!mddev->pers)
5999 return -ENODEV;
6000
6001 if (mddev->major_version != 0) {
6002 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6003 " version-0 superblocks.\n",
6004 mdname(mddev));
6005 return -EINVAL;
6006 }
6007 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006008 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 "%s: personality does not support diskops!\n",
6010 mdname(mddev));
6011 return -EINVAL;
6012 }
6013
NeilBrownd710e132008-10-13 11:55:12 +11006014 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006016 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 "md: error, md_import_device() returned %ld\n",
6018 PTR_ERR(rdev));
6019 return -EINVAL;
6020 }
6021
6022 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006023 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006025 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026
NeilBrown8190e752009-06-18 08:48:58 +10006027 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028
NeilBrownb2d444d2005-11-08 21:39:31 -08006029 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006030 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 "md: can not hot-add faulty %s disk to %s!\n",
6032 bdevname(rdev->bdev,b), mdname(mddev));
6033 err = -EINVAL;
6034 goto abort_export;
6035 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006036
6037 if (mddev_is_clustered(mddev))
6038 md_cluster_ops->metadata_update_start(mddev);
NeilBrownb2d444d2005-11-08 21:39:31 -08006039 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006041 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006042 err = bind_rdev_to_array(rdev, mddev);
6043 if (err)
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006044 goto abort_clustered;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045
6046 /*
6047 * The rest should better be atomic, we can have disk failures
6048 * noticed in interrupt contexts ...
6049 */
6050
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 rdev->raid_disk = -1;
6052
NeilBrown850b2b42006-10-03 01:15:46 -07006053 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006055 if (mddev_is_clustered(mddev))
6056 md_cluster_ops->metadata_update_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 /*
6058 * Kick recovery, maybe this spare has to be added to the
6059 * array immediately.
6060 */
6061 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6062 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006063 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 return 0;
6065
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006066abort_clustered:
6067 if (mddev_is_clustered(mddev))
6068 md_cluster_ops->metadata_update_cancel(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069abort_export:
6070 export_rdev(rdev);
6071 return err;
6072}
6073
NeilBrownfd01b882011-10-11 16:47:53 +11006074static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006075{
NeilBrown035328c2014-04-09 12:25:40 +10006076 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006077
NeilBrown36fa3062005-09-09 16:23:45 -07006078 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006079 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006080 return -EBUSY;
6081 if (mddev->recovery || mddev->sync_thread)
6082 return -EBUSY;
6083 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006084 }
6085
NeilBrown36fa3062005-09-09 16:23:45 -07006086 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006087 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006088 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006089
NeilBrown1e594bb2014-12-15 12:57:00 +11006090 if (mddev->bitmap || mddev->bitmap_info.file)
6091 return -EEXIST; /* cannot add when bitmap is present */
6092 f = fget(fd);
6093
6094 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006095 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6096 mdname(mddev));
6097 return -EBADF;
6098 }
6099
NeilBrown1e594bb2014-12-15 12:57:00 +11006100 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006101 if (!S_ISREG(inode->i_mode)) {
6102 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6103 mdname(mddev));
6104 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006105 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006106 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6107 mdname(mddev));
6108 err = -EBADF;
6109 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006110 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6111 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006112 err = -EBUSY;
6113 }
6114 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006115 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006116 return err;
6117 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006118 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006119 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006120 } else if (mddev->bitmap == NULL)
6121 return -ENOENT; /* cannot remove what isn't there */
6122 err = 0;
6123 if (mddev->pers) {
6124 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006125 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006126 struct bitmap *bitmap;
6127
6128 bitmap = bitmap_create(mddev, -1);
6129 if (!IS_ERR(bitmap)) {
6130 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006131 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006132 } else
6133 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006134 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006135 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006136 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006137 fd = -1; /* make sure to put the file */
6138 }
NeilBrown36fa3062005-09-09 16:23:45 -07006139 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006140 }
6141 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006142 struct file *f = mddev->bitmap_info.file;
6143 if (f) {
6144 spin_lock(&mddev->lock);
6145 mddev->bitmap_info.file = NULL;
6146 spin_unlock(&mddev->lock);
6147 fput(f);
6148 }
NeilBrown36fa3062005-09-09 16:23:45 -07006149 }
6150
NeilBrown32a76272005-06-21 17:17:14 -07006151 return err;
6152}
6153
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154/*
6155 * set_array_info is used two different ways
6156 * The original usage is when creating a new array.
6157 * In this usage, raid_disks is > 0 and it together with
6158 * level, size, not_persistent,layout,chunksize determine the
6159 * shape of the array.
6160 * This will always create an array with a type-0.90.0 superblock.
6161 * The newer usage is when assembling an array.
6162 * In this case raid_disks will be 0, and the major_version field is
6163 * use to determine which style super-blocks are to be found on the devices.
6164 * The minor and patch _version numbers are also kept incase the
6165 * super_block handler wishes to interpret them.
6166 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006167static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168{
6169
6170 if (info->raid_disks == 0) {
6171 /* just setting version number for superblock loading */
6172 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006173 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 super_types[info->major_version].name == NULL) {
6175 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006176 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 "md: superblock version %d not known\n",
6178 info->major_version);
6179 return -EINVAL;
6180 }
6181 mddev->major_version = info->major_version;
6182 mddev->minor_version = info->minor_version;
6183 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006184 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006185 /* ensure mddev_put doesn't delete this now that there
6186 * is some minimal configuration.
6187 */
6188 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 return 0;
6190 }
6191 mddev->major_version = MD_MAJOR_VERSION;
6192 mddev->minor_version = MD_MINOR_VERSION;
6193 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6194 mddev->ctime = get_seconds();
6195
6196 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006197 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006198 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 mddev->raid_disks = info->raid_disks;
6200 /* don't set md_minor, it is determined by which /dev/md* was
6201 * openned
6202 */
6203 if (info->state & (1<<MD_SB_CLEAN))
6204 mddev->recovery_cp = MaxSector;
6205 else
6206 mddev->recovery_cp = 0;
6207 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006208 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209
6210 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006211 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212
6213 mddev->max_disks = MD_SB_DISKS;
6214
NeilBrowne6910632008-02-06 01:39:51 -08006215 if (mddev->persistent)
6216 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006217 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
NeilBrownc3d97142009-12-14 12:49:52 +11006219 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006220 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006221 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006222
NeilBrownf6705572006-03-27 01:18:11 -08006223 mddev->reshape_position = MaxSector;
6224
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 /*
6226 * Generate a 128 bit UUID
6227 */
6228 get_random_bytes(mddev->uuid, 16);
6229
NeilBrownf6705572006-03-27 01:18:11 -08006230 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006231 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006232 mddev->new_layout = mddev->layout;
6233 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006234 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006235
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236 return 0;
6237}
6238
NeilBrownfd01b882011-10-11 16:47:53 +11006239void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006240{
Dan Williamsb522adc2009-03-31 15:00:31 +11006241 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6242
6243 if (mddev->external_size)
6244 return;
6245
Dan Williams1f403622009-03-31 14:59:03 +11006246 mddev->array_sectors = array_sectors;
6247}
6248EXPORT_SYMBOL(md_set_array_sectors);
6249
NeilBrownfd01b882011-10-11 16:47:53 +11006250static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006251{
NeilBrown3cb03002011-10-11 16:45:26 +11006252 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006253 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006254 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006255
6256 if (mddev->pers->resize == NULL)
6257 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006258 /* The "num_sectors" is the number of sectors of each device that
6259 * is used. This can only make sense for arrays with redundancy.
6260 * linear and raid0 always use whatever space is available. We can only
6261 * consider changing this number if no resync or reconstruction is
6262 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006263 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006264 * of each device. If num_sectors is zero, we find the largest size
6265 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006266 */
NeilBrownf851b602014-12-11 10:02:10 +11006267 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6268 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006269 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006270 if (mddev->ro)
6271 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006272
NeilBrowndafb20f2012-03-19 12:46:39 +11006273 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006274 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006275
Andre Nolld71f9f82008-07-11 22:02:22 +10006276 if (fit && (num_sectors == 0 || num_sectors > avail))
6277 num_sectors = avail;
6278 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006279 return -ENOSPC;
6280 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006281 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006282 if (!rv)
6283 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006284 return rv;
6285}
6286
NeilBrownfd01b882011-10-11 16:47:53 +11006287static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006288{
6289 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006290 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006291 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006292 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006293 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006294 if (mddev->ro)
6295 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006296 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006297 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006298 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006299 if (mddev->sync_thread ||
6300 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6301 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006302 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006303
6304 rdev_for_each(rdev, mddev) {
6305 if (mddev->raid_disks < raid_disks &&
6306 rdev->data_offset < rdev->new_data_offset)
6307 return -EINVAL;
6308 if (mddev->raid_disks > raid_disks &&
6309 rdev->data_offset > rdev->new_data_offset)
6310 return -EINVAL;
6311 }
6312
NeilBrown63c70c42006-03-27 01:18:13 -08006313 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006314 if (mddev->delta_disks < 0)
6315 mddev->reshape_backwards = 1;
6316 else if (mddev->delta_disks > 0)
6317 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006318
6319 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006320 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006321 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006322 mddev->reshape_backwards = 0;
6323 }
NeilBrownda943b992006-01-06 00:20:54 -08006324 return rv;
6325}
6326
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327/*
6328 * update_array_info is used to change the configuration of an
6329 * on-line array.
6330 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6331 * fields in the info are checked against the array.
6332 * Any differences that cannot be handled will cause an error.
6333 * Normally, only one change can be managed at a time.
6334 */
NeilBrownfd01b882011-10-11 16:47:53 +11006335static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336{
6337 int rv = 0;
6338 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006339 int state = 0;
6340
6341 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006342 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006343 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344
6345 if (mddev->major_version != info->major_version ||
6346 mddev->minor_version != info->minor_version ||
6347/* mddev->patch_version != info->patch_version || */
6348 mddev->ctime != info->ctime ||
6349 mddev->level != info->level ||
6350/* mddev->layout != info->layout || */
6351 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006352 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006353 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6354 ((state^info->state) & 0xfffffe00)
6355 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 return -EINVAL;
6357 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006358 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6359 cnt++;
6360 if (mddev->raid_disks != info->raid_disks)
6361 cnt++;
6362 if (mddev->layout != info->layout)
6363 cnt++;
6364 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6365 cnt++;
6366 if (cnt == 0)
6367 return 0;
6368 if (cnt > 1)
6369 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370
6371 if (mddev->layout != info->layout) {
6372 /* Change layout
6373 * we don't need to do anything at the md level, the
6374 * personality will take care of it all.
6375 */
NeilBrown50ac1682009-06-18 08:47:55 +10006376 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006378 else {
6379 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006380 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006381 if (rv)
6382 mddev->new_layout = mddev->layout;
6383 return rv;
6384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006386 if (mddev_is_clustered(mddev))
6387 md_cluster_ops->metadata_update_start(mddev);
Andre Noll58c0fed2009-03-31 14:33:13 +11006388 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006389 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390
NeilBrownda943b992006-01-06 00:20:54 -08006391 if (mddev->raid_disks != info->raid_disks)
6392 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393
NeilBrown36fa3062005-09-09 16:23:45 -07006394 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006395 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6396 rv = -EINVAL;
6397 goto err;
6398 }
6399 if (mddev->recovery || mddev->sync_thread) {
6400 rv = -EBUSY;
6401 goto err;
6402 }
NeilBrown36fa3062005-09-09 16:23:45 -07006403 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006404 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006405 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006406 if (mddev->bitmap) {
6407 rv = -EEXIST;
6408 goto err;
6409 }
6410 if (mddev->bitmap_info.default_offset == 0) {
6411 rv = -EINVAL;
6412 goto err;
6413 }
NeilBrownc3d97142009-12-14 12:49:52 +11006414 mddev->bitmap_info.offset =
6415 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006416 mddev->bitmap_info.space =
6417 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006418 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006419 bitmap = bitmap_create(mddev, -1);
6420 if (!IS_ERR(bitmap)) {
6421 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006422 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006423 } else
6424 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006425 if (rv)
6426 bitmap_destroy(mddev);
6427 mddev->pers->quiesce(mddev, 0);
6428 } else {
6429 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006430 if (!mddev->bitmap) {
6431 rv = -ENOENT;
6432 goto err;
6433 }
6434 if (mddev->bitmap->storage.file) {
6435 rv = -EINVAL;
6436 goto err;
6437 }
NeilBrown36fa3062005-09-09 16:23:45 -07006438 mddev->pers->quiesce(mddev, 1);
6439 bitmap_destroy(mddev);
6440 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006441 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006442 }
6443 }
NeilBrown850b2b42006-10-03 01:15:46 -07006444 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006445 if (mddev_is_clustered(mddev))
6446 md_cluster_ops->metadata_update_finish(mddev);
6447 return rv;
6448err:
6449 if (mddev_is_clustered(mddev))
6450 md_cluster_ops->metadata_update_cancel(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 return rv;
6452}
6453
NeilBrownfd01b882011-10-11 16:47:53 +11006454static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455{
NeilBrown3cb03002011-10-11 16:45:26 +11006456 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006457 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458
6459 if (mddev->pers == NULL)
6460 return -ENODEV;
6461
NeilBrown1ca69c42012-10-11 13:37:33 +11006462 rcu_read_lock();
6463 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006465 err = -ENODEV;
6466 else {
6467 md_error(mddev, rdev);
6468 if (!test_bit(Faulty, &rdev->flags))
6469 err = -EBUSY;
6470 }
6471 rcu_read_unlock();
6472 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473}
6474
Andre Noll2f9618c2008-04-25 18:57:58 +02006475/*
6476 * We have a problem here : there is no easy way to give a CHS
6477 * virtual geometry. We currently pretend that we have a 2 heads
6478 * 4 sectors (with a BIG number of cylinders...). This drives
6479 * dosfs just mad... ;-)
6480 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006481static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6482{
NeilBrownfd01b882011-10-11 16:47:53 +11006483 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006484
6485 geo->heads = 2;
6486 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006487 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006488 return 0;
6489}
6490
Nicolas Schichancb335f82014-01-15 16:58:52 +01006491static inline bool md_ioctl_valid(unsigned int cmd)
6492{
6493 switch (cmd) {
6494 case ADD_NEW_DISK:
6495 case BLKROSET:
6496 case GET_ARRAY_INFO:
6497 case GET_BITMAP_FILE:
6498 case GET_DISK_INFO:
6499 case HOT_ADD_DISK:
6500 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006501 case RAID_AUTORUN:
6502 case RAID_VERSION:
6503 case RESTART_ARRAY_RW:
6504 case RUN_ARRAY:
6505 case SET_ARRAY_INFO:
6506 case SET_BITMAP_FILE:
6507 case SET_DISK_FAULTY:
6508 case STOP_ARRAY:
6509 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006510 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006511 return true;
6512 default:
6513 return false;
6514 }
6515}
6516
Al Viroa39907f2008-03-02 10:31:15 -05006517static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 unsigned int cmd, unsigned long arg)
6519{
6520 int err = 0;
6521 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006522 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006523 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524
Nicolas Schichancb335f82014-01-15 16:58:52 +01006525 if (!md_ioctl_valid(cmd))
6526 return -ENOTTY;
6527
NeilBrown506c9e42011-12-23 10:17:26 +11006528 switch (cmd) {
6529 case RAID_VERSION:
6530 case GET_ARRAY_INFO:
6531 case GET_DISK_INFO:
6532 break;
6533 default:
6534 if (!capable(CAP_SYS_ADMIN))
6535 return -EACCES;
6536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537
6538 /*
6539 * Commands dealing with the RAID driver but not any
6540 * particular array:
6541 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006542 switch (cmd) {
6543 case RAID_VERSION:
6544 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006545 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006548 case RAID_AUTORUN:
6549 err = 0;
6550 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006551 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006553 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554 }
6555
6556 /*
6557 * Commands creating/starting a new array:
6558 */
6559
Al Viroa39907f2008-03-02 10:31:15 -05006560 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561
6562 if (!mddev) {
6563 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006564 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 }
6566
NeilBrown1ca69c42012-10-11 13:37:33 +11006567 /* Some actions do not requires the mutex */
6568 switch (cmd) {
6569 case GET_ARRAY_INFO:
6570 if (!mddev->raid_disks && !mddev->external)
6571 err = -ENODEV;
6572 else
6573 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006574 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006575
6576 case GET_DISK_INFO:
6577 if (!mddev->raid_disks && !mddev->external)
6578 err = -ENODEV;
6579 else
6580 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006581 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006582
6583 case SET_DISK_FAULTY:
6584 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006585 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006586
6587 case GET_BITMAP_FILE:
6588 err = get_bitmap_file(mddev, argp);
6589 goto out;
6590
NeilBrown1ca69c42012-10-11 13:37:33 +11006591 }
6592
NeilBrowna7a3f082012-12-11 13:35:54 +11006593 if (cmd == ADD_NEW_DISK)
6594 /* need to ensure md_delayed_delete() has completed */
6595 flush_workqueue(md_misc_wq);
6596
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006597 if (cmd == HOT_REMOVE_DISK)
6598 /* need to ensure recovery thread has run */
6599 wait_event_interruptible_timeout(mddev->sb_wait,
6600 !test_bit(MD_RECOVERY_NEEDED,
6601 &mddev->flags),
6602 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006603 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6604 /* Need to flush page cache, and ensure no-one else opens
6605 * and writes
6606 */
6607 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006608 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006609 mutex_unlock(&mddev->open_mutex);
6610 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006611 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006612 }
6613 set_bit(MD_STILL_CLOSED, &mddev->flags);
6614 mutex_unlock(&mddev->open_mutex);
6615 sync_blockdev(bdev);
6616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617 err = mddev_lock(mddev);
6618 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006619 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6621 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006622 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 }
6624
NeilBrownc02c0ae2012-12-11 13:39:21 +11006625 if (cmd == SET_ARRAY_INFO) {
6626 mdu_array_info_t info;
6627 if (!arg)
6628 memset(&info, 0, sizeof(info));
6629 else if (copy_from_user(&info, argp, sizeof(info))) {
6630 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006631 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006632 }
6633 if (mddev->pers) {
6634 err = update_array_info(mddev, &info);
6635 if (err) {
6636 printk(KERN_WARNING "md: couldn't update"
6637 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006638 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006640 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006641 }
6642 if (!list_empty(&mddev->disks)) {
6643 printk(KERN_WARNING
6644 "md: array %s already has disks!\n",
6645 mdname(mddev));
6646 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006647 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006648 }
6649 if (mddev->raid_disks) {
6650 printk(KERN_WARNING
6651 "md: array %s already initialised!\n",
6652 mdname(mddev));
6653 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006654 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006655 }
6656 err = set_array_info(mddev, &info);
6657 if (err) {
6658 printk(KERN_WARNING "md: couldn't set"
6659 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006660 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006661 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006662 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 }
6664
6665 /*
6666 * Commands querying/configuring an existing array:
6667 */
NeilBrown32a76272005-06-21 17:17:14 -07006668 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006669 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006670 if ((!mddev->raid_disks && !mddev->external)
6671 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6672 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6673 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006675 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676 }
6677
6678 /*
6679 * Commands even a read-only array can execute:
6680 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006681 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006682 case RESTART_ARRAY_RW:
6683 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006684 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006685
6686 case STOP_ARRAY:
6687 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006688 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006689
6690 case STOP_ARRAY_RO:
6691 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006692 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006693
NeilBrown3ea8929d2013-04-24 11:42:41 +10006694 case HOT_REMOVE_DISK:
6695 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006696 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006697
NeilBrown7ceb17e2013-04-24 11:42:42 +10006698 case ADD_NEW_DISK:
6699 /* We can support ADD_NEW_DISK on read-only arrays
6700 * on if we are re-adding a preexisting device.
6701 * So require mddev->pers and MD_DISK_SYNC.
6702 */
6703 if (mddev->pers) {
6704 mdu_disk_info_t info;
6705 if (copy_from_user(&info, argp, sizeof(info)))
6706 err = -EFAULT;
6707 else if (!(info.state & (1<<MD_DISK_SYNC)))
6708 /* Need to clear read-only for this */
6709 break;
6710 else
6711 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006712 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006713 }
6714 break;
6715
NeilBrownc02c0ae2012-12-11 13:39:21 +11006716 case BLKROSET:
6717 if (get_user(ro, (int __user *)(arg))) {
6718 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006719 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006720 }
6721 err = -EINVAL;
6722
6723 /* if the bdev is going readonly the value of mddev->ro
6724 * does not matter, no writes are coming
6725 */
6726 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006727 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006728
NeilBrownc02c0ae2012-12-11 13:39:21 +11006729 /* are we are already prepared for writes? */
6730 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006731 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006732
6733 /* transitioning to readauto need only happen for
6734 * arrays that call md_write_start
6735 */
6736 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006738 if (err == 0) {
6739 mddev->ro = 2;
6740 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006741 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006742 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006743 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 }
6745
6746 /*
6747 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006748 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749 */
NeilBrown326eb172014-09-30 15:36:28 +10006750 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006751 if (mddev->ro == 2) {
6752 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006753 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006754 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006755 /* mddev_unlock will wake thread */
6756 /* If a device failed while we were read-only, we
6757 * need to make sure the metadata is updated now.
6758 */
6759 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6760 mddev_unlock(mddev);
6761 wait_event(mddev->sb_wait,
6762 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6763 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006764 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006765 }
NeilBrownf91de922005-11-08 21:39:36 -08006766 } else {
6767 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006768 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 }
6771
NeilBrownc02c0ae2012-12-11 13:39:21 +11006772 switch (cmd) {
6773 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006775 mdu_disk_info_t info;
6776 if (copy_from_user(&info, argp, sizeof(info)))
6777 err = -EFAULT;
6778 else
6779 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006780 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006783 case CLUSTERED_DISK_NACK:
6784 if (mddev_is_clustered(mddev))
6785 md_cluster_ops->new_disk_ack(mddev, false);
6786 else
6787 err = -EINVAL;
6788 goto unlock;
6789
NeilBrownc02c0ae2012-12-11 13:39:21 +11006790 case HOT_ADD_DISK:
6791 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006792 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793
NeilBrownc02c0ae2012-12-11 13:39:21 +11006794 case RUN_ARRAY:
6795 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006796 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797
NeilBrownc02c0ae2012-12-11 13:39:21 +11006798 case SET_BITMAP_FILE:
6799 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006800 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006801
NeilBrownc02c0ae2012-12-11 13:39:21 +11006802 default:
6803 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006804 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 }
6806
NeilBrown3adc28d2014-09-30 15:46:41 +10006807unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006808 if (mddev->hold_active == UNTIL_IOCTL &&
6809 err != -EINVAL)
6810 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006812out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 return err;
6814}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006815#ifdef CONFIG_COMPAT
6816static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6817 unsigned int cmd, unsigned long arg)
6818{
6819 switch (cmd) {
6820 case HOT_REMOVE_DISK:
6821 case HOT_ADD_DISK:
6822 case SET_DISK_FAULTY:
6823 case SET_BITMAP_FILE:
6824 /* These take in integer arg, do not convert */
6825 break;
6826 default:
6827 arg = (unsigned long)compat_ptr(arg);
6828 break;
6829 }
6830
6831 return md_ioctl(bdev, mode, cmd, arg);
6832}
6833#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834
Al Viroa39907f2008-03-02 10:31:15 -05006835static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836{
6837 /*
6838 * Succeed if we can lock the mddev, which confirms that
6839 * it isn't being stopped right now.
6840 */
NeilBrownfd01b882011-10-11 16:47:53 +11006841 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 int err;
6843
Yuanhan Liu0c098222012-05-22 13:55:32 +10006844 if (!mddev)
6845 return -ENODEV;
6846
NeilBrownd3374822009-01-09 08:31:10 +11006847 if (mddev->gendisk != bdev->bd_disk) {
6848 /* we are racing with mddev_put which is discarding this
6849 * bd_disk.
6850 */
6851 mddev_put(mddev);
6852 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006853 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006854 /* Then retry the open from the top */
6855 return -ERESTARTSYS;
6856 }
6857 BUG_ON(mddev != bdev->bd_disk->private_data);
6858
NeilBrownc8c00a62009-08-10 12:50:52 +10006859 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 goto out;
6861
6862 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006863 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006864 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006865 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006867 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868 out:
6869 return err;
6870}
6871
Al Virodb2a1442013-05-05 21:52:57 -04006872static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873{
NeilBrownf72ffdd2014-09-30 14:23:59 +10006874 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006876 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006877 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006880
6881static int md_media_changed(struct gendisk *disk)
6882{
NeilBrownfd01b882011-10-11 16:47:53 +11006883 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006884
6885 return mddev->changed;
6886}
6887
6888static int md_revalidate(struct gendisk *disk)
6889{
NeilBrownfd01b882011-10-11 16:47:53 +11006890 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006891
6892 mddev->changed = 0;
6893 return 0;
6894}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006895static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896{
6897 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006898 .open = md_open,
6899 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006900 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006901#ifdef CONFIG_COMPAT
6902 .compat_ioctl = md_compat_ioctl,
6903#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006904 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006905 .media_changed = md_media_changed,
6906 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907};
6908
NeilBrownf72ffdd2014-09-30 14:23:59 +10006909static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910{
NeilBrown2b8bf342011-10-11 16:48:23 +11006911 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913 /*
6914 * md_thread is a 'system-thread', it's priority should be very
6915 * high. We avoid resource deadlocks individually in each
6916 * raid personality. (RAID5 does preallocation) We also use RR and
6917 * the very same RT priority as kswapd, thus we will never get
6918 * into a priority inversion deadlock.
6919 *
6920 * we definitely have to have equal or higher priority than
6921 * bdflush, otherwise bdflush will deadlock if there are too
6922 * many dirty RAID5 blocks.
6923 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924
NeilBrown6985c432005-10-19 21:23:47 -07006925 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006926 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927
NeilBrown93588e22005-11-15 00:09:12 -08006928 /* We need to wait INTERRUPTIBLE so that
6929 * we don't add to the load-average.
6930 * That means we need to be sure no signals are
6931 * pending
6932 */
6933 if (signal_pending(current))
6934 flush_signals(current);
6935
6936 wait_event_interruptible_timeout
6937 (thread->wqueue,
6938 test_bit(THREAD_WAKEUP, &thread->flags)
6939 || kthread_should_stop(),
6940 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941
NeilBrown6c987912011-01-14 09:13:53 +11006942 clear_bit(THREAD_WAKEUP, &thread->flags);
6943 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006944 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006946
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 return 0;
6948}
6949
NeilBrown2b8bf342011-10-11 16:48:23 +11006950void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951{
6952 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006953 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954 set_bit(THREAD_WAKEUP, &thread->flags);
6955 wake_up(&thread->wqueue);
6956 }
6957}
NeilBrown6c144d32014-09-30 16:15:38 +10006958EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959
Shaohua Li4ed87312012-10-11 13:34:00 +11006960struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6961 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962{
NeilBrown2b8bf342011-10-11 16:48:23 +11006963 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964
NeilBrown2b8bf342011-10-11 16:48:23 +11006965 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966 if (!thread)
6967 return NULL;
6968
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969 init_waitqueue_head(&thread->wqueue);
6970
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 thread->run = run;
6972 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006973 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006974 thread->tsk = kthread_run(md_thread, thread,
6975 "%s_%s",
6976 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006977 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006978 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979 kfree(thread);
6980 return NULL;
6981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982 return thread;
6983}
NeilBrown6c144d32014-09-30 16:15:38 +10006984EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985
NeilBrown2b8bf342011-10-11 16:48:23 +11006986void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987{
NeilBrown2b8bf342011-10-11 16:48:23 +11006988 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006989 if (!thread)
6990 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006991 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006992 /* Locking ensures that mddev_unlock does not wake_up a
6993 * non-existent thread
6994 */
6995 spin_lock(&pers_lock);
6996 *threadp = NULL;
6997 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006998
6999 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 kfree(thread);
7001}
NeilBrown6c144d32014-09-30 16:15:38 +10007002EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003
NeilBrownfd01b882011-10-11 16:47:53 +11007004void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005{
NeilBrownb2d444d2005-11-08 21:39:31 -08007006 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007008
NeilBrownde393cd2011-07-28 11:31:48 +10007009 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 return;
7011 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007012 if (mddev->degraded)
7013 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007014 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7016 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7017 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007018 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007019 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07007020 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021}
NeilBrown6c144d32014-09-30 16:15:38 +10007022EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023
7024/* seq_file implementation /proc/mdstat */
7025
7026static void status_unused(struct seq_file *seq)
7027{
7028 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007029 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030
7031 seq_printf(seq, "unused devices: ");
7032
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007033 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 char b[BDEVNAME_SIZE];
7035 i++;
7036 seq_printf(seq, "%s ",
7037 bdevname(rdev->bdev,b));
7038 }
7039 if (!i)
7040 seq_printf(seq, "<none>");
7041
7042 seq_printf(seq, "\n");
7043}
7044
NeilBrownf72ffdd2014-09-30 14:23:59 +10007045static void status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046{
NeilBrowndd71cf62009-05-07 12:49:35 +10007047 sector_t max_sectors, resync, res;
7048 unsigned long dt, db;
7049 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007050 int scale;
7051 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052
NeilBrown72f36d52012-10-11 14:25:57 +11007053 if (mddev->curr_resync <= 3)
7054 resync = 0;
7055 else
7056 resync = mddev->curr_resync
7057 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058
NeilBrownc804cde2012-05-21 09:28:33 +10007059 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7060 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007061 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007063 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064
NeilBrown403df472014-09-30 15:52:29 +10007065 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007066 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007067 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007068 * u32, as those are the requirements for sector_div.
7069 * Thus 'scale' must be at least 10
7070 */
7071 scale = 10;
7072 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007073 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007074 scale++;
7075 }
7076 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007077 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007078
7079 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 {
NeilBrown4588b422006-03-27 01:18:04 -08007081 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 seq_printf(seq, "[");
7083 for (i = 0; i < x; i++)
7084 seq_printf(seq, "=");
7085 seq_printf(seq, ">");
7086 for (i = 0; i < y; i++)
7087 seq_printf(seq, ".");
7088 seq_printf(seq, "] ");
7089 }
NeilBrown4588b422006-03-27 01:18:04 -08007090 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007091 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7092 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007093 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7094 "check" :
7095 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7096 "resync" : "recovery"))),
7097 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007098 (unsigned long long) resync/2,
7099 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100
7101 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 * dt: time from mark until now
7103 * db: blocks written from mark until now
7104 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007105 *
7106 * rt is a sector_t, so could be 32bit or 64bit.
7107 * So we divide before multiply in case it is 32bit and close
7108 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007109 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007110 * near the end of resync when the number of remaining sectors
7111 * is close to 'db'.
7112 * We then divide rt by 32 after multiplying by db to compensate.
7113 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114 */
7115 dt = ((jiffies - mddev->resync_mark) / HZ);
7116 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007117 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7118 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119
NeilBrowndd71cf62009-05-07 12:49:35 +10007120 rt = max_sectors - resync; /* number of remaining sectors */
7121 sector_div(rt, db/32+1);
7122 rt *= dt;
7123 rt >>= 5;
7124
7125 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7126 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127
NeilBrownff4e8d92006-07-10 04:44:16 -07007128 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129}
7130
7131static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7132{
7133 struct list_head *tmp;
7134 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007135 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136
7137 if (l >= 0x10000)
7138 return NULL;
7139 if (!l--)
7140 /* header */
7141 return (void*)1;
7142
7143 spin_lock(&all_mddevs_lock);
7144 list_for_each(tmp,&all_mddevs)
7145 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007146 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 mddev_get(mddev);
7148 spin_unlock(&all_mddevs_lock);
7149 return mddev;
7150 }
7151 spin_unlock(&all_mddevs_lock);
7152 if (!l--)
7153 return (void*)2;/* tail */
7154 return NULL;
7155}
7156
7157static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7158{
7159 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007160 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007161
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 ++*pos;
7163 if (v == (void*)2)
7164 return NULL;
7165
7166 spin_lock(&all_mddevs_lock);
7167 if (v == (void*)1)
7168 tmp = all_mddevs.next;
7169 else
7170 tmp = mddev->all_mddevs.next;
7171 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007172 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173 else {
7174 next_mddev = (void*)2;
7175 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177 spin_unlock(&all_mddevs_lock);
7178
7179 if (v != (void*)1)
7180 mddev_put(mddev);
7181 return next_mddev;
7182
7183}
7184
7185static void md_seq_stop(struct seq_file *seq, void *v)
7186{
NeilBrownfd01b882011-10-11 16:47:53 +11007187 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188
7189 if (mddev && v != (void*)1 && v != (void*)2)
7190 mddev_put(mddev);
7191}
7192
7193static int md_seq_show(struct seq_file *seq, void *v)
7194{
NeilBrownfd01b882011-10-11 16:47:53 +11007195 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007196 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007197 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198
7199 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007200 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201 seq_printf(seq, "Personalities : ");
7202 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007203 list_for_each_entry(pers, &pers_list, list)
7204 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205
7206 spin_unlock(&pers_lock);
7207 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007208 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209 return 0;
7210 }
7211 if (v == (void*)2) {
7212 status_unused(seq);
7213 return 0;
7214 }
7215
NeilBrown36d091f2014-12-15 12:56:58 +11007216 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7218 seq_printf(seq, "%s : %sactive", mdname(mddev),
7219 mddev->pers ? "" : "in");
7220 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007221 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007223 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007224 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225 seq_printf(seq, " %s", mddev->pers->name);
7226 }
7227
Andre Nolldd8ac332009-03-31 14:33:13 +11007228 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007229 rcu_read_lock();
7230 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 char b[BDEVNAME_SIZE];
7232 seq_printf(seq, " %s[%d]",
7233 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007234 if (test_bit(WriteMostly, &rdev->flags))
7235 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007236 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 seq_printf(seq, "(F)");
7238 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007239 }
7240 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007241 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007242 if (test_bit(Replacement, &rdev->flags))
7243 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007244 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007246 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247
7248 if (!list_empty(&mddev->disks)) {
7249 if (mddev->pers)
7250 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007251 (unsigned long long)
7252 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 else
7254 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007255 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007257 if (mddev->persistent) {
7258 if (mddev->major_version != 0 ||
7259 mddev->minor_version != 90) {
7260 seq_printf(seq," super %d.%d",
7261 mddev->major_version,
7262 mddev->minor_version);
7263 }
NeilBrowne6910632008-02-06 01:39:51 -08007264 } else if (mddev->external)
7265 seq_printf(seq, " super external:%s",
7266 mddev->metadata_type);
7267 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007268 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269
7270 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007271 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007272 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007273 if (mddev->pers->sync_request) {
7274 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007275 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007276 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007277 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007278 seq_printf(seq, "\tresync=DELAYED\n ");
7279 else if (mddev->recovery_cp < MaxSector)
7280 seq_printf(seq, "\tresync=PENDING\n ");
7281 }
NeilBrown32a76272005-06-21 17:17:14 -07007282 } else
7283 seq_printf(seq, "\n ");
7284
NeilBrown57148962012-03-19 12:46:40 +11007285 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286
7287 seq_printf(seq, "\n");
7288 }
NeilBrown36d091f2014-12-15 12:56:58 +11007289 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007290
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291 return 0;
7292}
7293
Jan Engelhardt110518b2009-05-07 12:49:37 +10007294static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295 .start = md_seq_start,
7296 .next = md_seq_next,
7297 .stop = md_seq_stop,
7298 .show = md_seq_show,
7299};
7300
7301static int md_seq_open(struct inode *inode, struct file *file)
7302{
Kay Sieversf1514632011-07-12 20:48:39 +02007303 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304 int error;
7305
7306 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007307 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007308 return error;
7309
7310 seq = file->private_data;
7311 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312 return error;
7313}
7314
NeilBrowne2f23b62014-04-09 14:33:51 +10007315static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007316static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7317{
Kay Sieversf1514632011-07-12 20:48:39 +02007318 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007319 int mask;
7320
NeilBrowne2f23b62014-04-09 14:33:51 +10007321 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007322 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007323 poll_wait(filp, &md_event_waiters, wait);
7324
7325 /* always allow read */
7326 mask = POLLIN | POLLRDNORM;
7327
Kay Sieversf1514632011-07-12 20:48:39 +02007328 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007329 mask |= POLLERR | POLLPRI;
7330 return mask;
7331}
7332
Arjan van de Venfa027c22007-02-12 00:55:33 -08007333static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007334 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335 .open = md_seq_open,
7336 .read = seq_read,
7337 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007338 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007339 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340};
7341
NeilBrown84fc4b52011-10-11 16:49:58 +11007342int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343{
Chao Yu50bd3772014-09-25 15:28:34 +08007344 printk(KERN_INFO "md: %s personality registered for level %d\n",
7345 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007347 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 spin_unlock(&pers_lock);
7349 return 0;
7350}
NeilBrown6c144d32014-09-30 16:15:38 +10007351EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352
NeilBrown84fc4b52011-10-11 16:49:58 +11007353int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354{
NeilBrown2604b702006-01-06 00:20:36 -08007355 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007357 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 spin_unlock(&pers_lock);
7359 return 0;
7360}
NeilBrown6c144d32014-09-30 16:15:38 +10007361EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007363int register_md_cluster_operations(struct md_cluster_operations *ops, struct module *module)
7364{
7365 if (md_cluster_ops != NULL)
7366 return -EALREADY;
7367 spin_lock(&pers_lock);
7368 md_cluster_ops = ops;
7369 md_cluster_mod = module;
7370 spin_unlock(&pers_lock);
7371 return 0;
7372}
7373EXPORT_SYMBOL(register_md_cluster_operations);
7374
7375int unregister_md_cluster_operations(void)
7376{
7377 spin_lock(&pers_lock);
7378 md_cluster_ops = NULL;
7379 spin_unlock(&pers_lock);
7380 return 0;
7381}
7382EXPORT_SYMBOL(unregister_md_cluster_operations);
7383
7384int md_setup_cluster(struct mddev *mddev, int nodes)
7385{
7386 int err;
7387
7388 err = request_module("md-cluster");
7389 if (err) {
7390 pr_err("md-cluster module not found.\n");
7391 return err;
7392 }
7393
7394 spin_lock(&pers_lock);
7395 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7396 spin_unlock(&pers_lock);
7397 return -ENOENT;
7398 }
7399 spin_unlock(&pers_lock);
7400
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007401 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007402}
7403
7404void md_cluster_stop(struct mddev *mddev)
7405{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007406 if (!md_cluster_ops)
7407 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007408 md_cluster_ops->leave(mddev);
7409 module_put(md_cluster_mod);
7410}
7411
NeilBrownfd01b882011-10-11 16:47:53 +11007412static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007414 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007416 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417
7418 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007419 rcu_read_lock();
7420 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007422 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7423 (int)part_stat_read(&disk->part0, sectors[1]) -
7424 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007425 /* sync IO will cause sync_io to increase before the disk_stats
7426 * as sync_io is counted when a request starts, and
7427 * disk_stats is counted when it completes.
7428 * So resync activity will cause curr_events to be smaller than
7429 * when there was no such activity.
7430 * non-sync IO will cause disk_stat to increase without
7431 * increasing sync_io so curr_events will (eventually)
7432 * be larger than it was before. Once it becomes
7433 * substantially larger, the test below will cause
7434 * the array to appear non-idle, and resync will slow
7435 * down.
7436 * If there is a lot of outstanding resync activity when
7437 * we set last_event to curr_events, then all that activity
7438 * completing might cause the array to appear non-idle
7439 * and resync will be slowed down even though there might
7440 * not have been non-resync activity. This will only
7441 * happen once though. 'last_events' will soon reflect
7442 * the state where there is little or no outstanding
7443 * resync requests, and further resync activity will
7444 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007445 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007447 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 rdev->last_events = curr_events;
7449 idle = 0;
7450 }
7451 }
NeilBrown4b809912008-07-21 17:05:25 +10007452 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 return idle;
7454}
7455
NeilBrownfd01b882011-10-11 16:47:53 +11007456void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457{
7458 /* another "blocks" (512byte) blocks have been synced */
7459 atomic_sub(blocks, &mddev->recovery_active);
7460 wake_up(&mddev->recovery_wait);
7461 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007462 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007463 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 md_wakeup_thread(mddev->thread);
7465 // stop recovery, signal do_sync ....
7466 }
7467}
NeilBrown6c144d32014-09-30 16:15:38 +10007468EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469
NeilBrown06d91a52005-06-21 17:17:12 -07007470/* md_write_start(mddev, bi)
7471 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007472 * in superblock) before writing, schedule a superblock update
7473 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007474 */
NeilBrownfd01b882011-10-11 16:47:53 +11007475void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476{
Neil Brown0fd62b82008-06-28 08:31:36 +10007477 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007478 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007479 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007480
NeilBrownf91de922005-11-08 21:39:36 -08007481 BUG_ON(mddev->ro == 1);
7482 if (mddev->ro == 2) {
7483 /* need to switch to read/write */
7484 mddev->ro = 0;
7485 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7486 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007487 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007488 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007489 }
NeilBrown06d91a52005-06-21 17:17:12 -07007490 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007491 if (mddev->safemode == 1)
7492 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007493 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007494 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007495 if (mddev->in_sync) {
7496 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007497 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007498 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007499 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007500 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007501 }
NeilBrown85572d72014-12-15 12:56:56 +11007502 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007503 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007504 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007505 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007506 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007507 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508}
NeilBrown6c144d32014-09-30 16:15:38 +10007509EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510
NeilBrownfd01b882011-10-11 16:47:53 +11007511void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512{
7513 if (atomic_dec_and_test(&mddev->writes_pending)) {
7514 if (mddev->safemode == 2)
7515 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007516 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007517 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7518 }
7519}
NeilBrown6c144d32014-09-30 16:15:38 +10007520EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521
NeilBrown2a2275d2007-01-26 00:57:11 -08007522/* md_allow_write(mddev)
7523 * Calling this ensures that the array is marked 'active' so that writes
7524 * may proceed without blocking. It is important to call this before
7525 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7526 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007527 *
7528 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7529 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007530 */
NeilBrownfd01b882011-10-11 16:47:53 +11007531int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007532{
7533 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007534 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007535 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007536 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007537 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007538 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007539
NeilBrown85572d72014-12-15 12:56:56 +11007540 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007541 if (mddev->in_sync) {
7542 mddev->in_sync = 0;
7543 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007544 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007545 if (mddev->safemode_delay &&
7546 mddev->safemode == 0)
7547 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007548 spin_unlock(&mddev->lock);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007549 if (mddev_is_clustered(mddev))
7550 md_cluster_ops->metadata_update_start(mddev);
NeilBrown2a2275d2007-01-26 00:57:11 -08007551 md_update_sb(mddev, 0);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007552 if (mddev_is_clustered(mddev))
7553 md_cluster_ops->metadata_update_finish(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007554 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007555 } else
NeilBrown85572d72014-12-15 12:56:56 +11007556 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007557
NeilBrown070dc6d2010-08-30 17:33:34 +10007558 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007559 return -EAGAIN;
7560 else
7561 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007562}
7563EXPORT_SYMBOL_GPL(md_allow_write);
7564
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565#define SYNC_MARKS 10
7566#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007567#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007568void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569{
Shaohua Li4ed87312012-10-11 13:34:00 +11007570 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007571 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572 unsigned int currspeed = 0,
7573 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007574 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007576 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577 sector_t mark_cnt[SYNC_MARKS];
7578 int last_mark,m;
7579 struct list_head *tmp;
7580 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007581 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007582 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007583 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007584 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585
7586 /* just incase thread restarts... */
7587 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7588 return;
NeilBrown3991b312014-05-28 13:39:23 +10007589 if (mddev->ro) {/* never try to sync a read-only array */
7590 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007591 return;
NeilBrown3991b312014-05-28 13:39:23 +10007592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593
NeilBrown61df9d92006-10-03 01:15:57 -07007594 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007595 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007596 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007597 action = "check";
7598 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007599 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007600 action = "repair";
7601 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007602 desc = "resync";
7603 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7604 desc = "reshape";
7605 else
7606 desc = "recovery";
7607
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007608 mddev->last_sync_action = action ?: desc;
7609
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 /* we overload curr_resync somewhat here.
7611 * 0 == not engaged in resync at all
7612 * 2 == checking that there is no conflict with another sync
7613 * 1 == like 2, but have yielded to allow conflicting resync to
7614 * commense
7615 * other == active in resync - this many blocks
7616 *
7617 * Before starting a resync we must have set curr_resync to
7618 * 2, and then checked that every "conflicting" array has curr_resync
7619 * less than ours. When we find one that is the same or higher
7620 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7621 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7622 * This will mean we have to start checking from the beginning again.
7623 *
7624 */
7625
7626 do {
7627 mddev->curr_resync = 2;
7628
7629 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007630 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007632 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 if (mddev2 == mddev)
7634 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007635 if (!mddev->parallel_resync
7636 && mddev2->curr_resync
7637 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638 DEFINE_WAIT(wq);
7639 if (mddev < mddev2 && mddev->curr_resync == 2) {
7640 /* arbitrarily yield */
7641 mddev->curr_resync = 1;
7642 wake_up(&resync_wait);
7643 }
7644 if (mddev > mddev2 && mddev->curr_resync == 1)
7645 /* no need to wait here, we can wait the next
7646 * time 'round when curr_resync == 2
7647 */
7648 continue;
NeilBrown97441972008-09-19 11:49:54 +10007649 /* We need to wait 'interruptible' so as not to
7650 * contribute to the load average, and not to
7651 * be caught by 'softlockup'
7652 */
7653 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007654 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007655 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007656 printk(KERN_INFO "md: delaying %s of %s"
7657 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007659 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007661 if (signal_pending(current))
7662 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663 schedule();
7664 finish_wait(&resync_wait, &wq);
7665 goto try_again;
7666 }
7667 finish_wait(&resync_wait, &wq);
7668 }
7669 }
7670 } while (mddev->curr_resync < 2);
7671
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007672 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007673 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007675 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676 */
7677 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007678 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007679 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007680 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7681 j = mddev->resync_min;
7682 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007683 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007684
NeilBrownccfcc3c2006-03-27 01:18:09 -08007685 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007686 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007687 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007689 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007690 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007691 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007692 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007693 if (rdev->raid_disk >= 0 &&
7694 !test_bit(Faulty, &rdev->flags) &&
7695 !test_bit(In_sync, &rdev->flags) &&
7696 rdev->recovery_offset < j)
7697 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007698 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007699
7700 /* If there is a bitmap, we need to make sure all
7701 * writes that started before we added a spare
7702 * complete before we start doing a recovery.
7703 * Otherwise the write might complete and (via
7704 * bitmap_endwrite) set a bit in the bitmap after the
7705 * recovery has checked that bit and skipped that
7706 * region.
7707 */
7708 if (mddev->bitmap) {
7709 mddev->pers->quiesce(mddev, 1);
7710 mddev->pers->quiesce(mddev, 0);
7711 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713
NeilBrown61df9d92006-10-03 01:15:57 -07007714 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7715 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7716 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007717 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007718 "(but not more than %d KB/sec) for %s.\n",
7719 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720
NeilBrowneea1bf32009-03-31 14:27:02 +11007721 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007722
NeilBrown57afd892005-06-21 17:17:13 -07007723 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007724 for (m = 0; m < SYNC_MARKS; m++) {
7725 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007726 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727 }
7728 last_mark = 0;
7729 mddev->resync_mark = mark[last_mark];
7730 mddev->resync_mark_cnt = mark_cnt[last_mark];
7731
7732 /*
7733 * Tune reconstruction:
7734 */
7735 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007736 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7737 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738
7739 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740 last_check = 0;
7741
7742 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007743 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007744 "md: resuming %s of %s from checkpoint.\n",
7745 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007746 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007747 } else
7748 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007749 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007750 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7751 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007752 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007753
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007754 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -05007755 md_cluster_ops->resync_start(mddev, j, max_sectors);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007756
majianpeng7c2c57c2012-07-03 12:12:26 +10007757 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007758 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007759 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760
NeilBrown57afd892005-06-21 17:17:13 -07007761 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007762
NeilBrown7a91ee12009-05-26 12:57:21 +10007763 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7764 ((mddev->curr_resync > mddev->curr_resync_completed &&
7765 (mddev->curr_resync - mddev->curr_resync_completed)
7766 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007767 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007768 (j - mddev->curr_resync_completed)*2
7769 >= mddev->resync_max - mddev->curr_resync_completed
7770 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007771 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007772 wait_event(mddev->recovery_wait,
7773 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007774 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007775 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7776 j > mddev->recovery_cp)
7777 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007778 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007779 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007780 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007781 }
NeilBrownacb180b2009-04-14 16:28:34 +10007782
NeilBrownc91abf52013-11-19 12:02:01 +11007783 while (j >= mddev->resync_max &&
7784 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007785 /* As this condition is controlled by user-space,
7786 * we can block indefinitely, so use '_interruptible'
7787 * to avoid triggering warnings.
7788 */
7789 flush_signals(current); /* just in case */
7790 wait_event_interruptible(mddev->recovery_wait,
7791 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007792 || test_bit(MD_RECOVERY_INTR,
7793 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007794 }
NeilBrownacb180b2009-04-14 16:28:34 +10007795
NeilBrownc91abf52013-11-19 12:02:01 +11007796 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7797 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007798
NeilBrown57afd892005-06-21 17:17:13 -07007799 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007800 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007801 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007802 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007803 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804 }
NeilBrown57afd892005-06-21 17:17:13 -07007805
7806 if (!skipped) { /* actual IO requested */
7807 io_sectors += sectors;
7808 atomic_add(sectors, &mddev->recovery_active);
7809 }
7810
NeilBrowne875ece2011-07-28 11:39:24 +10007811 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7812 break;
7813
Linus Torvalds1da177e2005-04-16 15:20:36 -07007814 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007815 if (j > 2)
7816 mddev->curr_resync = j;
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007817 if (mddev_is_clustered(mddev))
7818 md_cluster_ops->resync_info_update(mddev, j, max_sectors);
NeilBrownff4e8d92006-07-10 04:44:16 -07007819 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007820 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007821 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007822 * visible in /proc/mdstat
7823 */
7824 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007825
7826 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827 continue;
7828
NeilBrown57afd892005-06-21 17:17:13 -07007829 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007830 repeat:
7831 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7832 /* step marks */
7833 int next = (last_mark+1) % SYNC_MARKS;
7834
7835 mddev->resync_mark = mark[next];
7836 mddev->resync_mark_cnt = mark_cnt[next];
7837 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007838 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839 last_mark = next;
7840 }
7841
NeilBrownc91abf52013-11-19 12:02:01 +11007842 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7843 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844
7845 /*
7846 * this loop exits only if either when we are slower than
7847 * the 'hard' speed limit, or the system was IO-idle for
7848 * a jiffy.
7849 * the system might be non-idle CPU-wise, but we only care
7850 * about not overloading the IO subsystem. (things like an
7851 * e2fsck being done on the RAID array should execute fast)
7852 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853 cond_resched();
7854
Xiao Niac7e50a2014-08-07 09:37:41 -04007855 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7856 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07007857 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007858
NeilBrown88202a02006-01-06 00:21:36 -08007859 if (currspeed > speed_min(mddev)) {
7860 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007861 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007862 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 goto repeat;
7864 }
7865 }
7866 }
NeilBrownc91abf52013-11-19 12:02:01 +11007867 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7868 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7869 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870 /*
7871 * this also signals 'finished resyncing' to md_stop
7872 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007873 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7875
7876 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007877 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007879 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -05007880 md_cluster_ops->resync_finish(mddev);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007881
NeilBrowndfc70642008-05-23 13:04:39 -07007882 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007883 mddev->curr_resync > 2) {
7884 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7885 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7886 if (mddev->curr_resync >= mddev->recovery_cp) {
7887 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007888 "md: checkpointing %s of %s.\n",
7889 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007890 if (test_bit(MD_RECOVERY_ERROR,
7891 &mddev->recovery))
7892 mddev->recovery_cp =
7893 mddev->curr_resync_completed;
7894 else
7895 mddev->recovery_cp =
7896 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007897 }
7898 } else
7899 mddev->recovery_cp = MaxSector;
7900 } else {
7901 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7902 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007903 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007904 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007905 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007906 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007907 !test_bit(Faulty, &rdev->flags) &&
7908 !test_bit(In_sync, &rdev->flags) &&
7909 rdev->recovery_offset < mddev->curr_resync)
7910 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007911 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007914 skip:
NeilBrown17571282006-12-10 02:20:52 -08007915 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916
NeilBrown23da4222014-12-15 12:57:01 +11007917 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11007918 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7919 /* We completed so min/max setting can be forgotten if used. */
7920 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7921 mddev->resync_min = 0;
7922 mddev->resync_max = MaxSector;
7923 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7924 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11007926 spin_unlock(&mddev->lock);
7927
Linus Torvalds1da177e2005-04-16 15:20:36 -07007928 wake_up(&resync_wait);
7929 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7930 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007931 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932}
NeilBrown29269552006-03-27 01:18:10 -08007933EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934
NeilBrown746d3202013-04-24 11:42:41 +10007935static int remove_and_add_spares(struct mddev *mddev,
7936 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007937{
NeilBrown3cb03002011-10-11 16:45:26 +11007938 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007939 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007940 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007941
NeilBrowndafb20f2012-03-19 12:46:39 +11007942 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007943 if ((this == NULL || rdev == this) &&
7944 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007945 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007946 (test_bit(Faulty, &rdev->flags) ||
7947 ! test_bit(In_sync, &rdev->flags)) &&
7948 atomic_read(&rdev->nr_pending)==0) {
7949 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007950 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007951 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007952 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007953 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007954 }
7955 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007956 if (removed && mddev->kobj.sd)
7957 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007958
NeilBrown746d3202013-04-24 11:42:41 +10007959 if (this)
7960 goto no_add;
7961
NeilBrowndafb20f2012-03-19 12:46:39 +11007962 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007963 if (rdev->raid_disk >= 0 &&
7964 !test_bit(In_sync, &rdev->flags) &&
7965 !test_bit(Faulty, &rdev->flags))
7966 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007967 if (rdev->raid_disk >= 0)
7968 continue;
7969 if (test_bit(Faulty, &rdev->flags))
7970 continue;
7971 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11007972 ! (rdev->saved_raid_disk >= 0 &&
7973 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10007974 continue;
7975
NeilBrown7eb41882014-01-14 15:55:14 +11007976 if (rdev->saved_raid_disk < 0)
7977 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007978 if (mddev->pers->
7979 hot_add_disk(mddev, rdev) == 0) {
7980 if (sysfs_link_rdev(mddev, rdev))
7981 /* failure here is OK */;
7982 spares++;
7983 md_new_event(mddev);
7984 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007985 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007986 }
NeilBrown746d3202013-04-24 11:42:41 +10007987no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007988 if (removed)
7989 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007990 return spares;
7991}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007992
NeilBrownac05f252014-09-30 08:10:42 +10007993static void md_start_sync(struct work_struct *ws)
7994{
7995 struct mddev *mddev = container_of(ws, struct mddev, del_work);
7996
7997 mddev->sync_thread = md_register_thread(md_do_sync,
7998 mddev,
7999 "resync");
8000 if (!mddev->sync_thread) {
8001 printk(KERN_ERR "%s: could not start resync"
8002 " thread...\n",
8003 mdname(mddev));
8004 /* leave the spares where they are, it shouldn't hurt */
8005 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8006 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8007 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8008 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8009 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008010 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008011 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8012 &mddev->recovery))
8013 if (mddev->sysfs_action)
8014 sysfs_notify_dirent_safe(mddev->sysfs_action);
8015 } else
8016 md_wakeup_thread(mddev->sync_thread);
8017 sysfs_notify_dirent_safe(mddev->sysfs_action);
8018 md_new_event(mddev);
8019}
8020
Linus Torvalds1da177e2005-04-16 15:20:36 -07008021/*
8022 * This routine is regularly called by all per-raid-array threads to
8023 * deal with generic issues like resync and super-block update.
8024 * Raid personalities that don't have a thread (linear/raid0) do not
8025 * need this as they never do any recovery or update the superblock.
8026 *
8027 * It does not do any resync itself, but rather "forks" off other threads
8028 * to do that as needed.
8029 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8030 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008031 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008032 * and wakeups up this thread which will reap the thread and finish up.
8033 * This thread also removes any faulty devices (with nr_pending == 0).
8034 *
8035 * The overall approach is:
8036 * 1/ if the superblock needs updating, update it.
8037 * 2/ If a recovery thread is running, don't do anything else.
8038 * 3/ If recovery has finished, clean up, possibly marking spares active.
8039 * 4/ If there are any faulty devices, remove them.
8040 * 5/ If array is degraded, try to add spares devices
8041 * 6/ If array has spares or is not in-sync, start a resync thread.
8042 */
NeilBrownfd01b882011-10-11 16:47:53 +11008043void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008044{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008045 if (mddev->suspended)
8046 return;
8047
NeilBrown5f404022005-06-21 17:17:16 -07008048 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008049 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008050
NeilBrownfca4d842005-06-21 17:17:11 -07008051 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008052 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008053 printk(KERN_INFO "md: %s in immediate safe mode\n",
8054 mdname(mddev));
8055 mddev->safemode = 2;
8056 }
8057 flush_signals(current);
8058 }
8059
NeilBrownc89a8ee2008-08-05 15:54:13 +10008060 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8061 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008062 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008063 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008064 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008065 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008066 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008067 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8068 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008069 ))
8070 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008071
NeilBrowndf5b89b2006-03-27 01:18:20 -08008072 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008073 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008074
NeilBrownc89a8ee2008-08-05 15:54:13 +10008075 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10008076 /* On a read-only array we can:
8077 * - remove failed devices
8078 * - add already-in_sync devices if the array itself
8079 * is in-sync.
8080 * As we only add devices that are already in-sync,
8081 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008082 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008083 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008084 /* There is no thread, but we need to call
8085 * ->spare_active and clear saved_raid_disk
8086 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008087 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008088 md_reap_sync_thread(mddev);
8089 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008090 goto unlock;
8091 }
8092
NeilBrown31a59e32008-04-30 00:52:30 -07008093 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008094 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008095 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008096 if (mddev->safemode &&
8097 !atomic_read(&mddev->writes_pending) &&
8098 !mddev->in_sync &&
8099 mddev->recovery_cp == MaxSector) {
8100 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008101 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008102 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008103 }
8104 if (mddev->safemode == 1)
8105 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008106 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008107 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008108 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008109 }
NeilBrownfca4d842005-06-21 17:17:11 -07008110
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008111 if (mddev->flags & MD_UPDATE_SB_FLAGS) {
8112 if (mddev_is_clustered(mddev))
8113 md_cluster_ops->metadata_update_start(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07008114 md_update_sb(mddev, 0);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008115 if (mddev_is_clustered(mddev))
8116 md_cluster_ops->metadata_update_finish(mddev);
8117 }
NeilBrown06d91a52005-06-21 17:17:12 -07008118
Linus Torvalds1da177e2005-04-16 15:20:36 -07008119 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8120 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8121 /* resync/recovery still happening */
8122 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8123 goto unlock;
8124 }
8125 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008126 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008127 goto unlock;
8128 }
Neil Brown72a23c22008-06-28 08:31:41 +10008129 /* Set RUNNING before clearing NEEDED to avoid
8130 * any transients in the value of "sync_action".
8131 */
NeilBrown72f36d52012-10-11 14:25:57 +11008132 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008133 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008134 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008135 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008136 /* Clear some bits that don't mean anything, but
8137 * might be left set
8138 */
NeilBrown24dd4692005-11-08 21:39:26 -08008139 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8140 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008141
NeilBrowned209582012-04-24 10:23:14 +10008142 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8143 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008144 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008145 /* no recovery is running.
8146 * remove any failed drives, then
8147 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008148 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149 * the personality to fail the re-add.
8150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008152 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008153 if (mddev->pers->check_reshape == NULL ||
8154 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008155 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008156 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008157 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008158 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008159 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008160 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8161 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008162 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008163 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008164 } else if (mddev->recovery_cp < MaxSector) {
8165 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008166 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008167 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8168 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008169 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008170
Linus Torvalds1da177e2005-04-16 15:20:36 -07008171 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008172 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008173 /* We are adding a device or devices to an array
8174 * which has the bitmap stored on all devices.
8175 * So make sure all bitmap pages get written
8176 */
8177 bitmap_write_all(mddev->bitmap);
8178 }
NeilBrownac05f252014-09-30 08:10:42 +10008179 INIT_WORK(&mddev->del_work, md_start_sync);
8180 queue_work(md_misc_wq, &mddev->del_work);
8181 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008182 }
NeilBrownac05f252014-09-30 08:10:42 +10008183 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008184 if (!mddev->sync_thread) {
8185 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008186 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008187 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8188 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008189 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008190 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008191 }
NeilBrownac05f252014-09-30 08:10:42 +10008192 unlock:
8193 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008194 mddev_unlock(mddev);
8195 }
8196}
NeilBrown6c144d32014-09-30 16:15:38 +10008197EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008198
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008199void md_reap_sync_thread(struct mddev *mddev)
8200{
8201 struct md_rdev *rdev;
8202
8203 /* resync has finished, collect result */
8204 md_unregister_thread(&mddev->sync_thread);
8205 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8206 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8207 /* success...*/
8208 /* activate any spares */
8209 if (mddev->pers->spare_active(mddev)) {
8210 sysfs_notify(&mddev->kobj, NULL,
8211 "degraded");
8212 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8213 }
8214 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008215 if (mddev_is_clustered(mddev))
8216 md_cluster_ops->metadata_update_start(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008217 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8218 mddev->pers->finish_reshape)
8219 mddev->pers->finish_reshape(mddev);
8220
8221 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008222 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008223 */
NeilBrownf4667222013-12-09 12:04:56 +11008224 if (!mddev->degraded)
8225 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008226 rdev->saved_raid_disk = -1;
8227
8228 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008229 if (mddev_is_clustered(mddev))
8230 md_cluster_ops->metadata_update_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008231 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8232 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8233 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8234 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8235 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008236 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008237 /* flag recovery needed just to double check */
8238 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8239 sysfs_notify_dirent_safe(mddev->sysfs_action);
8240 md_new_event(mddev);
8241 if (mddev->event_work.func)
8242 queue_work(md_misc_wq, &mddev->event_work);
8243}
NeilBrown6c144d32014-09-30 16:15:38 +10008244EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008245
NeilBrownfd01b882011-10-11 16:47:53 +11008246void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008247{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008248 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008249 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008250 !test_bit(Blocked, &rdev->flags) &&
8251 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008252 msecs_to_jiffies(5000));
8253 rdev_dec_pending(rdev, mddev);
8254}
8255EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8256
NeilBrownc6563a82012-05-21 09:27:00 +10008257void md_finish_reshape(struct mddev *mddev)
8258{
8259 /* called be personality module when reshape completes. */
8260 struct md_rdev *rdev;
8261
8262 rdev_for_each(rdev, mddev) {
8263 if (rdev->data_offset > rdev->new_data_offset)
8264 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8265 else
8266 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8267 rdev->data_offset = rdev->new_data_offset;
8268 }
8269}
8270EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008271
8272/* Bad block management.
8273 * We can record which blocks on each device are 'bad' and so just
8274 * fail those blocks, or that stripe, rather than the whole device.
8275 * Entries in the bad-block table are 64bits wide. This comprises:
8276 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8277 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8278 * A 'shift' can be set so that larger blocks are tracked and
8279 * consequently larger devices can be covered.
8280 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8281 *
8282 * Locking of the bad-block table uses a seqlock so md_is_badblock
8283 * might need to retry if it is very unlucky.
8284 * We will sometimes want to check for bad blocks in a bi_end_io function,
8285 * so we use the write_seqlock_irq variant.
8286 *
8287 * When looking for a bad block we specify a range and want to
8288 * know if any block in the range is bad. So we binary-search
8289 * to the last range that starts at-or-before the given endpoint,
8290 * (or "before the sector after the target range")
8291 * then see if it ends after the given start.
8292 * We return
8293 * 0 if there are no known bad blocks in the range
8294 * 1 if there are known bad block which are all acknowledged
8295 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8296 * plus the start/length of the first bad section we overlap.
8297 */
8298int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8299 sector_t *first_bad, int *bad_sectors)
8300{
8301 int hi;
majianpengab056132012-11-06 17:13:44 +08008302 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008303 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008304 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008305 sector_t target = s + sectors;
8306 unsigned seq;
8307
8308 if (bb->shift > 0) {
8309 /* round the start down, and the end up */
8310 s >>= bb->shift;
8311 target += (1<<bb->shift) - 1;
8312 target >>= bb->shift;
8313 sectors = target - s;
8314 }
8315 /* 'target' is now the first block after the bad range */
8316
8317retry:
8318 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008319 lo = 0;
8320 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008321 hi = bb->count;
8322
8323 /* Binary search between lo and hi for 'target'
8324 * i.e. for the last range that starts before 'target'
8325 */
8326 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8327 * are known not to be the last range before target.
8328 * VARIANT: hi-lo is the number of possible
8329 * ranges, and decreases until it reaches 1
8330 */
8331 while (hi - lo > 1) {
8332 int mid = (lo + hi) / 2;
8333 sector_t a = BB_OFFSET(p[mid]);
8334 if (a < target)
8335 /* This could still be the one, earlier ranges
8336 * could not. */
8337 lo = mid;
8338 else
8339 /* This and later ranges are definitely out. */
8340 hi = mid;
8341 }
8342 /* 'lo' might be the last that started before target, but 'hi' isn't */
8343 if (hi > lo) {
8344 /* need to check all range that end after 's' to see if
8345 * any are unacknowledged.
8346 */
8347 while (lo >= 0 &&
8348 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8349 if (BB_OFFSET(p[lo]) < target) {
8350 /* starts before the end, and finishes after
8351 * the start, so they must overlap
8352 */
8353 if (rv != -1 && BB_ACK(p[lo]))
8354 rv = 1;
8355 else
8356 rv = -1;
8357 *first_bad = BB_OFFSET(p[lo]);
8358 *bad_sectors = BB_LEN(p[lo]);
8359 }
8360 lo--;
8361 }
8362 }
8363
8364 if (read_seqretry(&bb->lock, seq))
8365 goto retry;
8366
8367 return rv;
8368}
8369EXPORT_SYMBOL_GPL(md_is_badblock);
8370
8371/*
8372 * Add a range of bad blocks to the table.
8373 * This might extend the table, or might contract it
8374 * if two adjacent ranges can be merged.
8375 * We binary-search to find the 'insertion' point, then
8376 * decide how best to handle it.
8377 */
8378static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8379 int acknowledged)
8380{
8381 u64 *p;
8382 int lo, hi;
8383 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008384 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008385
8386 if (bb->shift < 0)
8387 /* badblocks are disabled */
8388 return 0;
8389
8390 if (bb->shift) {
8391 /* round the start down, and the end up */
8392 sector_t next = s + sectors;
8393 s >>= bb->shift;
8394 next += (1<<bb->shift) - 1;
8395 next >>= bb->shift;
8396 sectors = next - s;
8397 }
8398
Bian Yu905b0292013-10-12 01:10:03 -04008399 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008400
8401 p = bb->page;
8402 lo = 0;
8403 hi = bb->count;
8404 /* Find the last range that starts at-or-before 's' */
8405 while (hi - lo > 1) {
8406 int mid = (lo + hi) / 2;
8407 sector_t a = BB_OFFSET(p[mid]);
8408 if (a <= s)
8409 lo = mid;
8410 else
8411 hi = mid;
8412 }
8413 if (hi > lo && BB_OFFSET(p[lo]) > s)
8414 hi = lo;
8415
8416 if (hi > lo) {
8417 /* we found a range that might merge with the start
8418 * of our new range
8419 */
8420 sector_t a = BB_OFFSET(p[lo]);
8421 sector_t e = a + BB_LEN(p[lo]);
8422 int ack = BB_ACK(p[lo]);
8423 if (e >= s) {
8424 /* Yes, we can merge with a previous range */
8425 if (s == a && s + sectors >= e)
8426 /* new range covers old */
8427 ack = acknowledged;
8428 else
8429 ack = ack && acknowledged;
8430
8431 if (e < s + sectors)
8432 e = s + sectors;
8433 if (e - a <= BB_MAX_LEN) {
8434 p[lo] = BB_MAKE(a, e-a, ack);
8435 s = e;
8436 } else {
8437 /* does not all fit in one range,
8438 * make p[lo] maximal
8439 */
8440 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8441 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8442 s = a + BB_MAX_LEN;
8443 }
8444 sectors = e - s;
8445 }
8446 }
8447 if (sectors && hi < bb->count) {
8448 /* 'hi' points to the first range that starts after 's'.
8449 * Maybe we can merge with the start of that range */
8450 sector_t a = BB_OFFSET(p[hi]);
8451 sector_t e = a + BB_LEN(p[hi]);
8452 int ack = BB_ACK(p[hi]);
8453 if (a <= s + sectors) {
8454 /* merging is possible */
8455 if (e <= s + sectors) {
8456 /* full overlap */
8457 e = s + sectors;
8458 ack = acknowledged;
8459 } else
8460 ack = ack && acknowledged;
8461
8462 a = s;
8463 if (e - a <= BB_MAX_LEN) {
8464 p[hi] = BB_MAKE(a, e-a, ack);
8465 s = e;
8466 } else {
8467 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8468 s = a + BB_MAX_LEN;
8469 }
8470 sectors = e - s;
8471 lo = hi;
8472 hi++;
8473 }
8474 }
8475 if (sectors == 0 && hi < bb->count) {
8476 /* we might be able to combine lo and hi */
8477 /* Note: 's' is at the end of 'lo' */
8478 sector_t a = BB_OFFSET(p[hi]);
8479 int lolen = BB_LEN(p[lo]);
8480 int hilen = BB_LEN(p[hi]);
8481 int newlen = lolen + hilen - (s - a);
8482 if (s >= a && newlen < BB_MAX_LEN) {
8483 /* yes, we can combine them */
8484 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8485 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8486 memmove(p + hi, p + hi + 1,
8487 (bb->count - hi - 1) * 8);
8488 bb->count--;
8489 }
8490 }
8491 while (sectors) {
8492 /* didn't merge (it all).
8493 * Need to add a range just before 'hi' */
8494 if (bb->count >= MD_MAX_BADBLOCKS) {
8495 /* No room for more */
8496 rv = 0;
8497 break;
8498 } else {
8499 int this_sectors = sectors;
8500 memmove(p + hi + 1, p + hi,
8501 (bb->count - hi) * 8);
8502 bb->count++;
8503
8504 if (this_sectors > BB_MAX_LEN)
8505 this_sectors = BB_MAX_LEN;
8506 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8507 sectors -= this_sectors;
8508 s += this_sectors;
8509 }
8510 }
8511
8512 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008513 if (!acknowledged)
8514 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008515 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008516
8517 return rv;
8518}
8519
NeilBrown3cb03002011-10-11 16:45:26 +11008520int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008521 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008522{
NeilBrownc6563a82012-05-21 09:27:00 +10008523 int rv;
8524 if (is_new)
8525 s += rdev->new_data_offset;
8526 else
8527 s += rdev->data_offset;
8528 rv = md_set_badblocks(&rdev->badblocks,
8529 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008530 if (rv) {
8531 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008532 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008533 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8534 md_wakeup_thread(rdev->mddev->thread);
8535 }
8536 return rv;
8537}
8538EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8539
8540/*
8541 * Remove a range of bad blocks from the table.
8542 * This may involve extending the table if we spilt a region,
8543 * but it must not fail. So if the table becomes full, we just
8544 * drop the remove request.
8545 */
8546static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8547{
8548 u64 *p;
8549 int lo, hi;
8550 sector_t target = s + sectors;
8551 int rv = 0;
8552
8553 if (bb->shift > 0) {
8554 /* When clearing we round the start up and the end down.
8555 * This should not matter as the shift should align with
8556 * the block size and no rounding should ever be needed.
8557 * However it is better the think a block is bad when it
8558 * isn't than to think a block is not bad when it is.
8559 */
8560 s += (1<<bb->shift) - 1;
8561 s >>= bb->shift;
8562 target >>= bb->shift;
8563 sectors = target - s;
8564 }
8565
8566 write_seqlock_irq(&bb->lock);
8567
8568 p = bb->page;
8569 lo = 0;
8570 hi = bb->count;
8571 /* Find the last range that starts before 'target' */
8572 while (hi - lo > 1) {
8573 int mid = (lo + hi) / 2;
8574 sector_t a = BB_OFFSET(p[mid]);
8575 if (a < target)
8576 lo = mid;
8577 else
8578 hi = mid;
8579 }
8580 if (hi > lo) {
8581 /* p[lo] is the last range that could overlap the
8582 * current range. Earlier ranges could also overlap,
8583 * but only this one can overlap the end of the range.
8584 */
8585 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8586 /* Partial overlap, leave the tail of this range */
8587 int ack = BB_ACK(p[lo]);
8588 sector_t a = BB_OFFSET(p[lo]);
8589 sector_t end = a + BB_LEN(p[lo]);
8590
8591 if (a < s) {
8592 /* we need to split this range */
8593 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008594 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008595 goto out;
8596 }
8597 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8598 bb->count++;
8599 p[lo] = BB_MAKE(a, s-a, ack);
8600 lo++;
8601 }
8602 p[lo] = BB_MAKE(target, end - target, ack);
8603 /* there is no longer an overlap */
8604 hi = lo;
8605 lo--;
8606 }
8607 while (lo >= 0 &&
8608 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8609 /* This range does overlap */
8610 if (BB_OFFSET(p[lo]) < s) {
8611 /* Keep the early parts of this range. */
8612 int ack = BB_ACK(p[lo]);
8613 sector_t start = BB_OFFSET(p[lo]);
8614 p[lo] = BB_MAKE(start, s - start, ack);
8615 /* now low doesn't overlap, so.. */
8616 break;
8617 }
8618 lo--;
8619 }
8620 /* 'lo' is strictly before, 'hi' is strictly after,
8621 * anything between needs to be discarded
8622 */
8623 if (hi - lo > 1) {
8624 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8625 bb->count -= (hi - lo - 1);
8626 }
8627 }
8628
8629 bb->changed = 1;
8630out:
8631 write_sequnlock_irq(&bb->lock);
8632 return rv;
8633}
8634
NeilBrownc6563a82012-05-21 09:27:00 +10008635int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8636 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008637{
NeilBrownc6563a82012-05-21 09:27:00 +10008638 if (is_new)
8639 s += rdev->new_data_offset;
8640 else
8641 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008642 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008643 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008644}
8645EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8646
8647/*
8648 * Acknowledge all bad blocks in a list.
8649 * This only succeeds if ->changed is clear. It is used by
8650 * in-kernel metadata updates
8651 */
8652void md_ack_all_badblocks(struct badblocks *bb)
8653{
8654 if (bb->page == NULL || bb->changed)
8655 /* no point even trying */
8656 return;
8657 write_seqlock_irq(&bb->lock);
8658
majianpengecb178b2012-03-19 12:46:42 +11008659 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008660 u64 *p = bb->page;
8661 int i;
8662 for (i = 0; i < bb->count ; i++) {
8663 if (!BB_ACK(p[i])) {
8664 sector_t start = BB_OFFSET(p[i]);
8665 int len = BB_LEN(p[i]);
8666 p[i] = BB_MAKE(start, len, 1);
8667 }
8668 }
NeilBrownde393cd2011-07-28 11:31:48 +10008669 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008670 }
8671 write_sequnlock_irq(&bb->lock);
8672}
8673EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8674
NeilBrown16c791a2011-07-28 11:31:47 +10008675/* sysfs access to bad-blocks list.
8676 * We present two files.
8677 * 'bad-blocks' lists sector numbers and lengths of ranges that
8678 * are recorded as bad. The list is truncated to fit within
8679 * the one-page limit of sysfs.
8680 * Writing "sector length" to this file adds an acknowledged
8681 * bad block list.
8682 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8683 * been acknowledged. Writing to this file adds bad blocks
8684 * without acknowledging them. This is largely for testing.
8685 */
8686
8687static ssize_t
8688badblocks_show(struct badblocks *bb, char *page, int unack)
8689{
8690 size_t len;
8691 int i;
8692 u64 *p = bb->page;
8693 unsigned seq;
8694
8695 if (bb->shift < 0)
8696 return 0;
8697
8698retry:
8699 seq = read_seqbegin(&bb->lock);
8700
8701 len = 0;
8702 i = 0;
8703
8704 while (len < PAGE_SIZE && i < bb->count) {
8705 sector_t s = BB_OFFSET(p[i]);
8706 unsigned int length = BB_LEN(p[i]);
8707 int ack = BB_ACK(p[i]);
8708 i++;
8709
8710 if (unack && ack)
8711 continue;
8712
8713 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8714 (unsigned long long)s << bb->shift,
8715 length << bb->shift);
8716 }
NeilBrownde393cd2011-07-28 11:31:48 +10008717 if (unack && len == 0)
8718 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008719
8720 if (read_seqretry(&bb->lock, seq))
8721 goto retry;
8722
8723 return len;
8724}
8725
8726#define DO_DEBUG 1
8727
8728static ssize_t
8729badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8730{
8731 unsigned long long sector;
8732 int length;
8733 char newline;
8734#ifdef DO_DEBUG
8735 /* Allow clearing via sysfs *only* for testing/debugging.
8736 * Normally only a successful write may clear a badblock
8737 */
8738 int clear = 0;
8739 if (page[0] == '-') {
8740 clear = 1;
8741 page++;
8742 }
8743#endif /* DO_DEBUG */
8744
8745 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8746 case 3:
8747 if (newline != '\n')
8748 return -EINVAL;
8749 case 2:
8750 if (length <= 0)
8751 return -EINVAL;
8752 break;
8753 default:
8754 return -EINVAL;
8755 }
8756
8757#ifdef DO_DEBUG
8758 if (clear) {
8759 md_clear_badblocks(bb, sector, length);
8760 return len;
8761 }
8762#endif /* DO_DEBUG */
8763 if (md_set_badblocks(bb, sector, length, !unack))
8764 return len;
8765 else
8766 return -ENOSPC;
8767}
8768
Adrian Bunk75c96f82005-05-05 16:16:09 -07008769static int md_notify_reboot(struct notifier_block *this,
8770 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008771{
8772 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008773 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008774 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008775
NeilBrownc744a652012-03-19 12:46:37 +11008776 for_each_mddev(mddev, tmp) {
8777 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008778 if (mddev->pers)
8779 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008780 if (mddev->persistent)
8781 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008782 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008783 }
NeilBrownc744a652012-03-19 12:46:37 +11008784 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008785 }
NeilBrownc744a652012-03-19 12:46:37 +11008786 /*
8787 * certain more exotic SCSI devices are known to be
8788 * volatile wrt too early system reboots. While the
8789 * right place to handle this issue is the given
8790 * driver, we do want to have a safe RAID driver ...
8791 */
8792 if (need_delay)
8793 mdelay(1000*1);
8794
Linus Torvalds1da177e2005-04-16 15:20:36 -07008795 return NOTIFY_DONE;
8796}
8797
Adrian Bunk75c96f82005-05-05 16:16:09 -07008798static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008799 .notifier_call = md_notify_reboot,
8800 .next = NULL,
8801 .priority = INT_MAX, /* before any real devices */
8802};
8803
8804static void md_geninit(void)
8805{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008806 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008807
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008808 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008809}
8810
Adrian Bunk75c96f82005-05-05 16:16:09 -07008811static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008812{
Tejun Heoe804ac72010-10-15 15:36:08 +02008813 int ret = -ENOMEM;
8814
Tejun Heoada609e2011-01-25 14:35:54 +01008815 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008816 if (!md_wq)
8817 goto err_wq;
8818
8819 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8820 if (!md_misc_wq)
8821 goto err_misc_wq;
8822
8823 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8824 goto err_md;
8825
8826 if ((ret = register_blkdev(0, "mdp")) < 0)
8827 goto err_mdp;
8828 mdp_major = ret;
8829
NeilBrownaf5628f2014-07-31 13:54:54 +10008830 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008831 md_probe, NULL, NULL);
8832 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008833 md_probe, NULL, NULL);
8834
Linus Torvalds1da177e2005-04-16 15:20:36 -07008835 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008836 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008837
8838 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008840
Tejun Heoe804ac72010-10-15 15:36:08 +02008841err_mdp:
8842 unregister_blkdev(MD_MAJOR, "md");
8843err_md:
8844 destroy_workqueue(md_misc_wq);
8845err_misc_wq:
8846 destroy_workqueue(md_wq);
8847err_wq:
8848 return ret;
8849}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008851void md_reload_sb(struct mddev *mddev)
8852{
8853 struct md_rdev *rdev, *tmp;
8854
8855 rdev_for_each_safe(rdev, tmp, mddev) {
8856 rdev->sb_loaded = 0;
8857 ClearPageUptodate(rdev->sb_page);
8858 }
8859 mddev->raid_disks = 0;
8860 analyze_sbs(mddev);
8861 rdev_for_each_safe(rdev, tmp, mddev) {
8862 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8863 /* since we don't write to faulty devices, we figure out if the
8864 * disk is faulty by comparing events
8865 */
8866 if (mddev->events > sb->events)
8867 set_bit(Faulty, &rdev->flags);
8868 }
8869
8870}
8871EXPORT_SYMBOL(md_reload_sb);
8872
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873#ifndef MODULE
8874
8875/*
8876 * Searches all registered partitions for autorun RAID arrays
8877 * at boot time.
8878 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008879
8880static LIST_HEAD(all_detected_devices);
8881struct detected_devices_node {
8882 struct list_head list;
8883 dev_t dev;
8884};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008885
8886void md_autodetect_dev(dev_t dev)
8887{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008888 struct detected_devices_node *node_detected_dev;
8889
8890 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8891 if (node_detected_dev) {
8892 node_detected_dev->dev = dev;
8893 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8894 } else {
8895 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8896 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008898}
8899
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900static void autostart_arrays(int part)
8901{
NeilBrown3cb03002011-10-11 16:45:26 +11008902 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008903 struct detected_devices_node *node_detected_dev;
8904 dev_t dev;
8905 int i_scanned, i_passed;
8906
8907 i_scanned = 0;
8908 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008909
8910 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8911
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008912 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8913 i_scanned++;
8914 node_detected_dev = list_entry(all_detected_devices.next,
8915 struct detected_devices_node, list);
8916 list_del(&node_detected_dev->list);
8917 dev = node_detected_dev->dev;
8918 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008919 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008920 if (IS_ERR(rdev))
8921 continue;
8922
NeilBrown403df472014-09-30 15:52:29 +10008923 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008924 continue;
NeilBrown403df472014-09-30 15:52:29 +10008925
NeilBrownd0fae182008-03-04 14:29:31 -08008926 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008927 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008928 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008929 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008930
8931 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8932 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008933
8934 autorun_devices(part);
8935}
8936
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008937#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008938
8939static __exit void md_exit(void)
8940{
NeilBrownfd01b882011-10-11 16:47:53 +11008941 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008942 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008943 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008944
NeilBrownaf5628f2014-07-31 13:54:54 +10008945 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008946 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008947
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008948 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008949 unregister_blkdev(mdp_major, "mdp");
8950 unregister_reboot_notifier(&md_notifier);
8951 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008952
8953 /* We cannot unload the modules while some process is
8954 * waiting for us in select() or poll() - wake them up
8955 */
8956 md_unloading = 1;
8957 while (waitqueue_active(&md_event_waiters)) {
8958 /* not safe to leave yet */
8959 wake_up(&md_event_waiters);
8960 msleep(delay);
8961 delay += delay;
8962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008963 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008964
NeilBrown29ac4aa2008-02-06 01:39:58 -08008965 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008966 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008967 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008968 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008969 destroy_workqueue(md_misc_wq);
8970 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008971}
8972
Dan Williams685784a2007-07-09 11:56:42 -07008973subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008974module_exit(md_exit)
8975
NeilBrownf91de922005-11-08 21:39:36 -08008976static int get_ro(char *buffer, struct kernel_param *kp)
8977{
8978 return sprintf(buffer, "%d", start_readonly);
8979}
8980static int set_ro(const char *val, struct kernel_param *kp)
8981{
8982 char *e;
8983 int num = simple_strtoul(val, &e, 10);
8984 if (*val && (*e == '\0' || *e == '\n')) {
8985 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008986 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008987 }
8988 return -EINVAL;
8989}
8990
NeilBrown80ca3a42006-07-10 04:44:18 -07008991module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8992module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11008993module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008994
Linus Torvalds1da177e2005-04-16 15:20:36 -07008995MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008996MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008997MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008998MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);