blob: 807095f4c793bb4f1d4119f81abf2653b83240cb [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 Viroff01bb482011-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 */
Jens Axboedece1632015-11-05 10:41:16 -0700253static blk_qc_t 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;
Gu Zheng74672d02015-04-03 08:44:47 +0800258 int cpu;
NeilBrown49077322010-03-25 16:20:56 +1100259
Kent Overstreet54efd502015-04-23 22:37:18 -0700260 blk_queue_split(q, &bio, q->bio_split);
261
NeilBrown0ca69882011-01-14 09:14:33 +1100262 if (mddev == NULL || mddev->pers == NULL
263 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100264 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700265 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100266 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100267 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200268 if (bio_sectors(bio) != 0)
269 bio->bi_error = -EROFS;
270 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700271 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100272 }
NeilBrown0ca69882011-01-14 09:14:33 +1100273 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100274 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200275 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100276 DEFINE_WAIT(__wait);
277 for (;;) {
278 prepare_to_wait(&mddev->sb_wait, &__wait,
279 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200280 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100281 break;
282 rcu_read_unlock();
283 schedule();
284 rcu_read_lock();
285 }
286 finish_wait(&mddev->sb_wait, &__wait);
287 }
288 atomic_inc(&mddev->active_io);
289 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100290
Chris Masone91ece52011-02-07 19:21:48 -0500291 /*
292 * save the sectors now since our bio can
293 * go away inside make_request
294 */
295 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200296 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100297
Gu Zheng74672d02015-04-03 08:44:47 +0800298 cpu = part_stat_lock();
299 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
300 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
301 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100302
NeilBrown409c57f2009-03-31 14:39:39 +1100303 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
304 wake_up(&mddev->sb_wait);
Jens Axboedece1632015-11-05 10:41:16 -0700305
306 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
NeilBrown9e35b992010-04-06 14:23:02 +1000309/* mddev_suspend makes sure no new requests are submitted
310 * to the device, and that any requests that have been submitted
311 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100312 * Once mddev_detach() is called and completes, the module will be
313 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000314 */
NeilBrownfd01b882011-10-11 16:47:53 +1100315void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100316{
317 BUG_ON(mddev->suspended);
318 mddev->suspended = 1;
319 synchronize_rcu();
320 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
321 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500322
323 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100324}
NeilBrown390ee602010-06-01 19:37:27 +1000325EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100326
NeilBrownfd01b882011-10-11 16:47:53 +1100327void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100328{
329 mddev->suspended = 0;
330 wake_up(&mddev->sb_wait);
331 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500332
Jonathan Brassow47525e52012-05-22 13:55:29 +1000333 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500334 md_wakeup_thread(mddev->thread);
335 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100336}
NeilBrown390ee602010-06-01 19:37:27 +1000337EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100338
NeilBrownfd01b882011-10-11 16:47:53 +1100339int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000340{
NeilBrown5c675f82014-12-15 12:56:56 +1100341 struct md_personality *pers = mddev->pers;
342 int ret = 0;
343
344 rcu_read_lock();
345 if (mddev->suspended)
346 ret = 1;
347 else if (pers && pers->congested)
348 ret = pers->congested(mddev, bits);
349 rcu_read_unlock();
350 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000351}
NeilBrown5c675f82014-12-15 12:56:56 +1100352EXPORT_SYMBOL_GPL(mddev_congested);
353static int md_congested(void *data, int bits)
354{
355 struct mddev *mddev = data;
356 return mddev_congested(mddev, bits);
357}
NeilBrown3fa841d2009-09-23 18:10:29 +1000358
NeilBrowna2826aa2009-12-14 12:49:49 +1100359/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200360 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100361 */
362
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200363static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100364{
NeilBrown3cb03002011-10-11 16:45:26 +1100365 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100366 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100367
368 rdev_dec_pending(rdev, mddev);
369
370 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200371 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200372 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100373 }
374 bio_put(bio);
375}
376
NeilBrowna7a07e62010-12-09 16:04:25 +1100377static void md_submit_flush_data(struct work_struct *ws);
378
NeilBrowna035fc32010-12-09 16:17:51 +1100379static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100380{
NeilBrownfd01b882011-10-11 16:47:53 +1100381 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100382 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100383
NeilBrowna7a07e62010-12-09 16:04:25 +1100384 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
385 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100386 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100387 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100388 if (rdev->raid_disk >= 0 &&
389 !test_bit(Faulty, &rdev->flags)) {
390 /* Take two references, one is dropped
391 * when request finishes, one after
392 * we reclaim rcu_read_lock
393 */
394 struct bio *bi;
395 atomic_inc(&rdev->nr_pending);
396 atomic_inc(&rdev->nr_pending);
397 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000398 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200399 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100400 bi->bi_private = rdev;
401 bi->bi_bdev = rdev->bdev;
402 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200403 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100404 rcu_read_lock();
405 rdev_dec_pending(rdev, mddev);
406 }
407 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100408 if (atomic_dec_and_test(&mddev->flush_pending))
409 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100410}
411
Tejun Heoe9c74692010-09-03 11:56:18 +0200412static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100413{
NeilBrownfd01b882011-10-11 16:47:53 +1100414 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200415 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100416
Kent Overstreet4f024f32013-10-11 15:44:27 -0700417 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100418 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200419 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100420 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200421 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200422 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100423 }
NeilBrown2b74e122010-12-09 15:59:01 +1100424
425 mddev->flush_bio = NULL;
426 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100427}
428
NeilBrownfd01b882011-10-11 16:47:53 +1100429void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100430{
NeilBrown85572d72014-12-15 12:56:56 +1100431 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100432 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200433 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100434 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200435 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100436 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100437
NeilBrowna035fc32010-12-09 16:17:51 +1100438 INIT_WORK(&mddev->flush_work, submit_flushes);
439 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100440}
Tejun Heoe9c74692010-09-03 11:56:18 +0200441EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100442
NeilBrown74018dc2012-07-31 09:08:15 +0200443void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000444{
NeilBrown9cbb1752012-07-31 09:08:14 +0200445 struct mddev *mddev = cb->data;
446 md_wakeup_thread(mddev->thread);
447 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000448}
NeilBrown9cbb1752012-07-31 09:08:14 +0200449EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000450
NeilBrownfd01b882011-10-11 16:47:53 +1100451static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 atomic_inc(&mddev->active);
454 return mddev;
455}
456
Dan Williams5fd3a172009-03-04 00:57:25 -0700457static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100458
NeilBrownfd01b882011-10-11 16:47:53 +1100459static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
NeilBrowna167f662010-10-26 18:31:13 +1100461 struct bio_set *bs = NULL;
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
464 return;
NeilBrownd3374822009-01-09 08:31:10 +1100465 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100466 mddev->ctime == 0 && !mddev->hold_active) {
467 /* Array is not configured at all, and not held active,
468 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100469 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100470 bs = mddev->bio_set;
471 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100472 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200473 /* We did a probe so need to clean up. Call
474 * queue_work inside the spinlock so that
475 * flush_workqueue() after mddev_find will
476 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100477 */
478 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200479 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100480 } else
481 kfree(mddev);
482 }
483 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100484 if (bs)
485 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Sasha Levin25b2edf2015-07-24 18:19:58 -0400488static void md_safemode_timeout(unsigned long data);
489
NeilBrownfd01b882011-10-11 16:47:53 +1100490void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100491{
492 mutex_init(&mddev->open_mutex);
493 mutex_init(&mddev->reconfig_mutex);
494 mutex_init(&mddev->bitmap_info.mutex);
495 INIT_LIST_HEAD(&mddev->disks);
496 INIT_LIST_HEAD(&mddev->all_mddevs);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400497 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
498 (unsigned long) mddev);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100499 atomic_set(&mddev->active, 1);
500 atomic_set(&mddev->openers, 0);
501 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100502 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100503 atomic_set(&mddev->flush_pending, 0);
504 init_waitqueue_head(&mddev->sb_wait);
505 init_waitqueue_head(&mddev->recovery_wait);
506 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000507 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500508 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100509 mddev->resync_min = 0;
510 mddev->resync_max = MaxSector;
511 mddev->level = LEVEL_NONE;
512}
NeilBrown390ee602010-06-01 19:37:27 +1000513EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100514
NeilBrownf72ffdd2014-09-30 14:23:59 +1000515static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
NeilBrownfd01b882011-10-11 16:47:53 +1100517 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
NeilBrown8f5f02c2011-02-16 13:58:51 +1100519 if (unit && MAJOR(unit) != MD_MAJOR)
520 unit &= ~((1<<MdpMinorShift)-1);
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 retry:
523 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
NeilBrownefeb53c2009-01-09 08:31:10 +1100525 if (unit) {
526 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
527 if (mddev->unit == unit) {
528 mddev_get(mddev);
529 spin_unlock(&all_mddevs_lock);
530 kfree(new);
531 return mddev;
532 }
533
534 if (new) {
535 list_add(&new->all_mddevs, &all_mddevs);
536 spin_unlock(&all_mddevs_lock);
537 new->hold_active = UNTIL_IOCTL;
538 return new;
539 }
540 } else if (new) {
541 /* find an unused unit number */
542 static int next_minor = 512;
543 int start = next_minor;
544 int is_free = 0;
545 int dev = 0;
546 while (!is_free) {
547 dev = MKDEV(MD_MAJOR, next_minor);
548 next_minor++;
549 if (next_minor > MINORMASK)
550 next_minor = 0;
551 if (next_minor == start) {
552 /* Oh dear, all in use. */
553 spin_unlock(&all_mddevs_lock);
554 kfree(new);
555 return NULL;
556 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000557
NeilBrownefeb53c2009-01-09 08:31:10 +1100558 is_free = 1;
559 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
560 if (mddev->unit == dev) {
561 is_free = 0;
562 break;
563 }
564 }
565 new->unit = dev;
566 new->md_minor = MINOR(dev);
567 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 list_add(&new->all_mddevs, &all_mddevs);
569 spin_unlock(&all_mddevs_lock);
570 return new;
571 }
572 spin_unlock(&all_mddevs_lock);
573
NeilBrown9ffae0c2006-01-06 00:20:32 -0800574 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (!new)
576 return NULL;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 new->unit = unit;
579 if (MAJOR(unit) == MD_MAJOR)
580 new->md_minor = MINOR(unit);
581 else
582 new->md_minor = MINOR(unit) >> MdpMinorShift;
583
NeilBrownfafd7fb2010-04-01 15:55:30 +1100584 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto retry;
587}
588
NeilBrownb6eb1272010-04-15 10:13:47 +1000589static struct attribute_group md_redundancy_group;
590
NeilBrown5c47daf2014-12-15 12:57:01 +1100591void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
NeilBrowna64c8762010-04-14 17:15:37 +1000593 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000594 /* These cannot be removed under reconfig_mutex as
595 * an access to the files will try to take reconfig_mutex
596 * while holding the file unremovable, which leads to
597 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000598 * So hold set sysfs_active while the remove in happeing,
599 * and anything else which might set ->to_remove or my
600 * otherwise change the sysfs namespace will fail with
601 * -EBUSY if sysfs_active is still set.
602 * We set sysfs_active under reconfig_mutex and elsewhere
603 * test it under the same mutex to ensure its correct value
604 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000605 */
NeilBrowna64c8762010-04-14 17:15:37 +1000606 struct attribute_group *to_remove = mddev->to_remove;
607 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000608 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000609 mutex_unlock(&mddev->reconfig_mutex);
610
NeilBrown00bcb4a2010-06-01 19:37:23 +1000611 if (mddev->kobj.sd) {
612 if (to_remove != &md_redundancy_group)
613 sysfs_remove_group(&mddev->kobj, to_remove);
614 if (mddev->pers == NULL ||
615 mddev->pers->sync_request == NULL) {
616 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
617 if (mddev->sysfs_action)
618 sysfs_put(mddev->sysfs_action);
619 mddev->sysfs_action = NULL;
620 }
NeilBrowna64c8762010-04-14 17:15:37 +1000621 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000622 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000623 } else
624 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Chris Dunlop751e67c2011-10-19 16:48:26 +1100626 /* As we've dropped the mutex we need a spinlock to
627 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000628 */
629 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700630 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000631 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
NeilBrown5c47daf2014-12-15 12:57:01 +1100633EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500635struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100636{
637 struct md_rdev *rdev;
638
639 rdev_for_each_rcu(rdev, mddev)
640 if (rdev->desc_nr == nr)
641 return rdev;
642
643 return NULL;
644}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500645EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100646
647static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
NeilBrown3cb03002011-10-11 16:45:26 +1100649 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
NeilBrowndafb20f2012-03-19 12:46:39 +1100651 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (rdev->bdev->bd_dev == dev)
653 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return NULL;
656}
657
NeilBrown1ca69c42012-10-11 13:37:33 +1100658static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
659{
660 struct md_rdev *rdev;
661
662 rdev_for_each_rcu(rdev, mddev)
663 if (rdev->bdev->bd_dev == dev)
664 return rdev;
665
666 return NULL;
667}
668
NeilBrown84fc4b52011-10-11 16:49:58 +1100669static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800670{
NeilBrown84fc4b52011-10-11 16:49:58 +1100671 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800672 list_for_each_entry(pers, &pers_list, list) {
673 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800674 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800675 if (strcmp(pers->name, clevel)==0)
676 return pers;
677 }
NeilBrown2604b702006-01-06 00:20:36 -0800678 return NULL;
679}
680
Andre Nollb73df2d2008-07-11 22:02:23 +1000681/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100682static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100684 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000685 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
NeilBrownf72ffdd2014-09-30 14:23:59 +1000688static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 rdev->sb_page = alloc_page(GFP_KERNEL);
691 if (!rdev->sb_page) {
692 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000693 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695
696 return 0;
697}
698
NeilBrown545c8792012-05-22 13:54:30 +1000699void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800702 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 rdev->sb_loaded = 0;
704 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000705 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100706 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
NeilBrown2699b672011-07-28 11:31:47 +1000708 if (rdev->bb_page) {
709 put_page(rdev->bb_page);
710 rdev->bb_page = NULL;
711 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000712 kfree(rdev->badblocks.page);
713 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
NeilBrown545c8792012-05-22 13:54:30 +1000715EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200717static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700718{
NeilBrown3cb03002011-10-11 16:45:26 +1100719 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100720 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700721
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200722 if (bio->bi_error) {
723 printk("md: super_written gets error=%d\n", bio->bi_error);
NeilBrowna9701a32005-11-08 21:39:34 -0800724 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700725 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700726
NeilBrowna9701a32005-11-08 21:39:34 -0800727 if (atomic_dec_and_test(&mddev->pending_writes))
728 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700729 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700730}
731
NeilBrownfd01b882011-10-11 16:47:53 +1100732void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700733 sector_t sector, int size, struct page *page)
734{
735 /* write first size bytes of page to sector of rdev
736 * Increment mddev->pending_writes before returning
737 * and decrement it on completion, waking up sb_wait
738 * if zero is reached.
739 * If an error occurred, call md_error
740 */
NeilBrowna167f662010-10-26 18:31:13 +1100741 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700742
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100743 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700744 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700745 bio_add_page(bio, page, size, 0);
746 bio->bi_private = rdev;
747 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800748
NeilBrown7bfa19f2005-06-21 17:17:28 -0700749 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000750 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800751}
752
NeilBrownfd01b882011-10-11 16:47:53 +1100753void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800754{
Tejun Heoe9c74692010-09-03 11:56:18 +0200755 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000756 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700757}
758
NeilBrown3cb03002011-10-11 16:45:26 +1100759int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100760 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
NeilBrowna167f662010-10-26 18:31:13 +1100762 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 int ret;
764
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100765 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
766 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100767 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700768 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000769 else if (rdev->mddev->reshape_position != MaxSector &&
770 (rdev->mddev->reshape_backwards ==
771 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700772 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100773 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700774 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700776 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200778 ret = !bio->bi_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 bio_put(bio);
780 return ret;
781}
NeilBrowna8745db2006-01-06 00:20:34 -0800782EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
NeilBrownf72ffdd2014-09-30 14:23:59 +1000784static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (rdev->sb_loaded)
789 return 0;
790
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100791 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 goto fail;
793 rdev->sb_loaded = 1;
794 return 0;
795
796fail:
797 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
798 bdevname(rdev->bdev,b));
799 return -EINVAL;
800}
801
802static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
803{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000804 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000805 sb1->set_uuid1 == sb2->set_uuid1 &&
806 sb1->set_uuid2 == sb2->set_uuid2 &&
807 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
811{
812 int ret;
813 mdp_super_t *tmp1, *tmp2;
814
815 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
816 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
817
818 if (!tmp1 || !tmp2) {
819 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100820 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 goto abort;
822 }
823
824 *tmp1 = *sb1;
825 *tmp2 = *sb2;
826
827 /*
828 * nr_disks is not constant
829 */
830 tmp1->nr_disks = 0;
831 tmp2->nr_disks = 0;
832
Andre Nollce0c8e02008-07-11 22:02:20 +1000833 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700835 kfree(tmp1);
836 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return ret;
838}
839
NeilBrown4d167f02007-05-09 02:35:37 -0700840static u32 md_csum_fold(u32 csum)
841{
842 csum = (csum & 0xffff) + (csum >> 16);
843 return (csum & 0xffff) + (csum >> 16);
844}
845
NeilBrownf72ffdd2014-09-30 14:23:59 +1000846static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
NeilBrown4d167f02007-05-09 02:35:37 -0700848 u64 newcsum = 0;
849 u32 *sb32 = (u32*)sb;
850 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 unsigned int disk_csum, csum;
852
853 disk_csum = sb->sb_csum;
854 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700855
856 for (i = 0; i < MD_SB_BYTES/4 ; i++)
857 newcsum += sb32[i];
858 csum = (newcsum & 0xffffffff) + (newcsum>>32);
859
NeilBrown4d167f02007-05-09 02:35:37 -0700860#ifdef CONFIG_ALPHA
861 /* This used to use csum_partial, which was wrong for several
862 * reasons including that different results are returned on
863 * different architectures. It isn't critical that we get exactly
864 * the same return value as before (we always csum_fold before
865 * testing, and that removes any differences). However as we
866 * know that csum_partial always returned a 16bit value on
867 * alphas, do a fold to maximise conformity to previous behaviour.
868 */
869 sb->sb_csum = md_csum_fold(disk_csum);
870#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700872#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return csum;
874}
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876/*
877 * Handle superblock details.
878 * We want to be able to handle multiple superblock formats
879 * so we have a common interface to them all, and an array of
880 * different handlers.
881 * We rely on user-space to write the initial superblock, and support
882 * reading and updating of superblocks.
883 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100884 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 * loads and validates a superblock on dev.
886 * if refdev != NULL, compare superblocks on both devices
887 * Return:
888 * 0 - dev has a superblock that is compatible with refdev
889 * 1 - dev has a superblock that is compatible and newer than refdev
890 * so dev should be used as the refdev in future
891 * -EINVAL superblock incompatible or invalid
892 * -othererror e.g. -EIO
893 *
NeilBrownfd01b882011-10-11 16:47:53 +1100894 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 * Verify that dev is acceptable into mddev.
896 * The first time, mddev->raid_disks will be 0, and data from
897 * dev should be merged in. Subsequent calls check that dev
898 * is new enough. Return 0 or -EINVAL
899 *
NeilBrownfd01b882011-10-11 16:47:53 +1100900 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 * Update the superblock for rdev with data in mddev
902 * This does not write to disc.
903 *
904 */
905
906struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000907 char *name;
908 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000909 int (*load_super)(struct md_rdev *rdev,
910 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000911 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000912 int (*validate_super)(struct mddev *mddev,
913 struct md_rdev *rdev);
914 void (*sync_super)(struct mddev *mddev,
915 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100916 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000917 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000918 int (*allow_new_offset)(struct md_rdev *rdev,
919 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920};
921
922/*
Andre Noll0894cc32009-06-18 08:49:23 +1000923 * Check that the given mddev has no bitmap.
924 *
925 * This function is called from the run method of all personalities that do not
926 * support bitmaps. It prints an error message and returns non-zero if mddev
927 * has a bitmap. Otherwise, it returns 0.
928 *
929 */
NeilBrownfd01b882011-10-11 16:47:53 +1100930int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000931{
NeilBrownc3d97142009-12-14 12:49:52 +1100932 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000933 return 0;
934 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
935 mdname(mddev), mddev->pers->name);
936 return 1;
937}
938EXPORT_SYMBOL(md_check_no_bitmap);
939
940/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000941 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 */
NeilBrown3cb03002011-10-11 16:45:26 +1100943static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
946 mdp_super_t *sb;
947 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 /*
Andre Noll0f420352008-07-11 22:02:23 +1000950 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 * it's at the end of the disk.
952 *
953 * It also happens to be a multiple of 4Kb.
954 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100955 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
NeilBrown0002b272005-09-09 16:23:53 -0700957 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (ret) return ret;
959
960 ret = -EINVAL;
961
962 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000963 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 if (sb->md_magic != MD_SB_MAGIC) {
966 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
967 b);
968 goto abort;
969 }
970
971 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800972 sb->minor_version < 90 ||
973 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
975 sb->major_version, sb->minor_version,
976 b);
977 goto abort;
978 }
979
980 if (sb->raid_disks <= 0)
981 goto abort;
982
NeilBrown4d167f02007-05-09 02:35:37 -0700983 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
985 b);
986 goto abort;
987 }
988
989 rdev->preferred_minor = sb->md_minor;
990 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +1000991 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700992 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +1000993 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 if (sb->level == LEVEL_MULTIPATH)
996 rdev->desc_nr = -1;
997 else
998 rdev->desc_nr = sb->this_disk.number;
999
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001000 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001002 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001004 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (!uuid_equal(refsb, sb)) {
1006 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1007 b, bdevname(refdev->bdev,b2));
1008 goto abort;
1009 }
1010 if (!sb_equal(refsb, sb)) {
1011 printk(KERN_WARNING "md: %s has same UUID"
1012 " but different superblock to %s\n",
1013 b, bdevname(refdev->bdev, b2));
1014 goto abort;
1015 }
1016 ev1 = md_event(sb);
1017 ev2 = md_event(refsb);
1018 if (ev1 > ev2)
1019 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001020 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 ret = 0;
1022 }
NeilBrown8190e752009-06-18 08:48:58 +10001023 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001024 /* Limit to 4TB as metadata cannot record more than that.
1025 * (not needed for Linear and RAID0 as metadata doesn't
1026 * record this size)
1027 */
1028 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001029 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
NeilBrown27a7b262011-09-10 17:21:28 +10001031 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001032 /* "this cannot possibly happen" ... */
1033 ret = -EINVAL;
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 abort:
1036 return ret;
1037}
1038
1039/*
1040 * validate_super for 0.90.0
1041 */
NeilBrownfd01b882011-10-11 16:47:53 +11001042static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001045 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001046 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
NeilBrown41158c72005-06-21 17:17:25 -07001048 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001049 clear_bit(Faulty, &rdev->flags);
1050 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001051 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001052 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (mddev->raid_disks == 0) {
1055 mddev->major_version = 0;
1056 mddev->minor_version = sb->minor_version;
1057 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001058 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001059 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 mddev->ctime = sb->ctime;
1061 mddev->utime = sb->utime;
1062 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001063 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 mddev->layout = sb->layout;
1065 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001066 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001067 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001068 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001069 mddev->bitmap_info.space = 0;
1070 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001071 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001072 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001073 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
NeilBrownf6705572006-03-27 01:18:11 -08001075 if (mddev->minor_version >= 91) {
1076 mddev->reshape_position = sb->reshape_position;
1077 mddev->delta_disks = sb->delta_disks;
1078 mddev->new_level = sb->new_level;
1079 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001080 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001081 if (mddev->delta_disks < 0)
1082 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001083 } else {
1084 mddev->reshape_position = MaxSector;
1085 mddev->delta_disks = 0;
1086 mddev->new_level = mddev->level;
1087 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001088 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001089 }
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (sb->state & (1<<MD_SB_CLEAN))
1092 mddev->recovery_cp = MaxSector;
1093 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001094 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 sb->events_lo == sb->cp_events_lo) {
1096 mddev->recovery_cp = sb->recovery_cp;
1097 } else
1098 mddev->recovery_cp = 0;
1099 }
1100
1101 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1102 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1103 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1104 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1105
1106 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001107
1108 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001109 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001110 mddev->bitmap_info.offset =
1111 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001112 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001113 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001114 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001115
NeilBrown41158c72005-06-21 17:17:25 -07001116 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001117 /* Insist on good event counter while assembling, except
1118 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001120 if (sb->disks[rdev->desc_nr].state & (
1121 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001122 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001123 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001124 } else if (mddev->bitmap) {
1125 /* if adding to array with a bitmap, then we can accept an
1126 * older device ... but not too old.
1127 */
NeilBrown41158c72005-06-21 17:17:25 -07001128 if (ev1 < mddev->bitmap->events_cleared)
1129 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001130 if (ev1 < mddev->events)
1131 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001132 } else {
1133 if (ev1 < mddev->events)
1134 /* just a hot-add of a new device, leave raid_disk at -1 */
1135 return 0;
1136 }
NeilBrown41158c72005-06-21 17:17:25 -07001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 desc = sb->disks + rdev->desc_nr;
1140
1141 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001142 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001143 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1144 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001145 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001147 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001148 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1149 /* active but not in sync implies recovery up to
1150 * reshape position. We don't know exactly where
1151 * that is, so set to zero for now */
1152 if (mddev->minor_version >= 91) {
1153 rdev->recovery_offset = 0;
1154 rdev->raid_disk = desc->raid_disk;
1155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001157 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1158 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001159 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001160 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return 0;
1162}
1163
1164/*
1165 * sync_super for 0.90.0
1166 */
NeilBrownfd01b882011-10-11 16:47:53 +11001167static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001170 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /* make rdev->sb match mddev data..
1174 *
1175 * 1/ zero out disks
1176 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1177 * 3/ any empty disks < next_spare become removed
1178 *
1179 * disks[0] gets initialised to REMOVED because
1180 * we cannot be sure from other fields if it has
1181 * been initialised or not.
1182 */
1183 int i;
1184 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1185
NeilBrown61181562005-09-09 16:24:02 -07001186 rdev->sb_size = MD_SB_BYTES;
1187
Namhyung Kim65a06f062011-07-27 11:00:36 +10001188 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 memset(sb, 0, sizeof(*sb));
1191
1192 sb->md_magic = MD_SB_MAGIC;
1193 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 sb->patch_version = mddev->patch_version;
1195 sb->gvalid_words = 0; /* ignored */
1196 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1197 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1198 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1199 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1200
1201 sb->ctime = mddev->ctime;
1202 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001203 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 sb->raid_disks = mddev->raid_disks;
1205 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001206 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 sb->utime = mddev->utime;
1208 sb->state = 0;
1209 sb->events_hi = (mddev->events>>32);
1210 sb->events_lo = (u32)mddev->events;
1211
NeilBrownf6705572006-03-27 01:18:11 -08001212 if (mddev->reshape_position == MaxSector)
1213 sb->minor_version = 90;
1214 else {
1215 sb->minor_version = 91;
1216 sb->reshape_position = mddev->reshape_position;
1217 sb->new_level = mddev->new_level;
1218 sb->delta_disks = mddev->delta_disks;
1219 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001220 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001221 }
1222 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (mddev->in_sync)
1224 {
1225 sb->recovery_cp = mddev->recovery_cp;
1226 sb->cp_events_hi = (mddev->events>>32);
1227 sb->cp_events_lo = (u32)mddev->events;
1228 if (mddev->recovery_cp == MaxSector)
1229 sb->state = (1<< MD_SB_CLEAN);
1230 } else
1231 sb->recovery_cp = 0;
1232
1233 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001234 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
NeilBrownc3d97142009-12-14 12:49:52 +11001236 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001237 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001240 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001242 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001243 int is_active = test_bit(In_sync, &rdev2->flags);
1244
1245 if (rdev2->raid_disk >= 0 &&
1246 sb->minor_version >= 91)
1247 /* we have nowhere to store the recovery_offset,
1248 * but if it is not below the reshape_position,
1249 * we can piggy-back on that.
1250 */
1251 is_active = 1;
1252 if (rdev2->raid_disk < 0 ||
1253 test_bit(Faulty, &rdev2->flags))
1254 is_active = 0;
1255 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001256 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001258 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001259 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 d = &sb->disks[rdev2->desc_nr];
1261 nr_disks++;
1262 d->number = rdev2->desc_nr;
1263 d->major = MAJOR(rdev2->bdev->bd_dev);
1264 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001265 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 d->raid_disk = rdev2->raid_disk;
1267 else
1268 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001269 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001271 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001273 if (test_bit(In_sync, &rdev2->flags))
1274 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 active++;
1276 working++;
1277 } else {
1278 d->state = 0;
1279 spare++;
1280 working++;
1281 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001282 if (test_bit(WriteMostly, &rdev2->flags))
1283 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /* now set the "removed" and "faulty" bits on any missing devices */
1286 for (i=0 ; i < mddev->raid_disks ; i++) {
1287 mdp_disk_t *d = &sb->disks[i];
1288 if (d->state == 0 && d->number == 0) {
1289 d->number = i;
1290 d->raid_disk = i;
1291 d->state = (1<<MD_DISK_REMOVED);
1292 d->state |= (1<<MD_DISK_FAULTY);
1293 failed++;
1294 }
1295 }
1296 sb->nr_disks = nr_disks;
1297 sb->active_disks = active;
1298 sb->working_disks = working;
1299 sb->failed_disks = failed;
1300 sb->spare_disks = spare;
1301
1302 sb->this_disk = sb->disks[rdev->desc_nr];
1303 sb->sb_csum = calc_sb_csum(sb);
1304}
1305
1306/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001307 * rdev_size_change for 0.90.0
1308 */
1309static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001310super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001311{
Andre Noll58c0fed2009-03-31 14:33:13 +11001312 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001313 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001314 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001315 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001316 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001317 if (!num_sectors || num_sectors > rdev->sb_start)
1318 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001319 /* Limit to 4TB as metadata cannot record more than that.
1320 * 4TB == 2^32 KB, or 2*2^32 sectors.
1321 */
NeilBrown667a5312012-08-16 16:46:12 +10001322 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001323 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001324 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001325 rdev->sb_page);
1326 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001327 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001328}
1329
NeilBrownc6563a82012-05-21 09:27:00 +10001330static int
1331super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1332{
1333 /* non-zero offset changes not possible with v0.90 */
1334 return new_offset == 0;
1335}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001336
1337/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 * version 1 superblock
1339 */
1340
NeilBrownf72ffdd2014-09-30 14:23:59 +10001341static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001343 __le32 disk_csum;
1344 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 unsigned long long newcsum;
1346 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001347 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 disk_csum = sb->sb_csum;
1350 sb->sb_csum = 0;
1351 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001352 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 newcsum += le32_to_cpu(*isuper++);
1354
1355 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001356 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1359 sb->sb_csum = disk_csum;
1360 return cpu_to_le32(csum);
1361}
1362
NeilBrown2699b672011-07-28 11:31:47 +10001363static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1364 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001365static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 struct mdp_superblock_1 *sb;
1368 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001369 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001370 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001372 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 /*
Andre Noll0f420352008-07-11 22:02:23 +10001375 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 * It is always aligned to a 4K boundary and
1377 * depeding on minor_version, it can be:
1378 * 0: At least 8K, but less than 12K, from end of device
1379 * 1: At start of device
1380 * 2: 4K from start of device.
1381 */
1382 switch(minor_version) {
1383 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001384 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001385 sb_start -= 8*2;
1386 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 break;
1388 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001389 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 break;
1391 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001392 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 break;
1394 default:
1395 return -EINVAL;
1396 }
Andre Noll0f420352008-07-11 22:02:23 +10001397 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
NeilBrown0002b272005-09-09 16:23:53 -07001399 /* superblock is rarely larger than 1K, but it can be larger,
1400 * and it is safe to read 4k, so we do that
1401 */
1402 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (ret) return ret;
1404
Namhyung Kim65a06f062011-07-27 11:00:36 +10001405 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1408 sb->major_version != cpu_to_le32(1) ||
1409 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001410 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001411 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return -EINVAL;
1413
1414 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1415 printk("md: invalid superblock checksum on %s\n",
1416 bdevname(rdev->bdev,b));
1417 return -EINVAL;
1418 }
1419 if (le64_to_cpu(sb->data_size) < 10) {
1420 printk("md: data_size too small on %s\n",
1421 bdevname(rdev->bdev,b));
1422 return -EINVAL;
1423 }
NeilBrownc6563a82012-05-21 09:27:00 +10001424 if (sb->pad0 ||
1425 sb->pad3[0] ||
1426 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1427 /* Some padding is non-zero, might be a new feature */
1428 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 rdev->preferred_minor = 0xffff;
1431 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001432 rdev->new_data_offset = rdev->data_offset;
1433 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1434 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1435 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001436 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
NeilBrown0002b272005-09-09 16:23:53 -07001438 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001439 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001440 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001441 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1442
1443 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001444 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001445 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001446 if (minor_version
1447 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1448 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001449
NeilBrown31b65a02006-07-10 04:44:14 -07001450 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1451 rdev->desc_nr = -1;
1452 else
1453 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1454
NeilBrown2699b672011-07-28 11:31:47 +10001455 if (!rdev->bb_page) {
1456 rdev->bb_page = alloc_page(GFP_KERNEL);
1457 if (!rdev->bb_page)
1458 return -ENOMEM;
1459 }
1460 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1461 rdev->badblocks.count == 0) {
1462 /* need to load the bad block list.
1463 * Currently we limit it to one page.
1464 */
1465 s32 offset;
1466 sector_t bb_sector;
1467 u64 *bbp;
1468 int i;
1469 int sectors = le16_to_cpu(sb->bblog_size);
1470 if (sectors > (PAGE_SIZE / 512))
1471 return -EINVAL;
1472 offset = le32_to_cpu(sb->bblog_offset);
1473 if (offset == 0)
1474 return -EINVAL;
1475 bb_sector = (long long)offset;
1476 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1477 rdev->bb_page, READ, true))
1478 return -EIO;
1479 bbp = (u64 *)page_address(rdev->bb_page);
1480 rdev->badblocks.shift = sb->bblog_shift;
1481 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1482 u64 bb = le64_to_cpu(*bbp);
1483 int count = bb & (0x3ff);
1484 u64 sector = bb >> 10;
1485 sector <<= sb->bblog_shift;
1486 count <<= sb->bblog_shift;
1487 if (bb + 1 == 0)
1488 break;
1489 if (md_set_badblocks(&rdev->badblocks,
1490 sector, count, 1) == 0)
1491 return -EINVAL;
1492 }
NeilBrown486adf72013-04-24 11:42:44 +10001493 } else if (sb->bblog_offset != 0)
1494 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001495
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001496 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001497 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001498 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001500 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1503 sb->level != refsb->level ||
1504 sb->layout != refsb->layout ||
1505 sb->chunksize != refsb->chunksize) {
1506 printk(KERN_WARNING "md: %s has strangely different"
1507 " superblock to %s\n",
1508 bdevname(rdev->bdev,b),
1509 bdevname(refdev->bdev,b2));
1510 return -EINVAL;
1511 }
1512 ev1 = le64_to_cpu(sb->events);
1513 ev2 = le64_to_cpu(refsb->events);
1514
1515 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001516 ret = 1;
1517 else
1518 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
NeilBrownc6563a82012-05-21 09:27:00 +10001520 if (minor_version) {
1521 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1522 sectors -= rdev->data_offset;
1523 } else
1524 sectors = rdev->sb_start;
1525 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001527 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001528 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
NeilBrownfd01b882011-10-11 16:47:53 +11001531static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001533 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001534 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
NeilBrown41158c72005-06-21 17:17:25 -07001536 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001537 clear_bit(Faulty, &rdev->flags);
1538 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001539 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001540 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (mddev->raid_disks == 0) {
1543 mddev->major_version = 1;
1544 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001545 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001546 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1548 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1549 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001550 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 mddev->layout = le32_to_cpu(sb->layout);
1552 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001553 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001554 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001555 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001556 mddev->bitmap_info.space = 0;
1557 /* Default location for bitmap is 1K after superblock
1558 * using 3K - total of 4K
1559 */
NeilBrownc3d97142009-12-14 12:49:52 +11001560 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001561 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001562 mddev->reshape_backwards = 0;
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1565 memcpy(mddev->uuid, sb->set_uuid, 16);
1566
1567 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001568
NeilBrown71c08052005-09-09 16:23:51 -07001569 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001570 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001571 mddev->bitmap_info.offset =
1572 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001573 /* Metadata doesn't record how much space is available.
1574 * For 1.0, we assume we can use up to the superblock
1575 * if before, else to 4K beyond superblock.
1576 * For others, assume no change is possible.
1577 */
1578 if (mddev->minor_version > 0)
1579 mddev->bitmap_info.space = 0;
1580 else if (mddev->bitmap_info.offset > 0)
1581 mddev->bitmap_info.space =
1582 8 - mddev->bitmap_info.offset;
1583 else
1584 mddev->bitmap_info.space =
1585 -mddev->bitmap_info.offset;
1586 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001587
NeilBrownf6705572006-03-27 01:18:11 -08001588 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1589 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1590 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1591 mddev->new_level = le32_to_cpu(sb->new_level);
1592 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001593 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001594 if (mddev->delta_disks < 0 ||
1595 (mddev->delta_disks == 0 &&
1596 (le32_to_cpu(sb->feature_map)
1597 & MD_FEATURE_RESHAPE_BACKWARDS)))
1598 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001599 } else {
1600 mddev->reshape_position = MaxSector;
1601 mddev->delta_disks = 0;
1602 mddev->new_level = mddev->level;
1603 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001604 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001605 }
1606
NeilBrown41158c72005-06-21 17:17:25 -07001607 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001608 /* Insist of good event counter while assembling, except for
1609 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001611 if (rdev->desc_nr >= 0 &&
1612 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001613 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1614 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001615 if (ev1 < mddev->events)
1616 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001617 } else if (mddev->bitmap) {
1618 /* If adding to array with a bitmap, then we can accept an
1619 * older device, but not too old.
1620 */
NeilBrown41158c72005-06-21 17:17:25 -07001621 if (ev1 < mddev->bitmap->events_cleared)
1622 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001623 if (ev1 < mddev->events)
1624 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001625 } else {
1626 if (ev1 < mddev->events)
1627 /* just a hot-add of a new device, leave raid_disk at -1 */
1628 return 0;
1629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if (mddev->level != LEVEL_MULTIPATH) {
1631 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001632 if (rdev->desc_nr < 0 ||
1633 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001634 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001635 rdev->desc_nr = -1;
1636 } else
1637 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001639 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001641 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001642 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 break;
Song Liubac624f2015-08-13 14:31:55 -07001644 case MD_DISK_ROLE_JOURNAL: /* journal device */
1645 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1646 /* journal device without journal feature */
1647 printk(KERN_WARNING
1648 "md: journal device provided without journal feature, ignoring the device\n");
1649 return -EINVAL;
1650 }
1651 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001652 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Libd18f642015-09-02 13:49:50 -07001653 if (mddev->recovery_cp == MaxSector)
1654 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07001655 rdev->raid_disk = mddev->raid_disks;
Song Liubac624f2015-08-13 14:31:55 -07001656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 default:
NeilBrownf4667222013-12-09 12:04:56 +11001658 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001659 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001660 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001661 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001662 if (!(le32_to_cpu(sb->feature_map) &
1663 MD_FEATURE_RECOVERY_BITMAP))
1664 rdev->saved_raid_disk = -1;
1665 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001666 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 rdev->raid_disk = role;
1668 break;
1669 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001670 if (sb->devflags & WriteMostly1)
1671 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001672 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1673 set_bit(Replacement, &rdev->flags);
Song Liua97b7892015-10-08 21:54:09 -07001674 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
1675 set_bit(MD_HAS_JOURNAL, &mddev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001676 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001677 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return 0;
1680}
1681
NeilBrownfd01b882011-10-11 16:47:53 +11001682static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
1684 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001685 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 int max_dev, i;
1687 /* make rdev->sb match mddev and rdev data. */
1688
Namhyung Kim65a06f062011-07-27 11:00:36 +10001689 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 sb->feature_map = 0;
1692 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001693 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 memset(sb->pad3, 0, sizeof(sb->pad3));
1695
1696 sb->utime = cpu_to_le64((__u64)mddev->utime);
1697 sb->events = cpu_to_le64(mddev->events);
1698 if (mddev->in_sync)
1699 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001700 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1701 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 else
1703 sb->resync_offset = cpu_to_le64(0);
1704
NeilBrown1c05b4b2006-10-21 10:24:08 -07001705 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001706
NeilBrownf0ca3402006-02-02 14:28:04 -08001707 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001708 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001709 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001710 sb->level = cpu_to_le32(mddev->level);
1711 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001712
NeilBrownaeb9b212011-08-25 14:43:08 +10001713 if (test_bit(WriteMostly, &rdev->flags))
1714 sb->devflags |= WriteMostly1;
1715 else
1716 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001717 sb->data_offset = cpu_to_le64(rdev->data_offset);
1718 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001719
NeilBrownc3d97142009-12-14 12:49:52 +11001720 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1721 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001722 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001723 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001724
Shaohua Lif2076e72015-10-08 21:54:12 -07001725 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001726 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001727 sb->feature_map |=
1728 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1729 sb->recovery_offset =
1730 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001731 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1732 sb->feature_map |=
1733 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001734 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001735 /* Note: recovery_offset and journal_tail share space */
1736 if (test_bit(Journal, &rdev->flags))
1737 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001738 if (test_bit(Replacement, &rdev->flags))
1739 sb->feature_map |=
1740 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001741
NeilBrownf6705572006-03-27 01:18:11 -08001742 if (mddev->reshape_position != MaxSector) {
1743 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1744 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1745 sb->new_layout = cpu_to_le32(mddev->new_layout);
1746 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1747 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001748 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001749 if (mddev->delta_disks == 0 &&
1750 mddev->reshape_backwards)
1751 sb->feature_map
1752 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001753 if (rdev->new_data_offset != rdev->data_offset) {
1754 sb->feature_map
1755 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1756 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1757 - rdev->data_offset));
1758 }
NeilBrownf6705572006-03-27 01:18:11 -08001759 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001760
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001761 if (mddev_is_clustered(mddev))
1762 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1763
NeilBrown2699b672011-07-28 11:31:47 +10001764 if (rdev->badblocks.count == 0)
1765 /* Nothing to do for bad blocks*/ ;
1766 else if (sb->bblog_offset == 0)
1767 /* Cannot record bad blocks on this device */
1768 md_error(mddev, rdev);
1769 else {
1770 struct badblocks *bb = &rdev->badblocks;
1771 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1772 u64 *p = bb->page;
1773 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1774 if (bb->changed) {
1775 unsigned seq;
1776
1777retry:
1778 seq = read_seqbegin(&bb->lock);
1779
1780 memset(bbp, 0xff, PAGE_SIZE);
1781
1782 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001783 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001784 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1785 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001786 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001787 }
NeilBrownd0962932012-03-19 12:46:41 +11001788 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001789 if (read_seqretry(&bb->lock, seq))
1790 goto retry;
1791
1792 bb->sector = (rdev->sb_start +
1793 (int)le32_to_cpu(sb->bblog_offset));
1794 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001795 }
1796 }
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001799 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (rdev2->desc_nr+1 > max_dev)
1801 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001802
NeilBrown70471da2009-08-03 10:59:57 +10001803 if (max_dev > le32_to_cpu(sb->max_dev)) {
1804 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001805 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001806 rdev->sb_size = max_dev * 2 + 256;
1807 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1808 if (rdev->sb_size & bmask)
1809 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001810 } else
1811 max_dev = le32_to_cpu(sb->max_dev);
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 for (i=0; i<max_dev;i++)
Song Liuc4d4c912015-08-13 14:31:54 -07001814 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001815
Song Liua97b7892015-10-08 21:54:09 -07001816 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1817 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
NeilBrowndafb20f2012-03-19 12:46:39 +11001819 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001821 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001822 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001823 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001825 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001826 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001827 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001828 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 else
Song Liuc4d4c912015-08-13 14:31:54 -07001830 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 sb->sb_csum = calc_sb_1_csum(sb);
1834}
1835
Chris Webb0cd17fe2008-06-28 08:31:46 +10001836static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001837super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001838{
1839 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001840 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001841 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001842 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001843 if (rdev->data_offset != rdev->new_data_offset)
1844 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001845 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001846 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001847 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001848 max_sectors -= rdev->data_offset;
1849 if (!num_sectors || num_sectors > max_sectors)
1850 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001851 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001852 /* minor version 0 with bitmap we can't move */
1853 return 0;
1854 } else {
1855 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001856 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001857 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001858 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001859 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001860 if (!num_sectors || num_sectors > max_sectors)
1861 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001862 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001863 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001864 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001865 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001866 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001867 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001868 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001869 rdev->sb_page);
1870 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001871 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001872
1873}
1874
1875static int
1876super_1_allow_new_offset(struct md_rdev *rdev,
1877 unsigned long long new_offset)
1878{
1879 /* All necessary checks on new >= old have been done */
1880 struct bitmap *bitmap;
1881 if (new_offset >= rdev->data_offset)
1882 return 1;
1883
1884 /* with 1.0 metadata, there is no metadata to tread on
1885 * so we can always move back */
1886 if (rdev->mddev->minor_version == 0)
1887 return 1;
1888
1889 /* otherwise we must be sure not to step on
1890 * any metadata, so stay:
1891 * 36K beyond start of superblock
1892 * beyond end of badblocks
1893 * beyond write-intent bitmap
1894 */
1895 if (rdev->sb_start + (32+4)*2 > new_offset)
1896 return 0;
1897 bitmap = rdev->mddev->bitmap;
1898 if (bitmap && !rdev->mddev->bitmap_info.file &&
1899 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001900 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001901 return 0;
1902 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1903 return 0;
1904
1905 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001906}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Adrian Bunk75c96f82005-05-05 16:16:09 -07001908static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 [0] = {
1910 .name = "0.90.0",
1911 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001912 .load_super = super_90_load,
1913 .validate_super = super_90_validate,
1914 .sync_super = super_90_sync,
1915 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001916 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 },
1918 [1] = {
1919 .name = "md-1",
1920 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001921 .load_super = super_1_load,
1922 .validate_super = super_1_validate,
1923 .sync_super = super_1_sync,
1924 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001925 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 },
1927};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
NeilBrownfd01b882011-10-11 16:47:53 +11001929static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001930{
1931 if (mddev->sync_super) {
1932 mddev->sync_super(mddev, rdev);
1933 return;
1934 }
1935
1936 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1937
1938 super_types[mddev->major_version].sync_super(mddev, rdev);
1939}
1940
NeilBrownfd01b882011-10-11 16:47:53 +11001941static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
NeilBrown3cb03002011-10-11 16:45:26 +11001943 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
NeilBrown4b809912008-07-21 17:05:25 +10001945 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07001946 rdev_for_each_rcu(rdev, mddev1) {
1947 if (test_bit(Faulty, &rdev->flags) ||
1948 test_bit(Journal, &rdev->flags) ||
1949 rdev->raid_disk == -1)
1950 continue;
1951 rdev_for_each_rcu(rdev2, mddev2) {
1952 if (test_bit(Faulty, &rdev2->flags) ||
1953 test_bit(Journal, &rdev2->flags) ||
1954 rdev2->raid_disk == -1)
1955 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001956 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001957 rdev2->bdev->bd_contains) {
1958 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001959 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001960 }
Song Liu0b020e82015-09-03 23:00:35 -07001961 }
1962 }
NeilBrown4b809912008-07-21 17:05:25 +10001963 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return 0;
1965}
1966
1967static LIST_HEAD(pending_raid_disks);
1968
Andre Nollac5e7112009-08-03 10:59:47 +10001969/*
1970 * Try to register data integrity profile for an mddev
1971 *
1972 * This is called when an array is started and after a disk has been kicked
1973 * from the array. It only succeeds if all working and active component devices
1974 * are integrity capable with matching profiles.
1975 */
NeilBrownfd01b882011-10-11 16:47:53 +11001976int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001977{
NeilBrown3cb03002011-10-11 16:45:26 +11001978 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001979
Andre Nollac5e7112009-08-03 10:59:47 +10001980 if (list_empty(&mddev->disks))
1981 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001982 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1983 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001984 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001985 /* skip spares and non-functional disks */
1986 if (test_bit(Faulty, &rdev->flags))
1987 continue;
1988 if (rdev->raid_disk < 0)
1989 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001990 if (!reference) {
1991 /* Use the first rdev as the reference */
1992 reference = rdev;
1993 continue;
1994 }
1995 /* does this rdev's profile match the reference profile? */
1996 if (blk_integrity_compare(reference->bdev->bd_disk,
1997 rdev->bdev->bd_disk) < 0)
1998 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001999 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002000 if (!reference || !bdev_get_integrity(reference->bdev))
2001 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002002 /*
2003 * All component devices are integrity capable and have matching
2004 * profiles, register the common profile for the md device.
2005 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002006 blk_integrity_register(mddev->gendisk,
2007 bdev_get_integrity(reference->bdev));
2008
Martin K. Petersena91a2782011-03-17 11:11:05 +01002009 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2010 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2011 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2012 mdname(mddev));
2013 return -EINVAL;
2014 }
Andre Nollac5e7112009-08-03 10:59:47 +10002015 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002016}
Andre Nollac5e7112009-08-03 10:59:47 +10002017EXPORT_SYMBOL(md_integrity_register);
2018
2019/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002020void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002021{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002022 struct blk_integrity *bi_rdev;
2023 struct blk_integrity *bi_mddev;
2024
2025 if (!mddev->gendisk)
2026 return;
2027
2028 bi_rdev = bdev_get_integrity(rdev->bdev);
2029 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002030
2031 if (!bi_mddev) /* nothing to do */
2032 return;
2033 if (rdev->raid_disk < 0) /* skip spares */
2034 return;
2035 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2036 rdev->bdev->bd_disk) >= 0)
2037 return;
Dan Williamsc7bfced2015-10-21 13:20:02 -04002038 WARN_ON_ONCE(!mddev->suspended);
Andre Nollac5e7112009-08-03 10:59:47 +10002039 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2040 blk_integrity_unregister(mddev->gendisk);
2041}
2042EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002043
NeilBrownf72ffdd2014-09-30 14:23:59 +10002044static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002046 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002047 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002048 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Dan Williams11e2ede2008-04-30 00:52:32 -07002050 /* prevent duplicates */
2051 if (find_rdev(mddev, rdev->bdev->bd_dev))
2052 return -EEXIST;
2053
Andre Nolldd8ac332009-03-31 14:33:13 +11002054 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2055 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2056 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002057 if (mddev->pers) {
2058 /* Cannot change size, so fail
2059 * If mddev->level <= 0, then we don't care
2060 * about aligning sizes (e.g. linear)
2061 */
2062 if (mddev->level > 0)
2063 return -ENOSPC;
2064 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002065 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 /* Verify rdev->desc_nr is unique.
2069 * If it is -1, assign a free number, else
2070 * check number is not in use
2071 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002072 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (rdev->desc_nr < 0) {
2074 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002075 if (mddev->pers)
2076 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002077 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 choice++;
2079 rdev->desc_nr = choice;
2080 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002081 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002082 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002086 rcu_read_unlock();
NeilBrownde01dfa2009-02-06 18:02:46 +11002087 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2088 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2089 mdname(mddev), mddev->max_disks);
2090 return -EBUSY;
2091 }
NeilBrown19133a42005-11-08 21:39:35 -08002092 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002093 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002096 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002097
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002098 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002099 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002100
Tejun Heo0762b8b2008-08-25 19:56:12 +09002101 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002102 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2103 /* failure here is OK */;
2104 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002105
NeilBrown4b809912008-07-21 17:05:25 +10002106 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002107 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002108
2109 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002110 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002113
2114 fail:
2115 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2116 b, mdname(mddev));
2117 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
NeilBrown177a99b2008-02-06 01:39:56 -08002120static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002121{
NeilBrown3cb03002011-10-11 16:45:26 +11002122 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002123 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002124 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002125}
2126
NeilBrownf72ffdd2014-09-30 14:23:59 +10002127static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
2129 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002130
Tejun Heo49731ba2011-01-14 18:43:57 +01002131 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002132 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2134 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002135 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002136 sysfs_put(rdev->sysfs_state);
2137 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002138 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002139 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002140 * writing to 'remove' to "dev/state". We also need
2141 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002142 */
NeilBrown4b809912008-07-21 17:05:25 +10002143 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002144 INIT_WORK(&rdev->del_work, md_delayed_delete);
2145 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002146 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
2149/*
2150 * prevent the device from being mounted, repartitioned or
2151 * otherwise reused by a RAID array (or any other kernel
2152 * subsystem), by bd_claiming the device.
2153 */
NeilBrown3cb03002011-10-11 16:45:26 +11002154static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
2156 int err = 0;
2157 struct block_device *bdev;
2158 char b[BDEVNAME_SIZE];
2159
Tejun Heod4d77622010-11-13 11:55:18 +01002160 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002161 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (IS_ERR(bdev)) {
2163 printk(KERN_ERR "md: could not open %s.\n",
2164 __bdevname(dev, b));
2165 return PTR_ERR(bdev);
2166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 rdev->bdev = bdev;
2168 return err;
2169}
2170
NeilBrown3cb03002011-10-11 16:45:26 +11002171static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
2173 struct block_device *bdev = rdev->bdev;
2174 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002175 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
2178void md_autodetect_dev(dev_t dev);
2179
NeilBrownf72ffdd2014-09-30 14:23:59 +10002180static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 printk(KERN_INFO "md: export_rdev(%s)\n",
2185 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002186 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002188 if (test_bit(AutoDetected, &rdev->flags))
2189 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190#endif
2191 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002192 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002195void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
2197 unbind_rdev_from_array(rdev);
2198 export_rdev(rdev);
2199}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002200EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
NeilBrownfd01b882011-10-11 16:47:53 +11002202static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
NeilBrown0638bb02014-09-25 17:43:47 +10002204 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
NeilBrown0638bb02014-09-25 17:43:47 +10002206 while (!list_empty(&mddev->disks)) {
2207 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2208 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002209 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 mddev->raid_disks = 0;
2212 mddev->major_version = 0;
2213}
2214
NeilBrownf72ffdd2014-09-30 14:23:59 +10002215static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
NeilBrown42543762006-06-26 00:27:57 -07002217 /* Update each superblock (in-memory image), but
2218 * if we are allowed to, skip spares which already
2219 * have the right event counter, or have one earlier
2220 * (which would mean they aren't being marked as dirty
2221 * with the rest of the array)
2222 */
NeilBrown3cb03002011-10-11 16:45:26 +11002223 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002224 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002225 if (rdev->sb_events == mddev->events ||
2226 (nospares &&
2227 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002228 rdev->sb_events+1 == mddev->events)) {
2229 /* Don't update this superblock */
2230 rdev->sb_loaded = 2;
2231 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002232 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002233 rdev->sb_loaded = 1;
2234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236}
2237
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002238static bool does_sb_need_changing(struct mddev *mddev)
2239{
2240 struct md_rdev *rdev;
2241 struct mdp_superblock_1 *sb;
2242 int role;
2243
2244 /* Find a good rdev */
2245 rdev_for_each(rdev, mddev)
2246 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2247 break;
2248
2249 /* No good device found. */
2250 if (!rdev)
2251 return false;
2252
2253 sb = page_address(rdev->sb_page);
2254 /* Check if a device has become faulty or a spare become active */
2255 rdev_for_each(rdev, mddev) {
2256 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2257 /* Device activated? */
2258 if (role == 0xffff && rdev->raid_disk >=0 &&
2259 !test_bit(Faulty, &rdev->flags))
2260 return true;
2261 /* Device turned faulty? */
2262 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2263 return true;
2264 }
2265
2266 /* Check if any mddev parameters have changed */
2267 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2268 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002269 (mddev->layout != le64_to_cpu(sb->layout)) ||
2270 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2271 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2272 return true;
2273
2274 return false;
2275}
2276
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002277void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
NeilBrown3cb03002011-10-11 16:45:26 +11002279 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002280 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002281 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002282 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002283 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
NeilBrownd87f0642013-04-24 11:42:40 +10002285 if (mddev->ro) {
2286 if (force_change)
2287 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2288 return;
2289 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002290
2291 if (mddev_is_clustered(mddev)) {
2292 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2293 force_change = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002294 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002295 /* Has someone else has updated the sb */
2296 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002297 if (ret == 0)
2298 md_cluster_ops->metadata_update_cancel(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002299 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2300 return;
2301 }
2302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002304 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002305 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002306 if (rdev->raid_disk >= 0 &&
2307 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002308 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002309 !test_bit(In_sync, &rdev->flags) &&
2310 mddev->curr_resync_completed > rdev->recovery_offset)
2311 rdev->recovery_offset = mddev->curr_resync_completed;
2312
NeilBrownf72ffdd2014-09-30 14:23:59 +10002313 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002314 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002315 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002316 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002317 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002318 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002319 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002320 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002321 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002322 md_ack_all_badblocks(&rdev->badblocks);
2323 md_error(mddev, rdev);
2324 }
2325 clear_bit(Blocked, &rdev->flags);
2326 clear_bit(BlockedBadBlocks, &rdev->flags);
2327 wake_up(&rdev->blocked_wait);
2328 }
2329 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002330 wake_up(&mddev->sb_wait);
2331 return;
2332 }
2333
NeilBrown85572d72014-12-15 12:56:56 +11002334 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002335
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002336 mddev->utime = get_seconds();
2337
NeilBrown850b2b42006-10-03 01:15:46 -07002338 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2339 force_change = 1;
2340 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2341 /* just a clean<-> dirty transition, possibly leave spares alone,
2342 * though if events isn't the right even/odd, we will have to do
2343 * spares after all
2344 */
2345 nospares = 1;
2346 if (force_change)
2347 nospares = 0;
2348 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002349 /* If the array is degraded, then skipping spares is both
2350 * dangerous and fairly pointless.
2351 * Dangerous because a device that was removed from the array
2352 * might have a event_count that still looks up-to-date,
2353 * so it can be re-added without a resync.
2354 * Pointless because if there are any spares to skip,
2355 * then a recovery will happen and soon that array won't
2356 * be degraded any more and the spare can go back to sleep then.
2357 */
NeilBrown850b2b42006-10-03 01:15:46 -07002358 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002359
NeilBrown06d91a52005-06-21 17:17:12 -07002360 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002361
2362 /* If this is just a dirty<->clean transition, and the array is clean
2363 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002364 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002365 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002366 && mddev->can_decrease_events
2367 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002368 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002369 mddev->can_decrease_events = 0;
2370 } else {
NeilBrown42543762006-06-26 00:27:57 -07002371 /* otherwise we have to go forward and ... */
2372 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002373 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
NeilBrown403df472014-09-30 15:52:29 +10002376 /*
2377 * This 64-bit counter should never wrap.
2378 * Either we are in around ~1 trillion A.C., assuming
2379 * 1 reboot per second, or we have a bug...
2380 */
2381 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002382
NeilBrowndafb20f2012-03-19 12:46:39 +11002383 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002384 if (rdev->badblocks.changed)
2385 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002386 if (test_bit(Faulty, &rdev->flags))
2387 set_bit(FaultRecorded, &rdev->flags);
2388 }
NeilBrown2699b672011-07-28 11:31:47 +10002389
NeilBrowne6910632008-02-06 01:39:51 -08002390 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002391 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
NeilBrown36a4e1f2011-10-07 14:23:17 +11002393 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2394 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
NeilBrown4ad13662007-07-17 04:06:13 -07002396 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002397 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002399
NeilBrown42543762006-06-26 00:27:57 -07002400 if (rdev->sb_loaded != 1)
2401 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
NeilBrownf4667222013-12-09 12:04:56 +11002403 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002404 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002405 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002406 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002407 pr_debug("md: (write) %s's sb offset: %llu\n",
2408 bdevname(rdev->bdev, b),
2409 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002410 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002411 if (rdev->badblocks.size) {
2412 md_super_write(mddev, rdev,
2413 rdev->badblocks.sector,
2414 rdev->badblocks.size << 9,
2415 rdev->bb_page);
2416 rdev->badblocks.size = 0;
2417 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002418
NeilBrownf4667222013-12-09 12:04:56 +11002419 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002420 pr_debug("md: %s (skipping faulty)\n",
2421 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002422
NeilBrown7bfa19f2005-06-21 17:17:28 -07002423 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 /* only need to write one superblock... */
2425 break;
2426 }
NeilBrowna9701a32005-11-08 21:39:34 -08002427 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002428 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002429
NeilBrown85572d72014-12-15 12:56:56 +11002430 spin_lock(&mddev->lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002431 if (mddev->in_sync != sync_req ||
2432 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002433 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002434 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002435 goto repeat;
2436 }
NeilBrown850b2b42006-10-03 01:15:46 -07002437 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002438 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002439 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002440 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2441 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002442
NeilBrowndafb20f2012-03-19 12:46:39 +11002443 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002444 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2445 clear_bit(Blocked, &rdev->flags);
2446
2447 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002448 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002449 clear_bit(BlockedBadBlocks, &rdev->flags);
2450 wake_up(&rdev->blocked_wait);
2451 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002452
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002453 if (mddev_is_clustered(mddev) && ret == 0)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002454 md_cluster_ops->metadata_update_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002456EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002458static int add_bound_rdev(struct md_rdev *rdev)
2459{
2460 struct mddev *mddev = rdev->mddev;
2461 int err = 0;
2462
2463 if (!mddev->pers->hot_remove_disk) {
2464 /* If there is hot_add_disk but no hot_remove_disk
2465 * then added disks for geometry changes,
2466 * and should be added immediately.
2467 */
2468 super_types[mddev->major_version].
2469 validate_super(mddev, rdev);
2470 err = mddev->pers->hot_add_disk(mddev, rdev);
2471 if (err) {
2472 unbind_rdev_from_array(rdev);
2473 export_rdev(rdev);
2474 return err;
2475 }
2476 }
2477 sysfs_notify_dirent_safe(rdev->sysfs_state);
2478
2479 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2480 if (mddev->degraded)
2481 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2482 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2483 md_new_event(mddev);
2484 md_wakeup_thread(mddev->thread);
2485 return 0;
2486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Andre Noll7f6ce762008-03-23 18:34:54 +01002488/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002489 * We want to accept with case. For this we use cmd_match.
2490 */
2491static int cmd_match(const char *cmd, const char *str)
2492{
2493 /* See if cmd, written into a sysfs file, matches
2494 * str. They must either be the same, or cmd can
2495 * have a trailing newline
2496 */
2497 while (*cmd && *str && *cmd == *str) {
2498 cmd++;
2499 str++;
2500 }
2501 if (*cmd == '\n')
2502 cmd++;
2503 if (*str || *cmd)
2504 return 0;
2505 return 1;
2506}
2507
NeilBrown86e6ffd2005-11-08 21:39:24 -08002508struct rdev_sysfs_entry {
2509 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002510 ssize_t (*show)(struct md_rdev *, char *);
2511 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002512};
2513
2514static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002515state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002516{
2517 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002518 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002519 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002520
NeilBrown758bfc82014-12-15 12:56:59 +11002521 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002522 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002523 len+= sprintf(page+len, "%sfaulty",sep);
2524 sep = ",";
2525 }
NeilBrown758bfc82014-12-15 12:56:59 +11002526 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002527 len += sprintf(page+len, "%sin_sync",sep);
2528 sep = ",";
2529 }
Shaohua Liac6096e2015-10-04 09:20:11 -07002530 if (test_bit(Journal, &flags)) {
2531 len += sprintf(page+len, "%sjournal",sep);
2532 sep = ",";
2533 }
NeilBrown758bfc82014-12-15 12:56:59 +11002534 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002535 len += sprintf(page+len, "%swrite_mostly",sep);
2536 sep = ",";
2537 }
NeilBrown758bfc82014-12-15 12:56:59 +11002538 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002539 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002540 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002541 len += sprintf(page+len, "%sblocked", sep);
2542 sep = ",";
2543 }
NeilBrown758bfc82014-12-15 12:56:59 +11002544 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002545 !test_bit(Journal, &flags) &&
NeilBrown758bfc82014-12-15 12:56:59 +11002546 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002547 len += sprintf(page+len, "%sspare", sep);
2548 sep = ",";
2549 }
NeilBrown758bfc82014-12-15 12:56:59 +11002550 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002551 len += sprintf(page+len, "%swrite_error", sep);
2552 sep = ",";
2553 }
NeilBrown758bfc82014-12-15 12:56:59 +11002554 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002555 len += sprintf(page+len, "%swant_replacement", sep);
2556 sep = ",";
2557 }
NeilBrown758bfc82014-12-15 12:56:59 +11002558 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002559 len += sprintf(page+len, "%sreplacement", sep);
2560 sep = ",";
2561 }
2562
NeilBrown86e6ffd2005-11-08 21:39:24 -08002563 return len+sprintf(page+len, "\n");
2564}
2565
NeilBrown45dc2de2006-06-26 00:27:58 -07002566static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002567state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002568{
2569 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002570 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002571 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002572 * writemostly - sets write_mostly
2573 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002574 * blocked - sets the Blocked flags
2575 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002576 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002577 * -insync - clear Insync for a device with a slot assigned,
2578 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002579 * write_error - sets WriteErrorSeen
2580 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002581 */
2582 int err = -EINVAL;
2583 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2584 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002585 if (test_bit(Faulty, &rdev->flags))
2586 err = 0;
2587 else
2588 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002589 } else if (cmd_match(buf, "remove")) {
2590 if (rdev->raid_disk >= 0)
2591 err = -EBUSY;
2592 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002593 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002594 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002595 if (mddev_is_clustered(mddev))
2596 err = md_cluster_ops->remove_disk(mddev, rdev);
2597
2598 if (err == 0) {
2599 md_kick_rdev_from_array(rdev);
2600 if (mddev->pers)
2601 md_update_sb(mddev, 1);
2602 md_new_event(mddev);
2603 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002604 }
NeilBrownf6556752006-06-26 00:28:01 -07002605 } else if (cmd_match(buf, "writemostly")) {
2606 set_bit(WriteMostly, &rdev->flags);
2607 err = 0;
2608 } else if (cmd_match(buf, "-writemostly")) {
2609 clear_bit(WriteMostly, &rdev->flags);
2610 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002611 } else if (cmd_match(buf, "blocked")) {
2612 set_bit(Blocked, &rdev->flags);
2613 err = 0;
2614 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002615 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002616 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002617 /* metadata handler doesn't understand badblocks,
2618 * so we need to fail the device
2619 */
2620 md_error(rdev->mddev, rdev);
2621 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002622 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002623 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002624 wake_up(&rdev->blocked_wait);
2625 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2626 md_wakeup_thread(rdev->mddev->thread);
2627
2628 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002629 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2630 set_bit(In_sync, &rdev->flags);
2631 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002632 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2633 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002634 if (rdev->mddev->pers == NULL) {
2635 clear_bit(In_sync, &rdev->flags);
2636 rdev->saved_raid_disk = rdev->raid_disk;
2637 rdev->raid_disk = -1;
2638 err = 0;
2639 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002640 } else if (cmd_match(buf, "write_error")) {
2641 set_bit(WriteErrorSeen, &rdev->flags);
2642 err = 0;
2643 } else if (cmd_match(buf, "-write_error")) {
2644 clear_bit(WriteErrorSeen, &rdev->flags);
2645 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002646 } else if (cmd_match(buf, "want_replacement")) {
2647 /* Any non-spare device that is not a replacement can
2648 * become want_replacement at any time, but we then need to
2649 * check if recovery is needed.
2650 */
2651 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002652 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002653 !test_bit(Replacement, &rdev->flags))
2654 set_bit(WantReplacement, &rdev->flags);
2655 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2656 md_wakeup_thread(rdev->mddev->thread);
2657 err = 0;
2658 } else if (cmd_match(buf, "-want_replacement")) {
2659 /* Clearing 'want_replacement' is always allowed.
2660 * Once replacements starts it is too late though.
2661 */
2662 err = 0;
2663 clear_bit(WantReplacement, &rdev->flags);
2664 } else if (cmd_match(buf, "replacement")) {
2665 /* Can only set a device as a replacement when array has not
2666 * yet been started. Once running, replacement is automatic
2667 * from spares, or by assigning 'slot'.
2668 */
2669 if (rdev->mddev->pers)
2670 err = -EBUSY;
2671 else {
2672 set_bit(Replacement, &rdev->flags);
2673 err = 0;
2674 }
2675 } else if (cmd_match(buf, "-replacement")) {
2676 /* Similarly, can only clear Replacement before start */
2677 if (rdev->mddev->pers)
2678 err = -EBUSY;
2679 else {
2680 clear_bit(Replacement, &rdev->flags);
2681 err = 0;
2682 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002683 } else if (cmd_match(buf, "re-add")) {
2684 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002685 /* clear_bit is performed _after_ all the devices
2686 * have their local Faulty bit cleared. If any writes
2687 * happen in the meantime in the local node, they
2688 * will land in the local bitmap, which will be synced
2689 * by this node eventually
2690 */
2691 if (!mddev_is_clustered(rdev->mddev) ||
2692 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2693 clear_bit(Faulty, &rdev->flags);
2694 err = add_bound_rdev(rdev);
2695 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002696 } else
2697 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002698 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002699 if (!err)
2700 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002701 return err ? err : len;
2702}
NeilBrown80ca3a42006-07-10 04:44:18 -07002703static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002704__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002705
2706static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002707errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002708{
2709 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2710}
2711
2712static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002713errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002714{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002715 unsigned int n;
2716 int rv;
2717
2718 rv = kstrtouint(buf, 10, &n);
2719 if (rv < 0)
2720 return rv;
2721 atomic_set(&rdev->corrected_errors, n);
2722 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002723}
2724static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002725__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002726
NeilBrown014236d2006-01-06 00:20:55 -08002727static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002728slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002729{
Shaohua Lif2076e72015-10-08 21:54:12 -07002730 if (test_bit(Journal, &rdev->flags))
2731 return sprintf(page, "journal\n");
2732 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002733 return sprintf(page, "none\n");
2734 else
2735 return sprintf(page, "%d\n", rdev->raid_disk);
2736}
2737
2738static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002739slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002740{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002741 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002742 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002743
Shaohua Lif2076e72015-10-08 21:54:12 -07002744 if (test_bit(Journal, &rdev->flags))
2745 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002746 if (strncmp(buf, "none", 4)==0)
2747 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002748 else {
2749 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2750 if (err < 0)
2751 return err;
2752 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002753 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002754 /* Setting 'slot' on an active array requires also
2755 * updating the 'rd%d' link, and communicating
2756 * with the personality with ->hot_*_disk.
2757 * For now we only support removing
2758 * failed/spare devices. This normally happens automatically,
2759 * but not when the metadata is externally managed.
2760 */
NeilBrownc303da62008-02-06 01:39:51 -08002761 if (rdev->raid_disk == -1)
2762 return -EEXIST;
2763 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002764 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002765 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002766 clear_bit(Blocked, &rdev->flags);
2767 remove_and_add_spares(rdev->mddev, rdev);
2768 if (rdev->raid_disk >= 0)
2769 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002770 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2771 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002772 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002773 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002774 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002775 */
2776
2777 if (rdev->raid_disk != -1)
2778 return -EBUSY;
2779
NeilBrownc6751b22011-02-02 11:57:13 +11002780 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2781 return -EBUSY;
2782
Neil Brown6c2fce22008-06-28 08:31:31 +10002783 if (rdev->mddev->pers->hot_add_disk == NULL)
2784 return -EINVAL;
2785
NeilBrownba1b41b2011-01-14 09:14:34 +11002786 if (slot >= rdev->mddev->raid_disks &&
2787 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2788 return -ENOSPC;
2789
Neil Brown6c2fce22008-06-28 08:31:31 +10002790 rdev->raid_disk = slot;
2791 if (test_bit(In_sync, &rdev->flags))
2792 rdev->saved_raid_disk = slot;
2793 else
2794 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002795 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002796 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05002797 remove_and_add_spares(rdev->mddev, rdev);
2798 if (rdev->raid_disk == -1)
2799 return -EBUSY;
Neil Brown6c2fce22008-06-28 08:31:31 +10002800 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002801 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002802 if (slot >= rdev->mddev->raid_disks &&
2803 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002804 return -ENOSPC;
2805 rdev->raid_disk = slot;
2806 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002807 clear_bit(Faulty, &rdev->flags);
2808 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002809 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002810 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002811 }
NeilBrown014236d2006-01-06 00:20:55 -08002812 return len;
2813}
2814
NeilBrown014236d2006-01-06 00:20:55 -08002815static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002816__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002817
NeilBrown93c8cad2006-01-06 00:20:56 -08002818static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002819offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002820{
Andrew Morton6961ece2006-01-06 00:20:59 -08002821 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002822}
2823
2824static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002825offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002826{
NeilBrownc6563a82012-05-21 09:27:00 +10002827 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002828 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002829 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002830 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002831 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002832 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002833 /* Must set offset before size, so overlap checks
2834 * can be sane */
2835 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002836 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002837 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002838 return len;
2839}
2840
2841static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002842__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002843
NeilBrownc6563a82012-05-21 09:27:00 +10002844static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2845{
2846 return sprintf(page, "%llu\n",
2847 (unsigned long long)rdev->new_data_offset);
2848}
2849
2850static ssize_t new_offset_store(struct md_rdev *rdev,
2851 const char *buf, size_t len)
2852{
2853 unsigned long long new_offset;
2854 struct mddev *mddev = rdev->mddev;
2855
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002856 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002857 return -EINVAL;
2858
NeilBrownf851b602014-12-11 10:02:10 +11002859 if (mddev->sync_thread ||
2860 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002861 return -EBUSY;
2862 if (new_offset == rdev->data_offset)
2863 /* reset is always permitted */
2864 ;
2865 else if (new_offset > rdev->data_offset) {
2866 /* must not push array size beyond rdev_sectors */
2867 if (new_offset - rdev->data_offset
2868 + mddev->dev_sectors > rdev->sectors)
2869 return -E2BIG;
2870 }
2871 /* Metadata worries about other space details. */
2872
2873 /* decreasing the offset is inconsistent with a backwards
2874 * reshape.
2875 */
2876 if (new_offset < rdev->data_offset &&
2877 mddev->reshape_backwards)
2878 return -EINVAL;
2879 /* Increasing offset is inconsistent with forwards
2880 * reshape. reshape_direction should be set to
2881 * 'backwards' first.
2882 */
2883 if (new_offset > rdev->data_offset &&
2884 !mddev->reshape_backwards)
2885 return -EINVAL;
2886
2887 if (mddev->pers && mddev->persistent &&
2888 !super_types[mddev->major_version]
2889 .allow_new_offset(rdev, new_offset))
2890 return -E2BIG;
2891 rdev->new_data_offset = new_offset;
2892 if (new_offset > rdev->data_offset)
2893 mddev->reshape_backwards = 1;
2894 else if (new_offset < rdev->data_offset)
2895 mddev->reshape_backwards = 0;
2896
2897 return len;
2898}
2899static struct rdev_sysfs_entry rdev_new_offset =
2900__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2901
NeilBrown83303b62006-01-06 00:21:06 -08002902static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002903rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002904{
Andre Nolldd8ac332009-03-31 14:33:13 +11002905 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002906}
2907
NeilBrownc5d79ad2008-02-06 01:39:54 -08002908static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2909{
2910 /* check if two start/length pairs overlap */
2911 if (s1+l1 <= s2)
2912 return 0;
2913 if (s2+l2 <= s1)
2914 return 0;
2915 return 1;
2916}
2917
Dan Williamsb522adc2009-03-31 15:00:31 +11002918static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2919{
2920 unsigned long long blocks;
2921 sector_t new;
2922
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002923 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002924 return -EINVAL;
2925
2926 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2927 return -EINVAL; /* sector conversion overflow */
2928
2929 new = blocks * 2;
2930 if (new != blocks * 2)
2931 return -EINVAL; /* unsigned long long to sector_t overflow */
2932
2933 *sectors = new;
2934 return 0;
2935}
2936
NeilBrown83303b62006-01-06 00:21:06 -08002937static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002938rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002939{
NeilBrownfd01b882011-10-11 16:47:53 +11002940 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002941 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002942 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002943
Shaohua Lif2076e72015-10-08 21:54:12 -07002944 if (test_bit(Journal, &rdev->flags))
2945 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11002946 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002947 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002948 if (rdev->data_offset != rdev->new_data_offset)
2949 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002950 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002951 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002952 sectors = super_types[my_mddev->major_version].
2953 rdev_size_change(rdev, sectors);
2954 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002955 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002956 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002957 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002958 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002959 if (!my_mddev->pers->resize)
2960 /* Cannot change size for RAID0 or Linear etc */
2961 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002962 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002963 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002964 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002965
Andre Nolldd8ac332009-03-31 14:33:13 +11002966 rdev->sectors = sectors;
2967 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002968 /* Need to check that all other rdevs with the same
2969 * ->bdev do not overlap. 'rcu' is sufficient to walk
2970 * the rdev lists safely.
2971 * This check does not provide a hard guarantee, it
2972 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002973 */
NeilBrownfd01b882011-10-11 16:47:53 +11002974 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002975 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002976 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002977
NeilBrown8b1afc32014-09-29 15:33:20 +10002978 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002979 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002980 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002981
NeilBrowndafb20f2012-03-19 12:46:39 +11002982 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002983 if (rdev->bdev == rdev2->bdev &&
2984 rdev != rdev2 &&
2985 overlaps(rdev->data_offset, rdev->sectors,
2986 rdev2->data_offset,
2987 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002988 overlap = 1;
2989 break;
2990 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002991 if (overlap) {
2992 mddev_put(mddev);
2993 break;
2994 }
2995 }
NeilBrown8b1afc32014-09-29 15:33:20 +10002996 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08002997 if (overlap) {
2998 /* Someone else could have slipped in a size
2999 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003000 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003001 * safe, and trust userspace not to race with
3002 * itself
3003 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003004 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003005 return -EBUSY;
3006 }
3007 }
NeilBrown83303b62006-01-06 00:21:06 -08003008 return len;
3009}
3010
3011static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003012__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003013
NeilBrown3cb03002011-10-11 16:45:26 +11003014static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003015{
3016 unsigned long long recovery_start = rdev->recovery_offset;
3017
3018 if (test_bit(In_sync, &rdev->flags) ||
3019 recovery_start == MaxSector)
3020 return sprintf(page, "none\n");
3021
3022 return sprintf(page, "%llu\n", recovery_start);
3023}
3024
NeilBrown3cb03002011-10-11 16:45:26 +11003025static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003026{
3027 unsigned long long recovery_start;
3028
3029 if (cmd_match(buf, "none"))
3030 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003031 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003032 return -EINVAL;
3033
3034 if (rdev->mddev->pers &&
3035 rdev->raid_disk >= 0)
3036 return -EBUSY;
3037
3038 rdev->recovery_offset = recovery_start;
3039 if (recovery_start == MaxSector)
3040 set_bit(In_sync, &rdev->flags);
3041 else
3042 clear_bit(In_sync, &rdev->flags);
3043 return len;
3044}
3045
3046static struct rdev_sysfs_entry rdev_recovery_start =
3047__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3048
NeilBrown16c791a2011-07-28 11:31:47 +10003049static ssize_t
3050badblocks_show(struct badblocks *bb, char *page, int unack);
3051static ssize_t
3052badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3053
NeilBrown3cb03002011-10-11 16:45:26 +11003054static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003055{
3056 return badblocks_show(&rdev->badblocks, page, 0);
3057}
NeilBrown3cb03002011-10-11 16:45:26 +11003058static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003059{
NeilBrownde393cd2011-07-28 11:31:48 +10003060 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3061 /* Maybe that ack was all we needed */
3062 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3063 wake_up(&rdev->blocked_wait);
3064 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003065}
3066static struct rdev_sysfs_entry rdev_bad_blocks =
3067__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3068
NeilBrown3cb03002011-10-11 16:45:26 +11003069static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003070{
3071 return badblocks_show(&rdev->badblocks, page, 1);
3072}
NeilBrown3cb03002011-10-11 16:45:26 +11003073static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003074{
3075 return badblocks_store(&rdev->badblocks, page, len, 1);
3076}
3077static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3078__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3079
NeilBrown86e6ffd2005-11-08 21:39:24 -08003080static struct attribute *rdev_default_attrs[] = {
3081 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003082 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003083 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003084 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003085 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003086 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003087 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003088 &rdev_bad_blocks.attr,
3089 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003090 NULL,
3091};
3092static ssize_t
3093rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3094{
3095 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003096 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003097
3098 if (!entry->show)
3099 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003100 if (!rdev->mddev)
3101 return -EBUSY;
3102 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003103}
3104
3105static ssize_t
3106rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3107 const char *page, size_t length)
3108{
3109 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003110 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003111 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003112 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003113
3114 if (!entry->store)
3115 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003116 if (!capable(CAP_SYS_ADMIN))
3117 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003118 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003119 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003120 if (rdev->mddev == NULL)
3121 rv = -EBUSY;
3122 else
3123 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003124 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003125 }
3126 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003127}
3128
3129static void rdev_free(struct kobject *ko)
3130{
NeilBrown3cb03002011-10-11 16:45:26 +11003131 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003132 kfree(rdev);
3133}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003134static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003135 .show = rdev_attr_show,
3136 .store = rdev_attr_store,
3137};
3138static struct kobj_type rdev_ktype = {
3139 .release = rdev_free,
3140 .sysfs_ops = &rdev_sysfs_ops,
3141 .default_attrs = rdev_default_attrs,
3142};
3143
NeilBrown3cb03002011-10-11 16:45:26 +11003144int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003145{
3146 rdev->desc_nr = -1;
3147 rdev->saved_raid_disk = -1;
3148 rdev->raid_disk = -1;
3149 rdev->flags = 0;
3150 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003151 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003152 rdev->sb_events = 0;
3153 rdev->last_read_error.tv_sec = 0;
3154 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003155 rdev->sb_loaded = 0;
3156 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003157 atomic_set(&rdev->nr_pending, 0);
3158 atomic_set(&rdev->read_errors, 0);
3159 atomic_set(&rdev->corrected_errors, 0);
3160
3161 INIT_LIST_HEAD(&rdev->same_set);
3162 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003163
3164 /* Add space to store bad block list.
3165 * This reserves the space even on arrays where it cannot
3166 * be used - I wonder if that matters
3167 */
3168 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003169 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003170 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3171 seqlock_init(&rdev->badblocks.lock);
3172 if (rdev->badblocks.page == NULL)
3173 return -ENOMEM;
3174
3175 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003176}
3177EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178/*
3179 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3180 *
3181 * mark the device faulty if:
3182 *
3183 * - the device is nonexistent (zero size)
3184 * - the device has no valid superblock
3185 *
3186 * a faulty rdev _never_ has rdev->sb set.
3187 */
NeilBrown3cb03002011-10-11 16:45:26 +11003188static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
3190 char b[BDEVNAME_SIZE];
3191 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003192 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 sector_t size;
3194
NeilBrown9ffae0c2006-01-06 00:20:32 -08003195 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 if (!rdev) {
3197 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3198 return ERR_PTR(-ENOMEM);
3199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
NeilBrown2230dfe2011-07-28 11:31:46 +10003201 err = md_rdev_init(rdev);
3202 if (err)
3203 goto abort_free;
3204 err = alloc_disk_sb(rdev);
3205 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 goto abort_free;
3207
NeilBrownc5d79ad2008-02-06 01:39:54 -08003208 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 if (err)
3210 goto abort_free;
3211
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003212 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003213
Mike Snitzer77304d22010-11-08 14:39:12 +01003214 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003216 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 "md: %s has zero or unknown size, marking faulty!\n",
3218 bdevname(rdev->bdev,b));
3219 err = -EINVAL;
3220 goto abort_free;
3221 }
3222
3223 if (super_format >= 0) {
3224 err = super_types[super_format].
3225 load_super(rdev, NULL, super_minor);
3226 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003227 printk(KERN_WARNING
3228 "md: %s does not have a valid v%d.%d "
3229 "superblock, not importing!\n",
3230 bdevname(rdev->bdev,b),
3231 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 goto abort_free;
3233 }
3234 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003235 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 "md: could not read %s's sb, not importing!\n",
3237 bdevname(rdev->bdev,b));
3238 goto abort_free;
3239 }
3240 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003241
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 return rdev;
3243
3244abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003245 if (rdev->bdev)
3246 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003247 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 kfree(rdev);
3249 return ERR_PTR(err);
3250}
3251
3252/*
3253 * Check a full RAID array for plausibility
3254 */
3255
NeilBrownf72ffdd2014-09-30 14:23:59 +10003256static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257{
3258 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003259 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 char b[BDEVNAME_SIZE];
3261
3262 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003263 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 switch (super_types[mddev->major_version].
3265 load_super(rdev, freshest, mddev->minor_version)) {
3266 case 1:
3267 freshest = rdev;
3268 break;
3269 case 0:
3270 break;
3271 default:
3272 printk( KERN_ERR \
3273 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003274 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003276 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 }
3278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 super_types[mddev->major_version].
3280 validate_super(mddev, freshest);
3281
3282 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003283 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003284 if (mddev->max_disks &&
3285 (rdev->desc_nr >= mddev->max_disks ||
3286 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003287 printk(KERN_WARNING
3288 "md: %s: %s: only %d devices permitted\n",
3289 mdname(mddev), bdevname(rdev->bdev, b),
3290 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003291 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003292 continue;
3293 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003294 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 if (super_types[mddev->major_version].
3296 validate_super(mddev, rdev)) {
3297 printk(KERN_WARNING "md: kicking non-fresh %s"
3298 " from array!\n",
3299 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003300 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 continue;
3302 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 if (mddev->level == LEVEL_MULTIPATH) {
3305 rdev->desc_nr = i++;
3306 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003307 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003308 } else if (rdev->raid_disk >=
3309 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3310 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003311 rdev->raid_disk = -1;
3312 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 }
3314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
NeilBrown72e02072009-12-14 12:49:55 +11003317/* Read a fixed-point number.
3318 * Numbers in sysfs attributes should be in "standard" units where
3319 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003320 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003321 * milliseconds or jiffies.
3322 * This function takes a decimal number with a possible fractional
3323 * component, and produces an integer which is the result of
3324 * multiplying that number by 10^'scale'.
3325 * all without any floating-point arithmetic.
3326 */
3327int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3328{
3329 unsigned long result = 0;
3330 long decimals = -1;
3331 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3332 if (*cp == '.')
3333 decimals = 0;
3334 else if (decimals < scale) {
3335 unsigned int value;
3336 value = *cp - '0';
3337 result = result * 10 + value;
3338 if (decimals >= 0)
3339 decimals++;
3340 }
3341 cp++;
3342 }
3343 if (*cp == '\n')
3344 cp++;
3345 if (*cp)
3346 return -EINVAL;
3347 if (decimals < 0)
3348 decimals = 0;
3349 while (decimals < scale) {
3350 result *= 10;
3351 decimals ++;
3352 }
3353 *res = result;
3354 return 0;
3355}
3356
NeilBrowneae17012005-11-08 21:39:23 -08003357static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003358safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003359{
3360 int msec = (mddev->safemode_delay*1000)/HZ;
3361 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3362}
3363static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003364safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003365{
NeilBrown16f17b32006-06-26 00:27:37 -07003366 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003367
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003368 if (mddev_is_clustered(mddev)) {
3369 pr_info("md: Safemode is disabled for clustered mode\n");
3370 return -EINVAL;
3371 }
3372
NeilBrown72e02072009-12-14 12:49:55 +11003373 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003374 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003375 if (msec == 0)
3376 mddev->safemode_delay = 0;
3377 else {
NeilBrown19052c02008-08-05 15:54:13 +10003378 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003379 unsigned long new_delay = (msec*HZ)/1000;
3380
3381 if (new_delay == 0)
3382 new_delay = 1;
3383 mddev->safemode_delay = new_delay;
3384 if (new_delay < old_delay || old_delay == 0)
3385 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003386 }
3387 return len;
3388}
3389static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003390__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003391
3392static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003393level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003394{
NeilBrown36d091f2014-12-15 12:56:58 +11003395 struct md_personality *p;
3396 int ret;
3397 spin_lock(&mddev->lock);
3398 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003399 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003400 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003401 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003402 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003403 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003404 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003405 else
NeilBrown36d091f2014-12-15 12:56:58 +11003406 ret = 0;
3407 spin_unlock(&mddev->lock);
3408 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003409}
3410
NeilBrownd9d166c2006-01-06 00:20:51 -08003411static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003412level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003413{
Dan Williamsf2859af2010-05-02 10:04:16 -07003414 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003415 ssize_t rv;
3416 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003417 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003418 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003419 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003420 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003421
NeilBrown67918752014-12-15 12:57:01 +11003422 if (slen == 0 || slen >= sizeof(clevel))
3423 return -EINVAL;
3424
3425 rv = mddev_lock(mddev);
3426 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003427 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003428
3429 if (mddev->pers == NULL) {
3430 strncpy(mddev->clevel, buf, slen);
3431 if (mddev->clevel[slen-1] == '\n')
3432 slen--;
3433 mddev->clevel[slen] = 0;
3434 mddev->level = LEVEL_NONE;
3435 rv = len;
3436 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003437 }
NeilBrown67918752014-12-15 12:57:01 +11003438 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003439 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003440 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003441
3442 /* request to change the personality. Need to ensure:
3443 * - array is not engaged in resync/recovery/reshape
3444 * - old personality can be suspended
3445 * - new personality will access other array.
3446 */
3447
NeilBrown67918752014-12-15 12:57:01 +11003448 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003449 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003450 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003451 mddev->reshape_position != MaxSector ||
3452 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003453 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003454
NeilBrown67918752014-12-15 12:57:01 +11003455 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003456 if (!mddev->pers->quiesce) {
3457 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3458 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003459 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003460 }
3461
3462 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003463 strncpy(clevel, buf, slen);
3464 if (clevel[slen-1] == '\n')
3465 slen--;
3466 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003467 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003468 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003469
Dan Williamsf2859af2010-05-02 10:04:16 -07003470 if (request_module("md-%s", clevel) != 0)
3471 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003472 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003473 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003474 if (!pers || !try_module_get(pers->owner)) {
3475 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003476 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003477 rv = -EINVAL;
3478 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003479 }
3480 spin_unlock(&pers_lock);
3481
3482 if (pers == mddev->pers) {
3483 /* Nothing to do! */
3484 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003485 rv = len;
3486 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003487 }
3488 if (!pers->takeover) {
3489 module_put(pers->owner);
3490 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003491 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003492 rv = -EINVAL;
3493 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003494 }
3495
NeilBrowndafb20f2012-03-19 12:46:39 +11003496 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003497 rdev->new_raid_disk = rdev->raid_disk;
3498
NeilBrown245f46c2009-03-31 14:39:39 +11003499 /* ->takeover must set new_* and/or delta_disks
3500 * if it succeeds, and may set them when it fails.
3501 */
3502 priv = pers->takeover(mddev);
3503 if (IS_ERR(priv)) {
3504 mddev->new_level = mddev->level;
3505 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003506 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003507 mddev->raid_disks -= mddev->delta_disks;
3508 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003509 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003510 module_put(pers->owner);
3511 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003512 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003513 rv = PTR_ERR(priv);
3514 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003515 }
3516
3517 /* Looks like we have a winner */
3518 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003519 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003520
3521 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003522 oldpers = mddev->pers;
3523 oldpriv = mddev->private;
3524 mddev->pers = pers;
3525 mddev->private = priv;
3526 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3527 mddev->level = mddev->new_level;
3528 mddev->layout = mddev->new_layout;
3529 mddev->chunk_sectors = mddev->new_chunk_sectors;
3530 mddev->delta_disks = 0;
3531 mddev->reshape_backwards = 0;
3532 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003533 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003534
NeilBrowndb721d32014-12-15 12:56:58 +11003535 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003536 mddev->external) {
3537 /* We are converting from a no-redundancy array
3538 * to a redundancy array and metadata is managed
3539 * externally so we need to be sure that writes
3540 * won't block due to a need to transition
3541 * clean->dirty
3542 * until external management is started.
3543 */
3544 mddev->in_sync = 0;
3545 mddev->safemode_delay = 0;
3546 mddev->safemode = 0;
3547 }
3548
NeilBrowndb721d32014-12-15 12:56:58 +11003549 oldpers->free(mddev, oldpriv);
3550
3551 if (oldpers->sync_request == NULL &&
3552 pers->sync_request != NULL) {
3553 /* need to add the md_redundancy_group */
3554 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3555 printk(KERN_WARNING
3556 "md: cannot register extra attributes for %s\n",
3557 mdname(mddev));
3558 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3559 }
3560 if (oldpers->sync_request != NULL &&
3561 pers->sync_request == NULL) {
3562 /* need to remove the md_redundancy_group */
3563 if (mddev->to_remove == NULL)
3564 mddev->to_remove = &md_redundancy_group;
3565 }
3566
NeilBrowndafb20f2012-03-19 12:46:39 +11003567 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003568 if (rdev->raid_disk < 0)
3569 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003570 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003571 rdev->new_raid_disk = -1;
3572 if (rdev->new_raid_disk == rdev->raid_disk)
3573 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003574 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003575 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003576 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003577 if (rdev->raid_disk < 0)
3578 continue;
3579 if (rdev->new_raid_disk == rdev->raid_disk)
3580 continue;
3581 rdev->raid_disk = rdev->new_raid_disk;
3582 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003583 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003584 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003585 if (sysfs_link_rdev(mddev, rdev))
3586 printk(KERN_WARNING "md: cannot register rd%d"
3587 " for %s after level change\n",
3588 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003589 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003590 }
3591
NeilBrowndb721d32014-12-15 12:56:58 +11003592 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003593 /* this is now an array without redundancy, so
3594 * it must always be in_sync
3595 */
3596 mddev->in_sync = 1;
3597 del_timer_sync(&mddev->safemode_timer);
3598 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003599 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003600 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003601 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003602 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003603 if (!mddev->thread)
3604 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003605 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003606 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003607 rv = len;
3608out_unlock:
3609 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003610 return rv;
3611}
3612
3613static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003614__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003615
NeilBrownd4dbd022006-06-26 00:27:59 -07003616static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003617layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003618{
3619 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003620 if (mddev->reshape_position != MaxSector &&
3621 mddev->layout != mddev->new_layout)
3622 return sprintf(page, "%d (%d)\n",
3623 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003624 return sprintf(page, "%d\n", mddev->layout);
3625}
3626
3627static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003628layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003629{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003630 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003631 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003632
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003633 err = kstrtouint(buf, 10, &n);
3634 if (err < 0)
3635 return err;
NeilBrown67918752014-12-15 12:57:01 +11003636 err = mddev_lock(mddev);
3637 if (err)
3638 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003639
NeilBrownb3546032009-03-31 14:56:41 +11003640 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003641 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003642 err = -EBUSY;
3643 else if (mddev->ro)
3644 err = -EROFS;
3645 else {
3646 mddev->new_layout = n;
3647 err = mddev->pers->check_reshape(mddev);
3648 if (err)
3649 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003650 }
NeilBrownb3546032009-03-31 14:56:41 +11003651 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003652 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003653 if (mddev->reshape_position == MaxSector)
3654 mddev->layout = n;
3655 }
NeilBrown67918752014-12-15 12:57:01 +11003656 mddev_unlock(mddev);
3657 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003658}
3659static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003660__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003661
NeilBrowneae17012005-11-08 21:39:23 -08003662static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003663raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003664{
NeilBrownbb636542005-11-08 21:39:45 -08003665 if (mddev->raid_disks == 0)
3666 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003667 if (mddev->reshape_position != MaxSector &&
3668 mddev->delta_disks != 0)
3669 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3670 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003671 return sprintf(page, "%d\n", mddev->raid_disks);
3672}
3673
NeilBrownfd01b882011-10-11 16:47:53 +11003674static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003675
3676static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003677raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003678{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003679 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003680 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003681
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003682 err = kstrtouint(buf, 10, &n);
3683 if (err < 0)
3684 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003685
NeilBrown67918752014-12-15 12:57:01 +11003686 err = mddev_lock(mddev);
3687 if (err)
3688 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003689 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003690 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003691 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003692 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003693 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003694
NeilBrown67918752014-12-15 12:57:01 +11003695 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003696 rdev_for_each(rdev, mddev) {
3697 if (olddisks < n &&
3698 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003699 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003700 if (olddisks > n &&
3701 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003702 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003703 }
NeilBrown67918752014-12-15 12:57:01 +11003704 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003705 mddev->delta_disks = n - olddisks;
3706 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003707 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003708 } else
NeilBrownda943b992006-01-06 00:20:54 -08003709 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003710out_unlock:
3711 mddev_unlock(mddev);
3712 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003713}
3714static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003715__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003716
NeilBrown24dd4692005-11-08 21:39:26 -08003717static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003718chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003719{
NeilBrown08a02ec2007-05-09 02:35:38 -07003720 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003721 mddev->chunk_sectors != mddev->new_chunk_sectors)
3722 return sprintf(page, "%d (%d)\n",
3723 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003724 mddev->chunk_sectors << 9);
3725 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003726}
3727
3728static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003729chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003730{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003731 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003732 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003733
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003734 err = kstrtoul(buf, 10, &n);
3735 if (err < 0)
3736 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003737
NeilBrown67918752014-12-15 12:57:01 +11003738 err = mddev_lock(mddev);
3739 if (err)
3740 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003741 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003742 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003743 err = -EBUSY;
3744 else if (mddev->ro)
3745 err = -EROFS;
3746 else {
3747 mddev->new_chunk_sectors = n >> 9;
3748 err = mddev->pers->check_reshape(mddev);
3749 if (err)
3750 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003751 }
NeilBrownb3546032009-03-31 14:56:41 +11003752 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003753 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003754 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003755 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003756 }
NeilBrown67918752014-12-15 12:57:01 +11003757 mddev_unlock(mddev);
3758 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003759}
3760static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003761__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003762
NeilBrowna94213b2006-06-26 00:28:00 -07003763static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003764resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003765{
NeilBrownd1a7c502009-03-31 15:24:32 +11003766 if (mddev->recovery_cp == MaxSector)
3767 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003768 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3769}
3770
3771static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003772resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003773{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003774 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003775 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003776
3777 if (cmd_match(buf, "none"))
3778 n = MaxSector;
3779 else {
3780 err = kstrtoull(buf, 10, &n);
3781 if (err < 0)
3782 return err;
3783 if (n != (sector_t)n)
3784 return -EINVAL;
3785 }
NeilBrowna94213b2006-06-26 00:28:00 -07003786
NeilBrown67918752014-12-15 12:57:01 +11003787 err = mddev_lock(mddev);
3788 if (err)
3789 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003790 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003791 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003792
NeilBrown67918752014-12-15 12:57:01 +11003793 if (!err) {
3794 mddev->recovery_cp = n;
3795 if (mddev->pers)
3796 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3797 }
3798 mddev_unlock(mddev);
3799 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003800}
3801static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003802__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3803 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003804
NeilBrown9e653b62006-06-26 00:27:58 -07003805/*
3806 * The array state can be:
3807 *
3808 * clear
3809 * No devices, no size, no level
3810 * Equivalent to STOP_ARRAY ioctl
3811 * inactive
3812 * May have some settings, but array is not active
3813 * all IO results in error
3814 * When written, doesn't tear down array, but just stops it
3815 * suspended (not supported yet)
3816 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003817 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003818 * readonly
3819 * no resync can happen. no superblocks get written.
3820 * write requests fail
3821 * read-auto
3822 * like readonly, but behaves like 'clean' on a write request.
3823 *
3824 * clean - no pending writes, but otherwise active.
3825 * When written to inactive array, starts without resync
3826 * If a write request arrives then
3827 * if metadata is known, mark 'dirty' and switch to 'active'.
3828 * if not known, block and switch to write-pending
3829 * If written to an active array that has pending writes, then fails.
3830 * active
3831 * fully active: IO and resync can be happening.
3832 * When written to inactive array, starts with resync
3833 *
3834 * write-pending
3835 * clean, but writes are blocked waiting for 'active' to be written.
3836 *
3837 * active-idle
3838 * like active, but no writes have been seen for a while (100msec).
3839 *
3840 */
3841enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3842 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003843static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003844 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3845 "write-pending", "active-idle", NULL };
3846
3847static int match_word(const char *word, char **list)
3848{
3849 int n;
3850 for (n=0; list[n]; n++)
3851 if (cmd_match(word, list[n]))
3852 break;
3853 return n;
3854}
3855
3856static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003857array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003858{
3859 enum array_state st = inactive;
3860
3861 if (mddev->pers)
3862 switch(mddev->ro) {
3863 case 1:
3864 st = readonly;
3865 break;
3866 case 2:
3867 st = read_auto;
3868 break;
3869 case 0:
3870 if (mddev->in_sync)
3871 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003872 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003873 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003874 else if (mddev->safemode)
3875 st = active_idle;
3876 else
3877 st = active;
3878 }
3879 else {
3880 if (list_empty(&mddev->disks) &&
3881 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003882 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003883 st = clear;
3884 else
3885 st = inactive;
3886 }
3887 return sprintf(page, "%s\n", array_states[st]);
3888}
3889
NeilBrownf72ffdd2014-09-30 14:23:59 +10003890static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3891static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3892static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003893static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003894
3895static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003896array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003897{
NeilBrown67918752014-12-15 12:57:01 +11003898 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003899 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003900
3901 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3902 /* don't take reconfig_mutex when toggling between
3903 * clean and active
3904 */
3905 spin_lock(&mddev->lock);
3906 if (st == active) {
3907 restart_array(mddev);
3908 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3909 wake_up(&mddev->sb_wait);
3910 err = 0;
3911 } else /* st == clean */ {
3912 restart_array(mddev);
3913 if (atomic_read(&mddev->writes_pending) == 0) {
3914 if (mddev->in_sync == 0) {
3915 mddev->in_sync = 1;
3916 if (mddev->safemode == 1)
3917 mddev->safemode = 0;
3918 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3919 }
3920 err = 0;
3921 } else
3922 err = -EBUSY;
3923 }
3924 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003925 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003926 }
3927 err = mddev_lock(mddev);
3928 if (err)
3929 return err;
3930 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003931 switch(st) {
3932 case bad_word:
3933 break;
3934 case clear:
3935 /* stopping an active array */
NeilBrowna05b7ea02012-07-19 15:59:18 +10003936 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003937 break;
3938 case inactive:
3939 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003940 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003941 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003942 else
NeilBrowne6910632008-02-06 01:39:51 -08003943 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003944 break;
3945 case suspended:
3946 break; /* not supported yet */
3947 case readonly:
3948 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003949 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003950 else {
3951 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003952 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003953 err = do_md_run(mddev);
3954 }
3955 break;
3956 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003957 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003958 if (mddev->ro == 0)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003959 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003960 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003961 err = restart_array(mddev);
3962 if (err == 0) {
3963 mddev->ro = 2;
3964 set_disk_ro(mddev->gendisk, 0);
3965 }
NeilBrown9e653b62006-06-26 00:27:58 -07003966 } else {
3967 mddev->ro = 2;
3968 err = do_md_run(mddev);
3969 }
3970 break;
3971 case clean:
3972 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07003973 err = restart_array(mddev);
3974 if (err)
3975 break;
NeilBrown85572d72014-12-15 12:56:56 +11003976 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003977 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003978 if (mddev->in_sync == 0) {
3979 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003980 if (mddev->safemode == 1)
3981 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003982 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003983 }
3984 err = 0;
3985 } else
3986 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11003987 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003988 } else
3989 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003990 break;
3991 case active:
3992 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07003993 err = restart_array(mddev);
3994 if (err)
3995 break;
NeilBrown070dc6d2010-08-30 17:33:34 +10003996 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003997 wake_up(&mddev->sb_wait);
3998 err = 0;
3999 } else {
4000 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004001 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004002 err = do_md_run(mddev);
4003 }
4004 break;
4005 case write_pending:
4006 case active_idle:
4007 /* these cannot be set */
4008 break;
4009 }
NeilBrown67918752014-12-15 12:57:01 +11004010
4011 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004012 if (mddev->hold_active == UNTIL_IOCTL)
4013 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004014 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004015 }
NeilBrown67918752014-12-15 12:57:01 +11004016 mddev_unlock(mddev);
4017 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004018}
NeilBrown80ca3a42006-07-10 04:44:18 -07004019static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004020__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004021
NeilBrown6d7ff732006-01-06 00:21:16 -08004022static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004023max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004024 return sprintf(page, "%d\n",
4025 atomic_read(&mddev->max_corr_read_errors));
4026}
4027
4028static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004029max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004030{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004031 unsigned int n;
4032 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004033
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004034 rv = kstrtouint(buf, 10, &n);
4035 if (rv < 0)
4036 return rv;
4037 atomic_set(&mddev->max_corr_read_errors, n);
4038 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004039}
4040
4041static struct md_sysfs_entry max_corr_read_errors =
4042__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4043 max_corrected_read_errors_store);
4044
4045static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004046null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08004047{
4048 return -EINVAL;
4049}
4050
4051static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004052new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08004053{
4054 /* buf must be %d:%d\n? giving major and minor numbers */
4055 /* The new device is added to the array.
4056 * If the array has a persistent superblock, we read the
4057 * superblock to initialise info and check validity.
4058 * Otherwise, only checking done is that in bind_rdev_to_array,
4059 * which mainly checks size.
4060 */
4061 char *e;
4062 int major = simple_strtoul(buf, &e, 10);
4063 int minor;
4064 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004065 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08004066 int err;
4067
4068 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4069 return -EINVAL;
4070 minor = simple_strtoul(e+1, &e, 10);
4071 if (*e && *e != '\n')
4072 return -EINVAL;
4073 dev = MKDEV(major, minor);
4074 if (major != MAJOR(dev) ||
4075 minor != MINOR(dev))
4076 return -EOVERFLOW;
4077
NeilBrown67918752014-12-15 12:57:01 +11004078 flush_workqueue(md_misc_wq);
4079
4080 err = mddev_lock(mddev);
4081 if (err)
4082 return err;
NeilBrown6d7ff732006-01-06 00:21:16 -08004083 if (mddev->persistent) {
4084 rdev = md_import_device(dev, mddev->major_version,
4085 mddev->minor_version);
4086 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004087 struct md_rdev *rdev0
4088 = list_entry(mddev->disks.next,
4089 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08004090 err = super_types[mddev->major_version]
4091 .load_super(rdev, rdev0, mddev->minor_version);
4092 if (err < 0)
4093 goto out;
4094 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004095 } else if (mddev->external)
4096 rdev = md_import_device(dev, -2, -1);
4097 else
NeilBrown6d7ff732006-01-06 00:21:16 -08004098 rdev = md_import_device(dev, -1, -1);
4099
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004100 if (IS_ERR(rdev)) {
4101 mddev_unlock(mddev);
NeilBrown6d7ff732006-01-06 00:21:16 -08004102 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004103 }
NeilBrown6d7ff732006-01-06 00:21:16 -08004104 err = bind_rdev_to_array(rdev, mddev);
4105 out:
4106 if (err)
4107 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004108 mddev_unlock(mddev);
NeilBrown6d7ff732006-01-06 00:21:16 -08004109 return err ? err : len;
4110}
4111
4112static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004113__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004114
4115static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004116bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004117{
4118 char *end;
4119 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004120 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004121
NeilBrown67918752014-12-15 12:57:01 +11004122 err = mddev_lock(mddev);
4123 if (err)
4124 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004125 if (!mddev->bitmap)
4126 goto out;
4127 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4128 while (*buf) {
4129 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4130 if (buf == end) break;
4131 if (*end == '-') { /* range */
4132 buf = end + 1;
4133 end_chunk = simple_strtoul(buf, &end, 0);
4134 if (buf == end) break;
4135 }
4136 if (*end && !isspace(*end)) break;
4137 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004138 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004139 }
4140 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4141out:
NeilBrown67918752014-12-15 12:57:01 +11004142 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004143 return len;
4144}
4145
4146static struct md_sysfs_entry md_bitmap =
4147__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4148
4149static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004150size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004151{
Andre Noll58c0fed2009-03-31 14:33:13 +11004152 return sprintf(page, "%llu\n",
4153 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004154}
4155
NeilBrownfd01b882011-10-11 16:47:53 +11004156static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004157
4158static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004159size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004160{
4161 /* If array is inactive, we can reduce the component size, but
4162 * not increase it (except from 0).
4163 * If array is active, we can try an on-line resize
4164 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004165 sector_t sectors;
4166 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004167
Andre Noll58c0fed2009-03-31 14:33:13 +11004168 if (err < 0)
4169 return err;
NeilBrown67918752014-12-15 12:57:01 +11004170 err = mddev_lock(mddev);
4171 if (err)
4172 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004173 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004174 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004175 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004176 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004177 if (mddev->dev_sectors == 0 ||
4178 mddev->dev_sectors > sectors)
4179 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004180 else
4181 err = -ENOSPC;
4182 }
NeilBrown67918752014-12-15 12:57:01 +11004183 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004184 return err ? err : len;
4185}
4186
4187static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004188__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004189
Masanari Iida83f0d772012-10-30 00:18:08 +09004190/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004191 * This is one of
4192 * 'none' for arrays with no metadata (good luck...)
4193 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004194 * or N.M for internally known formats
4195 */
4196static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004197metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004198{
4199 if (mddev->persistent)
4200 return sprintf(page, "%d.%d\n",
4201 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004202 else if (mddev->external)
4203 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004204 else
4205 return sprintf(page, "none\n");
4206}
4207
4208static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004209metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004210{
4211 int major, minor;
4212 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004213 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004214 /* Changing the details of 'external' metadata is
4215 * always permitted. Otherwise there must be
4216 * no devices attached to the array.
4217 */
NeilBrown67918752014-12-15 12:57:01 +11004218
4219 err = mddev_lock(mddev);
4220 if (err)
4221 return err;
4222 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004223 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4224 ;
4225 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004226 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004227
NeilBrown67918752014-12-15 12:57:01 +11004228 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004229 if (cmd_match(buf, "none")) {
4230 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004231 mddev->external = 0;
4232 mddev->major_version = 0;
4233 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004234 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004235 }
4236 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004237 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004238 if (namelen >= sizeof(mddev->metadata_type))
4239 namelen = sizeof(mddev->metadata_type)-1;
4240 strncpy(mddev->metadata_type, buf+9, namelen);
4241 mddev->metadata_type[namelen] = 0;
4242 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4243 mddev->metadata_type[--namelen] = 0;
4244 mddev->persistent = 0;
4245 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004246 mddev->major_version = 0;
4247 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004248 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004249 }
4250 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004251 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004252 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004253 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004254 buf = e+1;
4255 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004256 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004257 goto out_unlock;
4258 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004259 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004260 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004261 mddev->major_version = major;
4262 mddev->minor_version = minor;
4263 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004264 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004265 err = 0;
4266out_unlock:
4267 mddev_unlock(mddev);
4268 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004269}
4270
4271static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004272__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004273
NeilBrowna35b0d62006-01-06 00:20:49 -08004274static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004275action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004276{
NeilBrown7eec3142005-11-08 21:39:44 -08004277 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004278 unsigned long recovery = mddev->recovery;
4279 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004280 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004281 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4282 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4283 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004284 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004285 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4286 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004287 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004288 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004289 type = "check";
4290 else
4291 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004292 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004293 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004294 else if (mddev->reshape_position != MaxSector)
4295 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004296 }
4297 return sprintf(page, "%s\n", type);
4298}
4299
4300static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004301action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004302{
NeilBrown7eec3142005-11-08 21:39:44 -08004303 if (!mddev->pers || !mddev->pers->sync_request)
4304 return -EINVAL;
4305
NeilBrownb6a9ce62009-05-26 09:41:17 +10004306
4307 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004308 if (cmd_match(page, "frozen"))
4309 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4310 else
4311 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004312 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4313 mddev_lock(mddev) == 0) {
4314 flush_workqueue(md_misc_wq);
4315 if (mddev->sync_thread) {
4316 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004317 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004318 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004319 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004320 }
NeilBrown03c902e2006-01-06 00:20:46 -08004321 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4322 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004323 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004324 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004325 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004326 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004327 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004328 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004329 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004330 int err;
4331 if (mddev->pers->start_reshape == NULL)
4332 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004333 err = mddev_lock(mddev);
4334 if (!err) {
NeilBrown56ccc112015-05-28 17:53:29 +10004335 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004336 err = mddev->pers->start_reshape(mddev);
4337 mddev_unlock(mddev);
4338 }
NeilBrown16484bf2006-03-27 01:18:13 -08004339 if (err)
4340 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004341 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004342 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004343 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004344 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004345 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004346 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004347 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004348 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4349 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004350 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004351 if (mddev->ro == 2) {
4352 /* A write to sync_action is enough to justify
4353 * canceling read-auto mode
4354 */
4355 mddev->ro = 0;
4356 md_wakeup_thread(mddev->sync_thread);
4357 }
NeilBrown03c902e2006-01-06 00:20:46 -08004358 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004359 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004360 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004361 return len;
4362}
4363
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004364static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004365__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004366
4367static ssize_t
4368last_sync_action_show(struct mddev *mddev, char *page)
4369{
4370 return sprintf(page, "%s\n", mddev->last_sync_action);
4371}
4372
4373static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4374
NeilBrown9d888832005-11-08 21:39:26 -08004375static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004376mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004377{
4378 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004379 (unsigned long long)
4380 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004381}
4382
NeilBrown80ca3a42006-07-10 04:44:18 -07004383static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004384
NeilBrown88202a02006-01-06 00:21:36 -08004385static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004386sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004387{
4388 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4389 mddev->sync_speed_min ? "local": "system");
4390}
4391
4392static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004393sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004394{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004395 unsigned int min;
4396 int rv;
4397
NeilBrown88202a02006-01-06 00:21:36 -08004398 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004399 min = 0;
4400 } else {
4401 rv = kstrtouint(buf, 10, &min);
4402 if (rv < 0)
4403 return rv;
4404 if (min == 0)
4405 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004406 }
NeilBrown88202a02006-01-06 00:21:36 -08004407 mddev->sync_speed_min = min;
4408 return len;
4409}
4410
4411static struct md_sysfs_entry md_sync_min =
4412__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4413
4414static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004415sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004416{
4417 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4418 mddev->sync_speed_max ? "local": "system");
4419}
4420
4421static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004422sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004423{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004424 unsigned int max;
4425 int rv;
4426
NeilBrown88202a02006-01-06 00:21:36 -08004427 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004428 max = 0;
4429 } else {
4430 rv = kstrtouint(buf, 10, &max);
4431 if (rv < 0)
4432 return rv;
4433 if (max == 0)
4434 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004435 }
NeilBrown88202a02006-01-06 00:21:36 -08004436 mddev->sync_speed_max = max;
4437 return len;
4438}
4439
4440static struct md_sysfs_entry md_sync_max =
4441__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4442
Iustin Popd7f3d292007-10-16 23:30:54 -07004443static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004444degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004445{
4446 return sprintf(page, "%d\n", mddev->degraded);
4447}
4448static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004449
4450static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004451sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004452{
4453 return sprintf(page, "%d\n", mddev->parallel_resync);
4454}
4455
4456static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004457sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004458{
4459 long n;
4460
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004461 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004462 return -EINVAL;
4463
4464 if (n != 0 && n != 1)
4465 return -EINVAL;
4466
4467 mddev->parallel_resync = n;
4468
4469 if (mddev->sync_thread)
4470 wake_up(&resync_wait);
4471
4472 return len;
4473}
4474
4475/* force parallel resync, even with shared block devices */
4476static struct md_sysfs_entry md_sync_force_parallel =
4477__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4478 sync_force_parallel_show, sync_force_parallel_store);
4479
4480static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004481sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004482{
4483 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004484 if (mddev->curr_resync == 0)
4485 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004486 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4487 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004488 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004489 db = resync - mddev->resync_mark_cnt;
4490 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004491}
4492
NeilBrown80ca3a42006-07-10 04:44:18 -07004493static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004494
4495static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004496sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004497{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004498 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004499
NeilBrownacb180b2009-04-14 16:28:34 +10004500 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4501 return sprintf(page, "none\n");
4502
NeilBrown72f36d52012-10-11 14:25:57 +11004503 if (mddev->curr_resync == 1 ||
4504 mddev->curr_resync == 2)
4505 return sprintf(page, "delayed\n");
4506
NeilBrownc804cde2012-05-21 09:28:33 +10004507 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4508 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004509 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004510 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004511 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004512
NeilBrownacb180b2009-04-14 16:28:34 +10004513 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004514 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004515}
4516
NeilBrown750f1992014-09-30 08:53:05 +10004517static struct md_sysfs_entry md_sync_completed =
4518 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004519
NeilBrowne464eaf2006-03-27 01:18:14 -08004520static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004521min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004522{
4523 return sprintf(page, "%llu\n",
4524 (unsigned long long)mddev->resync_min);
4525}
4526static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004527min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004528{
4529 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004530 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004531
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004532 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004533 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004534
4535 spin_lock(&mddev->lock);
4536 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004537 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004538 goto out_unlock;
4539
4540 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004541 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004542 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004543
NeilBrown50c37b12015-03-23 17:36:38 +11004544 /* Round down to multiple of 4K for safety */
4545 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004546 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004547
NeilBrown23da4222014-12-15 12:57:01 +11004548out_unlock:
4549 spin_unlock(&mddev->lock);
4550 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004551}
4552
4553static struct md_sysfs_entry md_min_sync =
4554__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4555
4556static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004557max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004558{
4559 if (mddev->resync_max == MaxSector)
4560 return sprintf(page, "max\n");
4561 else
4562 return sprintf(page, "%llu\n",
4563 (unsigned long long)mddev->resync_max);
4564}
4565static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004566max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004567{
NeilBrown23da4222014-12-15 12:57:01 +11004568 int err;
4569 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004570 if (strncmp(buf, "max", 3) == 0)
4571 mddev->resync_max = MaxSector;
4572 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004573 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004574 int chunk;
4575
4576 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004577 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004578 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004579 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004580 goto out_unlock;
4581
4582 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004583 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004584 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004585 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004586 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004587
4588 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004589 chunk = mddev->chunk_sectors;
4590 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004591 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004592
4593 err = -EINVAL;
4594 if (sector_div(temp, chunk))
4595 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004596 }
4597 mddev->resync_max = max;
4598 }
4599 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004600 err = 0;
4601out_unlock:
4602 spin_unlock(&mddev->lock);
4603 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004604}
4605
4606static struct md_sysfs_entry md_max_sync =
4607__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4608
4609static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004610suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004611{
4612 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4613}
4614
4615static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004616suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004617{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004618 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004619 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004620
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004621 err = kstrtoull(buf, 10, &new);
4622 if (err < 0)
4623 return err;
4624 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004625 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004626
NeilBrown67918752014-12-15 12:57:01 +11004627 err = mddev_lock(mddev);
4628 if (err)
4629 return err;
4630 err = -EINVAL;
4631 if (mddev->pers == NULL ||
4632 mddev->pers->quiesce == NULL)
4633 goto unlock;
4634 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004635 mddev->suspend_lo = new;
4636 if (new >= old)
4637 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004638 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004639 else {
4640 /* Expanding suspended region - need to wait */
4641 mddev->pers->quiesce(mddev, 1);
4642 mddev->pers->quiesce(mddev, 0);
4643 }
NeilBrown67918752014-12-15 12:57:01 +11004644 err = 0;
4645unlock:
4646 mddev_unlock(mddev);
4647 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004648}
4649static struct md_sysfs_entry md_suspend_lo =
4650__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4651
NeilBrowne464eaf2006-03-27 01:18:14 -08004652static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004653suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004654{
4655 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4656}
4657
4658static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004659suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004660{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004661 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004662 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004663
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004664 err = kstrtoull(buf, 10, &new);
4665 if (err < 0)
4666 return err;
4667 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004668 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004669
NeilBrown67918752014-12-15 12:57:01 +11004670 err = mddev_lock(mddev);
4671 if (err)
4672 return err;
4673 err = -EINVAL;
4674 if (mddev->pers == NULL ||
4675 mddev->pers->quiesce == NULL)
4676 goto unlock;
4677 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004678 mddev->suspend_hi = new;
4679 if (new <= old)
4680 /* Shrinking suspended region */
4681 mddev->pers->quiesce(mddev, 2);
4682 else {
4683 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004684 mddev->pers->quiesce(mddev, 1);
4685 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004686 }
NeilBrown67918752014-12-15 12:57:01 +11004687 err = 0;
4688unlock:
4689 mddev_unlock(mddev);
4690 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004691}
4692static struct md_sysfs_entry md_suspend_hi =
4693__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4694
NeilBrown08a02ec2007-05-09 02:35:38 -07004695static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004696reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004697{
4698 if (mddev->reshape_position != MaxSector)
4699 return sprintf(page, "%llu\n",
4700 (unsigned long long)mddev->reshape_position);
4701 strcpy(page, "none\n");
4702 return 5;
4703}
4704
4705static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004706reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004707{
NeilBrownc6563a82012-05-21 09:27:00 +10004708 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004709 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004710 int err;
NeilBrown67918752014-12-15 12:57:01 +11004711
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004712 err = kstrtoull(buf, 10, &new);
4713 if (err < 0)
4714 return err;
4715 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004716 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004717 err = mddev_lock(mddev);
4718 if (err)
4719 return err;
4720 err = -EBUSY;
4721 if (mddev->pers)
4722 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004723 mddev->reshape_position = new;
4724 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004725 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004726 mddev->new_level = mddev->level;
4727 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004728 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004729 rdev_for_each(rdev, mddev)
4730 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004731 err = 0;
4732unlock:
4733 mddev_unlock(mddev);
4734 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004735}
4736
4737static struct md_sysfs_entry md_reshape_position =
4738__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4739 reshape_position_store);
4740
Dan Williamsb522adc2009-03-31 15:00:31 +11004741static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004742reshape_direction_show(struct mddev *mddev, char *page)
4743{
4744 return sprintf(page, "%s\n",
4745 mddev->reshape_backwards ? "backwards" : "forwards");
4746}
4747
4748static ssize_t
4749reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4750{
4751 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004752 int err;
4753
NeilBrown2c810cd2012-05-21 09:27:00 +10004754 if (cmd_match(buf, "forwards"))
4755 backwards = 0;
4756 else if (cmd_match(buf, "backwards"))
4757 backwards = 1;
4758 else
4759 return -EINVAL;
4760 if (mddev->reshape_backwards == backwards)
4761 return len;
4762
NeilBrown67918752014-12-15 12:57:01 +11004763 err = mddev_lock(mddev);
4764 if (err)
4765 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004766 /* check if we are allowed to change */
4767 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004768 err = -EBUSY;
4769 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004770 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004771 err = -EINVAL;
4772 else
4773 mddev->reshape_backwards = backwards;
4774 mddev_unlock(mddev);
4775 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004776}
4777
4778static struct md_sysfs_entry md_reshape_direction =
4779__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4780 reshape_direction_store);
4781
4782static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004783array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004784{
4785 if (mddev->external_size)
4786 return sprintf(page, "%llu\n",
4787 (unsigned long long)mddev->array_sectors/2);
4788 else
4789 return sprintf(page, "default\n");
4790}
4791
4792static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004793array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004794{
4795 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004796 int err;
4797
4798 err = mddev_lock(mddev);
4799 if (err)
4800 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004801
4802 if (strncmp(buf, "default", 7) == 0) {
4803 if (mddev->pers)
4804 sectors = mddev->pers->size(mddev, 0, 0);
4805 else
4806 sectors = mddev->array_sectors;
4807
4808 mddev->external_size = 0;
4809 } else {
4810 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004811 err = -EINVAL;
4812 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4813 err = -E2BIG;
4814 else
4815 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004816 }
4817
NeilBrown67918752014-12-15 12:57:01 +11004818 if (!err) {
4819 mddev->array_sectors = sectors;
4820 if (mddev->pers) {
4821 set_capacity(mddev->gendisk, mddev->array_sectors);
4822 revalidate_disk(mddev->gendisk);
4823 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004824 }
NeilBrown67918752014-12-15 12:57:01 +11004825 mddev_unlock(mddev);
4826 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004827}
4828
4829static struct md_sysfs_entry md_array_size =
4830__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4831 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004832
NeilBrowneae17012005-11-08 21:39:23 -08004833static struct attribute *md_default_attrs[] = {
4834 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004835 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004836 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004837 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004838 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004839 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004840 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004841 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004842 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004843 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004844 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004845 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004846 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004847 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004848 NULL,
4849};
4850
4851static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004852 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004853 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004854 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004855 &md_sync_min.attr,
4856 &md_sync_max.attr,
4857 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004858 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004859 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004860 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004861 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004862 &md_suspend_lo.attr,
4863 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004864 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004865 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004866 NULL,
4867};
NeilBrown411036f2005-11-08 21:39:40 -08004868static struct attribute_group md_redundancy_group = {
4869 .name = NULL,
4870 .attrs = md_redundancy_attrs,
4871};
4872
NeilBrowneae17012005-11-08 21:39:23 -08004873static ssize_t
4874md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4875{
4876 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004877 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004878 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004879
4880 if (!entry->show)
4881 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004882 spin_lock(&all_mddevs_lock);
4883 if (list_empty(&mddev->all_mddevs)) {
4884 spin_unlock(&all_mddevs_lock);
4885 return -EBUSY;
4886 }
4887 mddev_get(mddev);
4888 spin_unlock(&all_mddevs_lock);
4889
NeilBrownb7b17c92014-12-15 12:56:59 +11004890 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004891 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004892 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004893}
4894
4895static ssize_t
4896md_attr_store(struct kobject *kobj, struct attribute *attr,
4897 const char *page, size_t length)
4898{
4899 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004900 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004901 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004902
4903 if (!entry->store)
4904 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004905 if (!capable(CAP_SYS_ADMIN))
4906 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004907 spin_lock(&all_mddevs_lock);
4908 if (list_empty(&mddev->all_mddevs)) {
4909 spin_unlock(&all_mddevs_lock);
4910 return -EBUSY;
4911 }
4912 mddev_get(mddev);
4913 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004914 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004915 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004916 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004917}
4918
4919static void md_free(struct kobject *ko)
4920{
NeilBrownfd01b882011-10-11 16:47:53 +11004921 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004922
4923 if (mddev->sysfs_state)
4924 sysfs_put(mddev->sysfs_state);
4925
NeilBrown6cd18e72015-04-27 14:12:22 +10004926 if (mddev->queue)
4927 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004928 if (mddev->gendisk) {
4929 del_gendisk(mddev->gendisk);
4930 put_disk(mddev->gendisk);
4931 }
NeilBrowna21d1502009-01-09 08:31:09 +11004932
NeilBrowneae17012005-11-08 21:39:23 -08004933 kfree(mddev);
4934}
4935
Emese Revfy52cf25d2010-01-19 02:58:23 +01004936static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004937 .show = md_attr_show,
4938 .store = md_attr_store,
4939};
4940static struct kobj_type md_ktype = {
4941 .release = md_free,
4942 .sysfs_ops = &md_sysfs_ops,
4943 .default_attrs = md_default_attrs,
4944};
4945
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946int mdp_major = 0;
4947
Dan Williams5fd3a172009-03-04 00:57:25 -07004948static void mddev_delayed_delete(struct work_struct *ws)
4949{
NeilBrownfd01b882011-10-11 16:47:53 +11004950 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004951
NeilBrown43a70502009-12-14 12:49:55 +11004952 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004953 kobject_del(&mddev->kobj);
4954 kobject_put(&mddev->kobj);
4955}
4956
NeilBrownefeb53c2009-01-09 08:31:10 +11004957static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004959 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004960 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004962 int partitioned;
4963 int shift;
4964 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004965 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
4967 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004968 return -ENODEV;
4969
4970 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4971 shift = partitioned ? MdpMinorShift : 0;
4972 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
Tejun Heoe804ac72010-10-15 15:36:08 +02004974 /* wait for any previous instance of this device to be
4975 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004976 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004977 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004978
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004979 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004980 error = -EEXIST;
4981 if (mddev->gendisk)
4982 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004983
4984 if (name) {
4985 /* Need to ensure that 'name' is not a duplicate.
4986 */
NeilBrownfd01b882011-10-11 16:47:53 +11004987 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004988 spin_lock(&all_mddevs_lock);
4989
4990 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4991 if (mddev2->gendisk &&
4992 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4993 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004994 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004995 }
4996 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 }
NeilBrown8b765392009-01-09 08:31:08 +11004998
NeilBrown0909dc42009-07-01 12:27:21 +10004999 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005000 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005001 if (!mddev->queue)
5002 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005003 mddev->queue->queuedata = mddev;
5004
NeilBrown409c57f2009-03-31 14:39:39 +11005005 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005006 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005007
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 disk = alloc_disk(1 << shift);
5009 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005010 blk_cleanup_queue(mddev->queue);
5011 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005012 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005014 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005016 if (name)
5017 strcpy(disk->disk_name, name);
5018 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07005020 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 disk->fops = &md_fops;
5023 disk->private_data = mddev;
5024 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10005025 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11005026 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005027 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005028 * remove it now.
5029 */
5030 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005032 /* As soon as we call add_disk(), another thread could get
5033 * through to md_open, so make sure it doesn't get too far
5034 */
5035 mutex_lock(&mddev->open_mutex);
5036 add_disk(disk);
5037
Tejun Heoed9e1982008-08-25 19:56:05 +09005038 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5039 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005040 if (error) {
5041 /* This isn't possible, but as kobject_init_and_add is marked
5042 * __must_check, we must do something with the result
5043 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08005044 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
5045 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005046 error = 0;
5047 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005048 if (mddev->kobj.sd &&
5049 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11005050 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005051 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005052 abort:
5053 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005054 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005055 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005056 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005057 }
NeilBrownd3374822009-01-09 08:31:10 +11005058 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005059 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005060}
5061
5062static struct kobject *md_probe(dev_t dev, int *part, void *data)
5063{
5064 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 return NULL;
5066}
5067
NeilBrownefeb53c2009-01-09 08:31:10 +11005068static int add_named_array(const char *val, struct kernel_param *kp)
5069{
5070 /* val must be "md_*" where * is not all digits.
5071 * We allocate an array with a large free minor number, and
5072 * set the name to val. val must not already be an active name.
5073 */
5074 int len = strlen(val);
5075 char buf[DISK_NAME_LEN];
5076
5077 while (len && val[len-1] == '\n')
5078 len--;
5079 if (len >= DISK_NAME_LEN)
5080 return -E2BIG;
5081 strlcpy(buf, val, len+1);
5082 if (strncmp(buf, "md_", 3) != 0)
5083 return -EINVAL;
5084 return md_alloc(0, buf);
5085}
5086
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087static void md_safemode_timeout(unsigned long data)
5088{
NeilBrownfd01b882011-10-11 16:47:53 +11005089 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
Neil Brown0fd62b82008-06-28 08:31:36 +10005091 if (!atomic_read(&mddev->writes_pending)) {
5092 mddev->safemode = 1;
5093 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10005094 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 md_wakeup_thread(mddev->thread);
5097}
5098
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005099static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
NeilBrownfd01b882011-10-11 16:47:53 +11005101int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102{
NeilBrown2604b702006-01-06 00:20:36 -08005103 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005104 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005105 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106
NeilBrowna757e642005-04-16 15:26:42 -07005107 if (list_empty(&mddev->disks))
5108 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110
5111 if (mddev->pers)
5112 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005113 /* Cannot run until previous stop completes properly */
5114 if (mddev->sysfs_active)
5115 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005116
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 /*
5118 * Analyze all RAID superblock(s)
5119 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005120 if (!mddev->raid_disks) {
5121 if (!mddev->persistent)
5122 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005123 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
NeilBrownd9d166c2006-01-06 00:20:51 -08005126 if (mddev->level != LEVEL_NONE)
5127 request_module("md-level-%d", mddev->level);
5128 else if (mddev->clevel[0])
5129 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130
5131 /*
5132 * Drop all container device buffers, from now on
5133 * the only valid external interface is through the md
5134 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005136 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005137 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 continue;
5139 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005140 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005141
5142 /* perform some consistency tests on the device.
5143 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005144 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005145 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005146 if (rdev->meta_bdev) {
5147 /* Nothing to check */;
5148 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005149 if (mddev->dev_sectors &&
5150 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005151 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005152 printk("md: %s: data overlaps metadata\n",
5153 mdname(mddev));
5154 return -EINVAL;
5155 }
5156 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005157 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005158 > rdev->data_offset) {
5159 printk("md: %s: metadata overlaps data\n",
5160 mdname(mddev));
5161 return -EINVAL;
5162 }
5163 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005164 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 }
5166
NeilBrowna167f662010-10-26 18:31:13 +11005167 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005168 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005169
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005171 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005172 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005174 if (mddev->level != LEVEL_NONE)
5175 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5176 mddev->level);
5177 else
5178 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5179 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 return -EINVAL;
5181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005183 if (mddev->level != pers->level) {
5184 mddev->level = pers->level;
5185 mddev->new_level = pers->level;
5186 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005187 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
NeilBrownf6705572006-03-27 01:18:11 -08005189 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005190 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005191 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005192 module_put(pers->owner);
5193 return -EINVAL;
5194 }
5195
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005196 if (pers->sync_request) {
5197 /* Warn if this is a potentially silly
5198 * configuration.
5199 */
5200 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005201 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005202 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005203
NeilBrowndafb20f2012-03-19 12:46:39 +11005204 rdev_for_each(rdev, mddev)
5205 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005206 if (rdev < rdev2 &&
5207 rdev->bdev->bd_contains ==
5208 rdev2->bdev->bd_contains) {
5209 printk(KERN_WARNING
5210 "%s: WARNING: %s appears to be"
5211 " on the same physical disk as"
5212 " %s.\n",
5213 mdname(mddev),
5214 bdevname(rdev->bdev,b),
5215 bdevname(rdev2->bdev,b2));
5216 warned = 1;
5217 }
5218 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005219
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005220 if (warned)
5221 printk(KERN_WARNING
5222 "True protection against single-disk"
5223 " failure might be compromised.\n");
5224 }
5225
NeilBrown657390d2005-08-26 18:34:16 -07005226 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005227 /* may be over-ridden by personality */
5228 mddev->resync_max_sectors = mddev->dev_sectors;
5229
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005230 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231
NeilBrown0f9552b2009-12-30 12:08:50 +11005232 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005233 mddev->ro = 2; /* read-only, but switch on first write */
5234
NeilBrown36d091f2014-12-15 12:56:58 +11005235 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005236 if (err)
5237 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005238 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005239 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5240 " but 'external_size' not in effect?\n", __func__);
5241 printk(KERN_ERR
5242 "md: invalid array_size %llu > default size %llu\n",
5243 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005244 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005245 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005246 }
NeilBrown36d091f2014-12-15 12:56:58 +11005247 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005248 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005249 struct bitmap *bitmap;
5250
5251 bitmap = bitmap_create(mddev, -1);
5252 if (IS_ERR(bitmap)) {
5253 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005254 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5255 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005256 } else
5257 mddev->bitmap = bitmap;
5258
NeilBrownb15c2e52006-01-06 00:20:16 -08005259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005261 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005262 if (mddev->private)
5263 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005264 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005265 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005266 bitmap_destroy(mddev);
5267 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 }
NeilBrown5c675f82014-12-15 12:56:56 +11005269 if (mddev->queue) {
5270 mddev->queue->backing_dev_info.congested_data = mddev;
5271 mddev->queue->backing_dev_info.congested_fn = md_congested;
5272 }
NeilBrown36d091f2014-12-15 12:56:58 +11005273 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005274 if (mddev->kobj.sd &&
5275 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005276 printk(KERN_WARNING
5277 "md: cannot register extra attributes for %s\n",
5278 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005279 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005280 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005281 mddev->ro = 0;
5282
NeilBrownf72ffdd2014-09-30 14:23:59 +10005283 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005284 atomic_set(&mddev->max_corr_read_errors,
5285 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005287 if (mddev_is_clustered(mddev))
5288 mddev->safemode_delay = 0;
5289 else
5290 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005292 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005293 spin_lock(&mddev->lock);
5294 mddev->pers = pers;
NeilBrown0ca69882011-01-14 09:14:33 +11005295 mddev->ready = 1;
NeilBrown36d091f2014-12-15 12:56:58 +11005296 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005297 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005298 if (rdev->raid_disk >= 0)
5299 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005300 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005301
NeilBrowna4a3d262015-07-17 11:57:30 +10005302 if (mddev->degraded && !mddev->ro)
5303 /* This ensures that recovering status is reported immediately
5304 * via sysfs - until a lack of spares is confirmed.
5305 */
5306 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005308
NeilBrown7a0a5352013-08-27 16:28:23 +10005309 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07005310 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311
NeilBrownd7603b72006-01-06 00:20:30 -08005312 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005313 sysfs_notify_dirent_safe(mddev->sysfs_state);
5314 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005315 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 return 0;
5317}
NeilBrown390ee602010-06-01 19:37:27 +10005318EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319
NeilBrownfd01b882011-10-11 16:47:53 +11005320static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005321{
5322 int err;
5323
5324 err = md_run(mddev);
5325 if (err)
5326 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005327 err = bitmap_load(mddev);
5328 if (err) {
5329 bitmap_destroy(mddev);
5330 goto out;
5331 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005332
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005333 if (mddev_is_clustered(mddev))
5334 md_allow_write(mddev);
5335
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005336 md_wakeup_thread(mddev->thread);
5337 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5338
NeilBrownfe60b012010-03-29 11:10:42 +11005339 set_capacity(mddev->gendisk, mddev->array_sectors);
5340 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005341 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005342 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5343out:
5344 return err;
5345}
5346
NeilBrownfd01b882011-10-11 16:47:53 +11005347static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348{
5349 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
Andre Noll80fab1d2008-07-11 22:02:21 +10005351 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005353 return -ENXIO;
5354 if (!mddev->pers)
5355 return -EINVAL;
5356 if (!mddev->ro)
5357 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005358 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5359 struct md_rdev *rdev;
5360 bool has_journal = false;
5361
5362 rcu_read_lock();
5363 rdev_for_each_rcu(rdev, mddev) {
5364 if (test_bit(Journal, &rdev->flags) &&
5365 !test_bit(Faulty, &rdev->flags)) {
5366 has_journal = true;
5367 break;
5368 }
5369 }
5370 rcu_read_unlock();
5371
5372 /* Don't restart rw with journal missing/faulty */
5373 if (!has_journal)
5374 return -EINVAL;
5375 }
5376
Andre Noll80fab1d2008-07-11 22:02:21 +10005377 mddev->safemode = 0;
5378 mddev->ro = 0;
5379 set_disk_ro(disk, 0);
5380 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5381 mdname(mddev));
5382 /* Kick recovery or resync if necessary */
5383 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5384 md_wakeup_thread(mddev->thread);
5385 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005386 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388}
5389
NeilBrownfd01b882011-10-11 16:47:53 +11005390static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005391{
5392 mddev->array_sectors = 0;
5393 mddev->external_size = 0;
5394 mddev->dev_sectors = 0;
5395 mddev->raid_disks = 0;
5396 mddev->recovery_cp = 0;
5397 mddev->resync_min = 0;
5398 mddev->resync_max = MaxSector;
5399 mddev->reshape_position = MaxSector;
5400 mddev->external = 0;
5401 mddev->persistent = 0;
5402 mddev->level = LEVEL_NONE;
5403 mddev->clevel[0] = 0;
5404 mddev->flags = 0;
5405 mddev->ro = 0;
5406 mddev->metadata_type[0] = 0;
5407 mddev->chunk_sectors = 0;
5408 mddev->ctime = mddev->utime = 0;
5409 mddev->layout = 0;
5410 mddev->max_disks = 0;
5411 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005412 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005413 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005414 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005415 mddev->new_level = LEVEL_NONE;
5416 mddev->new_layout = 0;
5417 mddev->new_chunk_sectors = 0;
5418 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005419 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005420 mddev->suspend_lo = mddev->suspend_hi = 0;
5421 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5422 mddev->recovery = 0;
5423 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005424 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005425 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005426 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005427 mddev->private = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005428 mddev->bitmap_info.offset = 0;
5429 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005430 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005431 mddev->bitmap_info.chunksize = 0;
5432 mddev->bitmap_info.daemon_sleep = 0;
5433 mddev->bitmap_info.max_write_behind = 0;
5434}
5435
NeilBrownfd01b882011-10-11 16:47:53 +11005436static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005437{
NeilBrown6b6204e2013-05-09 09:48:30 +10005438 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005439 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005440 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005441 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005442 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005443 }
5444
5445 del_timer_sync(&mddev->safemode_timer);
5446
5447 bitmap_flush(mddev);
5448 md_super_wait(mddev);
5449
NeilBrownb6d428c2013-04-24 11:42:42 +10005450 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005451 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5452 (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005453 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005454 if (!mddev_is_clustered(mddev))
5455 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005456 md_update_sb(mddev, 1);
5457 }
5458}
NeilBrowndefad612011-01-14 09:14:33 +11005459
NeilBrownfd01b882011-10-11 16:47:53 +11005460void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005461{
NeilBrown29f097c2013-11-14 17:54:51 +11005462 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005463 __md_stop_writes(mddev);
5464 mddev_unlock(mddev);
5465}
NeilBrown390ee602010-06-01 19:37:27 +10005466EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005467
NeilBrown5aa61f42014-12-15 12:56:57 +11005468static void mddev_detach(struct mddev *mddev)
5469{
5470 struct bitmap *bitmap = mddev->bitmap;
5471 /* wait for behind writes to complete */
5472 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5473 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5474 mdname(mddev));
5475 /* need to kick something here to make sure I/O goes? */
5476 wait_event(bitmap->behind_wait,
5477 atomic_read(&bitmap->behind_writes) == 0);
5478 }
NeilBrown36d091f2014-12-15 12:56:58 +11005479 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005480 mddev->pers->quiesce(mddev, 1);
5481 mddev->pers->quiesce(mddev, 0);
5482 }
5483 md_unregister_thread(&mddev->thread);
5484 if (mddev->queue)
5485 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5486}
5487
NeilBrown5eff3c42012-11-19 10:47:48 +11005488static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005489{
NeilBrown36d091f2014-12-15 12:56:58 +11005490 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005491 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005492 /* Ensure ->event_work is done */
5493 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005494 spin_lock(&mddev->lock);
5495 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005496 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005497 spin_unlock(&mddev->lock);
5498 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005499 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005500 if (pers->sync_request && mddev->to_remove == NULL)
5501 mddev->to_remove = &md_redundancy_group;
5502 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005503 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005504}
NeilBrown5eff3c42012-11-19 10:47:48 +11005505
5506void md_stop(struct mddev *mddev)
5507{
5508 /* stop the array and free an attached data structures.
5509 * This is called from dm-raid
5510 */
5511 __md_stop(mddev);
5512 bitmap_destroy(mddev);
5513 if (mddev->bio_set)
5514 bioset_free(mddev->bio_set);
5515}
5516
NeilBrown390ee602010-06-01 19:37:27 +10005517EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005518
NeilBrowna05b7ea02012-07-19 15:59:18 +10005519static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005520{
5521 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005522 int did_freeze = 0;
5523
5524 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5525 did_freeze = 1;
5526 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5527 md_wakeup_thread(mddev->thread);
5528 }
NeilBrownf851b602014-12-11 10:02:10 +11005529 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005530 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005531 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005532 /* Thread might be blocked waiting for metadata update
5533 * which will now never happen */
5534 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005535
NeilBrown88724bf2015-09-24 14:00:51 +10005536 if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags))
5537 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005538 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005539 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5540 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005541 wait_event(mddev->sb_wait,
5542 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005543 mddev_lock_nointr(mddev);
5544
NeilBrowna4bd82d2010-03-29 13:23:10 +11005545 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005546 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005547 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005548 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005549 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005550 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005551 if (did_freeze) {
5552 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005553 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005554 md_wakeup_thread(mddev->thread);
5555 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005556 err = -EBUSY;
5557 goto out;
5558 }
5559 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005560 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005561
5562 err = -ENXIO;
5563 if (mddev->ro==1)
5564 goto out;
5565 mddev->ro = 1;
5566 set_disk_ro(mddev->gendisk, 1);
5567 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005568 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5569 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005570 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005571 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005572 }
5573out:
5574 mutex_unlock(&mddev->open_mutex);
5575 return err;
5576}
5577
NeilBrown9e653b62006-06-26 00:27:58 -07005578/* mode:
5579 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005580 * 2 - stop but do not disassemble array
5581 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005582static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea02012-07-19 15:59:18 +10005583 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005586 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005587 int did_freeze = 0;
5588
5589 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5590 did_freeze = 1;
5591 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5592 md_wakeup_thread(mddev->thread);
5593 }
NeilBrownf851b602014-12-11 10:02:10 +11005594 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005595 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005596 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005597 /* Thread might be blocked waiting for metadata update
5598 * which will now never happen */
5599 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005600
NeilBrown30b8feb2013-11-14 15:16:17 +11005601 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005602 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5603 !test_bit(MD_RECOVERY_RUNNING,
5604 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005605 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606
NeilBrownc8c00a62009-08-10 12:50:52 +10005607 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005608 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005609 mddev->sysfs_active ||
5610 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005611 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005612 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005613 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005614 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005615 if (did_freeze) {
5616 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005617 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005618 md_wakeup_thread(mddev->thread);
5619 }
NeilBrown260fa032013-08-27 16:44:13 +10005620 return -EBUSY;
5621 }
NeilBrown6e17b022010-08-07 21:41:19 +10005622 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005623 if (mddev->ro)
5624 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005625
NeilBrowndefad612011-01-14 09:14:33 +11005626 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005627 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005628 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005629
NeilBrowna4bd82d2010-03-29 13:23:10 +11005630 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005631 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005632
NeilBrowndafb20f2012-03-19 12:46:39 +11005633 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005634 if (rdev->raid_disk >= 0)
5635 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005636
NeilBrowna4bd82d2010-03-29 13:23:10 +11005637 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005638 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005639 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005640 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005641
NeilBrowna4bd82d2010-03-29 13:23:10 +11005642 if (mddev->ro)
5643 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005644 } else
5645 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 /*
5647 * Free resources if final stop
5648 */
NeilBrown9e653b62006-06-26 00:27:58 -07005649 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5651
NeilBrown978f9462006-02-02 14:28:05 -08005652 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005653 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005654 struct file *f = mddev->bitmap_info.file;
5655 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005656 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005657 spin_unlock(&mddev->lock);
5658 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005659 }
NeilBrownc3d97142009-12-14 12:49:52 +11005660 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005661
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 export_array(mddev);
5663
NeilBrown6177b472010-03-29 11:37:13 +11005664 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005665 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005666 if (mddev->hold_active == UNTIL_STOP)
5667 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005668 }
NeilBrownd7603b72006-01-06 00:20:30 -08005669 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005670 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672}
5673
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005674#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005675static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676{
NeilBrown3cb03002011-10-11 16:45:26 +11005677 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 int err;
5679
NeilBrowna757e642005-04-16 15:26:42 -07005680 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
5683 printk(KERN_INFO "md: running: ");
5684
NeilBrowndafb20f2012-03-19 12:46:39 +11005685 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 char b[BDEVNAME_SIZE];
5687 printk("<%s>", bdevname(rdev->bdev,b));
5688 }
5689 printk("\n");
5690
NeilBrownd710e132008-10-13 11:55:12 +11005691 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 if (err) {
5693 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005694 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 }
5696}
5697
5698/*
5699 * lets try to run arrays based on all disks that have arrived
5700 * until now. (those are in pending_raid_disks)
5701 *
5702 * the method: pick the first pending disk, collect all disks with
5703 * the same UUID, remove all from the pending list and put them into
5704 * the 'same_array' list. Then order this list based on superblock
5705 * update time (freshest comes first), kick out 'old' disks and
5706 * compare superblocks. If everything's fine then run it.
5707 *
5708 * If "unit" is allocated, then bump its reference count
5709 */
5710static void autorun_devices(int part)
5711{
NeilBrown3cb03002011-10-11 16:45:26 +11005712 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005713 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 char b[BDEVNAME_SIZE];
5715
5716 printk(KERN_INFO "md: autorun ...\n");
5717 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005718 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005720 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005722 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723
5724 printk(KERN_INFO "md: considering %s ...\n",
5725 bdevname(rdev0->bdev,b));
5726 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005727 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 if (super_90_load(rdev, rdev0, 0) >= 0) {
5729 printk(KERN_INFO "md: adding %s ...\n",
5730 bdevname(rdev->bdev,b));
5731 list_move(&rdev->same_set, &candidates);
5732 }
5733 /*
5734 * now we have a set of devices, with all of them having
5735 * mostly sane superblocks. It's time to allocate the
5736 * mddev.
5737 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005738 if (part) {
5739 dev = MKDEV(mdp_major,
5740 rdev0->preferred_minor << MdpMinorShift);
5741 unit = MINOR(dev) >> MdpMinorShift;
5742 } else {
5743 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5744 unit = MINOR(dev);
5745 }
5746 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5748 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5749 break;
5750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751
5752 md_probe(dev, NULL, NULL);
5753 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005754 if (!mddev || !mddev->gendisk) {
5755 if (mddev)
5756 mddev_put(mddev);
5757 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 "md: cannot allocate memory for md drive.\n");
5759 break;
5760 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005761 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 printk(KERN_WARNING "md: %s locked, cannot run\n",
5763 mdname(mddev));
5764 else if (mddev->raid_disks || mddev->major_version
5765 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005766 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 "md: %s already running, cannot run %s\n",
5768 mdname(mddev), bdevname(rdev0->bdev,b));
5769 mddev_unlock(mddev);
5770 } else {
5771 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005772 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005773 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 list_del_init(&rdev->same_set);
5775 if (bind_rdev_to_array(rdev, mddev))
5776 export_rdev(rdev);
5777 }
5778 autorun_array(mddev);
5779 mddev_unlock(mddev);
5780 }
5781 /* on success, candidates will be empty, on error
5782 * it won't...
5783 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005784 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005785 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 mddev_put(mddev);
5789 }
5790 printk(KERN_INFO "md: ... autorun DONE.\n");
5791}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005792#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793
NeilBrownf72ffdd2014-09-30 14:23:59 +10005794static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795{
5796 mdu_version_t ver;
5797
5798 ver.major = MD_MAJOR_VERSION;
5799 ver.minor = MD_MINOR_VERSION;
5800 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5801
5802 if (copy_to_user(arg, &ver, sizeof(ver)))
5803 return -EFAULT;
5804
5805 return 0;
5806}
5807
NeilBrownf72ffdd2014-09-30 14:23:59 +10005808static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809{
5810 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005811 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005812 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813
NeilBrown1ca69c42012-10-11 13:37:33 +11005814 nr = working = insync = failed = spare = 0;
5815 rcu_read_lock();
5816 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005818 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 failed++;
5820 else {
5821 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005822 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005823 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 else
5825 spare++;
5826 }
5827 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005828 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829
5830 info.major_version = mddev->major_version;
5831 info.minor_version = mddev->minor_version;
5832 info.patch_version = MD_PATCHLEVEL_VERSION;
5833 info.ctime = mddev->ctime;
5834 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005835 info.size = mddev->dev_sectors / 2;
5836 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005837 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 info.nr_disks = nr;
5839 info.raid_disks = mddev->raid_disks;
5840 info.md_minor = mddev->md_minor;
5841 info.not_persistent= !mddev->persistent;
5842
5843 info.utime = mddev->utime;
5844 info.state = 0;
5845 if (mddev->in_sync)
5846 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005847 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005848 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005849 if (mddev_is_clustered(mddev))
5850 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005851 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 info.working_disks = working;
5853 info.failed_disks = failed;
5854 info.spare_disks = spare;
5855
5856 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005857 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858
5859 if (copy_to_user(arg, &info, sizeof(info)))
5860 return -EFAULT;
5861
5862 return 0;
5863}
5864
NeilBrownf72ffdd2014-09-30 14:23:59 +10005865static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005866{
5867 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005868 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005869 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005870
Benjamin Randazzob6878d92015-07-25 16:36:50 +02005871 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005872 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005873 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005874
NeilBrown32a76272005-06-21 17:17:14 -07005875 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005876 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02005877 /* bitmap enabled */
5878 if (mddev->bitmap_info.file) {
5879 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5880 sizeof(file->pathname));
5881 if (IS_ERR(ptr))
5882 err = PTR_ERR(ptr);
5883 else
5884 memmove(file->pathname, ptr,
5885 sizeof(file->pathname)-(ptr-file->pathname));
5886 }
NeilBrown4af1a042014-12-15 12:57:00 +11005887 spin_unlock(&mddev->lock);
5888
5889 if (err == 0 &&
5890 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005891 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005892
NeilBrown32a76272005-06-21 17:17:14 -07005893 kfree(file);
5894 return err;
5895}
5896
NeilBrownf72ffdd2014-09-30 14:23:59 +10005897static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898{
5899 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005900 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
5902 if (copy_from_user(&info, arg, sizeof(info)))
5903 return -EFAULT;
5904
NeilBrown1ca69c42012-10-11 13:37:33 +11005905 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005906 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 if (rdev) {
5908 info.major = MAJOR(rdev->bdev->bd_dev);
5909 info.minor = MINOR(rdev->bdev->bd_dev);
5910 info.raid_disk = rdev->raid_disk;
5911 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005912 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005914 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 info.state |= (1<<MD_DISK_ACTIVE);
5916 info.state |= (1<<MD_DISK_SYNC);
5917 }
Shaohua Li9efdca12015-10-12 16:59:50 -07005918 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07005919 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005920 if (test_bit(WriteMostly, &rdev->flags))
5921 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 } else {
5923 info.major = info.minor = 0;
5924 info.raid_disk = -1;
5925 info.state = (1<<MD_DISK_REMOVED);
5926 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005927 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928
5929 if (copy_to_user(arg, &info, sizeof(info)))
5930 return -EFAULT;
5931
5932 return 0;
5933}
5934
NeilBrownf72ffdd2014-09-30 14:23:59 +10005935static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936{
5937 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005938 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 dev_t dev = MKDEV(info->major,info->minor);
5940
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005941 if (mddev_is_clustered(mddev) &&
5942 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005943 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005944 mdname(mddev));
5945 return -EINVAL;
5946 }
5947
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5949 return -EOVERFLOW;
5950
5951 if (!mddev->raid_disks) {
5952 int err;
5953 /* expecting a device which has a superblock */
5954 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5955 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005956 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 "md: md_import_device returned %ld\n",
5958 PTR_ERR(rdev));
5959 return PTR_ERR(rdev);
5960 }
5961 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005962 struct md_rdev *rdev0
5963 = list_entry(mddev->disks.next,
5964 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005965 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 .load_super(rdev, rdev0, mddev->minor_version);
5967 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005968 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005970 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 bdevname(rdev0->bdev,b2));
5972 export_rdev(rdev);
5973 return -EINVAL;
5974 }
5975 }
5976 err = bind_rdev_to_array(rdev, mddev);
5977 if (err)
5978 export_rdev(rdev);
5979 return err;
5980 }
5981
5982 /*
5983 * add_new_disk can be used once the array is assembled
5984 * to add "hot spares". They must already have a superblock
5985 * written
5986 */
5987 if (mddev->pers) {
5988 int err;
5989 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005990 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 "%s: personality does not support diskops!\n",
5992 mdname(mddev));
5993 return -EINVAL;
5994 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005995 if (mddev->persistent)
5996 rdev = md_import_device(dev, mddev->major_version,
5997 mddev->minor_version);
5998 else
5999 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006001 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 "md: md_import_device returned %ld\n",
6003 PTR_ERR(rdev));
6004 return PTR_ERR(rdev);
6005 }
NeilBrown1a855a02010-12-09 16:36:28 +11006006 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006007 if (!mddev->persistent) {
6008 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006009 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006010 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006011 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006012 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006013 } else
NeilBrown41158c72005-06-21 17:17:25 -07006014 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006015 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006016 } else
6017 super_types[mddev->major_version].
6018 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006019 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006020 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006021 /* This was a hot-add request, but events doesn't
6022 * match, so reject it.
6023 */
6024 export_rdev(rdev);
6025 return -EINVAL;
6026 }
6027
NeilBrownb2d444d2005-11-08 21:39:31 -08006028 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006029 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6030 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006031 else
6032 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006033
Song Liubac624f2015-08-13 14:31:55 -07006034 if (info->state & (1<<MD_DISK_JOURNAL))
6035 set_bit(Journal, &rdev->flags);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006036 /*
6037 * check whether the device shows up in other nodes
6038 */
6039 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006040 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006041 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006042 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006043 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006044 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006045 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006046 export_rdev(rdev);
6047 return err;
6048 }
6049 }
6050 }
6051
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 rdev->raid_disk = -1;
6053 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006054
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 if (err)
6056 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006057
6058 if (mddev_is_clustered(mddev)) {
6059 if (info->state & (1 << MD_DISK_CANDIDATE))
6060 md_cluster_ops->new_disk_ack(mddev, (err == 0));
6061 else {
6062 if (err)
6063 md_cluster_ops->add_new_disk_cancel(mddev);
6064 else
6065 err = add_bound_rdev(rdev);
6066 }
6067
6068 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006069 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006070
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 return err;
6072 }
6073
6074 /* otherwise, add_new_disk is only allowed
6075 * for major_version==0 superblocks
6076 */
6077 if (mddev->major_version != 0) {
6078 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
6079 mdname(mddev));
6080 return -EINVAL;
6081 }
6082
6083 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6084 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006085 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006087 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 "md: error, md_import_device() returned %ld\n",
6089 PTR_ERR(rdev));
6090 return PTR_ERR(rdev);
6091 }
6092 rdev->desc_nr = info->number;
6093 if (info->raid_disk < mddev->raid_disks)
6094 rdev->raid_disk = info->raid_disk;
6095 else
6096 rdev->raid_disk = -1;
6097
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006099 if (info->state & (1<<MD_DISK_SYNC))
6100 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006102 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6103 set_bit(WriteMostly, &rdev->flags);
6104
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 if (!mddev->persistent) {
6106 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006107 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6108 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006109 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006110 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111
NeilBrown2bf071b2006-01-06 00:20:55 -08006112 err = bind_rdev_to_array(rdev, mddev);
6113 if (err) {
6114 export_rdev(rdev);
6115 return err;
6116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 }
6118
6119 return 0;
6120}
6121
NeilBrownf72ffdd2014-09-30 14:23:59 +10006122static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123{
6124 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006125 struct md_rdev *rdev;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08006126 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 rdev = find_rdev(mddev, dev);
6129 if (!rdev)
6130 return -ENXIO;
6131
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006132 if (mddev_is_clustered(mddev))
Guoqing Jiang23b63f92015-10-12 17:21:30 +08006133 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006134
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006135 if (rdev->raid_disk < 0)
6136 goto kick_rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137
NeilBrown3ea8929d2013-04-24 11:42:41 +10006138 clear_bit(Blocked, &rdev->flags);
6139 remove_and_add_spares(mddev, rdev);
6140
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 if (rdev->raid_disk >= 0)
6142 goto busy;
6143
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006144kick_rdev:
Guoqing Jiang23b63f92015-10-12 17:21:30 +08006145 if (mddev_is_clustered(mddev) && ret == 0)
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006146 md_cluster_ops->remove_disk(mddev, rdev);
6147
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006148 md_kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07006149 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006150 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151
6152 return 0;
6153busy:
Guoqing Jiang23b63f92015-10-12 17:21:30 +08006154 if (mddev_is_clustered(mddev) && ret == 0)
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006155 md_cluster_ops->metadata_update_cancel(mddev);
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006156
Nick Andrewfdefa4d2008-04-21 22:42:58 +00006157 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 bdevname(rdev->bdev,b), mdname(mddev));
6159 return -EBUSY;
6160}
6161
NeilBrownf72ffdd2014-09-30 14:23:59 +10006162static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163{
6164 char b[BDEVNAME_SIZE];
6165 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006166 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167
6168 if (!mddev->pers)
6169 return -ENODEV;
6170
6171 if (mddev->major_version != 0) {
6172 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6173 " version-0 superblocks.\n",
6174 mdname(mddev));
6175 return -EINVAL;
6176 }
6177 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006178 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 "%s: personality does not support diskops!\n",
6180 mdname(mddev));
6181 return -EINVAL;
6182 }
6183
NeilBrownd710e132008-10-13 11:55:12 +11006184 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006186 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 "md: error, md_import_device() returned %ld\n",
6188 PTR_ERR(rdev));
6189 return -EINVAL;
6190 }
6191
6192 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006193 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006195 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196
NeilBrown8190e752009-06-18 08:48:58 +10006197 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198
NeilBrownb2d444d2005-11-08 21:39:31 -08006199 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006200 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 "md: can not hot-add faulty %s disk to %s!\n",
6202 bdevname(rdev->bdev,b), mdname(mddev));
6203 err = -EINVAL;
6204 goto abort_export;
6205 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006206
NeilBrownb2d444d2005-11-08 21:39:31 -08006207 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006209 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006210 err = bind_rdev_to_array(rdev, mddev);
6211 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006212 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213
6214 /*
6215 * The rest should better be atomic, we can have disk failures
6216 * noticed in interrupt contexts ...
6217 */
6218
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 rdev->raid_disk = -1;
6220
NeilBrown850b2b42006-10-03 01:15:46 -07006221 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 /*
6223 * Kick recovery, maybe this spare has to be added to the
6224 * array immediately.
6225 */
6226 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6227 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006228 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 return 0;
6230
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231abort_export:
6232 export_rdev(rdev);
6233 return err;
6234}
6235
NeilBrownfd01b882011-10-11 16:47:53 +11006236static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006237{
NeilBrown035328c2014-04-09 12:25:40 +10006238 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006239
NeilBrown36fa3062005-09-09 16:23:45 -07006240 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006241 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006242 return -EBUSY;
6243 if (mddev->recovery || mddev->sync_thread)
6244 return -EBUSY;
6245 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006246 }
6247
NeilBrown36fa3062005-09-09 16:23:45 -07006248 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006249 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006250 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006251
NeilBrown1e594bb2014-12-15 12:57:00 +11006252 if (mddev->bitmap || mddev->bitmap_info.file)
6253 return -EEXIST; /* cannot add when bitmap is present */
6254 f = fget(fd);
6255
6256 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006257 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6258 mdname(mddev));
6259 return -EBADF;
6260 }
6261
NeilBrown1e594bb2014-12-15 12:57:00 +11006262 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006263 if (!S_ISREG(inode->i_mode)) {
6264 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6265 mdname(mddev));
6266 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006267 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006268 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6269 mdname(mddev));
6270 err = -EBADF;
6271 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006272 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6273 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006274 err = -EBUSY;
6275 }
6276 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006277 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006278 return err;
6279 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006280 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006281 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006282 } else if (mddev->bitmap == NULL)
6283 return -ENOENT; /* cannot remove what isn't there */
6284 err = 0;
6285 if (mddev->pers) {
6286 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006287 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006288 struct bitmap *bitmap;
6289
6290 bitmap = bitmap_create(mddev, -1);
6291 if (!IS_ERR(bitmap)) {
6292 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006293 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006294 } else
6295 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006296 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006297 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006298 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006299 fd = -1; /* make sure to put the file */
6300 }
NeilBrown36fa3062005-09-09 16:23:45 -07006301 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006302 }
6303 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006304 struct file *f = mddev->bitmap_info.file;
6305 if (f) {
6306 spin_lock(&mddev->lock);
6307 mddev->bitmap_info.file = NULL;
6308 spin_unlock(&mddev->lock);
6309 fput(f);
6310 }
NeilBrown36fa3062005-09-09 16:23:45 -07006311 }
6312
NeilBrown32a76272005-06-21 17:17:14 -07006313 return err;
6314}
6315
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316/*
6317 * set_array_info is used two different ways
6318 * The original usage is when creating a new array.
6319 * In this usage, raid_disks is > 0 and it together with
6320 * level, size, not_persistent,layout,chunksize determine the
6321 * shape of the array.
6322 * This will always create an array with a type-0.90.0 superblock.
6323 * The newer usage is when assembling an array.
6324 * In this case raid_disks will be 0, and the major_version field is
6325 * use to determine which style super-blocks are to be found on the devices.
6326 * The minor and patch _version numbers are also kept incase the
6327 * super_block handler wishes to interpret them.
6328 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006329static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330{
6331
6332 if (info->raid_disks == 0) {
6333 /* just setting version number for superblock loading */
6334 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006335 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 super_types[info->major_version].name == NULL) {
6337 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006338 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 "md: superblock version %d not known\n",
6340 info->major_version);
6341 return -EINVAL;
6342 }
6343 mddev->major_version = info->major_version;
6344 mddev->minor_version = info->minor_version;
6345 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006346 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006347 /* ensure mddev_put doesn't delete this now that there
6348 * is some minimal configuration.
6349 */
6350 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 return 0;
6352 }
6353 mddev->major_version = MD_MAJOR_VERSION;
6354 mddev->minor_version = MD_MINOR_VERSION;
6355 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6356 mddev->ctime = get_seconds();
6357
6358 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006359 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006360 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 mddev->raid_disks = info->raid_disks;
6362 /* don't set md_minor, it is determined by which /dev/md* was
6363 * openned
6364 */
6365 if (info->state & (1<<MD_SB_CLEAN))
6366 mddev->recovery_cp = MaxSector;
6367 else
6368 mddev->recovery_cp = 0;
6369 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006370 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371
6372 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006373 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374
6375 mddev->max_disks = MD_SB_DISKS;
6376
NeilBrowne6910632008-02-06 01:39:51 -08006377 if (mddev->persistent)
6378 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006379 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380
NeilBrownc3d97142009-12-14 12:49:52 +11006381 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006382 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006383 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006384
NeilBrownf6705572006-03-27 01:18:11 -08006385 mddev->reshape_position = MaxSector;
6386
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 /*
6388 * Generate a 128 bit UUID
6389 */
6390 get_random_bytes(mddev->uuid, 16);
6391
NeilBrownf6705572006-03-27 01:18:11 -08006392 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006393 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006394 mddev->new_layout = mddev->layout;
6395 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006396 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006397
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 return 0;
6399}
6400
NeilBrownfd01b882011-10-11 16:47:53 +11006401void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006402{
Dan Williamsb522adc2009-03-31 15:00:31 +11006403 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6404
6405 if (mddev->external_size)
6406 return;
6407
Dan Williams1f403622009-03-31 14:59:03 +11006408 mddev->array_sectors = array_sectors;
6409}
6410EXPORT_SYMBOL(md_set_array_sectors);
6411
NeilBrownfd01b882011-10-11 16:47:53 +11006412static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006413{
NeilBrown3cb03002011-10-11 16:45:26 +11006414 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006415 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006416 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006417
6418 if (mddev->pers->resize == NULL)
6419 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006420 /* The "num_sectors" is the number of sectors of each device that
6421 * is used. This can only make sense for arrays with redundancy.
6422 * linear and raid0 always use whatever space is available. We can only
6423 * consider changing this number if no resync or reconstruction is
6424 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006425 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006426 * of each device. If num_sectors is zero, we find the largest size
6427 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006428 */
NeilBrownf851b602014-12-11 10:02:10 +11006429 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6430 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006431 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006432 if (mddev->ro)
6433 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006434
NeilBrowndafb20f2012-03-19 12:46:39 +11006435 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006436 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006437
Andre Nolld71f9f82008-07-11 22:02:22 +10006438 if (fit && (num_sectors == 0 || num_sectors > avail))
6439 num_sectors = avail;
6440 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006441 return -ENOSPC;
6442 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006443 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006444 if (!rv)
6445 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006446 return rv;
6447}
6448
NeilBrownfd01b882011-10-11 16:47:53 +11006449static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006450{
6451 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006452 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006453 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006454 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006455 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006456 if (mddev->ro)
6457 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006458 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006459 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006460 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006461 if (mddev->sync_thread ||
6462 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6463 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006464 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006465
6466 rdev_for_each(rdev, mddev) {
6467 if (mddev->raid_disks < raid_disks &&
6468 rdev->data_offset < rdev->new_data_offset)
6469 return -EINVAL;
6470 if (mddev->raid_disks > raid_disks &&
6471 rdev->data_offset > rdev->new_data_offset)
6472 return -EINVAL;
6473 }
6474
NeilBrown63c70c42006-03-27 01:18:13 -08006475 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006476 if (mddev->delta_disks < 0)
6477 mddev->reshape_backwards = 1;
6478 else if (mddev->delta_disks > 0)
6479 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006480
6481 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006482 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006483 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006484 mddev->reshape_backwards = 0;
6485 }
NeilBrownda943b992006-01-06 00:20:54 -08006486 return rv;
6487}
6488
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489/*
6490 * update_array_info is used to change the configuration of an
6491 * on-line array.
6492 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6493 * fields in the info are checked against the array.
6494 * Any differences that cannot be handled will cause an error.
6495 * Normally, only one change can be managed at a time.
6496 */
NeilBrownfd01b882011-10-11 16:47:53 +11006497static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498{
6499 int rv = 0;
6500 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006501 int state = 0;
6502
6503 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006504 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006505 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506
6507 if (mddev->major_version != info->major_version ||
6508 mddev->minor_version != info->minor_version ||
6509/* mddev->patch_version != info->patch_version || */
6510 mddev->ctime != info->ctime ||
6511 mddev->level != info->level ||
6512/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006513 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006514 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006515 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6516 ((state^info->state) & 0xfffffe00)
6517 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 return -EINVAL;
6519 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006520 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6521 cnt++;
6522 if (mddev->raid_disks != info->raid_disks)
6523 cnt++;
6524 if (mddev->layout != info->layout)
6525 cnt++;
6526 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6527 cnt++;
6528 if (cnt == 0)
6529 return 0;
6530 if (cnt > 1)
6531 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532
6533 if (mddev->layout != info->layout) {
6534 /* Change layout
6535 * we don't need to do anything at the md level, the
6536 * personality will take care of it all.
6537 */
NeilBrown50ac1682009-06-18 08:47:55 +10006538 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006540 else {
6541 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006542 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006543 if (rv)
6544 mddev->new_layout = mddev->layout;
6545 return rv;
6546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006548 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006549 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550
NeilBrownda943b992006-01-06 00:20:54 -08006551 if (mddev->raid_disks != info->raid_disks)
6552 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553
NeilBrown36fa3062005-09-09 16:23:45 -07006554 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006555 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6556 rv = -EINVAL;
6557 goto err;
6558 }
6559 if (mddev->recovery || mddev->sync_thread) {
6560 rv = -EBUSY;
6561 goto err;
6562 }
NeilBrown36fa3062005-09-09 16:23:45 -07006563 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006564 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006565 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006566 if (mddev->bitmap) {
6567 rv = -EEXIST;
6568 goto err;
6569 }
6570 if (mddev->bitmap_info.default_offset == 0) {
6571 rv = -EINVAL;
6572 goto err;
6573 }
NeilBrownc3d97142009-12-14 12:49:52 +11006574 mddev->bitmap_info.offset =
6575 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006576 mddev->bitmap_info.space =
6577 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006578 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006579 bitmap = bitmap_create(mddev, -1);
6580 if (!IS_ERR(bitmap)) {
6581 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006582 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006583 } else
6584 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006585 if (rv)
6586 bitmap_destroy(mddev);
6587 mddev->pers->quiesce(mddev, 0);
6588 } else {
6589 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006590 if (!mddev->bitmap) {
6591 rv = -ENOENT;
6592 goto err;
6593 }
6594 if (mddev->bitmap->storage.file) {
6595 rv = -EINVAL;
6596 goto err;
6597 }
NeilBrown36fa3062005-09-09 16:23:45 -07006598 mddev->pers->quiesce(mddev, 1);
6599 bitmap_destroy(mddev);
6600 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006601 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006602 }
6603 }
NeilBrown850b2b42006-10-03 01:15:46 -07006604 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006605 return rv;
6606err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607 return rv;
6608}
6609
NeilBrownfd01b882011-10-11 16:47:53 +11006610static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611{
NeilBrown3cb03002011-10-11 16:45:26 +11006612 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006613 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614
6615 if (mddev->pers == NULL)
6616 return -ENODEV;
6617
NeilBrown1ca69c42012-10-11 13:37:33 +11006618 rcu_read_lock();
6619 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006621 err = -ENODEV;
6622 else {
6623 md_error(mddev, rdev);
6624 if (!test_bit(Faulty, &rdev->flags))
6625 err = -EBUSY;
6626 }
6627 rcu_read_unlock();
6628 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629}
6630
Andre Noll2f9618c2008-04-25 18:57:58 +02006631/*
6632 * We have a problem here : there is no easy way to give a CHS
6633 * virtual geometry. We currently pretend that we have a 2 heads
6634 * 4 sectors (with a BIG number of cylinders...). This drives
6635 * dosfs just mad... ;-)
6636 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006637static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6638{
NeilBrownfd01b882011-10-11 16:47:53 +11006639 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006640
6641 geo->heads = 2;
6642 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006643 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006644 return 0;
6645}
6646
Nicolas Schichancb335f82014-01-15 16:58:52 +01006647static inline bool md_ioctl_valid(unsigned int cmd)
6648{
6649 switch (cmd) {
6650 case ADD_NEW_DISK:
6651 case BLKROSET:
6652 case GET_ARRAY_INFO:
6653 case GET_BITMAP_FILE:
6654 case GET_DISK_INFO:
6655 case HOT_ADD_DISK:
6656 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006657 case RAID_AUTORUN:
6658 case RAID_VERSION:
6659 case RESTART_ARRAY_RW:
6660 case RUN_ARRAY:
6661 case SET_ARRAY_INFO:
6662 case SET_BITMAP_FILE:
6663 case SET_DISK_FAULTY:
6664 case STOP_ARRAY:
6665 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006666 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006667 return true;
6668 default:
6669 return false;
6670 }
6671}
6672
Al Viroa39907f2008-03-02 10:31:15 -05006673static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 unsigned int cmd, unsigned long arg)
6675{
6676 int err = 0;
6677 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006678 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006679 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680
Nicolas Schichancb335f82014-01-15 16:58:52 +01006681 if (!md_ioctl_valid(cmd))
6682 return -ENOTTY;
6683
NeilBrown506c9e42011-12-23 10:17:26 +11006684 switch (cmd) {
6685 case RAID_VERSION:
6686 case GET_ARRAY_INFO:
6687 case GET_DISK_INFO:
6688 break;
6689 default:
6690 if (!capable(CAP_SYS_ADMIN))
6691 return -EACCES;
6692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693
6694 /*
6695 * Commands dealing with the RAID driver but not any
6696 * particular array:
6697 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006698 switch (cmd) {
6699 case RAID_VERSION:
6700 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006701 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006704 case RAID_AUTORUN:
6705 err = 0;
6706 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006707 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006709 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 }
6711
6712 /*
6713 * Commands creating/starting a new array:
6714 */
6715
Al Viroa39907f2008-03-02 10:31:15 -05006716 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717
6718 if (!mddev) {
6719 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006720 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 }
6722
NeilBrown1ca69c42012-10-11 13:37:33 +11006723 /* Some actions do not requires the mutex */
6724 switch (cmd) {
6725 case GET_ARRAY_INFO:
6726 if (!mddev->raid_disks && !mddev->external)
6727 err = -ENODEV;
6728 else
6729 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006730 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006731
6732 case GET_DISK_INFO:
6733 if (!mddev->raid_disks && !mddev->external)
6734 err = -ENODEV;
6735 else
6736 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006737 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006738
6739 case SET_DISK_FAULTY:
6740 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006741 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006742
6743 case GET_BITMAP_FILE:
6744 err = get_bitmap_file(mddev, argp);
6745 goto out;
6746
NeilBrown1ca69c42012-10-11 13:37:33 +11006747 }
6748
NeilBrowna7a3f082012-12-11 13:35:54 +11006749 if (cmd == ADD_NEW_DISK)
6750 /* need to ensure md_delayed_delete() has completed */
6751 flush_workqueue(md_misc_wq);
6752
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006753 if (cmd == HOT_REMOVE_DISK)
6754 /* need to ensure recovery thread has run */
6755 wait_event_interruptible_timeout(mddev->sb_wait,
6756 !test_bit(MD_RECOVERY_NEEDED,
6757 &mddev->flags),
6758 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006759 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6760 /* Need to flush page cache, and ensure no-one else opens
6761 * and writes
6762 */
6763 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006764 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006765 mutex_unlock(&mddev->open_mutex);
6766 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006767 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006768 }
6769 set_bit(MD_STILL_CLOSED, &mddev->flags);
6770 mutex_unlock(&mddev->open_mutex);
6771 sync_blockdev(bdev);
6772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 err = mddev_lock(mddev);
6774 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006775 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6777 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006778 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 }
6780
NeilBrownc02c0ae2012-12-11 13:39:21 +11006781 if (cmd == SET_ARRAY_INFO) {
6782 mdu_array_info_t info;
6783 if (!arg)
6784 memset(&info, 0, sizeof(info));
6785 else if (copy_from_user(&info, argp, sizeof(info))) {
6786 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006787 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006788 }
6789 if (mddev->pers) {
6790 err = update_array_info(mddev, &info);
6791 if (err) {
6792 printk(KERN_WARNING "md: couldn't update"
6793 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006794 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006796 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006797 }
6798 if (!list_empty(&mddev->disks)) {
6799 printk(KERN_WARNING
6800 "md: array %s already has disks!\n",
6801 mdname(mddev));
6802 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006803 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006804 }
6805 if (mddev->raid_disks) {
6806 printk(KERN_WARNING
6807 "md: array %s already initialised!\n",
6808 mdname(mddev));
6809 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006810 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006811 }
6812 err = set_array_info(mddev, &info);
6813 if (err) {
6814 printk(KERN_WARNING "md: couldn't set"
6815 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006816 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006817 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006818 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 }
6820
6821 /*
6822 * Commands querying/configuring an existing array:
6823 */
NeilBrown32a76272005-06-21 17:17:14 -07006824 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006825 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006826 if ((!mddev->raid_disks && !mddev->external)
6827 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6828 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6829 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006831 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 }
6833
6834 /*
6835 * Commands even a read-only array can execute:
6836 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006837 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006838 case RESTART_ARRAY_RW:
6839 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006840 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006841
6842 case STOP_ARRAY:
6843 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006844 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006845
6846 case STOP_ARRAY_RO:
6847 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006848 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006849
NeilBrown3ea8929d2013-04-24 11:42:41 +10006850 case HOT_REMOVE_DISK:
6851 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006852 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006853
NeilBrown7ceb17e2013-04-24 11:42:42 +10006854 case ADD_NEW_DISK:
6855 /* We can support ADD_NEW_DISK on read-only arrays
6856 * on if we are re-adding a preexisting device.
6857 * So require mddev->pers and MD_DISK_SYNC.
6858 */
6859 if (mddev->pers) {
6860 mdu_disk_info_t info;
6861 if (copy_from_user(&info, argp, sizeof(info)))
6862 err = -EFAULT;
6863 else if (!(info.state & (1<<MD_DISK_SYNC)))
6864 /* Need to clear read-only for this */
6865 break;
6866 else
6867 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006868 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006869 }
6870 break;
6871
NeilBrownc02c0ae2012-12-11 13:39:21 +11006872 case BLKROSET:
6873 if (get_user(ro, (int __user *)(arg))) {
6874 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006875 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006876 }
6877 err = -EINVAL;
6878
6879 /* if the bdev is going readonly the value of mddev->ro
6880 * does not matter, no writes are coming
6881 */
6882 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006883 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006884
NeilBrownc02c0ae2012-12-11 13:39:21 +11006885 /* are we are already prepared for writes? */
6886 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006887 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006888
6889 /* transitioning to readauto need only happen for
6890 * arrays that call md_write_start
6891 */
6892 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006894 if (err == 0) {
6895 mddev->ro = 2;
6896 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006897 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006898 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006899 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900 }
6901
6902 /*
6903 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006904 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 */
NeilBrown326eb172014-09-30 15:36:28 +10006906 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006907 if (mddev->ro == 2) {
6908 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006909 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006910 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006911 /* mddev_unlock will wake thread */
6912 /* If a device failed while we were read-only, we
6913 * need to make sure the metadata is updated now.
6914 */
6915 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6916 mddev_unlock(mddev);
6917 wait_event(mddev->sb_wait,
6918 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6919 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006920 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006921 }
NeilBrownf91de922005-11-08 21:39:36 -08006922 } else {
6923 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006924 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 }
6927
NeilBrownc02c0ae2012-12-11 13:39:21 +11006928 switch (cmd) {
6929 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006931 mdu_disk_info_t info;
6932 if (copy_from_user(&info, argp, sizeof(info)))
6933 err = -EFAULT;
6934 else
6935 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006936 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006939 case CLUSTERED_DISK_NACK:
6940 if (mddev_is_clustered(mddev))
6941 md_cluster_ops->new_disk_ack(mddev, false);
6942 else
6943 err = -EINVAL;
6944 goto unlock;
6945
NeilBrownc02c0ae2012-12-11 13:39:21 +11006946 case HOT_ADD_DISK:
6947 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006948 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949
NeilBrownc02c0ae2012-12-11 13:39:21 +11006950 case RUN_ARRAY:
6951 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006952 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953
NeilBrownc02c0ae2012-12-11 13:39:21 +11006954 case SET_BITMAP_FILE:
6955 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006956 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006957
NeilBrownc02c0ae2012-12-11 13:39:21 +11006958 default:
6959 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006960 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961 }
6962
NeilBrown3adc28d2014-09-30 15:46:41 +10006963unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006964 if (mddev->hold_active == UNTIL_IOCTL &&
6965 err != -EINVAL)
6966 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006968out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969 return err;
6970}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006971#ifdef CONFIG_COMPAT
6972static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6973 unsigned int cmd, unsigned long arg)
6974{
6975 switch (cmd) {
6976 case HOT_REMOVE_DISK:
6977 case HOT_ADD_DISK:
6978 case SET_DISK_FAULTY:
6979 case SET_BITMAP_FILE:
6980 /* These take in integer arg, do not convert */
6981 break;
6982 default:
6983 arg = (unsigned long)compat_ptr(arg);
6984 break;
6985 }
6986
6987 return md_ioctl(bdev, mode, cmd, arg);
6988}
6989#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990
Al Viroa39907f2008-03-02 10:31:15 -05006991static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992{
6993 /*
6994 * Succeed if we can lock the mddev, which confirms that
6995 * it isn't being stopped right now.
6996 */
NeilBrownfd01b882011-10-11 16:47:53 +11006997 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 int err;
6999
Yuanhan Liu0c098222012-05-22 13:55:32 +10007000 if (!mddev)
7001 return -ENODEV;
7002
NeilBrownd3374822009-01-09 08:31:10 +11007003 if (mddev->gendisk != bdev->bd_disk) {
7004 /* we are racing with mddev_put which is discarding this
7005 * bd_disk.
7006 */
7007 mddev_put(mddev);
7008 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007009 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007010 /* Then retry the open from the top */
7011 return -ERESTARTSYS;
7012 }
7013 BUG_ON(mddev != bdev->bd_disk->private_data);
7014
NeilBrownc8c00a62009-08-10 12:50:52 +10007015 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 goto out;
7017
7018 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007019 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10007020 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10007021 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007023 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024 out:
7025 return err;
7026}
7027
Al Virodb2a1442013-05-05 21:52:57 -04007028static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007030 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007032 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007033 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007036
7037static int md_media_changed(struct gendisk *disk)
7038{
NeilBrownfd01b882011-10-11 16:47:53 +11007039 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007040
7041 return mddev->changed;
7042}
7043
7044static int md_revalidate(struct gendisk *disk)
7045{
NeilBrownfd01b882011-10-11 16:47:53 +11007046 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007047
7048 mddev->changed = 0;
7049 return 0;
7050}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007051static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052{
7053 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007054 .open = md_open,
7055 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007056 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007057#ifdef CONFIG_COMPAT
7058 .compat_ioctl = md_compat_ioctl,
7059#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007060 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007061 .media_changed = md_media_changed,
7062 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063};
7064
NeilBrownf72ffdd2014-09-30 14:23:59 +10007065static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066{
NeilBrown2b8bf342011-10-11 16:48:23 +11007067 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 /*
7070 * md_thread is a 'system-thread', it's priority should be very
7071 * high. We avoid resource deadlocks individually in each
7072 * raid personality. (RAID5 does preallocation) We also use RR and
7073 * the very same RT priority as kswapd, thus we will never get
7074 * into a priority inversion deadlock.
7075 *
7076 * we definitely have to have equal or higher priority than
7077 * bdflush, otherwise bdflush will deadlock if there are too
7078 * many dirty RAID5 blocks.
7079 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080
NeilBrown6985c432005-10-19 21:23:47 -07007081 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007082 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083
NeilBrown93588e22005-11-15 00:09:12 -08007084 /* We need to wait INTERRUPTIBLE so that
7085 * we don't add to the load-average.
7086 * That means we need to be sure no signals are
7087 * pending
7088 */
7089 if (signal_pending(current))
7090 flush_signals(current);
7091
7092 wait_event_interruptible_timeout
7093 (thread->wqueue,
7094 test_bit(THREAD_WAKEUP, &thread->flags)
7095 || kthread_should_stop(),
7096 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097
NeilBrown6c987912011-01-14 09:13:53 +11007098 clear_bit(THREAD_WAKEUP, &thread->flags);
7099 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007100 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007102
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 return 0;
7104}
7105
NeilBrown2b8bf342011-10-11 16:48:23 +11007106void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107{
7108 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007109 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110 set_bit(THREAD_WAKEUP, &thread->flags);
7111 wake_up(&thread->wqueue);
7112 }
7113}
NeilBrown6c144d32014-09-30 16:15:38 +10007114EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115
Shaohua Li4ed87312012-10-11 13:34:00 +11007116struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7117 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118{
NeilBrown2b8bf342011-10-11 16:48:23 +11007119 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120
NeilBrown2b8bf342011-10-11 16:48:23 +11007121 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122 if (!thread)
7123 return NULL;
7124
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125 init_waitqueue_head(&thread->wqueue);
7126
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 thread->run = run;
7128 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007129 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007130 thread->tsk = kthread_run(md_thread, thread,
7131 "%s_%s",
7132 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007133 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007134 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 kfree(thread);
7136 return NULL;
7137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 return thread;
7139}
NeilBrown6c144d32014-09-30 16:15:38 +10007140EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141
NeilBrown2b8bf342011-10-11 16:48:23 +11007142void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143{
NeilBrown2b8bf342011-10-11 16:48:23 +11007144 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007145 if (!thread)
7146 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007147 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007148 /* Locking ensures that mddev_unlock does not wake_up a
7149 * non-existent thread
7150 */
7151 spin_lock(&pers_lock);
7152 *threadp = NULL;
7153 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007154
7155 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 kfree(thread);
7157}
NeilBrown6c144d32014-09-30 16:15:38 +10007158EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159
NeilBrownfd01b882011-10-11 16:47:53 +11007160void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161{
NeilBrownb2d444d2005-11-08 21:39:31 -08007162 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007164
NeilBrownde393cd2011-07-28 11:31:48 +10007165 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166 return;
7167 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007168 if (mddev->degraded)
7169 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007170 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7172 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7173 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007174 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007175 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07007176 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177}
NeilBrown6c144d32014-09-30 16:15:38 +10007178EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179
7180/* seq_file implementation /proc/mdstat */
7181
7182static void status_unused(struct seq_file *seq)
7183{
7184 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007185 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186
7187 seq_printf(seq, "unused devices: ");
7188
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007189 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190 char b[BDEVNAME_SIZE];
7191 i++;
7192 seq_printf(seq, "%s ",
7193 bdevname(rdev->bdev,b));
7194 }
7195 if (!i)
7196 seq_printf(seq, "<none>");
7197
7198 seq_printf(seq, "\n");
7199}
7200
NeilBrownf7851be2015-07-02 17:12:58 +10007201static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202{
NeilBrowndd71cf62009-05-07 12:49:35 +10007203 sector_t max_sectors, resync, res;
7204 unsigned long dt, db;
7205 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007206 int scale;
7207 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208
NeilBrownc804cde2012-05-21 09:28:33 +10007209 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7210 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007211 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007213 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214
NeilBrownf7851be2015-07-02 17:12:58 +10007215 resync = mddev->curr_resync;
7216 if (resync <= 3) {
7217 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7218 /* Still cleaning up */
7219 resync = max_sectors;
7220 } else
7221 resync -= atomic_read(&mddev->recovery_active);
7222
7223 if (resync == 0) {
7224 if (mddev->recovery_cp < MaxSector) {
7225 seq_printf(seq, "\tresync=PENDING");
7226 return 1;
7227 }
7228 return 0;
7229 }
7230 if (resync < 3) {
7231 seq_printf(seq, "\tresync=DELAYED");
7232 return 1;
7233 }
7234
NeilBrown403df472014-09-30 15:52:29 +10007235 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007236 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007237 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007238 * u32, as those are the requirements for sector_div.
7239 * Thus 'scale' must be at least 10
7240 */
7241 scale = 10;
7242 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007243 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007244 scale++;
7245 }
7246 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007247 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007248
7249 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250 {
NeilBrown4588b422006-03-27 01:18:04 -08007251 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 seq_printf(seq, "[");
7253 for (i = 0; i < x; i++)
7254 seq_printf(seq, "=");
7255 seq_printf(seq, ">");
7256 for (i = 0; i < y; i++)
7257 seq_printf(seq, ".");
7258 seq_printf(seq, "] ");
7259 }
NeilBrown4588b422006-03-27 01:18:04 -08007260 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007261 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7262 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007263 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7264 "check" :
7265 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7266 "resync" : "recovery"))),
7267 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007268 (unsigned long long) resync/2,
7269 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270
7271 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 * dt: time from mark until now
7273 * db: blocks written from mark until now
7274 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007275 *
7276 * rt is a sector_t, so could be 32bit or 64bit.
7277 * So we divide before multiply in case it is 32bit and close
7278 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007279 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007280 * near the end of resync when the number of remaining sectors
7281 * is close to 'db'.
7282 * We then divide rt by 32 after multiplying by db to compensate.
7283 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284 */
7285 dt = ((jiffies - mddev->resync_mark) / HZ);
7286 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007287 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7288 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289
NeilBrowndd71cf62009-05-07 12:49:35 +10007290 rt = max_sectors - resync; /* number of remaining sectors */
7291 sector_div(rt, db/32+1);
7292 rt *= dt;
7293 rt >>= 5;
7294
7295 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7296 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297
NeilBrownff4e8d92006-07-10 04:44:16 -07007298 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007299 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300}
7301
7302static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7303{
7304 struct list_head *tmp;
7305 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007306 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307
7308 if (l >= 0x10000)
7309 return NULL;
7310 if (!l--)
7311 /* header */
7312 return (void*)1;
7313
7314 spin_lock(&all_mddevs_lock);
7315 list_for_each(tmp,&all_mddevs)
7316 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007317 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 mddev_get(mddev);
7319 spin_unlock(&all_mddevs_lock);
7320 return mddev;
7321 }
7322 spin_unlock(&all_mddevs_lock);
7323 if (!l--)
7324 return (void*)2;/* tail */
7325 return NULL;
7326}
7327
7328static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7329{
7330 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007331 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007332
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 ++*pos;
7334 if (v == (void*)2)
7335 return NULL;
7336
7337 spin_lock(&all_mddevs_lock);
7338 if (v == (void*)1)
7339 tmp = all_mddevs.next;
7340 else
7341 tmp = mddev->all_mddevs.next;
7342 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007343 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 else {
7345 next_mddev = (void*)2;
7346 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 spin_unlock(&all_mddevs_lock);
7349
7350 if (v != (void*)1)
7351 mddev_put(mddev);
7352 return next_mddev;
7353
7354}
7355
7356static void md_seq_stop(struct seq_file *seq, void *v)
7357{
NeilBrownfd01b882011-10-11 16:47:53 +11007358 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359
7360 if (mddev && v != (void*)1 && v != (void*)2)
7361 mddev_put(mddev);
7362}
7363
7364static int md_seq_show(struct seq_file *seq, void *v)
7365{
NeilBrownfd01b882011-10-11 16:47:53 +11007366 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007367 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007368 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369
7370 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007371 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372 seq_printf(seq, "Personalities : ");
7373 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007374 list_for_each_entry(pers, &pers_list, list)
7375 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376
7377 spin_unlock(&pers_lock);
7378 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007379 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 return 0;
7381 }
7382 if (v == (void*)2) {
7383 status_unused(seq);
7384 return 0;
7385 }
7386
NeilBrown36d091f2014-12-15 12:56:58 +11007387 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7389 seq_printf(seq, "%s : %sactive", mdname(mddev),
7390 mddev->pers ? "" : "in");
7391 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007392 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007394 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007395 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 seq_printf(seq, " %s", mddev->pers->name);
7397 }
7398
Andre Nolldd8ac332009-03-31 14:33:13 +11007399 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007400 rcu_read_lock();
7401 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402 char b[BDEVNAME_SIZE];
7403 seq_printf(seq, " %s[%d]",
7404 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007405 if (test_bit(WriteMostly, &rdev->flags))
7406 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007407 if (test_bit(Journal, &rdev->flags))
7408 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007409 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 seq_printf(seq, "(F)");
7411 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007412 }
7413 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007414 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007415 if (test_bit(Replacement, &rdev->flags))
7416 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007417 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007419 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420
7421 if (!list_empty(&mddev->disks)) {
7422 if (mddev->pers)
7423 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007424 (unsigned long long)
7425 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 else
7427 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007428 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007430 if (mddev->persistent) {
7431 if (mddev->major_version != 0 ||
7432 mddev->minor_version != 90) {
7433 seq_printf(seq," super %d.%d",
7434 mddev->major_version,
7435 mddev->minor_version);
7436 }
NeilBrowne6910632008-02-06 01:39:51 -08007437 } else if (mddev->external)
7438 seq_printf(seq, " super external:%s",
7439 mddev->metadata_type);
7440 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007441 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007442
7443 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007444 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007445 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007446 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007447 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007448 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007449 }
NeilBrown32a76272005-06-21 17:17:14 -07007450 } else
7451 seq_printf(seq, "\n ");
7452
NeilBrown57148962012-03-19 12:46:40 +11007453 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454
7455 seq_printf(seq, "\n");
7456 }
NeilBrown36d091f2014-12-15 12:56:58 +11007457 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007458
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 return 0;
7460}
7461
Jan Engelhardt110518b2009-05-07 12:49:37 +10007462static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463 .start = md_seq_start,
7464 .next = md_seq_next,
7465 .stop = md_seq_stop,
7466 .show = md_seq_show,
7467};
7468
7469static int md_seq_open(struct inode *inode, struct file *file)
7470{
Kay Sieversf1514632011-07-12 20:48:39 +02007471 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 int error;
7473
7474 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007475 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007476 return error;
7477
7478 seq = file->private_data;
7479 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480 return error;
7481}
7482
NeilBrowne2f23b62014-04-09 14:33:51 +10007483static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007484static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7485{
Kay Sieversf1514632011-07-12 20:48:39 +02007486 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007487 int mask;
7488
NeilBrowne2f23b62014-04-09 14:33:51 +10007489 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007490 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007491 poll_wait(filp, &md_event_waiters, wait);
7492
7493 /* always allow read */
7494 mask = POLLIN | POLLRDNORM;
7495
Kay Sieversf1514632011-07-12 20:48:39 +02007496 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007497 mask |= POLLERR | POLLPRI;
7498 return mask;
7499}
7500
Arjan van de Venfa027c22007-02-12 00:55:33 -08007501static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007502 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 .open = md_seq_open,
7504 .read = seq_read,
7505 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007506 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007507 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508};
7509
NeilBrown84fc4b52011-10-11 16:49:58 +11007510int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511{
Chao Yu50bd3772014-09-25 15:28:34 +08007512 printk(KERN_INFO "md: %s personality registered for level %d\n",
7513 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007515 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516 spin_unlock(&pers_lock);
7517 return 0;
7518}
NeilBrown6c144d32014-09-30 16:15:38 +10007519EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520
NeilBrown84fc4b52011-10-11 16:49:58 +11007521int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522{
NeilBrown2604b702006-01-06 00:20:36 -08007523 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007525 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526 spin_unlock(&pers_lock);
7527 return 0;
7528}
NeilBrown6c144d32014-09-30 16:15:38 +10007529EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530
NeilBrown6022e752015-08-13 12:32:55 +10007531int register_md_cluster_operations(struct md_cluster_operations *ops,
7532 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007533{
NeilBrown6022e752015-08-13 12:32:55 +10007534 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007535 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007536 if (md_cluster_ops != NULL)
7537 ret = -EALREADY;
7538 else {
7539 md_cluster_ops = ops;
7540 md_cluster_mod = module;
7541 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007542 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007543 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007544}
7545EXPORT_SYMBOL(register_md_cluster_operations);
7546
7547int unregister_md_cluster_operations(void)
7548{
7549 spin_lock(&pers_lock);
7550 md_cluster_ops = NULL;
7551 spin_unlock(&pers_lock);
7552 return 0;
7553}
7554EXPORT_SYMBOL(unregister_md_cluster_operations);
7555
7556int md_setup_cluster(struct mddev *mddev, int nodes)
7557{
7558 int err;
7559
7560 err = request_module("md-cluster");
7561 if (err) {
7562 pr_err("md-cluster module not found.\n");
Goldwyn Rodriguesb0c26a72015-07-22 12:09:15 -05007563 return -ENOENT;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007564 }
7565
7566 spin_lock(&pers_lock);
7567 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7568 spin_unlock(&pers_lock);
7569 return -ENOENT;
7570 }
7571 spin_unlock(&pers_lock);
7572
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007573 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007574}
7575
7576void md_cluster_stop(struct mddev *mddev)
7577{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007578 if (!md_cluster_ops)
7579 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007580 md_cluster_ops->leave(mddev);
7581 module_put(md_cluster_mod);
7582}
7583
NeilBrownfd01b882011-10-11 16:47:53 +11007584static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007586 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007588 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589
7590 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007591 rcu_read_lock();
7592 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007594 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7595 (int)part_stat_read(&disk->part0, sectors[1]) -
7596 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007597 /* sync IO will cause sync_io to increase before the disk_stats
7598 * as sync_io is counted when a request starts, and
7599 * disk_stats is counted when it completes.
7600 * So resync activity will cause curr_events to be smaller than
7601 * when there was no such activity.
7602 * non-sync IO will cause disk_stat to increase without
7603 * increasing sync_io so curr_events will (eventually)
7604 * be larger than it was before. Once it becomes
7605 * substantially larger, the test below will cause
7606 * the array to appear non-idle, and resync will slow
7607 * down.
7608 * If there is a lot of outstanding resync activity when
7609 * we set last_event to curr_events, then all that activity
7610 * completing might cause the array to appear non-idle
7611 * and resync will be slowed down even though there might
7612 * not have been non-resync activity. This will only
7613 * happen once though. 'last_events' will soon reflect
7614 * the state where there is little or no outstanding
7615 * resync requests, and further resync activity will
7616 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007617 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007619 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620 rdev->last_events = curr_events;
7621 idle = 0;
7622 }
7623 }
NeilBrown4b809912008-07-21 17:05:25 +10007624 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625 return idle;
7626}
7627
NeilBrownfd01b882011-10-11 16:47:53 +11007628void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629{
7630 /* another "blocks" (512byte) blocks have been synced */
7631 atomic_sub(blocks, &mddev->recovery_active);
7632 wake_up(&mddev->recovery_wait);
7633 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007634 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007635 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636 md_wakeup_thread(mddev->thread);
7637 // stop recovery, signal do_sync ....
7638 }
7639}
NeilBrown6c144d32014-09-30 16:15:38 +10007640EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641
NeilBrown06d91a52005-06-21 17:17:12 -07007642/* md_write_start(mddev, bi)
7643 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007644 * in superblock) before writing, schedule a superblock update
7645 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007646 */
NeilBrownfd01b882011-10-11 16:47:53 +11007647void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648{
Neil Brown0fd62b82008-06-28 08:31:36 +10007649 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007650 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007651 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007652
NeilBrownf91de922005-11-08 21:39:36 -08007653 BUG_ON(mddev->ro == 1);
7654 if (mddev->ro == 2) {
7655 /* need to switch to read/write */
7656 mddev->ro = 0;
7657 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7658 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007659 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007660 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007661 }
NeilBrown06d91a52005-06-21 17:17:12 -07007662 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007663 if (mddev->safemode == 1)
7664 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007665 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007666 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007667 if (mddev->in_sync) {
7668 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007669 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007670 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007671 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007672 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007673 }
NeilBrown85572d72014-12-15 12:56:56 +11007674 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007675 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007676 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007677 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007678 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007679 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680}
NeilBrown6c144d32014-09-30 16:15:38 +10007681EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682
NeilBrownfd01b882011-10-11 16:47:53 +11007683void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684{
7685 if (atomic_dec_and_test(&mddev->writes_pending)) {
7686 if (mddev->safemode == 2)
7687 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007688 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7690 }
7691}
NeilBrown6c144d32014-09-30 16:15:38 +10007692EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693
NeilBrown2a2275d2007-01-26 00:57:11 -08007694/* md_allow_write(mddev)
7695 * Calling this ensures that the array is marked 'active' so that writes
7696 * may proceed without blocking. It is important to call this before
7697 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7698 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007699 *
7700 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7701 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007702 */
NeilBrownfd01b882011-10-11 16:47:53 +11007703int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007704{
7705 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007706 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007707 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007708 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007709 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007710 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007711
NeilBrown85572d72014-12-15 12:56:56 +11007712 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007713 if (mddev->in_sync) {
7714 mddev->in_sync = 0;
7715 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007716 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007717 if (mddev->safemode_delay &&
7718 mddev->safemode == 0)
7719 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007720 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007721 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007722 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007723 } else
NeilBrown85572d72014-12-15 12:56:56 +11007724 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007725
NeilBrown070dc6d2010-08-30 17:33:34 +10007726 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007727 return -EAGAIN;
7728 else
7729 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007730}
7731EXPORT_SYMBOL_GPL(md_allow_write);
7732
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733#define SYNC_MARKS 10
7734#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007735#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007736void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007737{
Shaohua Li4ed87312012-10-11 13:34:00 +11007738 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007739 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740 unsigned int currspeed = 0,
7741 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007742 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007744 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745 sector_t mark_cnt[SYNC_MARKS];
7746 int last_mark,m;
7747 struct list_head *tmp;
7748 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007749 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007750 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007751 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007752 struct blk_plug plug;
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05007753 bool cluster_resync_finished = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007754
7755 /* just incase thread restarts... */
7756 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7757 return;
NeilBrown3991b312014-05-28 13:39:23 +10007758 if (mddev->ro) {/* never try to sync a read-only array */
7759 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007760 return;
NeilBrown3991b312014-05-28 13:39:23 +10007761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007762
NeilBrown61df9d92006-10-03 01:15:57 -07007763 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007764 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007765 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007766 action = "check";
7767 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007768 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007769 action = "repair";
7770 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007771 desc = "resync";
7772 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7773 desc = "reshape";
7774 else
7775 desc = "recovery";
7776
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007777 mddev->last_sync_action = action ?: desc;
7778
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779 /* we overload curr_resync somewhat here.
7780 * 0 == not engaged in resync at all
7781 * 2 == checking that there is no conflict with another sync
7782 * 1 == like 2, but have yielded to allow conflicting resync to
7783 * commense
7784 * other == active in resync - this many blocks
7785 *
7786 * Before starting a resync we must have set curr_resync to
7787 * 2, and then checked that every "conflicting" array has curr_resync
7788 * less than ours. When we find one that is the same or higher
7789 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7790 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7791 * This will mean we have to start checking from the beginning again.
7792 *
7793 */
7794
7795 do {
7796 mddev->curr_resync = 2;
7797
7798 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007799 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007801 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007802 if (mddev2 == mddev)
7803 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007804 if (!mddev->parallel_resync
7805 && mddev2->curr_resync
7806 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807 DEFINE_WAIT(wq);
7808 if (mddev < mddev2 && mddev->curr_resync == 2) {
7809 /* arbitrarily yield */
7810 mddev->curr_resync = 1;
7811 wake_up(&resync_wait);
7812 }
7813 if (mddev > mddev2 && mddev->curr_resync == 1)
7814 /* no need to wait here, we can wait the next
7815 * time 'round when curr_resync == 2
7816 */
7817 continue;
NeilBrown97441972008-09-19 11:49:54 +10007818 /* We need to wait 'interruptible' so as not to
7819 * contribute to the load average, and not to
7820 * be caught by 'softlockup'
7821 */
7822 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007823 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007824 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007825 printk(KERN_INFO "md: delaying %s of %s"
7826 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007828 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007830 if (signal_pending(current))
7831 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832 schedule();
7833 finish_wait(&resync_wait, &wq);
7834 goto try_again;
7835 }
7836 finish_wait(&resync_wait, &wq);
7837 }
7838 }
7839 } while (mddev->curr_resync < 2);
7840
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007841 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007842 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007844 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845 */
7846 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007847 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007848 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007849 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7850 j = mddev->resync_min;
7851 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007852 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007853
NeilBrownccfcc3c2006-03-27 01:18:09 -08007854 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007855 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007856 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007858 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007859 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007860 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007861 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007862 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07007863 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007864 !test_bit(Faulty, &rdev->flags) &&
7865 !test_bit(In_sync, &rdev->flags) &&
7866 rdev->recovery_offset < j)
7867 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007868 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007869
7870 /* If there is a bitmap, we need to make sure all
7871 * writes that started before we added a spare
7872 * complete before we start doing a recovery.
7873 * Otherwise the write might complete and (via
7874 * bitmap_endwrite) set a bit in the bitmap after the
7875 * recovery has checked that bit and skipped that
7876 * region.
7877 */
7878 if (mddev->bitmap) {
7879 mddev->pers->quiesce(mddev, 1);
7880 mddev->pers->quiesce(mddev, 0);
7881 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883
NeilBrown61df9d92006-10-03 01:15:57 -07007884 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7885 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7886 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007887 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007888 "(but not more than %d KB/sec) for %s.\n",
7889 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890
NeilBrowneea1bf32009-03-31 14:27:02 +11007891 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007892
NeilBrown57afd892005-06-21 17:17:13 -07007893 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894 for (m = 0; m < SYNC_MARKS; m++) {
7895 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007896 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897 }
7898 last_mark = 0;
7899 mddev->resync_mark = mark[last_mark];
7900 mddev->resync_mark_cnt = mark_cnt[last_mark];
7901
7902 /*
7903 * Tune reconstruction:
7904 */
7905 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007906 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7907 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007908
7909 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910 last_check = 0;
7911
7912 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007913 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007914 "md: resuming %s of %s from checkpoint.\n",
7915 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007917 } else
7918 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007919 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007920 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7921 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007922 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923
majianpeng7c2c57c2012-07-03 12:12:26 +10007924 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007926 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927
NeilBrown57afd892005-06-21 17:17:13 -07007928 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007929
NeilBrown7a91ee12009-05-26 12:57:21 +10007930 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7931 ((mddev->curr_resync > mddev->curr_resync_completed &&
7932 (mddev->curr_resync - mddev->curr_resync_completed)
7933 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007934 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007935 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10007936 >= mddev->resync_max - mddev->curr_resync_completed ||
7937 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10007938 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007939 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007940 wait_event(mddev->recovery_wait,
7941 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007942 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007943 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7944 j > mddev->recovery_cp)
7945 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007946 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007947 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007948 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007949 }
NeilBrownacb180b2009-04-14 16:28:34 +10007950
NeilBrownc91abf52013-11-19 12:02:01 +11007951 while (j >= mddev->resync_max &&
7952 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007953 /* As this condition is controlled by user-space,
7954 * we can block indefinitely, so use '_interruptible'
7955 * to avoid triggering warnings.
7956 */
7957 flush_signals(current); /* just in case */
7958 wait_event_interruptible(mddev->recovery_wait,
7959 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007960 || test_bit(MD_RECOVERY_INTR,
7961 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007962 }
NeilBrownacb180b2009-04-14 16:28:34 +10007963
NeilBrownc91abf52013-11-19 12:02:01 +11007964 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7965 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007966
NeilBrown09314792015-02-19 16:04:40 +11007967 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07007968 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007969 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007970 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007971 }
NeilBrown57afd892005-06-21 17:17:13 -07007972
7973 if (!skipped) { /* actual IO requested */
7974 io_sectors += sectors;
7975 atomic_add(sectors, &mddev->recovery_active);
7976 }
7977
NeilBrowne875ece2011-07-28 11:39:24 +10007978 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7979 break;
7980
Linus Torvalds1da177e2005-04-16 15:20:36 -07007981 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10007982 if (j > max_sectors)
7983 /* when skipping, extra large numbers can be returned. */
7984 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007985 if (j > 2)
7986 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007987 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007988 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007989 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007990 * visible in /proc/mdstat
7991 */
7992 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007993
7994 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007995 continue;
7996
NeilBrown57afd892005-06-21 17:17:13 -07007997 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007998 repeat:
7999 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8000 /* step marks */
8001 int next = (last_mark+1) % SYNC_MARKS;
8002
8003 mddev->resync_mark = mark[next];
8004 mddev->resync_mark_cnt = mark_cnt[next];
8005 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008006 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007 last_mark = next;
8008 }
8009
NeilBrownc91abf52013-11-19 12:02:01 +11008010 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8011 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008012
8013 /*
8014 * this loop exits only if either when we are slower than
8015 * the 'hard' speed limit, or the system was IO-idle for
8016 * a jiffy.
8017 * the system might be non-idle CPU-wise, but we only care
8018 * about not overloading the IO subsystem. (things like an
8019 * e2fsck being done on the RAID array should execute fast)
8020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008021 cond_resched();
8022
Xiao Niac7e50a2014-08-07 09:37:41 -04008023 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8024 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008025 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008026
NeilBrown88202a02006-01-06 00:21:36 -08008027 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008028 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008029 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008030 goto repeat;
8031 }
NeilBrownac8fa412015-02-19 16:55:00 +11008032 if (!is_mddev_idle(mddev, 0)) {
8033 /*
8034 * Give other IO more of a chance.
8035 * The faster the devices, the less we wait.
8036 */
8037 wait_event(mddev->recovery_wait,
8038 !atomic_read(&mddev->recovery_active));
8039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008040 }
8041 }
NeilBrownc91abf52013-11-19 12:02:01 +11008042 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
8043 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8044 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045 /*
8046 * this also signals 'finished resyncing' to md_stop
8047 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008048 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008049 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8050
NeilBrown5ed1df22015-07-24 13:27:08 +10008051 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8052 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8053 mddev->curr_resync > 2) {
8054 mddev->curr_resync_completed = mddev->curr_resync;
8055 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8056 }
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008057 /* tell personality and other nodes that we are finished */
8058 if (mddev_is_clustered(mddev)) {
8059 md_cluster_ops->resync_finish(mddev);
8060 cluster_resync_finished = true;
8061 }
NeilBrown09314792015-02-19 16:04:40 +11008062 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063
NeilBrowndfc70642008-05-23 13:04:39 -07008064 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008065 mddev->curr_resync > 2) {
8066 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8067 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8068 if (mddev->curr_resync >= mddev->recovery_cp) {
8069 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07008070 "md: checkpointing %s of %s.\n",
8071 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008072 if (test_bit(MD_RECOVERY_ERROR,
8073 &mddev->recovery))
8074 mddev->recovery_cp =
8075 mddev->curr_resync_completed;
8076 else
8077 mddev->recovery_cp =
8078 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008079 }
8080 } else
8081 mddev->recovery_cp = MaxSector;
8082 } else {
8083 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8084 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008085 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008086 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008087 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008088 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008089 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008090 !test_bit(Faulty, &rdev->flags) &&
8091 !test_bit(In_sync, &rdev->flags) &&
8092 rdev->recovery_offset < mddev->curr_resync)
8093 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008094 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008096 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008097 skip:
NeilBrown17571282006-12-10 02:20:52 -08008098 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008100 if (mddev_is_clustered(mddev) &&
8101 test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8102 !cluster_resync_finished)
8103 md_cluster_ops->resync_finish(mddev);
8104
NeilBrown23da4222014-12-15 12:57:01 +11008105 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008106 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8107 /* We completed so min/max setting can be forgotten if used. */
8108 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8109 mddev->resync_min = 0;
8110 mddev->resync_max = MaxSector;
8111 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8112 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008113 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008115 spin_unlock(&mddev->lock);
8116
Linus Torvalds1da177e2005-04-16 15:20:36 -07008117 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008119 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008120}
NeilBrown29269552006-03-27 01:18:10 -08008121EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008122
NeilBrown746d3202013-04-24 11:42:41 +10008123static int remove_and_add_spares(struct mddev *mddev,
8124 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008125{
NeilBrown3cb03002011-10-11 16:45:26 +11008126 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008127 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008128 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008129
NeilBrowndafb20f2012-03-19 12:46:39 +11008130 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10008131 if ((this == NULL || rdev == this) &&
8132 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008133 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008134 (test_bit(Faulty, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008135 (!test_bit(In_sync, &rdev->flags) &&
8136 !test_bit(Journal, &rdev->flags))) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008137 atomic_read(&rdev->nr_pending)==0) {
8138 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008139 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008140 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008141 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008142 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008143 }
8144 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008145 if (removed && mddev->kobj.sd)
8146 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008147
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008148 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008149 goto no_add;
8150
NeilBrowndafb20f2012-03-19 12:46:39 +11008151 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008152 if (this && this != rdev)
8153 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008154 if (test_bit(Candidate, &rdev->flags))
8155 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008156 if (rdev->raid_disk >= 0 &&
8157 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008158 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008159 !test_bit(Faulty, &rdev->flags))
8160 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008161 if (rdev->raid_disk >= 0)
8162 continue;
8163 if (test_bit(Faulty, &rdev->flags))
8164 continue;
Shaohua Li3069aa82015-08-13 14:31:56 -07008165 if (test_bit(Journal, &rdev->flags))
8166 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008167 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11008168 ! (rdev->saved_raid_disk >= 0 &&
8169 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10008170 continue;
8171
NeilBrownd01552a2015-10-31 11:00:56 +11008172 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008173 if (mddev->pers->
8174 hot_add_disk(mddev, rdev) == 0) {
8175 if (sysfs_link_rdev(mddev, rdev))
8176 /* failure here is OK */;
8177 spares++;
8178 md_new_event(mddev);
8179 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008180 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008181 }
NeilBrown746d3202013-04-24 11:42:41 +10008182no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008183 if (removed)
8184 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008185 return spares;
8186}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008187
NeilBrownac05f252014-09-30 08:10:42 +10008188static void md_start_sync(struct work_struct *ws)
8189{
8190 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008191 int ret = 0;
8192
8193 if (mddev_is_clustered(mddev)) {
8194 ret = md_cluster_ops->resync_start(mddev);
8195 if (ret) {
8196 mddev->sync_thread = NULL;
8197 goto out;
8198 }
8199 }
NeilBrownac05f252014-09-30 08:10:42 +10008200
8201 mddev->sync_thread = md_register_thread(md_do_sync,
8202 mddev,
8203 "resync");
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008204out:
NeilBrownac05f252014-09-30 08:10:42 +10008205 if (!mddev->sync_thread) {
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008206 if (!(mddev_is_clustered(mddev) && ret == -EAGAIN))
8207 printk(KERN_ERR "%s: could not start resync"
8208 " thread...\n",
8209 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008210 /* leave the spares where they are, it shouldn't hurt */
8211 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8212 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8213 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8214 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8215 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008216 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008217 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8218 &mddev->recovery))
8219 if (mddev->sysfs_action)
8220 sysfs_notify_dirent_safe(mddev->sysfs_action);
8221 } else
8222 md_wakeup_thread(mddev->sync_thread);
8223 sysfs_notify_dirent_safe(mddev->sysfs_action);
8224 md_new_event(mddev);
8225}
8226
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227/*
8228 * This routine is regularly called by all per-raid-array threads to
8229 * deal with generic issues like resync and super-block update.
8230 * Raid personalities that don't have a thread (linear/raid0) do not
8231 * need this as they never do any recovery or update the superblock.
8232 *
8233 * It does not do any resync itself, but rather "forks" off other threads
8234 * to do that as needed.
8235 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8236 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008237 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238 * and wakeups up this thread which will reap the thread and finish up.
8239 * This thread also removes any faulty devices (with nr_pending == 0).
8240 *
8241 * The overall approach is:
8242 * 1/ if the superblock needs updating, update it.
8243 * 2/ If a recovery thread is running, don't do anything else.
8244 * 3/ If recovery has finished, clean up, possibly marking spares active.
8245 * 4/ If there are any faulty devices, remove them.
8246 * 5/ If array is degraded, try to add spares devices
8247 * 6/ If array has spares or is not in-sync, start a resync thread.
8248 */
NeilBrownfd01b882011-10-11 16:47:53 +11008249void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008250{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008251 if (mddev->suspended)
8252 return;
8253
NeilBrown5f404022005-06-21 17:17:16 -07008254 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008255 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008256
NeilBrownfca4d842005-06-21 17:17:11 -07008257 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008258 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008259 printk(KERN_INFO "md: %s in immediate safe mode\n",
8260 mdname(mddev));
8261 mddev->safemode = 2;
8262 }
8263 flush_signals(current);
8264 }
8265
NeilBrownc89a8ee2008-08-05 15:54:13 +10008266 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8267 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008268 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008269 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008270 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008271 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008272 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008273 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8274 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008275 ))
8276 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008277
NeilBrowndf5b89b2006-03-27 01:18:20 -08008278 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008279 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008280
NeilBrownc89a8ee2008-08-05 15:54:13 +10008281 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008282 struct md_rdev *rdev;
8283 if (!mddev->external && mddev->in_sync)
8284 /* 'Blocked' flag not needed as failed devices
8285 * will be recorded if array switched to read/write.
8286 * Leaving it set will prevent the device
8287 * from being removed.
8288 */
8289 rdev_for_each(rdev, mddev)
8290 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008291 /* On a read-only array we can:
8292 * - remove failed devices
8293 * - add already-in_sync devices if the array itself
8294 * is in-sync.
8295 * As we only add devices that are already in-sync,
8296 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008297 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008298 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008299 /* There is no thread, but we need to call
8300 * ->spare_active and clear saved_raid_disk
8301 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008302 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008303 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008304 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008305 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Lid4929ad2015-09-18 10:20:12 -07008306 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008307 goto unlock;
8308 }
8309
NeilBrown31a59e32008-04-30 00:52:30 -07008310 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008311 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008312 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008313 if (mddev->safemode &&
8314 !atomic_read(&mddev->writes_pending) &&
8315 !mddev->in_sync &&
8316 mddev->recovery_cp == MaxSector) {
8317 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008318 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008319 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008320 }
8321 if (mddev->safemode == 1)
8322 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008323 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008324 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008325 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008326 }
NeilBrownfca4d842005-06-21 17:17:11 -07008327
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05008328 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07008329 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008330
Linus Torvalds1da177e2005-04-16 15:20:36 -07008331 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8332 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8333 /* resync/recovery still happening */
8334 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8335 goto unlock;
8336 }
8337 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008338 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339 goto unlock;
8340 }
Neil Brown72a23c22008-06-28 08:31:41 +10008341 /* Set RUNNING before clearing NEEDED to avoid
8342 * any transients in the value of "sync_action".
8343 */
NeilBrown72f36d52012-10-11 14:25:57 +11008344 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008345 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008346 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008347 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008348 /* Clear some bits that don't mean anything, but
8349 * might be left set
8350 */
NeilBrown24dd4692005-11-08 21:39:26 -08008351 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8352 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008353
NeilBrowned209582012-04-24 10:23:14 +10008354 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8355 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008356 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008357 /* no recovery is running.
8358 * remove any failed drives, then
8359 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008360 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008361 * the personality to fail the re-add.
8362 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008363
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008364 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008365 if (mddev->pers->check_reshape == NULL ||
8366 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008367 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008368 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008369 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008370 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008371 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008372 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8373 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008374 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008375 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008376 } else if (mddev->recovery_cp < MaxSector) {
8377 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008378 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008379 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8380 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008381 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008382
Linus Torvalds1da177e2005-04-16 15:20:36 -07008383 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008384 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008385 /* We are adding a device or devices to an array
8386 * which has the bitmap stored on all devices.
8387 * So make sure all bitmap pages get written
8388 */
8389 bitmap_write_all(mddev->bitmap);
8390 }
NeilBrownac05f252014-09-30 08:10:42 +10008391 INIT_WORK(&mddev->del_work, md_start_sync);
8392 queue_work(md_misc_wq, &mddev->del_work);
8393 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394 }
NeilBrownac05f252014-09-30 08:10:42 +10008395 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008396 if (!mddev->sync_thread) {
8397 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008398 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008399 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8400 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008401 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008402 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008403 }
NeilBrownac05f252014-09-30 08:10:42 +10008404 unlock:
8405 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008406 mddev_unlock(mddev);
8407 }
8408}
NeilBrown6c144d32014-09-30 16:15:38 +10008409EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008410
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008411void md_reap_sync_thread(struct mddev *mddev)
8412{
8413 struct md_rdev *rdev;
8414
8415 /* resync has finished, collect result */
8416 md_unregister_thread(&mddev->sync_thread);
8417 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8418 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8419 /* success...*/
8420 /* activate any spares */
8421 if (mddev->pers->spare_active(mddev)) {
8422 sysfs_notify(&mddev->kobj, NULL,
8423 "degraded");
8424 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8425 }
8426 }
8427 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8428 mddev->pers->finish_reshape)
8429 mddev->pers->finish_reshape(mddev);
8430
8431 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008432 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008433 */
NeilBrownf4667222013-12-09 12:04:56 +11008434 if (!mddev->degraded)
8435 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008436 rdev->saved_raid_disk = -1;
8437
8438 md_update_sb(mddev, 1);
8439 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008440 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008441 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8442 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8443 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8444 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008445 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008446 /* flag recovery needed just to double check */
8447 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8448 sysfs_notify_dirent_safe(mddev->sysfs_action);
8449 md_new_event(mddev);
8450 if (mddev->event_work.func)
8451 queue_work(md_misc_wq, &mddev->event_work);
8452}
NeilBrown6c144d32014-09-30 16:15:38 +10008453EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008454
NeilBrownfd01b882011-10-11 16:47:53 +11008455void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008456{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008457 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008458 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008459 !test_bit(Blocked, &rdev->flags) &&
8460 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008461 msecs_to_jiffies(5000));
8462 rdev_dec_pending(rdev, mddev);
8463}
8464EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8465
NeilBrownc6563a82012-05-21 09:27:00 +10008466void md_finish_reshape(struct mddev *mddev)
8467{
8468 /* called be personality module when reshape completes. */
8469 struct md_rdev *rdev;
8470
8471 rdev_for_each(rdev, mddev) {
8472 if (rdev->data_offset > rdev->new_data_offset)
8473 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8474 else
8475 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8476 rdev->data_offset = rdev->new_data_offset;
8477 }
8478}
8479EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008480
8481/* Bad block management.
8482 * We can record which blocks on each device are 'bad' and so just
8483 * fail those blocks, or that stripe, rather than the whole device.
8484 * Entries in the bad-block table are 64bits wide. This comprises:
8485 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8486 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8487 * A 'shift' can be set so that larger blocks are tracked and
8488 * consequently larger devices can be covered.
8489 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8490 *
8491 * Locking of the bad-block table uses a seqlock so md_is_badblock
8492 * might need to retry if it is very unlucky.
8493 * We will sometimes want to check for bad blocks in a bi_end_io function,
8494 * so we use the write_seqlock_irq variant.
8495 *
8496 * When looking for a bad block we specify a range and want to
8497 * know if any block in the range is bad. So we binary-search
8498 * to the last range that starts at-or-before the given endpoint,
8499 * (or "before the sector after the target range")
8500 * then see if it ends after the given start.
8501 * We return
8502 * 0 if there are no known bad blocks in the range
8503 * 1 if there are known bad block which are all acknowledged
8504 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8505 * plus the start/length of the first bad section we overlap.
8506 */
8507int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8508 sector_t *first_bad, int *bad_sectors)
8509{
8510 int hi;
majianpengab056132012-11-06 17:13:44 +08008511 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008512 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008513 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008514 sector_t target = s + sectors;
8515 unsigned seq;
8516
8517 if (bb->shift > 0) {
8518 /* round the start down, and the end up */
8519 s >>= bb->shift;
8520 target += (1<<bb->shift) - 1;
8521 target >>= bb->shift;
8522 sectors = target - s;
8523 }
8524 /* 'target' is now the first block after the bad range */
8525
8526retry:
8527 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008528 lo = 0;
8529 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008530 hi = bb->count;
8531
8532 /* Binary search between lo and hi for 'target'
8533 * i.e. for the last range that starts before 'target'
8534 */
8535 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8536 * are known not to be the last range before target.
8537 * VARIANT: hi-lo is the number of possible
8538 * ranges, and decreases until it reaches 1
8539 */
8540 while (hi - lo > 1) {
8541 int mid = (lo + hi) / 2;
8542 sector_t a = BB_OFFSET(p[mid]);
8543 if (a < target)
8544 /* This could still be the one, earlier ranges
8545 * could not. */
8546 lo = mid;
8547 else
8548 /* This and later ranges are definitely out. */
8549 hi = mid;
8550 }
8551 /* 'lo' might be the last that started before target, but 'hi' isn't */
8552 if (hi > lo) {
8553 /* need to check all range that end after 's' to see if
8554 * any are unacknowledged.
8555 */
8556 while (lo >= 0 &&
8557 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8558 if (BB_OFFSET(p[lo]) < target) {
8559 /* starts before the end, and finishes after
8560 * the start, so they must overlap
8561 */
8562 if (rv != -1 && BB_ACK(p[lo]))
8563 rv = 1;
8564 else
8565 rv = -1;
8566 *first_bad = BB_OFFSET(p[lo]);
8567 *bad_sectors = BB_LEN(p[lo]);
8568 }
8569 lo--;
8570 }
8571 }
8572
8573 if (read_seqretry(&bb->lock, seq))
8574 goto retry;
8575
8576 return rv;
8577}
8578EXPORT_SYMBOL_GPL(md_is_badblock);
8579
8580/*
8581 * Add a range of bad blocks to the table.
8582 * This might extend the table, or might contract it
8583 * if two adjacent ranges can be merged.
8584 * We binary-search to find the 'insertion' point, then
8585 * decide how best to handle it.
8586 */
8587static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8588 int acknowledged)
8589{
8590 u64 *p;
8591 int lo, hi;
8592 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008593 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008594
8595 if (bb->shift < 0)
8596 /* badblocks are disabled */
8597 return 0;
8598
8599 if (bb->shift) {
8600 /* round the start down, and the end up */
8601 sector_t next = s + sectors;
8602 s >>= bb->shift;
8603 next += (1<<bb->shift) - 1;
8604 next >>= bb->shift;
8605 sectors = next - s;
8606 }
8607
Bian Yu905b0292013-10-12 01:10:03 -04008608 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008609
8610 p = bb->page;
8611 lo = 0;
8612 hi = bb->count;
8613 /* Find the last range that starts at-or-before 's' */
8614 while (hi - lo > 1) {
8615 int mid = (lo + hi) / 2;
8616 sector_t a = BB_OFFSET(p[mid]);
8617 if (a <= s)
8618 lo = mid;
8619 else
8620 hi = mid;
8621 }
8622 if (hi > lo && BB_OFFSET(p[lo]) > s)
8623 hi = lo;
8624
8625 if (hi > lo) {
8626 /* we found a range that might merge with the start
8627 * of our new range
8628 */
8629 sector_t a = BB_OFFSET(p[lo]);
8630 sector_t e = a + BB_LEN(p[lo]);
8631 int ack = BB_ACK(p[lo]);
8632 if (e >= s) {
8633 /* Yes, we can merge with a previous range */
8634 if (s == a && s + sectors >= e)
8635 /* new range covers old */
8636 ack = acknowledged;
8637 else
8638 ack = ack && acknowledged;
8639
8640 if (e < s + sectors)
8641 e = s + sectors;
8642 if (e - a <= BB_MAX_LEN) {
8643 p[lo] = BB_MAKE(a, e-a, ack);
8644 s = e;
8645 } else {
8646 /* does not all fit in one range,
8647 * make p[lo] maximal
8648 */
8649 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8650 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8651 s = a + BB_MAX_LEN;
8652 }
8653 sectors = e - s;
8654 }
8655 }
8656 if (sectors && hi < bb->count) {
8657 /* 'hi' points to the first range that starts after 's'.
8658 * Maybe we can merge with the start of that range */
8659 sector_t a = BB_OFFSET(p[hi]);
8660 sector_t e = a + BB_LEN(p[hi]);
8661 int ack = BB_ACK(p[hi]);
8662 if (a <= s + sectors) {
8663 /* merging is possible */
8664 if (e <= s + sectors) {
8665 /* full overlap */
8666 e = s + sectors;
8667 ack = acknowledged;
8668 } else
8669 ack = ack && acknowledged;
8670
8671 a = s;
8672 if (e - a <= BB_MAX_LEN) {
8673 p[hi] = BB_MAKE(a, e-a, ack);
8674 s = e;
8675 } else {
8676 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8677 s = a + BB_MAX_LEN;
8678 }
8679 sectors = e - s;
8680 lo = hi;
8681 hi++;
8682 }
8683 }
8684 if (sectors == 0 && hi < bb->count) {
8685 /* we might be able to combine lo and hi */
8686 /* Note: 's' is at the end of 'lo' */
8687 sector_t a = BB_OFFSET(p[hi]);
8688 int lolen = BB_LEN(p[lo]);
8689 int hilen = BB_LEN(p[hi]);
8690 int newlen = lolen + hilen - (s - a);
8691 if (s >= a && newlen < BB_MAX_LEN) {
8692 /* yes, we can combine them */
8693 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8694 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8695 memmove(p + hi, p + hi + 1,
8696 (bb->count - hi - 1) * 8);
8697 bb->count--;
8698 }
8699 }
8700 while (sectors) {
8701 /* didn't merge (it all).
8702 * Need to add a range just before 'hi' */
8703 if (bb->count >= MD_MAX_BADBLOCKS) {
8704 /* No room for more */
8705 rv = 0;
8706 break;
8707 } else {
8708 int this_sectors = sectors;
8709 memmove(p + hi + 1, p + hi,
8710 (bb->count - hi) * 8);
8711 bb->count++;
8712
8713 if (this_sectors > BB_MAX_LEN)
8714 this_sectors = BB_MAX_LEN;
8715 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8716 sectors -= this_sectors;
8717 s += this_sectors;
8718 }
8719 }
8720
8721 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008722 if (!acknowledged)
8723 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008724 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008725
8726 return rv;
8727}
8728
NeilBrown3cb03002011-10-11 16:45:26 +11008729int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008730 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008731{
NeilBrownc6563a82012-05-21 09:27:00 +10008732 int rv;
8733 if (is_new)
8734 s += rdev->new_data_offset;
8735 else
8736 s += rdev->data_offset;
8737 rv = md_set_badblocks(&rdev->badblocks,
8738 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008739 if (rv) {
8740 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008741 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008742 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
NeilBrown55ce74d2015-08-14 11:11:10 +10008743 set_bit(MD_CHANGE_PENDING, &rdev->mddev->flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008744 md_wakeup_thread(rdev->mddev->thread);
8745 }
8746 return rv;
8747}
8748EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8749
8750/*
8751 * Remove a range of bad blocks from the table.
8752 * This may involve extending the table if we spilt a region,
8753 * but it must not fail. So if the table becomes full, we just
8754 * drop the remove request.
8755 */
8756static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8757{
8758 u64 *p;
8759 int lo, hi;
8760 sector_t target = s + sectors;
8761 int rv = 0;
8762
8763 if (bb->shift > 0) {
8764 /* When clearing we round the start up and the end down.
8765 * This should not matter as the shift should align with
8766 * the block size and no rounding should ever be needed.
8767 * However it is better the think a block is bad when it
8768 * isn't than to think a block is not bad when it is.
8769 */
8770 s += (1<<bb->shift) - 1;
8771 s >>= bb->shift;
8772 target >>= bb->shift;
8773 sectors = target - s;
8774 }
8775
8776 write_seqlock_irq(&bb->lock);
8777
8778 p = bb->page;
8779 lo = 0;
8780 hi = bb->count;
8781 /* Find the last range that starts before 'target' */
8782 while (hi - lo > 1) {
8783 int mid = (lo + hi) / 2;
8784 sector_t a = BB_OFFSET(p[mid]);
8785 if (a < target)
8786 lo = mid;
8787 else
8788 hi = mid;
8789 }
8790 if (hi > lo) {
8791 /* p[lo] is the last range that could overlap the
8792 * current range. Earlier ranges could also overlap,
8793 * but only this one can overlap the end of the range.
8794 */
8795 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8796 /* Partial overlap, leave the tail of this range */
8797 int ack = BB_ACK(p[lo]);
8798 sector_t a = BB_OFFSET(p[lo]);
8799 sector_t end = a + BB_LEN(p[lo]);
8800
8801 if (a < s) {
8802 /* we need to split this range */
8803 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008804 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008805 goto out;
8806 }
8807 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8808 bb->count++;
8809 p[lo] = BB_MAKE(a, s-a, ack);
8810 lo++;
8811 }
8812 p[lo] = BB_MAKE(target, end - target, ack);
8813 /* there is no longer an overlap */
8814 hi = lo;
8815 lo--;
8816 }
8817 while (lo >= 0 &&
8818 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8819 /* This range does overlap */
8820 if (BB_OFFSET(p[lo]) < s) {
8821 /* Keep the early parts of this range. */
8822 int ack = BB_ACK(p[lo]);
8823 sector_t start = BB_OFFSET(p[lo]);
8824 p[lo] = BB_MAKE(start, s - start, ack);
8825 /* now low doesn't overlap, so.. */
8826 break;
8827 }
8828 lo--;
8829 }
8830 /* 'lo' is strictly before, 'hi' is strictly after,
8831 * anything between needs to be discarded
8832 */
8833 if (hi - lo > 1) {
8834 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8835 bb->count -= (hi - lo - 1);
8836 }
8837 }
8838
8839 bb->changed = 1;
8840out:
8841 write_sequnlock_irq(&bb->lock);
8842 return rv;
8843}
8844
NeilBrownc6563a82012-05-21 09:27:00 +10008845int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8846 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008847{
NeilBrownc6563a82012-05-21 09:27:00 +10008848 if (is_new)
8849 s += rdev->new_data_offset;
8850 else
8851 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008852 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008853 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008854}
8855EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8856
8857/*
8858 * Acknowledge all bad blocks in a list.
8859 * This only succeeds if ->changed is clear. It is used by
8860 * in-kernel metadata updates
8861 */
8862void md_ack_all_badblocks(struct badblocks *bb)
8863{
8864 if (bb->page == NULL || bb->changed)
8865 /* no point even trying */
8866 return;
8867 write_seqlock_irq(&bb->lock);
8868
majianpengecb178b2012-03-19 12:46:42 +11008869 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008870 u64 *p = bb->page;
8871 int i;
8872 for (i = 0; i < bb->count ; i++) {
8873 if (!BB_ACK(p[i])) {
8874 sector_t start = BB_OFFSET(p[i]);
8875 int len = BB_LEN(p[i]);
8876 p[i] = BB_MAKE(start, len, 1);
8877 }
8878 }
NeilBrownde393cd2011-07-28 11:31:48 +10008879 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008880 }
8881 write_sequnlock_irq(&bb->lock);
8882}
8883EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8884
NeilBrown16c791a2011-07-28 11:31:47 +10008885/* sysfs access to bad-blocks list.
8886 * We present two files.
8887 * 'bad-blocks' lists sector numbers and lengths of ranges that
8888 * are recorded as bad. The list is truncated to fit within
8889 * the one-page limit of sysfs.
8890 * Writing "sector length" to this file adds an acknowledged
8891 * bad block list.
8892 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8893 * been acknowledged. Writing to this file adds bad blocks
8894 * without acknowledging them. This is largely for testing.
8895 */
8896
8897static ssize_t
8898badblocks_show(struct badblocks *bb, char *page, int unack)
8899{
8900 size_t len;
8901 int i;
8902 u64 *p = bb->page;
8903 unsigned seq;
8904
8905 if (bb->shift < 0)
8906 return 0;
8907
8908retry:
8909 seq = read_seqbegin(&bb->lock);
8910
8911 len = 0;
8912 i = 0;
8913
8914 while (len < PAGE_SIZE && i < bb->count) {
8915 sector_t s = BB_OFFSET(p[i]);
8916 unsigned int length = BB_LEN(p[i]);
8917 int ack = BB_ACK(p[i]);
8918 i++;
8919
8920 if (unack && ack)
8921 continue;
8922
8923 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8924 (unsigned long long)s << bb->shift,
8925 length << bb->shift);
8926 }
NeilBrownde393cd2011-07-28 11:31:48 +10008927 if (unack && len == 0)
8928 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008929
8930 if (read_seqretry(&bb->lock, seq))
8931 goto retry;
8932
8933 return len;
8934}
8935
8936#define DO_DEBUG 1
8937
8938static ssize_t
8939badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8940{
8941 unsigned long long sector;
8942 int length;
8943 char newline;
8944#ifdef DO_DEBUG
8945 /* Allow clearing via sysfs *only* for testing/debugging.
8946 * Normally only a successful write may clear a badblock
8947 */
8948 int clear = 0;
8949 if (page[0] == '-') {
8950 clear = 1;
8951 page++;
8952 }
8953#endif /* DO_DEBUG */
8954
8955 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8956 case 3:
8957 if (newline != '\n')
8958 return -EINVAL;
8959 case 2:
8960 if (length <= 0)
8961 return -EINVAL;
8962 break;
8963 default:
8964 return -EINVAL;
8965 }
8966
8967#ifdef DO_DEBUG
8968 if (clear) {
8969 md_clear_badblocks(bb, sector, length);
8970 return len;
8971 }
8972#endif /* DO_DEBUG */
8973 if (md_set_badblocks(bb, sector, length, !unack))
8974 return len;
8975 else
8976 return -ENOSPC;
8977}
8978
Adrian Bunk75c96f82005-05-05 16:16:09 -07008979static int md_notify_reboot(struct notifier_block *this,
8980 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981{
8982 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008983 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008984 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008985
NeilBrownc744a652012-03-19 12:46:37 +11008986 for_each_mddev(mddev, tmp) {
8987 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008988 if (mddev->pers)
8989 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008990 if (mddev->persistent)
8991 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008992 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008993 }
NeilBrownc744a652012-03-19 12:46:37 +11008994 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008995 }
NeilBrownc744a652012-03-19 12:46:37 +11008996 /*
8997 * certain more exotic SCSI devices are known to be
8998 * volatile wrt too early system reboots. While the
8999 * right place to handle this issue is the given
9000 * driver, we do want to have a safe RAID driver ...
9001 */
9002 if (need_delay)
9003 mdelay(1000*1);
9004
Linus Torvalds1da177e2005-04-16 15:20:36 -07009005 return NOTIFY_DONE;
9006}
9007
Adrian Bunk75c96f82005-05-05 16:16:09 -07009008static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009009 .notifier_call = md_notify_reboot,
9010 .next = NULL,
9011 .priority = INT_MAX, /* before any real devices */
9012};
9013
9014static void md_geninit(void)
9015{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009016 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009017
Denis V. Lunevc7705f32008-04-29 01:02:35 -07009018 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009019}
9020
Adrian Bunk75c96f82005-05-05 16:16:09 -07009021static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009022{
Tejun Heoe804ac72010-10-15 15:36:08 +02009023 int ret = -ENOMEM;
9024
Tejun Heoada609e2011-01-25 14:35:54 +01009025 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009026 if (!md_wq)
9027 goto err_wq;
9028
9029 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9030 if (!md_misc_wq)
9031 goto err_misc_wq;
9032
9033 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9034 goto err_md;
9035
9036 if ((ret = register_blkdev(0, "mdp")) < 0)
9037 goto err_mdp;
9038 mdp_major = ret;
9039
NeilBrownaf5628f2014-07-31 13:54:54 +10009040 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009041 md_probe, NULL, NULL);
9042 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009043 md_probe, NULL, NULL);
9044
Linus Torvalds1da177e2005-04-16 15:20:36 -07009045 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009046 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009047
9048 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009050
Tejun Heoe804ac72010-10-15 15:36:08 +02009051err_mdp:
9052 unregister_blkdev(MD_MAJOR, "md");
9053err_md:
9054 destroy_workqueue(md_misc_wq);
9055err_misc_wq:
9056 destroy_workqueue(md_wq);
9057err_wq:
9058 return ret;
9059}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009060
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009061static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009062{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009063 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9064 struct md_rdev *rdev2;
9065 int role, ret;
9066 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009067
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009068 /* Check for change of roles in the active devices */
9069 rdev_for_each(rdev2, mddev) {
9070 if (test_bit(Faulty, &rdev2->flags))
9071 continue;
9072
9073 /* Check if the roles changed */
9074 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009075
9076 if (test_bit(Candidate, &rdev2->flags)) {
9077 if (role == 0xfffe) {
9078 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9079 md_kick_rdev_from_array(rdev2);
9080 continue;
9081 }
9082 else
9083 clear_bit(Candidate, &rdev2->flags);
9084 }
9085
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009086 if (role != rdev2->raid_disk) {
9087 /* got activated */
9088 if (rdev2->raid_disk == -1 && role != 0xffff) {
9089 rdev2->saved_raid_disk = role;
9090 ret = remove_and_add_spares(mddev, rdev2);
9091 pr_info("Activated spare: %s\n",
9092 bdevname(rdev2->bdev,b));
9093 continue;
9094 }
9095 /* device faulty
9096 * We just want to do the minimum to mark the disk
9097 * as faulty. The recovery is performed by the
9098 * one who initiated the error.
9099 */
9100 if ((role == 0xfffe) || (role == 0xfffd)) {
9101 md_error(mddev, rdev2);
9102 clear_bit(Blocked, &rdev2->flags);
9103 }
9104 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009105 }
9106
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009107 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9108 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009109
9110 /* Finally set the event to be up to date */
9111 mddev->events = le64_to_cpu(sb->events);
9112}
9113
9114static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9115{
9116 int err;
9117 struct page *swapout = rdev->sb_page;
9118 struct mdp_superblock_1 *sb;
9119
9120 /* Store the sb page of the rdev in the swapout temporary
9121 * variable in case we err in the future
9122 */
9123 rdev->sb_page = NULL;
9124 alloc_disk_sb(rdev);
9125 ClearPageUptodate(rdev->sb_page);
9126 rdev->sb_loaded = 0;
9127 err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version);
9128
9129 if (err < 0) {
9130 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9131 __func__, __LINE__, rdev->desc_nr, err);
9132 put_page(rdev->sb_page);
9133 rdev->sb_page = swapout;
9134 rdev->sb_loaded = 1;
9135 return err;
9136 }
9137
9138 sb = page_address(rdev->sb_page);
9139 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9140 * is not set
9141 */
9142
9143 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9144 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9145
9146 /* The other node finished recovery, call spare_active to set
9147 * device In_sync and mddev->degraded
9148 */
9149 if (rdev->recovery_offset == MaxSector &&
9150 !test_bit(In_sync, &rdev->flags) &&
9151 mddev->pers->spare_active(mddev))
9152 sysfs_notify(&mddev->kobj, NULL, "degraded");
9153
9154 put_page(swapout);
9155 return 0;
9156}
9157
9158void md_reload_sb(struct mddev *mddev, int nr)
9159{
9160 struct md_rdev *rdev;
9161 int err;
9162
9163 /* Find the rdev */
9164 rdev_for_each_rcu(rdev, mddev) {
9165 if (rdev->desc_nr == nr)
9166 break;
9167 }
9168
9169 if (!rdev || rdev->desc_nr != nr) {
9170 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9171 return;
9172 }
9173
9174 err = read_rdev(mddev, rdev);
9175 if (err < 0)
9176 return;
9177
9178 check_sb_changes(mddev, rdev);
9179
9180 /* Read all rdev's to update recovery_offset */
9181 rdev_for_each_rcu(rdev, mddev)
9182 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009183}
9184EXPORT_SYMBOL(md_reload_sb);
9185
Linus Torvalds1da177e2005-04-16 15:20:36 -07009186#ifndef MODULE
9187
9188/*
9189 * Searches all registered partitions for autorun RAID arrays
9190 * at boot time.
9191 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009192
9193static LIST_HEAD(all_detected_devices);
9194struct detected_devices_node {
9195 struct list_head list;
9196 dev_t dev;
9197};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009198
9199void md_autodetect_dev(dev_t dev)
9200{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009201 struct detected_devices_node *node_detected_dev;
9202
9203 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9204 if (node_detected_dev) {
9205 node_detected_dev->dev = dev;
9206 list_add_tail(&node_detected_dev->list, &all_detected_devices);
9207 } else {
9208 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
9209 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
9210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009211}
9212
Linus Torvalds1da177e2005-04-16 15:20:36 -07009213static void autostart_arrays(int part)
9214{
NeilBrown3cb03002011-10-11 16:45:26 +11009215 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009216 struct detected_devices_node *node_detected_dev;
9217 dev_t dev;
9218 int i_scanned, i_passed;
9219
9220 i_scanned = 0;
9221 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009222
9223 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
9224
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009225 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9226 i_scanned++;
9227 node_detected_dev = list_entry(all_detected_devices.next,
9228 struct detected_devices_node, list);
9229 list_del(&node_detected_dev->list);
9230 dev = node_detected_dev->dev;
9231 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07009232 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009233 if (IS_ERR(rdev))
9234 continue;
9235
NeilBrown403df472014-09-30 15:52:29 +10009236 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009237 continue;
NeilBrown403df472014-09-30 15:52:29 +10009238
NeilBrownd0fae182008-03-04 14:29:31 -08009239 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009240 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009241 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009242 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009243
9244 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
9245 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009246
9247 autorun_devices(part);
9248}
9249
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009250#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009251
9252static __exit void md_exit(void)
9253{
NeilBrownfd01b882011-10-11 16:47:53 +11009254 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009255 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009256 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009257
NeilBrownaf5628f2014-07-31 13:54:54 +10009258 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009259 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009260
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009261 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009262 unregister_blkdev(mdp_major, "mdp");
9263 unregister_reboot_notifier(&md_notifier);
9264 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009265
9266 /* We cannot unload the modules while some process is
9267 * waiting for us in select() or poll() - wake them up
9268 */
9269 md_unloading = 1;
9270 while (waitqueue_active(&md_event_waiters)) {
9271 /* not safe to leave yet */
9272 wake_up(&md_event_waiters);
9273 msleep(delay);
9274 delay += delay;
9275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009276 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009277
NeilBrown29ac4aa2008-02-06 01:39:58 -08009278 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009279 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11009280 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009281 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009282 destroy_workqueue(md_misc_wq);
9283 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009284}
9285
Dan Williams685784a2007-07-09 11:56:42 -07009286subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009287module_exit(md_exit)
9288
NeilBrownf91de922005-11-08 21:39:36 -08009289static int get_ro(char *buffer, struct kernel_param *kp)
9290{
9291 return sprintf(buffer, "%d", start_readonly);
9292}
9293static int set_ro(const char *val, struct kernel_param *kp)
9294{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009295 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009296}
9297
NeilBrown80ca3a42006-07-10 04:44:18 -07009298module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9299module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009300module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009301
Linus Torvalds1da177e2005-04-16 15:20:36 -07009302MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009303MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009304MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009305MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);