blob: 61aacab424cf1860dbb5651f5a4fc6c9f7188027 [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{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100317 if (mddev->suspended++)
318 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100319 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{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100329 if (--mddev->suspended)
330 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100331 wake_up(&mddev->sb_wait);
332 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500333
Jonathan Brassow47525e52012-05-22 13:55:29 +1000334 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500335 md_wakeup_thread(mddev->thread);
336 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100337}
NeilBrown390ee602010-06-01 19:37:27 +1000338EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100339
NeilBrownfd01b882011-10-11 16:47:53 +1100340int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000341{
NeilBrown5c675f82014-12-15 12:56:56 +1100342 struct md_personality *pers = mddev->pers;
343 int ret = 0;
344
345 rcu_read_lock();
346 if (mddev->suspended)
347 ret = 1;
348 else if (pers && pers->congested)
349 ret = pers->congested(mddev, bits);
350 rcu_read_unlock();
351 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000352}
NeilBrown5c675f82014-12-15 12:56:56 +1100353EXPORT_SYMBOL_GPL(mddev_congested);
354static int md_congested(void *data, int bits)
355{
356 struct mddev *mddev = data;
357 return mddev_congested(mddev, bits);
358}
NeilBrown3fa841d2009-09-23 18:10:29 +1000359
NeilBrowna2826aa2009-12-14 12:49:49 +1100360/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200361 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100362 */
363
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200364static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100365{
NeilBrown3cb03002011-10-11 16:45:26 +1100366 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100367 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100368
369 rdev_dec_pending(rdev, mddev);
370
371 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200372 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200373 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100374 }
375 bio_put(bio);
376}
377
NeilBrowna7a07e62010-12-09 16:04:25 +1100378static void md_submit_flush_data(struct work_struct *ws);
379
NeilBrowna035fc32010-12-09 16:17:51 +1100380static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100381{
NeilBrownfd01b882011-10-11 16:47:53 +1100382 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100383 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100384
NeilBrowna7a07e62010-12-09 16:04:25 +1100385 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
386 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100387 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100388 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100389 if (rdev->raid_disk >= 0 &&
390 !test_bit(Faulty, &rdev->flags)) {
391 /* Take two references, one is dropped
392 * when request finishes, one after
393 * we reclaim rcu_read_lock
394 */
395 struct bio *bi;
396 atomic_inc(&rdev->nr_pending);
397 atomic_inc(&rdev->nr_pending);
398 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000399 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200400 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100401 bi->bi_private = rdev;
402 bi->bi_bdev = rdev->bdev;
403 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200404 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100405 rcu_read_lock();
406 rdev_dec_pending(rdev, mddev);
407 }
408 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100409 if (atomic_dec_and_test(&mddev->flush_pending))
410 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100411}
412
Tejun Heoe9c74692010-09-03 11:56:18 +0200413static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100414{
NeilBrownfd01b882011-10-11 16:47:53 +1100415 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200416 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100417
Kent Overstreet4f024f32013-10-11 15:44:27 -0700418 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100419 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200420 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100421 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200422 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200423 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100424 }
NeilBrown2b74e122010-12-09 15:59:01 +1100425
426 mddev->flush_bio = NULL;
427 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100428}
429
NeilBrownfd01b882011-10-11 16:47:53 +1100430void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100431{
NeilBrown85572d72014-12-15 12:56:56 +1100432 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100433 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200434 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100435 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200436 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100437 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100438
NeilBrowna035fc32010-12-09 16:17:51 +1100439 INIT_WORK(&mddev->flush_work, submit_flushes);
440 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100441}
Tejun Heoe9c74692010-09-03 11:56:18 +0200442EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100443
NeilBrown74018dc2012-07-31 09:08:15 +0200444void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000445{
NeilBrown9cbb1752012-07-31 09:08:14 +0200446 struct mddev *mddev = cb->data;
447 md_wakeup_thread(mddev->thread);
448 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000449}
NeilBrown9cbb1752012-07-31 09:08:14 +0200450EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000451
NeilBrownfd01b882011-10-11 16:47:53 +1100452static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 atomic_inc(&mddev->active);
455 return mddev;
456}
457
Dan Williams5fd3a172009-03-04 00:57:25 -0700458static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100459
NeilBrownfd01b882011-10-11 16:47:53 +1100460static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
NeilBrowna167f662010-10-26 18:31:13 +1100462 struct bio_set *bs = NULL;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
465 return;
NeilBrownd3374822009-01-09 08:31:10 +1100466 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100467 mddev->ctime == 0 && !mddev->hold_active) {
468 /* Array is not configured at all, and not held active,
469 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100470 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100471 bs = mddev->bio_set;
472 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100473 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200474 /* We did a probe so need to clean up. Call
475 * queue_work inside the spinlock so that
476 * flush_workqueue() after mddev_find will
477 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100478 */
479 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200480 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100481 } else
482 kfree(mddev);
483 }
484 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100485 if (bs)
486 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Sasha Levin25b2edf2015-07-24 18:19:58 -0400489static void md_safemode_timeout(unsigned long data);
490
NeilBrownfd01b882011-10-11 16:47:53 +1100491void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100492{
493 mutex_init(&mddev->open_mutex);
494 mutex_init(&mddev->reconfig_mutex);
495 mutex_init(&mddev->bitmap_info.mutex);
496 INIT_LIST_HEAD(&mddev->disks);
497 INIT_LIST_HEAD(&mddev->all_mddevs);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400498 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
499 (unsigned long) mddev);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100500 atomic_set(&mddev->active, 1);
501 atomic_set(&mddev->openers, 0);
502 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100503 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100504 atomic_set(&mddev->flush_pending, 0);
505 init_waitqueue_head(&mddev->sb_wait);
506 init_waitqueue_head(&mddev->recovery_wait);
507 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000508 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500509 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100510 mddev->resync_min = 0;
511 mddev->resync_max = MaxSector;
512 mddev->level = LEVEL_NONE;
513}
NeilBrown390ee602010-06-01 19:37:27 +1000514EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100515
NeilBrownf72ffdd2014-09-30 14:23:59 +1000516static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
NeilBrownfd01b882011-10-11 16:47:53 +1100518 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
NeilBrown8f5f02c2011-02-16 13:58:51 +1100520 if (unit && MAJOR(unit) != MD_MAJOR)
521 unit &= ~((1<<MdpMinorShift)-1);
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 retry:
524 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
NeilBrownefeb53c2009-01-09 08:31:10 +1100526 if (unit) {
527 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
528 if (mddev->unit == unit) {
529 mddev_get(mddev);
530 spin_unlock(&all_mddevs_lock);
531 kfree(new);
532 return mddev;
533 }
534
535 if (new) {
536 list_add(&new->all_mddevs, &all_mddevs);
537 spin_unlock(&all_mddevs_lock);
538 new->hold_active = UNTIL_IOCTL;
539 return new;
540 }
541 } else if (new) {
542 /* find an unused unit number */
543 static int next_minor = 512;
544 int start = next_minor;
545 int is_free = 0;
546 int dev = 0;
547 while (!is_free) {
548 dev = MKDEV(MD_MAJOR, next_minor);
549 next_minor++;
550 if (next_minor > MINORMASK)
551 next_minor = 0;
552 if (next_minor == start) {
553 /* Oh dear, all in use. */
554 spin_unlock(&all_mddevs_lock);
555 kfree(new);
556 return NULL;
557 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000558
NeilBrownefeb53c2009-01-09 08:31:10 +1100559 is_free = 1;
560 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
561 if (mddev->unit == dev) {
562 is_free = 0;
563 break;
564 }
565 }
566 new->unit = dev;
567 new->md_minor = MINOR(dev);
568 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 list_add(&new->all_mddevs, &all_mddevs);
570 spin_unlock(&all_mddevs_lock);
571 return new;
572 }
573 spin_unlock(&all_mddevs_lock);
574
NeilBrown9ffae0c2006-01-06 00:20:32 -0800575 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (!new)
577 return NULL;
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 new->unit = unit;
580 if (MAJOR(unit) == MD_MAJOR)
581 new->md_minor = MINOR(unit);
582 else
583 new->md_minor = MINOR(unit) >> MdpMinorShift;
584
NeilBrownfafd7fb2010-04-01 15:55:30 +1100585 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 goto retry;
588}
589
NeilBrownb6eb1272010-04-15 10:13:47 +1000590static struct attribute_group md_redundancy_group;
591
NeilBrown5c47daf2014-12-15 12:57:01 +1100592void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
NeilBrowna64c8762010-04-14 17:15:37 +1000594 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000595 /* These cannot be removed under reconfig_mutex as
596 * an access to the files will try to take reconfig_mutex
597 * while holding the file unremovable, which leads to
598 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000599 * So hold set sysfs_active while the remove in happeing,
600 * and anything else which might set ->to_remove or my
601 * otherwise change the sysfs namespace will fail with
602 * -EBUSY if sysfs_active is still set.
603 * We set sysfs_active under reconfig_mutex and elsewhere
604 * test it under the same mutex to ensure its correct value
605 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000606 */
NeilBrowna64c8762010-04-14 17:15:37 +1000607 struct attribute_group *to_remove = mddev->to_remove;
608 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000609 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000610 mutex_unlock(&mddev->reconfig_mutex);
611
NeilBrown00bcb4a2010-06-01 19:37:23 +1000612 if (mddev->kobj.sd) {
613 if (to_remove != &md_redundancy_group)
614 sysfs_remove_group(&mddev->kobj, to_remove);
615 if (mddev->pers == NULL ||
616 mddev->pers->sync_request == NULL) {
617 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
618 if (mddev->sysfs_action)
619 sysfs_put(mddev->sysfs_action);
620 mddev->sysfs_action = NULL;
621 }
NeilBrowna64c8762010-04-14 17:15:37 +1000622 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000623 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000624 } else
625 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Chris Dunlop751e67c2011-10-19 16:48:26 +1100627 /* As we've dropped the mutex we need a spinlock to
628 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000629 */
630 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700631 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000632 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
NeilBrown5c47daf2014-12-15 12:57:01 +1100634EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500636struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100637{
638 struct md_rdev *rdev;
639
640 rdev_for_each_rcu(rdev, mddev)
641 if (rdev->desc_nr == nr)
642 return rdev;
643
644 return NULL;
645}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500646EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100647
648static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
NeilBrown3cb03002011-10-11 16:45:26 +1100650 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
NeilBrowndafb20f2012-03-19 12:46:39 +1100652 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (rdev->bdev->bd_dev == dev)
654 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return NULL;
657}
658
NeilBrown1ca69c42012-10-11 13:37:33 +1100659static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
660{
661 struct md_rdev *rdev;
662
663 rdev_for_each_rcu(rdev, mddev)
664 if (rdev->bdev->bd_dev == dev)
665 return rdev;
666
667 return NULL;
668}
669
NeilBrown84fc4b52011-10-11 16:49:58 +1100670static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800671{
NeilBrown84fc4b52011-10-11 16:49:58 +1100672 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800673 list_for_each_entry(pers, &pers_list, list) {
674 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800675 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800676 if (strcmp(pers->name, clevel)==0)
677 return pers;
678 }
NeilBrown2604b702006-01-06 00:20:36 -0800679 return NULL;
680}
681
Andre Nollb73df2d2008-07-11 22:02:23 +1000682/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100683static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100685 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000686 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
NeilBrownf72ffdd2014-09-30 14:23:59 +1000689static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 rdev->sb_page = alloc_page(GFP_KERNEL);
692 if (!rdev->sb_page) {
693 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000694 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
697 return 0;
698}
699
NeilBrown545c8792012-05-22 13:54:30 +1000700void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800703 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 rdev->sb_loaded = 0;
705 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000706 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100707 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
NeilBrown2699b672011-07-28 11:31:47 +1000709 if (rdev->bb_page) {
710 put_page(rdev->bb_page);
711 rdev->bb_page = NULL;
712 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000713 kfree(rdev->badblocks.page);
714 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
NeilBrown545c8792012-05-22 13:54:30 +1000716EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200718static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700719{
NeilBrown3cb03002011-10-11 16:45:26 +1100720 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100721 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700722
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200723 if (bio->bi_error) {
724 printk("md: super_written gets error=%d\n", bio->bi_error);
NeilBrowna9701a32005-11-08 21:39:34 -0800725 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700726 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700727
NeilBrowna9701a32005-11-08 21:39:34 -0800728 if (atomic_dec_and_test(&mddev->pending_writes))
729 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700730 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700731}
732
NeilBrownfd01b882011-10-11 16:47:53 +1100733void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700734 sector_t sector, int size, struct page *page)
735{
736 /* write first size bytes of page to sector of rdev
737 * Increment mddev->pending_writes before returning
738 * and decrement it on completion, waking up sb_wait
739 * if zero is reached.
740 * If an error occurred, call md_error
741 */
NeilBrowna167f662010-10-26 18:31:13 +1100742 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700743
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100744 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700745 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700746 bio_add_page(bio, page, size, 0);
747 bio->bi_private = rdev;
748 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800749
NeilBrown7bfa19f2005-06-21 17:17:28 -0700750 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000751 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800752}
753
NeilBrownfd01b882011-10-11 16:47:53 +1100754void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800755{
Tejun Heoe9c74692010-09-03 11:56:18 +0200756 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000757 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700758}
759
NeilBrown3cb03002011-10-11 16:45:26 +1100760int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100761 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
NeilBrowna167f662010-10-26 18:31:13 +1100763 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 int ret;
765
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100766 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
767 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100768 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700769 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000770 else if (rdev->mddev->reshape_position != MaxSector &&
771 (rdev->mddev->reshape_backwards ==
772 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700773 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100774 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700775 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700777 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200779 ret = !bio->bi_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 bio_put(bio);
781 return ret;
782}
NeilBrowna8745db2006-01-06 00:20:34 -0800783EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
NeilBrownf72ffdd2014-09-30 14:23:59 +1000785static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (rdev->sb_loaded)
790 return 0;
791
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100792 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 goto fail;
794 rdev->sb_loaded = 1;
795 return 0;
796
797fail:
798 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
799 bdevname(rdev->bdev,b));
800 return -EINVAL;
801}
802
803static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
804{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000805 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000806 sb1->set_uuid1 == sb2->set_uuid1 &&
807 sb1->set_uuid2 == sb2->set_uuid2 &&
808 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
812{
813 int ret;
814 mdp_super_t *tmp1, *tmp2;
815
816 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
817 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
818
819 if (!tmp1 || !tmp2) {
820 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100821 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 goto abort;
823 }
824
825 *tmp1 = *sb1;
826 *tmp2 = *sb2;
827
828 /*
829 * nr_disks is not constant
830 */
831 tmp1->nr_disks = 0;
832 tmp2->nr_disks = 0;
833
Andre Nollce0c8e02008-07-11 22:02:20 +1000834 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700836 kfree(tmp1);
837 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return ret;
839}
840
NeilBrown4d167f02007-05-09 02:35:37 -0700841static u32 md_csum_fold(u32 csum)
842{
843 csum = (csum & 0xffff) + (csum >> 16);
844 return (csum & 0xffff) + (csum >> 16);
845}
846
NeilBrownf72ffdd2014-09-30 14:23:59 +1000847static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
NeilBrown4d167f02007-05-09 02:35:37 -0700849 u64 newcsum = 0;
850 u32 *sb32 = (u32*)sb;
851 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 unsigned int disk_csum, csum;
853
854 disk_csum = sb->sb_csum;
855 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700856
857 for (i = 0; i < MD_SB_BYTES/4 ; i++)
858 newcsum += sb32[i];
859 csum = (newcsum & 0xffffffff) + (newcsum>>32);
860
NeilBrown4d167f02007-05-09 02:35:37 -0700861#ifdef CONFIG_ALPHA
862 /* This used to use csum_partial, which was wrong for several
863 * reasons including that different results are returned on
864 * different architectures. It isn't critical that we get exactly
865 * the same return value as before (we always csum_fold before
866 * testing, and that removes any differences). However as we
867 * know that csum_partial always returned a 16bit value on
868 * alphas, do a fold to maximise conformity to previous behaviour.
869 */
870 sb->sb_csum = md_csum_fold(disk_csum);
871#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700873#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return csum;
875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877/*
878 * Handle superblock details.
879 * We want to be able to handle multiple superblock formats
880 * so we have a common interface to them all, and an array of
881 * different handlers.
882 * We rely on user-space to write the initial superblock, and support
883 * reading and updating of superblocks.
884 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100885 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * loads and validates a superblock on dev.
887 * if refdev != NULL, compare superblocks on both devices
888 * Return:
889 * 0 - dev has a superblock that is compatible with refdev
890 * 1 - dev has a superblock that is compatible and newer than refdev
891 * so dev should be used as the refdev in future
892 * -EINVAL superblock incompatible or invalid
893 * -othererror e.g. -EIO
894 *
NeilBrownfd01b882011-10-11 16:47:53 +1100895 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * Verify that dev is acceptable into mddev.
897 * The first time, mddev->raid_disks will be 0, and data from
898 * dev should be merged in. Subsequent calls check that dev
899 * is new enough. Return 0 or -EINVAL
900 *
NeilBrownfd01b882011-10-11 16:47:53 +1100901 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 * Update the superblock for rdev with data in mddev
903 * This does not write to disc.
904 *
905 */
906
907struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000908 char *name;
909 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000910 int (*load_super)(struct md_rdev *rdev,
911 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000912 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000913 int (*validate_super)(struct mddev *mddev,
914 struct md_rdev *rdev);
915 void (*sync_super)(struct mddev *mddev,
916 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100917 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000918 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000919 int (*allow_new_offset)(struct md_rdev *rdev,
920 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921};
922
923/*
Andre Noll0894cc32009-06-18 08:49:23 +1000924 * Check that the given mddev has no bitmap.
925 *
926 * This function is called from the run method of all personalities that do not
927 * support bitmaps. It prints an error message and returns non-zero if mddev
928 * has a bitmap. Otherwise, it returns 0.
929 *
930 */
NeilBrownfd01b882011-10-11 16:47:53 +1100931int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000932{
NeilBrownc3d97142009-12-14 12:49:52 +1100933 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000934 return 0;
935 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
936 mdname(mddev), mddev->pers->name);
937 return 1;
938}
939EXPORT_SYMBOL(md_check_no_bitmap);
940
941/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000942 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 */
NeilBrown3cb03002011-10-11 16:45:26 +1100944static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
947 mdp_super_t *sb;
948 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 /*
Andre Noll0f420352008-07-11 22:02:23 +1000951 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 * it's at the end of the disk.
953 *
954 * It also happens to be a multiple of 4Kb.
955 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100956 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
NeilBrown0002b272005-09-09 16:23:53 -0700958 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (ret) return ret;
960
961 ret = -EINVAL;
962
963 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000964 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (sb->md_magic != MD_SB_MAGIC) {
967 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
968 b);
969 goto abort;
970 }
971
972 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800973 sb->minor_version < 90 ||
974 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
976 sb->major_version, sb->minor_version,
977 b);
978 goto abort;
979 }
980
981 if (sb->raid_disks <= 0)
982 goto abort;
983
NeilBrown4d167f02007-05-09 02:35:37 -0700984 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
986 b);
987 goto abort;
988 }
989
990 rdev->preferred_minor = sb->md_minor;
991 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +1000992 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700993 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +1000994 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 if (sb->level == LEVEL_MULTIPATH)
997 rdev->desc_nr = -1;
998 else
999 rdev->desc_nr = sb->this_disk.number;
1000
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001001 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001003 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001005 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (!uuid_equal(refsb, sb)) {
1007 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1008 b, bdevname(refdev->bdev,b2));
1009 goto abort;
1010 }
1011 if (!sb_equal(refsb, sb)) {
1012 printk(KERN_WARNING "md: %s has same UUID"
1013 " but different superblock to %s\n",
1014 b, bdevname(refdev->bdev, b2));
1015 goto abort;
1016 }
1017 ev1 = md_event(sb);
1018 ev2 = md_event(refsb);
1019 if (ev1 > ev2)
1020 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001021 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 ret = 0;
1023 }
NeilBrown8190e752009-06-18 08:48:58 +10001024 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001025 /* Limit to 4TB as metadata cannot record more than that.
1026 * (not needed for Linear and RAID0 as metadata doesn't
1027 * record this size)
1028 */
1029 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001030 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
NeilBrown27a7b262011-09-10 17:21:28 +10001032 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001033 /* "this cannot possibly happen" ... */
1034 ret = -EINVAL;
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 abort:
1037 return ret;
1038}
1039
1040/*
1041 * validate_super for 0.90.0
1042 */
NeilBrownfd01b882011-10-11 16:47:53 +11001043static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001046 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001047 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
NeilBrown41158c72005-06-21 17:17:25 -07001049 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001050 clear_bit(Faulty, &rdev->flags);
1051 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001052 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001053 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (mddev->raid_disks == 0) {
1056 mddev->major_version = 0;
1057 mddev->minor_version = sb->minor_version;
1058 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001059 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001060 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 mddev->ctime = sb->ctime;
1062 mddev->utime = sb->utime;
1063 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001064 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 mddev->layout = sb->layout;
1066 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001067 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001068 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001069 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001070 mddev->bitmap_info.space = 0;
1071 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001072 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001073 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001074 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
NeilBrownf6705572006-03-27 01:18:11 -08001076 if (mddev->minor_version >= 91) {
1077 mddev->reshape_position = sb->reshape_position;
1078 mddev->delta_disks = sb->delta_disks;
1079 mddev->new_level = sb->new_level;
1080 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001081 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001082 if (mddev->delta_disks < 0)
1083 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001084 } else {
1085 mddev->reshape_position = MaxSector;
1086 mddev->delta_disks = 0;
1087 mddev->new_level = mddev->level;
1088 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001089 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001090 }
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (sb->state & (1<<MD_SB_CLEAN))
1093 mddev->recovery_cp = MaxSector;
1094 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001095 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 sb->events_lo == sb->cp_events_lo) {
1097 mddev->recovery_cp = sb->recovery_cp;
1098 } else
1099 mddev->recovery_cp = 0;
1100 }
1101
1102 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1103 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1104 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1105 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1106
1107 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001108
1109 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001110 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001111 mddev->bitmap_info.offset =
1112 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001113 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001114 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001115 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001116
NeilBrown41158c72005-06-21 17:17:25 -07001117 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001118 /* Insist on good event counter while assembling, except
1119 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001121 if (sb->disks[rdev->desc_nr].state & (
1122 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001123 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001124 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001125 } else if (mddev->bitmap) {
1126 /* if adding to array with a bitmap, then we can accept an
1127 * older device ... but not too old.
1128 */
NeilBrown41158c72005-06-21 17:17:25 -07001129 if (ev1 < mddev->bitmap->events_cleared)
1130 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001131 if (ev1 < mddev->events)
1132 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001133 } else {
1134 if (ev1 < mddev->events)
1135 /* just a hot-add of a new device, leave raid_disk at -1 */
1136 return 0;
1137 }
NeilBrown41158c72005-06-21 17:17:25 -07001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 desc = sb->disks + rdev->desc_nr;
1141
1142 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001143 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001144 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1145 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001146 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001148 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001149 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1150 /* active but not in sync implies recovery up to
1151 * reshape position. We don't know exactly where
1152 * that is, so set to zero for now */
1153 if (mddev->minor_version >= 91) {
1154 rdev->recovery_offset = 0;
1155 rdev->raid_disk = desc->raid_disk;
1156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001158 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1159 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001160 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001161 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return 0;
1163}
1164
1165/*
1166 * sync_super for 0.90.0
1167 */
NeilBrownfd01b882011-10-11 16:47:53 +11001168static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001171 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /* make rdev->sb match mddev data..
1175 *
1176 * 1/ zero out disks
1177 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1178 * 3/ any empty disks < next_spare become removed
1179 *
1180 * disks[0] gets initialised to REMOVED because
1181 * we cannot be sure from other fields if it has
1182 * been initialised or not.
1183 */
1184 int i;
1185 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1186
NeilBrown61181562005-09-09 16:24:02 -07001187 rdev->sb_size = MD_SB_BYTES;
1188
Namhyung Kim65a06f062011-07-27 11:00:36 +10001189 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 memset(sb, 0, sizeof(*sb));
1192
1193 sb->md_magic = MD_SB_MAGIC;
1194 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 sb->patch_version = mddev->patch_version;
1196 sb->gvalid_words = 0; /* ignored */
1197 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1198 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1199 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1200 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1201
1202 sb->ctime = mddev->ctime;
1203 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001204 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 sb->raid_disks = mddev->raid_disks;
1206 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001207 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 sb->utime = mddev->utime;
1209 sb->state = 0;
1210 sb->events_hi = (mddev->events>>32);
1211 sb->events_lo = (u32)mddev->events;
1212
NeilBrownf6705572006-03-27 01:18:11 -08001213 if (mddev->reshape_position == MaxSector)
1214 sb->minor_version = 90;
1215 else {
1216 sb->minor_version = 91;
1217 sb->reshape_position = mddev->reshape_position;
1218 sb->new_level = mddev->new_level;
1219 sb->delta_disks = mddev->delta_disks;
1220 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001221 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001222 }
1223 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (mddev->in_sync)
1225 {
1226 sb->recovery_cp = mddev->recovery_cp;
1227 sb->cp_events_hi = (mddev->events>>32);
1228 sb->cp_events_lo = (u32)mddev->events;
1229 if (mddev->recovery_cp == MaxSector)
1230 sb->state = (1<< MD_SB_CLEAN);
1231 } else
1232 sb->recovery_cp = 0;
1233
1234 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001235 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
NeilBrownc3d97142009-12-14 12:49:52 +11001237 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001238 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001241 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001243 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001244 int is_active = test_bit(In_sync, &rdev2->flags);
1245
1246 if (rdev2->raid_disk >= 0 &&
1247 sb->minor_version >= 91)
1248 /* we have nowhere to store the recovery_offset,
1249 * but if it is not below the reshape_position,
1250 * we can piggy-back on that.
1251 */
1252 is_active = 1;
1253 if (rdev2->raid_disk < 0 ||
1254 test_bit(Faulty, &rdev2->flags))
1255 is_active = 0;
1256 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001257 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001259 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001260 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 d = &sb->disks[rdev2->desc_nr];
1262 nr_disks++;
1263 d->number = rdev2->desc_nr;
1264 d->major = MAJOR(rdev2->bdev->bd_dev);
1265 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001266 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 d->raid_disk = rdev2->raid_disk;
1268 else
1269 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001270 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001272 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001274 if (test_bit(In_sync, &rdev2->flags))
1275 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 active++;
1277 working++;
1278 } else {
1279 d->state = 0;
1280 spare++;
1281 working++;
1282 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001283 if (test_bit(WriteMostly, &rdev2->flags))
1284 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 /* now set the "removed" and "faulty" bits on any missing devices */
1287 for (i=0 ; i < mddev->raid_disks ; i++) {
1288 mdp_disk_t *d = &sb->disks[i];
1289 if (d->state == 0 && d->number == 0) {
1290 d->number = i;
1291 d->raid_disk = i;
1292 d->state = (1<<MD_DISK_REMOVED);
1293 d->state |= (1<<MD_DISK_FAULTY);
1294 failed++;
1295 }
1296 }
1297 sb->nr_disks = nr_disks;
1298 sb->active_disks = active;
1299 sb->working_disks = working;
1300 sb->failed_disks = failed;
1301 sb->spare_disks = spare;
1302
1303 sb->this_disk = sb->disks[rdev->desc_nr];
1304 sb->sb_csum = calc_sb_csum(sb);
1305}
1306
1307/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001308 * rdev_size_change for 0.90.0
1309 */
1310static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001311super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001312{
Andre Noll58c0fed2009-03-31 14:33:13 +11001313 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001314 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001315 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001316 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001317 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001318 if (!num_sectors || num_sectors > rdev->sb_start)
1319 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001320 /* Limit to 4TB as metadata cannot record more than that.
1321 * 4TB == 2^32 KB, or 2*2^32 sectors.
1322 */
NeilBrown667a5312012-08-16 16:46:12 +10001323 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001324 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001325 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001326 rdev->sb_page);
1327 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001328 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001329}
1330
NeilBrownc6563a82012-05-21 09:27:00 +10001331static int
1332super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1333{
1334 /* non-zero offset changes not possible with v0.90 */
1335 return new_offset == 0;
1336}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001337
1338/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 * version 1 superblock
1340 */
1341
NeilBrownf72ffdd2014-09-30 14:23:59 +10001342static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001344 __le32 disk_csum;
1345 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 unsigned long long newcsum;
1347 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001348 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 disk_csum = sb->sb_csum;
1351 sb->sb_csum = 0;
1352 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001353 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 newcsum += le32_to_cpu(*isuper++);
1355
1356 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001357 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1360 sb->sb_csum = disk_csum;
1361 return cpu_to_le32(csum);
1362}
1363
NeilBrown2699b672011-07-28 11:31:47 +10001364static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1365 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001366static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 struct mdp_superblock_1 *sb;
1369 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001370 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001371 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001373 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 /*
Andre Noll0f420352008-07-11 22:02:23 +10001376 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 * It is always aligned to a 4K boundary and
1378 * depeding on minor_version, it can be:
1379 * 0: At least 8K, but less than 12K, from end of device
1380 * 1: At start of device
1381 * 2: 4K from start of device.
1382 */
1383 switch(minor_version) {
1384 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001385 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001386 sb_start -= 8*2;
1387 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 break;
1389 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001390 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 break;
1392 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001393 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 break;
1395 default:
1396 return -EINVAL;
1397 }
Andre Noll0f420352008-07-11 22:02:23 +10001398 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
NeilBrown0002b272005-09-09 16:23:53 -07001400 /* superblock is rarely larger than 1K, but it can be larger,
1401 * and it is safe to read 4k, so we do that
1402 */
1403 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (ret) return ret;
1405
Namhyung Kim65a06f062011-07-27 11:00:36 +10001406 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1409 sb->major_version != cpu_to_le32(1) ||
1410 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001411 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001412 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return -EINVAL;
1414
1415 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1416 printk("md: invalid superblock checksum on %s\n",
1417 bdevname(rdev->bdev,b));
1418 return -EINVAL;
1419 }
1420 if (le64_to_cpu(sb->data_size) < 10) {
1421 printk("md: data_size too small on %s\n",
1422 bdevname(rdev->bdev,b));
1423 return -EINVAL;
1424 }
NeilBrownc6563a82012-05-21 09:27:00 +10001425 if (sb->pad0 ||
1426 sb->pad3[0] ||
1427 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1428 /* Some padding is non-zero, might be a new feature */
1429 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 rdev->preferred_minor = 0xffff;
1432 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001433 rdev->new_data_offset = rdev->data_offset;
1434 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1435 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1436 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001437 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
NeilBrown0002b272005-09-09 16:23:53 -07001439 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001440 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001441 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001442 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1443
1444 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001445 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001446 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001447 if (minor_version
1448 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1449 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001450
NeilBrown31b65a02006-07-10 04:44:14 -07001451 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1452 rdev->desc_nr = -1;
1453 else
1454 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1455
NeilBrown2699b672011-07-28 11:31:47 +10001456 if (!rdev->bb_page) {
1457 rdev->bb_page = alloc_page(GFP_KERNEL);
1458 if (!rdev->bb_page)
1459 return -ENOMEM;
1460 }
1461 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1462 rdev->badblocks.count == 0) {
1463 /* need to load the bad block list.
1464 * Currently we limit it to one page.
1465 */
1466 s32 offset;
1467 sector_t bb_sector;
1468 u64 *bbp;
1469 int i;
1470 int sectors = le16_to_cpu(sb->bblog_size);
1471 if (sectors > (PAGE_SIZE / 512))
1472 return -EINVAL;
1473 offset = le32_to_cpu(sb->bblog_offset);
1474 if (offset == 0)
1475 return -EINVAL;
1476 bb_sector = (long long)offset;
1477 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1478 rdev->bb_page, READ, true))
1479 return -EIO;
1480 bbp = (u64 *)page_address(rdev->bb_page);
1481 rdev->badblocks.shift = sb->bblog_shift;
1482 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1483 u64 bb = le64_to_cpu(*bbp);
1484 int count = bb & (0x3ff);
1485 u64 sector = bb >> 10;
1486 sector <<= sb->bblog_shift;
1487 count <<= sb->bblog_shift;
1488 if (bb + 1 == 0)
1489 break;
1490 if (md_set_badblocks(&rdev->badblocks,
1491 sector, count, 1) == 0)
1492 return -EINVAL;
1493 }
NeilBrown486adf72013-04-24 11:42:44 +10001494 } else if (sb->bblog_offset != 0)
1495 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001496
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001497 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001498 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001499 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001501 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1504 sb->level != refsb->level ||
1505 sb->layout != refsb->layout ||
1506 sb->chunksize != refsb->chunksize) {
1507 printk(KERN_WARNING "md: %s has strangely different"
1508 " superblock to %s\n",
1509 bdevname(rdev->bdev,b),
1510 bdevname(refdev->bdev,b2));
1511 return -EINVAL;
1512 }
1513 ev1 = le64_to_cpu(sb->events);
1514 ev2 = le64_to_cpu(refsb->events);
1515
1516 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001517 ret = 1;
1518 else
1519 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
NeilBrownc6563a82012-05-21 09:27:00 +10001521 if (minor_version) {
1522 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1523 sectors -= rdev->data_offset;
1524 } else
1525 sectors = rdev->sb_start;
1526 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001528 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001529 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
NeilBrownfd01b882011-10-11 16:47:53 +11001532static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001534 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001535 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
NeilBrown41158c72005-06-21 17:17:25 -07001537 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001538 clear_bit(Faulty, &rdev->flags);
1539 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001540 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001541 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (mddev->raid_disks == 0) {
1544 mddev->major_version = 1;
1545 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001546 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001547 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1549 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1550 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001551 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 mddev->layout = le32_to_cpu(sb->layout);
1553 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001554 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001555 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001556 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001557 mddev->bitmap_info.space = 0;
1558 /* Default location for bitmap is 1K after superblock
1559 * using 3K - total of 4K
1560 */
NeilBrownc3d97142009-12-14 12:49:52 +11001561 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001562 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001563 mddev->reshape_backwards = 0;
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1566 memcpy(mddev->uuid, sb->set_uuid, 16);
1567
1568 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001569
NeilBrown71c08052005-09-09 16:23:51 -07001570 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001571 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001572 mddev->bitmap_info.offset =
1573 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001574 /* Metadata doesn't record how much space is available.
1575 * For 1.0, we assume we can use up to the superblock
1576 * if before, else to 4K beyond superblock.
1577 * For others, assume no change is possible.
1578 */
1579 if (mddev->minor_version > 0)
1580 mddev->bitmap_info.space = 0;
1581 else if (mddev->bitmap_info.offset > 0)
1582 mddev->bitmap_info.space =
1583 8 - mddev->bitmap_info.offset;
1584 else
1585 mddev->bitmap_info.space =
1586 -mddev->bitmap_info.offset;
1587 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001588
NeilBrownf6705572006-03-27 01:18:11 -08001589 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1590 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1591 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1592 mddev->new_level = le32_to_cpu(sb->new_level);
1593 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001594 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001595 if (mddev->delta_disks < 0 ||
1596 (mddev->delta_disks == 0 &&
1597 (le32_to_cpu(sb->feature_map)
1598 & MD_FEATURE_RESHAPE_BACKWARDS)))
1599 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001600 } else {
1601 mddev->reshape_position = MaxSector;
1602 mddev->delta_disks = 0;
1603 mddev->new_level = mddev->level;
1604 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001605 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001606 }
1607
NeilBrown41158c72005-06-21 17:17:25 -07001608 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001609 /* Insist of good event counter while assembling, except for
1610 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001612 if (rdev->desc_nr >= 0 &&
1613 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001614 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1615 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001616 if (ev1 < mddev->events)
1617 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001618 } else if (mddev->bitmap) {
1619 /* If adding to array with a bitmap, then we can accept an
1620 * older device, but not too old.
1621 */
NeilBrown41158c72005-06-21 17:17:25 -07001622 if (ev1 < mddev->bitmap->events_cleared)
1623 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001624 if (ev1 < mddev->events)
1625 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001626 } else {
1627 if (ev1 < mddev->events)
1628 /* just a hot-add of a new device, leave raid_disk at -1 */
1629 return 0;
1630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (mddev->level != LEVEL_MULTIPATH) {
1632 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001633 if (rdev->desc_nr < 0 ||
1634 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001635 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001636 rdev->desc_nr = -1;
1637 } else
1638 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001640 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001642 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001643 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 break;
Song Liubac624f2015-08-13 14:31:55 -07001645 case MD_DISK_ROLE_JOURNAL: /* journal device */
1646 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1647 /* journal device without journal feature */
1648 printk(KERN_WARNING
1649 "md: journal device provided without journal feature, ignoring the device\n");
1650 return -EINVAL;
1651 }
1652 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001653 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Libd18f642015-09-02 13:49:50 -07001654 if (mddev->recovery_cp == MaxSector)
1655 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
Shaohua Li9b156032015-12-18 15:19:16 +11001656 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001657 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 default:
NeilBrownf4667222013-12-09 12:04:56 +11001659 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001660 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001661 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001662 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001663 if (!(le32_to_cpu(sb->feature_map) &
1664 MD_FEATURE_RECOVERY_BITMAP))
1665 rdev->saved_raid_disk = -1;
1666 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001667 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 rdev->raid_disk = role;
1669 break;
1670 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001671 if (sb->devflags & WriteMostly1)
1672 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001673 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1674 set_bit(Replacement, &rdev->flags);
Song Liua97b7892015-10-08 21:54:09 -07001675 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
1676 set_bit(MD_HAS_JOURNAL, &mddev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001677 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001678 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return 0;
1681}
1682
NeilBrownfd01b882011-10-11 16:47:53 +11001683static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001686 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 int max_dev, i;
1688 /* make rdev->sb match mddev and rdev data. */
1689
Namhyung Kim65a06f062011-07-27 11:00:36 +10001690 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 sb->feature_map = 0;
1693 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001694 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 memset(sb->pad3, 0, sizeof(sb->pad3));
1696
1697 sb->utime = cpu_to_le64((__u64)mddev->utime);
1698 sb->events = cpu_to_le64(mddev->events);
1699 if (mddev->in_sync)
1700 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001701 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1702 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 else
1704 sb->resync_offset = cpu_to_le64(0);
1705
NeilBrown1c05b4b2006-10-21 10:24:08 -07001706 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001707
NeilBrownf0ca3402006-02-02 14:28:04 -08001708 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001709 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001710 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001711 sb->level = cpu_to_le32(mddev->level);
1712 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001713
NeilBrownaeb9b212011-08-25 14:43:08 +10001714 if (test_bit(WriteMostly, &rdev->flags))
1715 sb->devflags |= WriteMostly1;
1716 else
1717 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001718 sb->data_offset = cpu_to_le64(rdev->data_offset);
1719 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001720
NeilBrownc3d97142009-12-14 12:49:52 +11001721 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1722 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001723 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001724 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001725
Shaohua Lif2076e72015-10-08 21:54:12 -07001726 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001727 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001728 sb->feature_map |=
1729 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1730 sb->recovery_offset =
1731 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001732 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1733 sb->feature_map |=
1734 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001735 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001736 /* Note: recovery_offset and journal_tail share space */
1737 if (test_bit(Journal, &rdev->flags))
1738 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001739 if (test_bit(Replacement, &rdev->flags))
1740 sb->feature_map |=
1741 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001742
NeilBrownf6705572006-03-27 01:18:11 -08001743 if (mddev->reshape_position != MaxSector) {
1744 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1745 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1746 sb->new_layout = cpu_to_le32(mddev->new_layout);
1747 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1748 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001749 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001750 if (mddev->delta_disks == 0 &&
1751 mddev->reshape_backwards)
1752 sb->feature_map
1753 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001754 if (rdev->new_data_offset != rdev->data_offset) {
1755 sb->feature_map
1756 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1757 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1758 - rdev->data_offset));
1759 }
NeilBrownf6705572006-03-27 01:18:11 -08001760 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001761
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001762 if (mddev_is_clustered(mddev))
1763 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1764
NeilBrown2699b672011-07-28 11:31:47 +10001765 if (rdev->badblocks.count == 0)
1766 /* Nothing to do for bad blocks*/ ;
1767 else if (sb->bblog_offset == 0)
1768 /* Cannot record bad blocks on this device */
1769 md_error(mddev, rdev);
1770 else {
1771 struct badblocks *bb = &rdev->badblocks;
1772 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1773 u64 *p = bb->page;
1774 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1775 if (bb->changed) {
1776 unsigned seq;
1777
1778retry:
1779 seq = read_seqbegin(&bb->lock);
1780
1781 memset(bbp, 0xff, PAGE_SIZE);
1782
1783 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001784 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001785 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1786 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001787 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001788 }
NeilBrownd0962932012-03-19 12:46:41 +11001789 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001790 if (read_seqretry(&bb->lock, seq))
1791 goto retry;
1792
1793 bb->sector = (rdev->sb_start +
1794 (int)le32_to_cpu(sb->bblog_offset));
1795 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001796 }
1797 }
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001800 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (rdev2->desc_nr+1 > max_dev)
1802 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001803
NeilBrown70471da2009-08-03 10:59:57 +10001804 if (max_dev > le32_to_cpu(sb->max_dev)) {
1805 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001806 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001807 rdev->sb_size = max_dev * 2 + 256;
1808 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1809 if (rdev->sb_size & bmask)
1810 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001811 } else
1812 max_dev = le32_to_cpu(sb->max_dev);
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 for (i=0; i<max_dev;i++)
Song Liuc4d4c912015-08-13 14:31:54 -07001815 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001816
Song Liua97b7892015-10-08 21:54:09 -07001817 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1818 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
NeilBrowndafb20f2012-03-19 12:46:39 +11001820 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001822 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001823 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001824 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001826 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001827 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001828 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001829 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 else
Song Liuc4d4c912015-08-13 14:31:54 -07001831 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 sb->sb_csum = calc_sb_1_csum(sb);
1835}
1836
Chris Webb0cd17fe2008-06-28 08:31:46 +10001837static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001838super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001839{
1840 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001841 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001842 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001843 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001844 if (rdev->data_offset != rdev->new_data_offset)
1845 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001846 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001847 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001848 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001849 max_sectors -= rdev->data_offset;
1850 if (!num_sectors || num_sectors > max_sectors)
1851 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001852 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001853 /* minor version 0 with bitmap we can't move */
1854 return 0;
1855 } else {
1856 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001857 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001858 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001859 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001860 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001861 if (!num_sectors || num_sectors > max_sectors)
1862 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001863 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001864 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001865 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001866 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001867 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001868 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001869 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001870 rdev->sb_page);
1871 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001872 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001873
1874}
1875
1876static int
1877super_1_allow_new_offset(struct md_rdev *rdev,
1878 unsigned long long new_offset)
1879{
1880 /* All necessary checks on new >= old have been done */
1881 struct bitmap *bitmap;
1882 if (new_offset >= rdev->data_offset)
1883 return 1;
1884
1885 /* with 1.0 metadata, there is no metadata to tread on
1886 * so we can always move back */
1887 if (rdev->mddev->minor_version == 0)
1888 return 1;
1889
1890 /* otherwise we must be sure not to step on
1891 * any metadata, so stay:
1892 * 36K beyond start of superblock
1893 * beyond end of badblocks
1894 * beyond write-intent bitmap
1895 */
1896 if (rdev->sb_start + (32+4)*2 > new_offset)
1897 return 0;
1898 bitmap = rdev->mddev->bitmap;
1899 if (bitmap && !rdev->mddev->bitmap_info.file &&
1900 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001901 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001902 return 0;
1903 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1904 return 0;
1905
1906 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001907}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Adrian Bunk75c96f82005-05-05 16:16:09 -07001909static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 [0] = {
1911 .name = "0.90.0",
1912 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001913 .load_super = super_90_load,
1914 .validate_super = super_90_validate,
1915 .sync_super = super_90_sync,
1916 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001917 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 },
1919 [1] = {
1920 .name = "md-1",
1921 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001922 .load_super = super_1_load,
1923 .validate_super = super_1_validate,
1924 .sync_super = super_1_sync,
1925 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001926 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 },
1928};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
NeilBrownfd01b882011-10-11 16:47:53 +11001930static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001931{
1932 if (mddev->sync_super) {
1933 mddev->sync_super(mddev, rdev);
1934 return;
1935 }
1936
1937 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1938
1939 super_types[mddev->major_version].sync_super(mddev, rdev);
1940}
1941
NeilBrownfd01b882011-10-11 16:47:53 +11001942static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
NeilBrown3cb03002011-10-11 16:45:26 +11001944 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
NeilBrown4b809912008-07-21 17:05:25 +10001946 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07001947 rdev_for_each_rcu(rdev, mddev1) {
1948 if (test_bit(Faulty, &rdev->flags) ||
1949 test_bit(Journal, &rdev->flags) ||
1950 rdev->raid_disk == -1)
1951 continue;
1952 rdev_for_each_rcu(rdev2, mddev2) {
1953 if (test_bit(Faulty, &rdev2->flags) ||
1954 test_bit(Journal, &rdev2->flags) ||
1955 rdev2->raid_disk == -1)
1956 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001957 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001958 rdev2->bdev->bd_contains) {
1959 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001960 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001961 }
Song Liu0b020e82015-09-03 23:00:35 -07001962 }
1963 }
NeilBrown4b809912008-07-21 17:05:25 +10001964 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return 0;
1966}
1967
1968static LIST_HEAD(pending_raid_disks);
1969
Andre Nollac5e7112009-08-03 10:59:47 +10001970/*
1971 * Try to register data integrity profile for an mddev
1972 *
1973 * This is called when an array is started and after a disk has been kicked
1974 * from the array. It only succeeds if all working and active component devices
1975 * are integrity capable with matching profiles.
1976 */
NeilBrownfd01b882011-10-11 16:47:53 +11001977int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001978{
NeilBrown3cb03002011-10-11 16:45:26 +11001979 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001980
Andre Nollac5e7112009-08-03 10:59:47 +10001981 if (list_empty(&mddev->disks))
1982 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001983 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1984 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001985 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001986 /* skip spares and non-functional disks */
1987 if (test_bit(Faulty, &rdev->flags))
1988 continue;
1989 if (rdev->raid_disk < 0)
1990 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001991 if (!reference) {
1992 /* Use the first rdev as the reference */
1993 reference = rdev;
1994 continue;
1995 }
1996 /* does this rdev's profile match the reference profile? */
1997 if (blk_integrity_compare(reference->bdev->bd_disk,
1998 rdev->bdev->bd_disk) < 0)
1999 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002000 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002001 if (!reference || !bdev_get_integrity(reference->bdev))
2002 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002003 /*
2004 * All component devices are integrity capable and have matching
2005 * profiles, register the common profile for the md device.
2006 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002007 blk_integrity_register(mddev->gendisk,
2008 bdev_get_integrity(reference->bdev));
2009
Martin K. Petersena91a2782011-03-17 11:11:05 +01002010 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2011 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2012 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2013 mdname(mddev));
2014 return -EINVAL;
2015 }
Andre Nollac5e7112009-08-03 10:59:47 +10002016 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002017}
Andre Nollac5e7112009-08-03 10:59:47 +10002018EXPORT_SYMBOL(md_integrity_register);
2019
2020/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002021void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002022{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002023 struct blk_integrity *bi_rdev;
2024 struct blk_integrity *bi_mddev;
2025
2026 if (!mddev->gendisk)
2027 return;
2028
2029 bi_rdev = bdev_get_integrity(rdev->bdev);
2030 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002031
2032 if (!bi_mddev) /* nothing to do */
2033 return;
2034 if (rdev->raid_disk < 0) /* skip spares */
2035 return;
2036 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2037 rdev->bdev->bd_disk) >= 0)
2038 return;
Dan Williamsc7bfced2015-10-21 13:20:02 -04002039 WARN_ON_ONCE(!mddev->suspended);
Andre Nollac5e7112009-08-03 10:59:47 +10002040 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2041 blk_integrity_unregister(mddev->gendisk);
2042}
2043EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002044
NeilBrownf72ffdd2014-09-30 14:23:59 +10002045static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002047 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002048 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002049 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Dan Williams11e2ede2008-04-30 00:52:32 -07002051 /* prevent duplicates */
2052 if (find_rdev(mddev, rdev->bdev->bd_dev))
2053 return -EEXIST;
2054
Andre Nolldd8ac332009-03-31 14:33:13 +11002055 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2056 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2057 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002058 if (mddev->pers) {
2059 /* Cannot change size, so fail
2060 * If mddev->level <= 0, then we don't care
2061 * about aligning sizes (e.g. linear)
2062 */
2063 if (mddev->level > 0)
2064 return -ENOSPC;
2065 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002066 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 /* Verify rdev->desc_nr is unique.
2070 * If it is -1, assign a free number, else
2071 * check number is not in use
2072 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002073 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (rdev->desc_nr < 0) {
2075 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002076 if (mddev->pers)
2077 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002078 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 choice++;
2080 rdev->desc_nr = choice;
2081 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002082 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002083 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002087 rcu_read_unlock();
NeilBrownde01dfa2009-02-06 18:02:46 +11002088 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2089 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2090 mdname(mddev), mddev->max_disks);
2091 return -EBUSY;
2092 }
NeilBrown19133a42005-11-08 21:39:35 -08002093 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002094 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002097 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002098
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002099 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002100 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002101
Tejun Heo0762b8b2008-08-25 19:56:12 +09002102 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002103 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2104 /* failure here is OK */;
2105 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002106
NeilBrown4b809912008-07-21 17:05:25 +10002107 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002108 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002109
2110 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002111 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002114
2115 fail:
2116 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2117 b, mdname(mddev));
2118 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
NeilBrown177a99b2008-02-06 01:39:56 -08002121static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002122{
NeilBrown3cb03002011-10-11 16:45:26 +11002123 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002124 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002125 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002126}
2127
NeilBrownf72ffdd2014-09-30 14:23:59 +10002128static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002131
Tejun Heo49731ba2011-01-14 18:43:57 +01002132 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002133 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2135 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002136 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002137 sysfs_put(rdev->sysfs_state);
2138 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002139 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002140 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002141 * writing to 'remove' to "dev/state". We also need
2142 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002143 */
NeilBrown4b809912008-07-21 17:05:25 +10002144 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002145 INIT_WORK(&rdev->del_work, md_delayed_delete);
2146 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002147 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
2150/*
2151 * prevent the device from being mounted, repartitioned or
2152 * otherwise reused by a RAID array (or any other kernel
2153 * subsystem), by bd_claiming the device.
2154 */
NeilBrown3cb03002011-10-11 16:45:26 +11002155static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 int err = 0;
2158 struct block_device *bdev;
2159 char b[BDEVNAME_SIZE];
2160
Tejun Heod4d77622010-11-13 11:55:18 +01002161 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002162 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (IS_ERR(bdev)) {
2164 printk(KERN_ERR "md: could not open %s.\n",
2165 __bdevname(dev, b));
2166 return PTR_ERR(bdev);
2167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 rdev->bdev = bdev;
2169 return err;
2170}
2171
NeilBrown3cb03002011-10-11 16:45:26 +11002172static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 struct block_device *bdev = rdev->bdev;
2175 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002176 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
2179void md_autodetect_dev(dev_t dev);
2180
NeilBrownf72ffdd2014-09-30 14:23:59 +10002181static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
2183 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 printk(KERN_INFO "md: export_rdev(%s)\n",
2186 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002187 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002189 if (test_bit(AutoDetected, &rdev->flags))
2190 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191#endif
2192 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002193 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002196void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
2198 unbind_rdev_from_array(rdev);
2199 export_rdev(rdev);
2200}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002201EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
NeilBrownfd01b882011-10-11 16:47:53 +11002203static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
NeilBrown0638bb02014-09-25 17:43:47 +10002205 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
NeilBrown0638bb02014-09-25 17:43:47 +10002207 while (!list_empty(&mddev->disks)) {
2208 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2209 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002210 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 mddev->raid_disks = 0;
2213 mddev->major_version = 0;
2214}
2215
NeilBrownf72ffdd2014-09-30 14:23:59 +10002216static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
NeilBrown42543762006-06-26 00:27:57 -07002218 /* Update each superblock (in-memory image), but
2219 * if we are allowed to, skip spares which already
2220 * have the right event counter, or have one earlier
2221 * (which would mean they aren't being marked as dirty
2222 * with the rest of the array)
2223 */
NeilBrown3cb03002011-10-11 16:45:26 +11002224 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002225 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002226 if (rdev->sb_events == mddev->events ||
2227 (nospares &&
2228 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002229 rdev->sb_events+1 == mddev->events)) {
2230 /* Don't update this superblock */
2231 rdev->sb_loaded = 2;
2232 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002233 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002234 rdev->sb_loaded = 1;
2235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237}
2238
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002239static bool does_sb_need_changing(struct mddev *mddev)
2240{
2241 struct md_rdev *rdev;
2242 struct mdp_superblock_1 *sb;
2243 int role;
2244
2245 /* Find a good rdev */
2246 rdev_for_each(rdev, mddev)
2247 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2248 break;
2249
2250 /* No good device found. */
2251 if (!rdev)
2252 return false;
2253
2254 sb = page_address(rdev->sb_page);
2255 /* Check if a device has become faulty or a spare become active */
2256 rdev_for_each(rdev, mddev) {
2257 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2258 /* Device activated? */
2259 if (role == 0xffff && rdev->raid_disk >=0 &&
2260 !test_bit(Faulty, &rdev->flags))
2261 return true;
2262 /* Device turned faulty? */
2263 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2264 return true;
2265 }
2266
2267 /* Check if any mddev parameters have changed */
2268 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2269 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002270 (mddev->layout != le64_to_cpu(sb->layout)) ||
2271 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2272 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2273 return true;
2274
2275 return false;
2276}
2277
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002278void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
NeilBrown3cb03002011-10-11 16:45:26 +11002280 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002281 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002282 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002283 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002284 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
NeilBrownd87f0642013-04-24 11:42:40 +10002286 if (mddev->ro) {
2287 if (force_change)
2288 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2289 return;
2290 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002291
2292 if (mddev_is_clustered(mddev)) {
2293 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2294 force_change = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002295 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002296 /* Has someone else has updated the sb */
2297 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002298 if (ret == 0)
2299 md_cluster_ops->metadata_update_cancel(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002300 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2301 return;
2302 }
2303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002305 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002306 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002307 if (rdev->raid_disk >= 0 &&
2308 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002309 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002310 !test_bit(In_sync, &rdev->flags) &&
2311 mddev->curr_resync_completed > rdev->recovery_offset)
2312 rdev->recovery_offset = mddev->curr_resync_completed;
2313
NeilBrownf72ffdd2014-09-30 14:23:59 +10002314 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002315 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002316 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002317 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002318 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002319 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002320 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002321 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002322 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002323 md_ack_all_badblocks(&rdev->badblocks);
2324 md_error(mddev, rdev);
2325 }
2326 clear_bit(Blocked, &rdev->flags);
2327 clear_bit(BlockedBadBlocks, &rdev->flags);
2328 wake_up(&rdev->blocked_wait);
2329 }
2330 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002331 wake_up(&mddev->sb_wait);
2332 return;
2333 }
2334
NeilBrown85572d72014-12-15 12:56:56 +11002335 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002336
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002337 mddev->utime = get_seconds();
2338
NeilBrown850b2b42006-10-03 01:15:46 -07002339 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2340 force_change = 1;
2341 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2342 /* just a clean<-> dirty transition, possibly leave spares alone,
2343 * though if events isn't the right even/odd, we will have to do
2344 * spares after all
2345 */
2346 nospares = 1;
2347 if (force_change)
2348 nospares = 0;
2349 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002350 /* If the array is degraded, then skipping spares is both
2351 * dangerous and fairly pointless.
2352 * Dangerous because a device that was removed from the array
2353 * might have a event_count that still looks up-to-date,
2354 * so it can be re-added without a resync.
2355 * Pointless because if there are any spares to skip,
2356 * then a recovery will happen and soon that array won't
2357 * be degraded any more and the spare can go back to sleep then.
2358 */
NeilBrown850b2b42006-10-03 01:15:46 -07002359 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002360
NeilBrown06d91a52005-06-21 17:17:12 -07002361 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002362
2363 /* If this is just a dirty<->clean transition, and the array is clean
2364 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002365 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002366 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002367 && mddev->can_decrease_events
2368 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002369 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002370 mddev->can_decrease_events = 0;
2371 } else {
NeilBrown42543762006-06-26 00:27:57 -07002372 /* otherwise we have to go forward and ... */
2373 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002374 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
NeilBrown403df472014-09-30 15:52:29 +10002377 /*
2378 * This 64-bit counter should never wrap.
2379 * Either we are in around ~1 trillion A.C., assuming
2380 * 1 reboot per second, or we have a bug...
2381 */
2382 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002383
NeilBrowndafb20f2012-03-19 12:46:39 +11002384 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002385 if (rdev->badblocks.changed)
2386 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002387 if (test_bit(Faulty, &rdev->flags))
2388 set_bit(FaultRecorded, &rdev->flags);
2389 }
NeilBrown2699b672011-07-28 11:31:47 +10002390
NeilBrowne6910632008-02-06 01:39:51 -08002391 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002392 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
NeilBrown36a4e1f2011-10-07 14:23:17 +11002394 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2395 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
NeilBrown4ad13662007-07-17 04:06:13 -07002397 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002398 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002400
NeilBrown42543762006-06-26 00:27:57 -07002401 if (rdev->sb_loaded != 1)
2402 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
NeilBrownf4667222013-12-09 12:04:56 +11002404 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002405 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002406 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002407 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002408 pr_debug("md: (write) %s's sb offset: %llu\n",
2409 bdevname(rdev->bdev, b),
2410 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002411 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002412 if (rdev->badblocks.size) {
2413 md_super_write(mddev, rdev,
2414 rdev->badblocks.sector,
2415 rdev->badblocks.size << 9,
2416 rdev->bb_page);
2417 rdev->badblocks.size = 0;
2418 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002419
NeilBrownf4667222013-12-09 12:04:56 +11002420 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002421 pr_debug("md: %s (skipping faulty)\n",
2422 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002423
NeilBrown7bfa19f2005-06-21 17:17:28 -07002424 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 /* only need to write one superblock... */
2426 break;
2427 }
NeilBrowna9701a32005-11-08 21:39:34 -08002428 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002429 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002430
NeilBrown85572d72014-12-15 12:56:56 +11002431 spin_lock(&mddev->lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002432 if (mddev->in_sync != sync_req ||
2433 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002434 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002435 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002436 goto repeat;
2437 }
NeilBrown850b2b42006-10-03 01:15:46 -07002438 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002439 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002440 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002441 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2442 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002443
NeilBrowndafb20f2012-03-19 12:46:39 +11002444 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002445 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2446 clear_bit(Blocked, &rdev->flags);
2447
2448 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002449 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002450 clear_bit(BlockedBadBlocks, &rdev->flags);
2451 wake_up(&rdev->blocked_wait);
2452 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002453
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002454 if (mddev_is_clustered(mddev) && ret == 0)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002455 md_cluster_ops->metadata_update_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002457EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002459static int add_bound_rdev(struct md_rdev *rdev)
2460{
2461 struct mddev *mddev = rdev->mddev;
2462 int err = 0;
2463
2464 if (!mddev->pers->hot_remove_disk) {
2465 /* If there is hot_add_disk but no hot_remove_disk
2466 * then added disks for geometry changes,
2467 * and should be added immediately.
2468 */
2469 super_types[mddev->major_version].
2470 validate_super(mddev, rdev);
2471 err = mddev->pers->hot_add_disk(mddev, rdev);
2472 if (err) {
2473 unbind_rdev_from_array(rdev);
2474 export_rdev(rdev);
2475 return err;
2476 }
2477 }
2478 sysfs_notify_dirent_safe(rdev->sysfs_state);
2479
2480 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2481 if (mddev->degraded)
2482 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2483 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2484 md_new_event(mddev);
2485 md_wakeup_thread(mddev->thread);
2486 return 0;
2487}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Andre Noll7f6ce762008-03-23 18:34:54 +01002489/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002490 * We want to accept with case. For this we use cmd_match.
2491 */
2492static int cmd_match(const char *cmd, const char *str)
2493{
2494 /* See if cmd, written into a sysfs file, matches
2495 * str. They must either be the same, or cmd can
2496 * have a trailing newline
2497 */
2498 while (*cmd && *str && *cmd == *str) {
2499 cmd++;
2500 str++;
2501 }
2502 if (*cmd == '\n')
2503 cmd++;
2504 if (*str || *cmd)
2505 return 0;
2506 return 1;
2507}
2508
NeilBrown86e6ffd2005-11-08 21:39:24 -08002509struct rdev_sysfs_entry {
2510 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002511 ssize_t (*show)(struct md_rdev *, char *);
2512 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002513};
2514
2515static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002516state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002517{
2518 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002519 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002520 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002521
NeilBrown758bfc82014-12-15 12:56:59 +11002522 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002523 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002524 len+= sprintf(page+len, "%sfaulty",sep);
2525 sep = ",";
2526 }
NeilBrown758bfc82014-12-15 12:56:59 +11002527 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002528 len += sprintf(page+len, "%sin_sync",sep);
2529 sep = ",";
2530 }
Shaohua Liac6096e2015-10-04 09:20:11 -07002531 if (test_bit(Journal, &flags)) {
2532 len += sprintf(page+len, "%sjournal",sep);
2533 sep = ",";
2534 }
NeilBrown758bfc82014-12-15 12:56:59 +11002535 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002536 len += sprintf(page+len, "%swrite_mostly",sep);
2537 sep = ",";
2538 }
NeilBrown758bfc82014-12-15 12:56:59 +11002539 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002540 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002541 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002542 len += sprintf(page+len, "%sblocked", sep);
2543 sep = ",";
2544 }
NeilBrown758bfc82014-12-15 12:56:59 +11002545 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002546 !test_bit(Journal, &flags) &&
NeilBrown758bfc82014-12-15 12:56:59 +11002547 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002548 len += sprintf(page+len, "%sspare", sep);
2549 sep = ",";
2550 }
NeilBrown758bfc82014-12-15 12:56:59 +11002551 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002552 len += sprintf(page+len, "%swrite_error", sep);
2553 sep = ",";
2554 }
NeilBrown758bfc82014-12-15 12:56:59 +11002555 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002556 len += sprintf(page+len, "%swant_replacement", sep);
2557 sep = ",";
2558 }
NeilBrown758bfc82014-12-15 12:56:59 +11002559 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002560 len += sprintf(page+len, "%sreplacement", sep);
2561 sep = ",";
2562 }
2563
NeilBrown86e6ffd2005-11-08 21:39:24 -08002564 return len+sprintf(page+len, "\n");
2565}
2566
NeilBrown45dc2de2006-06-26 00:27:58 -07002567static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002568state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002569{
2570 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002571 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002572 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002573 * writemostly - sets write_mostly
2574 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002575 * blocked - sets the Blocked flags
2576 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002577 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002578 * -insync - clear Insync for a device with a slot assigned,
2579 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002580 * write_error - sets WriteErrorSeen
2581 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002582 */
2583 int err = -EINVAL;
2584 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2585 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002586 if (test_bit(Faulty, &rdev->flags))
2587 err = 0;
2588 else
2589 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002590 } else if (cmd_match(buf, "remove")) {
2591 if (rdev->raid_disk >= 0)
2592 err = -EBUSY;
2593 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002594 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002595 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002596 if (mddev_is_clustered(mddev))
2597 err = md_cluster_ops->remove_disk(mddev, rdev);
2598
2599 if (err == 0) {
2600 md_kick_rdev_from_array(rdev);
2601 if (mddev->pers)
2602 md_update_sb(mddev, 1);
2603 md_new_event(mddev);
2604 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002605 }
NeilBrownf6556752006-06-26 00:28:01 -07002606 } else if (cmd_match(buf, "writemostly")) {
2607 set_bit(WriteMostly, &rdev->flags);
2608 err = 0;
2609 } else if (cmd_match(buf, "-writemostly")) {
2610 clear_bit(WriteMostly, &rdev->flags);
2611 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002612 } else if (cmd_match(buf, "blocked")) {
2613 set_bit(Blocked, &rdev->flags);
2614 err = 0;
2615 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002616 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002617 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002618 /* metadata handler doesn't understand badblocks,
2619 * so we need to fail the device
2620 */
2621 md_error(rdev->mddev, rdev);
2622 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002623 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002624 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002625 wake_up(&rdev->blocked_wait);
2626 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2627 md_wakeup_thread(rdev->mddev->thread);
2628
2629 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002630 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2631 set_bit(In_sync, &rdev->flags);
2632 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002633 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2634 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002635 if (rdev->mddev->pers == NULL) {
2636 clear_bit(In_sync, &rdev->flags);
2637 rdev->saved_raid_disk = rdev->raid_disk;
2638 rdev->raid_disk = -1;
2639 err = 0;
2640 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002641 } else if (cmd_match(buf, "write_error")) {
2642 set_bit(WriteErrorSeen, &rdev->flags);
2643 err = 0;
2644 } else if (cmd_match(buf, "-write_error")) {
2645 clear_bit(WriteErrorSeen, &rdev->flags);
2646 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002647 } else if (cmd_match(buf, "want_replacement")) {
2648 /* Any non-spare device that is not a replacement can
2649 * become want_replacement at any time, but we then need to
2650 * check if recovery is needed.
2651 */
2652 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002653 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002654 !test_bit(Replacement, &rdev->flags))
2655 set_bit(WantReplacement, &rdev->flags);
2656 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2657 md_wakeup_thread(rdev->mddev->thread);
2658 err = 0;
2659 } else if (cmd_match(buf, "-want_replacement")) {
2660 /* Clearing 'want_replacement' is always allowed.
2661 * Once replacements starts it is too late though.
2662 */
2663 err = 0;
2664 clear_bit(WantReplacement, &rdev->flags);
2665 } else if (cmd_match(buf, "replacement")) {
2666 /* Can only set a device as a replacement when array has not
2667 * yet been started. Once running, replacement is automatic
2668 * from spares, or by assigning 'slot'.
2669 */
2670 if (rdev->mddev->pers)
2671 err = -EBUSY;
2672 else {
2673 set_bit(Replacement, &rdev->flags);
2674 err = 0;
2675 }
2676 } else if (cmd_match(buf, "-replacement")) {
2677 /* Similarly, can only clear Replacement before start */
2678 if (rdev->mddev->pers)
2679 err = -EBUSY;
2680 else {
2681 clear_bit(Replacement, &rdev->flags);
2682 err = 0;
2683 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002684 } else if (cmd_match(buf, "re-add")) {
2685 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002686 /* clear_bit is performed _after_ all the devices
2687 * have their local Faulty bit cleared. If any writes
2688 * happen in the meantime in the local node, they
2689 * will land in the local bitmap, which will be synced
2690 * by this node eventually
2691 */
2692 if (!mddev_is_clustered(rdev->mddev) ||
2693 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2694 clear_bit(Faulty, &rdev->flags);
2695 err = add_bound_rdev(rdev);
2696 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002697 } else
2698 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002699 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002700 if (!err)
2701 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002702 return err ? err : len;
2703}
NeilBrown80ca3a42006-07-10 04:44:18 -07002704static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002705__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002706
2707static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002708errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002709{
2710 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2711}
2712
2713static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002714errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002715{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002716 unsigned int n;
2717 int rv;
2718
2719 rv = kstrtouint(buf, 10, &n);
2720 if (rv < 0)
2721 return rv;
2722 atomic_set(&rdev->corrected_errors, n);
2723 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002724}
2725static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002726__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002727
NeilBrown014236d2006-01-06 00:20:55 -08002728static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002729slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002730{
Shaohua Lif2076e72015-10-08 21:54:12 -07002731 if (test_bit(Journal, &rdev->flags))
2732 return sprintf(page, "journal\n");
2733 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002734 return sprintf(page, "none\n");
2735 else
2736 return sprintf(page, "%d\n", rdev->raid_disk);
2737}
2738
2739static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002740slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002741{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002742 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002743 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002744
Shaohua Lif2076e72015-10-08 21:54:12 -07002745 if (test_bit(Journal, &rdev->flags))
2746 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002747 if (strncmp(buf, "none", 4)==0)
2748 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002749 else {
2750 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2751 if (err < 0)
2752 return err;
2753 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002754 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002755 /* Setting 'slot' on an active array requires also
2756 * updating the 'rd%d' link, and communicating
2757 * with the personality with ->hot_*_disk.
2758 * For now we only support removing
2759 * failed/spare devices. This normally happens automatically,
2760 * but not when the metadata is externally managed.
2761 */
NeilBrownc303da62008-02-06 01:39:51 -08002762 if (rdev->raid_disk == -1)
2763 return -EEXIST;
2764 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002765 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002766 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002767 clear_bit(Blocked, &rdev->flags);
2768 remove_and_add_spares(rdev->mddev, rdev);
2769 if (rdev->raid_disk >= 0)
2770 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002771 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2772 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002773 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002774 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002775 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002776 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002777 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002778
2779 if (rdev->raid_disk != -1)
2780 return -EBUSY;
2781
NeilBrownc6751b22011-02-02 11:57:13 +11002782 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2783 return -EBUSY;
2784
Neil Brown6c2fce22008-06-28 08:31:31 +10002785 if (rdev->mddev->pers->hot_add_disk == NULL)
2786 return -EINVAL;
2787
NeilBrownba1b41b2011-01-14 09:14:34 +11002788 if (slot >= rdev->mddev->raid_disks &&
2789 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2790 return -ENOSPC;
2791
Neil Brown6c2fce22008-06-28 08:31:31 +10002792 rdev->raid_disk = slot;
2793 if (test_bit(In_sync, &rdev->flags))
2794 rdev->saved_raid_disk = slot;
2795 else
2796 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002797 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002798 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002799 err = rdev->mddev->pers->
2800 hot_add_disk(rdev->mddev, rdev);
2801 if (err) {
2802 rdev->raid_disk = -1;
2803 return err;
2804 } else
2805 sysfs_notify_dirent_safe(rdev->sysfs_state);
2806 if (sysfs_link_rdev(rdev->mddev, rdev))
2807 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002808 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002809 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002810 if (slot >= rdev->mddev->raid_disks &&
2811 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002812 return -ENOSPC;
2813 rdev->raid_disk = slot;
2814 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002815 clear_bit(Faulty, &rdev->flags);
2816 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002817 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002818 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002819 }
NeilBrown014236d2006-01-06 00:20:55 -08002820 return len;
2821}
2822
NeilBrown014236d2006-01-06 00:20:55 -08002823static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002824__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002825
NeilBrown93c8cad2006-01-06 00:20:56 -08002826static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002827offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002828{
Andrew Morton6961ece2006-01-06 00:20:59 -08002829 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002830}
2831
2832static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002833offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002834{
NeilBrownc6563a82012-05-21 09:27:00 +10002835 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002836 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002837 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002838 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002839 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002840 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002841 /* Must set offset before size, so overlap checks
2842 * can be sane */
2843 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002844 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002845 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002846 return len;
2847}
2848
2849static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002850__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002851
NeilBrownc6563a82012-05-21 09:27:00 +10002852static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2853{
2854 return sprintf(page, "%llu\n",
2855 (unsigned long long)rdev->new_data_offset);
2856}
2857
2858static ssize_t new_offset_store(struct md_rdev *rdev,
2859 const char *buf, size_t len)
2860{
2861 unsigned long long new_offset;
2862 struct mddev *mddev = rdev->mddev;
2863
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002864 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002865 return -EINVAL;
2866
NeilBrownf851b602014-12-11 10:02:10 +11002867 if (mddev->sync_thread ||
2868 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002869 return -EBUSY;
2870 if (new_offset == rdev->data_offset)
2871 /* reset is always permitted */
2872 ;
2873 else if (new_offset > rdev->data_offset) {
2874 /* must not push array size beyond rdev_sectors */
2875 if (new_offset - rdev->data_offset
2876 + mddev->dev_sectors > rdev->sectors)
2877 return -E2BIG;
2878 }
2879 /* Metadata worries about other space details. */
2880
2881 /* decreasing the offset is inconsistent with a backwards
2882 * reshape.
2883 */
2884 if (new_offset < rdev->data_offset &&
2885 mddev->reshape_backwards)
2886 return -EINVAL;
2887 /* Increasing offset is inconsistent with forwards
2888 * reshape. reshape_direction should be set to
2889 * 'backwards' first.
2890 */
2891 if (new_offset > rdev->data_offset &&
2892 !mddev->reshape_backwards)
2893 return -EINVAL;
2894
2895 if (mddev->pers && mddev->persistent &&
2896 !super_types[mddev->major_version]
2897 .allow_new_offset(rdev, new_offset))
2898 return -E2BIG;
2899 rdev->new_data_offset = new_offset;
2900 if (new_offset > rdev->data_offset)
2901 mddev->reshape_backwards = 1;
2902 else if (new_offset < rdev->data_offset)
2903 mddev->reshape_backwards = 0;
2904
2905 return len;
2906}
2907static struct rdev_sysfs_entry rdev_new_offset =
2908__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2909
NeilBrown83303b62006-01-06 00:21:06 -08002910static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002911rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002912{
Andre Nolldd8ac332009-03-31 14:33:13 +11002913 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002914}
2915
NeilBrownc5d79ad2008-02-06 01:39:54 -08002916static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2917{
2918 /* check if two start/length pairs overlap */
2919 if (s1+l1 <= s2)
2920 return 0;
2921 if (s2+l2 <= s1)
2922 return 0;
2923 return 1;
2924}
2925
Dan Williamsb522adc2009-03-31 15:00:31 +11002926static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2927{
2928 unsigned long long blocks;
2929 sector_t new;
2930
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002931 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002932 return -EINVAL;
2933
2934 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2935 return -EINVAL; /* sector conversion overflow */
2936
2937 new = blocks * 2;
2938 if (new != blocks * 2)
2939 return -EINVAL; /* unsigned long long to sector_t overflow */
2940
2941 *sectors = new;
2942 return 0;
2943}
2944
NeilBrown83303b62006-01-06 00:21:06 -08002945static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002946rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002947{
NeilBrownfd01b882011-10-11 16:47:53 +11002948 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002949 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002950 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002951
Shaohua Lif2076e72015-10-08 21:54:12 -07002952 if (test_bit(Journal, &rdev->flags))
2953 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11002954 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002955 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002956 if (rdev->data_offset != rdev->new_data_offset)
2957 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002958 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002959 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002960 sectors = super_types[my_mddev->major_version].
2961 rdev_size_change(rdev, sectors);
2962 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002963 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002964 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002965 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002966 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002967 if (!my_mddev->pers->resize)
2968 /* Cannot change size for RAID0 or Linear etc */
2969 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002970 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002971 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002972 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002973
Andre Nolldd8ac332009-03-31 14:33:13 +11002974 rdev->sectors = sectors;
2975 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002976 /* Need to check that all other rdevs with the same
2977 * ->bdev do not overlap. 'rcu' is sufficient to walk
2978 * the rdev lists safely.
2979 * This check does not provide a hard guarantee, it
2980 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002981 */
NeilBrownfd01b882011-10-11 16:47:53 +11002982 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002983 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002984 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002985
NeilBrown8b1afc32014-09-29 15:33:20 +10002986 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002987 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002988 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002989
NeilBrowndafb20f2012-03-19 12:46:39 +11002990 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002991 if (rdev->bdev == rdev2->bdev &&
2992 rdev != rdev2 &&
2993 overlaps(rdev->data_offset, rdev->sectors,
2994 rdev2->data_offset,
2995 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002996 overlap = 1;
2997 break;
2998 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002999 if (overlap) {
3000 mddev_put(mddev);
3001 break;
3002 }
3003 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003004 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003005 if (overlap) {
3006 /* Someone else could have slipped in a size
3007 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003008 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003009 * safe, and trust userspace not to race with
3010 * itself
3011 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003012 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003013 return -EBUSY;
3014 }
3015 }
NeilBrown83303b62006-01-06 00:21:06 -08003016 return len;
3017}
3018
3019static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003020__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003021
NeilBrown3cb03002011-10-11 16:45:26 +11003022static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003023{
3024 unsigned long long recovery_start = rdev->recovery_offset;
3025
3026 if (test_bit(In_sync, &rdev->flags) ||
3027 recovery_start == MaxSector)
3028 return sprintf(page, "none\n");
3029
3030 return sprintf(page, "%llu\n", recovery_start);
3031}
3032
NeilBrown3cb03002011-10-11 16:45:26 +11003033static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003034{
3035 unsigned long long recovery_start;
3036
3037 if (cmd_match(buf, "none"))
3038 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003039 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003040 return -EINVAL;
3041
3042 if (rdev->mddev->pers &&
3043 rdev->raid_disk >= 0)
3044 return -EBUSY;
3045
3046 rdev->recovery_offset = recovery_start;
3047 if (recovery_start == MaxSector)
3048 set_bit(In_sync, &rdev->flags);
3049 else
3050 clear_bit(In_sync, &rdev->flags);
3051 return len;
3052}
3053
3054static struct rdev_sysfs_entry rdev_recovery_start =
3055__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3056
NeilBrown16c791a2011-07-28 11:31:47 +10003057static ssize_t
3058badblocks_show(struct badblocks *bb, char *page, int unack);
3059static ssize_t
3060badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3061
NeilBrown3cb03002011-10-11 16:45:26 +11003062static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003063{
3064 return badblocks_show(&rdev->badblocks, page, 0);
3065}
NeilBrown3cb03002011-10-11 16:45:26 +11003066static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003067{
NeilBrownde393cd2011-07-28 11:31:48 +10003068 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3069 /* Maybe that ack was all we needed */
3070 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3071 wake_up(&rdev->blocked_wait);
3072 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003073}
3074static struct rdev_sysfs_entry rdev_bad_blocks =
3075__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3076
NeilBrown3cb03002011-10-11 16:45:26 +11003077static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003078{
3079 return badblocks_show(&rdev->badblocks, page, 1);
3080}
NeilBrown3cb03002011-10-11 16:45:26 +11003081static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003082{
3083 return badblocks_store(&rdev->badblocks, page, len, 1);
3084}
3085static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3086__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3087
NeilBrown86e6ffd2005-11-08 21:39:24 -08003088static struct attribute *rdev_default_attrs[] = {
3089 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003090 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003091 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003092 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003093 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003094 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003095 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003096 &rdev_bad_blocks.attr,
3097 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003098 NULL,
3099};
3100static ssize_t
3101rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3102{
3103 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003104 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003105
3106 if (!entry->show)
3107 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003108 if (!rdev->mddev)
3109 return -EBUSY;
3110 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003111}
3112
3113static ssize_t
3114rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3115 const char *page, size_t length)
3116{
3117 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003118 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003119 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003120 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003121
3122 if (!entry->store)
3123 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003124 if (!capable(CAP_SYS_ADMIN))
3125 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003126 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003127 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003128 if (rdev->mddev == NULL)
3129 rv = -EBUSY;
3130 else
3131 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003132 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003133 }
3134 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003135}
3136
3137static void rdev_free(struct kobject *ko)
3138{
NeilBrown3cb03002011-10-11 16:45:26 +11003139 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003140 kfree(rdev);
3141}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003142static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003143 .show = rdev_attr_show,
3144 .store = rdev_attr_store,
3145};
3146static struct kobj_type rdev_ktype = {
3147 .release = rdev_free,
3148 .sysfs_ops = &rdev_sysfs_ops,
3149 .default_attrs = rdev_default_attrs,
3150};
3151
NeilBrown3cb03002011-10-11 16:45:26 +11003152int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003153{
3154 rdev->desc_nr = -1;
3155 rdev->saved_raid_disk = -1;
3156 rdev->raid_disk = -1;
3157 rdev->flags = 0;
3158 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003159 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003160 rdev->sb_events = 0;
3161 rdev->last_read_error.tv_sec = 0;
3162 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003163 rdev->sb_loaded = 0;
3164 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003165 atomic_set(&rdev->nr_pending, 0);
3166 atomic_set(&rdev->read_errors, 0);
3167 atomic_set(&rdev->corrected_errors, 0);
3168
3169 INIT_LIST_HEAD(&rdev->same_set);
3170 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003171
3172 /* Add space to store bad block list.
3173 * This reserves the space even on arrays where it cannot
3174 * be used - I wonder if that matters
3175 */
3176 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003177 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003178 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3179 seqlock_init(&rdev->badblocks.lock);
3180 if (rdev->badblocks.page == NULL)
3181 return -ENOMEM;
3182
3183 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003184}
3185EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186/*
3187 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3188 *
3189 * mark the device faulty if:
3190 *
3191 * - the device is nonexistent (zero size)
3192 * - the device has no valid superblock
3193 *
3194 * a faulty rdev _never_ has rdev->sb set.
3195 */
NeilBrown3cb03002011-10-11 16:45:26 +11003196static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
3198 char b[BDEVNAME_SIZE];
3199 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003200 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 sector_t size;
3202
NeilBrown9ffae0c2006-01-06 00:20:32 -08003203 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 if (!rdev) {
3205 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3206 return ERR_PTR(-ENOMEM);
3207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
NeilBrown2230dfe2011-07-28 11:31:46 +10003209 err = md_rdev_init(rdev);
3210 if (err)
3211 goto abort_free;
3212 err = alloc_disk_sb(rdev);
3213 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 goto abort_free;
3215
NeilBrownc5d79ad2008-02-06 01:39:54 -08003216 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 if (err)
3218 goto abort_free;
3219
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003220 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003221
Mike Snitzer77304d22010-11-08 14:39:12 +01003222 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003224 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 "md: %s has zero or unknown size, marking faulty!\n",
3226 bdevname(rdev->bdev,b));
3227 err = -EINVAL;
3228 goto abort_free;
3229 }
3230
3231 if (super_format >= 0) {
3232 err = super_types[super_format].
3233 load_super(rdev, NULL, super_minor);
3234 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003235 printk(KERN_WARNING
3236 "md: %s does not have a valid v%d.%d "
3237 "superblock, not importing!\n",
3238 bdevname(rdev->bdev,b),
3239 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 goto abort_free;
3241 }
3242 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003243 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 "md: could not read %s's sb, not importing!\n",
3245 bdevname(rdev->bdev,b));
3246 goto abort_free;
3247 }
3248 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 return rdev;
3251
3252abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003253 if (rdev->bdev)
3254 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003255 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 kfree(rdev);
3257 return ERR_PTR(err);
3258}
3259
3260/*
3261 * Check a full RAID array for plausibility
3262 */
3263
NeilBrownf72ffdd2014-09-30 14:23:59 +10003264static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
3266 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003267 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 char b[BDEVNAME_SIZE];
3269
3270 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003271 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 switch (super_types[mddev->major_version].
3273 load_super(rdev, freshest, mddev->minor_version)) {
3274 case 1:
3275 freshest = rdev;
3276 break;
3277 case 0:
3278 break;
3279 default:
3280 printk( KERN_ERR \
3281 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003282 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003284 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 }
3286
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 super_types[mddev->major_version].
3288 validate_super(mddev, freshest);
3289
3290 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003291 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003292 if (mddev->max_disks &&
3293 (rdev->desc_nr >= mddev->max_disks ||
3294 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003295 printk(KERN_WARNING
3296 "md: %s: %s: only %d devices permitted\n",
3297 mdname(mddev), bdevname(rdev->bdev, b),
3298 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003299 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003300 continue;
3301 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003302 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 if (super_types[mddev->major_version].
3304 validate_super(mddev, rdev)) {
3305 printk(KERN_WARNING "md: kicking non-fresh %s"
3306 " from array!\n",
3307 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003308 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 continue;
3310 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 if (mddev->level == LEVEL_MULTIPATH) {
3313 rdev->desc_nr = i++;
3314 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003315 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003316 } else if (rdev->raid_disk >=
3317 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3318 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003319 rdev->raid_disk = -1;
3320 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 }
3322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323}
3324
NeilBrown72e02072009-12-14 12:49:55 +11003325/* Read a fixed-point number.
3326 * Numbers in sysfs attributes should be in "standard" units where
3327 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003328 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003329 * milliseconds or jiffies.
3330 * This function takes a decimal number with a possible fractional
3331 * component, and produces an integer which is the result of
3332 * multiplying that number by 10^'scale'.
3333 * all without any floating-point arithmetic.
3334 */
3335int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3336{
3337 unsigned long result = 0;
3338 long decimals = -1;
3339 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3340 if (*cp == '.')
3341 decimals = 0;
3342 else if (decimals < scale) {
3343 unsigned int value;
3344 value = *cp - '0';
3345 result = result * 10 + value;
3346 if (decimals >= 0)
3347 decimals++;
3348 }
3349 cp++;
3350 }
3351 if (*cp == '\n')
3352 cp++;
3353 if (*cp)
3354 return -EINVAL;
3355 if (decimals < 0)
3356 decimals = 0;
3357 while (decimals < scale) {
3358 result *= 10;
3359 decimals ++;
3360 }
3361 *res = result;
3362 return 0;
3363}
3364
NeilBrowneae17012005-11-08 21:39:23 -08003365static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003366safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003367{
3368 int msec = (mddev->safemode_delay*1000)/HZ;
3369 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3370}
3371static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003372safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003373{
NeilBrown16f17b32006-06-26 00:27:37 -07003374 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003375
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003376 if (mddev_is_clustered(mddev)) {
3377 pr_info("md: Safemode is disabled for clustered mode\n");
3378 return -EINVAL;
3379 }
3380
NeilBrown72e02072009-12-14 12:49:55 +11003381 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003382 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003383 if (msec == 0)
3384 mddev->safemode_delay = 0;
3385 else {
NeilBrown19052c02008-08-05 15:54:13 +10003386 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003387 unsigned long new_delay = (msec*HZ)/1000;
3388
3389 if (new_delay == 0)
3390 new_delay = 1;
3391 mddev->safemode_delay = new_delay;
3392 if (new_delay < old_delay || old_delay == 0)
3393 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003394 }
3395 return len;
3396}
3397static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003398__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003399
3400static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003401level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003402{
NeilBrown36d091f2014-12-15 12:56:58 +11003403 struct md_personality *p;
3404 int ret;
3405 spin_lock(&mddev->lock);
3406 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003407 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003408 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003409 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003410 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003411 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003412 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003413 else
NeilBrown36d091f2014-12-15 12:56:58 +11003414 ret = 0;
3415 spin_unlock(&mddev->lock);
3416 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003417}
3418
NeilBrownd9d166c2006-01-06 00:20:51 -08003419static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003420level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003421{
Dan Williamsf2859af2010-05-02 10:04:16 -07003422 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003423 ssize_t rv;
3424 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003425 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003426 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003427 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003428 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003429
NeilBrown67918752014-12-15 12:57:01 +11003430 if (slen == 0 || slen >= sizeof(clevel))
3431 return -EINVAL;
3432
3433 rv = mddev_lock(mddev);
3434 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003435 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003436
3437 if (mddev->pers == NULL) {
3438 strncpy(mddev->clevel, buf, slen);
3439 if (mddev->clevel[slen-1] == '\n')
3440 slen--;
3441 mddev->clevel[slen] = 0;
3442 mddev->level = LEVEL_NONE;
3443 rv = len;
3444 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003445 }
NeilBrown67918752014-12-15 12:57:01 +11003446 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003447 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003448 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003449
3450 /* request to change the personality. Need to ensure:
3451 * - array is not engaged in resync/recovery/reshape
3452 * - old personality can be suspended
3453 * - new personality will access other array.
3454 */
3455
NeilBrown67918752014-12-15 12:57:01 +11003456 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003457 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003458 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003459 mddev->reshape_position != MaxSector ||
3460 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003461 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003462
NeilBrown67918752014-12-15 12:57:01 +11003463 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003464 if (!mddev->pers->quiesce) {
3465 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3466 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003467 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003468 }
3469
3470 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003471 strncpy(clevel, buf, slen);
3472 if (clevel[slen-1] == '\n')
3473 slen--;
3474 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003475 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003476 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003477
Dan Williamsf2859af2010-05-02 10:04:16 -07003478 if (request_module("md-%s", clevel) != 0)
3479 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003480 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003481 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003482 if (!pers || !try_module_get(pers->owner)) {
3483 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003484 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003485 rv = -EINVAL;
3486 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003487 }
3488 spin_unlock(&pers_lock);
3489
3490 if (pers == mddev->pers) {
3491 /* Nothing to do! */
3492 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003493 rv = len;
3494 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003495 }
3496 if (!pers->takeover) {
3497 module_put(pers->owner);
3498 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003499 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003500 rv = -EINVAL;
3501 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003502 }
3503
NeilBrowndafb20f2012-03-19 12:46:39 +11003504 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003505 rdev->new_raid_disk = rdev->raid_disk;
3506
NeilBrown245f46c2009-03-31 14:39:39 +11003507 /* ->takeover must set new_* and/or delta_disks
3508 * if it succeeds, and may set them when it fails.
3509 */
3510 priv = pers->takeover(mddev);
3511 if (IS_ERR(priv)) {
3512 mddev->new_level = mddev->level;
3513 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003514 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003515 mddev->raid_disks -= mddev->delta_disks;
3516 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003517 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003518 module_put(pers->owner);
3519 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003520 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003521 rv = PTR_ERR(priv);
3522 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003523 }
3524
3525 /* Looks like we have a winner */
3526 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003527 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003528
3529 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003530 oldpers = mddev->pers;
3531 oldpriv = mddev->private;
3532 mddev->pers = pers;
3533 mddev->private = priv;
3534 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3535 mddev->level = mddev->new_level;
3536 mddev->layout = mddev->new_layout;
3537 mddev->chunk_sectors = mddev->new_chunk_sectors;
3538 mddev->delta_disks = 0;
3539 mddev->reshape_backwards = 0;
3540 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003541 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003542
NeilBrowndb721d32014-12-15 12:56:58 +11003543 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003544 mddev->external) {
3545 /* We are converting from a no-redundancy array
3546 * to a redundancy array and metadata is managed
3547 * externally so we need to be sure that writes
3548 * won't block due to a need to transition
3549 * clean->dirty
3550 * until external management is started.
3551 */
3552 mddev->in_sync = 0;
3553 mddev->safemode_delay = 0;
3554 mddev->safemode = 0;
3555 }
3556
NeilBrowndb721d32014-12-15 12:56:58 +11003557 oldpers->free(mddev, oldpriv);
3558
3559 if (oldpers->sync_request == NULL &&
3560 pers->sync_request != NULL) {
3561 /* need to add the md_redundancy_group */
3562 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3563 printk(KERN_WARNING
3564 "md: cannot register extra attributes for %s\n",
3565 mdname(mddev));
3566 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3567 }
3568 if (oldpers->sync_request != NULL &&
3569 pers->sync_request == NULL) {
3570 /* need to remove the md_redundancy_group */
3571 if (mddev->to_remove == NULL)
3572 mddev->to_remove = &md_redundancy_group;
3573 }
3574
NeilBrowndafb20f2012-03-19 12:46:39 +11003575 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003576 if (rdev->raid_disk < 0)
3577 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003578 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003579 rdev->new_raid_disk = -1;
3580 if (rdev->new_raid_disk == rdev->raid_disk)
3581 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003582 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003583 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003584 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003585 if (rdev->raid_disk < 0)
3586 continue;
3587 if (rdev->new_raid_disk == rdev->raid_disk)
3588 continue;
3589 rdev->raid_disk = rdev->new_raid_disk;
3590 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003591 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003592 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003593 if (sysfs_link_rdev(mddev, rdev))
3594 printk(KERN_WARNING "md: cannot register rd%d"
3595 " for %s after level change\n",
3596 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003597 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003598 }
3599
NeilBrowndb721d32014-12-15 12:56:58 +11003600 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003601 /* this is now an array without redundancy, so
3602 * it must always be in_sync
3603 */
3604 mddev->in_sync = 1;
3605 del_timer_sync(&mddev->safemode_timer);
3606 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003607 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003608 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003609 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003610 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003611 if (!mddev->thread)
3612 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003613 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003614 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003615 rv = len;
3616out_unlock:
3617 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003618 return rv;
3619}
3620
3621static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003622__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003623
NeilBrownd4dbd022006-06-26 00:27:59 -07003624static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003625layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003626{
3627 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003628 if (mddev->reshape_position != MaxSector &&
3629 mddev->layout != mddev->new_layout)
3630 return sprintf(page, "%d (%d)\n",
3631 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003632 return sprintf(page, "%d\n", mddev->layout);
3633}
3634
3635static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003636layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003637{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003638 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003639 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003640
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003641 err = kstrtouint(buf, 10, &n);
3642 if (err < 0)
3643 return err;
NeilBrown67918752014-12-15 12:57:01 +11003644 err = mddev_lock(mddev);
3645 if (err)
3646 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003647
NeilBrownb3546032009-03-31 14:56:41 +11003648 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003649 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003650 err = -EBUSY;
3651 else if (mddev->ro)
3652 err = -EROFS;
3653 else {
3654 mddev->new_layout = n;
3655 err = mddev->pers->check_reshape(mddev);
3656 if (err)
3657 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003658 }
NeilBrownb3546032009-03-31 14:56:41 +11003659 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003660 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003661 if (mddev->reshape_position == MaxSector)
3662 mddev->layout = n;
3663 }
NeilBrown67918752014-12-15 12:57:01 +11003664 mddev_unlock(mddev);
3665 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003666}
3667static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003668__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003669
NeilBrowneae17012005-11-08 21:39:23 -08003670static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003671raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003672{
NeilBrownbb636542005-11-08 21:39:45 -08003673 if (mddev->raid_disks == 0)
3674 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003675 if (mddev->reshape_position != MaxSector &&
3676 mddev->delta_disks != 0)
3677 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3678 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003679 return sprintf(page, "%d\n", mddev->raid_disks);
3680}
3681
NeilBrownfd01b882011-10-11 16:47:53 +11003682static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003683
3684static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003685raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003686{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003687 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003688 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003689
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003690 err = kstrtouint(buf, 10, &n);
3691 if (err < 0)
3692 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003693
NeilBrown67918752014-12-15 12:57:01 +11003694 err = mddev_lock(mddev);
3695 if (err)
3696 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003697 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003698 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003699 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003700 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003701 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003702
NeilBrown67918752014-12-15 12:57:01 +11003703 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003704 rdev_for_each(rdev, mddev) {
3705 if (olddisks < n &&
3706 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003707 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003708 if (olddisks > n &&
3709 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003710 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003711 }
NeilBrown67918752014-12-15 12:57:01 +11003712 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003713 mddev->delta_disks = n - olddisks;
3714 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003715 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003716 } else
NeilBrownda943b992006-01-06 00:20:54 -08003717 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003718out_unlock:
3719 mddev_unlock(mddev);
3720 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003721}
3722static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003723__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003724
NeilBrown24dd4692005-11-08 21:39:26 -08003725static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003726chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003727{
NeilBrown08a02ec2007-05-09 02:35:38 -07003728 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003729 mddev->chunk_sectors != mddev->new_chunk_sectors)
3730 return sprintf(page, "%d (%d)\n",
3731 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003732 mddev->chunk_sectors << 9);
3733 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003734}
3735
3736static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003737chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003738{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003739 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003740 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003741
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003742 err = kstrtoul(buf, 10, &n);
3743 if (err < 0)
3744 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003745
NeilBrown67918752014-12-15 12:57:01 +11003746 err = mddev_lock(mddev);
3747 if (err)
3748 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003749 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003750 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003751 err = -EBUSY;
3752 else if (mddev->ro)
3753 err = -EROFS;
3754 else {
3755 mddev->new_chunk_sectors = n >> 9;
3756 err = mddev->pers->check_reshape(mddev);
3757 if (err)
3758 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003759 }
NeilBrownb3546032009-03-31 14:56:41 +11003760 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003761 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003762 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003763 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003764 }
NeilBrown67918752014-12-15 12:57:01 +11003765 mddev_unlock(mddev);
3766 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003767}
3768static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003769__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003770
NeilBrowna94213b2006-06-26 00:28:00 -07003771static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003772resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003773{
NeilBrownd1a7c502009-03-31 15:24:32 +11003774 if (mddev->recovery_cp == MaxSector)
3775 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003776 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3777}
3778
3779static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003780resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003781{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003782 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003783 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003784
3785 if (cmd_match(buf, "none"))
3786 n = MaxSector;
3787 else {
3788 err = kstrtoull(buf, 10, &n);
3789 if (err < 0)
3790 return err;
3791 if (n != (sector_t)n)
3792 return -EINVAL;
3793 }
NeilBrowna94213b2006-06-26 00:28:00 -07003794
NeilBrown67918752014-12-15 12:57:01 +11003795 err = mddev_lock(mddev);
3796 if (err)
3797 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003798 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003799 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003800
NeilBrown67918752014-12-15 12:57:01 +11003801 if (!err) {
3802 mddev->recovery_cp = n;
3803 if (mddev->pers)
3804 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3805 }
3806 mddev_unlock(mddev);
3807 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003808}
3809static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003810__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3811 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003812
NeilBrown9e653b62006-06-26 00:27:58 -07003813/*
3814 * The array state can be:
3815 *
3816 * clear
3817 * No devices, no size, no level
3818 * Equivalent to STOP_ARRAY ioctl
3819 * inactive
3820 * May have some settings, but array is not active
3821 * all IO results in error
3822 * When written, doesn't tear down array, but just stops it
3823 * suspended (not supported yet)
3824 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003825 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003826 * readonly
3827 * no resync can happen. no superblocks get written.
3828 * write requests fail
3829 * read-auto
3830 * like readonly, but behaves like 'clean' on a write request.
3831 *
3832 * clean - no pending writes, but otherwise active.
3833 * When written to inactive array, starts without resync
3834 * If a write request arrives then
3835 * if metadata is known, mark 'dirty' and switch to 'active'.
3836 * if not known, block and switch to write-pending
3837 * If written to an active array that has pending writes, then fails.
3838 * active
3839 * fully active: IO and resync can be happening.
3840 * When written to inactive array, starts with resync
3841 *
3842 * write-pending
3843 * clean, but writes are blocked waiting for 'active' to be written.
3844 *
3845 * active-idle
3846 * like active, but no writes have been seen for a while (100msec).
3847 *
3848 */
3849enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3850 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003851static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003852 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3853 "write-pending", "active-idle", NULL };
3854
3855static int match_word(const char *word, char **list)
3856{
3857 int n;
3858 for (n=0; list[n]; n++)
3859 if (cmd_match(word, list[n]))
3860 break;
3861 return n;
3862}
3863
3864static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003865array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003866{
3867 enum array_state st = inactive;
3868
3869 if (mddev->pers)
3870 switch(mddev->ro) {
3871 case 1:
3872 st = readonly;
3873 break;
3874 case 2:
3875 st = read_auto;
3876 break;
3877 case 0:
3878 if (mddev->in_sync)
3879 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003880 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003881 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003882 else if (mddev->safemode)
3883 st = active_idle;
3884 else
3885 st = active;
3886 }
3887 else {
3888 if (list_empty(&mddev->disks) &&
3889 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003890 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003891 st = clear;
3892 else
3893 st = inactive;
3894 }
3895 return sprintf(page, "%s\n", array_states[st]);
3896}
3897
NeilBrownf72ffdd2014-09-30 14:23:59 +10003898static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3899static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3900static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003901static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003902
3903static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003904array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003905{
NeilBrown67918752014-12-15 12:57:01 +11003906 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003907 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003908
3909 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3910 /* don't take reconfig_mutex when toggling between
3911 * clean and active
3912 */
3913 spin_lock(&mddev->lock);
3914 if (st == active) {
3915 restart_array(mddev);
3916 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3917 wake_up(&mddev->sb_wait);
3918 err = 0;
3919 } else /* st == clean */ {
3920 restart_array(mddev);
3921 if (atomic_read(&mddev->writes_pending) == 0) {
3922 if (mddev->in_sync == 0) {
3923 mddev->in_sync = 1;
3924 if (mddev->safemode == 1)
3925 mddev->safemode = 0;
3926 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3927 }
3928 err = 0;
3929 } else
3930 err = -EBUSY;
3931 }
3932 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003933 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003934 }
3935 err = mddev_lock(mddev);
3936 if (err)
3937 return err;
3938 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003939 switch(st) {
3940 case bad_word:
3941 break;
3942 case clear:
3943 /* stopping an active array */
NeilBrowna05b7ea02012-07-19 15:59:18 +10003944 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003945 break;
3946 case inactive:
3947 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003948 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003949 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003950 else
NeilBrowne6910632008-02-06 01:39:51 -08003951 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003952 break;
3953 case suspended:
3954 break; /* not supported yet */
3955 case readonly:
3956 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003957 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003958 else {
3959 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003960 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003961 err = do_md_run(mddev);
3962 }
3963 break;
3964 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003965 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003966 if (mddev->ro == 0)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003967 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003968 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003969 err = restart_array(mddev);
3970 if (err == 0) {
3971 mddev->ro = 2;
3972 set_disk_ro(mddev->gendisk, 0);
3973 }
NeilBrown9e653b62006-06-26 00:27:58 -07003974 } else {
3975 mddev->ro = 2;
3976 err = do_md_run(mddev);
3977 }
3978 break;
3979 case clean:
3980 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07003981 err = restart_array(mddev);
3982 if (err)
3983 break;
NeilBrown85572d72014-12-15 12:56:56 +11003984 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003985 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003986 if (mddev->in_sync == 0) {
3987 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003988 if (mddev->safemode == 1)
3989 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003990 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003991 }
3992 err = 0;
3993 } else
3994 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11003995 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003996 } else
3997 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003998 break;
3999 case active:
4000 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004001 err = restart_array(mddev);
4002 if (err)
4003 break;
NeilBrown070dc6d2010-08-30 17:33:34 +10004004 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004005 wake_up(&mddev->sb_wait);
4006 err = 0;
4007 } else {
4008 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004009 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004010 err = do_md_run(mddev);
4011 }
4012 break;
4013 case write_pending:
4014 case active_idle:
4015 /* these cannot be set */
4016 break;
4017 }
NeilBrown67918752014-12-15 12:57:01 +11004018
4019 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004020 if (mddev->hold_active == UNTIL_IOCTL)
4021 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004022 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004023 }
NeilBrown67918752014-12-15 12:57:01 +11004024 mddev_unlock(mddev);
4025 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004026}
NeilBrown80ca3a42006-07-10 04:44:18 -07004027static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004028__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004029
NeilBrown6d7ff732006-01-06 00:21:16 -08004030static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004031max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004032 return sprintf(page, "%d\n",
4033 atomic_read(&mddev->max_corr_read_errors));
4034}
4035
4036static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004037max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004038{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004039 unsigned int n;
4040 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004041
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004042 rv = kstrtouint(buf, 10, &n);
4043 if (rv < 0)
4044 return rv;
4045 atomic_set(&mddev->max_corr_read_errors, n);
4046 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004047}
4048
4049static struct md_sysfs_entry max_corr_read_errors =
4050__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4051 max_corrected_read_errors_store);
4052
4053static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004054null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08004055{
4056 return -EINVAL;
4057}
4058
4059static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004060new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08004061{
4062 /* buf must be %d:%d\n? giving major and minor numbers */
4063 /* The new device is added to the array.
4064 * If the array has a persistent superblock, we read the
4065 * superblock to initialise info and check validity.
4066 * Otherwise, only checking done is that in bind_rdev_to_array,
4067 * which mainly checks size.
4068 */
4069 char *e;
4070 int major = simple_strtoul(buf, &e, 10);
4071 int minor;
4072 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004073 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08004074 int err;
4075
4076 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4077 return -EINVAL;
4078 minor = simple_strtoul(e+1, &e, 10);
4079 if (*e && *e != '\n')
4080 return -EINVAL;
4081 dev = MKDEV(major, minor);
4082 if (major != MAJOR(dev) ||
4083 minor != MINOR(dev))
4084 return -EOVERFLOW;
4085
NeilBrown67918752014-12-15 12:57:01 +11004086 flush_workqueue(md_misc_wq);
4087
4088 err = mddev_lock(mddev);
4089 if (err)
4090 return err;
NeilBrown6d7ff732006-01-06 00:21:16 -08004091 if (mddev->persistent) {
4092 rdev = md_import_device(dev, mddev->major_version,
4093 mddev->minor_version);
4094 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004095 struct md_rdev *rdev0
4096 = list_entry(mddev->disks.next,
4097 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08004098 err = super_types[mddev->major_version]
4099 .load_super(rdev, rdev0, mddev->minor_version);
4100 if (err < 0)
4101 goto out;
4102 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004103 } else if (mddev->external)
4104 rdev = md_import_device(dev, -2, -1);
4105 else
NeilBrown6d7ff732006-01-06 00:21:16 -08004106 rdev = md_import_device(dev, -1, -1);
4107
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004108 if (IS_ERR(rdev)) {
4109 mddev_unlock(mddev);
NeilBrown6d7ff732006-01-06 00:21:16 -08004110 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004111 }
NeilBrown6d7ff732006-01-06 00:21:16 -08004112 err = bind_rdev_to_array(rdev, mddev);
4113 out:
4114 if (err)
4115 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004116 mddev_unlock(mddev);
NeilBrown6d7ff732006-01-06 00:21:16 -08004117 return err ? err : len;
4118}
4119
4120static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004121__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004122
4123static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004124bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004125{
4126 char *end;
4127 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004128 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004129
NeilBrown67918752014-12-15 12:57:01 +11004130 err = mddev_lock(mddev);
4131 if (err)
4132 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004133 if (!mddev->bitmap)
4134 goto out;
4135 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4136 while (*buf) {
4137 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4138 if (buf == end) break;
4139 if (*end == '-') { /* range */
4140 buf = end + 1;
4141 end_chunk = simple_strtoul(buf, &end, 0);
4142 if (buf == end) break;
4143 }
4144 if (*end && !isspace(*end)) break;
4145 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004146 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004147 }
4148 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4149out:
NeilBrown67918752014-12-15 12:57:01 +11004150 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004151 return len;
4152}
4153
4154static struct md_sysfs_entry md_bitmap =
4155__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4156
4157static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004158size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004159{
Andre Noll58c0fed2009-03-31 14:33:13 +11004160 return sprintf(page, "%llu\n",
4161 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004162}
4163
NeilBrownfd01b882011-10-11 16:47:53 +11004164static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004165
4166static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004167size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004168{
4169 /* If array is inactive, we can reduce the component size, but
4170 * not increase it (except from 0).
4171 * If array is active, we can try an on-line resize
4172 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004173 sector_t sectors;
4174 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004175
Andre Noll58c0fed2009-03-31 14:33:13 +11004176 if (err < 0)
4177 return err;
NeilBrown67918752014-12-15 12:57:01 +11004178 err = mddev_lock(mddev);
4179 if (err)
4180 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004181 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004182 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004183 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004184 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004185 if (mddev->dev_sectors == 0 ||
4186 mddev->dev_sectors > sectors)
4187 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004188 else
4189 err = -ENOSPC;
4190 }
NeilBrown67918752014-12-15 12:57:01 +11004191 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004192 return err ? err : len;
4193}
4194
4195static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004196__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004197
Masanari Iida83f0d772012-10-30 00:18:08 +09004198/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004199 * This is one of
4200 * 'none' for arrays with no metadata (good luck...)
4201 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004202 * or N.M for internally known formats
4203 */
4204static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004205metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004206{
4207 if (mddev->persistent)
4208 return sprintf(page, "%d.%d\n",
4209 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004210 else if (mddev->external)
4211 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004212 else
4213 return sprintf(page, "none\n");
4214}
4215
4216static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004217metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004218{
4219 int major, minor;
4220 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004221 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004222 /* Changing the details of 'external' metadata is
4223 * always permitted. Otherwise there must be
4224 * no devices attached to the array.
4225 */
NeilBrown67918752014-12-15 12:57:01 +11004226
4227 err = mddev_lock(mddev);
4228 if (err)
4229 return err;
4230 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004231 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4232 ;
4233 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004234 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004235
NeilBrown67918752014-12-15 12:57:01 +11004236 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004237 if (cmd_match(buf, "none")) {
4238 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004239 mddev->external = 0;
4240 mddev->major_version = 0;
4241 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004242 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004243 }
4244 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004245 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004246 if (namelen >= sizeof(mddev->metadata_type))
4247 namelen = sizeof(mddev->metadata_type)-1;
4248 strncpy(mddev->metadata_type, buf+9, namelen);
4249 mddev->metadata_type[namelen] = 0;
4250 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4251 mddev->metadata_type[--namelen] = 0;
4252 mddev->persistent = 0;
4253 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004254 mddev->major_version = 0;
4255 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004256 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004257 }
4258 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004259 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004260 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004261 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004262 buf = e+1;
4263 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004264 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004265 goto out_unlock;
4266 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004267 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004268 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004269 mddev->major_version = major;
4270 mddev->minor_version = minor;
4271 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004272 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004273 err = 0;
4274out_unlock:
4275 mddev_unlock(mddev);
4276 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004277}
4278
4279static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004280__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004281
NeilBrowna35b0d62006-01-06 00:20:49 -08004282static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004283action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004284{
NeilBrown7eec3142005-11-08 21:39:44 -08004285 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004286 unsigned long recovery = mddev->recovery;
4287 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004288 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004289 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4290 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4291 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004292 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004293 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4294 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004295 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004296 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004297 type = "check";
4298 else
4299 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004300 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004301 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004302 else if (mddev->reshape_position != MaxSector)
4303 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004304 }
4305 return sprintf(page, "%s\n", type);
4306}
4307
4308static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004309action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004310{
NeilBrown7eec3142005-11-08 21:39:44 -08004311 if (!mddev->pers || !mddev->pers->sync_request)
4312 return -EINVAL;
4313
NeilBrownb6a9ce62009-05-26 09:41:17 +10004314
4315 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004316 if (cmd_match(page, "frozen"))
4317 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4318 else
4319 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004320 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4321 mddev_lock(mddev) == 0) {
4322 flush_workqueue(md_misc_wq);
4323 if (mddev->sync_thread) {
4324 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004325 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004326 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004327 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004328 }
NeilBrown312045e2015-12-21 11:01:21 +11004329 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004330 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004331 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004332 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004333 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004334 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004335 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004336 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004337 int err;
4338 if (mddev->pers->start_reshape == NULL)
4339 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004340 err = mddev_lock(mddev);
4341 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004342 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4343 err = -EBUSY;
4344 else {
4345 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4346 err = mddev->pers->start_reshape(mddev);
4347 }
NeilBrown67918752014-12-15 12:57:01 +11004348 mddev_unlock(mddev);
4349 }
NeilBrown16484bf2006-03-27 01:18:13 -08004350 if (err)
4351 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004352 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004353 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004354 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004355 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004356 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004357 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004358 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004359 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4360 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004361 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004362 if (mddev->ro == 2) {
4363 /* A write to sync_action is enough to justify
4364 * canceling read-auto mode
4365 */
4366 mddev->ro = 0;
4367 md_wakeup_thread(mddev->sync_thread);
4368 }
NeilBrown03c902e2006-01-06 00:20:46 -08004369 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004370 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004371 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004372 return len;
4373}
4374
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004375static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004376__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004377
4378static ssize_t
4379last_sync_action_show(struct mddev *mddev, char *page)
4380{
4381 return sprintf(page, "%s\n", mddev->last_sync_action);
4382}
4383
4384static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4385
NeilBrown9d888832005-11-08 21:39:26 -08004386static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004387mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004388{
4389 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004390 (unsigned long long)
4391 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004392}
4393
NeilBrown80ca3a42006-07-10 04:44:18 -07004394static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004395
NeilBrown88202a02006-01-06 00:21:36 -08004396static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004397sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004398{
4399 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4400 mddev->sync_speed_min ? "local": "system");
4401}
4402
4403static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004404sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004405{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004406 unsigned int min;
4407 int rv;
4408
NeilBrown88202a02006-01-06 00:21:36 -08004409 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004410 min = 0;
4411 } else {
4412 rv = kstrtouint(buf, 10, &min);
4413 if (rv < 0)
4414 return rv;
4415 if (min == 0)
4416 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004417 }
NeilBrown88202a02006-01-06 00:21:36 -08004418 mddev->sync_speed_min = min;
4419 return len;
4420}
4421
4422static struct md_sysfs_entry md_sync_min =
4423__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4424
4425static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004426sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004427{
4428 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4429 mddev->sync_speed_max ? "local": "system");
4430}
4431
4432static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004433sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004434{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004435 unsigned int max;
4436 int rv;
4437
NeilBrown88202a02006-01-06 00:21:36 -08004438 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004439 max = 0;
4440 } else {
4441 rv = kstrtouint(buf, 10, &max);
4442 if (rv < 0)
4443 return rv;
4444 if (max == 0)
4445 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004446 }
NeilBrown88202a02006-01-06 00:21:36 -08004447 mddev->sync_speed_max = max;
4448 return len;
4449}
4450
4451static struct md_sysfs_entry md_sync_max =
4452__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4453
Iustin Popd7f3d292007-10-16 23:30:54 -07004454static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004455degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004456{
4457 return sprintf(page, "%d\n", mddev->degraded);
4458}
4459static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004460
4461static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004462sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004463{
4464 return sprintf(page, "%d\n", mddev->parallel_resync);
4465}
4466
4467static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004468sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004469{
4470 long n;
4471
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004472 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004473 return -EINVAL;
4474
4475 if (n != 0 && n != 1)
4476 return -EINVAL;
4477
4478 mddev->parallel_resync = n;
4479
4480 if (mddev->sync_thread)
4481 wake_up(&resync_wait);
4482
4483 return len;
4484}
4485
4486/* force parallel resync, even with shared block devices */
4487static struct md_sysfs_entry md_sync_force_parallel =
4488__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4489 sync_force_parallel_show, sync_force_parallel_store);
4490
4491static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004492sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004493{
4494 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004495 if (mddev->curr_resync == 0)
4496 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004497 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4498 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004499 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004500 db = resync - mddev->resync_mark_cnt;
4501 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004502}
4503
NeilBrown80ca3a42006-07-10 04:44:18 -07004504static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004505
4506static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004507sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004508{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004509 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004510
NeilBrownacb180b2009-04-14 16:28:34 +10004511 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4512 return sprintf(page, "none\n");
4513
NeilBrown72f36d52012-10-11 14:25:57 +11004514 if (mddev->curr_resync == 1 ||
4515 mddev->curr_resync == 2)
4516 return sprintf(page, "delayed\n");
4517
NeilBrownc804cde2012-05-21 09:28:33 +10004518 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4519 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004520 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004521 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004522 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004523
NeilBrownacb180b2009-04-14 16:28:34 +10004524 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004525 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004526}
4527
NeilBrown750f1992014-09-30 08:53:05 +10004528static struct md_sysfs_entry md_sync_completed =
4529 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004530
NeilBrowne464eaf2006-03-27 01:18:14 -08004531static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004532min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004533{
4534 return sprintf(page, "%llu\n",
4535 (unsigned long long)mddev->resync_min);
4536}
4537static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004538min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004539{
4540 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004541 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004542
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004543 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004544 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004545
4546 spin_lock(&mddev->lock);
4547 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004548 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004549 goto out_unlock;
4550
4551 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004552 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004553 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004554
NeilBrown50c37b12015-03-23 17:36:38 +11004555 /* Round down to multiple of 4K for safety */
4556 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004557 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004558
NeilBrown23da4222014-12-15 12:57:01 +11004559out_unlock:
4560 spin_unlock(&mddev->lock);
4561 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004562}
4563
4564static struct md_sysfs_entry md_min_sync =
4565__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4566
4567static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004568max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004569{
4570 if (mddev->resync_max == MaxSector)
4571 return sprintf(page, "max\n");
4572 else
4573 return sprintf(page, "%llu\n",
4574 (unsigned long long)mddev->resync_max);
4575}
4576static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004577max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004578{
NeilBrown23da4222014-12-15 12:57:01 +11004579 int err;
4580 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004581 if (strncmp(buf, "max", 3) == 0)
4582 mddev->resync_max = MaxSector;
4583 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004584 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004585 int chunk;
4586
4587 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004588 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004589 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004590 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004591 goto out_unlock;
4592
4593 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004594 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004595 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004596 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004597 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004598
4599 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004600 chunk = mddev->chunk_sectors;
4601 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004602 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004603
4604 err = -EINVAL;
4605 if (sector_div(temp, chunk))
4606 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004607 }
4608 mddev->resync_max = max;
4609 }
4610 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004611 err = 0;
4612out_unlock:
4613 spin_unlock(&mddev->lock);
4614 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004615}
4616
4617static struct md_sysfs_entry md_max_sync =
4618__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4619
4620static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004621suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004622{
4623 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4624}
4625
4626static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004627suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004628{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004629 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004630 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004631
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004632 err = kstrtoull(buf, 10, &new);
4633 if (err < 0)
4634 return err;
4635 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004636 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004637
NeilBrown67918752014-12-15 12:57:01 +11004638 err = mddev_lock(mddev);
4639 if (err)
4640 return err;
4641 err = -EINVAL;
4642 if (mddev->pers == NULL ||
4643 mddev->pers->quiesce == NULL)
4644 goto unlock;
4645 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004646 mddev->suspend_lo = new;
4647 if (new >= old)
4648 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004649 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004650 else {
4651 /* Expanding suspended region - need to wait */
4652 mddev->pers->quiesce(mddev, 1);
4653 mddev->pers->quiesce(mddev, 0);
4654 }
NeilBrown67918752014-12-15 12:57:01 +11004655 err = 0;
4656unlock:
4657 mddev_unlock(mddev);
4658 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004659}
4660static struct md_sysfs_entry md_suspend_lo =
4661__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4662
NeilBrowne464eaf2006-03-27 01:18:14 -08004663static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004664suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004665{
4666 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4667}
4668
4669static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004670suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004671{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004672 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004673 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004674
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004675 err = kstrtoull(buf, 10, &new);
4676 if (err < 0)
4677 return err;
4678 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004679 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004680
NeilBrown67918752014-12-15 12:57:01 +11004681 err = mddev_lock(mddev);
4682 if (err)
4683 return err;
4684 err = -EINVAL;
4685 if (mddev->pers == NULL ||
4686 mddev->pers->quiesce == NULL)
4687 goto unlock;
4688 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004689 mddev->suspend_hi = new;
4690 if (new <= old)
4691 /* Shrinking suspended region */
4692 mddev->pers->quiesce(mddev, 2);
4693 else {
4694 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004695 mddev->pers->quiesce(mddev, 1);
4696 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004697 }
NeilBrown67918752014-12-15 12:57:01 +11004698 err = 0;
4699unlock:
4700 mddev_unlock(mddev);
4701 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004702}
4703static struct md_sysfs_entry md_suspend_hi =
4704__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4705
NeilBrown08a02ec2007-05-09 02:35:38 -07004706static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004707reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004708{
4709 if (mddev->reshape_position != MaxSector)
4710 return sprintf(page, "%llu\n",
4711 (unsigned long long)mddev->reshape_position);
4712 strcpy(page, "none\n");
4713 return 5;
4714}
4715
4716static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004717reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004718{
NeilBrownc6563a82012-05-21 09:27:00 +10004719 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004720 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004721 int err;
NeilBrown67918752014-12-15 12:57:01 +11004722
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004723 err = kstrtoull(buf, 10, &new);
4724 if (err < 0)
4725 return err;
4726 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004727 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004728 err = mddev_lock(mddev);
4729 if (err)
4730 return err;
4731 err = -EBUSY;
4732 if (mddev->pers)
4733 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004734 mddev->reshape_position = new;
4735 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004736 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004737 mddev->new_level = mddev->level;
4738 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004739 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004740 rdev_for_each(rdev, mddev)
4741 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004742 err = 0;
4743unlock:
4744 mddev_unlock(mddev);
4745 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004746}
4747
4748static struct md_sysfs_entry md_reshape_position =
4749__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4750 reshape_position_store);
4751
Dan Williamsb522adc2009-03-31 15:00:31 +11004752static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004753reshape_direction_show(struct mddev *mddev, char *page)
4754{
4755 return sprintf(page, "%s\n",
4756 mddev->reshape_backwards ? "backwards" : "forwards");
4757}
4758
4759static ssize_t
4760reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4761{
4762 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004763 int err;
4764
NeilBrown2c810cd2012-05-21 09:27:00 +10004765 if (cmd_match(buf, "forwards"))
4766 backwards = 0;
4767 else if (cmd_match(buf, "backwards"))
4768 backwards = 1;
4769 else
4770 return -EINVAL;
4771 if (mddev->reshape_backwards == backwards)
4772 return len;
4773
NeilBrown67918752014-12-15 12:57:01 +11004774 err = mddev_lock(mddev);
4775 if (err)
4776 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004777 /* check if we are allowed to change */
4778 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004779 err = -EBUSY;
4780 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004781 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004782 err = -EINVAL;
4783 else
4784 mddev->reshape_backwards = backwards;
4785 mddev_unlock(mddev);
4786 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004787}
4788
4789static struct md_sysfs_entry md_reshape_direction =
4790__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4791 reshape_direction_store);
4792
4793static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004794array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004795{
4796 if (mddev->external_size)
4797 return sprintf(page, "%llu\n",
4798 (unsigned long long)mddev->array_sectors/2);
4799 else
4800 return sprintf(page, "default\n");
4801}
4802
4803static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004804array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004805{
4806 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004807 int err;
4808
4809 err = mddev_lock(mddev);
4810 if (err)
4811 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004812
4813 if (strncmp(buf, "default", 7) == 0) {
4814 if (mddev->pers)
4815 sectors = mddev->pers->size(mddev, 0, 0);
4816 else
4817 sectors = mddev->array_sectors;
4818
4819 mddev->external_size = 0;
4820 } else {
4821 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004822 err = -EINVAL;
4823 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4824 err = -E2BIG;
4825 else
4826 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004827 }
4828
NeilBrown67918752014-12-15 12:57:01 +11004829 if (!err) {
4830 mddev->array_sectors = sectors;
4831 if (mddev->pers) {
4832 set_capacity(mddev->gendisk, mddev->array_sectors);
4833 revalidate_disk(mddev->gendisk);
4834 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004835 }
NeilBrown67918752014-12-15 12:57:01 +11004836 mddev_unlock(mddev);
4837 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004838}
4839
4840static struct md_sysfs_entry md_array_size =
4841__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4842 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004843
NeilBrowneae17012005-11-08 21:39:23 -08004844static struct attribute *md_default_attrs[] = {
4845 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004846 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004847 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004848 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004849 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004850 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004851 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004852 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004853 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004854 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004855 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004856 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004857 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004858 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004859 NULL,
4860};
4861
4862static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004863 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004864 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004865 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004866 &md_sync_min.attr,
4867 &md_sync_max.attr,
4868 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004869 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004870 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004871 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004872 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004873 &md_suspend_lo.attr,
4874 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004875 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004876 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004877 NULL,
4878};
NeilBrown411036f2005-11-08 21:39:40 -08004879static struct attribute_group md_redundancy_group = {
4880 .name = NULL,
4881 .attrs = md_redundancy_attrs,
4882};
4883
NeilBrowneae17012005-11-08 21:39:23 -08004884static ssize_t
4885md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4886{
4887 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004888 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004889 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004890
4891 if (!entry->show)
4892 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004893 spin_lock(&all_mddevs_lock);
4894 if (list_empty(&mddev->all_mddevs)) {
4895 spin_unlock(&all_mddevs_lock);
4896 return -EBUSY;
4897 }
4898 mddev_get(mddev);
4899 spin_unlock(&all_mddevs_lock);
4900
NeilBrownb7b17c92014-12-15 12:56:59 +11004901 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004902 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004903 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004904}
4905
4906static ssize_t
4907md_attr_store(struct kobject *kobj, struct attribute *attr,
4908 const char *page, size_t length)
4909{
4910 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004911 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004912 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004913
4914 if (!entry->store)
4915 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004916 if (!capable(CAP_SYS_ADMIN))
4917 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004918 spin_lock(&all_mddevs_lock);
4919 if (list_empty(&mddev->all_mddevs)) {
4920 spin_unlock(&all_mddevs_lock);
4921 return -EBUSY;
4922 }
4923 mddev_get(mddev);
4924 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004925 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004926 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004927 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004928}
4929
4930static void md_free(struct kobject *ko)
4931{
NeilBrownfd01b882011-10-11 16:47:53 +11004932 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004933
4934 if (mddev->sysfs_state)
4935 sysfs_put(mddev->sysfs_state);
4936
NeilBrown6cd18e72015-04-27 14:12:22 +10004937 if (mddev->queue)
4938 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004939 if (mddev->gendisk) {
4940 del_gendisk(mddev->gendisk);
4941 put_disk(mddev->gendisk);
4942 }
NeilBrowna21d1502009-01-09 08:31:09 +11004943
NeilBrowneae17012005-11-08 21:39:23 -08004944 kfree(mddev);
4945}
4946
Emese Revfy52cf25d2010-01-19 02:58:23 +01004947static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004948 .show = md_attr_show,
4949 .store = md_attr_store,
4950};
4951static struct kobj_type md_ktype = {
4952 .release = md_free,
4953 .sysfs_ops = &md_sysfs_ops,
4954 .default_attrs = md_default_attrs,
4955};
4956
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957int mdp_major = 0;
4958
Dan Williams5fd3a172009-03-04 00:57:25 -07004959static void mddev_delayed_delete(struct work_struct *ws)
4960{
NeilBrownfd01b882011-10-11 16:47:53 +11004961 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004962
NeilBrown43a70502009-12-14 12:49:55 +11004963 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004964 kobject_del(&mddev->kobj);
4965 kobject_put(&mddev->kobj);
4966}
4967
NeilBrownefeb53c2009-01-09 08:31:10 +11004968static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004970 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004971 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004973 int partitioned;
4974 int shift;
4975 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004976 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
4978 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004979 return -ENODEV;
4980
4981 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4982 shift = partitioned ? MdpMinorShift : 0;
4983 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984
Tejun Heoe804ac72010-10-15 15:36:08 +02004985 /* wait for any previous instance of this device to be
4986 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004987 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004988 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004989
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004990 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004991 error = -EEXIST;
4992 if (mddev->gendisk)
4993 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004994
4995 if (name) {
4996 /* Need to ensure that 'name' is not a duplicate.
4997 */
NeilBrownfd01b882011-10-11 16:47:53 +11004998 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004999 spin_lock(&all_mddevs_lock);
5000
5001 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5002 if (mddev2->gendisk &&
5003 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5004 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005005 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005006 }
5007 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 }
NeilBrown8b765392009-01-09 08:31:08 +11005009
NeilBrown0909dc42009-07-01 12:27:21 +10005010 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005011 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005012 if (!mddev->queue)
5013 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005014 mddev->queue->queuedata = mddev;
5015
NeilBrown409c57f2009-03-31 14:39:39 +11005016 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005017 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005018
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 disk = alloc_disk(1 << shift);
5020 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005021 blk_cleanup_queue(mddev->queue);
5022 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005023 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005025 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005027 if (name)
5028 strcpy(disk->disk_name, name);
5029 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07005031 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 disk->fops = &md_fops;
5034 disk->private_data = mddev;
5035 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10005036 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11005037 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005038 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005039 * remove it now.
5040 */
5041 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005043 /* As soon as we call add_disk(), another thread could get
5044 * through to md_open, so make sure it doesn't get too far
5045 */
5046 mutex_lock(&mddev->open_mutex);
5047 add_disk(disk);
5048
Tejun Heoed9e1982008-08-25 19:56:05 +09005049 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5050 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005051 if (error) {
5052 /* This isn't possible, but as kobject_init_and_add is marked
5053 * __must_check, we must do something with the result
5054 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08005055 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
5056 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005057 error = 0;
5058 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005059 if (mddev->kobj.sd &&
5060 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11005061 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005062 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005063 abort:
5064 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005065 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005066 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005067 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005068 }
NeilBrownd3374822009-01-09 08:31:10 +11005069 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005070 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005071}
5072
5073static struct kobject *md_probe(dev_t dev, int *part, void *data)
5074{
5075 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 return NULL;
5077}
5078
NeilBrownefeb53c2009-01-09 08:31:10 +11005079static int add_named_array(const char *val, struct kernel_param *kp)
5080{
5081 /* val must be "md_*" where * is not all digits.
5082 * We allocate an array with a large free minor number, and
5083 * set the name to val. val must not already be an active name.
5084 */
5085 int len = strlen(val);
5086 char buf[DISK_NAME_LEN];
5087
5088 while (len && val[len-1] == '\n')
5089 len--;
5090 if (len >= DISK_NAME_LEN)
5091 return -E2BIG;
5092 strlcpy(buf, val, len+1);
5093 if (strncmp(buf, "md_", 3) != 0)
5094 return -EINVAL;
5095 return md_alloc(0, buf);
5096}
5097
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098static void md_safemode_timeout(unsigned long data)
5099{
NeilBrownfd01b882011-10-11 16:47:53 +11005100 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
Neil Brown0fd62b82008-06-28 08:31:36 +10005102 if (!atomic_read(&mddev->writes_pending)) {
5103 mddev->safemode = 1;
5104 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10005105 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 md_wakeup_thread(mddev->thread);
5108}
5109
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005110static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
NeilBrownfd01b882011-10-11 16:47:53 +11005112int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113{
NeilBrown2604b702006-01-06 00:20:36 -08005114 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005115 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005116 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117
NeilBrowna757e642005-04-16 15:26:42 -07005118 if (list_empty(&mddev->disks))
5119 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121
5122 if (mddev->pers)
5123 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005124 /* Cannot run until previous stop completes properly */
5125 if (mddev->sysfs_active)
5126 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005127
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 /*
5129 * Analyze all RAID superblock(s)
5130 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005131 if (!mddev->raid_disks) {
5132 if (!mddev->persistent)
5133 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005134 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
NeilBrownd9d166c2006-01-06 00:20:51 -08005137 if (mddev->level != LEVEL_NONE)
5138 request_module("md-level-%d", mddev->level);
5139 else if (mddev->clevel[0])
5140 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
5142 /*
5143 * Drop all container device buffers, from now on
5144 * the only valid external interface is through the md
5145 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005147 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005148 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 continue;
5150 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005151 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005152
5153 /* perform some consistency tests on the device.
5154 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005155 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005156 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005157 if (rdev->meta_bdev) {
5158 /* Nothing to check */;
5159 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005160 if (mddev->dev_sectors &&
5161 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005162 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005163 printk("md: %s: data overlaps metadata\n",
5164 mdname(mddev));
5165 return -EINVAL;
5166 }
5167 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005168 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005169 > rdev->data_offset) {
5170 printk("md: %s: metadata overlaps data\n",
5171 mdname(mddev));
5172 return -EINVAL;
5173 }
5174 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005175 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 }
5177
NeilBrowna167f662010-10-26 18:31:13 +11005178 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005179 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005180
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005182 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005183 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005185 if (mddev->level != LEVEL_NONE)
5186 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5187 mddev->level);
5188 else
5189 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5190 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 return -EINVAL;
5192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005194 if (mddev->level != pers->level) {
5195 mddev->level = pers->level;
5196 mddev->new_level = pers->level;
5197 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005198 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199
NeilBrownf6705572006-03-27 01:18:11 -08005200 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005201 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005202 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005203 module_put(pers->owner);
5204 return -EINVAL;
5205 }
5206
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005207 if (pers->sync_request) {
5208 /* Warn if this is a potentially silly
5209 * configuration.
5210 */
5211 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005212 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005213 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005214
NeilBrowndafb20f2012-03-19 12:46:39 +11005215 rdev_for_each(rdev, mddev)
5216 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005217 if (rdev < rdev2 &&
5218 rdev->bdev->bd_contains ==
5219 rdev2->bdev->bd_contains) {
5220 printk(KERN_WARNING
5221 "%s: WARNING: %s appears to be"
5222 " on the same physical disk as"
5223 " %s.\n",
5224 mdname(mddev),
5225 bdevname(rdev->bdev,b),
5226 bdevname(rdev2->bdev,b2));
5227 warned = 1;
5228 }
5229 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005230
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005231 if (warned)
5232 printk(KERN_WARNING
5233 "True protection against single-disk"
5234 " failure might be compromised.\n");
5235 }
5236
NeilBrown657390d2005-08-26 18:34:16 -07005237 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005238 /* may be over-ridden by personality */
5239 mddev->resync_max_sectors = mddev->dev_sectors;
5240
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005241 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242
NeilBrown0f9552b2009-12-30 12:08:50 +11005243 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005244 mddev->ro = 2; /* read-only, but switch on first write */
5245
NeilBrown36d091f2014-12-15 12:56:58 +11005246 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005247 if (err)
5248 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005249 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005250 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5251 " but 'external_size' not in effect?\n", __func__);
5252 printk(KERN_ERR
5253 "md: invalid array_size %llu > default size %llu\n",
5254 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005255 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005256 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005257 }
NeilBrown36d091f2014-12-15 12:56:58 +11005258 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005259 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005260 struct bitmap *bitmap;
5261
5262 bitmap = bitmap_create(mddev, -1);
5263 if (IS_ERR(bitmap)) {
5264 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005265 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5266 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005267 } else
5268 mddev->bitmap = bitmap;
5269
NeilBrownb15c2e52006-01-06 00:20:16 -08005270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005272 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005273 if (mddev->private)
5274 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005275 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005276 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005277 bitmap_destroy(mddev);
5278 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 }
NeilBrown5c675f82014-12-15 12:56:56 +11005280 if (mddev->queue) {
5281 mddev->queue->backing_dev_info.congested_data = mddev;
5282 mddev->queue->backing_dev_info.congested_fn = md_congested;
5283 }
NeilBrown36d091f2014-12-15 12:56:58 +11005284 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005285 if (mddev->kobj.sd &&
5286 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005287 printk(KERN_WARNING
5288 "md: cannot register extra attributes for %s\n",
5289 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005290 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005291 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005292 mddev->ro = 0;
5293
NeilBrownf72ffdd2014-09-30 14:23:59 +10005294 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005295 atomic_set(&mddev->max_corr_read_errors,
5296 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005298 if (mddev_is_clustered(mddev))
5299 mddev->safemode_delay = 0;
5300 else
5301 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005303 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005304 spin_lock(&mddev->lock);
5305 mddev->pers = pers;
NeilBrown0ca69882011-01-14 09:14:33 +11005306 mddev->ready = 1;
NeilBrown36d091f2014-12-15 12:56:58 +11005307 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005308 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005309 if (rdev->raid_disk >= 0)
5310 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005311 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005312
NeilBrowna4a3d262015-07-17 11:57:30 +10005313 if (mddev->degraded && !mddev->ro)
5314 /* This ensures that recovering status is reported immediately
5315 * via sysfs - until a lack of spares is confirmed.
5316 */
5317 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005319
NeilBrown7a0a5352013-08-27 16:28:23 +10005320 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07005321 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322
NeilBrownd7603b72006-01-06 00:20:30 -08005323 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005324 sysfs_notify_dirent_safe(mddev->sysfs_state);
5325 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005326 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 return 0;
5328}
NeilBrown390ee602010-06-01 19:37:27 +10005329EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330
NeilBrownfd01b882011-10-11 16:47:53 +11005331static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005332{
5333 int err;
5334
5335 err = md_run(mddev);
5336 if (err)
5337 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005338 err = bitmap_load(mddev);
5339 if (err) {
5340 bitmap_destroy(mddev);
5341 goto out;
5342 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005343
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005344 if (mddev_is_clustered(mddev))
5345 md_allow_write(mddev);
5346
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005347 md_wakeup_thread(mddev->thread);
5348 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5349
NeilBrownfe60b012010-03-29 11:10:42 +11005350 set_capacity(mddev->gendisk, mddev->array_sectors);
5351 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005352 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005353 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5354out:
5355 return err;
5356}
5357
NeilBrownfd01b882011-10-11 16:47:53 +11005358static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359{
5360 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
Andre Noll80fab1d2008-07-11 22:02:21 +10005362 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005364 return -ENXIO;
5365 if (!mddev->pers)
5366 return -EINVAL;
5367 if (!mddev->ro)
5368 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005369 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5370 struct md_rdev *rdev;
5371 bool has_journal = false;
5372
5373 rcu_read_lock();
5374 rdev_for_each_rcu(rdev, mddev) {
5375 if (test_bit(Journal, &rdev->flags) &&
5376 !test_bit(Faulty, &rdev->flags)) {
5377 has_journal = true;
5378 break;
5379 }
5380 }
5381 rcu_read_unlock();
5382
5383 /* Don't restart rw with journal missing/faulty */
5384 if (!has_journal)
5385 return -EINVAL;
5386 }
5387
Andre Noll80fab1d2008-07-11 22:02:21 +10005388 mddev->safemode = 0;
5389 mddev->ro = 0;
5390 set_disk_ro(disk, 0);
5391 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5392 mdname(mddev));
5393 /* Kick recovery or resync if necessary */
5394 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5395 md_wakeup_thread(mddev->thread);
5396 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005397 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399}
5400
NeilBrownfd01b882011-10-11 16:47:53 +11005401static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005402{
5403 mddev->array_sectors = 0;
5404 mddev->external_size = 0;
5405 mddev->dev_sectors = 0;
5406 mddev->raid_disks = 0;
5407 mddev->recovery_cp = 0;
5408 mddev->resync_min = 0;
5409 mddev->resync_max = MaxSector;
5410 mddev->reshape_position = MaxSector;
5411 mddev->external = 0;
5412 mddev->persistent = 0;
5413 mddev->level = LEVEL_NONE;
5414 mddev->clevel[0] = 0;
5415 mddev->flags = 0;
5416 mddev->ro = 0;
5417 mddev->metadata_type[0] = 0;
5418 mddev->chunk_sectors = 0;
5419 mddev->ctime = mddev->utime = 0;
5420 mddev->layout = 0;
5421 mddev->max_disks = 0;
5422 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005423 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005424 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005425 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005426 mddev->new_level = LEVEL_NONE;
5427 mddev->new_layout = 0;
5428 mddev->new_chunk_sectors = 0;
5429 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005430 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005431 mddev->suspend_lo = mddev->suspend_hi = 0;
5432 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5433 mddev->recovery = 0;
5434 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005435 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005436 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005437 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005438 mddev->private = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005439 mddev->bitmap_info.offset = 0;
5440 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005441 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005442 mddev->bitmap_info.chunksize = 0;
5443 mddev->bitmap_info.daemon_sleep = 0;
5444 mddev->bitmap_info.max_write_behind = 0;
5445}
5446
NeilBrownfd01b882011-10-11 16:47:53 +11005447static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005448{
NeilBrown6b6204e2013-05-09 09:48:30 +10005449 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005450 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005451 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005452 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005453 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005454 }
5455
5456 del_timer_sync(&mddev->safemode_timer);
5457
5458 bitmap_flush(mddev);
5459 md_super_wait(mddev);
5460
NeilBrownb6d428c2013-04-24 11:42:42 +10005461 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005462 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5463 (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005464 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005465 if (!mddev_is_clustered(mddev))
5466 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005467 md_update_sb(mddev, 1);
5468 }
5469}
NeilBrowndefad612011-01-14 09:14:33 +11005470
NeilBrownfd01b882011-10-11 16:47:53 +11005471void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005472{
NeilBrown29f097c2013-11-14 17:54:51 +11005473 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005474 __md_stop_writes(mddev);
5475 mddev_unlock(mddev);
5476}
NeilBrown390ee602010-06-01 19:37:27 +10005477EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005478
NeilBrown5aa61f42014-12-15 12:56:57 +11005479static void mddev_detach(struct mddev *mddev)
5480{
5481 struct bitmap *bitmap = mddev->bitmap;
5482 /* wait for behind writes to complete */
5483 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5484 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5485 mdname(mddev));
5486 /* need to kick something here to make sure I/O goes? */
5487 wait_event(bitmap->behind_wait,
5488 atomic_read(&bitmap->behind_writes) == 0);
5489 }
NeilBrown36d091f2014-12-15 12:56:58 +11005490 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005491 mddev->pers->quiesce(mddev, 1);
5492 mddev->pers->quiesce(mddev, 0);
5493 }
5494 md_unregister_thread(&mddev->thread);
5495 if (mddev->queue)
5496 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5497}
5498
NeilBrown5eff3c42012-11-19 10:47:48 +11005499static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005500{
NeilBrown36d091f2014-12-15 12:56:58 +11005501 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005502 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005503 /* Ensure ->event_work is done */
5504 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005505 spin_lock(&mddev->lock);
5506 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005507 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005508 spin_unlock(&mddev->lock);
5509 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005510 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005511 if (pers->sync_request && mddev->to_remove == NULL)
5512 mddev->to_remove = &md_redundancy_group;
5513 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005514 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005515}
NeilBrown5eff3c42012-11-19 10:47:48 +11005516
5517void md_stop(struct mddev *mddev)
5518{
5519 /* stop the array and free an attached data structures.
5520 * This is called from dm-raid
5521 */
5522 __md_stop(mddev);
5523 bitmap_destroy(mddev);
5524 if (mddev->bio_set)
5525 bioset_free(mddev->bio_set);
5526}
5527
NeilBrown390ee602010-06-01 19:37:27 +10005528EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005529
NeilBrowna05b7ea02012-07-19 15:59:18 +10005530static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005531{
5532 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005533 int did_freeze = 0;
5534
5535 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5536 did_freeze = 1;
5537 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5538 md_wakeup_thread(mddev->thread);
5539 }
NeilBrownf851b602014-12-11 10:02:10 +11005540 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005541 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005542 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005543 /* Thread might be blocked waiting for metadata update
5544 * which will now never happen */
5545 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005546
NeilBrown88724bf2015-09-24 14:00:51 +10005547 if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags))
5548 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005549 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005550 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5551 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005552 wait_event(mddev->sb_wait,
5553 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005554 mddev_lock_nointr(mddev);
5555
NeilBrowna4bd82d2010-03-29 13:23:10 +11005556 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005557 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005558 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005559 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005560 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005561 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005562 if (did_freeze) {
5563 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005564 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005565 md_wakeup_thread(mddev->thread);
5566 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005567 err = -EBUSY;
5568 goto out;
5569 }
5570 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005571 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005572
5573 err = -ENXIO;
5574 if (mddev->ro==1)
5575 goto out;
5576 mddev->ro = 1;
5577 set_disk_ro(mddev->gendisk, 1);
5578 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005579 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5580 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005581 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005582 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005583 }
5584out:
5585 mutex_unlock(&mddev->open_mutex);
5586 return err;
5587}
5588
NeilBrown9e653b62006-06-26 00:27:58 -07005589/* mode:
5590 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005591 * 2 - stop but do not disassemble array
5592 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005593static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea02012-07-19 15:59:18 +10005594 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005597 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005598 int did_freeze = 0;
5599
5600 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5601 did_freeze = 1;
5602 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5603 md_wakeup_thread(mddev->thread);
5604 }
NeilBrownf851b602014-12-11 10:02:10 +11005605 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005606 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005607 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005608 /* Thread might be blocked waiting for metadata update
5609 * which will now never happen */
5610 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005611
NeilBrown30b8feb2013-11-14 15:16:17 +11005612 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005613 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5614 !test_bit(MD_RECOVERY_RUNNING,
5615 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005616 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
NeilBrownc8c00a62009-08-10 12:50:52 +10005618 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005619 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005620 mddev->sysfs_active ||
5621 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005622 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005623 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005624 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005625 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005626 if (did_freeze) {
5627 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005628 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005629 md_wakeup_thread(mddev->thread);
5630 }
NeilBrown260fa032013-08-27 16:44:13 +10005631 return -EBUSY;
5632 }
NeilBrown6e17b022010-08-07 21:41:19 +10005633 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005634 if (mddev->ro)
5635 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005636
NeilBrowndefad612011-01-14 09:14:33 +11005637 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005638 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005639 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005640
NeilBrowna4bd82d2010-03-29 13:23:10 +11005641 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005642 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005643
NeilBrowndafb20f2012-03-19 12:46:39 +11005644 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005645 if (rdev->raid_disk >= 0)
5646 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005647
NeilBrowna4bd82d2010-03-29 13:23:10 +11005648 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005649 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005650 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005651 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005652
NeilBrowna4bd82d2010-03-29 13:23:10 +11005653 if (mddev->ro)
5654 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005655 } else
5656 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 /*
5658 * Free resources if final stop
5659 */
NeilBrown9e653b62006-06-26 00:27:58 -07005660 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5662
NeilBrown978f9462006-02-02 14:28:05 -08005663 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005664 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005665 struct file *f = mddev->bitmap_info.file;
5666 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005667 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005668 spin_unlock(&mddev->lock);
5669 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005670 }
NeilBrownc3d97142009-12-14 12:49:52 +11005671 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005672
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 export_array(mddev);
5674
NeilBrown6177b472010-03-29 11:37:13 +11005675 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005676 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005677 if (mddev->hold_active == UNTIL_STOP)
5678 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005679 }
NeilBrownd7603b72006-01-06 00:20:30 -08005680 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005681 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683}
5684
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005685#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005686static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687{
NeilBrown3cb03002011-10-11 16:45:26 +11005688 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 int err;
5690
NeilBrowna757e642005-04-16 15:26:42 -07005691 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693
5694 printk(KERN_INFO "md: running: ");
5695
NeilBrowndafb20f2012-03-19 12:46:39 +11005696 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 char b[BDEVNAME_SIZE];
5698 printk("<%s>", bdevname(rdev->bdev,b));
5699 }
5700 printk("\n");
5701
NeilBrownd710e132008-10-13 11:55:12 +11005702 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 if (err) {
5704 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005705 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 }
5707}
5708
5709/*
5710 * lets try to run arrays based on all disks that have arrived
5711 * until now. (those are in pending_raid_disks)
5712 *
5713 * the method: pick the first pending disk, collect all disks with
5714 * the same UUID, remove all from the pending list and put them into
5715 * the 'same_array' list. Then order this list based on superblock
5716 * update time (freshest comes first), kick out 'old' disks and
5717 * compare superblocks. If everything's fine then run it.
5718 *
5719 * If "unit" is allocated, then bump its reference count
5720 */
5721static void autorun_devices(int part)
5722{
NeilBrown3cb03002011-10-11 16:45:26 +11005723 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005724 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 char b[BDEVNAME_SIZE];
5726
5727 printk(KERN_INFO "md: autorun ...\n");
5728 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005729 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005731 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005733 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
5735 printk(KERN_INFO "md: considering %s ...\n",
5736 bdevname(rdev0->bdev,b));
5737 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005738 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 if (super_90_load(rdev, rdev0, 0) >= 0) {
5740 printk(KERN_INFO "md: adding %s ...\n",
5741 bdevname(rdev->bdev,b));
5742 list_move(&rdev->same_set, &candidates);
5743 }
5744 /*
5745 * now we have a set of devices, with all of them having
5746 * mostly sane superblocks. It's time to allocate the
5747 * mddev.
5748 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005749 if (part) {
5750 dev = MKDEV(mdp_major,
5751 rdev0->preferred_minor << MdpMinorShift);
5752 unit = MINOR(dev) >> MdpMinorShift;
5753 } else {
5754 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5755 unit = MINOR(dev);
5756 }
5757 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5759 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5760 break;
5761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762
5763 md_probe(dev, NULL, NULL);
5764 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005765 if (!mddev || !mddev->gendisk) {
5766 if (mddev)
5767 mddev_put(mddev);
5768 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 "md: cannot allocate memory for md drive.\n");
5770 break;
5771 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005772 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 printk(KERN_WARNING "md: %s locked, cannot run\n",
5774 mdname(mddev));
5775 else if (mddev->raid_disks || mddev->major_version
5776 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005777 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 "md: %s already running, cannot run %s\n",
5779 mdname(mddev), bdevname(rdev0->bdev,b));
5780 mddev_unlock(mddev);
5781 } else {
5782 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005783 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005784 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 list_del_init(&rdev->same_set);
5786 if (bind_rdev_to_array(rdev, mddev))
5787 export_rdev(rdev);
5788 }
5789 autorun_array(mddev);
5790 mddev_unlock(mddev);
5791 }
5792 /* on success, candidates will be empty, on error
5793 * it won't...
5794 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005795 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005796 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 mddev_put(mddev);
5800 }
5801 printk(KERN_INFO "md: ... autorun DONE.\n");
5802}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005803#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804
NeilBrownf72ffdd2014-09-30 14:23:59 +10005805static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806{
5807 mdu_version_t ver;
5808
5809 ver.major = MD_MAJOR_VERSION;
5810 ver.minor = MD_MINOR_VERSION;
5811 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5812
5813 if (copy_to_user(arg, &ver, sizeof(ver)))
5814 return -EFAULT;
5815
5816 return 0;
5817}
5818
NeilBrownf72ffdd2014-09-30 14:23:59 +10005819static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820{
5821 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005822 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005823 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824
NeilBrown1ca69c42012-10-11 13:37:33 +11005825 nr = working = insync = failed = spare = 0;
5826 rcu_read_lock();
5827 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005829 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 failed++;
5831 else {
5832 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005833 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005834 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 else
5836 spare++;
5837 }
5838 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005839 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840
5841 info.major_version = mddev->major_version;
5842 info.minor_version = mddev->minor_version;
5843 info.patch_version = MD_PATCHLEVEL_VERSION;
5844 info.ctime = mddev->ctime;
5845 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005846 info.size = mddev->dev_sectors / 2;
5847 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005848 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 info.nr_disks = nr;
5850 info.raid_disks = mddev->raid_disks;
5851 info.md_minor = mddev->md_minor;
5852 info.not_persistent= !mddev->persistent;
5853
5854 info.utime = mddev->utime;
5855 info.state = 0;
5856 if (mddev->in_sync)
5857 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005858 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005859 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005860 if (mddev_is_clustered(mddev))
5861 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005862 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 info.working_disks = working;
5864 info.failed_disks = failed;
5865 info.spare_disks = spare;
5866
5867 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005868 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869
5870 if (copy_to_user(arg, &info, sizeof(info)))
5871 return -EFAULT;
5872
5873 return 0;
5874}
5875
NeilBrownf72ffdd2014-09-30 14:23:59 +10005876static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005877{
5878 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005879 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005880 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005881
Benjamin Randazzob6878d92015-07-25 16:36:50 +02005882 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005883 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005884 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005885
NeilBrown32a76272005-06-21 17:17:14 -07005886 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005887 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02005888 /* bitmap enabled */
5889 if (mddev->bitmap_info.file) {
5890 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5891 sizeof(file->pathname));
5892 if (IS_ERR(ptr))
5893 err = PTR_ERR(ptr);
5894 else
5895 memmove(file->pathname, ptr,
5896 sizeof(file->pathname)-(ptr-file->pathname));
5897 }
NeilBrown4af1a042014-12-15 12:57:00 +11005898 spin_unlock(&mddev->lock);
5899
5900 if (err == 0 &&
5901 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005902 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005903
NeilBrown32a76272005-06-21 17:17:14 -07005904 kfree(file);
5905 return err;
5906}
5907
NeilBrownf72ffdd2014-09-30 14:23:59 +10005908static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909{
5910 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005911 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912
5913 if (copy_from_user(&info, arg, sizeof(info)))
5914 return -EFAULT;
5915
NeilBrown1ca69c42012-10-11 13:37:33 +11005916 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005917 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 if (rdev) {
5919 info.major = MAJOR(rdev->bdev->bd_dev);
5920 info.minor = MINOR(rdev->bdev->bd_dev);
5921 info.raid_disk = rdev->raid_disk;
5922 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005923 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005925 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 info.state |= (1<<MD_DISK_ACTIVE);
5927 info.state |= (1<<MD_DISK_SYNC);
5928 }
Shaohua Li9efdca12015-10-12 16:59:50 -07005929 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07005930 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005931 if (test_bit(WriteMostly, &rdev->flags))
5932 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 } else {
5934 info.major = info.minor = 0;
5935 info.raid_disk = -1;
5936 info.state = (1<<MD_DISK_REMOVED);
5937 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005938 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
5940 if (copy_to_user(arg, &info, sizeof(info)))
5941 return -EFAULT;
5942
5943 return 0;
5944}
5945
NeilBrownf72ffdd2014-09-30 14:23:59 +10005946static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947{
5948 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005949 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 dev_t dev = MKDEV(info->major,info->minor);
5951
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005952 if (mddev_is_clustered(mddev) &&
5953 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005954 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005955 mdname(mddev));
5956 return -EINVAL;
5957 }
5958
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5960 return -EOVERFLOW;
5961
5962 if (!mddev->raid_disks) {
5963 int err;
5964 /* expecting a device which has a superblock */
5965 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5966 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005967 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 "md: md_import_device returned %ld\n",
5969 PTR_ERR(rdev));
5970 return PTR_ERR(rdev);
5971 }
5972 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005973 struct md_rdev *rdev0
5974 = list_entry(mddev->disks.next,
5975 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005976 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 .load_super(rdev, rdev0, mddev->minor_version);
5978 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005979 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005981 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 bdevname(rdev0->bdev,b2));
5983 export_rdev(rdev);
5984 return -EINVAL;
5985 }
5986 }
5987 err = bind_rdev_to_array(rdev, mddev);
5988 if (err)
5989 export_rdev(rdev);
5990 return err;
5991 }
5992
5993 /*
5994 * add_new_disk can be used once the array is assembled
5995 * to add "hot spares". They must already have a superblock
5996 * written
5997 */
5998 if (mddev->pers) {
5999 int err;
6000 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006001 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 "%s: personality does not support diskops!\n",
6003 mdname(mddev));
6004 return -EINVAL;
6005 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006006 if (mddev->persistent)
6007 rdev = md_import_device(dev, mddev->major_version,
6008 mddev->minor_version);
6009 else
6010 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006012 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 "md: md_import_device returned %ld\n",
6014 PTR_ERR(rdev));
6015 return PTR_ERR(rdev);
6016 }
NeilBrown1a855a02010-12-09 16:36:28 +11006017 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006018 if (!mddev->persistent) {
6019 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006020 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006021 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006022 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006023 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006024 } else
NeilBrown41158c72005-06-21 17:17:25 -07006025 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006026 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006027 } else
6028 super_types[mddev->major_version].
6029 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006030 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006031 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006032 /* This was a hot-add request, but events doesn't
6033 * match, so reject it.
6034 */
6035 export_rdev(rdev);
6036 return -EINVAL;
6037 }
6038
NeilBrownb2d444d2005-11-08 21:39:31 -08006039 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006040 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6041 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006042 else
6043 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006044
Song Liubac624f2015-08-13 14:31:55 -07006045 if (info->state & (1<<MD_DISK_JOURNAL))
6046 set_bit(Journal, &rdev->flags);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006047 /*
6048 * check whether the device shows up in other nodes
6049 */
6050 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006051 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006052 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006053 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006054 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006055 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006056 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006057 export_rdev(rdev);
6058 return err;
6059 }
6060 }
6061 }
6062
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 rdev->raid_disk = -1;
6064 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006065
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 if (err)
6067 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006068
6069 if (mddev_is_clustered(mddev)) {
6070 if (info->state & (1 << MD_DISK_CANDIDATE))
6071 md_cluster_ops->new_disk_ack(mddev, (err == 0));
6072 else {
6073 if (err)
6074 md_cluster_ops->add_new_disk_cancel(mddev);
6075 else
6076 err = add_bound_rdev(rdev);
6077 }
6078
6079 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006080 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006081
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 return err;
6083 }
6084
6085 /* otherwise, add_new_disk is only allowed
6086 * for major_version==0 superblocks
6087 */
6088 if (mddev->major_version != 0) {
6089 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
6090 mdname(mddev));
6091 return -EINVAL;
6092 }
6093
6094 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6095 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006096 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006098 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 "md: error, md_import_device() returned %ld\n",
6100 PTR_ERR(rdev));
6101 return PTR_ERR(rdev);
6102 }
6103 rdev->desc_nr = info->number;
6104 if (info->raid_disk < mddev->raid_disks)
6105 rdev->raid_disk = info->raid_disk;
6106 else
6107 rdev->raid_disk = -1;
6108
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006110 if (info->state & (1<<MD_DISK_SYNC))
6111 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006113 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6114 set_bit(WriteMostly, &rdev->flags);
6115
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 if (!mddev->persistent) {
6117 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006118 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6119 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006120 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006121 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122
NeilBrown2bf071b2006-01-06 00:20:55 -08006123 err = bind_rdev_to_array(rdev, mddev);
6124 if (err) {
6125 export_rdev(rdev);
6126 return err;
6127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 }
6129
6130 return 0;
6131}
6132
NeilBrownf72ffdd2014-09-30 14:23:59 +10006133static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134{
6135 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006136 struct md_rdev *rdev;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08006137 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 rdev = find_rdev(mddev, dev);
6140 if (!rdev)
6141 return -ENXIO;
6142
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006143 if (mddev_is_clustered(mddev))
Guoqing Jiang23b63f92015-10-12 17:21:30 +08006144 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006145
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006146 if (rdev->raid_disk < 0)
6147 goto kick_rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148
NeilBrown3ea8929d2013-04-24 11:42:41 +10006149 clear_bit(Blocked, &rdev->flags);
6150 remove_and_add_spares(mddev, rdev);
6151
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 if (rdev->raid_disk >= 0)
6153 goto busy;
6154
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006155kick_rdev:
Guoqing Jiang23b63f92015-10-12 17:21:30 +08006156 if (mddev_is_clustered(mddev) && ret == 0)
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006157 md_cluster_ops->remove_disk(mddev, rdev);
6158
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006159 md_kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07006160 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006161 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
6163 return 0;
6164busy:
Guoqing Jiang23b63f92015-10-12 17:21:30 +08006165 if (mddev_is_clustered(mddev) && ret == 0)
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006166 md_cluster_ops->metadata_update_cancel(mddev);
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006167
Nick Andrewfdefa4d2008-04-21 22:42:58 +00006168 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 bdevname(rdev->bdev,b), mdname(mddev));
6170 return -EBUSY;
6171}
6172
NeilBrownf72ffdd2014-09-30 14:23:59 +10006173static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174{
6175 char b[BDEVNAME_SIZE];
6176 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006177 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178
6179 if (!mddev->pers)
6180 return -ENODEV;
6181
6182 if (mddev->major_version != 0) {
6183 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6184 " version-0 superblocks.\n",
6185 mdname(mddev));
6186 return -EINVAL;
6187 }
6188 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006189 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 "%s: personality does not support diskops!\n",
6191 mdname(mddev));
6192 return -EINVAL;
6193 }
6194
NeilBrownd710e132008-10-13 11:55:12 +11006195 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006197 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 "md: error, md_import_device() returned %ld\n",
6199 PTR_ERR(rdev));
6200 return -EINVAL;
6201 }
6202
6203 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006204 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006206 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207
NeilBrown8190e752009-06-18 08:48:58 +10006208 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209
NeilBrownb2d444d2005-11-08 21:39:31 -08006210 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006211 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 "md: can not hot-add faulty %s disk to %s!\n",
6213 bdevname(rdev->bdev,b), mdname(mddev));
6214 err = -EINVAL;
6215 goto abort_export;
6216 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006217
NeilBrownb2d444d2005-11-08 21:39:31 -08006218 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006220 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006221 err = bind_rdev_to_array(rdev, mddev);
6222 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006223 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224
6225 /*
6226 * The rest should better be atomic, we can have disk failures
6227 * noticed in interrupt contexts ...
6228 */
6229
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 rdev->raid_disk = -1;
6231
NeilBrown850b2b42006-10-03 01:15:46 -07006232 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 /*
6234 * Kick recovery, maybe this spare has to be added to the
6235 * array immediately.
6236 */
6237 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6238 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006239 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 return 0;
6241
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242abort_export:
6243 export_rdev(rdev);
6244 return err;
6245}
6246
NeilBrownfd01b882011-10-11 16:47:53 +11006247static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006248{
NeilBrown035328c2014-04-09 12:25:40 +10006249 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006250
NeilBrown36fa3062005-09-09 16:23:45 -07006251 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006252 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006253 return -EBUSY;
6254 if (mddev->recovery || mddev->sync_thread)
6255 return -EBUSY;
6256 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006257 }
6258
NeilBrown36fa3062005-09-09 16:23:45 -07006259 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006260 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006261 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006262
NeilBrown1e594bb2014-12-15 12:57:00 +11006263 if (mddev->bitmap || mddev->bitmap_info.file)
6264 return -EEXIST; /* cannot add when bitmap is present */
6265 f = fget(fd);
6266
6267 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006268 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6269 mdname(mddev));
6270 return -EBADF;
6271 }
6272
NeilBrown1e594bb2014-12-15 12:57:00 +11006273 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006274 if (!S_ISREG(inode->i_mode)) {
6275 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6276 mdname(mddev));
6277 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006278 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006279 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6280 mdname(mddev));
6281 err = -EBADF;
6282 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006283 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6284 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006285 err = -EBUSY;
6286 }
6287 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006288 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006289 return err;
6290 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006291 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006292 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006293 } else if (mddev->bitmap == NULL)
6294 return -ENOENT; /* cannot remove what isn't there */
6295 err = 0;
6296 if (mddev->pers) {
6297 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006298 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006299 struct bitmap *bitmap;
6300
6301 bitmap = bitmap_create(mddev, -1);
6302 if (!IS_ERR(bitmap)) {
6303 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006304 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006305 } else
6306 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006307 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006308 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006309 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006310 fd = -1; /* make sure to put the file */
6311 }
NeilBrown36fa3062005-09-09 16:23:45 -07006312 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006313 }
6314 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006315 struct file *f = mddev->bitmap_info.file;
6316 if (f) {
6317 spin_lock(&mddev->lock);
6318 mddev->bitmap_info.file = NULL;
6319 spin_unlock(&mddev->lock);
6320 fput(f);
6321 }
NeilBrown36fa3062005-09-09 16:23:45 -07006322 }
6323
NeilBrown32a76272005-06-21 17:17:14 -07006324 return err;
6325}
6326
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327/*
6328 * set_array_info is used two different ways
6329 * The original usage is when creating a new array.
6330 * In this usage, raid_disks is > 0 and it together with
6331 * level, size, not_persistent,layout,chunksize determine the
6332 * shape of the array.
6333 * This will always create an array with a type-0.90.0 superblock.
6334 * The newer usage is when assembling an array.
6335 * In this case raid_disks will be 0, and the major_version field is
6336 * use to determine which style super-blocks are to be found on the devices.
6337 * The minor and patch _version numbers are also kept incase the
6338 * super_block handler wishes to interpret them.
6339 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006340static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341{
6342
6343 if (info->raid_disks == 0) {
6344 /* just setting version number for superblock loading */
6345 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006346 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 super_types[info->major_version].name == NULL) {
6348 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006349 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 "md: superblock version %d not known\n",
6351 info->major_version);
6352 return -EINVAL;
6353 }
6354 mddev->major_version = info->major_version;
6355 mddev->minor_version = info->minor_version;
6356 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006357 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006358 /* ensure mddev_put doesn't delete this now that there
6359 * is some minimal configuration.
6360 */
6361 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 return 0;
6363 }
6364 mddev->major_version = MD_MAJOR_VERSION;
6365 mddev->minor_version = MD_MINOR_VERSION;
6366 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6367 mddev->ctime = get_seconds();
6368
6369 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006370 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006371 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 mddev->raid_disks = info->raid_disks;
6373 /* don't set md_minor, it is determined by which /dev/md* was
6374 * openned
6375 */
6376 if (info->state & (1<<MD_SB_CLEAN))
6377 mddev->recovery_cp = MaxSector;
6378 else
6379 mddev->recovery_cp = 0;
6380 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006381 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382
6383 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006384 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385
6386 mddev->max_disks = MD_SB_DISKS;
6387
NeilBrowne6910632008-02-06 01:39:51 -08006388 if (mddev->persistent)
6389 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006390 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391
NeilBrownc3d97142009-12-14 12:49:52 +11006392 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006393 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006394 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006395
NeilBrownf6705572006-03-27 01:18:11 -08006396 mddev->reshape_position = MaxSector;
6397
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 /*
6399 * Generate a 128 bit UUID
6400 */
6401 get_random_bytes(mddev->uuid, 16);
6402
NeilBrownf6705572006-03-27 01:18:11 -08006403 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006404 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006405 mddev->new_layout = mddev->layout;
6406 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006407 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006408
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 return 0;
6410}
6411
NeilBrownfd01b882011-10-11 16:47:53 +11006412void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006413{
Dan Williamsb522adc2009-03-31 15:00:31 +11006414 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6415
6416 if (mddev->external_size)
6417 return;
6418
Dan Williams1f403622009-03-31 14:59:03 +11006419 mddev->array_sectors = array_sectors;
6420}
6421EXPORT_SYMBOL(md_set_array_sectors);
6422
NeilBrownfd01b882011-10-11 16:47:53 +11006423static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006424{
NeilBrown3cb03002011-10-11 16:45:26 +11006425 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006426 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006427 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006428
6429 if (mddev->pers->resize == NULL)
6430 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006431 /* The "num_sectors" is the number of sectors of each device that
6432 * is used. This can only make sense for arrays with redundancy.
6433 * linear and raid0 always use whatever space is available. We can only
6434 * consider changing this number if no resync or reconstruction is
6435 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006436 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006437 * of each device. If num_sectors is zero, we find the largest size
6438 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006439 */
NeilBrownf851b602014-12-11 10:02:10 +11006440 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6441 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006442 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006443 if (mddev->ro)
6444 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006445
NeilBrowndafb20f2012-03-19 12:46:39 +11006446 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006447 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006448
Andre Nolld71f9f82008-07-11 22:02:22 +10006449 if (fit && (num_sectors == 0 || num_sectors > avail))
6450 num_sectors = avail;
6451 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006452 return -ENOSPC;
6453 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006454 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006455 if (!rv)
6456 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006457 return rv;
6458}
6459
NeilBrownfd01b882011-10-11 16:47:53 +11006460static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006461{
6462 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006463 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006464 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006465 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006466 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006467 if (mddev->ro)
6468 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006469 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006470 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006471 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006472 if (mddev->sync_thread ||
6473 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6474 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006475 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006476
6477 rdev_for_each(rdev, mddev) {
6478 if (mddev->raid_disks < raid_disks &&
6479 rdev->data_offset < rdev->new_data_offset)
6480 return -EINVAL;
6481 if (mddev->raid_disks > raid_disks &&
6482 rdev->data_offset > rdev->new_data_offset)
6483 return -EINVAL;
6484 }
6485
NeilBrown63c70c42006-03-27 01:18:13 -08006486 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006487 if (mddev->delta_disks < 0)
6488 mddev->reshape_backwards = 1;
6489 else if (mddev->delta_disks > 0)
6490 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006491
6492 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006493 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006494 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006495 mddev->reshape_backwards = 0;
6496 }
NeilBrownda943b992006-01-06 00:20:54 -08006497 return rv;
6498}
6499
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500/*
6501 * update_array_info is used to change the configuration of an
6502 * on-line array.
6503 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6504 * fields in the info are checked against the array.
6505 * Any differences that cannot be handled will cause an error.
6506 * Normally, only one change can be managed at a time.
6507 */
NeilBrownfd01b882011-10-11 16:47:53 +11006508static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509{
6510 int rv = 0;
6511 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006512 int state = 0;
6513
6514 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006515 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006516 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517
6518 if (mddev->major_version != info->major_version ||
6519 mddev->minor_version != info->minor_version ||
6520/* mddev->patch_version != info->patch_version || */
6521 mddev->ctime != info->ctime ||
6522 mddev->level != info->level ||
6523/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006524 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006525 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006526 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6527 ((state^info->state) & 0xfffffe00)
6528 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 return -EINVAL;
6530 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006531 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6532 cnt++;
6533 if (mddev->raid_disks != info->raid_disks)
6534 cnt++;
6535 if (mddev->layout != info->layout)
6536 cnt++;
6537 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6538 cnt++;
6539 if (cnt == 0)
6540 return 0;
6541 if (cnt > 1)
6542 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543
6544 if (mddev->layout != info->layout) {
6545 /* Change layout
6546 * we don't need to do anything at the md level, the
6547 * personality will take care of it all.
6548 */
NeilBrown50ac1682009-06-18 08:47:55 +10006549 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006551 else {
6552 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006553 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006554 if (rv)
6555 mddev->new_layout = mddev->layout;
6556 return rv;
6557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006559 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006560 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561
NeilBrownda943b992006-01-06 00:20:54 -08006562 if (mddev->raid_disks != info->raid_disks)
6563 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564
NeilBrown36fa3062005-09-09 16:23:45 -07006565 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006566 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6567 rv = -EINVAL;
6568 goto err;
6569 }
6570 if (mddev->recovery || mddev->sync_thread) {
6571 rv = -EBUSY;
6572 goto err;
6573 }
NeilBrown36fa3062005-09-09 16:23:45 -07006574 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006575 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006576 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006577 if (mddev->bitmap) {
6578 rv = -EEXIST;
6579 goto err;
6580 }
6581 if (mddev->bitmap_info.default_offset == 0) {
6582 rv = -EINVAL;
6583 goto err;
6584 }
NeilBrownc3d97142009-12-14 12:49:52 +11006585 mddev->bitmap_info.offset =
6586 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006587 mddev->bitmap_info.space =
6588 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006589 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006590 bitmap = bitmap_create(mddev, -1);
6591 if (!IS_ERR(bitmap)) {
6592 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006593 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006594 } else
6595 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006596 if (rv)
6597 bitmap_destroy(mddev);
6598 mddev->pers->quiesce(mddev, 0);
6599 } else {
6600 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006601 if (!mddev->bitmap) {
6602 rv = -ENOENT;
6603 goto err;
6604 }
6605 if (mddev->bitmap->storage.file) {
6606 rv = -EINVAL;
6607 goto err;
6608 }
NeilBrown36fa3062005-09-09 16:23:45 -07006609 mddev->pers->quiesce(mddev, 1);
6610 bitmap_destroy(mddev);
6611 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006612 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006613 }
6614 }
NeilBrown850b2b42006-10-03 01:15:46 -07006615 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006616 return rv;
6617err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618 return rv;
6619}
6620
NeilBrownfd01b882011-10-11 16:47:53 +11006621static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622{
NeilBrown3cb03002011-10-11 16:45:26 +11006623 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006624 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625
6626 if (mddev->pers == NULL)
6627 return -ENODEV;
6628
NeilBrown1ca69c42012-10-11 13:37:33 +11006629 rcu_read_lock();
6630 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006632 err = -ENODEV;
6633 else {
6634 md_error(mddev, rdev);
6635 if (!test_bit(Faulty, &rdev->flags))
6636 err = -EBUSY;
6637 }
6638 rcu_read_unlock();
6639 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640}
6641
Andre Noll2f9618c2008-04-25 18:57:58 +02006642/*
6643 * We have a problem here : there is no easy way to give a CHS
6644 * virtual geometry. We currently pretend that we have a 2 heads
6645 * 4 sectors (with a BIG number of cylinders...). This drives
6646 * dosfs just mad... ;-)
6647 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006648static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6649{
NeilBrownfd01b882011-10-11 16:47:53 +11006650 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006651
6652 geo->heads = 2;
6653 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006654 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006655 return 0;
6656}
6657
Nicolas Schichancb335f82014-01-15 16:58:52 +01006658static inline bool md_ioctl_valid(unsigned int cmd)
6659{
6660 switch (cmd) {
6661 case ADD_NEW_DISK:
6662 case BLKROSET:
6663 case GET_ARRAY_INFO:
6664 case GET_BITMAP_FILE:
6665 case GET_DISK_INFO:
6666 case HOT_ADD_DISK:
6667 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006668 case RAID_AUTORUN:
6669 case RAID_VERSION:
6670 case RESTART_ARRAY_RW:
6671 case RUN_ARRAY:
6672 case SET_ARRAY_INFO:
6673 case SET_BITMAP_FILE:
6674 case SET_DISK_FAULTY:
6675 case STOP_ARRAY:
6676 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006677 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006678 return true;
6679 default:
6680 return false;
6681 }
6682}
6683
Al Viroa39907f2008-03-02 10:31:15 -05006684static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 unsigned int cmd, unsigned long arg)
6686{
6687 int err = 0;
6688 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006689 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006690 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691
Nicolas Schichancb335f82014-01-15 16:58:52 +01006692 if (!md_ioctl_valid(cmd))
6693 return -ENOTTY;
6694
NeilBrown506c9e42011-12-23 10:17:26 +11006695 switch (cmd) {
6696 case RAID_VERSION:
6697 case GET_ARRAY_INFO:
6698 case GET_DISK_INFO:
6699 break;
6700 default:
6701 if (!capable(CAP_SYS_ADMIN))
6702 return -EACCES;
6703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704
6705 /*
6706 * Commands dealing with the RAID driver but not any
6707 * particular array:
6708 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006709 switch (cmd) {
6710 case RAID_VERSION:
6711 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006712 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006715 case RAID_AUTORUN:
6716 err = 0;
6717 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006718 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006720 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 }
6722
6723 /*
6724 * Commands creating/starting a new array:
6725 */
6726
Al Viroa39907f2008-03-02 10:31:15 -05006727 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728
6729 if (!mddev) {
6730 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006731 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 }
6733
NeilBrown1ca69c42012-10-11 13:37:33 +11006734 /* Some actions do not requires the mutex */
6735 switch (cmd) {
6736 case GET_ARRAY_INFO:
6737 if (!mddev->raid_disks && !mddev->external)
6738 err = -ENODEV;
6739 else
6740 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006741 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006742
6743 case GET_DISK_INFO:
6744 if (!mddev->raid_disks && !mddev->external)
6745 err = -ENODEV;
6746 else
6747 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006748 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006749
6750 case SET_DISK_FAULTY:
6751 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006752 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006753
6754 case GET_BITMAP_FILE:
6755 err = get_bitmap_file(mddev, argp);
6756 goto out;
6757
NeilBrown1ca69c42012-10-11 13:37:33 +11006758 }
6759
NeilBrowna7a3f082012-12-11 13:35:54 +11006760 if (cmd == ADD_NEW_DISK)
6761 /* need to ensure md_delayed_delete() has completed */
6762 flush_workqueue(md_misc_wq);
6763
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006764 if (cmd == HOT_REMOVE_DISK)
6765 /* need to ensure recovery thread has run */
6766 wait_event_interruptible_timeout(mddev->sb_wait,
6767 !test_bit(MD_RECOVERY_NEEDED,
6768 &mddev->flags),
6769 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006770 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6771 /* Need to flush page cache, and ensure no-one else opens
6772 * and writes
6773 */
6774 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006775 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006776 mutex_unlock(&mddev->open_mutex);
6777 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006778 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006779 }
6780 set_bit(MD_STILL_CLOSED, &mddev->flags);
6781 mutex_unlock(&mddev->open_mutex);
6782 sync_blockdev(bdev);
6783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 err = mddev_lock(mddev);
6785 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006786 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6788 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006789 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 }
6791
NeilBrownc02c0ae2012-12-11 13:39:21 +11006792 if (cmd == SET_ARRAY_INFO) {
6793 mdu_array_info_t info;
6794 if (!arg)
6795 memset(&info, 0, sizeof(info));
6796 else if (copy_from_user(&info, argp, sizeof(info))) {
6797 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006798 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006799 }
6800 if (mddev->pers) {
6801 err = update_array_info(mddev, &info);
6802 if (err) {
6803 printk(KERN_WARNING "md: couldn't update"
6804 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006805 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006807 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006808 }
6809 if (!list_empty(&mddev->disks)) {
6810 printk(KERN_WARNING
6811 "md: array %s already has disks!\n",
6812 mdname(mddev));
6813 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006814 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006815 }
6816 if (mddev->raid_disks) {
6817 printk(KERN_WARNING
6818 "md: array %s already initialised!\n",
6819 mdname(mddev));
6820 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006821 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006822 }
6823 err = set_array_info(mddev, &info);
6824 if (err) {
6825 printk(KERN_WARNING "md: couldn't set"
6826 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006827 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006828 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006829 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830 }
6831
6832 /*
6833 * Commands querying/configuring an existing array:
6834 */
NeilBrown32a76272005-06-21 17:17:14 -07006835 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006836 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006837 if ((!mddev->raid_disks && !mddev->external)
6838 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6839 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6840 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006842 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843 }
6844
6845 /*
6846 * Commands even a read-only array can execute:
6847 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006848 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006849 case RESTART_ARRAY_RW:
6850 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006851 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006852
6853 case STOP_ARRAY:
6854 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006855 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006856
6857 case STOP_ARRAY_RO:
6858 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006859 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006860
NeilBrown3ea8929d2013-04-24 11:42:41 +10006861 case HOT_REMOVE_DISK:
6862 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006863 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006864
NeilBrown7ceb17e2013-04-24 11:42:42 +10006865 case ADD_NEW_DISK:
6866 /* We can support ADD_NEW_DISK on read-only arrays
6867 * on if we are re-adding a preexisting device.
6868 * So require mddev->pers and MD_DISK_SYNC.
6869 */
6870 if (mddev->pers) {
6871 mdu_disk_info_t info;
6872 if (copy_from_user(&info, argp, sizeof(info)))
6873 err = -EFAULT;
6874 else if (!(info.state & (1<<MD_DISK_SYNC)))
6875 /* Need to clear read-only for this */
6876 break;
6877 else
6878 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006879 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006880 }
6881 break;
6882
NeilBrownc02c0ae2012-12-11 13:39:21 +11006883 case BLKROSET:
6884 if (get_user(ro, (int __user *)(arg))) {
6885 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006886 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006887 }
6888 err = -EINVAL;
6889
6890 /* if the bdev is going readonly the value of mddev->ro
6891 * does not matter, no writes are coming
6892 */
6893 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006894 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006895
NeilBrownc02c0ae2012-12-11 13:39:21 +11006896 /* are we are already prepared for writes? */
6897 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006898 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006899
6900 /* transitioning to readauto need only happen for
6901 * arrays that call md_write_start
6902 */
6903 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006905 if (err == 0) {
6906 mddev->ro = 2;
6907 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006908 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006909 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006910 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 }
6912
6913 /*
6914 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006915 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 */
NeilBrown326eb172014-09-30 15:36:28 +10006917 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006918 if (mddev->ro == 2) {
6919 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006920 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006921 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006922 /* mddev_unlock will wake thread */
6923 /* If a device failed while we were read-only, we
6924 * need to make sure the metadata is updated now.
6925 */
6926 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6927 mddev_unlock(mddev);
6928 wait_event(mddev->sb_wait,
6929 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6930 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006931 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006932 }
NeilBrownf91de922005-11-08 21:39:36 -08006933 } else {
6934 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006935 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937 }
6938
NeilBrownc02c0ae2012-12-11 13:39:21 +11006939 switch (cmd) {
6940 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006942 mdu_disk_info_t info;
6943 if (copy_from_user(&info, argp, sizeof(info)))
6944 err = -EFAULT;
6945 else
6946 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006947 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006950 case CLUSTERED_DISK_NACK:
6951 if (mddev_is_clustered(mddev))
6952 md_cluster_ops->new_disk_ack(mddev, false);
6953 else
6954 err = -EINVAL;
6955 goto unlock;
6956
NeilBrownc02c0ae2012-12-11 13:39:21 +11006957 case HOT_ADD_DISK:
6958 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006959 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960
NeilBrownc02c0ae2012-12-11 13:39:21 +11006961 case RUN_ARRAY:
6962 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006963 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964
NeilBrownc02c0ae2012-12-11 13:39:21 +11006965 case SET_BITMAP_FILE:
6966 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006967 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006968
NeilBrownc02c0ae2012-12-11 13:39:21 +11006969 default:
6970 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006971 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 }
6973
NeilBrown3adc28d2014-09-30 15:46:41 +10006974unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006975 if (mddev->hold_active == UNTIL_IOCTL &&
6976 err != -EINVAL)
6977 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006979out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 return err;
6981}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006982#ifdef CONFIG_COMPAT
6983static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6984 unsigned int cmd, unsigned long arg)
6985{
6986 switch (cmd) {
6987 case HOT_REMOVE_DISK:
6988 case HOT_ADD_DISK:
6989 case SET_DISK_FAULTY:
6990 case SET_BITMAP_FILE:
6991 /* These take in integer arg, do not convert */
6992 break;
6993 default:
6994 arg = (unsigned long)compat_ptr(arg);
6995 break;
6996 }
6997
6998 return md_ioctl(bdev, mode, cmd, arg);
6999}
7000#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001
Al Viroa39907f2008-03-02 10:31:15 -05007002static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003{
7004 /*
7005 * Succeed if we can lock the mddev, which confirms that
7006 * it isn't being stopped right now.
7007 */
NeilBrownfd01b882011-10-11 16:47:53 +11007008 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009 int err;
7010
Yuanhan Liu0c098222012-05-22 13:55:32 +10007011 if (!mddev)
7012 return -ENODEV;
7013
NeilBrownd3374822009-01-09 08:31:10 +11007014 if (mddev->gendisk != bdev->bd_disk) {
7015 /* we are racing with mddev_put which is discarding this
7016 * bd_disk.
7017 */
7018 mddev_put(mddev);
7019 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007020 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007021 /* Then retry the open from the top */
7022 return -ERESTARTSYS;
7023 }
7024 BUG_ON(mddev != bdev->bd_disk->private_data);
7025
NeilBrownc8c00a62009-08-10 12:50:52 +10007026 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 goto out;
7028
7029 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007030 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10007031 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10007032 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007034 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035 out:
7036 return err;
7037}
7038
Al Virodb2a1442013-05-05 21:52:57 -04007039static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007041 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007043 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007044 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007047
7048static int md_media_changed(struct gendisk *disk)
7049{
NeilBrownfd01b882011-10-11 16:47:53 +11007050 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007051
7052 return mddev->changed;
7053}
7054
7055static int md_revalidate(struct gendisk *disk)
7056{
NeilBrownfd01b882011-10-11 16:47:53 +11007057 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007058
7059 mddev->changed = 0;
7060 return 0;
7061}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007062static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063{
7064 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007065 .open = md_open,
7066 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007067 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007068#ifdef CONFIG_COMPAT
7069 .compat_ioctl = md_compat_ioctl,
7070#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007071 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007072 .media_changed = md_media_changed,
7073 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074};
7075
NeilBrownf72ffdd2014-09-30 14:23:59 +10007076static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077{
NeilBrown2b8bf342011-10-11 16:48:23 +11007078 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 /*
7081 * md_thread is a 'system-thread', it's priority should be very
7082 * high. We avoid resource deadlocks individually in each
7083 * raid personality. (RAID5 does preallocation) We also use RR and
7084 * the very same RT priority as kswapd, thus we will never get
7085 * into a priority inversion deadlock.
7086 *
7087 * we definitely have to have equal or higher priority than
7088 * bdflush, otherwise bdflush will deadlock if there are too
7089 * many dirty RAID5 blocks.
7090 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091
NeilBrown6985c432005-10-19 21:23:47 -07007092 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007093 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094
NeilBrown93588e22005-11-15 00:09:12 -08007095 /* We need to wait INTERRUPTIBLE so that
7096 * we don't add to the load-average.
7097 * That means we need to be sure no signals are
7098 * pending
7099 */
7100 if (signal_pending(current))
7101 flush_signals(current);
7102
7103 wait_event_interruptible_timeout
7104 (thread->wqueue,
7105 test_bit(THREAD_WAKEUP, &thread->flags)
7106 || kthread_should_stop(),
7107 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108
NeilBrown6c987912011-01-14 09:13:53 +11007109 clear_bit(THREAD_WAKEUP, &thread->flags);
7110 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007111 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007113
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114 return 0;
7115}
7116
NeilBrown2b8bf342011-10-11 16:48:23 +11007117void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118{
7119 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007120 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 set_bit(THREAD_WAKEUP, &thread->flags);
7122 wake_up(&thread->wqueue);
7123 }
7124}
NeilBrown6c144d32014-09-30 16:15:38 +10007125EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126
Shaohua Li4ed87312012-10-11 13:34:00 +11007127struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7128 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129{
NeilBrown2b8bf342011-10-11 16:48:23 +11007130 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131
NeilBrown2b8bf342011-10-11 16:48:23 +11007132 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133 if (!thread)
7134 return NULL;
7135
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 init_waitqueue_head(&thread->wqueue);
7137
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 thread->run = run;
7139 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007140 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007141 thread->tsk = kthread_run(md_thread, thread,
7142 "%s_%s",
7143 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007144 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007145 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 kfree(thread);
7147 return NULL;
7148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149 return thread;
7150}
NeilBrown6c144d32014-09-30 16:15:38 +10007151EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152
NeilBrown2b8bf342011-10-11 16:48:23 +11007153void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154{
NeilBrown2b8bf342011-10-11 16:48:23 +11007155 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007156 if (!thread)
7157 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007158 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007159 /* Locking ensures that mddev_unlock does not wake_up a
7160 * non-existent thread
7161 */
7162 spin_lock(&pers_lock);
7163 *threadp = NULL;
7164 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007165
7166 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167 kfree(thread);
7168}
NeilBrown6c144d32014-09-30 16:15:38 +10007169EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170
NeilBrownfd01b882011-10-11 16:47:53 +11007171void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172{
NeilBrownb2d444d2005-11-08 21:39:31 -08007173 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007175
NeilBrownde393cd2011-07-28 11:31:48 +10007176 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177 return;
7178 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007179 if (mddev->degraded)
7180 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007181 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7183 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7184 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007185 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007186 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07007187 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188}
NeilBrown6c144d32014-09-30 16:15:38 +10007189EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190
7191/* seq_file implementation /proc/mdstat */
7192
7193static void status_unused(struct seq_file *seq)
7194{
7195 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007196 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197
7198 seq_printf(seq, "unused devices: ");
7199
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007200 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201 char b[BDEVNAME_SIZE];
7202 i++;
7203 seq_printf(seq, "%s ",
7204 bdevname(rdev->bdev,b));
7205 }
7206 if (!i)
7207 seq_printf(seq, "<none>");
7208
7209 seq_printf(seq, "\n");
7210}
7211
NeilBrownf7851be2015-07-02 17:12:58 +10007212static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213{
NeilBrowndd71cf62009-05-07 12:49:35 +10007214 sector_t max_sectors, resync, res;
7215 unsigned long dt, db;
7216 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007217 int scale;
7218 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219
NeilBrownc804cde2012-05-21 09:28:33 +10007220 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7221 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007222 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007224 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225
NeilBrownf7851be2015-07-02 17:12:58 +10007226 resync = mddev->curr_resync;
7227 if (resync <= 3) {
7228 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7229 /* Still cleaning up */
7230 resync = max_sectors;
7231 } else
7232 resync -= atomic_read(&mddev->recovery_active);
7233
7234 if (resync == 0) {
7235 if (mddev->recovery_cp < MaxSector) {
7236 seq_printf(seq, "\tresync=PENDING");
7237 return 1;
7238 }
7239 return 0;
7240 }
7241 if (resync < 3) {
7242 seq_printf(seq, "\tresync=DELAYED");
7243 return 1;
7244 }
7245
NeilBrown403df472014-09-30 15:52:29 +10007246 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007247 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007248 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007249 * u32, as those are the requirements for sector_div.
7250 * Thus 'scale' must be at least 10
7251 */
7252 scale = 10;
7253 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007254 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007255 scale++;
7256 }
7257 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007258 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007259
7260 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261 {
NeilBrown4588b422006-03-27 01:18:04 -08007262 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263 seq_printf(seq, "[");
7264 for (i = 0; i < x; i++)
7265 seq_printf(seq, "=");
7266 seq_printf(seq, ">");
7267 for (i = 0; i < y; i++)
7268 seq_printf(seq, ".");
7269 seq_printf(seq, "] ");
7270 }
NeilBrown4588b422006-03-27 01:18:04 -08007271 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007272 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7273 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007274 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7275 "check" :
7276 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7277 "resync" : "recovery"))),
7278 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007279 (unsigned long long) resync/2,
7280 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281
7282 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 * dt: time from mark until now
7284 * db: blocks written from mark until now
7285 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007286 *
7287 * rt is a sector_t, so could be 32bit or 64bit.
7288 * So we divide before multiply in case it is 32bit and close
7289 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007290 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007291 * near the end of resync when the number of remaining sectors
7292 * is close to 'db'.
7293 * We then divide rt by 32 after multiplying by db to compensate.
7294 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295 */
7296 dt = ((jiffies - mddev->resync_mark) / HZ);
7297 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007298 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7299 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300
NeilBrowndd71cf62009-05-07 12:49:35 +10007301 rt = max_sectors - resync; /* number of remaining sectors */
7302 sector_div(rt, db/32+1);
7303 rt *= dt;
7304 rt >>= 5;
7305
7306 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7307 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308
NeilBrownff4e8d92006-07-10 04:44:16 -07007309 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007310 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311}
7312
7313static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7314{
7315 struct list_head *tmp;
7316 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007317 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318
7319 if (l >= 0x10000)
7320 return NULL;
7321 if (!l--)
7322 /* header */
7323 return (void*)1;
7324
7325 spin_lock(&all_mddevs_lock);
7326 list_for_each(tmp,&all_mddevs)
7327 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007328 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329 mddev_get(mddev);
7330 spin_unlock(&all_mddevs_lock);
7331 return mddev;
7332 }
7333 spin_unlock(&all_mddevs_lock);
7334 if (!l--)
7335 return (void*)2;/* tail */
7336 return NULL;
7337}
7338
7339static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7340{
7341 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007342 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007343
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 ++*pos;
7345 if (v == (void*)2)
7346 return NULL;
7347
7348 spin_lock(&all_mddevs_lock);
7349 if (v == (void*)1)
7350 tmp = all_mddevs.next;
7351 else
7352 tmp = mddev->all_mddevs.next;
7353 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007354 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 else {
7356 next_mddev = (void*)2;
7357 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359 spin_unlock(&all_mddevs_lock);
7360
7361 if (v != (void*)1)
7362 mddev_put(mddev);
7363 return next_mddev;
7364
7365}
7366
7367static void md_seq_stop(struct seq_file *seq, void *v)
7368{
NeilBrownfd01b882011-10-11 16:47:53 +11007369 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370
7371 if (mddev && v != (void*)1 && v != (void*)2)
7372 mddev_put(mddev);
7373}
7374
7375static int md_seq_show(struct seq_file *seq, void *v)
7376{
NeilBrownfd01b882011-10-11 16:47:53 +11007377 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007378 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007379 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380
7381 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007382 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383 seq_printf(seq, "Personalities : ");
7384 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007385 list_for_each_entry(pers, &pers_list, list)
7386 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387
7388 spin_unlock(&pers_lock);
7389 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007390 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391 return 0;
7392 }
7393 if (v == (void*)2) {
7394 status_unused(seq);
7395 return 0;
7396 }
7397
NeilBrown36d091f2014-12-15 12:56:58 +11007398 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7400 seq_printf(seq, "%s : %sactive", mdname(mddev),
7401 mddev->pers ? "" : "in");
7402 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007403 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007405 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007406 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407 seq_printf(seq, " %s", mddev->pers->name);
7408 }
7409
Andre Nolldd8ac332009-03-31 14:33:13 +11007410 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007411 rcu_read_lock();
7412 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413 char b[BDEVNAME_SIZE];
7414 seq_printf(seq, " %s[%d]",
7415 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007416 if (test_bit(WriteMostly, &rdev->flags))
7417 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007418 if (test_bit(Journal, &rdev->flags))
7419 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007420 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 seq_printf(seq, "(F)");
7422 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007423 }
7424 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007425 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007426 if (test_bit(Replacement, &rdev->flags))
7427 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007428 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007430 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431
7432 if (!list_empty(&mddev->disks)) {
7433 if (mddev->pers)
7434 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007435 (unsigned long long)
7436 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437 else
7438 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007439 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007441 if (mddev->persistent) {
7442 if (mddev->major_version != 0 ||
7443 mddev->minor_version != 90) {
7444 seq_printf(seq," super %d.%d",
7445 mddev->major_version,
7446 mddev->minor_version);
7447 }
NeilBrowne6910632008-02-06 01:39:51 -08007448 } else if (mddev->external)
7449 seq_printf(seq, " super external:%s",
7450 mddev->metadata_type);
7451 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007452 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453
7454 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007455 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007456 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007457 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007458 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007459 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007460 }
NeilBrown32a76272005-06-21 17:17:14 -07007461 } else
7462 seq_printf(seq, "\n ");
7463
NeilBrown57148962012-03-19 12:46:40 +11007464 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465
7466 seq_printf(seq, "\n");
7467 }
NeilBrown36d091f2014-12-15 12:56:58 +11007468 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007469
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470 return 0;
7471}
7472
Jan Engelhardt110518b2009-05-07 12:49:37 +10007473static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 .start = md_seq_start,
7475 .next = md_seq_next,
7476 .stop = md_seq_stop,
7477 .show = md_seq_show,
7478};
7479
7480static int md_seq_open(struct inode *inode, struct file *file)
7481{
Kay Sieversf1514632011-07-12 20:48:39 +02007482 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483 int error;
7484
7485 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007486 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007487 return error;
7488
7489 seq = file->private_data;
7490 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491 return error;
7492}
7493
NeilBrowne2f23b62014-04-09 14:33:51 +10007494static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007495static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7496{
Kay Sieversf1514632011-07-12 20:48:39 +02007497 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007498 int mask;
7499
NeilBrowne2f23b62014-04-09 14:33:51 +10007500 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007501 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007502 poll_wait(filp, &md_event_waiters, wait);
7503
7504 /* always allow read */
7505 mask = POLLIN | POLLRDNORM;
7506
Kay Sieversf1514632011-07-12 20:48:39 +02007507 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007508 mask |= POLLERR | POLLPRI;
7509 return mask;
7510}
7511
Arjan van de Venfa027c22007-02-12 00:55:33 -08007512static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007513 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514 .open = md_seq_open,
7515 .read = seq_read,
7516 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007517 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007518 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519};
7520
NeilBrown84fc4b52011-10-11 16:49:58 +11007521int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522{
Chao Yu50bd3772014-09-25 15:28:34 +08007523 printk(KERN_INFO "md: %s personality registered for level %d\n",
7524 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007526 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527 spin_unlock(&pers_lock);
7528 return 0;
7529}
NeilBrown6c144d32014-09-30 16:15:38 +10007530EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531
NeilBrown84fc4b52011-10-11 16:49:58 +11007532int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533{
NeilBrown2604b702006-01-06 00:20:36 -08007534 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007536 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537 spin_unlock(&pers_lock);
7538 return 0;
7539}
NeilBrown6c144d32014-09-30 16:15:38 +10007540EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541
NeilBrown6022e752015-08-13 12:32:55 +10007542int register_md_cluster_operations(struct md_cluster_operations *ops,
7543 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007544{
NeilBrown6022e752015-08-13 12:32:55 +10007545 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007546 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007547 if (md_cluster_ops != NULL)
7548 ret = -EALREADY;
7549 else {
7550 md_cluster_ops = ops;
7551 md_cluster_mod = module;
7552 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007553 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007554 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007555}
7556EXPORT_SYMBOL(register_md_cluster_operations);
7557
7558int unregister_md_cluster_operations(void)
7559{
7560 spin_lock(&pers_lock);
7561 md_cluster_ops = NULL;
7562 spin_unlock(&pers_lock);
7563 return 0;
7564}
7565EXPORT_SYMBOL(unregister_md_cluster_operations);
7566
7567int md_setup_cluster(struct mddev *mddev, int nodes)
7568{
7569 int err;
7570
7571 err = request_module("md-cluster");
7572 if (err) {
7573 pr_err("md-cluster module not found.\n");
Goldwyn Rodriguesb0c26a72015-07-22 12:09:15 -05007574 return -ENOENT;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007575 }
7576
7577 spin_lock(&pers_lock);
7578 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7579 spin_unlock(&pers_lock);
7580 return -ENOENT;
7581 }
7582 spin_unlock(&pers_lock);
7583
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007584 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007585}
7586
7587void md_cluster_stop(struct mddev *mddev)
7588{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007589 if (!md_cluster_ops)
7590 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007591 md_cluster_ops->leave(mddev);
7592 module_put(md_cluster_mod);
7593}
7594
NeilBrownfd01b882011-10-11 16:47:53 +11007595static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007597 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007598 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007599 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007600
7601 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007602 rcu_read_lock();
7603 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007605 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7606 (int)part_stat_read(&disk->part0, sectors[1]) -
7607 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007608 /* sync IO will cause sync_io to increase before the disk_stats
7609 * as sync_io is counted when a request starts, and
7610 * disk_stats is counted when it completes.
7611 * So resync activity will cause curr_events to be smaller than
7612 * when there was no such activity.
7613 * non-sync IO will cause disk_stat to increase without
7614 * increasing sync_io so curr_events will (eventually)
7615 * be larger than it was before. Once it becomes
7616 * substantially larger, the test below will cause
7617 * the array to appear non-idle, and resync will slow
7618 * down.
7619 * If there is a lot of outstanding resync activity when
7620 * we set last_event to curr_events, then all that activity
7621 * completing might cause the array to appear non-idle
7622 * and resync will be slowed down even though there might
7623 * not have been non-resync activity. This will only
7624 * happen once though. 'last_events' will soon reflect
7625 * the state where there is little or no outstanding
7626 * resync requests, and further resync activity will
7627 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007628 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007630 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631 rdev->last_events = curr_events;
7632 idle = 0;
7633 }
7634 }
NeilBrown4b809912008-07-21 17:05:25 +10007635 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636 return idle;
7637}
7638
NeilBrownfd01b882011-10-11 16:47:53 +11007639void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640{
7641 /* another "blocks" (512byte) blocks have been synced */
7642 atomic_sub(blocks, &mddev->recovery_active);
7643 wake_up(&mddev->recovery_wait);
7644 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007645 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007646 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647 md_wakeup_thread(mddev->thread);
7648 // stop recovery, signal do_sync ....
7649 }
7650}
NeilBrown6c144d32014-09-30 16:15:38 +10007651EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652
NeilBrown06d91a52005-06-21 17:17:12 -07007653/* md_write_start(mddev, bi)
7654 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007655 * in superblock) before writing, schedule a superblock update
7656 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007657 */
NeilBrownfd01b882011-10-11 16:47:53 +11007658void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007659{
Neil Brown0fd62b82008-06-28 08:31:36 +10007660 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007661 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007662 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007663
NeilBrownf91de922005-11-08 21:39:36 -08007664 BUG_ON(mddev->ro == 1);
7665 if (mddev->ro == 2) {
7666 /* need to switch to read/write */
7667 mddev->ro = 0;
7668 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7669 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007670 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007671 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007672 }
NeilBrown06d91a52005-06-21 17:17:12 -07007673 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007674 if (mddev->safemode == 1)
7675 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007676 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007677 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007678 if (mddev->in_sync) {
7679 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007680 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007681 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007682 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007683 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007684 }
NeilBrown85572d72014-12-15 12:56:56 +11007685 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007686 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007687 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007688 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007689 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007690 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007691}
NeilBrown6c144d32014-09-30 16:15:38 +10007692EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693
NeilBrownfd01b882011-10-11 16:47:53 +11007694void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007695{
7696 if (atomic_dec_and_test(&mddev->writes_pending)) {
7697 if (mddev->safemode == 2)
7698 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007699 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7701 }
7702}
NeilBrown6c144d32014-09-30 16:15:38 +10007703EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007704
NeilBrown2a2275d2007-01-26 00:57:11 -08007705/* md_allow_write(mddev)
7706 * Calling this ensures that the array is marked 'active' so that writes
7707 * may proceed without blocking. It is important to call this before
7708 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7709 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007710 *
7711 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7712 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007713 */
NeilBrownfd01b882011-10-11 16:47:53 +11007714int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007715{
7716 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007717 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007718 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007719 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007720 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007721 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007722
NeilBrown85572d72014-12-15 12:56:56 +11007723 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007724 if (mddev->in_sync) {
7725 mddev->in_sync = 0;
7726 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007727 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007728 if (mddev->safemode_delay &&
7729 mddev->safemode == 0)
7730 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007731 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007732 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007733 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007734 } else
NeilBrown85572d72014-12-15 12:56:56 +11007735 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007736
NeilBrown070dc6d2010-08-30 17:33:34 +10007737 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007738 return -EAGAIN;
7739 else
7740 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007741}
7742EXPORT_SYMBOL_GPL(md_allow_write);
7743
Linus Torvalds1da177e2005-04-16 15:20:36 -07007744#define SYNC_MARKS 10
7745#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007746#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007747void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007748{
Shaohua Li4ed87312012-10-11 13:34:00 +11007749 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007750 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751 unsigned int currspeed = 0,
7752 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007753 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007754 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007755 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007756 sector_t mark_cnt[SYNC_MARKS];
7757 int last_mark,m;
7758 struct list_head *tmp;
7759 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007760 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007761 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007762 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007763 struct blk_plug plug;
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05007764 bool cluster_resync_finished = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007765
7766 /* just incase thread restarts... */
7767 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7768 return;
NeilBrown3991b312014-05-28 13:39:23 +10007769 if (mddev->ro) {/* never try to sync a read-only array */
7770 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007771 return;
NeilBrown3991b312014-05-28 13:39:23 +10007772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007773
NeilBrown61df9d92006-10-03 01:15:57 -07007774 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007775 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007776 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007777 action = "check";
7778 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007779 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007780 action = "repair";
7781 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007782 desc = "resync";
7783 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7784 desc = "reshape";
7785 else
7786 desc = "recovery";
7787
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007788 mddev->last_sync_action = action ?: desc;
7789
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790 /* we overload curr_resync somewhat here.
7791 * 0 == not engaged in resync at all
7792 * 2 == checking that there is no conflict with another sync
7793 * 1 == like 2, but have yielded to allow conflicting resync to
7794 * commense
7795 * other == active in resync - this many blocks
7796 *
7797 * Before starting a resync we must have set curr_resync to
7798 * 2, and then checked that every "conflicting" array has curr_resync
7799 * less than ours. When we find one that is the same or higher
7800 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7801 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7802 * This will mean we have to start checking from the beginning again.
7803 *
7804 */
7805
7806 do {
7807 mddev->curr_resync = 2;
7808
7809 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007810 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007812 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813 if (mddev2 == mddev)
7814 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007815 if (!mddev->parallel_resync
7816 && mddev2->curr_resync
7817 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007818 DEFINE_WAIT(wq);
7819 if (mddev < mddev2 && mddev->curr_resync == 2) {
7820 /* arbitrarily yield */
7821 mddev->curr_resync = 1;
7822 wake_up(&resync_wait);
7823 }
7824 if (mddev > mddev2 && mddev->curr_resync == 1)
7825 /* no need to wait here, we can wait the next
7826 * time 'round when curr_resync == 2
7827 */
7828 continue;
NeilBrown97441972008-09-19 11:49:54 +10007829 /* We need to wait 'interruptible' so as not to
7830 * contribute to the load average, and not to
7831 * be caught by 'softlockup'
7832 */
7833 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007834 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007835 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007836 printk(KERN_INFO "md: delaying %s of %s"
7837 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007838 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007839 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007841 if (signal_pending(current))
7842 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843 schedule();
7844 finish_wait(&resync_wait, &wq);
7845 goto try_again;
7846 }
7847 finish_wait(&resync_wait, &wq);
7848 }
7849 }
7850 } while (mddev->curr_resync < 2);
7851
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007852 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007853 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007855 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856 */
7857 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007858 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007859 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007860 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7861 j = mddev->resync_min;
7862 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007863 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007864
NeilBrownccfcc3c2006-03-27 01:18:09 -08007865 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007866 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007867 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007869 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007870 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007871 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007872 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007873 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07007874 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007875 !test_bit(Faulty, &rdev->flags) &&
7876 !test_bit(In_sync, &rdev->flags) &&
7877 rdev->recovery_offset < j)
7878 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007879 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007880
7881 /* If there is a bitmap, we need to make sure all
7882 * writes that started before we added a spare
7883 * complete before we start doing a recovery.
7884 * Otherwise the write might complete and (via
7885 * bitmap_endwrite) set a bit in the bitmap after the
7886 * recovery has checked that bit and skipped that
7887 * region.
7888 */
7889 if (mddev->bitmap) {
7890 mddev->pers->quiesce(mddev, 1);
7891 mddev->pers->quiesce(mddev, 0);
7892 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894
NeilBrown61df9d92006-10-03 01:15:57 -07007895 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7896 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7897 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007898 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007899 "(but not more than %d KB/sec) for %s.\n",
7900 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901
NeilBrowneea1bf32009-03-31 14:27:02 +11007902 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007903
NeilBrown57afd892005-06-21 17:17:13 -07007904 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905 for (m = 0; m < SYNC_MARKS; m++) {
7906 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007907 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007908 }
7909 last_mark = 0;
7910 mddev->resync_mark = mark[last_mark];
7911 mddev->resync_mark_cnt = mark_cnt[last_mark];
7912
7913 /*
7914 * Tune reconstruction:
7915 */
7916 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007917 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7918 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919
7920 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921 last_check = 0;
7922
7923 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007924 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007925 "md: resuming %s of %s from checkpoint.\n",
7926 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007928 } else
7929 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007930 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007931 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7932 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007933 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934
majianpeng7c2c57c2012-07-03 12:12:26 +10007935 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007937 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938
NeilBrown57afd892005-06-21 17:17:13 -07007939 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007940
NeilBrown7a91ee12009-05-26 12:57:21 +10007941 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7942 ((mddev->curr_resync > mddev->curr_resync_completed &&
7943 (mddev->curr_resync - mddev->curr_resync_completed)
7944 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007945 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007946 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10007947 >= mddev->resync_max - mddev->curr_resync_completed ||
7948 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10007949 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007950 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007951 wait_event(mddev->recovery_wait,
7952 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007953 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007954 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7955 j > mddev->recovery_cp)
7956 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007957 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007958 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007959 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007960 }
NeilBrownacb180b2009-04-14 16:28:34 +10007961
NeilBrownc91abf52013-11-19 12:02:01 +11007962 while (j >= mddev->resync_max &&
7963 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007964 /* As this condition is controlled by user-space,
7965 * we can block indefinitely, so use '_interruptible'
7966 * to avoid triggering warnings.
7967 */
7968 flush_signals(current); /* just in case */
7969 wait_event_interruptible(mddev->recovery_wait,
7970 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007971 || test_bit(MD_RECOVERY_INTR,
7972 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007973 }
NeilBrownacb180b2009-04-14 16:28:34 +10007974
NeilBrownc91abf52013-11-19 12:02:01 +11007975 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7976 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007977
NeilBrown09314792015-02-19 16:04:40 +11007978 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07007979 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007980 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007981 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982 }
NeilBrown57afd892005-06-21 17:17:13 -07007983
7984 if (!skipped) { /* actual IO requested */
7985 io_sectors += sectors;
7986 atomic_add(sectors, &mddev->recovery_active);
7987 }
7988
NeilBrowne875ece2011-07-28 11:39:24 +10007989 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7990 break;
7991
Linus Torvalds1da177e2005-04-16 15:20:36 -07007992 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10007993 if (j > max_sectors)
7994 /* when skipping, extra large numbers can be returned. */
7995 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007996 if (j > 2)
7997 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007998 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007999 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008000 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008001 * visible in /proc/mdstat
8002 */
8003 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008004
8005 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006 continue;
8007
NeilBrown57afd892005-06-21 17:17:13 -07008008 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008009 repeat:
8010 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8011 /* step marks */
8012 int next = (last_mark+1) % SYNC_MARKS;
8013
8014 mddev->resync_mark = mark[next];
8015 mddev->resync_mark_cnt = mark_cnt[next];
8016 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008017 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008018 last_mark = next;
8019 }
8020
NeilBrownc91abf52013-11-19 12:02:01 +11008021 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8022 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008023
8024 /*
8025 * this loop exits only if either when we are slower than
8026 * the 'hard' speed limit, or the system was IO-idle for
8027 * a jiffy.
8028 * the system might be non-idle CPU-wise, but we only care
8029 * about not overloading the IO subsystem. (things like an
8030 * e2fsck being done on the RAID array should execute fast)
8031 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008032 cond_resched();
8033
Xiao Niac7e50a2014-08-07 09:37:41 -04008034 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8035 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008036 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008037
NeilBrown88202a02006-01-06 00:21:36 -08008038 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008039 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008040 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008041 goto repeat;
8042 }
NeilBrownac8fa412015-02-19 16:55:00 +11008043 if (!is_mddev_idle(mddev, 0)) {
8044 /*
8045 * Give other IO more of a chance.
8046 * The faster the devices, the less we wait.
8047 */
8048 wait_event(mddev->recovery_wait,
8049 !atomic_read(&mddev->recovery_active));
8050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008051 }
8052 }
NeilBrownc91abf52013-11-19 12:02:01 +11008053 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
8054 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8055 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056 /*
8057 * this also signals 'finished resyncing' to md_stop
8058 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008059 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008060 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8061
NeilBrown5ed1df22015-07-24 13:27:08 +10008062 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8063 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8064 mddev->curr_resync > 2) {
8065 mddev->curr_resync_completed = mddev->curr_resync;
8066 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8067 }
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008068 /* tell personality and other nodes that we are finished */
8069 if (mddev_is_clustered(mddev)) {
8070 md_cluster_ops->resync_finish(mddev);
8071 cluster_resync_finished = true;
8072 }
NeilBrown09314792015-02-19 16:04:40 +11008073 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008074
NeilBrowndfc70642008-05-23 13:04:39 -07008075 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008076 mddev->curr_resync > 2) {
8077 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8078 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8079 if (mddev->curr_resync >= mddev->recovery_cp) {
8080 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07008081 "md: checkpointing %s of %s.\n",
8082 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008083 if (test_bit(MD_RECOVERY_ERROR,
8084 &mddev->recovery))
8085 mddev->recovery_cp =
8086 mddev->curr_resync_completed;
8087 else
8088 mddev->recovery_cp =
8089 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008090 }
8091 } else
8092 mddev->recovery_cp = MaxSector;
8093 } else {
8094 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8095 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008096 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008097 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008098 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008099 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008100 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008101 !test_bit(Faulty, &rdev->flags) &&
8102 !test_bit(In_sync, &rdev->flags) &&
8103 rdev->recovery_offset < mddev->curr_resync)
8104 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008105 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008107 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008108 skip:
NeilBrown17571282006-12-10 02:20:52 -08008109 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008110
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008111 if (mddev_is_clustered(mddev) &&
8112 test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8113 !cluster_resync_finished)
8114 md_cluster_ops->resync_finish(mddev);
8115
NeilBrown23da4222014-12-15 12:57:01 +11008116 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008117 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8118 /* We completed so min/max setting can be forgotten if used. */
8119 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8120 mddev->resync_min = 0;
8121 mddev->resync_max = MaxSector;
8122 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8123 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008124 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008125 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008126 spin_unlock(&mddev->lock);
8127
Linus Torvalds1da177e2005-04-16 15:20:36 -07008128 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008129 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008130 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008131}
NeilBrown29269552006-03-27 01:18:10 -08008132EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008133
NeilBrown746d3202013-04-24 11:42:41 +10008134static int remove_and_add_spares(struct mddev *mddev,
8135 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008136{
NeilBrown3cb03002011-10-11 16:45:26 +11008137 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008138 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008139 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008140
NeilBrowndafb20f2012-03-19 12:46:39 +11008141 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10008142 if ((this == NULL || rdev == this) &&
8143 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008144 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008145 (test_bit(Faulty, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008146 (!test_bit(In_sync, &rdev->flags) &&
8147 !test_bit(Journal, &rdev->flags))) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008148 atomic_read(&rdev->nr_pending)==0) {
8149 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008150 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008151 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008152 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008153 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008154 }
8155 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008156 if (removed && mddev->kobj.sd)
8157 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008158
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008159 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008160 goto no_add;
8161
NeilBrowndafb20f2012-03-19 12:46:39 +11008162 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008163 if (this && this != rdev)
8164 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008165 if (test_bit(Candidate, &rdev->flags))
8166 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008167 if (rdev->raid_disk >= 0 &&
8168 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008169 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008170 !test_bit(Faulty, &rdev->flags))
8171 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008172 if (rdev->raid_disk >= 0)
8173 continue;
8174 if (test_bit(Faulty, &rdev->flags))
8175 continue;
Shaohua Li3069aa82015-08-13 14:31:56 -07008176 if (test_bit(Journal, &rdev->flags))
8177 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008178 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11008179 ! (rdev->saved_raid_disk >= 0 &&
8180 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10008181 continue;
8182
NeilBrownd01552a2015-10-31 11:00:56 +11008183 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008184 if (mddev->pers->
8185 hot_add_disk(mddev, rdev) == 0) {
8186 if (sysfs_link_rdev(mddev, rdev))
8187 /* failure here is OK */;
8188 spares++;
8189 md_new_event(mddev);
8190 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008191 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008192 }
NeilBrown746d3202013-04-24 11:42:41 +10008193no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008194 if (removed)
8195 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008196 return spares;
8197}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008198
NeilBrownac05f252014-09-30 08:10:42 +10008199static void md_start_sync(struct work_struct *ws)
8200{
8201 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008202 int ret = 0;
8203
8204 if (mddev_is_clustered(mddev)) {
8205 ret = md_cluster_ops->resync_start(mddev);
8206 if (ret) {
8207 mddev->sync_thread = NULL;
8208 goto out;
8209 }
8210 }
NeilBrownac05f252014-09-30 08:10:42 +10008211
8212 mddev->sync_thread = md_register_thread(md_do_sync,
8213 mddev,
8214 "resync");
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008215out:
NeilBrownac05f252014-09-30 08:10:42 +10008216 if (!mddev->sync_thread) {
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008217 if (!(mddev_is_clustered(mddev) && ret == -EAGAIN))
8218 printk(KERN_ERR "%s: could not start resync"
8219 " thread...\n",
8220 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008221 /* leave the spares where they are, it shouldn't hurt */
8222 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8223 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8224 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8225 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8226 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008227 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008228 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8229 &mddev->recovery))
8230 if (mddev->sysfs_action)
8231 sysfs_notify_dirent_safe(mddev->sysfs_action);
8232 } else
8233 md_wakeup_thread(mddev->sync_thread);
8234 sysfs_notify_dirent_safe(mddev->sysfs_action);
8235 md_new_event(mddev);
8236}
8237
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238/*
8239 * This routine is regularly called by all per-raid-array threads to
8240 * deal with generic issues like resync and super-block update.
8241 * Raid personalities that don't have a thread (linear/raid0) do not
8242 * need this as they never do any recovery or update the superblock.
8243 *
8244 * It does not do any resync itself, but rather "forks" off other threads
8245 * to do that as needed.
8246 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8247 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008248 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249 * and wakeups up this thread which will reap the thread and finish up.
8250 * This thread also removes any faulty devices (with nr_pending == 0).
8251 *
8252 * The overall approach is:
8253 * 1/ if the superblock needs updating, update it.
8254 * 2/ If a recovery thread is running, don't do anything else.
8255 * 3/ If recovery has finished, clean up, possibly marking spares active.
8256 * 4/ If there are any faulty devices, remove them.
8257 * 5/ If array is degraded, try to add spares devices
8258 * 6/ If array has spares or is not in-sync, start a resync thread.
8259 */
NeilBrownfd01b882011-10-11 16:47:53 +11008260void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008261{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008262 if (mddev->suspended)
8263 return;
8264
NeilBrown5f404022005-06-21 17:17:16 -07008265 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008266 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008267
NeilBrownfca4d842005-06-21 17:17:11 -07008268 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008269 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008270 printk(KERN_INFO "md: %s in immediate safe mode\n",
8271 mdname(mddev));
8272 mddev->safemode = 2;
8273 }
8274 flush_signals(current);
8275 }
8276
NeilBrownc89a8ee2008-08-05 15:54:13 +10008277 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8278 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008279 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008280 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008281 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008282 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008283 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008284 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8285 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008286 ))
8287 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008288
NeilBrowndf5b89b2006-03-27 01:18:20 -08008289 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008290 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008291
NeilBrownc89a8ee2008-08-05 15:54:13 +10008292 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008293 struct md_rdev *rdev;
8294 if (!mddev->external && mddev->in_sync)
8295 /* 'Blocked' flag not needed as failed devices
8296 * will be recorded if array switched to read/write.
8297 * Leaving it set will prevent the device
8298 * from being removed.
8299 */
8300 rdev_for_each(rdev, mddev)
8301 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008302 /* On a read-only array we can:
8303 * - remove failed devices
8304 * - add already-in_sync devices if the array itself
8305 * is in-sync.
8306 * As we only add devices that are already in-sync,
8307 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008308 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008309 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008310 /* There is no thread, but we need to call
8311 * ->spare_active and clear saved_raid_disk
8312 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008313 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008314 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008315 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008316 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Lid4929ad2015-09-18 10:20:12 -07008317 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008318 goto unlock;
8319 }
8320
NeilBrown31a59e32008-04-30 00:52:30 -07008321 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008322 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008323 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008324 if (mddev->safemode &&
8325 !atomic_read(&mddev->writes_pending) &&
8326 !mddev->in_sync &&
8327 mddev->recovery_cp == MaxSector) {
8328 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008329 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008330 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008331 }
8332 if (mddev->safemode == 1)
8333 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008334 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008335 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008336 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008337 }
NeilBrownfca4d842005-06-21 17:17:11 -07008338
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05008339 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07008340 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008341
Linus Torvalds1da177e2005-04-16 15:20:36 -07008342 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8343 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8344 /* resync/recovery still happening */
8345 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8346 goto unlock;
8347 }
8348 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008349 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008350 goto unlock;
8351 }
Neil Brown72a23c22008-06-28 08:31:41 +10008352 /* Set RUNNING before clearing NEEDED to avoid
8353 * any transients in the value of "sync_action".
8354 */
NeilBrown72f36d52012-10-11 14:25:57 +11008355 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008356 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008357 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008358 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008359 /* Clear some bits that don't mean anything, but
8360 * might be left set
8361 */
NeilBrown24dd4692005-11-08 21:39:26 -08008362 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8363 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008364
NeilBrowned209582012-04-24 10:23:14 +10008365 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8366 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008367 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008368 /* no recovery is running.
8369 * remove any failed drives, then
8370 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008371 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008372 * the personality to fail the re-add.
8373 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008375 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008376 if (mddev->pers->check_reshape == NULL ||
8377 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008378 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008379 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008380 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008381 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008382 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008383 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8384 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008385 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008386 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008387 } else if (mddev->recovery_cp < MaxSector) {
8388 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008389 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008390 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8391 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008392 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008393
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008395 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008396 /* We are adding a device or devices to an array
8397 * which has the bitmap stored on all devices.
8398 * So make sure all bitmap pages get written
8399 */
8400 bitmap_write_all(mddev->bitmap);
8401 }
NeilBrownac05f252014-09-30 08:10:42 +10008402 INIT_WORK(&mddev->del_work, md_start_sync);
8403 queue_work(md_misc_wq, &mddev->del_work);
8404 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008405 }
NeilBrownac05f252014-09-30 08:10:42 +10008406 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008407 if (!mddev->sync_thread) {
8408 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008409 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008410 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8411 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008412 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008413 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008414 }
NeilBrownac05f252014-09-30 08:10:42 +10008415 unlock:
8416 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008417 mddev_unlock(mddev);
8418 }
8419}
NeilBrown6c144d32014-09-30 16:15:38 +10008420EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008421
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008422void md_reap_sync_thread(struct mddev *mddev)
8423{
8424 struct md_rdev *rdev;
8425
8426 /* resync has finished, collect result */
8427 md_unregister_thread(&mddev->sync_thread);
8428 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8429 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8430 /* success...*/
8431 /* activate any spares */
8432 if (mddev->pers->spare_active(mddev)) {
8433 sysfs_notify(&mddev->kobj, NULL,
8434 "degraded");
8435 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8436 }
8437 }
8438 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8439 mddev->pers->finish_reshape)
8440 mddev->pers->finish_reshape(mddev);
8441
8442 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008443 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008444 */
NeilBrownf4667222013-12-09 12:04:56 +11008445 if (!mddev->degraded)
8446 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008447 rdev->saved_raid_disk = -1;
8448
8449 md_update_sb(mddev, 1);
8450 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008451 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008452 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8453 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8454 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8455 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008456 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008457 /* flag recovery needed just to double check */
8458 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8459 sysfs_notify_dirent_safe(mddev->sysfs_action);
8460 md_new_event(mddev);
8461 if (mddev->event_work.func)
8462 queue_work(md_misc_wq, &mddev->event_work);
8463}
NeilBrown6c144d32014-09-30 16:15:38 +10008464EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008465
NeilBrownfd01b882011-10-11 16:47:53 +11008466void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008467{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008468 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008469 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008470 !test_bit(Blocked, &rdev->flags) &&
8471 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008472 msecs_to_jiffies(5000));
8473 rdev_dec_pending(rdev, mddev);
8474}
8475EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8476
NeilBrownc6563a82012-05-21 09:27:00 +10008477void md_finish_reshape(struct mddev *mddev)
8478{
8479 /* called be personality module when reshape completes. */
8480 struct md_rdev *rdev;
8481
8482 rdev_for_each(rdev, mddev) {
8483 if (rdev->data_offset > rdev->new_data_offset)
8484 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8485 else
8486 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8487 rdev->data_offset = rdev->new_data_offset;
8488 }
8489}
8490EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008491
8492/* Bad block management.
8493 * We can record which blocks on each device are 'bad' and so just
8494 * fail those blocks, or that stripe, rather than the whole device.
8495 * Entries in the bad-block table are 64bits wide. This comprises:
8496 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8497 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8498 * A 'shift' can be set so that larger blocks are tracked and
8499 * consequently larger devices can be covered.
8500 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8501 *
8502 * Locking of the bad-block table uses a seqlock so md_is_badblock
8503 * might need to retry if it is very unlucky.
8504 * We will sometimes want to check for bad blocks in a bi_end_io function,
8505 * so we use the write_seqlock_irq variant.
8506 *
8507 * When looking for a bad block we specify a range and want to
8508 * know if any block in the range is bad. So we binary-search
8509 * to the last range that starts at-or-before the given endpoint,
8510 * (or "before the sector after the target range")
8511 * then see if it ends after the given start.
8512 * We return
8513 * 0 if there are no known bad blocks in the range
8514 * 1 if there are known bad block which are all acknowledged
8515 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8516 * plus the start/length of the first bad section we overlap.
8517 */
8518int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8519 sector_t *first_bad, int *bad_sectors)
8520{
8521 int hi;
majianpengab056132012-11-06 17:13:44 +08008522 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008523 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008524 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008525 sector_t target = s + sectors;
8526 unsigned seq;
8527
8528 if (bb->shift > 0) {
8529 /* round the start down, and the end up */
8530 s >>= bb->shift;
8531 target += (1<<bb->shift) - 1;
8532 target >>= bb->shift;
8533 sectors = target - s;
8534 }
8535 /* 'target' is now the first block after the bad range */
8536
8537retry:
8538 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008539 lo = 0;
8540 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008541 hi = bb->count;
8542
8543 /* Binary search between lo and hi for 'target'
8544 * i.e. for the last range that starts before 'target'
8545 */
8546 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8547 * are known not to be the last range before target.
8548 * VARIANT: hi-lo is the number of possible
8549 * ranges, and decreases until it reaches 1
8550 */
8551 while (hi - lo > 1) {
8552 int mid = (lo + hi) / 2;
8553 sector_t a = BB_OFFSET(p[mid]);
8554 if (a < target)
8555 /* This could still be the one, earlier ranges
8556 * could not. */
8557 lo = mid;
8558 else
8559 /* This and later ranges are definitely out. */
8560 hi = mid;
8561 }
8562 /* 'lo' might be the last that started before target, but 'hi' isn't */
8563 if (hi > lo) {
8564 /* need to check all range that end after 's' to see if
8565 * any are unacknowledged.
8566 */
8567 while (lo >= 0 &&
8568 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8569 if (BB_OFFSET(p[lo]) < target) {
8570 /* starts before the end, and finishes after
8571 * the start, so they must overlap
8572 */
8573 if (rv != -1 && BB_ACK(p[lo]))
8574 rv = 1;
8575 else
8576 rv = -1;
8577 *first_bad = BB_OFFSET(p[lo]);
8578 *bad_sectors = BB_LEN(p[lo]);
8579 }
8580 lo--;
8581 }
8582 }
8583
8584 if (read_seqretry(&bb->lock, seq))
8585 goto retry;
8586
8587 return rv;
8588}
8589EXPORT_SYMBOL_GPL(md_is_badblock);
8590
8591/*
8592 * Add a range of bad blocks to the table.
8593 * This might extend the table, or might contract it
8594 * if two adjacent ranges can be merged.
8595 * We binary-search to find the 'insertion' point, then
8596 * decide how best to handle it.
8597 */
8598static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8599 int acknowledged)
8600{
8601 u64 *p;
8602 int lo, hi;
8603 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008604 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008605
8606 if (bb->shift < 0)
8607 /* badblocks are disabled */
8608 return 0;
8609
8610 if (bb->shift) {
8611 /* round the start down, and the end up */
8612 sector_t next = s + sectors;
8613 s >>= bb->shift;
8614 next += (1<<bb->shift) - 1;
8615 next >>= bb->shift;
8616 sectors = next - s;
8617 }
8618
Bian Yu905b0292013-10-12 01:10:03 -04008619 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008620
8621 p = bb->page;
8622 lo = 0;
8623 hi = bb->count;
8624 /* Find the last range that starts at-or-before 's' */
8625 while (hi - lo > 1) {
8626 int mid = (lo + hi) / 2;
8627 sector_t a = BB_OFFSET(p[mid]);
8628 if (a <= s)
8629 lo = mid;
8630 else
8631 hi = mid;
8632 }
8633 if (hi > lo && BB_OFFSET(p[lo]) > s)
8634 hi = lo;
8635
8636 if (hi > lo) {
8637 /* we found a range that might merge with the start
8638 * of our new range
8639 */
8640 sector_t a = BB_OFFSET(p[lo]);
8641 sector_t e = a + BB_LEN(p[lo]);
8642 int ack = BB_ACK(p[lo]);
8643 if (e >= s) {
8644 /* Yes, we can merge with a previous range */
8645 if (s == a && s + sectors >= e)
8646 /* new range covers old */
8647 ack = acknowledged;
8648 else
8649 ack = ack && acknowledged;
8650
8651 if (e < s + sectors)
8652 e = s + sectors;
8653 if (e - a <= BB_MAX_LEN) {
8654 p[lo] = BB_MAKE(a, e-a, ack);
8655 s = e;
8656 } else {
8657 /* does not all fit in one range,
8658 * make p[lo] maximal
8659 */
8660 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8661 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8662 s = a + BB_MAX_LEN;
8663 }
8664 sectors = e - s;
8665 }
8666 }
8667 if (sectors && hi < bb->count) {
8668 /* 'hi' points to the first range that starts after 's'.
8669 * Maybe we can merge with the start of that range */
8670 sector_t a = BB_OFFSET(p[hi]);
8671 sector_t e = a + BB_LEN(p[hi]);
8672 int ack = BB_ACK(p[hi]);
8673 if (a <= s + sectors) {
8674 /* merging is possible */
8675 if (e <= s + sectors) {
8676 /* full overlap */
8677 e = s + sectors;
8678 ack = acknowledged;
8679 } else
8680 ack = ack && acknowledged;
8681
8682 a = s;
8683 if (e - a <= BB_MAX_LEN) {
8684 p[hi] = BB_MAKE(a, e-a, ack);
8685 s = e;
8686 } else {
8687 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8688 s = a + BB_MAX_LEN;
8689 }
8690 sectors = e - s;
8691 lo = hi;
8692 hi++;
8693 }
8694 }
8695 if (sectors == 0 && hi < bb->count) {
8696 /* we might be able to combine lo and hi */
8697 /* Note: 's' is at the end of 'lo' */
8698 sector_t a = BB_OFFSET(p[hi]);
8699 int lolen = BB_LEN(p[lo]);
8700 int hilen = BB_LEN(p[hi]);
8701 int newlen = lolen + hilen - (s - a);
8702 if (s >= a && newlen < BB_MAX_LEN) {
8703 /* yes, we can combine them */
8704 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8705 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8706 memmove(p + hi, p + hi + 1,
8707 (bb->count - hi - 1) * 8);
8708 bb->count--;
8709 }
8710 }
8711 while (sectors) {
8712 /* didn't merge (it all).
8713 * Need to add a range just before 'hi' */
8714 if (bb->count >= MD_MAX_BADBLOCKS) {
8715 /* No room for more */
8716 rv = 0;
8717 break;
8718 } else {
8719 int this_sectors = sectors;
8720 memmove(p + hi + 1, p + hi,
8721 (bb->count - hi) * 8);
8722 bb->count++;
8723
8724 if (this_sectors > BB_MAX_LEN)
8725 this_sectors = BB_MAX_LEN;
8726 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8727 sectors -= this_sectors;
8728 s += this_sectors;
8729 }
8730 }
8731
8732 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008733 if (!acknowledged)
8734 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008735 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008736
8737 return rv;
8738}
8739
NeilBrown3cb03002011-10-11 16:45:26 +11008740int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008741 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008742{
NeilBrownc6563a82012-05-21 09:27:00 +10008743 int rv;
8744 if (is_new)
8745 s += rdev->new_data_offset;
8746 else
8747 s += rdev->data_offset;
8748 rv = md_set_badblocks(&rdev->badblocks,
8749 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008750 if (rv) {
8751 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008752 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008753 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
NeilBrown55ce74d2015-08-14 11:11:10 +10008754 set_bit(MD_CHANGE_PENDING, &rdev->mddev->flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008755 md_wakeup_thread(rdev->mddev->thread);
8756 }
8757 return rv;
8758}
8759EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8760
8761/*
8762 * Remove a range of bad blocks from the table.
8763 * This may involve extending the table if we spilt a region,
8764 * but it must not fail. So if the table becomes full, we just
8765 * drop the remove request.
8766 */
8767static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8768{
8769 u64 *p;
8770 int lo, hi;
8771 sector_t target = s + sectors;
8772 int rv = 0;
8773
8774 if (bb->shift > 0) {
8775 /* When clearing we round the start up and the end down.
8776 * This should not matter as the shift should align with
8777 * the block size and no rounding should ever be needed.
8778 * However it is better the think a block is bad when it
8779 * isn't than to think a block is not bad when it is.
8780 */
8781 s += (1<<bb->shift) - 1;
8782 s >>= bb->shift;
8783 target >>= bb->shift;
8784 sectors = target - s;
8785 }
8786
8787 write_seqlock_irq(&bb->lock);
8788
8789 p = bb->page;
8790 lo = 0;
8791 hi = bb->count;
8792 /* Find the last range that starts before 'target' */
8793 while (hi - lo > 1) {
8794 int mid = (lo + hi) / 2;
8795 sector_t a = BB_OFFSET(p[mid]);
8796 if (a < target)
8797 lo = mid;
8798 else
8799 hi = mid;
8800 }
8801 if (hi > lo) {
8802 /* p[lo] is the last range that could overlap the
8803 * current range. Earlier ranges could also overlap,
8804 * but only this one can overlap the end of the range.
8805 */
8806 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8807 /* Partial overlap, leave the tail of this range */
8808 int ack = BB_ACK(p[lo]);
8809 sector_t a = BB_OFFSET(p[lo]);
8810 sector_t end = a + BB_LEN(p[lo]);
8811
8812 if (a < s) {
8813 /* we need to split this range */
8814 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008815 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008816 goto out;
8817 }
8818 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8819 bb->count++;
8820 p[lo] = BB_MAKE(a, s-a, ack);
8821 lo++;
8822 }
8823 p[lo] = BB_MAKE(target, end - target, ack);
8824 /* there is no longer an overlap */
8825 hi = lo;
8826 lo--;
8827 }
8828 while (lo >= 0 &&
8829 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8830 /* This range does overlap */
8831 if (BB_OFFSET(p[lo]) < s) {
8832 /* Keep the early parts of this range. */
8833 int ack = BB_ACK(p[lo]);
8834 sector_t start = BB_OFFSET(p[lo]);
8835 p[lo] = BB_MAKE(start, s - start, ack);
8836 /* now low doesn't overlap, so.. */
8837 break;
8838 }
8839 lo--;
8840 }
8841 /* 'lo' is strictly before, 'hi' is strictly after,
8842 * anything between needs to be discarded
8843 */
8844 if (hi - lo > 1) {
8845 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8846 bb->count -= (hi - lo - 1);
8847 }
8848 }
8849
8850 bb->changed = 1;
8851out:
8852 write_sequnlock_irq(&bb->lock);
8853 return rv;
8854}
8855
NeilBrownc6563a82012-05-21 09:27:00 +10008856int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8857 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008858{
NeilBrownc6563a82012-05-21 09:27:00 +10008859 if (is_new)
8860 s += rdev->new_data_offset;
8861 else
8862 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008863 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008864 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008865}
8866EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8867
8868/*
8869 * Acknowledge all bad blocks in a list.
8870 * This only succeeds if ->changed is clear. It is used by
8871 * in-kernel metadata updates
8872 */
8873void md_ack_all_badblocks(struct badblocks *bb)
8874{
8875 if (bb->page == NULL || bb->changed)
8876 /* no point even trying */
8877 return;
8878 write_seqlock_irq(&bb->lock);
8879
majianpengecb178b2012-03-19 12:46:42 +11008880 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008881 u64 *p = bb->page;
8882 int i;
8883 for (i = 0; i < bb->count ; i++) {
8884 if (!BB_ACK(p[i])) {
8885 sector_t start = BB_OFFSET(p[i]);
8886 int len = BB_LEN(p[i]);
8887 p[i] = BB_MAKE(start, len, 1);
8888 }
8889 }
NeilBrownde393cd2011-07-28 11:31:48 +10008890 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008891 }
8892 write_sequnlock_irq(&bb->lock);
8893}
8894EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8895
NeilBrown16c791a2011-07-28 11:31:47 +10008896/* sysfs access to bad-blocks list.
8897 * We present two files.
8898 * 'bad-blocks' lists sector numbers and lengths of ranges that
8899 * are recorded as bad. The list is truncated to fit within
8900 * the one-page limit of sysfs.
8901 * Writing "sector length" to this file adds an acknowledged
8902 * bad block list.
8903 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8904 * been acknowledged. Writing to this file adds bad blocks
8905 * without acknowledging them. This is largely for testing.
8906 */
8907
8908static ssize_t
8909badblocks_show(struct badblocks *bb, char *page, int unack)
8910{
8911 size_t len;
8912 int i;
8913 u64 *p = bb->page;
8914 unsigned seq;
8915
8916 if (bb->shift < 0)
8917 return 0;
8918
8919retry:
8920 seq = read_seqbegin(&bb->lock);
8921
8922 len = 0;
8923 i = 0;
8924
8925 while (len < PAGE_SIZE && i < bb->count) {
8926 sector_t s = BB_OFFSET(p[i]);
8927 unsigned int length = BB_LEN(p[i]);
8928 int ack = BB_ACK(p[i]);
8929 i++;
8930
8931 if (unack && ack)
8932 continue;
8933
8934 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8935 (unsigned long long)s << bb->shift,
8936 length << bb->shift);
8937 }
NeilBrownde393cd2011-07-28 11:31:48 +10008938 if (unack && len == 0)
8939 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008940
8941 if (read_seqretry(&bb->lock, seq))
8942 goto retry;
8943
8944 return len;
8945}
8946
8947#define DO_DEBUG 1
8948
8949static ssize_t
8950badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8951{
8952 unsigned long long sector;
8953 int length;
8954 char newline;
8955#ifdef DO_DEBUG
8956 /* Allow clearing via sysfs *only* for testing/debugging.
8957 * Normally only a successful write may clear a badblock
8958 */
8959 int clear = 0;
8960 if (page[0] == '-') {
8961 clear = 1;
8962 page++;
8963 }
8964#endif /* DO_DEBUG */
8965
8966 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8967 case 3:
8968 if (newline != '\n')
8969 return -EINVAL;
8970 case 2:
8971 if (length <= 0)
8972 return -EINVAL;
8973 break;
8974 default:
8975 return -EINVAL;
8976 }
8977
8978#ifdef DO_DEBUG
8979 if (clear) {
8980 md_clear_badblocks(bb, sector, length);
8981 return len;
8982 }
8983#endif /* DO_DEBUG */
8984 if (md_set_badblocks(bb, sector, length, !unack))
8985 return len;
8986 else
8987 return -ENOSPC;
8988}
8989
Adrian Bunk75c96f82005-05-05 16:16:09 -07008990static int md_notify_reboot(struct notifier_block *this,
8991 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008992{
8993 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008994 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008995 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008996
NeilBrownc744a652012-03-19 12:46:37 +11008997 for_each_mddev(mddev, tmp) {
8998 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008999 if (mddev->pers)
9000 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009001 if (mddev->persistent)
9002 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009003 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009004 }
NeilBrownc744a652012-03-19 12:46:37 +11009005 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009006 }
NeilBrownc744a652012-03-19 12:46:37 +11009007 /*
9008 * certain more exotic SCSI devices are known to be
9009 * volatile wrt too early system reboots. While the
9010 * right place to handle this issue is the given
9011 * driver, we do want to have a safe RAID driver ...
9012 */
9013 if (need_delay)
9014 mdelay(1000*1);
9015
Linus Torvalds1da177e2005-04-16 15:20:36 -07009016 return NOTIFY_DONE;
9017}
9018
Adrian Bunk75c96f82005-05-05 16:16:09 -07009019static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009020 .notifier_call = md_notify_reboot,
9021 .next = NULL,
9022 .priority = INT_MAX, /* before any real devices */
9023};
9024
9025static void md_geninit(void)
9026{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009027 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009028
Denis V. Lunevc7705f32008-04-29 01:02:35 -07009029 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009030}
9031
Adrian Bunk75c96f82005-05-05 16:16:09 -07009032static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009033{
Tejun Heoe804ac72010-10-15 15:36:08 +02009034 int ret = -ENOMEM;
9035
Tejun Heoada609e2011-01-25 14:35:54 +01009036 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009037 if (!md_wq)
9038 goto err_wq;
9039
9040 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9041 if (!md_misc_wq)
9042 goto err_misc_wq;
9043
9044 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9045 goto err_md;
9046
9047 if ((ret = register_blkdev(0, "mdp")) < 0)
9048 goto err_mdp;
9049 mdp_major = ret;
9050
NeilBrownaf5628f2014-07-31 13:54:54 +10009051 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009052 md_probe, NULL, NULL);
9053 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009054 md_probe, NULL, NULL);
9055
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009057 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009058
9059 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009060 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009061
Tejun Heoe804ac72010-10-15 15:36:08 +02009062err_mdp:
9063 unregister_blkdev(MD_MAJOR, "md");
9064err_md:
9065 destroy_workqueue(md_misc_wq);
9066err_misc_wq:
9067 destroy_workqueue(md_wq);
9068err_wq:
9069 return ret;
9070}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009071
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009072static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009073{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009074 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9075 struct md_rdev *rdev2;
9076 int role, ret;
9077 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009078
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009079 /* Check for change of roles in the active devices */
9080 rdev_for_each(rdev2, mddev) {
9081 if (test_bit(Faulty, &rdev2->flags))
9082 continue;
9083
9084 /* Check if the roles changed */
9085 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009086
9087 if (test_bit(Candidate, &rdev2->flags)) {
9088 if (role == 0xfffe) {
9089 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9090 md_kick_rdev_from_array(rdev2);
9091 continue;
9092 }
9093 else
9094 clear_bit(Candidate, &rdev2->flags);
9095 }
9096
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009097 if (role != rdev2->raid_disk) {
9098 /* got activated */
9099 if (rdev2->raid_disk == -1 && role != 0xffff) {
9100 rdev2->saved_raid_disk = role;
9101 ret = remove_and_add_spares(mddev, rdev2);
9102 pr_info("Activated spare: %s\n",
9103 bdevname(rdev2->bdev,b));
9104 continue;
9105 }
9106 /* device faulty
9107 * We just want to do the minimum to mark the disk
9108 * as faulty. The recovery is performed by the
9109 * one who initiated the error.
9110 */
9111 if ((role == 0xfffe) || (role == 0xfffd)) {
9112 md_error(mddev, rdev2);
9113 clear_bit(Blocked, &rdev2->flags);
9114 }
9115 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009116 }
9117
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009118 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9119 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009120
9121 /* Finally set the event to be up to date */
9122 mddev->events = le64_to_cpu(sb->events);
9123}
9124
9125static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9126{
9127 int err;
9128 struct page *swapout = rdev->sb_page;
9129 struct mdp_superblock_1 *sb;
9130
9131 /* Store the sb page of the rdev in the swapout temporary
9132 * variable in case we err in the future
9133 */
9134 rdev->sb_page = NULL;
9135 alloc_disk_sb(rdev);
9136 ClearPageUptodate(rdev->sb_page);
9137 rdev->sb_loaded = 0;
9138 err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version);
9139
9140 if (err < 0) {
9141 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9142 __func__, __LINE__, rdev->desc_nr, err);
9143 put_page(rdev->sb_page);
9144 rdev->sb_page = swapout;
9145 rdev->sb_loaded = 1;
9146 return err;
9147 }
9148
9149 sb = page_address(rdev->sb_page);
9150 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9151 * is not set
9152 */
9153
9154 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9155 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9156
9157 /* The other node finished recovery, call spare_active to set
9158 * device In_sync and mddev->degraded
9159 */
9160 if (rdev->recovery_offset == MaxSector &&
9161 !test_bit(In_sync, &rdev->flags) &&
9162 mddev->pers->spare_active(mddev))
9163 sysfs_notify(&mddev->kobj, NULL, "degraded");
9164
9165 put_page(swapout);
9166 return 0;
9167}
9168
9169void md_reload_sb(struct mddev *mddev, int nr)
9170{
9171 struct md_rdev *rdev;
9172 int err;
9173
9174 /* Find the rdev */
9175 rdev_for_each_rcu(rdev, mddev) {
9176 if (rdev->desc_nr == nr)
9177 break;
9178 }
9179
9180 if (!rdev || rdev->desc_nr != nr) {
9181 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9182 return;
9183 }
9184
9185 err = read_rdev(mddev, rdev);
9186 if (err < 0)
9187 return;
9188
9189 check_sb_changes(mddev, rdev);
9190
9191 /* Read all rdev's to update recovery_offset */
9192 rdev_for_each_rcu(rdev, mddev)
9193 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009194}
9195EXPORT_SYMBOL(md_reload_sb);
9196
Linus Torvalds1da177e2005-04-16 15:20:36 -07009197#ifndef MODULE
9198
9199/*
9200 * Searches all registered partitions for autorun RAID arrays
9201 * at boot time.
9202 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009203
9204static LIST_HEAD(all_detected_devices);
9205struct detected_devices_node {
9206 struct list_head list;
9207 dev_t dev;
9208};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009209
9210void md_autodetect_dev(dev_t dev)
9211{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009212 struct detected_devices_node *node_detected_dev;
9213
9214 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9215 if (node_detected_dev) {
9216 node_detected_dev->dev = dev;
9217 list_add_tail(&node_detected_dev->list, &all_detected_devices);
9218 } else {
9219 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
9220 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
9221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009222}
9223
Linus Torvalds1da177e2005-04-16 15:20:36 -07009224static void autostart_arrays(int part)
9225{
NeilBrown3cb03002011-10-11 16:45:26 +11009226 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009227 struct detected_devices_node *node_detected_dev;
9228 dev_t dev;
9229 int i_scanned, i_passed;
9230
9231 i_scanned = 0;
9232 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009233
9234 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
9235
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009236 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9237 i_scanned++;
9238 node_detected_dev = list_entry(all_detected_devices.next,
9239 struct detected_devices_node, list);
9240 list_del(&node_detected_dev->list);
9241 dev = node_detected_dev->dev;
9242 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07009243 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009244 if (IS_ERR(rdev))
9245 continue;
9246
NeilBrown403df472014-09-30 15:52:29 +10009247 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009248 continue;
NeilBrown403df472014-09-30 15:52:29 +10009249
NeilBrownd0fae182008-03-04 14:29:31 -08009250 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009251 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009252 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009253 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009254
9255 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
9256 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009257
9258 autorun_devices(part);
9259}
9260
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009261#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009262
9263static __exit void md_exit(void)
9264{
NeilBrownfd01b882011-10-11 16:47:53 +11009265 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009266 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009267 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009268
NeilBrownaf5628f2014-07-31 13:54:54 +10009269 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009270 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009271
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009272 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009273 unregister_blkdev(mdp_major, "mdp");
9274 unregister_reboot_notifier(&md_notifier);
9275 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009276
9277 /* We cannot unload the modules while some process is
9278 * waiting for us in select() or poll() - wake them up
9279 */
9280 md_unloading = 1;
9281 while (waitqueue_active(&md_event_waiters)) {
9282 /* not safe to leave yet */
9283 wake_up(&md_event_waiters);
9284 msleep(delay);
9285 delay += delay;
9286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009287 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009288
NeilBrown29ac4aa2008-02-06 01:39:58 -08009289 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009290 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11009291 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009293 destroy_workqueue(md_misc_wq);
9294 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009295}
9296
Dan Williams685784a2007-07-09 11:56:42 -07009297subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009298module_exit(md_exit)
9299
NeilBrownf91de922005-11-08 21:39:36 -08009300static int get_ro(char *buffer, struct kernel_param *kp)
9301{
9302 return sprintf(buffer, "%d", start_readonly);
9303}
9304static int set_ro(const char *val, struct kernel_param *kp)
9305{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009306 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009307}
9308
NeilBrown80ca3a42006-07-10 04:44:18 -07009309module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9310module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009311module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009312
Linus Torvalds1da177e2005-04-16 15:20:36 -07009313MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009314MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009315MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009316MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);