blob: 9155f00dca20b42e7bf1c52455a599a4f035faaa [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.
NeilBrown9d487392016-11-02 14:16:49 +110033
34 Errors, Warnings, etc.
35 Please use:
36 pr_crit() for error conditions that risk data loss
37 pr_err() for error conditions that are unexpected, like an IO error
38 or internal inconsistency
39 pr_warn() for error conditions that could have been predicated, like
40 adding a device to an array when it has incompatible metadata
41 pr_info() for every interesting, very rare events, like an array starting
42 or stopping, or resync starting or stopping
43 pr_debug() for everything else.
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045*/
46
Ingo Molnar3f07c012017-02-08 18:51:30 +010047#include <linux/sched/signal.h>
NeilBrowna6fb0932005-09-09 16:23:56 -070048#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110049#include <linux/blkdev.h>
Vishal Vermafc974ee2015-12-24 19:20:34 -070050#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040053#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080054#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070055#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080056#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110057#include <linux/hdreg.h>
58#include <linux/proc_fs.h>
59#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040060#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110061#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070062#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110063#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110064#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110065#include <linux/raid/md_p.h>
66#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090067#include <linux/slab.h>
NeilBrown4ad23a972017-03-15 14:05:14 +110068#include <linux/percpu-refcount.h>
69
Shaohua Li504634f2016-11-18 09:44:08 -080070#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110071#include "md.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040072#include "md-bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050073#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110076static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#endif
78
NeilBrown01f96c02011-09-21 15:30:20 +100079/* pers_list is a list of registered personalities protected
80 * by pers_lock.
81 * pers_lock does extra service to protect accesses to
82 * mddev->thread when the mutex cannot be held.
83 */
NeilBrown2604b702006-01-06 00:20:36 -080084static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static DEFINE_SPINLOCK(pers_lock);
86
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050087struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050088EXPORT_SYMBOL(md_cluster_ops);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050089struct module *md_cluster_mod;
90EXPORT_SYMBOL(md_cluster_mod);
91
Bernd Schubert90b08712008-05-23 13:04:38 -070092static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020093static struct workqueue_struct *md_wq;
94static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070095
NeilBrown746d3202013-04-24 11:42:41 +100096static int remove_and_add_spares(struct mddev *mddev,
97 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110098static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
Robert Becker1e509152009-12-14 12:49:58 +1100101 * Default number of read corrections we'll attempt on an rdev
102 * before ejecting it from the array. We divide the read error
103 * count by 2 for every hour elapsed between read errors.
104 */
105#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
106/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
108 * is 1000 KB/sec, so the extra system load does not show up that much.
109 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -0700110 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * subsystem is idle. There is also an 'absolute maximum' reconstruction
112 * speed limit - in case reconstruction slows down your system despite
113 * idle IO detection.
114 *
115 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800116 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118
119static int sysctl_speed_limit_min = 1000;
120static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100121static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800122{
123 return mddev->sync_speed_min ?
124 mddev->sync_speed_min : sysctl_speed_limit_min;
125}
126
NeilBrownfd01b882011-10-11 16:47:53 +1100127static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800128{
129 return mddev->sync_speed_max ?
130 mddev->sync_speed_max : sysctl_speed_limit_max;
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133static struct ctl_table_header *raid_table_header;
134
Joe Perches82592c32013-11-14 15:16:18 +1100135static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .procname = "speed_limit_min",
138 .data = &sysctl_speed_limit_min,
139 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700140 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800141 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 },
143 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .procname = "speed_limit_max",
145 .data = &sysctl_speed_limit_max,
146 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700147 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800148 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800150 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Joe Perches82592c32013-11-14 15:16:18 +1100153static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .procname = "raid",
156 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700157 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .child = raid_table,
159 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800160 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
Joe Perches82592c32013-11-14 15:16:18 +1100163static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 .procname = "dev",
166 .maxlen = 0,
167 .mode = 0555,
168 .child = raid_dir_table,
169 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800170 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700173static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
NeilBrownf91de922005-11-08 21:39:36 -0800175static int start_readonly;
176
NeilBrown78b63502017-04-12 16:26:13 +1000177/*
178 * The original mechanism for creating an md device is to create
179 * a device node in /dev and to open it. This causes races with device-close.
180 * The preferred method is to write to the "new_array" module parameter.
181 * This can avoid races.
182 * Setting create_on_open to false disables the original mechanism
183 * so all the races disappear.
184 */
185static bool create_on_open = true;
186
NeilBrowna167f662010-10-26 18:31:13 +1100187/* bio_clone_mddev
NeilBrown9b10f6a2017-06-18 14:38:59 +1000188 * like bio_clone_bioset, but with a local bio set
NeilBrowna167f662010-10-26 18:31:13 +1100189 */
190
NeilBrowna167f662010-10-26 18:31:13 +1100191struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100192 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100193{
194 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100195
196 if (!mddev || !mddev->bio_set)
197 return bio_alloc(gfp_mask, nr_iovecs);
198
Kent Overstreet395c72a2012-09-06 15:34:55 -0700199 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100200 if (!b)
201 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100202 return b;
203}
204EXPORT_SYMBOL_GPL(bio_alloc_mddev);
205
NeilBrown5a850712017-06-21 09:12:21 +1000206static struct bio *md_bio_alloc_sync(struct mddev *mddev)
207{
Shaohua Li7f053a6a2017-06-23 09:19:49 -0700208 if (!mddev || !mddev->sync_set)
NeilBrown5a850712017-06-21 09:12:21 +1000209 return bio_alloc(GFP_NOIO, 1);
210
211 return bio_alloc_bioset(GFP_NOIO, 1, mddev->sync_set);
212}
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
NeilBrownd7603b72006-01-06 00:20:30 -0800215 * We have a system wide 'event count' that is incremented
216 * on any 'interesting' event, and readers of /proc/mdstat
217 * can use 'poll' or 'select' to find out when the event
218 * count increases.
219 *
220 * Events are:
221 * start array, stop array, error, add device, remove device,
222 * start build, activate spare
223 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800224static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800225static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100226void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800227{
228 atomic_inc(&md_event_count);
229 wake_up(&md_event_waiters);
230}
NeilBrown29269552006-03-27 01:18:10 -0800231EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800232
233/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * Enables to iterate over all existing md arrays
235 * all_mddevs_lock protects this list.
236 */
237static LIST_HEAD(all_mddevs);
238static DEFINE_SPINLOCK(all_mddevs_lock);
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
241 * iterates through all used mddevs in the system.
242 * We take care to grab the all_mddevs_lock whenever navigating
243 * the list, and to always hold a refcount when unlocked.
244 * Any code which breaks out of this loop while own
245 * a reference to the current mddev and must mddev_put it.
246 */
NeilBrownfd01b882011-10-11 16:47:53 +1100247#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000249 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100250 _tmp = all_mddevs.next; \
251 _mddev = NULL;}); \
252 ({ if (_tmp != &all_mddevs) \
253 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100255 if (_mddev) mddev_put(_mddev); \
256 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
257 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100259 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 )
261
NeilBrown409c57f2009-03-31 14:39:39 +1100262/* Rather than calling directly into the personality make_request function,
263 * IO requests come here first so that we can check if the device is
264 * being suspended pending a reconfiguration.
265 * We hold a refcount over the call to ->make_request. By the time that
266 * call has finished, the bio has been linked into some internal structure
267 * and so is visible to ->quiesce(), so we don't need the refcount any more.
268 */
NeilBrownb3143b92017-10-17 13:46:43 +1100269static bool is_suspended(struct mddev *mddev, struct bio *bio)
270{
271 if (mddev->suspended)
272 return true;
273 if (bio_data_dir(bio) != WRITE)
274 return false;
275 if (mddev->suspend_lo >= mddev->suspend_hi)
276 return false;
277 if (bio->bi_iter.bi_sector >= mddev->suspend_hi)
278 return false;
279 if (bio_end_sector(bio) < mddev->suspend_lo)
280 return false;
281 return true;
282}
283
Shaohua Li393debc2017-09-21 10:23:35 -0700284void md_handle_request(struct mddev *mddev, struct bio *bio)
285{
286check_suspended:
287 rcu_read_lock();
NeilBrownb3143b92017-10-17 13:46:43 +1100288 if (is_suspended(mddev, bio)) {
Shaohua Li393debc2017-09-21 10:23:35 -0700289 DEFINE_WAIT(__wait);
290 for (;;) {
291 prepare_to_wait(&mddev->sb_wait, &__wait,
292 TASK_UNINTERRUPTIBLE);
NeilBrownb3143b92017-10-17 13:46:43 +1100293 if (!is_suspended(mddev, bio))
Shaohua Li393debc2017-09-21 10:23:35 -0700294 break;
295 rcu_read_unlock();
296 schedule();
297 rcu_read_lock();
298 }
299 finish_wait(&mddev->sb_wait, &__wait);
300 }
301 atomic_inc(&mddev->active_io);
302 rcu_read_unlock();
303
304 if (!mddev->pers->make_request(mddev, bio)) {
305 atomic_dec(&mddev->active_io);
306 wake_up(&mddev->sb_wait);
307 goto check_suspended;
308 }
309
310 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
311 wake_up(&mddev->sb_wait);
312}
313EXPORT_SYMBOL(md_handle_request);
314
Jens Axboedece1632015-11-05 10:41:16 -0700315static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
NeilBrown49077322010-03-25 16:20:56 +1100317 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100318 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500319 unsigned int sectors;
Gu Zheng74672d02015-04-03 08:44:47 +0800320 int cpu;
NeilBrown49077322010-03-25 16:20:56 +1100321
NeilBrownaf67c312017-06-18 14:38:57 +1000322 blk_queue_split(q, &bio);
Kent Overstreet54efd502015-04-23 22:37:18 -0700323
NeilBrown274d8cb2016-01-04 16:16:58 +1100324 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100325 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700326 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100327 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100328 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200329 if (bio_sectors(bio) != 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200330 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200331 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700332 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100333 }
NeilBrown49077322010-03-25 16:20:56 +1100334
Chris Masone91ece52011-02-07 19:21:48 -0500335 /*
336 * save the sectors now since our bio can
337 * go away inside make_request
338 */
339 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700340 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600341 bio->bi_opf &= ~REQ_NOMERGE;
Shaohua Li393debc2017-09-21 10:23:35 -0700342
343 md_handle_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100344
Gu Zheng74672d02015-04-03 08:44:47 +0800345 cpu = part_stat_lock();
346 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
347 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
348 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100349
Jens Axboedece1632015-11-05 10:41:16 -0700350 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
NeilBrown9e35b992010-04-06 14:23:02 +1000353/* mddev_suspend makes sure no new requests are submitted
354 * to the device, and that any requests that have been submitted
355 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100356 * Once mddev_detach() is called and completes, the module will be
357 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000358 */
NeilBrownfd01b882011-10-11 16:47:53 +1100359void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100360{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200361 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
NeilBrown4d5324f2017-10-19 12:17:16 +1100362 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100363 if (mddev->suspended++)
364 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100365 synchronize_rcu();
NeilBrowncc27b0c2017-06-05 16:49:39 +1000366 wake_up(&mddev->sb_wait);
NeilBrown35bfc522017-10-17 13:46:43 +1100367 set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
368 smp_mb__after_atomic();
NeilBrown409c57f2009-03-31 14:39:39 +1100369 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
370 mddev->pers->quiesce(mddev, 1);
NeilBrown35bfc522017-10-17 13:46:43 +1100371 clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
372 wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500373
374 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100375}
NeilBrown390ee602010-06-01 19:37:27 +1000376EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100377
NeilBrownfd01b882011-10-11 16:47:53 +1100378void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100379{
NeilBrown4d5324f2017-10-19 12:17:16 +1100380 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100381 if (--mddev->suspended)
382 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100383 wake_up(&mddev->sb_wait);
384 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500385
Jonathan Brassow47525e52012-05-22 13:55:29 +1000386 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500387 md_wakeup_thread(mddev->thread);
388 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100389}
NeilBrown390ee602010-06-01 19:37:27 +1000390EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100391
NeilBrownfd01b882011-10-11 16:47:53 +1100392int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000393{
NeilBrown5c675f82014-12-15 12:56:56 +1100394 struct md_personality *pers = mddev->pers;
395 int ret = 0;
396
397 rcu_read_lock();
398 if (mddev->suspended)
399 ret = 1;
400 else if (pers && pers->congested)
401 ret = pers->congested(mddev, bits);
402 rcu_read_unlock();
403 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000404}
NeilBrown5c675f82014-12-15 12:56:56 +1100405EXPORT_SYMBOL_GPL(mddev_congested);
406static int md_congested(void *data, int bits)
407{
408 struct mddev *mddev = data;
409 return mddev_congested(mddev, bits);
410}
NeilBrown3fa841d2009-09-23 18:10:29 +1000411
NeilBrowna2826aa2009-12-14 12:49:49 +1100412/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200413 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100414 */
415
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200416static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100417{
NeilBrown3cb03002011-10-11 16:45:26 +1100418 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100419 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100420
421 rdev_dec_pending(rdev, mddev);
422
423 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200424 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200425 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100426 }
427 bio_put(bio);
428}
429
NeilBrowna7a07e62010-12-09 16:04:25 +1100430static void md_submit_flush_data(struct work_struct *ws);
431
NeilBrowna035fc32010-12-09 16:17:51 +1100432static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100433{
NeilBrownfd01b882011-10-11 16:47:53 +1100434 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100435 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100436
NeilBrowna7a07e62010-12-09 16:04:25 +1100437 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
438 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100439 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100440 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100441 if (rdev->raid_disk >= 0 &&
442 !test_bit(Faulty, &rdev->flags)) {
443 /* Take two references, one is dropped
444 * when request finishes, one after
445 * we reclaim rcu_read_lock
446 */
447 struct bio *bi;
448 atomic_inc(&rdev->nr_pending);
449 atomic_inc(&rdev->nr_pending);
450 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000451 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200452 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100453 bi->bi_private = rdev;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200454 bio_set_dev(bi, rdev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600455 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
NeilBrowna2826aa2009-12-14 12:49:49 +1100456 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500457 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100458 rcu_read_lock();
459 rdev_dec_pending(rdev, mddev);
460 }
461 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100462 if (atomic_dec_and_test(&mddev->flush_pending))
463 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100464}
465
Tejun Heoe9c74692010-09-03 11:56:18 +0200466static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100467{
NeilBrownfd01b882011-10-11 16:47:53 +1100468 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200469 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100470
Shaohua Li79bf31a2017-09-21 09:55:28 -0700471 /*
472 * must reset flush_bio before calling into md_handle_request to avoid a
473 * deadlock, because other bios passed md_handle_request suspend check
474 * could wait for this and below md_handle_request could wait for those
475 * bios because of suspend check
476 */
477 mddev->flush_bio = NULL;
478 wake_up(&mddev->sb_wait);
479
Kent Overstreet4f024f32013-10-11 15:44:27 -0700480 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100481 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200482 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100483 else {
Jens Axboe1eff9d32016-08-05 15:35:16 -0600484 bio->bi_opf &= ~REQ_PREFLUSH;
Shaohua Li79bf31a2017-09-21 09:55:28 -0700485 md_handle_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100486 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100487}
488
NeilBrownfd01b882011-10-11 16:47:53 +1100489void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100490{
NeilBrown85572d72014-12-15 12:56:56 +1100491 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100492 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200493 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100494 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200495 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100496 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100497
NeilBrowna035fc32010-12-09 16:17:51 +1100498 INIT_WORK(&mddev->flush_work, submit_flushes);
499 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100500}
Tejun Heoe9c74692010-09-03 11:56:18 +0200501EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100502
NeilBrownfd01b882011-10-11 16:47:53 +1100503static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 atomic_inc(&mddev->active);
506 return mddev;
507}
508
Dan Williams5fd3a172009-03-04 00:57:25 -0700509static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100510
NeilBrownfd01b882011-10-11 16:47:53 +1100511static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Shaohua Li7184ef82017-07-03 14:34:23 -0700513 struct bio_set *bs = NULL, *sync_bs = NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
516 return;
NeilBrownd3374822009-01-09 08:31:10 +1100517 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100518 mddev->ctime == 0 && !mddev->hold_active) {
519 /* Array is not configured at all, and not held active,
520 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100521 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100522 bs = mddev->bio_set;
Shaohua Li7184ef82017-07-03 14:34:23 -0700523 sync_bs = mddev->sync_set;
NeilBrowna167f662010-10-26 18:31:13 +1100524 mddev->bio_set = NULL;
NeilBrown5a850712017-06-21 09:12:21 +1000525 mddev->sync_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100526 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200527 /* We did a probe so need to clean up. Call
528 * queue_work inside the spinlock so that
529 * flush_workqueue() after mddev_find will
530 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100531 */
532 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200533 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100534 } else
535 kfree(mddev);
536 }
537 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100538 if (bs)
539 bioset_free(bs);
Shaohua Li7184ef82017-07-03 14:34:23 -0700540 if (sync_bs)
541 bioset_free(sync_bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Sasha Levin25b2edf2015-07-24 18:19:58 -0400544static void md_safemode_timeout(unsigned long data);
545
NeilBrownfd01b882011-10-11 16:47:53 +1100546void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100547{
548 mutex_init(&mddev->open_mutex);
549 mutex_init(&mddev->reconfig_mutex);
550 mutex_init(&mddev->bitmap_info.mutex);
551 INIT_LIST_HEAD(&mddev->disks);
552 INIT_LIST_HEAD(&mddev->all_mddevs);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400553 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
554 (unsigned long) mddev);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100555 atomic_set(&mddev->active, 1);
556 atomic_set(&mddev->openers, 0);
557 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100558 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100559 atomic_set(&mddev->flush_pending, 0);
560 init_waitqueue_head(&mddev->sb_wait);
561 init_waitqueue_head(&mddev->recovery_wait);
562 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000563 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500564 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100565 mddev->resync_min = 0;
566 mddev->resync_max = MaxSector;
567 mddev->level = LEVEL_NONE;
568}
NeilBrown390ee602010-06-01 19:37:27 +1000569EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100570
NeilBrownf72ffdd2014-09-30 14:23:59 +1000571static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
NeilBrownfd01b882011-10-11 16:47:53 +1100573 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
NeilBrown8f5f02c2011-02-16 13:58:51 +1100575 if (unit && MAJOR(unit) != MD_MAJOR)
576 unit &= ~((1<<MdpMinorShift)-1);
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 retry:
579 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
NeilBrownefeb53c2009-01-09 08:31:10 +1100581 if (unit) {
582 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
583 if (mddev->unit == unit) {
584 mddev_get(mddev);
585 spin_unlock(&all_mddevs_lock);
586 kfree(new);
587 return mddev;
588 }
589
590 if (new) {
591 list_add(&new->all_mddevs, &all_mddevs);
592 spin_unlock(&all_mddevs_lock);
593 new->hold_active = UNTIL_IOCTL;
594 return new;
595 }
596 } else if (new) {
597 /* find an unused unit number */
598 static int next_minor = 512;
599 int start = next_minor;
600 int is_free = 0;
601 int dev = 0;
602 while (!is_free) {
603 dev = MKDEV(MD_MAJOR, next_minor);
604 next_minor++;
605 if (next_minor > MINORMASK)
606 next_minor = 0;
607 if (next_minor == start) {
608 /* Oh dear, all in use. */
609 spin_unlock(&all_mddevs_lock);
610 kfree(new);
611 return NULL;
612 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000613
NeilBrownefeb53c2009-01-09 08:31:10 +1100614 is_free = 1;
615 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
616 if (mddev->unit == dev) {
617 is_free = 0;
618 break;
619 }
620 }
621 new->unit = dev;
622 new->md_minor = MINOR(dev);
623 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 list_add(&new->all_mddevs, &all_mddevs);
625 spin_unlock(&all_mddevs_lock);
626 return new;
627 }
628 spin_unlock(&all_mddevs_lock);
629
NeilBrown9ffae0c2006-01-06 00:20:32 -0800630 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (!new)
632 return NULL;
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 new->unit = unit;
635 if (MAJOR(unit) == MD_MAJOR)
636 new->md_minor = MINOR(unit);
637 else
638 new->md_minor = MINOR(unit) >> MdpMinorShift;
639
NeilBrownfafd7fb2010-04-01 15:55:30 +1100640 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 goto retry;
643}
644
NeilBrownb6eb1272010-04-15 10:13:47 +1000645static struct attribute_group md_redundancy_group;
646
NeilBrown5c47daf2014-12-15 12:57:01 +1100647void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
NeilBrowna64c8762010-04-14 17:15:37 +1000649 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000650 /* These cannot be removed under reconfig_mutex as
651 * an access to the files will try to take reconfig_mutex
652 * while holding the file unremovable, which leads to
653 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000654 * So hold set sysfs_active while the remove in happeing,
655 * and anything else which might set ->to_remove or my
656 * otherwise change the sysfs namespace will fail with
657 * -EBUSY if sysfs_active is still set.
658 * We set sysfs_active under reconfig_mutex and elsewhere
659 * test it under the same mutex to ensure its correct value
660 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000661 */
NeilBrowna64c8762010-04-14 17:15:37 +1000662 struct attribute_group *to_remove = mddev->to_remove;
663 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000664 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000665 mutex_unlock(&mddev->reconfig_mutex);
666
NeilBrown00bcb4a2010-06-01 19:37:23 +1000667 if (mddev->kobj.sd) {
668 if (to_remove != &md_redundancy_group)
669 sysfs_remove_group(&mddev->kobj, to_remove);
670 if (mddev->pers == NULL ||
671 mddev->pers->sync_request == NULL) {
672 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
673 if (mddev->sysfs_action)
674 sysfs_put(mddev->sysfs_action);
675 mddev->sysfs_action = NULL;
676 }
NeilBrowna64c8762010-04-14 17:15:37 +1000677 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000678 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000679 } else
680 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Chris Dunlop751e67c2011-10-19 16:48:26 +1100682 /* As we've dropped the mutex we need a spinlock to
683 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000684 */
685 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700686 md_wakeup_thread(mddev->thread);
NeilBrown4d5324f2017-10-19 12:17:16 +1100687 wake_up(&mddev->sb_wait);
NeilBrown01f96c02011-09-21 15:30:20 +1000688 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
NeilBrown5c47daf2014-12-15 12:57:01 +1100690EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500692struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100693{
694 struct md_rdev *rdev;
695
696 rdev_for_each_rcu(rdev, mddev)
697 if (rdev->desc_nr == nr)
698 return rdev;
699
700 return NULL;
701}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500702EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100703
704static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
NeilBrown3cb03002011-10-11 16:45:26 +1100706 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
NeilBrowndafb20f2012-03-19 12:46:39 +1100708 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (rdev->bdev->bd_dev == dev)
710 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return NULL;
713}
714
NeilBrown1ca69c42012-10-11 13:37:33 +1100715static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
716{
717 struct md_rdev *rdev;
718
719 rdev_for_each_rcu(rdev, mddev)
720 if (rdev->bdev->bd_dev == dev)
721 return rdev;
722
723 return NULL;
724}
725
NeilBrown84fc4b52011-10-11 16:49:58 +1100726static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800727{
NeilBrown84fc4b52011-10-11 16:49:58 +1100728 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800729 list_for_each_entry(pers, &pers_list, list) {
730 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800731 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800732 if (strcmp(pers->name, clevel)==0)
733 return pers;
734 }
NeilBrown2604b702006-01-06 00:20:36 -0800735 return NULL;
736}
737
Andre Nollb73df2d2008-07-11 22:02:23 +1000738/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100739static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100741 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000742 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
NeilBrownf72ffdd2014-09-30 14:23:59 +1000745static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100748 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000749 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return 0;
751}
752
NeilBrown545c8792012-05-22 13:54:30 +1000753void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800756 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 rdev->sb_loaded = 0;
758 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000759 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100760 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
NeilBrown2699b672011-07-28 11:31:47 +1000762 if (rdev->bb_page) {
763 put_page(rdev->bb_page);
764 rdev->bb_page = NULL;
765 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800766 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
NeilBrown545c8792012-05-22 13:54:30 +1000768EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200770static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700771{
NeilBrown3cb03002011-10-11 16:45:26 +1100772 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100773 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700774
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200775 if (bio->bi_status) {
776 pr_err("md: super_written gets error=%d\n", bio->bi_status);
NeilBrowna9701a32005-11-08 21:39:34 -0800777 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100778 if (!test_bit(Faulty, &rdev->flags)
779 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800780 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100781 set_bit(LastDev, &rdev->flags);
782 }
783 } else
784 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700785
NeilBrowna9701a32005-11-08 21:39:34 -0800786 if (atomic_dec_and_test(&mddev->pending_writes))
787 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700788 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700789 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700790}
791
NeilBrownfd01b882011-10-11 16:47:53 +1100792void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700793 sector_t sector, int size, struct page *page)
794{
795 /* write first size bytes of page to sector of rdev
796 * Increment mddev->pending_writes before returning
797 * and decrement it on completion, waking up sb_wait
798 * if zero is reached.
799 * If an error occurred, call md_error
800 */
NeilBrown46533ff2016-11-18 16:16:11 +1100801 struct bio *bio;
802 int ff = 0;
803
804 if (test_bit(Faulty, &rdev->flags))
805 return;
806
NeilBrown5a850712017-06-21 09:12:21 +1000807 bio = md_bio_alloc_sync(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700808
Shaohua Lied3b98c2016-03-29 14:00:19 -0700809 atomic_inc(&rdev->nr_pending);
810
Christoph Hellwig74d46992017-08-23 19:10:32 +0200811 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700812 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700813 bio_add_page(bio, page, size, 0);
814 bio->bi_private = rdev;
815 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100816
817 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
818 test_bit(FailFast, &rdev->flags) &&
819 !test_bit(LastDev, &rdev->flags))
820 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +0200821 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -0800822
NeilBrown7bfa19f2005-06-21 17:17:28 -0700823 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500824 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800825}
826
NeilBrown46533ff2016-11-18 16:16:11 +1100827int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800828{
Tejun Heoe9c74692010-09-03 11:56:18 +0200829 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000830 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -0800831 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +1100832 return -EAGAIN;
833 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700834}
835
NeilBrown3cb03002011-10-11 16:45:26 +1100836int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -0500837 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
NeilBrown5a850712017-06-21 09:12:21 +1000839 struct bio *bio = md_bio_alloc_sync(rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 int ret;
841
Christoph Hellwig74d46992017-08-23 19:10:32 +0200842 if (metadata_op && rdev->meta_bdev)
843 bio_set_dev(bio, rdev->meta_bdev);
844 else
845 bio_set_dev(bio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -0500846 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100847 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700848 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000849 else if (rdev->mddev->reshape_position != MaxSector &&
850 (rdev->mddev->reshape_backwards ==
851 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700852 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100853 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700854 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500856
857 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200859 ret = !bio->bi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 bio_put(bio);
861 return ret;
862}
NeilBrowna8745db2006-01-06 00:20:34 -0800863EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
NeilBrownf72ffdd2014-09-30 14:23:59 +1000865static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (rdev->sb_loaded)
870 return 0;
871
Mike Christie796a5cf2016-06-05 14:32:07 -0500872 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 goto fail;
874 rdev->sb_loaded = 1;
875 return 0;
876
877fail:
NeilBrown9d487392016-11-02 14:16:49 +1100878 pr_err("md: disabled device %s, could not read superblock.\n",
879 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return -EINVAL;
881}
882
Amir Goldsteine6fd2092017-05-04 16:26:20 +0300883static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000885 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000886 sb1->set_uuid1 == sb2->set_uuid1 &&
887 sb1->set_uuid2 == sb2->set_uuid2 &&
888 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Amir Goldsteine6fd2092017-05-04 16:26:20 +0300891static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 int ret;
894 mdp_super_t *tmp1, *tmp2;
895
896 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
897 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
898
899 if (!tmp1 || !tmp2) {
900 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 goto abort;
902 }
903
904 *tmp1 = *sb1;
905 *tmp2 = *sb2;
906
907 /*
908 * nr_disks is not constant
909 */
910 tmp1->nr_disks = 0;
911 tmp2->nr_disks = 0;
912
Andre Nollce0c8e02008-07-11 22:02:20 +1000913 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700915 kfree(tmp1);
916 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return ret;
918}
919
NeilBrown4d167f02007-05-09 02:35:37 -0700920static u32 md_csum_fold(u32 csum)
921{
922 csum = (csum & 0xffff) + (csum >> 16);
923 return (csum & 0xffff) + (csum >> 16);
924}
925
NeilBrownf72ffdd2014-09-30 14:23:59 +1000926static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
NeilBrown4d167f02007-05-09 02:35:37 -0700928 u64 newcsum = 0;
929 u32 *sb32 = (u32*)sb;
930 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 unsigned int disk_csum, csum;
932
933 disk_csum = sb->sb_csum;
934 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700935
936 for (i = 0; i < MD_SB_BYTES/4 ; i++)
937 newcsum += sb32[i];
938 csum = (newcsum & 0xffffffff) + (newcsum>>32);
939
NeilBrown4d167f02007-05-09 02:35:37 -0700940#ifdef CONFIG_ALPHA
941 /* This used to use csum_partial, which was wrong for several
942 * reasons including that different results are returned on
943 * different architectures. It isn't critical that we get exactly
944 * the same return value as before (we always csum_fold before
945 * testing, and that removes any differences). However as we
946 * know that csum_partial always returned a 16bit value on
947 * alphas, do a fold to maximise conformity to previous behaviour.
948 */
949 sb->sb_csum = md_csum_fold(disk_csum);
950#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700952#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return csum;
954}
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956/*
957 * Handle superblock details.
958 * We want to be able to handle multiple superblock formats
959 * so we have a common interface to them all, and an array of
960 * different handlers.
961 * We rely on user-space to write the initial superblock, and support
962 * reading and updating of superblocks.
963 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100964 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 * loads and validates a superblock on dev.
966 * if refdev != NULL, compare superblocks on both devices
967 * Return:
968 * 0 - dev has a superblock that is compatible with refdev
969 * 1 - dev has a superblock that is compatible and newer than refdev
970 * so dev should be used as the refdev in future
971 * -EINVAL superblock incompatible or invalid
972 * -othererror e.g. -EIO
973 *
NeilBrownfd01b882011-10-11 16:47:53 +1100974 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 * Verify that dev is acceptable into mddev.
976 * The first time, mddev->raid_disks will be 0, and data from
977 * dev should be merged in. Subsequent calls check that dev
978 * is new enough. Return 0 or -EINVAL
979 *
NeilBrownfd01b882011-10-11 16:47:53 +1100980 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 * Update the superblock for rdev with data in mddev
982 * This does not write to disc.
983 *
984 */
985
986struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000987 char *name;
988 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000989 int (*load_super)(struct md_rdev *rdev,
990 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000991 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000992 int (*validate_super)(struct mddev *mddev,
993 struct md_rdev *rdev);
994 void (*sync_super)(struct mddev *mddev,
995 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100996 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000997 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000998 int (*allow_new_offset)(struct md_rdev *rdev,
999 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000};
1001
1002/*
Andre Noll0894cc32009-06-18 08:49:23 +10001003 * Check that the given mddev has no bitmap.
1004 *
1005 * This function is called from the run method of all personalities that do not
1006 * support bitmaps. It prints an error message and returns non-zero if mddev
1007 * has a bitmap. Otherwise, it returns 0.
1008 *
1009 */
NeilBrownfd01b882011-10-11 16:47:53 +11001010int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001011{
NeilBrownc3d97142009-12-14 12:49:52 +11001012 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001013 return 0;
NeilBrown9d487392016-11-02 14:16:49 +11001014 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +10001015 mdname(mddev), mddev->pers->name);
1016 return 1;
1017}
1018EXPORT_SYMBOL(md_check_no_bitmap);
1019
1020/*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001021 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 */
NeilBrown3cb03002011-10-11 16:45:26 +11001023static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1026 mdp_super_t *sb;
1027 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 /*
Andre Noll0f420352008-07-11 22:02:23 +10001030 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 * it's at the end of the disk.
1032 *
1033 * It also happens to be a multiple of 4Kb.
1034 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001035 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
NeilBrown0002b272005-09-09 16:23:53 -07001037 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +11001038 if (ret)
1039 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 ret = -EINVAL;
1042
1043 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001044 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +11001047 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 goto abort;
1049 }
1050
1051 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001052 sb->minor_version < 90 ||
1053 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +11001054 pr_warn("Bad version number %d.%d on %s\n",
1055 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 goto abort;
1057 }
1058
1059 if (sb->raid_disks <= 0)
1060 goto abort;
1061
NeilBrown4d167f02007-05-09 02:35:37 -07001062 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001063 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 goto abort;
1065 }
1066
1067 rdev->preferred_minor = sb->md_minor;
1068 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001069 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001070 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001071 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 if (sb->level == LEVEL_MULTIPATH)
1074 rdev->desc_nr = -1;
1075 else
1076 rdev->desc_nr = sb->this_disk.number;
1077
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001078 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001080 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001082 mdp_super_t *refsb = page_address(refdev->sb_page);
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001083 if (!md_uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001084 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 b, bdevname(refdev->bdev,b2));
1086 goto abort;
1087 }
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001088 if (!md_sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001089 pr_warn("md: %s has same UUID but different superblock to %s\n",
1090 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 goto abort;
1092 }
1093 ev1 = md_event(sb);
1094 ev2 = md_event(refsb);
1095 if (ev1 > ev2)
1096 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001097 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 ret = 0;
1099 }
NeilBrown8190e752009-06-18 08:48:58 +10001100 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001101 /* Limit to 4TB as metadata cannot record more than that.
1102 * (not needed for Linear and RAID0 as metadata doesn't
1103 * record this size)
1104 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001105 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1106 sb->level >= 1)
1107 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
NeilBrown27a7b262011-09-10 17:21:28 +10001109 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001110 /* "this cannot possibly happen" ... */
1111 ret = -EINVAL;
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 abort:
1114 return ret;
1115}
1116
1117/*
1118 * validate_super for 0.90.0
1119 */
NeilBrownfd01b882011-10-11 16:47:53 +11001120static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001123 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001124 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
NeilBrown41158c72005-06-21 17:17:25 -07001126 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001127 clear_bit(Faulty, &rdev->flags);
1128 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001129 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001130 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (mddev->raid_disks == 0) {
1133 mddev->major_version = 0;
1134 mddev->minor_version = sb->minor_version;
1135 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001136 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001137 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 mddev->ctime = sb->ctime;
1139 mddev->utime = sb->utime;
1140 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001141 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 mddev->layout = sb->layout;
1143 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001144 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001145 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001146 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001147 mddev->bitmap_info.space = 0;
1148 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001149 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001150 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001151 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
NeilBrownf6705572006-03-27 01:18:11 -08001153 if (mddev->minor_version >= 91) {
1154 mddev->reshape_position = sb->reshape_position;
1155 mddev->delta_disks = sb->delta_disks;
1156 mddev->new_level = sb->new_level;
1157 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001158 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001159 if (mddev->delta_disks < 0)
1160 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001161 } else {
1162 mddev->reshape_position = MaxSector;
1163 mddev->delta_disks = 0;
1164 mddev->new_level = mddev->level;
1165 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001166 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001167 }
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (sb->state & (1<<MD_SB_CLEAN))
1170 mddev->recovery_cp = MaxSector;
1171 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001172 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 sb->events_lo == sb->cp_events_lo) {
1174 mddev->recovery_cp = sb->recovery_cp;
1175 } else
1176 mddev->recovery_cp = 0;
1177 }
1178
1179 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1180 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1181 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1182 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1183
1184 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001185
1186 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001187 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001188 mddev->bitmap_info.offset =
1189 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001190 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001191 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001192 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001193
NeilBrown41158c72005-06-21 17:17:25 -07001194 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001195 /* Insist on good event counter while assembling, except
1196 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001198 if (sb->disks[rdev->desc_nr].state & (
1199 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001200 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001201 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001202 } else if (mddev->bitmap) {
1203 /* if adding to array with a bitmap, then we can accept an
1204 * older device ... but not too old.
1205 */
NeilBrown41158c72005-06-21 17:17:25 -07001206 if (ev1 < mddev->bitmap->events_cleared)
1207 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001208 if (ev1 < mddev->events)
1209 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001210 } else {
1211 if (ev1 < mddev->events)
1212 /* just a hot-add of a new device, leave raid_disk at -1 */
1213 return 0;
1214 }
NeilBrown41158c72005-06-21 17:17:25 -07001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 desc = sb->disks + rdev->desc_nr;
1218
1219 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001220 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001221 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1222 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001223 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001225 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001226 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1227 /* active but not in sync implies recovery up to
1228 * reshape position. We don't know exactly where
1229 * that is, so set to zero for now */
1230 if (mddev->minor_version >= 91) {
1231 rdev->recovery_offset = 0;
1232 rdev->raid_disk = desc->raid_disk;
1233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001235 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1236 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001237 if (desc->state & (1<<MD_DISK_FAILFAST))
1238 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001239 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001240 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return 0;
1242}
1243
1244/*
1245 * sync_super for 0.90.0
1246 */
NeilBrownfd01b882011-10-11 16:47:53 +11001247static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001250 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /* make rdev->sb match mddev data..
1254 *
1255 * 1/ zero out disks
1256 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1257 * 3/ any empty disks < next_spare become removed
1258 *
1259 * disks[0] gets initialised to REMOVED because
1260 * we cannot be sure from other fields if it has
1261 * been initialised or not.
1262 */
1263 int i;
1264 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1265
NeilBrown61181562005-09-09 16:24:02 -07001266 rdev->sb_size = MD_SB_BYTES;
1267
Namhyung Kim65a06f062011-07-27 11:00:36 +10001268 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 memset(sb, 0, sizeof(*sb));
1271
1272 sb->md_magic = MD_SB_MAGIC;
1273 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 sb->patch_version = mddev->patch_version;
1275 sb->gvalid_words = 0; /* ignored */
1276 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1277 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1278 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1279 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1280
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001281 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001283 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 sb->raid_disks = mddev->raid_disks;
1285 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001286 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001287 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 sb->state = 0;
1289 sb->events_hi = (mddev->events>>32);
1290 sb->events_lo = (u32)mddev->events;
1291
NeilBrownf6705572006-03-27 01:18:11 -08001292 if (mddev->reshape_position == MaxSector)
1293 sb->minor_version = 90;
1294 else {
1295 sb->minor_version = 91;
1296 sb->reshape_position = mddev->reshape_position;
1297 sb->new_level = mddev->new_level;
1298 sb->delta_disks = mddev->delta_disks;
1299 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001300 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001301 }
1302 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (mddev->in_sync)
1304 {
1305 sb->recovery_cp = mddev->recovery_cp;
1306 sb->cp_events_hi = (mddev->events>>32);
1307 sb->cp_events_lo = (u32)mddev->events;
1308 if (mddev->recovery_cp == MaxSector)
1309 sb->state = (1<< MD_SB_CLEAN);
1310 } else
1311 sb->recovery_cp = 0;
1312
1313 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001314 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
NeilBrownc3d97142009-12-14 12:49:52 +11001316 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001317 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001320 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001322 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001323 int is_active = test_bit(In_sync, &rdev2->flags);
1324
1325 if (rdev2->raid_disk >= 0 &&
1326 sb->minor_version >= 91)
1327 /* we have nowhere to store the recovery_offset,
1328 * but if it is not below the reshape_position,
1329 * we can piggy-back on that.
1330 */
1331 is_active = 1;
1332 if (rdev2->raid_disk < 0 ||
1333 test_bit(Faulty, &rdev2->flags))
1334 is_active = 0;
1335 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001336 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001338 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001339 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 d = &sb->disks[rdev2->desc_nr];
1341 nr_disks++;
1342 d->number = rdev2->desc_nr;
1343 d->major = MAJOR(rdev2->bdev->bd_dev);
1344 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001345 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 d->raid_disk = rdev2->raid_disk;
1347 else
1348 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001349 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001351 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001353 if (test_bit(In_sync, &rdev2->flags))
1354 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 active++;
1356 working++;
1357 } else {
1358 d->state = 0;
1359 spare++;
1360 working++;
1361 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001362 if (test_bit(WriteMostly, &rdev2->flags))
1363 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001364 if (test_bit(FailFast, &rdev2->flags))
1365 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /* now set the "removed" and "faulty" bits on any missing devices */
1368 for (i=0 ; i < mddev->raid_disks ; i++) {
1369 mdp_disk_t *d = &sb->disks[i];
1370 if (d->state == 0 && d->number == 0) {
1371 d->number = i;
1372 d->raid_disk = i;
1373 d->state = (1<<MD_DISK_REMOVED);
1374 d->state |= (1<<MD_DISK_FAULTY);
1375 failed++;
1376 }
1377 }
1378 sb->nr_disks = nr_disks;
1379 sb->active_disks = active;
1380 sb->working_disks = working;
1381 sb->failed_disks = failed;
1382 sb->spare_disks = spare;
1383
1384 sb->this_disk = sb->disks[rdev->desc_nr];
1385 sb->sb_csum = calc_sb_csum(sb);
1386}
1387
1388/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001389 * rdev_size_change for 0.90.0
1390 */
1391static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001392super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001393{
Andre Noll58c0fed2009-03-31 14:33:13 +11001394 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001395 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001396 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001397 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001398 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001399 if (!num_sectors || num_sectors > rdev->sb_start)
1400 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001401 /* Limit to 4TB as metadata cannot record more than that.
1402 * 4TB == 2^32 KB, or 2*2^32 sectors.
1403 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001404 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1405 rdev->mddev->level >= 1)
1406 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001407 do {
1408 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001409 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001410 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001411 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001412}
1413
NeilBrownc6563a82012-05-21 09:27:00 +10001414static int
1415super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1416{
1417 /* non-zero offset changes not possible with v0.90 */
1418 return new_offset == 0;
1419}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001420
1421/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 * version 1 superblock
1423 */
1424
NeilBrownf72ffdd2014-09-30 14:23:59 +10001425static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001427 __le32 disk_csum;
1428 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 unsigned long long newcsum;
1430 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001431 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 disk_csum = sb->sb_csum;
1434 sb->sb_csum = 0;
1435 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001436 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 newcsum += le32_to_cpu(*isuper++);
1438
1439 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001440 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1443 sb->sb_csum = disk_csum;
1444 return cpu_to_le32(csum);
1445}
1446
NeilBrown3cb03002011-10-11 16:45:26 +11001447static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 struct mdp_superblock_1 *sb;
1450 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001451 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001452 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001454 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 /*
Andre Noll0f420352008-07-11 22:02:23 +10001457 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 * It is always aligned to a 4K boundary and
1459 * depeding on minor_version, it can be:
1460 * 0: At least 8K, but less than 12K, from end of device
1461 * 1: At start of device
1462 * 2: 4K from start of device.
1463 */
1464 switch(minor_version) {
1465 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001466 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001467 sb_start -= 8*2;
1468 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 break;
1470 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001471 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 break;
1473 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001474 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 break;
1476 default:
1477 return -EINVAL;
1478 }
Andre Noll0f420352008-07-11 22:02:23 +10001479 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
NeilBrown0002b272005-09-09 16:23:53 -07001481 /* superblock is rarely larger than 1K, but it can be larger,
1482 * and it is safe to read 4k, so we do that
1483 */
1484 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (ret) return ret;
1486
Namhyung Kim65a06f062011-07-27 11:00:36 +10001487 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1490 sb->major_version != cpu_to_le32(1) ||
1491 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001492 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001493 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -EINVAL;
1495
1496 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001497 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 bdevname(rdev->bdev,b));
1499 return -EINVAL;
1500 }
1501 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001502 pr_warn("md: data_size too small on %s\n",
1503 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return -EINVAL;
1505 }
NeilBrownc6563a82012-05-21 09:27:00 +10001506 if (sb->pad0 ||
1507 sb->pad3[0] ||
1508 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1509 /* Some padding is non-zero, might be a new feature */
1510 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 rdev->preferred_minor = 0xffff;
1513 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001514 rdev->new_data_offset = rdev->data_offset;
1515 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1516 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1517 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001518 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
NeilBrown0002b272005-09-09 16:23:53 -07001520 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001521 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001522 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001523 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1524
1525 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001526 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001527 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001528 if (minor_version
1529 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1530 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001531
NeilBrown31b65a02006-07-10 04:44:14 -07001532 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1533 rdev->desc_nr = -1;
1534 else
1535 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1536
NeilBrown2699b672011-07-28 11:31:47 +10001537 if (!rdev->bb_page) {
1538 rdev->bb_page = alloc_page(GFP_KERNEL);
1539 if (!rdev->bb_page)
1540 return -ENOMEM;
1541 }
1542 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1543 rdev->badblocks.count == 0) {
1544 /* need to load the bad block list.
1545 * Currently we limit it to one page.
1546 */
1547 s32 offset;
1548 sector_t bb_sector;
1549 u64 *bbp;
1550 int i;
1551 int sectors = le16_to_cpu(sb->bblog_size);
1552 if (sectors > (PAGE_SIZE / 512))
1553 return -EINVAL;
1554 offset = le32_to_cpu(sb->bblog_offset);
1555 if (offset == 0)
1556 return -EINVAL;
1557 bb_sector = (long long)offset;
1558 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001559 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001560 return -EIO;
1561 bbp = (u64 *)page_address(rdev->bb_page);
1562 rdev->badblocks.shift = sb->bblog_shift;
1563 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1564 u64 bb = le64_to_cpu(*bbp);
1565 int count = bb & (0x3ff);
1566 u64 sector = bb >> 10;
1567 sector <<= sb->bblog_shift;
1568 count <<= sb->bblog_shift;
1569 if (bb + 1 == 0)
1570 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001571 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001572 return -EINVAL;
1573 }
NeilBrown486adf72013-04-24 11:42:44 +10001574 } else if (sb->bblog_offset != 0)
1575 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001576
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001577 if ((le32_to_cpu(sb->feature_map) &
1578 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001579 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1580 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1581 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1582 }
1583
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001584 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001585 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001586 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001588 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1591 sb->level != refsb->level ||
1592 sb->layout != refsb->layout ||
1593 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001594 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 bdevname(rdev->bdev,b),
1596 bdevname(refdev->bdev,b2));
1597 return -EINVAL;
1598 }
1599 ev1 = le64_to_cpu(sb->events);
1600 ev2 = le64_to_cpu(refsb->events);
1601
1602 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001603 ret = 1;
1604 else
1605 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
NeilBrownc6563a82012-05-21 09:27:00 +10001607 if (minor_version) {
1608 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1609 sectors -= rdev->data_offset;
1610 } else
1611 sectors = rdev->sb_start;
1612 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001614 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001615 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
NeilBrownfd01b882011-10-11 16:47:53 +11001618static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001620 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001621 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
NeilBrown41158c72005-06-21 17:17:25 -07001623 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001624 clear_bit(Faulty, &rdev->flags);
1625 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001626 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001627 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (mddev->raid_disks == 0) {
1630 mddev->major_version = 1;
1631 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001632 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001633 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001634 mddev->ctime = le64_to_cpu(sb->ctime);
1635 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001637 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 mddev->layout = le32_to_cpu(sb->layout);
1639 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001640 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001641 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001642 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001643 mddev->bitmap_info.space = 0;
1644 /* Default location for bitmap is 1K after superblock
1645 * using 3K - total of 4K
1646 */
NeilBrownc3d97142009-12-14 12:49:52 +11001647 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001648 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001649 mddev->reshape_backwards = 0;
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1652 memcpy(mddev->uuid, sb->set_uuid, 16);
1653
1654 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001655
NeilBrown71c08052005-09-09 16:23:51 -07001656 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001657 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001658 mddev->bitmap_info.offset =
1659 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001660 /* Metadata doesn't record how much space is available.
1661 * For 1.0, we assume we can use up to the superblock
1662 * if before, else to 4K beyond superblock.
1663 * For others, assume no change is possible.
1664 */
1665 if (mddev->minor_version > 0)
1666 mddev->bitmap_info.space = 0;
1667 else if (mddev->bitmap_info.offset > 0)
1668 mddev->bitmap_info.space =
1669 8 - mddev->bitmap_info.offset;
1670 else
1671 mddev->bitmap_info.space =
1672 -mddev->bitmap_info.offset;
1673 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001674
NeilBrownf6705572006-03-27 01:18:11 -08001675 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1676 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1677 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1678 mddev->new_level = le32_to_cpu(sb->new_level);
1679 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001680 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001681 if (mddev->delta_disks < 0 ||
1682 (mddev->delta_disks == 0 &&
1683 (le32_to_cpu(sb->feature_map)
1684 & MD_FEATURE_RESHAPE_BACKWARDS)))
1685 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001686 } else {
1687 mddev->reshape_position = MaxSector;
1688 mddev->delta_disks = 0;
1689 mddev->new_level = mddev->level;
1690 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001691 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001692 }
1693
Song Liu486b0f72016-08-19 15:34:01 -07001694 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001695 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001696
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001697 if (le32_to_cpu(sb->feature_map) &
1698 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001699 if (le32_to_cpu(sb->feature_map) &
1700 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1701 return -EINVAL;
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001702 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1703 (le32_to_cpu(sb->feature_map) &
1704 MD_FEATURE_MULTIPLE_PPLS))
1705 return -EINVAL;
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001706 set_bit(MD_HAS_PPL, &mddev->flags);
1707 }
NeilBrown41158c72005-06-21 17:17:25 -07001708 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001709 /* Insist of good event counter while assembling, except for
1710 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001712 if (rdev->desc_nr >= 0 &&
1713 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001714 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1715 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001716 if (ev1 < mddev->events)
1717 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001718 } else if (mddev->bitmap) {
1719 /* If adding to array with a bitmap, then we can accept an
1720 * older device, but not too old.
1721 */
NeilBrown41158c72005-06-21 17:17:25 -07001722 if (ev1 < mddev->bitmap->events_cleared)
1723 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001724 if (ev1 < mddev->events)
1725 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001726 } else {
1727 if (ev1 < mddev->events)
1728 /* just a hot-add of a new device, leave raid_disk at -1 */
1729 return 0;
1730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (mddev->level != LEVEL_MULTIPATH) {
1732 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001733 if (rdev->desc_nr < 0 ||
1734 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001735 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001736 rdev->desc_nr = -1;
1737 } else
1738 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001740 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001742 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001743 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 break;
Song Liubac624f2015-08-13 14:31:55 -07001745 case MD_DISK_ROLE_JOURNAL: /* journal device */
1746 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1747 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001748 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001749 return -EINVAL;
1750 }
1751 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001752 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001753 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001754 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 default:
NeilBrownf4667222013-12-09 12:04:56 +11001756 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001757 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001758 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001759 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001760 if (!(le32_to_cpu(sb->feature_map) &
1761 MD_FEATURE_RECOVERY_BITMAP))
1762 rdev->saved_raid_disk = -1;
1763 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001764 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 rdev->raid_disk = role;
1766 break;
1767 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001768 if (sb->devflags & WriteMostly1)
1769 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001770 if (sb->devflags & FailFast1)
1771 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001772 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1773 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001774 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001775 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return 0;
1778}
1779
NeilBrownfd01b882011-10-11 16:47:53 +11001780static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
1782 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001783 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 int max_dev, i;
1785 /* make rdev->sb match mddev and rdev data. */
1786
Namhyung Kim65a06f062011-07-27 11:00:36 +10001787 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 sb->feature_map = 0;
1790 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001791 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 memset(sb->pad3, 0, sizeof(sb->pad3));
1793
1794 sb->utime = cpu_to_le64((__u64)mddev->utime);
1795 sb->events = cpu_to_le64(mddev->events);
1796 if (mddev->in_sync)
1797 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001798 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1799 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 else
1801 sb->resync_offset = cpu_to_le64(0);
1802
NeilBrown1c05b4b2006-10-21 10:24:08 -07001803 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001804
NeilBrownf0ca3402006-02-02 14:28:04 -08001805 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001806 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001807 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001808 sb->level = cpu_to_le32(mddev->level);
1809 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11001810 if (test_bit(FailFast, &rdev->flags))
1811 sb->devflags |= FailFast1;
1812 else
1813 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08001814
NeilBrownaeb9b2112011-08-25 14:43:08 +10001815 if (test_bit(WriteMostly, &rdev->flags))
1816 sb->devflags |= WriteMostly1;
1817 else
1818 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001819 sb->data_offset = cpu_to_le64(rdev->data_offset);
1820 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001821
NeilBrownc3d97142009-12-14 12:49:52 +11001822 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1823 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001824 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001825 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001826
Shaohua Lif2076e72015-10-08 21:54:12 -07001827 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001828 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001829 sb->feature_map |=
1830 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1831 sb->recovery_offset =
1832 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001833 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1834 sb->feature_map |=
1835 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001836 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001837 /* Note: recovery_offset and journal_tail share space */
1838 if (test_bit(Journal, &rdev->flags))
1839 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001840 if (test_bit(Replacement, &rdev->flags))
1841 sb->feature_map |=
1842 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001843
NeilBrownf6705572006-03-27 01:18:11 -08001844 if (mddev->reshape_position != MaxSector) {
1845 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1846 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1847 sb->new_layout = cpu_to_le32(mddev->new_layout);
1848 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1849 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001850 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001851 if (mddev->delta_disks == 0 &&
1852 mddev->reshape_backwards)
1853 sb->feature_map
1854 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001855 if (rdev->new_data_offset != rdev->data_offset) {
1856 sb->feature_map
1857 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1858 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1859 - rdev->data_offset));
1860 }
NeilBrownf6705572006-03-27 01:18:11 -08001861 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001862
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001863 if (mddev_is_clustered(mddev))
1864 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1865
NeilBrown2699b672011-07-28 11:31:47 +10001866 if (rdev->badblocks.count == 0)
1867 /* Nothing to do for bad blocks*/ ;
1868 else if (sb->bblog_offset == 0)
1869 /* Cannot record bad blocks on this device */
1870 md_error(mddev, rdev);
1871 else {
1872 struct badblocks *bb = &rdev->badblocks;
1873 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1874 u64 *p = bb->page;
1875 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1876 if (bb->changed) {
1877 unsigned seq;
1878
1879retry:
1880 seq = read_seqbegin(&bb->lock);
1881
1882 memset(bbp, 0xff, PAGE_SIZE);
1883
1884 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001885 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001886 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1887 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001888 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001889 }
NeilBrownd0962932012-03-19 12:46:41 +11001890 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001891 if (read_seqretry(&bb->lock, seq))
1892 goto retry;
1893
1894 bb->sector = (rdev->sb_start +
1895 (int)le32_to_cpu(sb->bblog_offset));
1896 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001897 }
1898 }
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001901 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (rdev2->desc_nr+1 > max_dev)
1903 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001904
NeilBrown70471da2009-08-03 10:59:57 +10001905 if (max_dev > le32_to_cpu(sb->max_dev)) {
1906 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001907 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001908 rdev->sb_size = max_dev * 2 + 256;
1909 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1910 if (rdev->sb_size & bmask)
1911 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001912 } else
1913 max_dev = le32_to_cpu(sb->max_dev);
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 for (i=0; i<max_dev;i++)
Lidong Zhong8df72022017-06-12 10:45:55 +08001916 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001917
Song Liua97b7892015-10-08 21:54:09 -07001918 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1919 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001921 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001922 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
1923 sb->feature_map |=
1924 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
1925 else
1926 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001927 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
1928 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
1929 }
1930
NeilBrowndafb20f2012-03-19 12:46:39 +11001931 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001933 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001934 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001935 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001937 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001938 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001939 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001940 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 else
Song Liuc4d4c912015-08-13 14:31:54 -07001942 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 sb->sb_csum = calc_sb_1_csum(sb);
1946}
1947
Chris Webb0cd17fe2008-06-28 08:31:46 +10001948static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001949super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001950{
1951 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001952 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001953 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001954 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001955 if (rdev->data_offset != rdev->new_data_offset)
1956 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001957 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001958 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001959 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001960 max_sectors -= rdev->data_offset;
1961 if (!num_sectors || num_sectors > max_sectors)
1962 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001963 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001964 /* minor version 0 with bitmap we can't move */
1965 return 0;
1966 } else {
1967 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001968 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001969 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001970 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001971 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001972 if (!num_sectors || num_sectors > max_sectors)
1973 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001974 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001975 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001976 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001977 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08001978 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10001979 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11001980 do {
1981 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1982 rdev->sb_page);
1983 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001984 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001985
1986}
1987
1988static int
1989super_1_allow_new_offset(struct md_rdev *rdev,
1990 unsigned long long new_offset)
1991{
1992 /* All necessary checks on new >= old have been done */
1993 struct bitmap *bitmap;
1994 if (new_offset >= rdev->data_offset)
1995 return 1;
1996
1997 /* with 1.0 metadata, there is no metadata to tread on
1998 * so we can always move back */
1999 if (rdev->mddev->minor_version == 0)
2000 return 1;
2001
2002 /* otherwise we must be sure not to step on
2003 * any metadata, so stay:
2004 * 36K beyond start of superblock
2005 * beyond end of badblocks
2006 * beyond write-intent bitmap
2007 */
2008 if (rdev->sb_start + (32+4)*2 > new_offset)
2009 return 0;
2010 bitmap = rdev->mddev->bitmap;
2011 if (bitmap && !rdev->mddev->bitmap_info.file &&
2012 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10002013 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10002014 return 0;
2015 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2016 return 0;
2017
2018 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002019}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Adrian Bunk75c96f82005-05-05 16:16:09 -07002021static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 [0] = {
2023 .name = "0.90.0",
2024 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002025 .load_super = super_90_load,
2026 .validate_super = super_90_validate,
2027 .sync_super = super_90_sync,
2028 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002029 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 },
2031 [1] = {
2032 .name = "md-1",
2033 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002034 .load_super = super_1_load,
2035 .validate_super = super_1_validate,
2036 .sync_super = super_1_sync,
2037 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002038 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 },
2040};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
NeilBrownfd01b882011-10-11 16:47:53 +11002042static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05002043{
2044 if (mddev->sync_super) {
2045 mddev->sync_super(mddev, rdev);
2046 return;
2047 }
2048
2049 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2050
2051 super_types[mddev->major_version].sync_super(mddev, rdev);
2052}
2053
NeilBrownfd01b882011-10-11 16:47:53 +11002054static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
NeilBrown3cb03002011-10-11 16:45:26 +11002056 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
NeilBrown4b809912008-07-21 17:05:25 +10002058 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07002059 rdev_for_each_rcu(rdev, mddev1) {
2060 if (test_bit(Faulty, &rdev->flags) ||
2061 test_bit(Journal, &rdev->flags) ||
2062 rdev->raid_disk == -1)
2063 continue;
2064 rdev_for_each_rcu(rdev2, mddev2) {
2065 if (test_bit(Faulty, &rdev2->flags) ||
2066 test_bit(Journal, &rdev2->flags) ||
2067 rdev2->raid_disk == -1)
2068 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002069 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002070 rdev2->bdev->bd_contains) {
2071 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002072 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002073 }
Song Liu0b020e82015-09-03 23:00:35 -07002074 }
2075 }
NeilBrown4b809912008-07-21 17:05:25 +10002076 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return 0;
2078}
2079
2080static LIST_HEAD(pending_raid_disks);
2081
Andre Nollac5e7112009-08-03 10:59:47 +10002082/*
2083 * Try to register data integrity profile for an mddev
2084 *
2085 * This is called when an array is started and after a disk has been kicked
2086 * from the array. It only succeeds if all working and active component devices
2087 * are integrity capable with matching profiles.
2088 */
NeilBrownfd01b882011-10-11 16:47:53 +11002089int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002090{
NeilBrown3cb03002011-10-11 16:45:26 +11002091 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002092
Andre Nollac5e7112009-08-03 10:59:47 +10002093 if (list_empty(&mddev->disks))
2094 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002095 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2096 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002097 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002098 /* skip spares and non-functional disks */
2099 if (test_bit(Faulty, &rdev->flags))
2100 continue;
2101 if (rdev->raid_disk < 0)
2102 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002103 if (!reference) {
2104 /* Use the first rdev as the reference */
2105 reference = rdev;
2106 continue;
2107 }
2108 /* does this rdev's profile match the reference profile? */
2109 if (blk_integrity_compare(reference->bdev->bd_disk,
2110 rdev->bdev->bd_disk) < 0)
2111 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002112 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002113 if (!reference || !bdev_get_integrity(reference->bdev))
2114 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002115 /*
2116 * All component devices are integrity capable and have matching
2117 * profiles, register the common profile for the md device.
2118 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002119 blk_integrity_register(mddev->gendisk,
2120 bdev_get_integrity(reference->bdev));
2121
NeilBrown9d487392016-11-02 14:16:49 +11002122 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Martin K. Petersena91a2782011-03-17 11:11:05 +01002123 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002124 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002125 mdname(mddev));
2126 return -EINVAL;
2127 }
Andre Nollac5e7112009-08-03 10:59:47 +10002128 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002129}
Andre Nollac5e7112009-08-03 10:59:47 +10002130EXPORT_SYMBOL(md_integrity_register);
2131
Dan Williams1501efa2016-01-13 16:00:07 -08002132/*
2133 * Attempt to add an rdev, but only if it is consistent with the current
2134 * integrity profile
2135 */
2136int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002137{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002138 struct blk_integrity *bi_rdev;
2139 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002140 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002141
2142 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002143 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002144
2145 bi_rdev = bdev_get_integrity(rdev->bdev);
2146 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002147
2148 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002149 return 0;
2150
2151 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002152 pr_err("%s: incompatible integrity profile for %s\n",
2153 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002154 return -ENXIO;
2155 }
2156
2157 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002158}
2159EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002160
NeilBrownf72ffdd2014-09-30 14:23:59 +10002161static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002163 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002164 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002165 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Dan Williams11e2ede2008-04-30 00:52:32 -07002167 /* prevent duplicates */
2168 if (find_rdev(mddev, rdev->bdev->bd_dev))
2169 return -EEXIST;
2170
NeilBrown97b20ef2017-04-13 08:53:48 +10002171 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2172 mddev->pers)
2173 return -EROFS;
2174
Andre Nolldd8ac332009-03-31 14:33:13 +11002175 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002176 if (!test_bit(Journal, &rdev->flags) &&
2177 rdev->sectors &&
2178 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002179 if (mddev->pers) {
2180 /* Cannot change size, so fail
2181 * If mddev->level <= 0, then we don't care
2182 * about aligning sizes (e.g. linear)
2183 */
2184 if (mddev->level > 0)
2185 return -ENOSPC;
2186 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002187 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 /* Verify rdev->desc_nr is unique.
2191 * If it is -1, assign a free number, else
2192 * check number is not in use
2193 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002194 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (rdev->desc_nr < 0) {
2196 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002197 if (mddev->pers)
2198 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002199 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 choice++;
2201 rdev->desc_nr = choice;
2202 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002203 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002204 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002208 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002209 if (!test_bit(Journal, &rdev->flags) &&
2210 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002211 pr_warn("md: %s: array is limited to %d devices\n",
2212 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002213 return -EBUSY;
2214 }
NeilBrown19133a42005-11-08 21:39:35 -08002215 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002216 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002219 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002220
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002221 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002222 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002223
Tejun Heo0762b8b2008-08-25 19:56:12 +09002224 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002225 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2226 /* failure here is OK */;
2227 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002228
NeilBrown4b809912008-07-21 17:05:25 +10002229 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002230 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002231
2232 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002233 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002236
2237 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002238 pr_warn("md: failed to register dev-%s for %s\n",
2239 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002240 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
NeilBrown177a99b2008-02-06 01:39:56 -08002243static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002244{
NeilBrown3cb03002011-10-11 16:45:26 +11002245 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002246 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002247 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002248}
2249
NeilBrownf72ffdd2014-09-30 14:23:59 +10002250static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
2252 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002253
Tejun Heo49731ba2011-01-14 18:43:57 +01002254 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002255 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002256 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002258 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002259 sysfs_put(rdev->sysfs_state);
2260 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002261 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002262 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002263 * writing to 'remove' to "dev/state". We also need
2264 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002265 */
NeilBrown4b809912008-07-21 17:05:25 +10002266 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002267 INIT_WORK(&rdev->del_work, md_delayed_delete);
2268 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002269 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
2272/*
2273 * prevent the device from being mounted, repartitioned or
2274 * otherwise reused by a RAID array (or any other kernel
2275 * subsystem), by bd_claiming the device.
2276 */
NeilBrown3cb03002011-10-11 16:45:26 +11002277static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
2279 int err = 0;
2280 struct block_device *bdev;
2281 char b[BDEVNAME_SIZE];
2282
Tejun Heod4d77622010-11-13 11:55:18 +01002283 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002284 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002286 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return PTR_ERR(bdev);
2288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 rdev->bdev = bdev;
2290 return err;
2291}
2292
NeilBrown3cb03002011-10-11 16:45:26 +11002293static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
2295 struct block_device *bdev = rdev->bdev;
2296 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002297 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
2299
2300void md_autodetect_dev(dev_t dev);
2301
NeilBrownf72ffdd2014-09-30 14:23:59 +10002302static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
2304 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002305
NeilBrown9d487392016-11-02 14:16:49 +11002306 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002307 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002309 if (test_bit(AutoDetected, &rdev->flags))
2310 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311#endif
2312 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002313 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
2315
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002316void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
2318 unbind_rdev_from_array(rdev);
2319 export_rdev(rdev);
2320}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002321EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
NeilBrownfd01b882011-10-11 16:47:53 +11002323static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
NeilBrown0638bb02014-09-25 17:43:47 +10002325 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
NeilBrown0638bb02014-09-25 17:43:47 +10002327 while (!list_empty(&mddev->disks)) {
2328 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2329 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002330 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 mddev->raid_disks = 0;
2333 mddev->major_version = 0;
2334}
2335
NeilBrown6497709b2017-03-15 14:05:14 +11002336static bool set_in_sync(struct mddev *mddev)
2337{
Shaohua Lied9b66d2017-07-25 15:18:13 -07002338 WARN_ON_ONCE(NR_CPUS != 1 && !spin_is_locked(&mddev->lock));
NeilBrown4ad23a972017-03-15 14:05:14 +11002339 if (!mddev->in_sync) {
2340 mddev->sync_checkers++;
2341 spin_unlock(&mddev->lock);
2342 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2343 spin_lock(&mddev->lock);
2344 if (!mddev->in_sync &&
2345 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002346 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002347 /*
2348 * Ensure ->in_sync is visible before we clear
2349 * ->sync_checkers.
2350 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002351 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002352 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2353 sysfs_notify_dirent_safe(mddev->sysfs_state);
2354 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002355 if (--mddev->sync_checkers == 0)
2356 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002357 }
2358 if (mddev->safemode == 1)
2359 mddev->safemode = 0;
2360 return mddev->in_sync;
2361}
2362
NeilBrownf72ffdd2014-09-30 14:23:59 +10002363static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
NeilBrown42543762006-06-26 00:27:57 -07002365 /* Update each superblock (in-memory image), but
2366 * if we are allowed to, skip spares which already
2367 * have the right event counter, or have one earlier
2368 * (which would mean they aren't being marked as dirty
2369 * with the rest of the array)
2370 */
NeilBrown3cb03002011-10-11 16:45:26 +11002371 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002372 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002373 if (rdev->sb_events == mddev->events ||
2374 (nospares &&
2375 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002376 rdev->sb_events+1 == mddev->events)) {
2377 /* Don't update this superblock */
2378 rdev->sb_loaded = 2;
2379 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002380 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002381 rdev->sb_loaded = 1;
2382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
2384}
2385
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002386static bool does_sb_need_changing(struct mddev *mddev)
2387{
2388 struct md_rdev *rdev;
2389 struct mdp_superblock_1 *sb;
2390 int role;
2391
2392 /* Find a good rdev */
2393 rdev_for_each(rdev, mddev)
2394 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2395 break;
2396
2397 /* No good device found. */
2398 if (!rdev)
2399 return false;
2400
2401 sb = page_address(rdev->sb_page);
2402 /* Check if a device has become faulty or a spare become active */
2403 rdev_for_each(rdev, mddev) {
2404 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2405 /* Device activated? */
2406 if (role == 0xffff && rdev->raid_disk >=0 &&
2407 !test_bit(Faulty, &rdev->flags))
2408 return true;
2409 /* Device turned faulty? */
2410 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2411 return true;
2412 }
2413
2414 /* Check if any mddev parameters have changed */
2415 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2416 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002417 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002418 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2419 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2420 return true;
2421
2422 return false;
2423}
2424
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002425void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
NeilBrown3cb03002011-10-11 16:45:26 +11002427 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002428 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002429 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002430 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002431 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
NeilBrownd87f0642013-04-24 11:42:40 +10002433 if (mddev->ro) {
2434 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002435 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002436 return;
2437 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002438
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002439repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002440 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002441 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002442 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002443 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002444 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002445 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002446 /* Has someone else has updated the sb */
2447 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002448 if (ret == 0)
2449 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002450 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2451 BIT(MD_SB_CHANGE_DEVS) |
2452 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002453 return;
2454 }
2455 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002456
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002457 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002458 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002459 if (rdev->raid_disk >= 0 &&
2460 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002461 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002462 !test_bit(In_sync, &rdev->flags) &&
2463 mddev->curr_resync_completed > rdev->recovery_offset)
2464 rdev->recovery_offset = mddev->curr_resync_completed;
2465
NeilBrownf72ffdd2014-09-30 14:23:59 +10002466 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002467 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002468 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2469 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002470 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002471 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002472 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002473 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002474 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002475 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002476 md_error(mddev, rdev);
2477 }
2478 clear_bit(Blocked, &rdev->flags);
2479 clear_bit(BlockedBadBlocks, &rdev->flags);
2480 wake_up(&rdev->blocked_wait);
2481 }
2482 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002483 wake_up(&mddev->sb_wait);
2484 return;
2485 }
2486
NeilBrown85572d72014-12-15 12:56:56 +11002487 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002488
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002489 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002490
Shaohua Li29530792016-12-08 15:48:19 -08002491 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b42006-10-03 01:15:46 -07002492 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002493 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b42006-10-03 01:15:46 -07002494 /* just a clean<-> dirty transition, possibly leave spares alone,
2495 * though if events isn't the right even/odd, we will have to do
2496 * spares after all
2497 */
2498 nospares = 1;
2499 if (force_change)
2500 nospares = 0;
2501 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002502 /* If the array is degraded, then skipping spares is both
2503 * dangerous and fairly pointless.
2504 * Dangerous because a device that was removed from the array
2505 * might have a event_count that still looks up-to-date,
2506 * so it can be re-added without a resync.
2507 * Pointless because if there are any spares to skip,
2508 * then a recovery will happen and soon that array won't
2509 * be degraded any more and the spare can go back to sleep then.
2510 */
NeilBrown850b2b42006-10-03 01:15:46 -07002511 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002512
NeilBrown06d91a52005-06-21 17:17:12 -07002513 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002514
2515 /* If this is just a dirty<->clean transition, and the array is clean
2516 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002517 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002518 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002519 && mddev->can_decrease_events
2520 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002521 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002522 mddev->can_decrease_events = 0;
2523 } else {
NeilBrown42543762006-06-26 00:27:57 -07002524 /* otherwise we have to go forward and ... */
2525 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002526 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
NeilBrown403df472014-09-30 15:52:29 +10002529 /*
2530 * This 64-bit counter should never wrap.
2531 * Either we are in around ~1 trillion A.C., assuming
2532 * 1 reboot per second, or we have a bug...
2533 */
2534 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002535
NeilBrowndafb20f2012-03-19 12:46:39 +11002536 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002537 if (rdev->badblocks.changed)
2538 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002539 if (test_bit(Faulty, &rdev->flags))
2540 set_bit(FaultRecorded, &rdev->flags);
2541 }
NeilBrown2699b672011-07-28 11:31:47 +10002542
NeilBrowne6910632008-02-06 01:39:51 -08002543 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002544 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
NeilBrown36a4e1f2011-10-07 14:23:17 +11002546 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2547 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Shaohua Li504634f2016-11-18 09:44:08 -08002549 if (mddev->queue)
2550 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002551rewrite:
NeilBrown4ad13662007-07-17 04:06:13 -07002552 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002553 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002555
NeilBrown42543762006-06-26 00:27:57 -07002556 if (rdev->sb_loaded != 1)
2557 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
NeilBrownf4667222013-12-09 12:04:56 +11002559 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002560 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002561 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002562 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002563 pr_debug("md: (write) %s's sb offset: %llu\n",
2564 bdevname(rdev->bdev, b),
2565 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002566 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002567 if (rdev->badblocks.size) {
2568 md_super_write(mddev, rdev,
2569 rdev->badblocks.sector,
2570 rdev->badblocks.size << 9,
2571 rdev->bb_page);
2572 rdev->badblocks.size = 0;
2573 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002574
NeilBrownf4667222013-12-09 12:04:56 +11002575 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002576 pr_debug("md: %s (skipping faulty)\n",
2577 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002578
NeilBrown7bfa19f2005-06-21 17:17:28 -07002579 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 /* only need to write one superblock... */
2581 break;
2582 }
NeilBrown46533ff2016-11-18 16:16:11 +11002583 if (md_super_wait(mddev) < 0)
2584 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002585 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002586
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002587 if (mddev_is_clustered(mddev) && ret == 0)
2588 md_cluster_ops->metadata_update_finish(mddev);
2589
NeilBrown850b2b42006-10-03 01:15:46 -07002590 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002591 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2592 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002593 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002594 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002595 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002596 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2597 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002598
NeilBrowndafb20f2012-03-19 12:46:39 +11002599 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002600 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2601 clear_bit(Blocked, &rdev->flags);
2602
2603 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002604 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002605 clear_bit(BlockedBadBlocks, &rdev->flags);
2606 wake_up(&rdev->blocked_wait);
2607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002609EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002611static int add_bound_rdev(struct md_rdev *rdev)
2612{
2613 struct mddev *mddev = rdev->mddev;
2614 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002615 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002616
Shaohua Li87d4d912016-01-06 14:37:14 -08002617 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002618 /* If there is hot_add_disk but no hot_remove_disk
2619 * then added disks for geometry changes,
2620 * and should be added immediately.
2621 */
2622 super_types[mddev->major_version].
2623 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002624 if (add_journal)
2625 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002626 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002627 if (add_journal)
2628 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002629 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002630 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002631 return err;
2632 }
2633 }
2634 sysfs_notify_dirent_safe(rdev->sysfs_state);
2635
Shaohua Li29530792016-12-08 15:48:19 -08002636 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002637 if (mddev->degraded)
2638 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2639 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2640 md_new_event(mddev);
2641 md_wakeup_thread(mddev->thread);
2642 return 0;
2643}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Andre Noll7f6ce762008-03-23 18:34:54 +01002645/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002646 * We want to accept with case. For this we use cmd_match.
2647 */
2648static int cmd_match(const char *cmd, const char *str)
2649{
2650 /* See if cmd, written into a sysfs file, matches
2651 * str. They must either be the same, or cmd can
2652 * have a trailing newline
2653 */
2654 while (*cmd && *str && *cmd == *str) {
2655 cmd++;
2656 str++;
2657 }
2658 if (*cmd == '\n')
2659 cmd++;
2660 if (*str || *cmd)
2661 return 0;
2662 return 1;
2663}
2664
NeilBrown86e6ffd2005-11-08 21:39:24 -08002665struct rdev_sysfs_entry {
2666 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002667 ssize_t (*show)(struct md_rdev *, char *);
2668 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002669};
2670
2671static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002672state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002673{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002674 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002675 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002676 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002677
NeilBrown758bfc82014-12-15 12:56:59 +11002678 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002679 (!test_bit(ExternalBbl, &flags) &&
2680 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002681 len += sprintf(page+len, "faulty%s", sep);
2682 if (test_bit(In_sync, &flags))
2683 len += sprintf(page+len, "in_sync%s", sep);
2684 if (test_bit(Journal, &flags))
2685 len += sprintf(page+len, "journal%s", sep);
2686 if (test_bit(WriteMostly, &flags))
2687 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002688 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002689 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002690 && !test_bit(Faulty, &flags)))
2691 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002692 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002693 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002694 !test_bit(In_sync, &flags))
2695 len += sprintf(page+len, "spare%s", sep);
2696 if (test_bit(WriteErrorSeen, &flags))
2697 len += sprintf(page+len, "write_error%s", sep);
2698 if (test_bit(WantReplacement, &flags))
2699 len += sprintf(page+len, "want_replacement%s", sep);
2700 if (test_bit(Replacement, &flags))
2701 len += sprintf(page+len, "replacement%s", sep);
2702 if (test_bit(ExternalBbl, &flags))
2703 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002704 if (test_bit(FailFast, &flags))
2705 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002706
2707 if (len)
2708 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002709
NeilBrown86e6ffd2005-11-08 21:39:24 -08002710 return len+sprintf(page+len, "\n");
2711}
2712
NeilBrown45dc2de2006-06-26 00:27:58 -07002713static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002714state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002715{
2716 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002717 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002718 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002719 * writemostly - sets write_mostly
2720 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002721 * blocked - sets the Blocked flags
2722 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002723 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002724 * -insync - clear Insync for a device with a slot assigned,
2725 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002726 * write_error - sets WriteErrorSeen
2727 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002728 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002729 */
2730 int err = -EINVAL;
2731 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2732 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002733 if (test_bit(Faulty, &rdev->flags))
2734 err = 0;
2735 else
2736 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002737 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002738 if (rdev->mddev->pers) {
2739 clear_bit(Blocked, &rdev->flags);
2740 remove_and_add_spares(rdev->mddev, rdev);
2741 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002742 if (rdev->raid_disk >= 0)
2743 err = -EBUSY;
2744 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002745 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002746 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002747 if (mddev_is_clustered(mddev))
2748 err = md_cluster_ops->remove_disk(mddev, rdev);
2749
2750 if (err == 0) {
2751 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002752 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002753 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002754 md_wakeup_thread(mddev->thread);
2755 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002756 md_new_event(mddev);
2757 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002758 }
NeilBrownf6556752006-06-26 00:28:01 -07002759 } else if (cmd_match(buf, "writemostly")) {
2760 set_bit(WriteMostly, &rdev->flags);
2761 err = 0;
2762 } else if (cmd_match(buf, "-writemostly")) {
2763 clear_bit(WriteMostly, &rdev->flags);
2764 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002765 } else if (cmd_match(buf, "blocked")) {
2766 set_bit(Blocked, &rdev->flags);
2767 err = 0;
2768 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002769 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002770 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002771 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002772 /* metadata handler doesn't understand badblocks,
2773 * so we need to fail the device
2774 */
2775 md_error(rdev->mddev, rdev);
2776 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002777 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002778 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002779 wake_up(&rdev->blocked_wait);
2780 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2781 md_wakeup_thread(rdev->mddev->thread);
2782
2783 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002784 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2785 set_bit(In_sync, &rdev->flags);
2786 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11002787 } else if (cmd_match(buf, "failfast")) {
2788 set_bit(FailFast, &rdev->flags);
2789 err = 0;
2790 } else if (cmd_match(buf, "-failfast")) {
2791 clear_bit(FailFast, &rdev->flags);
2792 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002793 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2794 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002795 if (rdev->mddev->pers == NULL) {
2796 clear_bit(In_sync, &rdev->flags);
2797 rdev->saved_raid_disk = rdev->raid_disk;
2798 rdev->raid_disk = -1;
2799 err = 0;
2800 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002801 } else if (cmd_match(buf, "write_error")) {
2802 set_bit(WriteErrorSeen, &rdev->flags);
2803 err = 0;
2804 } else if (cmd_match(buf, "-write_error")) {
2805 clear_bit(WriteErrorSeen, &rdev->flags);
2806 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002807 } else if (cmd_match(buf, "want_replacement")) {
2808 /* Any non-spare device that is not a replacement can
2809 * become want_replacement at any time, but we then need to
2810 * check if recovery is needed.
2811 */
2812 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002813 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002814 !test_bit(Replacement, &rdev->flags))
2815 set_bit(WantReplacement, &rdev->flags);
2816 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2817 md_wakeup_thread(rdev->mddev->thread);
2818 err = 0;
2819 } else if (cmd_match(buf, "-want_replacement")) {
2820 /* Clearing 'want_replacement' is always allowed.
2821 * Once replacements starts it is too late though.
2822 */
2823 err = 0;
2824 clear_bit(WantReplacement, &rdev->flags);
2825 } else if (cmd_match(buf, "replacement")) {
2826 /* Can only set a device as a replacement when array has not
2827 * yet been started. Once running, replacement is automatic
2828 * from spares, or by assigning 'slot'.
2829 */
2830 if (rdev->mddev->pers)
2831 err = -EBUSY;
2832 else {
2833 set_bit(Replacement, &rdev->flags);
2834 err = 0;
2835 }
2836 } else if (cmd_match(buf, "-replacement")) {
2837 /* Similarly, can only clear Replacement before start */
2838 if (rdev->mddev->pers)
2839 err = -EBUSY;
2840 else {
2841 clear_bit(Replacement, &rdev->flags);
2842 err = 0;
2843 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002844 } else if (cmd_match(buf, "re-add")) {
2845 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002846 /* clear_bit is performed _after_ all the devices
2847 * have their local Faulty bit cleared. If any writes
2848 * happen in the meantime in the local node, they
2849 * will land in the local bitmap, which will be synced
2850 * by this node eventually
2851 */
2852 if (!mddev_is_clustered(rdev->mddev) ||
2853 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2854 clear_bit(Faulty, &rdev->flags);
2855 err = add_bound_rdev(rdev);
2856 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002857 } else
2858 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002859 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2860 set_bit(ExternalBbl, &rdev->flags);
2861 rdev->badblocks.shift = 0;
2862 err = 0;
2863 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2864 clear_bit(ExternalBbl, &rdev->flags);
2865 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002866 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002867 if (!err)
2868 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002869 return err ? err : len;
2870}
NeilBrown80ca3a42006-07-10 04:44:18 -07002871static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002872__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002873
2874static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002875errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002876{
2877 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2878}
2879
2880static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002881errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002882{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002883 unsigned int n;
2884 int rv;
2885
2886 rv = kstrtouint(buf, 10, &n);
2887 if (rv < 0)
2888 return rv;
2889 atomic_set(&rdev->corrected_errors, n);
2890 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002891}
2892static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002893__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002894
NeilBrown014236d2006-01-06 00:20:55 -08002895static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002896slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002897{
Shaohua Lif2076e72015-10-08 21:54:12 -07002898 if (test_bit(Journal, &rdev->flags))
2899 return sprintf(page, "journal\n");
2900 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002901 return sprintf(page, "none\n");
2902 else
2903 return sprintf(page, "%d\n", rdev->raid_disk);
2904}
2905
2906static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002907slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002908{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002909 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002910 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002911
Shaohua Lif2076e72015-10-08 21:54:12 -07002912 if (test_bit(Journal, &rdev->flags))
2913 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002914 if (strncmp(buf, "none", 4)==0)
2915 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002916 else {
2917 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2918 if (err < 0)
2919 return err;
2920 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002921 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002922 /* Setting 'slot' on an active array requires also
2923 * updating the 'rd%d' link, and communicating
2924 * with the personality with ->hot_*_disk.
2925 * For now we only support removing
2926 * failed/spare devices. This normally happens automatically,
2927 * but not when the metadata is externally managed.
2928 */
NeilBrownc303da62008-02-06 01:39:51 -08002929 if (rdev->raid_disk == -1)
2930 return -EEXIST;
2931 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002932 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002933 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002934 clear_bit(Blocked, &rdev->flags);
2935 remove_and_add_spares(rdev->mddev, rdev);
2936 if (rdev->raid_disk >= 0)
2937 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002938 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2939 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002940 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002941 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002942 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002943 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002944 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002945
2946 if (rdev->raid_disk != -1)
2947 return -EBUSY;
2948
NeilBrownc6751b22011-02-02 11:57:13 +11002949 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2950 return -EBUSY;
2951
Neil Brown6c2fce22008-06-28 08:31:31 +10002952 if (rdev->mddev->pers->hot_add_disk == NULL)
2953 return -EINVAL;
2954
NeilBrownba1b41b2011-01-14 09:14:34 +11002955 if (slot >= rdev->mddev->raid_disks &&
2956 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2957 return -ENOSPC;
2958
Neil Brown6c2fce22008-06-28 08:31:31 +10002959 rdev->raid_disk = slot;
2960 if (test_bit(In_sync, &rdev->flags))
2961 rdev->saved_raid_disk = slot;
2962 else
2963 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002964 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002965 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002966 err = rdev->mddev->pers->
2967 hot_add_disk(rdev->mddev, rdev);
2968 if (err) {
2969 rdev->raid_disk = -1;
2970 return err;
2971 } else
2972 sysfs_notify_dirent_safe(rdev->sysfs_state);
2973 if (sysfs_link_rdev(rdev->mddev, rdev))
2974 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002975 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002976 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002977 if (slot >= rdev->mddev->raid_disks &&
2978 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002979 return -ENOSPC;
2980 rdev->raid_disk = slot;
2981 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002982 clear_bit(Faulty, &rdev->flags);
2983 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002984 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002985 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002986 }
NeilBrown014236d2006-01-06 00:20:55 -08002987 return len;
2988}
2989
NeilBrown014236d2006-01-06 00:20:55 -08002990static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002991__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002992
NeilBrown93c8cad2006-01-06 00:20:56 -08002993static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002994offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002995{
Andrew Morton6961ece2006-01-06 00:20:59 -08002996 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002997}
2998
2999static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003000offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08003001{
NeilBrownc6563a82012-05-21 09:27:00 +10003002 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003003 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003004 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10003005 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003006 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003007 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08003008 /* Must set offset before size, so overlap checks
3009 * can be sane */
3010 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08003011 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10003012 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08003013 return len;
3014}
3015
3016static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07003017__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08003018
NeilBrownc6563a82012-05-21 09:27:00 +10003019static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3020{
3021 return sprintf(page, "%llu\n",
3022 (unsigned long long)rdev->new_data_offset);
3023}
3024
3025static ssize_t new_offset_store(struct md_rdev *rdev,
3026 const char *buf, size_t len)
3027{
3028 unsigned long long new_offset;
3029 struct mddev *mddev = rdev->mddev;
3030
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003031 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10003032 return -EINVAL;
3033
NeilBrownf851b602014-12-11 10:02:10 +11003034 if (mddev->sync_thread ||
3035 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10003036 return -EBUSY;
3037 if (new_offset == rdev->data_offset)
3038 /* reset is always permitted */
3039 ;
3040 else if (new_offset > rdev->data_offset) {
3041 /* must not push array size beyond rdev_sectors */
3042 if (new_offset - rdev->data_offset
3043 + mddev->dev_sectors > rdev->sectors)
3044 return -E2BIG;
3045 }
3046 /* Metadata worries about other space details. */
3047
3048 /* decreasing the offset is inconsistent with a backwards
3049 * reshape.
3050 */
3051 if (new_offset < rdev->data_offset &&
3052 mddev->reshape_backwards)
3053 return -EINVAL;
3054 /* Increasing offset is inconsistent with forwards
3055 * reshape. reshape_direction should be set to
3056 * 'backwards' first.
3057 */
3058 if (new_offset > rdev->data_offset &&
3059 !mddev->reshape_backwards)
3060 return -EINVAL;
3061
3062 if (mddev->pers && mddev->persistent &&
3063 !super_types[mddev->major_version]
3064 .allow_new_offset(rdev, new_offset))
3065 return -E2BIG;
3066 rdev->new_data_offset = new_offset;
3067 if (new_offset > rdev->data_offset)
3068 mddev->reshape_backwards = 1;
3069 else if (new_offset < rdev->data_offset)
3070 mddev->reshape_backwards = 0;
3071
3072 return len;
3073}
3074static struct rdev_sysfs_entry rdev_new_offset =
3075__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3076
NeilBrown83303b62006-01-06 00:21:06 -08003077static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003078rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003079{
Andre Nolldd8ac332009-03-31 14:33:13 +11003080 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003081}
3082
NeilBrownc5d79ad2008-02-06 01:39:54 -08003083static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3084{
3085 /* check if two start/length pairs overlap */
3086 if (s1+l1 <= s2)
3087 return 0;
3088 if (s2+l2 <= s1)
3089 return 0;
3090 return 1;
3091}
3092
Dan Williamsb522adc2009-03-31 15:00:31 +11003093static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3094{
3095 unsigned long long blocks;
3096 sector_t new;
3097
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003098 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003099 return -EINVAL;
3100
3101 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3102 return -EINVAL; /* sector conversion overflow */
3103
3104 new = blocks * 2;
3105 if (new != blocks * 2)
3106 return -EINVAL; /* unsigned long long to sector_t overflow */
3107
3108 *sectors = new;
3109 return 0;
3110}
3111
NeilBrown83303b62006-01-06 00:21:06 -08003112static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003113rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003114{
NeilBrownfd01b882011-10-11 16:47:53 +11003115 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003116 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003117 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003118
Shaohua Lif2076e72015-10-08 21:54:12 -07003119 if (test_bit(Journal, &rdev->flags))
3120 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003121 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003122 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003123 if (rdev->data_offset != rdev->new_data_offset)
3124 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003125 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003126 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003127 sectors = super_types[my_mddev->major_version].
3128 rdev_size_change(rdev, sectors);
3129 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003130 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003131 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003132 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003133 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003134 if (!my_mddev->pers->resize)
3135 /* Cannot change size for RAID0 or Linear etc */
3136 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003137 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003138 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003139 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003140
Andre Nolldd8ac332009-03-31 14:33:13 +11003141 rdev->sectors = sectors;
3142 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003143 /* Need to check that all other rdevs with the same
3144 * ->bdev do not overlap. 'rcu' is sufficient to walk
3145 * the rdev lists safely.
3146 * This check does not provide a hard guarantee, it
3147 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003148 */
NeilBrownfd01b882011-10-11 16:47:53 +11003149 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003150 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003151 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003152
NeilBrown8b1afc32014-09-29 15:33:20 +10003153 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003154 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003155 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003156
NeilBrowndafb20f2012-03-19 12:46:39 +11003157 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003158 if (rdev->bdev == rdev2->bdev &&
3159 rdev != rdev2 &&
3160 overlaps(rdev->data_offset, rdev->sectors,
3161 rdev2->data_offset,
3162 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003163 overlap = 1;
3164 break;
3165 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003166 if (overlap) {
3167 mddev_put(mddev);
3168 break;
3169 }
3170 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003171 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003172 if (overlap) {
3173 /* Someone else could have slipped in a size
3174 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003175 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003176 * safe, and trust userspace not to race with
3177 * itself
3178 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003179 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003180 return -EBUSY;
3181 }
3182 }
NeilBrown83303b62006-01-06 00:21:06 -08003183 return len;
3184}
3185
3186static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003187__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003188
NeilBrown3cb03002011-10-11 16:45:26 +11003189static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003190{
3191 unsigned long long recovery_start = rdev->recovery_offset;
3192
3193 if (test_bit(In_sync, &rdev->flags) ||
3194 recovery_start == MaxSector)
3195 return sprintf(page, "none\n");
3196
3197 return sprintf(page, "%llu\n", recovery_start);
3198}
3199
NeilBrown3cb03002011-10-11 16:45:26 +11003200static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003201{
3202 unsigned long long recovery_start;
3203
3204 if (cmd_match(buf, "none"))
3205 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003206 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003207 return -EINVAL;
3208
3209 if (rdev->mddev->pers &&
3210 rdev->raid_disk >= 0)
3211 return -EBUSY;
3212
3213 rdev->recovery_offset = recovery_start;
3214 if (recovery_start == MaxSector)
3215 set_bit(In_sync, &rdev->flags);
3216 else
3217 clear_bit(In_sync, &rdev->flags);
3218 return len;
3219}
3220
3221static struct rdev_sysfs_entry rdev_recovery_start =
3222__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3223
Vishal Vermafc974ee2015-12-24 19:20:34 -07003224/* sysfs access to bad-blocks list.
3225 * We present two files.
3226 * 'bad-blocks' lists sector numbers and lengths of ranges that
3227 * are recorded as bad. The list is truncated to fit within
3228 * the one-page limit of sysfs.
3229 * Writing "sector length" to this file adds an acknowledged
3230 * bad block list.
3231 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3232 * been acknowledged. Writing to this file adds bad blocks
3233 * without acknowledging them. This is largely for testing.
3234 */
NeilBrown3cb03002011-10-11 16:45:26 +11003235static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003236{
3237 return badblocks_show(&rdev->badblocks, page, 0);
3238}
NeilBrown3cb03002011-10-11 16:45:26 +11003239static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003240{
NeilBrownde393cd2011-07-28 11:31:48 +10003241 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3242 /* Maybe that ack was all we needed */
3243 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3244 wake_up(&rdev->blocked_wait);
3245 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003246}
3247static struct rdev_sysfs_entry rdev_bad_blocks =
3248__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3249
NeilBrown3cb03002011-10-11 16:45:26 +11003250static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003251{
3252 return badblocks_show(&rdev->badblocks, page, 1);
3253}
NeilBrown3cb03002011-10-11 16:45:26 +11003254static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003255{
3256 return badblocks_store(&rdev->badblocks, page, len, 1);
3257}
3258static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3259__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3260
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003261static ssize_t
3262ppl_sector_show(struct md_rdev *rdev, char *page)
3263{
3264 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3265}
3266
3267static ssize_t
3268ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3269{
3270 unsigned long long sector;
3271
3272 if (kstrtoull(buf, 10, &sector) < 0)
3273 return -EINVAL;
3274 if (sector != (sector_t)sector)
3275 return -EINVAL;
3276
3277 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3278 rdev->raid_disk >= 0)
3279 return -EBUSY;
3280
3281 if (rdev->mddev->persistent) {
3282 if (rdev->mddev->major_version == 0)
3283 return -EINVAL;
3284 if ((sector > rdev->sb_start &&
3285 sector - rdev->sb_start > S16_MAX) ||
3286 (sector < rdev->sb_start &&
3287 rdev->sb_start - sector > -S16_MIN))
3288 return -EINVAL;
3289 rdev->ppl.offset = sector - rdev->sb_start;
3290 } else if (!rdev->mddev->external) {
3291 return -EBUSY;
3292 }
3293 rdev->ppl.sector = sector;
3294 return len;
3295}
3296
3297static struct rdev_sysfs_entry rdev_ppl_sector =
3298__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3299
3300static ssize_t
3301ppl_size_show(struct md_rdev *rdev, char *page)
3302{
3303 return sprintf(page, "%u\n", rdev->ppl.size);
3304}
3305
3306static ssize_t
3307ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3308{
3309 unsigned int size;
3310
3311 if (kstrtouint(buf, 10, &size) < 0)
3312 return -EINVAL;
3313
3314 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3315 rdev->raid_disk >= 0)
3316 return -EBUSY;
3317
3318 if (rdev->mddev->persistent) {
3319 if (rdev->mddev->major_version == 0)
3320 return -EINVAL;
3321 if (size > U16_MAX)
3322 return -EINVAL;
3323 } else if (!rdev->mddev->external) {
3324 return -EBUSY;
3325 }
3326 rdev->ppl.size = size;
3327 return len;
3328}
3329
3330static struct rdev_sysfs_entry rdev_ppl_size =
3331__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3332
NeilBrown86e6ffd2005-11-08 21:39:24 -08003333static struct attribute *rdev_default_attrs[] = {
3334 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003335 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003336 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003337 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003338 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003339 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003340 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003341 &rdev_bad_blocks.attr,
3342 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003343 &rdev_ppl_sector.attr,
3344 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003345 NULL,
3346};
3347static ssize_t
3348rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3349{
3350 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003351 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003352
3353 if (!entry->show)
3354 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003355 if (!rdev->mddev)
3356 return -EBUSY;
3357 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003358}
3359
3360static ssize_t
3361rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3362 const char *page, size_t length)
3363{
3364 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003365 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003366 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003367 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003368
3369 if (!entry->store)
3370 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003371 if (!capable(CAP_SYS_ADMIN))
3372 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003373 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003374 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003375 if (rdev->mddev == NULL)
3376 rv = -EBUSY;
3377 else
3378 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003379 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003380 }
3381 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003382}
3383
3384static void rdev_free(struct kobject *ko)
3385{
NeilBrown3cb03002011-10-11 16:45:26 +11003386 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003387 kfree(rdev);
3388}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003389static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003390 .show = rdev_attr_show,
3391 .store = rdev_attr_store,
3392};
3393static struct kobj_type rdev_ktype = {
3394 .release = rdev_free,
3395 .sysfs_ops = &rdev_sysfs_ops,
3396 .default_attrs = rdev_default_attrs,
3397};
3398
NeilBrown3cb03002011-10-11 16:45:26 +11003399int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003400{
3401 rdev->desc_nr = -1;
3402 rdev->saved_raid_disk = -1;
3403 rdev->raid_disk = -1;
3404 rdev->flags = 0;
3405 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003406 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003407 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003408 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003409 rdev->sb_loaded = 0;
3410 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003411 atomic_set(&rdev->nr_pending, 0);
3412 atomic_set(&rdev->read_errors, 0);
3413 atomic_set(&rdev->corrected_errors, 0);
3414
3415 INIT_LIST_HEAD(&rdev->same_set);
3416 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003417
3418 /* Add space to store bad block list.
3419 * This reserves the space even on arrays where it cannot
3420 * be used - I wonder if that matters
3421 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003422 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003423}
3424EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425/*
3426 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3427 *
3428 * mark the device faulty if:
3429 *
3430 * - the device is nonexistent (zero size)
3431 * - the device has no valid superblock
3432 *
3433 * a faulty rdev _never_ has rdev->sb set.
3434 */
NeilBrown3cb03002011-10-11 16:45:26 +11003435static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436{
3437 char b[BDEVNAME_SIZE];
3438 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003439 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 sector_t size;
3441
NeilBrown9ffae0c2006-01-06 00:20:32 -08003442 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003443 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
NeilBrown2230dfe2011-07-28 11:31:46 +10003446 err = md_rdev_init(rdev);
3447 if (err)
3448 goto abort_free;
3449 err = alloc_disk_sb(rdev);
3450 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 goto abort_free;
3452
NeilBrownc5d79ad2008-02-06 01:39:54 -08003453 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 if (err)
3455 goto abort_free;
3456
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003457 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003458
Mike Snitzer77304d22010-11-08 14:39:12 +01003459 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003461 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 bdevname(rdev->bdev,b));
3463 err = -EINVAL;
3464 goto abort_free;
3465 }
3466
3467 if (super_format >= 0) {
3468 err = super_types[super_format].
3469 load_super(rdev, NULL, super_minor);
3470 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003471 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003472 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003473 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 goto abort_free;
3475 }
3476 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003477 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 bdevname(rdev->bdev,b));
3479 goto abort_free;
3480 }
3481 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003482
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 return rdev;
3484
3485abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003486 if (rdev->bdev)
3487 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003488 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 kfree(rdev);
3490 return ERR_PTR(err);
3491}
3492
3493/*
3494 * Check a full RAID array for plausibility
3495 */
3496
NeilBrownf72ffdd2014-09-30 14:23:59 +10003497static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
3499 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003500 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 char b[BDEVNAME_SIZE];
3502
3503 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003504 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 switch (super_types[mddev->major_version].
3506 load_super(rdev, freshest, mddev->minor_version)) {
3507 case 1:
3508 freshest = rdev;
3509 break;
3510 case 0:
3511 break;
3512 default:
NeilBrown9d487392016-11-02 14:16:49 +11003513 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003515 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 }
3517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 super_types[mddev->major_version].
3519 validate_super(mddev, freshest);
3520
3521 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003522 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003523 if (mddev->max_disks &&
3524 (rdev->desc_nr >= mddev->max_disks ||
3525 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003526 pr_warn("md: %s: %s: only %d devices permitted\n",
3527 mdname(mddev), bdevname(rdev->bdev, b),
3528 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003529 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003530 continue;
3531 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003532 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 if (super_types[mddev->major_version].
3534 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003535 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003537 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 continue;
3539 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 if (mddev->level == LEVEL_MULTIPATH) {
3542 rdev->desc_nr = i++;
3543 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003544 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003545 } else if (rdev->raid_disk >=
3546 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3547 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003548 rdev->raid_disk = -1;
3549 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 }
3551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552}
3553
NeilBrown72e02072009-12-14 12:49:55 +11003554/* Read a fixed-point number.
3555 * Numbers in sysfs attributes should be in "standard" units where
3556 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003557 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003558 * milliseconds or jiffies.
3559 * This function takes a decimal number with a possible fractional
3560 * component, and produces an integer which is the result of
3561 * multiplying that number by 10^'scale'.
3562 * all without any floating-point arithmetic.
3563 */
3564int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3565{
3566 unsigned long result = 0;
3567 long decimals = -1;
3568 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3569 if (*cp == '.')
3570 decimals = 0;
3571 else if (decimals < scale) {
3572 unsigned int value;
3573 value = *cp - '0';
3574 result = result * 10 + value;
3575 if (decimals >= 0)
3576 decimals++;
3577 }
3578 cp++;
3579 }
3580 if (*cp == '\n')
3581 cp++;
3582 if (*cp)
3583 return -EINVAL;
3584 if (decimals < 0)
3585 decimals = 0;
3586 while (decimals < scale) {
3587 result *= 10;
3588 decimals ++;
3589 }
3590 *res = result;
3591 return 0;
3592}
3593
NeilBrowneae17012005-11-08 21:39:23 -08003594static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003595safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003596{
3597 int msec = (mddev->safemode_delay*1000)/HZ;
3598 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3599}
3600static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003601safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003602{
NeilBrown16f17b32006-06-26 00:27:37 -07003603 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003604
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003605 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003606 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003607 return -EINVAL;
3608 }
3609
NeilBrown72e02072009-12-14 12:49:55 +11003610 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003611 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003612 if (msec == 0)
3613 mddev->safemode_delay = 0;
3614 else {
NeilBrown19052c02008-08-05 15:54:13 +10003615 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003616 unsigned long new_delay = (msec*HZ)/1000;
3617
3618 if (new_delay == 0)
3619 new_delay = 1;
3620 mddev->safemode_delay = new_delay;
3621 if (new_delay < old_delay || old_delay == 0)
3622 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003623 }
3624 return len;
3625}
3626static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003627__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003628
3629static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003630level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003631{
NeilBrown36d091f2014-12-15 12:56:58 +11003632 struct md_personality *p;
3633 int ret;
3634 spin_lock(&mddev->lock);
3635 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003636 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003637 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003638 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003639 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003640 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003641 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003642 else
NeilBrown36d091f2014-12-15 12:56:58 +11003643 ret = 0;
3644 spin_unlock(&mddev->lock);
3645 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003646}
3647
NeilBrownd9d166c2006-01-06 00:20:51 -08003648static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003649level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003650{
Dan Williamsf2859af2010-05-02 10:04:16 -07003651 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003652 ssize_t rv;
3653 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003654 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003655 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003656 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003657 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003658
NeilBrown67918752014-12-15 12:57:01 +11003659 if (slen == 0 || slen >= sizeof(clevel))
3660 return -EINVAL;
3661
3662 rv = mddev_lock(mddev);
3663 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003664 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003665
3666 if (mddev->pers == NULL) {
3667 strncpy(mddev->clevel, buf, slen);
3668 if (mddev->clevel[slen-1] == '\n')
3669 slen--;
3670 mddev->clevel[slen] = 0;
3671 mddev->level = LEVEL_NONE;
3672 rv = len;
3673 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003674 }
NeilBrown67918752014-12-15 12:57:01 +11003675 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003676 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003677 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003678
3679 /* request to change the personality. Need to ensure:
3680 * - array is not engaged in resync/recovery/reshape
3681 * - old personality can be suspended
3682 * - new personality will access other array.
3683 */
3684
NeilBrown67918752014-12-15 12:57:01 +11003685 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003686 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003687 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003688 mddev->reshape_position != MaxSector ||
3689 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003690 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003691
NeilBrown67918752014-12-15 12:57:01 +11003692 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003693 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003694 pr_warn("md: %s: %s does not support online personality change\n",
3695 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003696 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003697 }
3698
3699 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003700 strncpy(clevel, buf, slen);
3701 if (clevel[slen-1] == '\n')
3702 slen--;
3703 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003704 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003705 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003706
Dan Williamsf2859af2010-05-02 10:04:16 -07003707 if (request_module("md-%s", clevel) != 0)
3708 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003709 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003710 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003711 if (!pers || !try_module_get(pers->owner)) {
3712 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003713 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003714 rv = -EINVAL;
3715 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003716 }
3717 spin_unlock(&pers_lock);
3718
3719 if (pers == mddev->pers) {
3720 /* Nothing to do! */
3721 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003722 rv = len;
3723 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003724 }
3725 if (!pers->takeover) {
3726 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003727 pr_warn("md: %s: %s does not support personality takeover\n",
3728 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003729 rv = -EINVAL;
3730 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003731 }
3732
NeilBrowndafb20f2012-03-19 12:46:39 +11003733 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003734 rdev->new_raid_disk = rdev->raid_disk;
3735
NeilBrown245f46c2009-03-31 14:39:39 +11003736 /* ->takeover must set new_* and/or delta_disks
3737 * if it succeeds, and may set them when it fails.
3738 */
3739 priv = pers->takeover(mddev);
3740 if (IS_ERR(priv)) {
3741 mddev->new_level = mddev->level;
3742 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003743 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003744 mddev->raid_disks -= mddev->delta_disks;
3745 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003746 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003747 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003748 pr_warn("md: %s: %s would not accept array\n",
3749 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003750 rv = PTR_ERR(priv);
3751 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003752 }
3753
3754 /* Looks like we have a winner */
3755 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003756 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003757
3758 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003759 oldpers = mddev->pers;
3760 oldpriv = mddev->private;
3761 mddev->pers = pers;
3762 mddev->private = priv;
3763 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3764 mddev->level = mddev->new_level;
3765 mddev->layout = mddev->new_layout;
3766 mddev->chunk_sectors = mddev->new_chunk_sectors;
3767 mddev->delta_disks = 0;
3768 mddev->reshape_backwards = 0;
3769 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003770 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003771
NeilBrowndb721d32014-12-15 12:56:58 +11003772 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003773 mddev->external) {
3774 /* We are converting from a no-redundancy array
3775 * to a redundancy array and metadata is managed
3776 * externally so we need to be sure that writes
3777 * won't block due to a need to transition
3778 * clean->dirty
3779 * until external management is started.
3780 */
3781 mddev->in_sync = 0;
3782 mddev->safemode_delay = 0;
3783 mddev->safemode = 0;
3784 }
3785
NeilBrowndb721d32014-12-15 12:56:58 +11003786 oldpers->free(mddev, oldpriv);
3787
3788 if (oldpers->sync_request == NULL &&
3789 pers->sync_request != NULL) {
3790 /* need to add the md_redundancy_group */
3791 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11003792 pr_warn("md: cannot register extra attributes for %s\n",
3793 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11003794 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3795 }
3796 if (oldpers->sync_request != NULL &&
3797 pers->sync_request == NULL) {
3798 /* need to remove the md_redundancy_group */
3799 if (mddev->to_remove == NULL)
3800 mddev->to_remove = &md_redundancy_group;
3801 }
3802
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003803 module_put(oldpers->owner);
3804
NeilBrowndafb20f2012-03-19 12:46:39 +11003805 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003806 if (rdev->raid_disk < 0)
3807 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003808 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003809 rdev->new_raid_disk = -1;
3810 if (rdev->new_raid_disk == rdev->raid_disk)
3811 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003812 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003813 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003814 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003815 if (rdev->raid_disk < 0)
3816 continue;
3817 if (rdev->new_raid_disk == rdev->raid_disk)
3818 continue;
3819 rdev->raid_disk = rdev->new_raid_disk;
3820 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003821 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003822 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003823 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11003824 pr_warn("md: cannot register rd%d for %s after level change\n",
3825 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003826 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003827 }
3828
NeilBrowndb721d32014-12-15 12:56:58 +11003829 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003830 /* this is now an array without redundancy, so
3831 * it must always be in_sync
3832 */
3833 mddev->in_sync = 1;
3834 del_timer_sync(&mddev->safemode_timer);
3835 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003836 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003837 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08003838 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003839 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003840 if (!mddev->thread)
3841 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003842 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003843 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003844 rv = len;
3845out_unlock:
3846 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003847 return rv;
3848}
3849
3850static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003851__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003852
NeilBrownd4dbd022006-06-26 00:27:59 -07003853static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003854layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003855{
3856 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003857 if (mddev->reshape_position != MaxSector &&
3858 mddev->layout != mddev->new_layout)
3859 return sprintf(page, "%d (%d)\n",
3860 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003861 return sprintf(page, "%d\n", mddev->layout);
3862}
3863
3864static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003865layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003866{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003867 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003868 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003869
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003870 err = kstrtouint(buf, 10, &n);
3871 if (err < 0)
3872 return err;
NeilBrown67918752014-12-15 12:57:01 +11003873 err = mddev_lock(mddev);
3874 if (err)
3875 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003876
NeilBrownb3546032009-03-31 14:56:41 +11003877 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003878 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003879 err = -EBUSY;
3880 else if (mddev->ro)
3881 err = -EROFS;
3882 else {
3883 mddev->new_layout = n;
3884 err = mddev->pers->check_reshape(mddev);
3885 if (err)
3886 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003887 }
NeilBrownb3546032009-03-31 14:56:41 +11003888 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003889 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003890 if (mddev->reshape_position == MaxSector)
3891 mddev->layout = n;
3892 }
NeilBrown67918752014-12-15 12:57:01 +11003893 mddev_unlock(mddev);
3894 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003895}
3896static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003897__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003898
NeilBrowneae17012005-11-08 21:39:23 -08003899static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003900raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003901{
NeilBrownbb636542005-11-08 21:39:45 -08003902 if (mddev->raid_disks == 0)
3903 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003904 if (mddev->reshape_position != MaxSector &&
3905 mddev->delta_disks != 0)
3906 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3907 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003908 return sprintf(page, "%d\n", mddev->raid_disks);
3909}
3910
NeilBrownfd01b882011-10-11 16:47:53 +11003911static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003912
3913static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003914raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003915{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003916 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003917 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003918
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003919 err = kstrtouint(buf, 10, &n);
3920 if (err < 0)
3921 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003922
NeilBrown67918752014-12-15 12:57:01 +11003923 err = mddev_lock(mddev);
3924 if (err)
3925 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003926 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003927 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003928 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003929 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003930 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003931
NeilBrown67918752014-12-15 12:57:01 +11003932 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003933 rdev_for_each(rdev, mddev) {
3934 if (olddisks < n &&
3935 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003936 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003937 if (olddisks > n &&
3938 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003939 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003940 }
NeilBrown67918752014-12-15 12:57:01 +11003941 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003942 mddev->delta_disks = n - olddisks;
3943 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003944 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003945 } else
NeilBrownda943b992006-01-06 00:20:54 -08003946 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003947out_unlock:
3948 mddev_unlock(mddev);
3949 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003950}
3951static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003952__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003953
NeilBrown24dd4692005-11-08 21:39:26 -08003954static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003955chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003956{
NeilBrown08a02ec2007-05-09 02:35:38 -07003957 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003958 mddev->chunk_sectors != mddev->new_chunk_sectors)
3959 return sprintf(page, "%d (%d)\n",
3960 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003961 mddev->chunk_sectors << 9);
3962 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003963}
3964
3965static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003966chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003967{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003968 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003969 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003970
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003971 err = kstrtoul(buf, 10, &n);
3972 if (err < 0)
3973 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003974
NeilBrown67918752014-12-15 12:57:01 +11003975 err = mddev_lock(mddev);
3976 if (err)
3977 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003978 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003979 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003980 err = -EBUSY;
3981 else if (mddev->ro)
3982 err = -EROFS;
3983 else {
3984 mddev->new_chunk_sectors = n >> 9;
3985 err = mddev->pers->check_reshape(mddev);
3986 if (err)
3987 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003988 }
NeilBrownb3546032009-03-31 14:56:41 +11003989 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003990 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003991 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003992 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003993 }
NeilBrown67918752014-12-15 12:57:01 +11003994 mddev_unlock(mddev);
3995 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003996}
3997static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003998__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003999
NeilBrowna94213b2006-06-26 00:28:00 -07004000static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004001resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07004002{
NeilBrownd1a7c502009-03-31 15:24:32 +11004003 if (mddev->recovery_cp == MaxSector)
4004 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07004005 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4006}
4007
4008static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004009resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07004010{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004011 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11004012 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004013
4014 if (cmd_match(buf, "none"))
4015 n = MaxSector;
4016 else {
4017 err = kstrtoull(buf, 10, &n);
4018 if (err < 0)
4019 return err;
4020 if (n != (sector_t)n)
4021 return -EINVAL;
4022 }
NeilBrowna94213b2006-06-26 00:28:00 -07004023
NeilBrown67918752014-12-15 12:57:01 +11004024 err = mddev_lock(mddev);
4025 if (err)
4026 return err;
NeilBrownb0986362011-05-11 15:52:21 +10004027 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11004028 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07004029
NeilBrown67918752014-12-15 12:57:01 +11004030 if (!err) {
4031 mddev->recovery_cp = n;
4032 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08004033 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004034 }
4035 mddev_unlock(mddev);
4036 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07004037}
4038static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10004039__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4040 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07004041
NeilBrown9e653b62006-06-26 00:27:58 -07004042/*
4043 * The array state can be:
4044 *
4045 * clear
4046 * No devices, no size, no level
4047 * Equivalent to STOP_ARRAY ioctl
4048 * inactive
4049 * May have some settings, but array is not active
4050 * all IO results in error
4051 * When written, doesn't tear down array, but just stops it
4052 * suspended (not supported yet)
4053 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01004054 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07004055 * readonly
4056 * no resync can happen. no superblocks get written.
4057 * write requests fail
4058 * read-auto
4059 * like readonly, but behaves like 'clean' on a write request.
4060 *
4061 * clean - no pending writes, but otherwise active.
4062 * When written to inactive array, starts without resync
4063 * If a write request arrives then
4064 * if metadata is known, mark 'dirty' and switch to 'active'.
4065 * if not known, block and switch to write-pending
4066 * If written to an active array that has pending writes, then fails.
4067 * active
4068 * fully active: IO and resync can be happening.
4069 * When written to inactive array, starts with resync
4070 *
4071 * write-pending
4072 * clean, but writes are blocked waiting for 'active' to be written.
4073 *
4074 * active-idle
4075 * like active, but no writes have been seen for a while (100msec).
4076 *
4077 */
4078enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4079 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004080static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004081 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4082 "write-pending", "active-idle", NULL };
4083
4084static int match_word(const char *word, char **list)
4085{
4086 int n;
4087 for (n=0; list[n]; n++)
4088 if (cmd_match(word, list[n]))
4089 break;
4090 return n;
4091}
4092
4093static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004094array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004095{
4096 enum array_state st = inactive;
4097
4098 if (mddev->pers)
4099 switch(mddev->ro) {
4100 case 1:
4101 st = readonly;
4102 break;
4103 case 2:
4104 st = read_auto;
4105 break;
4106 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004107 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004108 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004109 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004110 else if (mddev->in_sync)
4111 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004112 else if (mddev->safemode)
4113 st = active_idle;
4114 else
4115 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004116 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004117 }
4118 else {
4119 if (list_empty(&mddev->disks) &&
4120 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004121 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004122 st = clear;
4123 else
4124 st = inactive;
4125 }
4126 return sprintf(page, "%s\n", array_states[st]);
4127}
4128
NeilBrownf72ffdd2014-09-30 14:23:59 +10004129static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4130static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4131static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004132static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004133
4134static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004135array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004136{
NeilBrown6497709b2017-03-15 14:05:14 +11004137 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004138 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004139
4140 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4141 /* don't take reconfig_mutex when toggling between
4142 * clean and active
4143 */
4144 spin_lock(&mddev->lock);
4145 if (st == active) {
4146 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004147 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004148 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004149 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004150 } else /* st == clean */ {
4151 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004152 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004153 err = -EBUSY;
4154 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004155 if (!err)
4156 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004157 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004158 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004159 }
4160 err = mddev_lock(mddev);
4161 if (err)
4162 return err;
4163 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004164 switch(st) {
4165 case bad_word:
4166 break;
4167 case clear:
4168 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004169 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004170 break;
4171 case inactive:
4172 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004173 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004174 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004175 else
NeilBrowne6910632008-02-06 01:39:51 -08004176 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004177 break;
4178 case suspended:
4179 break; /* not supported yet */
4180 case readonly:
4181 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004182 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004183 else {
4184 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004185 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004186 err = do_md_run(mddev);
4187 }
4188 break;
4189 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004190 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004191 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004192 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004193 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004194 err = restart_array(mddev);
4195 if (err == 0) {
4196 mddev->ro = 2;
4197 set_disk_ro(mddev->gendisk, 0);
4198 }
NeilBrown9e653b62006-06-26 00:27:58 -07004199 } else {
4200 mddev->ro = 2;
4201 err = do_md_run(mddev);
4202 }
4203 break;
4204 case clean:
4205 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004206 err = restart_array(mddev);
4207 if (err)
4208 break;
NeilBrown85572d72014-12-15 12:56:56 +11004209 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004210 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004211 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004212 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004213 } else
4214 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004215 break;
4216 case active:
4217 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004218 err = restart_array(mddev);
4219 if (err)
4220 break;
Shaohua Li29530792016-12-08 15:48:19 -08004221 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004222 wake_up(&mddev->sb_wait);
4223 err = 0;
4224 } else {
4225 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004226 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004227 err = do_md_run(mddev);
4228 }
4229 break;
4230 case write_pending:
4231 case active_idle:
4232 /* these cannot be set */
4233 break;
4234 }
NeilBrown67918752014-12-15 12:57:01 +11004235
4236 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004237 if (mddev->hold_active == UNTIL_IOCTL)
4238 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004239 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004240 }
NeilBrown67918752014-12-15 12:57:01 +11004241 mddev_unlock(mddev);
4242 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004243}
NeilBrown80ca3a42006-07-10 04:44:18 -07004244static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004245__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004246
NeilBrown6d7ff7382006-01-06 00:21:16 -08004247static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004248max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004249 return sprintf(page, "%d\n",
4250 atomic_read(&mddev->max_corr_read_errors));
4251}
4252
4253static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004254max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004255{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004256 unsigned int n;
4257 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004258
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004259 rv = kstrtouint(buf, 10, &n);
4260 if (rv < 0)
4261 return rv;
4262 atomic_set(&mddev->max_corr_read_errors, n);
4263 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004264}
4265
4266static struct md_sysfs_entry max_corr_read_errors =
4267__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4268 max_corrected_read_errors_store);
4269
4270static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004271null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004272{
4273 return -EINVAL;
4274}
4275
4276static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004277new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004278{
4279 /* buf must be %d:%d\n? giving major and minor numbers */
4280 /* The new device is added to the array.
4281 * If the array has a persistent superblock, we read the
4282 * superblock to initialise info and check validity.
4283 * Otherwise, only checking done is that in bind_rdev_to_array,
4284 * which mainly checks size.
4285 */
4286 char *e;
4287 int major = simple_strtoul(buf, &e, 10);
4288 int minor;
4289 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004290 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004291 int err;
4292
4293 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4294 return -EINVAL;
4295 minor = simple_strtoul(e+1, &e, 10);
4296 if (*e && *e != '\n')
4297 return -EINVAL;
4298 dev = MKDEV(major, minor);
4299 if (major != MAJOR(dev) ||
4300 minor != MINOR(dev))
4301 return -EOVERFLOW;
4302
NeilBrown67918752014-12-15 12:57:01 +11004303 flush_workqueue(md_misc_wq);
4304
4305 err = mddev_lock(mddev);
4306 if (err)
4307 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004308 if (mddev->persistent) {
4309 rdev = md_import_device(dev, mddev->major_version,
4310 mddev->minor_version);
4311 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004312 struct md_rdev *rdev0
4313 = list_entry(mddev->disks.next,
4314 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004315 err = super_types[mddev->major_version]
4316 .load_super(rdev, rdev0, mddev->minor_version);
4317 if (err < 0)
4318 goto out;
4319 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004320 } else if (mddev->external)
4321 rdev = md_import_device(dev, -2, -1);
4322 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004323 rdev = md_import_device(dev, -1, -1);
4324
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004325 if (IS_ERR(rdev)) {
4326 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004327 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004328 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004329 err = bind_rdev_to_array(rdev, mddev);
4330 out:
4331 if (err)
4332 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004333 mddev_unlock(mddev);
Alexey Obitotskiy5492c462017-07-28 15:49:25 +02004334 if (!err)
4335 md_new_event(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004336 return err ? err : len;
4337}
4338
4339static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004340__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004341
4342static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004343bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004344{
4345 char *end;
4346 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004347 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004348
NeilBrown67918752014-12-15 12:57:01 +11004349 err = mddev_lock(mddev);
4350 if (err)
4351 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004352 if (!mddev->bitmap)
4353 goto out;
4354 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4355 while (*buf) {
4356 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4357 if (buf == end) break;
4358 if (*end == '-') { /* range */
4359 buf = end + 1;
4360 end_chunk = simple_strtoul(buf, &end, 0);
4361 if (buf == end) break;
4362 }
4363 if (*end && !isspace(*end)) break;
4364 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004365 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004366 }
4367 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4368out:
NeilBrown67918752014-12-15 12:57:01 +11004369 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004370 return len;
4371}
4372
4373static struct md_sysfs_entry md_bitmap =
4374__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4375
4376static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004377size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004378{
Andre Noll58c0fed2009-03-31 14:33:13 +11004379 return sprintf(page, "%llu\n",
4380 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004381}
4382
NeilBrownfd01b882011-10-11 16:47:53 +11004383static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004384
4385static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004386size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004387{
4388 /* If array is inactive, we can reduce the component size, but
4389 * not increase it (except from 0).
4390 * If array is active, we can try an on-line resize
4391 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004392 sector_t sectors;
4393 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004394
Andre Noll58c0fed2009-03-31 14:33:13 +11004395 if (err < 0)
4396 return err;
NeilBrown67918752014-12-15 12:57:01 +11004397 err = mddev_lock(mddev);
4398 if (err)
4399 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004400 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004401 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004402 if (err == 0)
4403 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004404 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004405 if (mddev->dev_sectors == 0 ||
4406 mddev->dev_sectors > sectors)
4407 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004408 else
4409 err = -ENOSPC;
4410 }
NeilBrown67918752014-12-15 12:57:01 +11004411 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004412 return err ? err : len;
4413}
4414
4415static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004416__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004417
Masanari Iida83f0d772012-10-30 00:18:08 +09004418/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004419 * This is one of
4420 * 'none' for arrays with no metadata (good luck...)
4421 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004422 * or N.M for internally known formats
4423 */
4424static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004425metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004426{
4427 if (mddev->persistent)
4428 return sprintf(page, "%d.%d\n",
4429 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004430 else if (mddev->external)
4431 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004432 else
4433 return sprintf(page, "none\n");
4434}
4435
4436static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004437metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004438{
4439 int major, minor;
4440 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004441 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004442 /* Changing the details of 'external' metadata is
4443 * always permitted. Otherwise there must be
4444 * no devices attached to the array.
4445 */
NeilBrown67918752014-12-15 12:57:01 +11004446
4447 err = mddev_lock(mddev);
4448 if (err)
4449 return err;
4450 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004451 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4452 ;
4453 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004454 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004455
NeilBrown67918752014-12-15 12:57:01 +11004456 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004457 if (cmd_match(buf, "none")) {
4458 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004459 mddev->external = 0;
4460 mddev->major_version = 0;
4461 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004462 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004463 }
4464 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004465 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004466 if (namelen >= sizeof(mddev->metadata_type))
4467 namelen = sizeof(mddev->metadata_type)-1;
4468 strncpy(mddev->metadata_type, buf+9, namelen);
4469 mddev->metadata_type[namelen] = 0;
4470 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4471 mddev->metadata_type[--namelen] = 0;
4472 mddev->persistent = 0;
4473 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004474 mddev->major_version = 0;
4475 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004476 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004477 }
4478 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004479 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004480 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004481 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004482 buf = e+1;
4483 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004484 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004485 goto out_unlock;
4486 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004487 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004488 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004489 mddev->major_version = major;
4490 mddev->minor_version = minor;
4491 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004492 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004493 err = 0;
4494out_unlock:
4495 mddev_unlock(mddev);
4496 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004497}
4498
4499static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004500__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004501
NeilBrowna35b0d62006-01-06 00:20:49 -08004502static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004503action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004504{
NeilBrown7eec3142005-11-08 21:39:44 -08004505 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004506 unsigned long recovery = mddev->recovery;
4507 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004508 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004509 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4510 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4511 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004512 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004513 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4514 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004515 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004516 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004517 type = "check";
4518 else
4519 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004520 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004521 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004522 else if (mddev->reshape_position != MaxSector)
4523 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004524 }
4525 return sprintf(page, "%s\n", type);
4526}
4527
4528static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004529action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004530{
NeilBrown7eec3142005-11-08 21:39:44 -08004531 if (!mddev->pers || !mddev->pers->sync_request)
4532 return -EINVAL;
4533
NeilBrownb6a9ce62009-05-26 09:41:17 +10004534
4535 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004536 if (cmd_match(page, "frozen"))
4537 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4538 else
4539 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004540 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4541 mddev_lock(mddev) == 0) {
4542 flush_workqueue(md_misc_wq);
4543 if (mddev->sync_thread) {
4544 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004545 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004546 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004547 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004548 }
NeilBrown312045e2015-12-21 11:01:21 +11004549 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004550 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004551 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004552 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004553 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004554 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004555 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004556 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004557 int err;
4558 if (mddev->pers->start_reshape == NULL)
4559 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004560 err = mddev_lock(mddev);
4561 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004562 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4563 err = -EBUSY;
4564 else {
4565 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4566 err = mddev->pers->start_reshape(mddev);
4567 }
NeilBrown67918752014-12-15 12:57:01 +11004568 mddev_unlock(mddev);
4569 }
NeilBrown16484bf2006-03-27 01:18:13 -08004570 if (err)
4571 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004572 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004573 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004574 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004575 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004576 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004577 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004578 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004579 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4580 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004581 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004582 if (mddev->ro == 2) {
4583 /* A write to sync_action is enough to justify
4584 * canceling read-auto mode
4585 */
4586 mddev->ro = 0;
4587 md_wakeup_thread(mddev->sync_thread);
4588 }
NeilBrown03c902e2006-01-06 00:20:46 -08004589 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004590 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004591 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004592 return len;
4593}
4594
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004595static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004596__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004597
4598static ssize_t
4599last_sync_action_show(struct mddev *mddev, char *page)
4600{
4601 return sprintf(page, "%s\n", mddev->last_sync_action);
4602}
4603
4604static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4605
NeilBrown9d888832005-11-08 21:39:26 -08004606static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004607mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004608{
4609 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004610 (unsigned long long)
4611 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004612}
4613
NeilBrown80ca3a42006-07-10 04:44:18 -07004614static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004615
NeilBrown88202a02006-01-06 00:21:36 -08004616static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004617sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004618{
4619 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4620 mddev->sync_speed_min ? "local": "system");
4621}
4622
4623static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004624sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004625{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004626 unsigned int min;
4627 int rv;
4628
NeilBrown88202a02006-01-06 00:21:36 -08004629 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004630 min = 0;
4631 } else {
4632 rv = kstrtouint(buf, 10, &min);
4633 if (rv < 0)
4634 return rv;
4635 if (min == 0)
4636 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004637 }
NeilBrown88202a02006-01-06 00:21:36 -08004638 mddev->sync_speed_min = min;
4639 return len;
4640}
4641
4642static struct md_sysfs_entry md_sync_min =
4643__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4644
4645static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004646sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004647{
4648 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4649 mddev->sync_speed_max ? "local": "system");
4650}
4651
4652static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004653sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004654{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004655 unsigned int max;
4656 int rv;
4657
NeilBrown88202a02006-01-06 00:21:36 -08004658 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004659 max = 0;
4660 } else {
4661 rv = kstrtouint(buf, 10, &max);
4662 if (rv < 0)
4663 return rv;
4664 if (max == 0)
4665 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004666 }
NeilBrown88202a02006-01-06 00:21:36 -08004667 mddev->sync_speed_max = max;
4668 return len;
4669}
4670
4671static struct md_sysfs_entry md_sync_max =
4672__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4673
Iustin Popd7f3d292007-10-16 23:30:54 -07004674static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004675degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004676{
4677 return sprintf(page, "%d\n", mddev->degraded);
4678}
4679static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004680
4681static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004682sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004683{
4684 return sprintf(page, "%d\n", mddev->parallel_resync);
4685}
4686
4687static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004688sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004689{
4690 long n;
4691
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004692 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004693 return -EINVAL;
4694
4695 if (n != 0 && n != 1)
4696 return -EINVAL;
4697
4698 mddev->parallel_resync = n;
4699
4700 if (mddev->sync_thread)
4701 wake_up(&resync_wait);
4702
4703 return len;
4704}
4705
4706/* force parallel resync, even with shared block devices */
4707static struct md_sysfs_entry md_sync_force_parallel =
4708__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4709 sync_force_parallel_show, sync_force_parallel_store);
4710
4711static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004712sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004713{
4714 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004715 if (mddev->curr_resync == 0)
4716 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004717 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4718 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004719 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004720 db = resync - mddev->resync_mark_cnt;
4721 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004722}
4723
NeilBrown80ca3a42006-07-10 04:44:18 -07004724static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004725
4726static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004727sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004728{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004729 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004730
NeilBrownacb180b2009-04-14 16:28:34 +10004731 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4732 return sprintf(page, "none\n");
4733
NeilBrown72f36d52012-10-11 14:25:57 +11004734 if (mddev->curr_resync == 1 ||
4735 mddev->curr_resync == 2)
4736 return sprintf(page, "delayed\n");
4737
NeilBrownc804cde2012-05-21 09:28:33 +10004738 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4739 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004740 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004741 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004742 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004743
NeilBrownacb180b2009-04-14 16:28:34 +10004744 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004745 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004746}
4747
NeilBrown750f1992014-09-30 08:53:05 +10004748static struct md_sysfs_entry md_sync_completed =
4749 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004750
NeilBrowne464eaf2006-03-27 01:18:14 -08004751static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004752min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004753{
4754 return sprintf(page, "%llu\n",
4755 (unsigned long long)mddev->resync_min);
4756}
4757static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004758min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004759{
4760 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004761 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004762
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004763 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004764 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004765
4766 spin_lock(&mddev->lock);
4767 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004768 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004769 goto out_unlock;
4770
4771 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004772 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004773 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004774
NeilBrown50c37b12015-03-23 17:36:38 +11004775 /* Round down to multiple of 4K for safety */
4776 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004777 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004778
NeilBrown23da4222014-12-15 12:57:01 +11004779out_unlock:
4780 spin_unlock(&mddev->lock);
4781 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004782}
4783
4784static struct md_sysfs_entry md_min_sync =
4785__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4786
4787static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004788max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004789{
4790 if (mddev->resync_max == MaxSector)
4791 return sprintf(page, "max\n");
4792 else
4793 return sprintf(page, "%llu\n",
4794 (unsigned long long)mddev->resync_max);
4795}
4796static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004797max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004798{
NeilBrown23da4222014-12-15 12:57:01 +11004799 int err;
4800 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004801 if (strncmp(buf, "max", 3) == 0)
4802 mddev->resync_max = MaxSector;
4803 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004804 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004805 int chunk;
4806
4807 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004808 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004809 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004810 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004811 goto out_unlock;
4812
4813 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004814 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004815 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004816 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004817 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004818
4819 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004820 chunk = mddev->chunk_sectors;
4821 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004822 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004823
4824 err = -EINVAL;
4825 if (sector_div(temp, chunk))
4826 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004827 }
4828 mddev->resync_max = max;
4829 }
4830 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004831 err = 0;
4832out_unlock:
4833 spin_unlock(&mddev->lock);
4834 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004835}
4836
4837static struct md_sysfs_entry md_max_sync =
4838__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4839
4840static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004841suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004842{
4843 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4844}
4845
4846static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004847suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004848{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004849 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004850 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004851
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004852 err = kstrtoull(buf, 10, &new);
4853 if (err < 0)
4854 return err;
4855 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004856 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004857
NeilBrown67918752014-12-15 12:57:01 +11004858 err = mddev_lock(mddev);
4859 if (err)
4860 return err;
4861 err = -EINVAL;
4862 if (mddev->pers == NULL ||
4863 mddev->pers->quiesce == NULL)
4864 goto unlock;
4865 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004866 mddev->suspend_lo = new;
NeilBrownb3143b92017-10-17 13:46:43 +11004867 if (new >= old) {
NeilBrown23ddff32011-01-14 09:14:34 +11004868 /* Shrinking suspended region */
NeilBrownb3143b92017-10-17 13:46:43 +11004869 wake_up(&mddev->sb_wait);
NeilBrowne464eaf2006-03-27 01:18:14 -08004870 mddev->pers->quiesce(mddev, 2);
NeilBrownb3143b92017-10-17 13:46:43 +11004871 } else {
NeilBrown23ddff32011-01-14 09:14:34 +11004872 /* Expanding suspended region - need to wait */
NeilBrown9e1cc0a2017-10-17 13:46:43 +11004873 mddev_suspend(mddev);
4874 mddev_resume(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11004875 }
NeilBrown67918752014-12-15 12:57:01 +11004876 err = 0;
4877unlock:
4878 mddev_unlock(mddev);
4879 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004880}
4881static struct md_sysfs_entry md_suspend_lo =
4882__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4883
NeilBrowne464eaf2006-03-27 01:18:14 -08004884static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004885suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004886{
4887 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4888}
4889
4890static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004891suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004892{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004893 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004894 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004895
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004896 err = kstrtoull(buf, 10, &new);
4897 if (err < 0)
4898 return err;
4899 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004900 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004901
NeilBrown67918752014-12-15 12:57:01 +11004902 err = mddev_lock(mddev);
4903 if (err)
4904 return err;
4905 err = -EINVAL;
4906 if (mddev->pers == NULL ||
4907 mddev->pers->quiesce == NULL)
4908 goto unlock;
4909 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004910 mddev->suspend_hi = new;
NeilBrownb3143b92017-10-17 13:46:43 +11004911 if (new <= old) {
NeilBrown23ddff32011-01-14 09:14:34 +11004912 /* Shrinking suspended region */
NeilBrownb3143b92017-10-17 13:46:43 +11004913 wake_up(&mddev->sb_wait);
NeilBrown23ddff32011-01-14 09:14:34 +11004914 mddev->pers->quiesce(mddev, 2);
NeilBrownb3143b92017-10-17 13:46:43 +11004915 } else {
NeilBrown23ddff32011-01-14 09:14:34 +11004916 /* Expanding suspended region - need to wait */
NeilBrown9e1cc0a2017-10-17 13:46:43 +11004917 mddev_suspend(mddev);
4918 mddev_resume(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11004919 }
NeilBrown67918752014-12-15 12:57:01 +11004920 err = 0;
4921unlock:
4922 mddev_unlock(mddev);
4923 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004924}
4925static struct md_sysfs_entry md_suspend_hi =
4926__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4927
NeilBrown08a02ec2007-05-09 02:35:38 -07004928static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004929reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004930{
4931 if (mddev->reshape_position != MaxSector)
4932 return sprintf(page, "%llu\n",
4933 (unsigned long long)mddev->reshape_position);
4934 strcpy(page, "none\n");
4935 return 5;
4936}
4937
4938static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004939reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004940{
NeilBrownc6563a82012-05-21 09:27:00 +10004941 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004942 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004943 int err;
NeilBrown67918752014-12-15 12:57:01 +11004944
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004945 err = kstrtoull(buf, 10, &new);
4946 if (err < 0)
4947 return err;
4948 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004949 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004950 err = mddev_lock(mddev);
4951 if (err)
4952 return err;
4953 err = -EBUSY;
4954 if (mddev->pers)
4955 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004956 mddev->reshape_position = new;
4957 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004958 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004959 mddev->new_level = mddev->level;
4960 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004961 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004962 rdev_for_each(rdev, mddev)
4963 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004964 err = 0;
4965unlock:
4966 mddev_unlock(mddev);
4967 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004968}
4969
4970static struct md_sysfs_entry md_reshape_position =
4971__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4972 reshape_position_store);
4973
Dan Williamsb522adc2009-03-31 15:00:31 +11004974static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004975reshape_direction_show(struct mddev *mddev, char *page)
4976{
4977 return sprintf(page, "%s\n",
4978 mddev->reshape_backwards ? "backwards" : "forwards");
4979}
4980
4981static ssize_t
4982reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4983{
4984 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004985 int err;
4986
NeilBrown2c810cd2012-05-21 09:27:00 +10004987 if (cmd_match(buf, "forwards"))
4988 backwards = 0;
4989 else if (cmd_match(buf, "backwards"))
4990 backwards = 1;
4991 else
4992 return -EINVAL;
4993 if (mddev->reshape_backwards == backwards)
4994 return len;
4995
NeilBrown67918752014-12-15 12:57:01 +11004996 err = mddev_lock(mddev);
4997 if (err)
4998 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004999 /* check if we are allowed to change */
5000 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11005001 err = -EBUSY;
5002 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10005003 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11005004 err = -EINVAL;
5005 else
5006 mddev->reshape_backwards = backwards;
5007 mddev_unlock(mddev);
5008 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10005009}
5010
5011static struct md_sysfs_entry md_reshape_direction =
5012__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5013 reshape_direction_store);
5014
5015static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005016array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11005017{
5018 if (mddev->external_size)
5019 return sprintf(page, "%llu\n",
5020 (unsigned long long)mddev->array_sectors/2);
5021 else
5022 return sprintf(page, "default\n");
5023}
5024
5025static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005026array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11005027{
5028 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11005029 int err;
5030
5031 err = mddev_lock(mddev);
5032 if (err)
5033 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11005034
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005035 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08005036 if (mddev_is_clustered(mddev)) {
5037 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005038 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08005039 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005040
Dan Williamsb522adc2009-03-31 15:00:31 +11005041 if (strncmp(buf, "default", 7) == 0) {
5042 if (mddev->pers)
5043 sectors = mddev->pers->size(mddev, 0, 0);
5044 else
5045 sectors = mddev->array_sectors;
5046
5047 mddev->external_size = 0;
5048 } else {
5049 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11005050 err = -EINVAL;
5051 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5052 err = -E2BIG;
5053 else
5054 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11005055 }
5056
NeilBrown67918752014-12-15 12:57:01 +11005057 if (!err) {
5058 mddev->array_sectors = sectors;
5059 if (mddev->pers) {
5060 set_capacity(mddev->gendisk, mddev->array_sectors);
5061 revalidate_disk(mddev->gendisk);
5062 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11005063 }
NeilBrown67918752014-12-15 12:57:01 +11005064 mddev_unlock(mddev);
5065 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11005066}
5067
5068static struct md_sysfs_entry md_array_size =
5069__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5070 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005071
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005072static ssize_t
5073consistency_policy_show(struct mddev *mddev, char *page)
5074{
5075 int ret;
5076
5077 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5078 ret = sprintf(page, "journal\n");
5079 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5080 ret = sprintf(page, "ppl\n");
5081 } else if (mddev->bitmap) {
5082 ret = sprintf(page, "bitmap\n");
5083 } else if (mddev->pers) {
5084 if (mddev->pers->sync_request)
5085 ret = sprintf(page, "resync\n");
5086 else
5087 ret = sprintf(page, "none\n");
5088 } else {
5089 ret = sprintf(page, "unknown\n");
5090 }
5091
5092 return ret;
5093}
5094
5095static ssize_t
5096consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5097{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005098 int err = 0;
5099
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005100 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005101 if (mddev->pers->change_consistency_policy)
5102 err = mddev->pers->change_consistency_policy(mddev, buf);
5103 else
5104 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005105 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5106 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005107 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005108 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005109 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005110
5111 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005112}
5113
5114static struct md_sysfs_entry md_consistency_policy =
5115__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5116 consistency_policy_store);
5117
NeilBrowneae17012005-11-08 21:39:23 -08005118static struct attribute *md_default_attrs[] = {
5119 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005120 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005121 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005122 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005123 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005124 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005125 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005126 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005127 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005128 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005129 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005130 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005131 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005132 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005133 &md_consistency_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005134 NULL,
5135};
5136
5137static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005138 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005139 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005140 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005141 &md_sync_min.attr,
5142 &md_sync_max.attr,
5143 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005144 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005145 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005146 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005147 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005148 &md_suspend_lo.attr,
5149 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005150 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005151 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005152 NULL,
5153};
NeilBrown411036f2005-11-08 21:39:40 -08005154static struct attribute_group md_redundancy_group = {
5155 .name = NULL,
5156 .attrs = md_redundancy_attrs,
5157};
5158
NeilBrowneae17012005-11-08 21:39:23 -08005159static ssize_t
5160md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5161{
5162 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005163 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005164 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005165
5166 if (!entry->show)
5167 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005168 spin_lock(&all_mddevs_lock);
5169 if (list_empty(&mddev->all_mddevs)) {
5170 spin_unlock(&all_mddevs_lock);
5171 return -EBUSY;
5172 }
5173 mddev_get(mddev);
5174 spin_unlock(&all_mddevs_lock);
5175
NeilBrownb7b17c92014-12-15 12:56:59 +11005176 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005177 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005178 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005179}
5180
5181static ssize_t
5182md_attr_store(struct kobject *kobj, struct attribute *attr,
5183 const char *page, size_t length)
5184{
5185 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005186 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005187 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005188
5189 if (!entry->store)
5190 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005191 if (!capable(CAP_SYS_ADMIN))
5192 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005193 spin_lock(&all_mddevs_lock);
5194 if (list_empty(&mddev->all_mddevs)) {
5195 spin_unlock(&all_mddevs_lock);
5196 return -EBUSY;
5197 }
5198 mddev_get(mddev);
5199 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005200 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005201 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005202 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005203}
5204
5205static void md_free(struct kobject *ko)
5206{
NeilBrownfd01b882011-10-11 16:47:53 +11005207 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005208
5209 if (mddev->sysfs_state)
5210 sysfs_put(mddev->sysfs_state);
5211
NeilBrown6cd18e72015-04-27 14:12:22 +10005212 if (mddev->queue)
5213 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11005214 if (mddev->gendisk) {
5215 del_gendisk(mddev->gendisk);
5216 put_disk(mddev->gendisk);
5217 }
NeilBrown4ad23a972017-03-15 14:05:14 +11005218 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005219
NeilBrowneae17012005-11-08 21:39:23 -08005220 kfree(mddev);
5221}
5222
Emese Revfy52cf25d2010-01-19 02:58:23 +01005223static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005224 .show = md_attr_show,
5225 .store = md_attr_store,
5226};
5227static struct kobj_type md_ktype = {
5228 .release = md_free,
5229 .sysfs_ops = &md_sysfs_ops,
5230 .default_attrs = md_default_attrs,
5231};
5232
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233int mdp_major = 0;
5234
Dan Williams5fd3a172009-03-04 00:57:25 -07005235static void mddev_delayed_delete(struct work_struct *ws)
5236{
NeilBrownfd01b882011-10-11 16:47:53 +11005237 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005238
NeilBrown43a70502009-12-14 12:49:55 +11005239 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005240 kobject_del(&mddev->kobj);
5241 kobject_put(&mddev->kobj);
5242}
5243
NeilBrown4ad23a972017-03-15 14:05:14 +11005244static void no_op(struct percpu_ref *r) {}
5245
NeilBrowna415c0f2017-06-05 16:05:13 +10005246int mddev_init_writes_pending(struct mddev *mddev)
5247{
5248 if (mddev->writes_pending.percpu_count_ptr)
5249 return 0;
5250 if (percpu_ref_init(&mddev->writes_pending, no_op, 0, GFP_KERNEL) < 0)
5251 return -ENOMEM;
5252 /* We want to start with the refcount at zero */
5253 percpu_ref_put(&mddev->writes_pending);
5254 return 0;
5255}
5256EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5257
NeilBrownefeb53c2009-01-09 08:31:10 +11005258static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259{
NeilBrown039b7222017-04-12 16:26:13 +10005260 /*
5261 * If dev is zero, name is the name of a device to allocate with
5262 * an arbitrary minor number. It will be "md_???"
5263 * If dev is non-zero it must be a device number with a MAJOR of
5264 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5265 * the device is being created by opening a node in /dev.
5266 * If "name" is not NULL, the device is being created by
5267 * writing to /sys/module/md_mod/parameters/new_array.
5268 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005269 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005270 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005272 int partitioned;
5273 int shift;
5274 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005275 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276
5277 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005278 return -ENODEV;
5279
5280 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5281 shift = partitioned ? MdpMinorShift : 0;
5282 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283
Tejun Heoe804ac72010-10-15 15:36:08 +02005284 /* wait for any previous instance of this device to be
5285 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005286 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005287 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005288
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005289 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005290 error = -EEXIST;
5291 if (mddev->gendisk)
5292 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005293
NeilBrown039b7222017-04-12 16:26:13 +10005294 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005295 /* Need to ensure that 'name' is not a duplicate.
5296 */
NeilBrownfd01b882011-10-11 16:47:53 +11005297 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005298 spin_lock(&all_mddevs_lock);
5299
5300 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5301 if (mddev2->gendisk &&
5302 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5303 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005304 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005305 }
5306 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 }
NeilBrown039b7222017-04-12 16:26:13 +10005308 if (name && dev)
5309 /*
5310 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5311 */
5312 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005313
NeilBrown0909dc42009-07-01 12:27:21 +10005314 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005315 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005316 if (!mddev->queue)
5317 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005318 mddev->queue->queuedata = mddev;
5319
NeilBrown409c57f2009-03-31 14:39:39 +11005320 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005321 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005322
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 disk = alloc_disk(1 << shift);
5324 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005325 blk_cleanup_queue(mddev->queue);
5326 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005327 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005329 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005331 if (name)
5332 strcpy(disk->disk_name, name);
5333 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005335 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 disk->fops = &md_fops;
5338 disk->private_data = mddev;
5339 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005340 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005341 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005342 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005343 * remove it now.
5344 */
5345 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005347 /* As soon as we call add_disk(), another thread could get
5348 * through to md_open, so make sure it doesn't get too far
5349 */
5350 mutex_lock(&mddev->open_mutex);
5351 add_disk(disk);
5352
Tejun Heoed9e1982008-08-25 19:56:05 +09005353 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5354 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005355 if (error) {
5356 /* This isn't possible, but as kobject_init_and_add is marked
5357 * __must_check, we must do something with the result
5358 */
NeilBrown9d487392016-11-02 14:16:49 +11005359 pr_debug("md: cannot register %s/md - name in use\n",
5360 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005361 error = 0;
5362 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005363 if (mddev->kobj.sd &&
5364 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005365 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005366 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005367 abort:
5368 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005369 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005370 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005371 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005372 }
NeilBrownd3374822009-01-09 08:31:10 +11005373 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005374 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005375}
5376
5377static struct kobject *md_probe(dev_t dev, int *part, void *data)
5378{
NeilBrown78b63502017-04-12 16:26:13 +10005379 if (create_on_open)
5380 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 return NULL;
5382}
5383
NeilBrownefeb53c2009-01-09 08:31:10 +11005384static int add_named_array(const char *val, struct kernel_param *kp)
5385{
NeilBrown039b7222017-04-12 16:26:13 +10005386 /*
5387 * val must be "md_*" or "mdNNN".
5388 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005389 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005390 * For "mdNNN" we allocate an array with the minor number NNN
5391 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005392 */
5393 int len = strlen(val);
5394 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005395 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005396
5397 while (len && val[len-1] == '\n')
5398 len--;
5399 if (len >= DISK_NAME_LEN)
5400 return -E2BIG;
5401 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005402 if (strncmp(buf, "md_", 3) == 0)
5403 return md_alloc(0, buf);
5404 if (strncmp(buf, "md", 2) == 0 &&
5405 isdigit(buf[2]) &&
5406 kstrtoul(buf+2, 10, &devnum) == 0 &&
5407 devnum <= MINORMASK)
5408 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5409
5410 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005411}
5412
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413static void md_safemode_timeout(unsigned long data)
5414{
NeilBrownfd01b882011-10-11 16:47:53 +11005415 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
NeilBrown4ad23a972017-03-15 14:05:14 +11005417 mddev->safemode = 1;
5418 if (mddev->external)
5419 sysfs_notify_dirent_safe(mddev->sysfs_state);
5420
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 md_wakeup_thread(mddev->thread);
5422}
5423
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005424static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
NeilBrownfd01b882011-10-11 16:47:53 +11005426int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427{
NeilBrown2604b702006-01-06 00:20:36 -08005428 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005429 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005430 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431
NeilBrowna757e642005-04-16 15:26:42 -07005432 if (list_empty(&mddev->disks))
5433 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435
5436 if (mddev->pers)
5437 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005438 /* Cannot run until previous stop completes properly */
5439 if (mddev->sysfs_active)
5440 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005441
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 /*
5443 * Analyze all RAID superblock(s)
5444 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005445 if (!mddev->raid_disks) {
5446 if (!mddev->persistent)
5447 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005448 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
NeilBrownd9d166c2006-01-06 00:20:51 -08005451 if (mddev->level != LEVEL_NONE)
5452 request_module("md-level-%d", mddev->level);
5453 else if (mddev->clevel[0])
5454 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
5456 /*
5457 * Drop all container device buffers, from now on
5458 * the only valid external interface is through the md
5459 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005461 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005462 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 continue;
5464 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005465 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005466 if (mddev->ro != 1 &&
5467 (bdev_read_only(rdev->bdev) ||
5468 bdev_read_only(rdev->meta_bdev))) {
5469 mddev->ro = 1;
5470 if (mddev->gendisk)
5471 set_disk_ro(mddev->gendisk, 1);
5472 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005473
5474 /* perform some consistency tests on the device.
5475 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005476 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005477 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005478 if (rdev->meta_bdev) {
5479 /* Nothing to check */;
5480 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005481 if (mddev->dev_sectors &&
5482 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005483 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005484 pr_warn("md: %s: data overlaps metadata\n",
5485 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005486 return -EINVAL;
5487 }
5488 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005489 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005490 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005491 pr_warn("md: %s: metadata overlaps data\n",
5492 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005493 return -EINVAL;
5494 }
5495 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005496 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 }
5498
Ming Lei10273172017-02-14 23:29:00 +08005499 if (mddev->bio_set == NULL) {
NeilBrown011067b2017-06-18 14:38:57 +10005500 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
Ming Lei10273172017-02-14 23:29:00 +08005501 if (!mddev->bio_set)
5502 return -ENOMEM;
5503 }
NeilBrown5a850712017-06-21 09:12:21 +10005504 if (mddev->sync_set == NULL) {
Linus Torvalds026d15f2017-07-08 12:50:18 -07005505 mddev->sync_set = bioset_create(BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
NeilBrown5a850712017-06-21 09:12:21 +10005506 if (!mddev->sync_set)
5507 return -ENOMEM;
5508 }
NeilBrowna167f662010-10-26 18:31:13 +11005509
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005511 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005512 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005514 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005515 pr_warn("md: personality for level %d is not loaded!\n",
5516 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005517 else
NeilBrown9d487392016-11-02 14:16:49 +11005518 pr_warn("md: personality for level %s is not loaded!\n",
5519 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 return -EINVAL;
5521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005523 if (mddev->level != pers->level) {
5524 mddev->level = pers->level;
5525 mddev->new_level = pers->level;
5526 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005527 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528
NeilBrownf6705572006-03-27 01:18:11 -08005529 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005530 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005531 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005532 module_put(pers->owner);
5533 return -EINVAL;
5534 }
5535
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005536 if (pers->sync_request) {
5537 /* Warn if this is a potentially silly
5538 * configuration.
5539 */
5540 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005541 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005542 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005543
NeilBrowndafb20f2012-03-19 12:46:39 +11005544 rdev_for_each(rdev, mddev)
5545 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005546 if (rdev < rdev2 &&
5547 rdev->bdev->bd_contains ==
5548 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005549 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5550 mdname(mddev),
5551 bdevname(rdev->bdev,b),
5552 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005553 warned = 1;
5554 }
5555 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005556
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005557 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005558 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005559 }
5560
NeilBrown657390d2005-08-26 18:34:16 -07005561 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005562 /* may be over-ridden by personality */
5563 mddev->resync_max_sectors = mddev->dev_sectors;
5564
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005565 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
NeilBrown0f9552b52009-12-30 12:08:50 +11005567 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005568 mddev->ro = 2; /* read-only, but switch on first write */
5569
Song Liua85dd7b2017-01-23 17:12:57 -08005570 /*
5571 * NOTE: some pers->run(), for example r5l_recovery_log(), wakes
5572 * up mddev->thread. It is important to initialize critical
5573 * resources for mddev->thread BEFORE calling pers->run().
5574 */
NeilBrown36d091f2014-12-15 12:56:58 +11005575 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005576 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005577 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005578 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005579 WARN_ONCE(!mddev->external_size,
5580 "%s: default size too small, but 'external_size' not in effect?\n",
5581 __func__);
5582 pr_warn("md: invalid array_size %llu > default size %llu\n",
5583 (unsigned long long)mddev->array_sectors / 2,
5584 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005585 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005586 }
NeilBrown36d091f2014-12-15 12:56:58 +11005587 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005588 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005589 struct bitmap *bitmap;
5590
5591 bitmap = bitmap_create(mddev, -1);
5592 if (IS_ERR(bitmap)) {
5593 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005594 pr_warn("%s: failed to create bitmap (%d)\n",
5595 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005596 } else
5597 mddev->bitmap = bitmap;
5598
NeilBrownb15c2e52006-01-06 00:20:16 -08005599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005601 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005602 if (mddev->private)
5603 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005604 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005605 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005606 bitmap_destroy(mddev);
5607 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 }
NeilBrown5c675f82014-12-15 12:56:56 +11005609 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005610 bool nonrot = true;
5611
5612 rdev_for_each(rdev, mddev) {
5613 if (rdev->raid_disk >= 0 &&
5614 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5615 nonrot = false;
5616 break;
5617 }
5618 }
5619 if (mddev->degraded)
5620 nonrot = false;
5621 if (nonrot)
5622 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5623 else
5624 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005625 mddev->queue->backing_dev_info->congested_data = mddev;
5626 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005627 }
NeilBrown36d091f2014-12-15 12:56:58 +11005628 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005629 if (mddev->kobj.sd &&
5630 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005631 pr_warn("md: cannot register extra attributes for %s\n",
5632 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005633 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005634 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005635 mddev->ro = 0;
5636
Robert Becker1e509152009-12-14 12:49:58 +11005637 atomic_set(&mddev->max_corr_read_errors,
5638 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005640 if (mddev_is_clustered(mddev))
5641 mddev->safemode_delay = 0;
5642 else
5643 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005645 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005646 spin_lock(&mddev->lock);
5647 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005648 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005649 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005650 if (rdev->raid_disk >= 0)
5651 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005652 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005653
NeilBrowna4a3d262015-07-17 11:57:30 +10005654 if (mddev->degraded && !mddev->ro)
5655 /* This ensures that recovering status is reported immediately
5656 * via sysfs - until a lack of spares is confirmed.
5657 */
5658 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005660
Shaohua Li29530792016-12-08 15:48:19 -08005661 if (mddev->sb_flags)
NeilBrown850b2b42006-10-03 01:15:46 -07005662 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663
NeilBrownd7603b72006-01-06 00:20:30 -08005664 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005665 sysfs_notify_dirent_safe(mddev->sysfs_state);
5666 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005667 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 return 0;
5669}
NeilBrown390ee602010-06-01 19:37:27 +10005670EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
NeilBrownfd01b882011-10-11 16:47:53 +11005672static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005673{
5674 int err;
5675
5676 err = md_run(mddev);
5677 if (err)
5678 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005679 err = bitmap_load(mddev);
5680 if (err) {
5681 bitmap_destroy(mddev);
5682 goto out;
5683 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005684
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005685 if (mddev_is_clustered(mddev))
5686 md_allow_write(mddev);
5687
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005688 md_wakeup_thread(mddev->thread);
5689 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5690
NeilBrownfe60b012010-03-29 11:10:42 +11005691 set_capacity(mddev->gendisk, mddev->array_sectors);
5692 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005693 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005694 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5695out:
5696 return err;
5697}
5698
NeilBrownfd01b882011-10-11 16:47:53 +11005699static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700{
5701 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10005702 struct md_rdev *rdev;
5703 bool has_journal = false;
5704 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705
Andre Noll80fab1d2008-07-11 22:02:21 +10005706 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005708 return -ENXIO;
5709 if (!mddev->pers)
5710 return -EINVAL;
5711 if (!mddev->ro)
5712 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005713
NeilBrown97b20ef2017-04-13 08:53:48 +10005714 rcu_read_lock();
5715 rdev_for_each_rcu(rdev, mddev) {
5716 if (test_bit(Journal, &rdev->flags) &&
5717 !test_bit(Faulty, &rdev->flags))
5718 has_journal = true;
5719 if (bdev_read_only(rdev->bdev))
5720 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07005721 }
NeilBrown97b20ef2017-04-13 08:53:48 +10005722 rcu_read_unlock();
5723 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
5724 /* Don't restart rw with journal missing/faulty */
5725 return -EINVAL;
5726 if (has_readonly)
5727 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07005728
Andre Noll80fab1d2008-07-11 22:02:21 +10005729 mddev->safemode = 0;
5730 mddev->ro = 0;
5731 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11005732 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10005733 /* Kick recovery or resync if necessary */
5734 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5735 md_wakeup_thread(mddev->thread);
5736 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005737 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739}
5740
NeilBrownfd01b882011-10-11 16:47:53 +11005741static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005742{
5743 mddev->array_sectors = 0;
5744 mddev->external_size = 0;
5745 mddev->dev_sectors = 0;
5746 mddev->raid_disks = 0;
5747 mddev->recovery_cp = 0;
5748 mddev->resync_min = 0;
5749 mddev->resync_max = MaxSector;
5750 mddev->reshape_position = MaxSector;
5751 mddev->external = 0;
5752 mddev->persistent = 0;
5753 mddev->level = LEVEL_NONE;
5754 mddev->clevel[0] = 0;
5755 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08005756 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005757 mddev->ro = 0;
5758 mddev->metadata_type[0] = 0;
5759 mddev->chunk_sectors = 0;
5760 mddev->ctime = mddev->utime = 0;
5761 mddev->layout = 0;
5762 mddev->max_disks = 0;
5763 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005764 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005765 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005766 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005767 mddev->new_level = LEVEL_NONE;
5768 mddev->new_layout = 0;
5769 mddev->new_chunk_sectors = 0;
5770 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005771 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005772 mddev->suspend_lo = mddev->suspend_hi = 0;
5773 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5774 mddev->recovery = 0;
5775 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005776 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005777 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005778 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005779 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005780 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005781 mddev->bitmap_info.offset = 0;
5782 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005783 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005784 mddev->bitmap_info.chunksize = 0;
5785 mddev->bitmap_info.daemon_sleep = 0;
5786 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005787 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005788}
5789
NeilBrownfd01b882011-10-11 16:47:53 +11005790static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005791{
NeilBrown6b6204e2013-05-09 09:48:30 +10005792 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005793 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005794 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005795 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005796 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005797 }
5798
5799 del_timer_sync(&mddev->safemode_timer);
5800
Shaohua Li034e33f2016-11-21 10:29:19 -08005801 if (mddev->pers && mddev->pers->quiesce) {
5802 mddev->pers->quiesce(mddev, 1);
5803 mddev->pers->quiesce(mddev, 0);
5804 }
NeilBrowna047e122010-03-29 12:07:53 +11005805 bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005806
NeilBrownb6d428c2013-04-24 11:42:42 +10005807 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005808 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08005809 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11005810 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005811 if (!mddev_is_clustered(mddev))
5812 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005813 md_update_sb(mddev, 1);
5814 }
5815}
NeilBrowndefad612011-01-14 09:14:33 +11005816
NeilBrownfd01b882011-10-11 16:47:53 +11005817void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005818{
NeilBrown29f097c2013-11-14 17:54:51 +11005819 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005820 __md_stop_writes(mddev);
5821 mddev_unlock(mddev);
5822}
NeilBrown390ee602010-06-01 19:37:27 +10005823EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005824
NeilBrown5aa61f42014-12-15 12:56:57 +11005825static void mddev_detach(struct mddev *mddev)
5826{
Guoqing Jiang48df4982017-03-14 09:40:20 +08005827 bitmap_wait_behind_writes(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005828 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005829 mddev->pers->quiesce(mddev, 1);
5830 mddev->pers->quiesce(mddev, 0);
5831 }
5832 md_unregister_thread(&mddev->thread);
5833 if (mddev->queue)
5834 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5835}
5836
NeilBrown5eff3c42012-11-19 10:47:48 +11005837static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005838{
NeilBrown36d091f2014-12-15 12:56:58 +11005839 struct md_personality *pers = mddev->pers;
Guoqing Jiang48df4982017-03-14 09:40:20 +08005840 bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11005841 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005842 /* Ensure ->event_work is done */
5843 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005844 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005845 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005846 spin_unlock(&mddev->lock);
5847 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005848 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005849 if (pers->sync_request && mddev->to_remove == NULL)
5850 mddev->to_remove = &md_redundancy_group;
5851 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005852 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005853}
NeilBrown5eff3c42012-11-19 10:47:48 +11005854
5855void md_stop(struct mddev *mddev)
5856{
5857 /* stop the array and free an attached data structures.
5858 * This is called from dm-raid
5859 */
5860 __md_stop(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005861 if (mddev->bio_set)
5862 bioset_free(mddev->bio_set);
5863}
5864
NeilBrown390ee602010-06-01 19:37:27 +10005865EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005866
NeilBrowna05b7ea2012-07-19 15:59:18 +10005867static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005868{
5869 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005870 int did_freeze = 0;
5871
5872 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5873 did_freeze = 1;
5874 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5875 md_wakeup_thread(mddev->thread);
5876 }
NeilBrownf851b602014-12-11 10:02:10 +11005877 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005878 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005879 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005880 /* Thread might be blocked waiting for metadata update
5881 * which will now never happen */
5882 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005883
Shaohua Li29530792016-12-08 15:48:19 -08005884 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10005885 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005886 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005887 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5888 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005889 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005890 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005891 mddev_lock_nointr(mddev);
5892
NeilBrowna4bd82d2010-03-29 13:23:10 +11005893 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005894 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005895 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005896 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005897 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005898 if (did_freeze) {
5899 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005900 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005901 md_wakeup_thread(mddev->thread);
5902 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005903 err = -EBUSY;
5904 goto out;
5905 }
5906 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005907 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005908
5909 err = -ENXIO;
5910 if (mddev->ro==1)
5911 goto out;
5912 mddev->ro = 1;
5913 set_disk_ro(mddev->gendisk, 1);
5914 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005915 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5916 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005917 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005918 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005919 }
5920out:
5921 mutex_unlock(&mddev->open_mutex);
5922 return err;
5923}
5924
NeilBrown9e653b62006-06-26 00:27:58 -07005925/* mode:
5926 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005927 * 2 - stop but do not disassemble array
5928 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005929static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005930 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005933 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005934 int did_freeze = 0;
5935
5936 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5937 did_freeze = 1;
5938 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5939 md_wakeup_thread(mddev->thread);
5940 }
NeilBrownf851b602014-12-11 10:02:10 +11005941 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005942 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005943 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005944 /* Thread might be blocked waiting for metadata update
5945 * which will now never happen */
5946 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005947
NeilBrown30b8feb2013-11-14 15:16:17 +11005948 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005949 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5950 !test_bit(MD_RECOVERY_RUNNING,
5951 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005952 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953
NeilBrownc8c00a62009-08-10 12:50:52 +10005954 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005955 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005956 mddev->sysfs_active ||
5957 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005958 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005959 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005960 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005961 if (did_freeze) {
5962 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005963 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005964 md_wakeup_thread(mddev->thread);
5965 }
NeilBrown260fa032013-08-27 16:44:13 +10005966 return -EBUSY;
5967 }
NeilBrown6e17b022010-08-07 21:41:19 +10005968 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005969 if (mddev->ro)
5970 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005971
NeilBrowndefad612011-01-14 09:14:33 +11005972 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005973 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01005974 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005975
NeilBrowna4bd82d2010-03-29 13:23:10 +11005976 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005977 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005978
NeilBrowndafb20f2012-03-19 12:46:39 +11005979 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005980 if (rdev->raid_disk >= 0)
5981 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005982
NeilBrowna4bd82d2010-03-29 13:23:10 +11005983 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005984 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005985 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005986 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005987
NeilBrowna4bd82d2010-03-29 13:23:10 +11005988 if (mddev->ro)
5989 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005990 } else
5991 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 /*
5993 * Free resources if final stop
5994 */
NeilBrown9e653b62006-06-26 00:27:58 -07005995 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005996 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997
NeilBrownc3d97142009-12-14 12:49:52 +11005998 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005999 struct file *f = mddev->bitmap_info.file;
6000 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11006001 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11006002 spin_unlock(&mddev->lock);
6003 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08006004 }
NeilBrownc3d97142009-12-14 12:49:52 +11006005 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08006006
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 export_array(mddev);
6008
NeilBrown6177b472010-03-29 11:37:13 +11006009 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11006010 if (mddev->hold_active == UNTIL_STOP)
6011 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006012 }
NeilBrownd7603b72006-01-06 00:20:30 -08006013 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006014 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10006015 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016}
6017
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006018#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11006019static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020{
NeilBrown3cb03002011-10-11 16:45:26 +11006021 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 int err;
6023
NeilBrowna757e642005-04-16 15:26:42 -07006024 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026
NeilBrown9d487392016-11-02 14:16:49 +11006027 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028
NeilBrowndafb20f2012-03-19 12:46:39 +11006029 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11006031 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 }
NeilBrown9d487392016-11-02 14:16:49 +11006033 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
NeilBrownd710e132008-10-13 11:55:12 +11006035 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006037 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10006038 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 }
6040}
6041
6042/*
6043 * lets try to run arrays based on all disks that have arrived
6044 * until now. (those are in pending_raid_disks)
6045 *
6046 * the method: pick the first pending disk, collect all disks with
6047 * the same UUID, remove all from the pending list and put them into
6048 * the 'same_array' list. Then order this list based on superblock
6049 * update time (freshest comes first), kick out 'old' disks and
6050 * compare superblocks. If everything's fine then run it.
6051 *
6052 * If "unit" is allocated, then bump its reference count
6053 */
6054static void autorun_devices(int part)
6055{
NeilBrown3cb03002011-10-11 16:45:26 +11006056 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006057 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 char b[BDEVNAME_SIZE];
6059
NeilBrown9d487392016-11-02 14:16:49 +11006060 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07006062 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08006064 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11006066 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
NeilBrown9d487392016-11-02 14:16:49 +11006068 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006070 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006072 pr_debug("md: adding %s ...\n",
6073 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 list_move(&rdev->same_set, &candidates);
6075 }
6076 /*
6077 * now we have a set of devices, with all of them having
6078 * mostly sane superblocks. It's time to allocate the
6079 * mddev.
6080 */
NeilBrowne8703fe2006-10-03 01:15:59 -07006081 if (part) {
6082 dev = MKDEV(mdp_major,
6083 rdev0->preferred_minor << MdpMinorShift);
6084 unit = MINOR(dev) >> MdpMinorShift;
6085 } else {
6086 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6087 unit = MINOR(dev);
6088 }
6089 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006090 pr_warn("md: unit number in %s is bad: %d\n",
6091 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 break;
6093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094
6095 md_probe(dev, NULL, NULL);
6096 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006097 if (!mddev || !mddev->gendisk) {
6098 if (mddev)
6099 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 break;
6101 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006102 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006103 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 else if (mddev->raid_disks || mddev->major_version
6105 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006106 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 mdname(mddev), bdevname(rdev0->bdev,b));
6108 mddev_unlock(mddev);
6109 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006110 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006111 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006112 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 list_del_init(&rdev->same_set);
6114 if (bind_rdev_to_array(rdev, mddev))
6115 export_rdev(rdev);
6116 }
6117 autorun_array(mddev);
6118 mddev_unlock(mddev);
6119 }
6120 /* on success, candidates will be empty, on error
6121 * it won't...
6122 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006123 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006124 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 mddev_put(mddev);
6128 }
NeilBrown9d487392016-11-02 14:16:49 +11006129 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006131#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132
NeilBrownf72ffdd2014-09-30 14:23:59 +10006133static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134{
6135 mdu_version_t ver;
6136
6137 ver.major = MD_MAJOR_VERSION;
6138 ver.minor = MD_MINOR_VERSION;
6139 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6140
6141 if (copy_to_user(arg, &ver, sizeof(ver)))
6142 return -EFAULT;
6143
6144 return 0;
6145}
6146
NeilBrownf72ffdd2014-09-30 14:23:59 +10006147static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148{
6149 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006150 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006151 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152
NeilBrown1ca69c42012-10-11 13:37:33 +11006153 nr = working = insync = failed = spare = 0;
6154 rcu_read_lock();
6155 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006157 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 failed++;
6159 else {
6160 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006161 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006162 insync++;
Song Liub347af82016-08-11 17:14:45 -07006163 else if (test_bit(Journal, &rdev->flags))
6164 /* TODO: add journal count to md_u.h */
6165 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 else
6167 spare++;
6168 }
6169 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006170 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171
6172 info.major_version = mddev->major_version;
6173 info.minor_version = mddev->minor_version;
6174 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006175 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006177 info.size = mddev->dev_sectors / 2;
6178 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006179 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 info.nr_disks = nr;
6181 info.raid_disks = mddev->raid_disks;
6182 info.md_minor = mddev->md_minor;
6183 info.not_persistent= !mddev->persistent;
6184
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006185 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 info.state = 0;
6187 if (mddev->in_sync)
6188 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006189 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006190 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006191 if (mddev_is_clustered(mddev))
6192 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006193 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 info.working_disks = working;
6195 info.failed_disks = failed;
6196 info.spare_disks = spare;
6197
6198 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006199 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200
6201 if (copy_to_user(arg, &info, sizeof(info)))
6202 return -EFAULT;
6203
6204 return 0;
6205}
6206
NeilBrownf72ffdd2014-09-30 14:23:59 +10006207static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006208{
6209 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006210 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006211 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006212
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006213 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006214 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006215 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006216
NeilBrown32a76272005-06-21 17:17:14 -07006217 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006218 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006219 /* bitmap enabled */
6220 if (mddev->bitmap_info.file) {
6221 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6222 sizeof(file->pathname));
6223 if (IS_ERR(ptr))
6224 err = PTR_ERR(ptr);
6225 else
6226 memmove(file->pathname, ptr,
6227 sizeof(file->pathname)-(ptr-file->pathname));
6228 }
NeilBrown4af1a042014-12-15 12:57:00 +11006229 spin_unlock(&mddev->lock);
6230
6231 if (err == 0 &&
6232 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006233 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006234
NeilBrown32a76272005-06-21 17:17:14 -07006235 kfree(file);
6236 return err;
6237}
6238
NeilBrownf72ffdd2014-09-30 14:23:59 +10006239static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240{
6241 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006242 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
6244 if (copy_from_user(&info, arg, sizeof(info)))
6245 return -EFAULT;
6246
NeilBrown1ca69c42012-10-11 13:37:33 +11006247 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006248 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 if (rdev) {
6250 info.major = MAJOR(rdev->bdev->bd_dev);
6251 info.minor = MINOR(rdev->bdev->bd_dev);
6252 info.raid_disk = rdev->raid_disk;
6253 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006254 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006256 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 info.state |= (1<<MD_DISK_ACTIVE);
6258 info.state |= (1<<MD_DISK_SYNC);
6259 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006260 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006261 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006262 if (test_bit(WriteMostly, &rdev->flags))
6263 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006264 if (test_bit(FailFast, &rdev->flags))
6265 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 } else {
6267 info.major = info.minor = 0;
6268 info.raid_disk = -1;
6269 info.state = (1<<MD_DISK_REMOVED);
6270 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006271 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272
6273 if (copy_to_user(arg, &info, sizeof(info)))
6274 return -EFAULT;
6275
6276 return 0;
6277}
6278
NeilBrownf72ffdd2014-09-30 14:23:59 +10006279static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280{
6281 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006282 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 dev_t dev = MKDEV(info->major,info->minor);
6284
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006285 if (mddev_is_clustered(mddev) &&
6286 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006287 pr_warn("%s: Cannot add to clustered mddev.\n",
6288 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006289 return -EINVAL;
6290 }
6291
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6293 return -EOVERFLOW;
6294
6295 if (!mddev->raid_disks) {
6296 int err;
6297 /* expecting a device which has a superblock */
6298 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6299 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006300 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 PTR_ERR(rdev));
6302 return PTR_ERR(rdev);
6303 }
6304 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006305 struct md_rdev *rdev0
6306 = list_entry(mddev->disks.next,
6307 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006308 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 .load_super(rdev, rdev0, mddev->minor_version);
6310 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006311 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006312 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 bdevname(rdev0->bdev,b2));
6314 export_rdev(rdev);
6315 return -EINVAL;
6316 }
6317 }
6318 err = bind_rdev_to_array(rdev, mddev);
6319 if (err)
6320 export_rdev(rdev);
6321 return err;
6322 }
6323
6324 /*
6325 * add_new_disk can be used once the array is assembled
6326 * to add "hot spares". They must already have a superblock
6327 * written
6328 */
6329 if (mddev->pers) {
6330 int err;
6331 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006332 pr_warn("%s: personality does not support diskops!\n",
6333 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 return -EINVAL;
6335 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006336 if (mddev->persistent)
6337 rdev = md_import_device(dev, mddev->major_version,
6338 mddev->minor_version);
6339 else
6340 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006342 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 PTR_ERR(rdev));
6344 return PTR_ERR(rdev);
6345 }
NeilBrown1a855a02010-12-09 16:36:28 +11006346 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006347 if (!mddev->persistent) {
6348 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006349 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006350 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006351 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006352 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006353 } else
NeilBrown41158c72005-06-21 17:17:25 -07006354 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006355 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006356 } else
6357 super_types[mddev->major_version].
6358 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006359 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006360 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006361 /* This was a hot-add request, but events doesn't
6362 * match, so reject it.
6363 */
6364 export_rdev(rdev);
6365 return -EINVAL;
6366 }
6367
NeilBrownb2d444d2005-11-08 21:39:31 -08006368 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006369 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6370 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006371 else
6372 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006373 if (info->state & (1<<MD_DISK_FAILFAST))
6374 set_bit(FailFast, &rdev->flags);
6375 else
6376 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006377
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006378 if (info->state & (1<<MD_DISK_JOURNAL)) {
6379 struct md_rdev *rdev2;
6380 bool has_journal = false;
6381
6382 /* make sure no existing journal disk */
6383 rdev_for_each(rdev2, mddev) {
6384 if (test_bit(Journal, &rdev2->flags)) {
6385 has_journal = true;
6386 break;
6387 }
6388 }
NeilBrown230b55f2017-10-17 14:24:09 +11006389 if (has_journal || mddev->bitmap) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006390 export_rdev(rdev);
6391 return -EBUSY;
6392 }
Song Liubac624f2015-08-13 14:31:55 -07006393 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006394 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006395 /*
6396 * check whether the device shows up in other nodes
6397 */
6398 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006399 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006400 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006401 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006402 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006403 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006404 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006405 export_rdev(rdev);
6406 return err;
6407 }
6408 }
6409 }
6410
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 rdev->raid_disk = -1;
6412 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006413
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 if (err)
6415 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006416
6417 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006418 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6419 if (!err) {
6420 err = md_cluster_ops->new_disk_ack(mddev,
6421 err == 0);
6422 if (err)
6423 md_kick_rdev_from_array(rdev);
6424 }
6425 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006426 if (err)
6427 md_cluster_ops->add_new_disk_cancel(mddev);
6428 else
6429 err = add_bound_rdev(rdev);
6430 }
6431
6432 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006433 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006434
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 return err;
6436 }
6437
6438 /* otherwise, add_new_disk is only allowed
6439 * for major_version==0 superblocks
6440 */
6441 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006442 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 return -EINVAL;
6444 }
6445
6446 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6447 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006448 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006450 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 PTR_ERR(rdev));
6452 return PTR_ERR(rdev);
6453 }
6454 rdev->desc_nr = info->number;
6455 if (info->raid_disk < mddev->raid_disks)
6456 rdev->raid_disk = info->raid_disk;
6457 else
6458 rdev->raid_disk = -1;
6459
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006461 if (info->state & (1<<MD_DISK_SYNC))
6462 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006464 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6465 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006466 if (info->state & (1<<MD_DISK_FAILFAST))
6467 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006468
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006470 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006471 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6472 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006473 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006474 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475
NeilBrown2bf071b2006-01-06 00:20:55 -08006476 err = bind_rdev_to_array(rdev, mddev);
6477 if (err) {
6478 export_rdev(rdev);
6479 return err;
6480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 }
6482
6483 return 0;
6484}
6485
NeilBrownf72ffdd2014-09-30 14:23:59 +10006486static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487{
6488 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006489 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 rdev = find_rdev(mddev, dev);
6492 if (!rdev)
6493 return -ENXIO;
6494
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006495 if (rdev->raid_disk < 0)
6496 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006497
NeilBrown3ea8929d2013-04-24 11:42:41 +10006498 clear_bit(Blocked, &rdev->flags);
6499 remove_and_add_spares(mddev, rdev);
6500
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501 if (rdev->raid_disk >= 0)
6502 goto busy;
6503
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006504kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006505 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006506 md_cluster_ops->remove_disk(mddev, rdev);
6507
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006508 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006509 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006510 if (mddev->thread)
6511 md_wakeup_thread(mddev->thread);
6512 else
6513 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006514 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515
6516 return 0;
6517busy:
NeilBrown9d487392016-11-02 14:16:49 +11006518 pr_debug("md: cannot remove active disk %s from %s ...\n",
6519 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520 return -EBUSY;
6521}
6522
NeilBrownf72ffdd2014-09-30 14:23:59 +10006523static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524{
6525 char b[BDEVNAME_SIZE];
6526 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006527 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528
6529 if (!mddev->pers)
6530 return -ENODEV;
6531
6532 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006533 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534 mdname(mddev));
6535 return -EINVAL;
6536 }
6537 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006538 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 mdname(mddev));
6540 return -EINVAL;
6541 }
6542
NeilBrownd710e132008-10-13 11:55:12 +11006543 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006545 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546 PTR_ERR(rdev));
6547 return -EINVAL;
6548 }
6549
6550 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006551 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006553 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554
NeilBrown8190e752009-06-18 08:48:58 +10006555 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556
NeilBrownb2d444d2005-11-08 21:39:31 -08006557 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006558 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 bdevname(rdev->bdev,b), mdname(mddev));
6560 err = -EINVAL;
6561 goto abort_export;
6562 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006563
NeilBrownb2d444d2005-11-08 21:39:31 -08006564 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006566 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006567 err = bind_rdev_to_array(rdev, mddev);
6568 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006569 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570
6571 /*
6572 * The rest should better be atomic, we can have disk failures
6573 * noticed in interrupt contexts ...
6574 */
6575
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 rdev->raid_disk = -1;
6577
Shaohua Li29530792016-12-08 15:48:19 -08006578 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006579 if (!mddev->thread)
6580 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 /*
6582 * Kick recovery, maybe this spare has to be added to the
6583 * array immediately.
6584 */
6585 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6586 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006587 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588 return 0;
6589
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590abort_export:
6591 export_rdev(rdev);
6592 return err;
6593}
6594
NeilBrownfd01b882011-10-11 16:47:53 +11006595static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006596{
NeilBrown035328c2014-04-09 12:25:40 +10006597 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006598
NeilBrown36fa3062005-09-09 16:23:45 -07006599 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006600 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006601 return -EBUSY;
6602 if (mddev->recovery || mddev->sync_thread)
6603 return -EBUSY;
6604 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006605 }
6606
NeilBrown36fa3062005-09-09 16:23:45 -07006607 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006608 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006609 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006610
NeilBrown1e594bb2014-12-15 12:57:00 +11006611 if (mddev->bitmap || mddev->bitmap_info.file)
6612 return -EEXIST; /* cannot add when bitmap is present */
6613 f = fget(fd);
6614
6615 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006616 pr_warn("%s: error: failed to get bitmap file\n",
6617 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006618 return -EBADF;
6619 }
6620
NeilBrown1e594bb2014-12-15 12:57:00 +11006621 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006622 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006623 pr_warn("%s: error: bitmap file must be a regular file\n",
6624 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006625 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006626 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006627 pr_warn("%s: error: bitmap file must open for write\n",
6628 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006629 err = -EBADF;
6630 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006631 pr_warn("%s: error: bitmap file is already in use\n",
6632 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006633 err = -EBUSY;
6634 }
6635 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006636 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006637 return err;
6638 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006639 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006640 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006641 } else if (mddev->bitmap == NULL)
6642 return -ENOENT; /* cannot remove what isn't there */
6643 err = 0;
6644 if (mddev->pers) {
NeilBrown69e51b42010-06-01 19:37:35 +10006645 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006646 struct bitmap *bitmap;
6647
6648 bitmap = bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006649 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006650 if (!IS_ERR(bitmap)) {
6651 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006652 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006653 } else
6654 err = PTR_ERR(bitmap);
NeilBrown52a0d492017-10-17 13:46:43 +11006655 if (err) {
6656 bitmap_destroy(mddev);
6657 fd = -1;
6658 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006659 mddev_resume(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11006660 } else if (fd < 0) {
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006661 mddev_suspend(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006662 bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006663 mddev_resume(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006664 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006665 }
6666 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006667 struct file *f = mddev->bitmap_info.file;
6668 if (f) {
6669 spin_lock(&mddev->lock);
6670 mddev->bitmap_info.file = NULL;
6671 spin_unlock(&mddev->lock);
6672 fput(f);
6673 }
NeilBrown36fa3062005-09-09 16:23:45 -07006674 }
6675
NeilBrown32a76272005-06-21 17:17:14 -07006676 return err;
6677}
6678
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679/*
6680 * set_array_info is used two different ways
6681 * The original usage is when creating a new array.
6682 * In this usage, raid_disks is > 0 and it together with
6683 * level, size, not_persistent,layout,chunksize determine the
6684 * shape of the array.
6685 * This will always create an array with a type-0.90.0 superblock.
6686 * The newer usage is when assembling an array.
6687 * In this case raid_disks will be 0, and the major_version field is
6688 * use to determine which style super-blocks are to be found on the devices.
6689 * The minor and patch _version numbers are also kept incase the
6690 * super_block handler wishes to interpret them.
6691 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006692static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693{
6694
6695 if (info->raid_disks == 0) {
6696 /* just setting version number for superblock loading */
6697 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006698 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699 super_types[info->major_version].name == NULL) {
6700 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11006701 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 info->major_version);
6703 return -EINVAL;
6704 }
6705 mddev->major_version = info->major_version;
6706 mddev->minor_version = info->minor_version;
6707 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006708 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006709 /* ensure mddev_put doesn't delete this now that there
6710 * is some minimal configuration.
6711 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006712 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713 return 0;
6714 }
6715 mddev->major_version = MD_MAJOR_VERSION;
6716 mddev->minor_version = MD_MINOR_VERSION;
6717 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006718 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719
6720 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006721 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006722 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 mddev->raid_disks = info->raid_disks;
6724 /* don't set md_minor, it is determined by which /dev/md* was
6725 * openned
6726 */
6727 if (info->state & (1<<MD_SB_CLEAN))
6728 mddev->recovery_cp = MaxSector;
6729 else
6730 mddev->recovery_cp = 0;
6731 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006732 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733
6734 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006735 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736
Shaohua Li29530792016-12-08 15:48:19 -08006737 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11006738 mddev->max_disks = MD_SB_DISKS;
6739 mddev->flags = 0;
6740 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006741 }
6742 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743
NeilBrownc3d97142009-12-14 12:49:52 +11006744 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006745 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006746 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006747
NeilBrownf6705572006-03-27 01:18:11 -08006748 mddev->reshape_position = MaxSector;
6749
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 /*
6751 * Generate a 128 bit UUID
6752 */
6753 get_random_bytes(mddev->uuid, 16);
6754
NeilBrownf6705572006-03-27 01:18:11 -08006755 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006756 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006757 mddev->new_layout = mddev->layout;
6758 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006759 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006760
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761 return 0;
6762}
6763
NeilBrownfd01b882011-10-11 16:47:53 +11006764void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006765{
Dan Williamsb522adc2009-03-31 15:00:31 +11006766 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6767
6768 if (mddev->external_size)
6769 return;
6770
Dan Williams1f403622009-03-31 14:59:03 +11006771 mddev->array_sectors = array_sectors;
6772}
6773EXPORT_SYMBOL(md_set_array_sectors);
6774
NeilBrownfd01b882011-10-11 16:47:53 +11006775static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006776{
NeilBrown3cb03002011-10-11 16:45:26 +11006777 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006778 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006779 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08006780 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006781
NeilBrowna35b0d62006-01-06 00:20:49 -08006782 if (mddev->pers->resize == NULL)
6783 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006784 /* The "num_sectors" is the number of sectors of each device that
6785 * is used. This can only make sense for arrays with redundancy.
6786 * linear and raid0 always use whatever space is available. We can only
6787 * consider changing this number if no resync or reconstruction is
6788 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006789 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006790 * of each device. If num_sectors is zero, we find the largest size
6791 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006792 */
NeilBrownf851b602014-12-11 10:02:10 +11006793 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6794 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006795 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006796 if (mddev->ro)
6797 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006798
NeilBrowndafb20f2012-03-19 12:46:39 +11006799 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006800 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006801
Andre Nolld71f9f82008-07-11 22:02:22 +10006802 if (fit && (num_sectors == 0 || num_sectors > avail))
6803 num_sectors = avail;
6804 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006805 return -ENOSPC;
6806 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006807 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08006808 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08006809 if (mddev_is_clustered(mddev))
6810 md_cluster_ops->update_size(mddev, old_dev_sectors);
6811 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08006812 set_capacity(mddev->gendisk, mddev->array_sectors);
6813 revalidate_disk(mddev->gendisk);
6814 }
6815 }
NeilBrowna35b0d62006-01-06 00:20:49 -08006816 return rv;
6817}
6818
NeilBrownfd01b882011-10-11 16:47:53 +11006819static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006820{
6821 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006822 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006823 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006824 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006825 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006826 if (mddev->ro)
6827 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006828 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006829 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006830 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006831 if (mddev->sync_thread ||
6832 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6833 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006834 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006835
6836 rdev_for_each(rdev, mddev) {
6837 if (mddev->raid_disks < raid_disks &&
6838 rdev->data_offset < rdev->new_data_offset)
6839 return -EINVAL;
6840 if (mddev->raid_disks > raid_disks &&
6841 rdev->data_offset > rdev->new_data_offset)
6842 return -EINVAL;
6843 }
6844
NeilBrown63c70c42006-03-27 01:18:13 -08006845 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006846 if (mddev->delta_disks < 0)
6847 mddev->reshape_backwards = 1;
6848 else if (mddev->delta_disks > 0)
6849 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006850
6851 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006852 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006853 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006854 mddev->reshape_backwards = 0;
6855 }
NeilBrownda943b992006-01-06 00:20:54 -08006856 return rv;
6857}
6858
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859/*
6860 * update_array_info is used to change the configuration of an
6861 * on-line array.
6862 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6863 * fields in the info are checked against the array.
6864 * Any differences that cannot be handled will cause an error.
6865 * Normally, only one change can be managed at a time.
6866 */
NeilBrownfd01b882011-10-11 16:47:53 +11006867static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868{
6869 int rv = 0;
6870 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006871 int state = 0;
6872
6873 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006874 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006875 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876
6877 if (mddev->major_version != info->major_version ||
6878 mddev->minor_version != info->minor_version ||
6879/* mddev->patch_version != info->patch_version || */
6880 mddev->ctime != info->ctime ||
6881 mddev->level != info->level ||
6882/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006883 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006884 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006885 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6886 ((state^info->state) & 0xfffffe00)
6887 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888 return -EINVAL;
6889 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006890 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6891 cnt++;
6892 if (mddev->raid_disks != info->raid_disks)
6893 cnt++;
6894 if (mddev->layout != info->layout)
6895 cnt++;
6896 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6897 cnt++;
6898 if (cnt == 0)
6899 return 0;
6900 if (cnt > 1)
6901 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902
6903 if (mddev->layout != info->layout) {
6904 /* Change layout
6905 * we don't need to do anything at the md level, the
6906 * personality will take care of it all.
6907 */
NeilBrown50ac1682009-06-18 08:47:55 +10006908 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006910 else {
6911 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006912 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006913 if (rv)
6914 mddev->new_layout = mddev->layout;
6915 return rv;
6916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006918 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006919 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920
NeilBrownda943b992006-01-06 00:20:54 -08006921 if (mddev->raid_disks != info->raid_disks)
6922 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923
NeilBrown36fa3062005-09-09 16:23:45 -07006924 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006925 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6926 rv = -EINVAL;
6927 goto err;
6928 }
6929 if (mddev->recovery || mddev->sync_thread) {
6930 rv = -EBUSY;
6931 goto err;
6932 }
NeilBrown36fa3062005-09-09 16:23:45 -07006933 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006934 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006935 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006936 if (mddev->bitmap) {
6937 rv = -EEXIST;
6938 goto err;
6939 }
6940 if (mddev->bitmap_info.default_offset == 0) {
6941 rv = -EINVAL;
6942 goto err;
6943 }
NeilBrownc3d97142009-12-14 12:49:52 +11006944 mddev->bitmap_info.offset =
6945 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006946 mddev->bitmap_info.space =
6947 mddev->bitmap_info.default_space;
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006948 bitmap = bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006949 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006950 if (!IS_ERR(bitmap)) {
6951 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006952 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006953 } else
6954 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006955 if (rv)
6956 bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006957 mddev_resume(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006958 } else {
6959 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006960 if (!mddev->bitmap) {
6961 rv = -ENOENT;
6962 goto err;
6963 }
6964 if (mddev->bitmap->storage.file) {
6965 rv = -EINVAL;
6966 goto err;
6967 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006968 if (mddev->bitmap_info.nodes) {
6969 /* hold PW on all the bitmap lock */
6970 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006971 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006972 rv = -EPERM;
6973 md_cluster_ops->unlock_all_bitmaps(mddev);
6974 goto err;
6975 }
6976
6977 mddev->bitmap_info.nodes = 0;
6978 md_cluster_ops->leave(mddev);
6979 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006980 mddev_suspend(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006981 bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006982 mddev_resume(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11006983 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006984 }
6985 }
NeilBrown850b2b42006-10-03 01:15:46 -07006986 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006987 return rv;
6988err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989 return rv;
6990}
6991
NeilBrownfd01b882011-10-11 16:47:53 +11006992static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993{
NeilBrown3cb03002011-10-11 16:45:26 +11006994 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006995 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996
6997 if (mddev->pers == NULL)
6998 return -ENODEV;
6999
NeilBrown1ca69c42012-10-11 13:37:33 +11007000 rcu_read_lock();
7001 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11007003 err = -ENODEV;
7004 else {
7005 md_error(mddev, rdev);
7006 if (!test_bit(Faulty, &rdev->flags))
7007 err = -EBUSY;
7008 }
7009 rcu_read_unlock();
7010 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011}
7012
Andre Noll2f9618c2008-04-25 18:57:58 +02007013/*
7014 * We have a problem here : there is no easy way to give a CHS
7015 * virtual geometry. We currently pretend that we have a 2 heads
7016 * 4 sectors (with a BIG number of cylinders...). This drives
7017 * dosfs just mad... ;-)
7018 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007019static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7020{
NeilBrownfd01b882011-10-11 16:47:53 +11007021 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007022
7023 geo->heads = 2;
7024 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11007025 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007026 return 0;
7027}
7028
Nicolas Schichancb335f82014-01-15 16:58:52 +01007029static inline bool md_ioctl_valid(unsigned int cmd)
7030{
7031 switch (cmd) {
7032 case ADD_NEW_DISK:
7033 case BLKROSET:
7034 case GET_ARRAY_INFO:
7035 case GET_BITMAP_FILE:
7036 case GET_DISK_INFO:
7037 case HOT_ADD_DISK:
7038 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007039 case RAID_AUTORUN:
7040 case RAID_VERSION:
7041 case RESTART_ARRAY_RW:
7042 case RUN_ARRAY:
7043 case SET_ARRAY_INFO:
7044 case SET_BITMAP_FILE:
7045 case SET_DISK_FAULTY:
7046 case STOP_ARRAY:
7047 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007048 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007049 return true;
7050 default:
7051 return false;
7052 }
7053}
7054
Al Viroa39907f2008-03-02 10:31:15 -05007055static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 unsigned int cmd, unsigned long arg)
7057{
7058 int err = 0;
7059 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11007060 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10007061 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08007062 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063
Nicolas Schichancb335f82014-01-15 16:58:52 +01007064 if (!md_ioctl_valid(cmd))
7065 return -ENOTTY;
7066
NeilBrown506c9e42011-12-23 10:17:26 +11007067 switch (cmd) {
7068 case RAID_VERSION:
7069 case GET_ARRAY_INFO:
7070 case GET_DISK_INFO:
7071 break;
7072 default:
7073 if (!capable(CAP_SYS_ADMIN))
7074 return -EACCES;
7075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076
7077 /*
7078 * Commands dealing with the RAID driver but not any
7079 * particular array:
7080 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007081 switch (cmd) {
7082 case RAID_VERSION:
7083 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007084 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11007087 case RAID_AUTORUN:
7088 err = 0;
7089 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007090 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11007092 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093 }
7094
7095 /*
7096 * Commands creating/starting a new array:
7097 */
7098
Al Viroa39907f2008-03-02 10:31:15 -05007099 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100
7101 if (!mddev) {
7102 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007103 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104 }
7105
NeilBrown1ca69c42012-10-11 13:37:33 +11007106 /* Some actions do not requires the mutex */
7107 switch (cmd) {
7108 case GET_ARRAY_INFO:
7109 if (!mddev->raid_disks && !mddev->external)
7110 err = -ENODEV;
7111 else
7112 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007113 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007114
7115 case GET_DISK_INFO:
7116 if (!mddev->raid_disks && !mddev->external)
7117 err = -ENODEV;
7118 else
7119 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007120 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007121
7122 case SET_DISK_FAULTY:
7123 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007124 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007125
7126 case GET_BITMAP_FILE:
7127 err = get_bitmap_file(mddev, argp);
7128 goto out;
7129
NeilBrown1ca69c42012-10-11 13:37:33 +11007130 }
7131
NeilBrowna7a3f082012-12-11 13:35:54 +11007132 if (cmd == ADD_NEW_DISK)
7133 /* need to ensure md_delayed_delete() has completed */
7134 flush_workqueue(md_misc_wq);
7135
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007136 if (cmd == HOT_REMOVE_DISK)
7137 /* need to ensure recovery thread has run */
7138 wait_event_interruptible_timeout(mddev->sb_wait,
7139 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007140 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007141 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007142 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7143 /* Need to flush page cache, and ensure no-one else opens
7144 * and writes
7145 */
7146 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007147 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007148 mutex_unlock(&mddev->open_mutex);
7149 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007150 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007151 }
NeilBrown065e5192017-04-06 11:16:33 +08007152 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007153 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007154 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007155 mutex_unlock(&mddev->open_mutex);
7156 sync_blockdev(bdev);
7157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 err = mddev_lock(mddev);
7159 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007160 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7161 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007162 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 }
7164
NeilBrownc02c0ae2012-12-11 13:39:21 +11007165 if (cmd == SET_ARRAY_INFO) {
7166 mdu_array_info_t info;
7167 if (!arg)
7168 memset(&info, 0, sizeof(info));
7169 else if (copy_from_user(&info, argp, sizeof(info))) {
7170 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007171 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007172 }
7173 if (mddev->pers) {
7174 err = update_array_info(mddev, &info);
7175 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007176 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007177 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007179 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007180 }
7181 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007182 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007183 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007184 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007185 }
7186 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007187 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007188 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007189 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007190 }
7191 err = set_array_info(mddev, &info);
7192 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007193 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007194 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007195 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007196 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 }
7198
7199 /*
7200 * Commands querying/configuring an existing array:
7201 */
NeilBrown32a76272005-06-21 17:17:14 -07007202 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007203 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007204 if ((!mddev->raid_disks && !mddev->external)
7205 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7206 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7207 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007209 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 }
7211
7212 /*
7213 * Commands even a read-only array can execute:
7214 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007215 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007216 case RESTART_ARRAY_RW:
7217 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007218 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007219
7220 case STOP_ARRAY:
7221 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007222 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007223
7224 case STOP_ARRAY_RO:
7225 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007226 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007227
NeilBrown3ea8929d2013-04-24 11:42:41 +10007228 case HOT_REMOVE_DISK:
7229 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007230 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007231
NeilBrown7ceb17e2013-04-24 11:42:42 +10007232 case ADD_NEW_DISK:
7233 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007234 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007235 * So require mddev->pers and MD_DISK_SYNC.
7236 */
7237 if (mddev->pers) {
7238 mdu_disk_info_t info;
7239 if (copy_from_user(&info, argp, sizeof(info)))
7240 err = -EFAULT;
7241 else if (!(info.state & (1<<MD_DISK_SYNC)))
7242 /* Need to clear read-only for this */
7243 break;
7244 else
7245 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007246 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007247 }
7248 break;
7249
NeilBrownc02c0ae2012-12-11 13:39:21 +11007250 case BLKROSET:
7251 if (get_user(ro, (int __user *)(arg))) {
7252 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007253 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007254 }
7255 err = -EINVAL;
7256
7257 /* if the bdev is going readonly the value of mddev->ro
7258 * does not matter, no writes are coming
7259 */
7260 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007261 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007262
NeilBrownc02c0ae2012-12-11 13:39:21 +11007263 /* are we are already prepared for writes? */
7264 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007265 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007266
7267 /* transitioning to readauto need only happen for
7268 * arrays that call md_write_start
7269 */
7270 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007271 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007272 if (err == 0) {
7273 mddev->ro = 2;
7274 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007275 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007276 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007277 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278 }
7279
7280 /*
7281 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007282 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 */
NeilBrown326eb172014-09-30 15:36:28 +10007284 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007285 if (mddev->ro == 2) {
7286 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007287 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007288 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007289 /* mddev_unlock will wake thread */
7290 /* If a device failed while we were read-only, we
7291 * need to make sure the metadata is updated now.
7292 */
Shaohua Li29530792016-12-08 15:48:19 -08007293 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007294 mddev_unlock(mddev);
7295 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007296 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7297 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007298 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007299 }
NeilBrownf91de922005-11-08 21:39:36 -08007300 } else {
7301 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007302 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304 }
7305
NeilBrownc02c0ae2012-12-11 13:39:21 +11007306 switch (cmd) {
7307 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007309 mdu_disk_info_t info;
7310 if (copy_from_user(&info, argp, sizeof(info)))
7311 err = -EFAULT;
7312 else
7313 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007314 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007317 case CLUSTERED_DISK_NACK:
7318 if (mddev_is_clustered(mddev))
7319 md_cluster_ops->new_disk_ack(mddev, false);
7320 else
7321 err = -EINVAL;
7322 goto unlock;
7323
NeilBrownc02c0ae2012-12-11 13:39:21 +11007324 case HOT_ADD_DISK:
7325 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007326 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327
NeilBrownc02c0ae2012-12-11 13:39:21 +11007328 case RUN_ARRAY:
7329 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007330 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331
NeilBrownc02c0ae2012-12-11 13:39:21 +11007332 case SET_BITMAP_FILE:
7333 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007334 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007335
NeilBrownc02c0ae2012-12-11 13:39:21 +11007336 default:
7337 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007338 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339 }
7340
NeilBrown3adc28d2014-09-30 15:46:41 +10007341unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007342 if (mddev->hold_active == UNTIL_IOCTL &&
7343 err != -EINVAL)
7344 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007346out:
NeilBrown065e5192017-04-06 11:16:33 +08007347 if(did_set_md_closing)
7348 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 return err;
7350}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007351#ifdef CONFIG_COMPAT
7352static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7353 unsigned int cmd, unsigned long arg)
7354{
7355 switch (cmd) {
7356 case HOT_REMOVE_DISK:
7357 case HOT_ADD_DISK:
7358 case SET_DISK_FAULTY:
7359 case SET_BITMAP_FILE:
7360 /* These take in integer arg, do not convert */
7361 break;
7362 default:
7363 arg = (unsigned long)compat_ptr(arg);
7364 break;
7365 }
7366
7367 return md_ioctl(bdev, mode, cmd, arg);
7368}
7369#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370
Al Viroa39907f2008-03-02 10:31:15 -05007371static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372{
7373 /*
7374 * Succeed if we can lock the mddev, which confirms that
7375 * it isn't being stopped right now.
7376 */
NeilBrownfd01b882011-10-11 16:47:53 +11007377 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378 int err;
7379
Yuanhan Liu0c098222012-05-22 13:55:32 +10007380 if (!mddev)
7381 return -ENODEV;
7382
NeilBrownd3374822009-01-09 08:31:10 +11007383 if (mddev->gendisk != bdev->bd_disk) {
7384 /* we are racing with mddev_put which is discarding this
7385 * bd_disk.
7386 */
7387 mddev_put(mddev);
7388 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007389 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007390 /* Then retry the open from the top */
7391 return -ERESTARTSYS;
7392 }
7393 BUG_ON(mddev != bdev->bd_disk->private_data);
7394
NeilBrownc8c00a62009-08-10 12:50:52 +10007395 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 goto out;
7397
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007398 if (test_bit(MD_CLOSING, &mddev->flags)) {
7399 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007400 err = -ENODEV;
7401 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007402 }
7403
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007405 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007406 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007408 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007410 if (err)
7411 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412 return err;
7413}
7414
Al Virodb2a1442013-05-05 21:52:57 -04007415static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007417 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007419 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007420 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007423
7424static int md_media_changed(struct gendisk *disk)
7425{
NeilBrownfd01b882011-10-11 16:47:53 +11007426 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007427
7428 return mddev->changed;
7429}
7430
7431static int md_revalidate(struct gendisk *disk)
7432{
NeilBrownfd01b882011-10-11 16:47:53 +11007433 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007434
7435 mddev->changed = 0;
7436 return 0;
7437}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007438static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439{
7440 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007441 .open = md_open,
7442 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007443 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007444#ifdef CONFIG_COMPAT
7445 .compat_ioctl = md_compat_ioctl,
7446#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007447 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007448 .media_changed = md_media_changed,
7449 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450};
7451
NeilBrownf72ffdd2014-09-30 14:23:59 +10007452static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453{
NeilBrown2b8bf342011-10-11 16:48:23 +11007454 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 /*
7457 * md_thread is a 'system-thread', it's priority should be very
7458 * high. We avoid resource deadlocks individually in each
7459 * raid personality. (RAID5 does preallocation) We also use RR and
7460 * the very same RT priority as kswapd, thus we will never get
7461 * into a priority inversion deadlock.
7462 *
7463 * we definitely have to have equal or higher priority than
7464 * bdflush, otherwise bdflush will deadlock if there are too
7465 * many dirty RAID5 blocks.
7466 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467
NeilBrown6985c432005-10-19 21:23:47 -07007468 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007469 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470
NeilBrown93588e22005-11-15 00:09:12 -08007471 /* We need to wait INTERRUPTIBLE so that
7472 * we don't add to the load-average.
7473 * That means we need to be sure no signals are
7474 * pending
7475 */
7476 if (signal_pending(current))
7477 flush_signals(current);
7478
7479 wait_event_interruptible_timeout
7480 (thread->wqueue,
7481 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007482 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007483 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484
NeilBrown6c987912011-01-14 09:13:53 +11007485 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007486 if (kthread_should_park())
7487 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007488 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007489 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007491
Linus Torvalds1da177e2005-04-16 15:20:36 -07007492 return 0;
7493}
7494
NeilBrown2b8bf342011-10-11 16:48:23 +11007495void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496{
7497 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007498 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Guoqing Jiangd1d90142017-10-09 10:32:48 +08007499 set_bit(THREAD_WAKEUP, &thread->flags);
7500 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501 }
7502}
NeilBrown6c144d32014-09-30 16:15:38 +10007503EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504
Shaohua Li4ed87312012-10-11 13:34:00 +11007505struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7506 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507{
NeilBrown2b8bf342011-10-11 16:48:23 +11007508 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509
NeilBrown2b8bf342011-10-11 16:48:23 +11007510 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511 if (!thread)
7512 return NULL;
7513
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514 init_waitqueue_head(&thread->wqueue);
7515
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516 thread->run = run;
7517 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007518 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007519 thread->tsk = kthread_run(md_thread, thread,
7520 "%s_%s",
7521 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007522 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007523 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524 kfree(thread);
7525 return NULL;
7526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527 return thread;
7528}
NeilBrown6c144d32014-09-30 16:15:38 +10007529EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530
NeilBrown2b8bf342011-10-11 16:48:23 +11007531void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532{
NeilBrown2b8bf342011-10-11 16:48:23 +11007533 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007534 if (!thread)
7535 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007536 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007537 /* Locking ensures that mddev_unlock does not wake_up a
7538 * non-existent thread
7539 */
7540 spin_lock(&pers_lock);
7541 *threadp = NULL;
7542 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007543
7544 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545 kfree(thread);
7546}
NeilBrown6c144d32014-09-30 16:15:38 +10007547EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548
NeilBrownfd01b882011-10-11 16:47:53 +11007549void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550{
NeilBrownb2d444d2005-11-08 21:39:31 -08007551 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007553
NeilBrownde393cd2011-07-28 11:31:48 +10007554 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555 return;
7556 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007557 if (mddev->degraded)
7558 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007559 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7561 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7562 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007563 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007564 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007565 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566}
NeilBrown6c144d32014-09-30 16:15:38 +10007567EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568
7569/* seq_file implementation /proc/mdstat */
7570
7571static void status_unused(struct seq_file *seq)
7572{
7573 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007574 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575
7576 seq_printf(seq, "unused devices: ");
7577
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007578 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579 char b[BDEVNAME_SIZE];
7580 i++;
7581 seq_printf(seq, "%s ",
7582 bdevname(rdev->bdev,b));
7583 }
7584 if (!i)
7585 seq_printf(seq, "<none>");
7586
7587 seq_printf(seq, "\n");
7588}
7589
NeilBrownf7851be2015-07-02 17:12:58 +10007590static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591{
NeilBrowndd71cf62009-05-07 12:49:35 +10007592 sector_t max_sectors, resync, res;
7593 unsigned long dt, db;
7594 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007595 int scale;
7596 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007597
NeilBrownc804cde2012-05-21 09:28:33 +10007598 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7599 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007600 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007601 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007602 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603
NeilBrownf7851be2015-07-02 17:12:58 +10007604 resync = mddev->curr_resync;
7605 if (resync <= 3) {
7606 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7607 /* Still cleaning up */
7608 resync = max_sectors;
7609 } else
7610 resync -= atomic_read(&mddev->recovery_active);
7611
7612 if (resync == 0) {
7613 if (mddev->recovery_cp < MaxSector) {
7614 seq_printf(seq, "\tresync=PENDING");
7615 return 1;
7616 }
7617 return 0;
7618 }
7619 if (resync < 3) {
7620 seq_printf(seq, "\tresync=DELAYED");
7621 return 1;
7622 }
7623
NeilBrown403df472014-09-30 15:52:29 +10007624 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007625 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007626 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007627 * u32, as those are the requirements for sector_div.
7628 * Thus 'scale' must be at least 10
7629 */
7630 scale = 10;
7631 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007632 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007633 scale++;
7634 }
7635 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007636 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007637
7638 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639 {
NeilBrown4588b422006-03-27 01:18:04 -08007640 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641 seq_printf(seq, "[");
7642 for (i = 0; i < x; i++)
7643 seq_printf(seq, "=");
7644 seq_printf(seq, ">");
7645 for (i = 0; i < y; i++)
7646 seq_printf(seq, ".");
7647 seq_printf(seq, "] ");
7648 }
NeilBrown4588b422006-03-27 01:18:04 -08007649 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007650 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7651 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007652 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7653 "check" :
7654 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7655 "resync" : "recovery"))),
7656 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007657 (unsigned long long) resync/2,
7658 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007659
7660 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661 * dt: time from mark until now
7662 * db: blocks written from mark until now
7663 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007664 *
7665 * rt is a sector_t, so could be 32bit or 64bit.
7666 * So we divide before multiply in case it is 32bit and close
7667 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007668 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007669 * near the end of resync when the number of remaining sectors
7670 * is close to 'db'.
7671 * We then divide rt by 32 after multiplying by db to compensate.
7672 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673 */
7674 dt = ((jiffies - mddev->resync_mark) / HZ);
7675 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007676 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7677 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678
NeilBrowndd71cf62009-05-07 12:49:35 +10007679 rt = max_sectors - resync; /* number of remaining sectors */
7680 sector_div(rt, db/32+1);
7681 rt *= dt;
7682 rt >>= 5;
7683
7684 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7685 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007686
NeilBrownff4e8d92006-07-10 04:44:16 -07007687 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007688 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689}
7690
7691static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7692{
7693 struct list_head *tmp;
7694 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007695 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696
7697 if (l >= 0x10000)
7698 return NULL;
7699 if (!l--)
7700 /* header */
7701 return (void*)1;
7702
7703 spin_lock(&all_mddevs_lock);
7704 list_for_each(tmp,&all_mddevs)
7705 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007706 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007707 mddev_get(mddev);
7708 spin_unlock(&all_mddevs_lock);
7709 return mddev;
7710 }
7711 spin_unlock(&all_mddevs_lock);
7712 if (!l--)
7713 return (void*)2;/* tail */
7714 return NULL;
7715}
7716
7717static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7718{
7719 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007720 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007721
Linus Torvalds1da177e2005-04-16 15:20:36 -07007722 ++*pos;
7723 if (v == (void*)2)
7724 return NULL;
7725
7726 spin_lock(&all_mddevs_lock);
7727 if (v == (void*)1)
7728 tmp = all_mddevs.next;
7729 else
7730 tmp = mddev->all_mddevs.next;
7731 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007732 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733 else {
7734 next_mddev = (void*)2;
7735 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007737 spin_unlock(&all_mddevs_lock);
7738
7739 if (v != (void*)1)
7740 mddev_put(mddev);
7741 return next_mddev;
7742
7743}
7744
7745static void md_seq_stop(struct seq_file *seq, void *v)
7746{
NeilBrownfd01b882011-10-11 16:47:53 +11007747 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007748
7749 if (mddev && v != (void*)1 && v != (void*)2)
7750 mddev_put(mddev);
7751}
7752
7753static int md_seq_show(struct seq_file *seq, void *v)
7754{
NeilBrownfd01b882011-10-11 16:47:53 +11007755 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007756 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007757 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007758
7759 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007760 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007761 seq_printf(seq, "Personalities : ");
7762 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007763 list_for_each_entry(pers, &pers_list, list)
7764 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007765
7766 spin_unlock(&pers_lock);
7767 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007768 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769 return 0;
7770 }
7771 if (v == (void*)2) {
7772 status_unused(seq);
7773 return 0;
7774 }
7775
NeilBrown36d091f2014-12-15 12:56:58 +11007776 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007777 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7778 seq_printf(seq, "%s : %sactive", mdname(mddev),
7779 mddev->pers ? "" : "in");
7780 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007781 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007782 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007783 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007784 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785 seq_printf(seq, " %s", mddev->pers->name);
7786 }
7787
Andre Nolldd8ac332009-03-31 14:33:13 +11007788 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007789 rcu_read_lock();
7790 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791 char b[BDEVNAME_SIZE];
7792 seq_printf(seq, " %s[%d]",
7793 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007794 if (test_bit(WriteMostly, &rdev->flags))
7795 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007796 if (test_bit(Journal, &rdev->flags))
7797 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007798 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007799 seq_printf(seq, "(F)");
7800 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007801 }
7802 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007803 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007804 if (test_bit(Replacement, &rdev->flags))
7805 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007806 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007808 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007809
7810 if (!list_empty(&mddev->disks)) {
7811 if (mddev->pers)
7812 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007813 (unsigned long long)
7814 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007815 else
7816 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007817 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007818 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007819 if (mddev->persistent) {
7820 if (mddev->major_version != 0 ||
7821 mddev->minor_version != 90) {
7822 seq_printf(seq," super %d.%d",
7823 mddev->major_version,
7824 mddev->minor_version);
7825 }
NeilBrowne6910632008-02-06 01:39:51 -08007826 } else if (mddev->external)
7827 seq_printf(seq, " super external:%s",
7828 mddev->metadata_type);
7829 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007830 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007831
7832 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007833 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007834 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007835 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007836 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007837 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007838 }
NeilBrown32a76272005-06-21 17:17:14 -07007839 } else
7840 seq_printf(seq, "\n ");
7841
NeilBrown57148962012-03-19 12:46:40 +11007842 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843
7844 seq_printf(seq, "\n");
7845 }
NeilBrown36d091f2014-12-15 12:56:58 +11007846 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007847
Linus Torvalds1da177e2005-04-16 15:20:36 -07007848 return 0;
7849}
7850
Jan Engelhardt110518b2009-05-07 12:49:37 +10007851static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852 .start = md_seq_start,
7853 .next = md_seq_next,
7854 .stop = md_seq_stop,
7855 .show = md_seq_show,
7856};
7857
7858static int md_seq_open(struct inode *inode, struct file *file)
7859{
Kay Sieversf1514632011-07-12 20:48:39 +02007860 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861 int error;
7862
7863 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007864 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007865 return error;
7866
7867 seq = file->private_data;
7868 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869 return error;
7870}
7871
NeilBrowne2f23b62014-04-09 14:33:51 +10007872static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007873static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7874{
Kay Sieversf1514632011-07-12 20:48:39 +02007875 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007876 int mask;
7877
NeilBrowne2f23b62014-04-09 14:33:51 +10007878 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007879 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007880 poll_wait(filp, &md_event_waiters, wait);
7881
7882 /* always allow read */
7883 mask = POLLIN | POLLRDNORM;
7884
Kay Sieversf1514632011-07-12 20:48:39 +02007885 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007886 mask |= POLLERR | POLLPRI;
7887 return mask;
7888}
7889
Arjan van de Venfa027c22007-02-12 00:55:33 -08007890static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007891 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892 .open = md_seq_open,
7893 .read = seq_read,
7894 .llseek = seq_lseek,
Cihangir Akturk26e13042017-07-29 19:52:45 +03007895 .release = seq_release,
NeilBrownd7603b72006-01-06 00:20:30 -08007896 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897};
7898
NeilBrown84fc4b52011-10-11 16:49:58 +11007899int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007900{
NeilBrown9d487392016-11-02 14:16:49 +11007901 pr_debug("md: %s personality registered for level %d\n",
7902 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007904 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905 spin_unlock(&pers_lock);
7906 return 0;
7907}
NeilBrown6c144d32014-09-30 16:15:38 +10007908EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909
NeilBrown84fc4b52011-10-11 16:49:58 +11007910int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911{
NeilBrown9d487392016-11-02 14:16:49 +11007912 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007914 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915 spin_unlock(&pers_lock);
7916 return 0;
7917}
NeilBrown6c144d32014-09-30 16:15:38 +10007918EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919
NeilBrown6022e752015-08-13 12:32:55 +10007920int register_md_cluster_operations(struct md_cluster_operations *ops,
7921 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007922{
NeilBrown6022e752015-08-13 12:32:55 +10007923 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007924 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007925 if (md_cluster_ops != NULL)
7926 ret = -EALREADY;
7927 else {
7928 md_cluster_ops = ops;
7929 md_cluster_mod = module;
7930 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007931 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007932 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007933}
7934EXPORT_SYMBOL(register_md_cluster_operations);
7935
7936int unregister_md_cluster_operations(void)
7937{
7938 spin_lock(&pers_lock);
7939 md_cluster_ops = NULL;
7940 spin_unlock(&pers_lock);
7941 return 0;
7942}
7943EXPORT_SYMBOL(unregister_md_cluster_operations);
7944
7945int md_setup_cluster(struct mddev *mddev, int nodes)
7946{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007947 if (!md_cluster_ops)
7948 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007949 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007950 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007951 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11007952 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007953 spin_unlock(&pers_lock);
7954 return -ENOENT;
7955 }
7956 spin_unlock(&pers_lock);
7957
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007958 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007959}
7960
7961void md_cluster_stop(struct mddev *mddev)
7962{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007963 if (!md_cluster_ops)
7964 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007965 md_cluster_ops->leave(mddev);
7966 module_put(md_cluster_mod);
7967}
7968
NeilBrownfd01b882011-10-11 16:47:53 +11007969static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007970{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007971 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007973 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007974
7975 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007976 rcu_read_lock();
7977 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007978 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007979 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7980 (int)part_stat_read(&disk->part0, sectors[1]) -
7981 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007982 /* sync IO will cause sync_io to increase before the disk_stats
7983 * as sync_io is counted when a request starts, and
7984 * disk_stats is counted when it completes.
7985 * So resync activity will cause curr_events to be smaller than
7986 * when there was no such activity.
7987 * non-sync IO will cause disk_stat to increase without
7988 * increasing sync_io so curr_events will (eventually)
7989 * be larger than it was before. Once it becomes
7990 * substantially larger, the test below will cause
7991 * the array to appear non-idle, and resync will slow
7992 * down.
7993 * If there is a lot of outstanding resync activity when
7994 * we set last_event to curr_events, then all that activity
7995 * completing might cause the array to appear non-idle
7996 * and resync will be slowed down even though there might
7997 * not have been non-resync activity. This will only
7998 * happen once though. 'last_events' will soon reflect
7999 * the state where there is little or no outstanding
8000 * resync requests, and further resync activity will
8001 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08008002 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003 */
NeilBrowneea1bf32009-03-31 14:27:02 +11008004 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005 rdev->last_events = curr_events;
8006 idle = 0;
8007 }
8008 }
NeilBrown4b809912008-07-21 17:05:25 +10008009 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008010 return idle;
8011}
8012
NeilBrownfd01b882011-10-11 16:47:53 +11008013void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008014{
8015 /* another "blocks" (512byte) blocks have been synced */
8016 atomic_sub(blocks, &mddev->recovery_active);
8017 wake_up(&mddev->recovery_wait);
8018 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07008019 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08008020 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008021 md_wakeup_thread(mddev->thread);
8022 // stop recovery, signal do_sync ....
8023 }
8024}
NeilBrown6c144d32014-09-30 16:15:38 +10008025EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008026
NeilBrown06d91a52005-06-21 17:17:12 -07008027/* md_write_start(mddev, bi)
8028 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07008029 * in superblock) before writing, schedule a superblock update
8030 * and wait for it to complete.
NeilBrowncc27b0c2017-06-05 16:49:39 +10008031 * A return value of 'false' means that the write wasn't recorded
8032 * and cannot proceed as the array is being suspend.
NeilBrown06d91a52005-06-21 17:17:12 -07008033 */
NeilBrowncc27b0c2017-06-05 16:49:39 +10008034bool md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008035{
Neil Brown0fd62b82008-06-28 08:31:36 +10008036 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07008037 if (bio_data_dir(bi) != WRITE)
NeilBrowncc27b0c2017-06-05 16:49:39 +10008038 return true;
NeilBrown06d91a52005-06-21 17:17:12 -07008039
NeilBrownf91de922005-11-08 21:39:36 -08008040 BUG_ON(mddev->ro == 1);
8041 if (mddev->ro == 2) {
8042 /* need to switch to read/write */
8043 mddev->ro = 0;
8044 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8045 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08008046 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008047 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08008048 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008049 rcu_read_lock();
8050 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11008051 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07008052 if (mddev->safemode == 1)
8053 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11008054 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
NeilBrown81fe48e2017-08-08 16:56:36 +10008055 if (mddev->in_sync || mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11008056 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07008057 if (mddev->in_sync) {
8058 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008059 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8060 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07008061 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008062 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07008063 }
NeilBrown85572d72014-12-15 12:56:56 +11008064 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07008065 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008066 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10008067 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008068 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07008069 wait_event(mddev->sb_wait,
NeilBrownd47c8ad2017-10-05 16:23:16 +11008070 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8071 mddev->suspended);
NeilBrowncc27b0c2017-06-05 16:49:39 +10008072 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8073 percpu_ref_put(&mddev->writes_pending);
8074 return false;
8075 }
8076 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008077}
NeilBrown6c144d32014-09-30 16:15:38 +10008078EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008079
NeilBrown49728052017-03-15 14:05:12 +11008080/* md_write_inc can only be called when md_write_start() has
8081 * already been called at least once of the current request.
8082 * It increments the counter and is useful when a single request
8083 * is split into several parts. Each part causes an increment and
8084 * so needs a matching md_write_end().
8085 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8086 * a spinlocked region.
8087 */
8088void md_write_inc(struct mddev *mddev, struct bio *bi)
8089{
8090 if (bio_data_dir(bi) != WRITE)
8091 return;
8092 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008093 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008094}
8095EXPORT_SYMBOL(md_write_inc);
8096
NeilBrownfd01b882011-10-11 16:47:53 +11008097void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008098{
NeilBrown4ad23a972017-03-15 14:05:14 +11008099 percpu_ref_put(&mddev->writes_pending);
8100
8101 if (mddev->safemode == 2)
8102 md_wakeup_thread(mddev->thread);
8103 else if (mddev->safemode_delay)
8104 /* The roundup() ensures this only performs locking once
8105 * every ->safemode_delay jiffies
8106 */
8107 mod_timer(&mddev->safemode_timer,
8108 roundup(jiffies, mddev->safemode_delay) +
8109 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008110}
NeilBrown4ad23a972017-03-15 14:05:14 +11008111
NeilBrown6c144d32014-09-30 16:15:38 +10008112EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008113
NeilBrown2a2275d2007-01-26 00:57:11 -08008114/* md_allow_write(mddev)
8115 * Calling this ensures that the array is marked 'active' so that writes
8116 * may proceed without blocking. It is important to call this before
8117 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8118 * Must be called with mddev_lock held.
8119 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008120void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008121{
8122 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008123 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008124 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008125 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008126 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008127 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008128
NeilBrown85572d72014-12-15 12:56:56 +11008129 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008130 if (mddev->in_sync) {
8131 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008132 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8133 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008134 if (mddev->safemode_delay &&
8135 mddev->safemode == 0)
8136 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008137 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008138 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008139 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008140 /* wait for the dirty state to be recorded in the metadata */
8141 wait_event(mddev->sb_wait,
8142 !test_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags) &&
8143 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008144 } else
NeilBrown85572d72014-12-15 12:56:56 +11008145 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008146}
8147EXPORT_SYMBOL_GPL(md_allow_write);
8148
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149#define SYNC_MARKS 10
8150#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008151#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008152void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008153{
Shaohua Li4ed87312012-10-11 13:34:00 +11008154 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008155 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008156 unsigned int currspeed = 0,
8157 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008158 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008159 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008160 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161 sector_t mark_cnt[SYNC_MARKS];
8162 int last_mark,m;
8163 struct list_head *tmp;
8164 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008165 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008166 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008167 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008168 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008169 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008170
8171 /* just incase thread restarts... */
8172 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
8173 return;
NeilBrown3991b312014-05-28 13:39:23 +10008174 if (mddev->ro) {/* never try to sync a read-only array */
8175 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008176 return;
NeilBrown3991b312014-05-28 13:39:23 +10008177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008179 if (mddev_is_clustered(mddev)) {
8180 ret = md_cluster_ops->resync_start(mddev);
8181 if (ret)
8182 goto skip;
8183
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008184 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008185 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8186 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8187 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8188 && ((unsigned long long)mddev->curr_resync_completed
8189 < (unsigned long long)mddev->resync_max_sectors))
8190 goto skip;
8191 }
8192
NeilBrown61df9d92006-10-03 01:15:57 -07008193 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008194 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008195 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008196 action = "check";
8197 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008198 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008199 action = "repair";
8200 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008201 desc = "resync";
8202 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8203 desc = "reshape";
8204 else
8205 desc = "recovery";
8206
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008207 mddev->last_sync_action = action ?: desc;
8208
Linus Torvalds1da177e2005-04-16 15:20:36 -07008209 /* we overload curr_resync somewhat here.
8210 * 0 == not engaged in resync at all
8211 * 2 == checking that there is no conflict with another sync
8212 * 1 == like 2, but have yielded to allow conflicting resync to
8213 * commense
8214 * other == active in resync - this many blocks
8215 *
8216 * Before starting a resync we must have set curr_resync to
8217 * 2, and then checked that every "conflicting" array has curr_resync
8218 * less than ours. When we find one that is the same or higher
8219 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8220 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8221 * This will mean we have to start checking from the beginning again.
8222 *
8223 */
8224
8225 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008226 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227 mddev->curr_resync = 2;
8228
8229 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008230 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008231 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008232 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008233 if (mddev2 == mddev)
8234 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008235 if (!mddev->parallel_resync
8236 && mddev2->curr_resync
8237 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238 DEFINE_WAIT(wq);
8239 if (mddev < mddev2 && mddev->curr_resync == 2) {
8240 /* arbitrarily yield */
8241 mddev->curr_resync = 1;
8242 wake_up(&resync_wait);
8243 }
8244 if (mddev > mddev2 && mddev->curr_resync == 1)
8245 /* no need to wait here, we can wait the next
8246 * time 'round when curr_resync == 2
8247 */
8248 continue;
NeilBrown97441972008-09-19 11:49:54 +10008249 /* We need to wait 'interruptible' so as not to
8250 * contribute to the load average, and not to
8251 * be caught by 'softlockup'
8252 */
8253 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008254 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008255 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008256 if (mddev2_minor != mddev2->md_minor) {
8257 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008258 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8259 desc, mdname(mddev),
8260 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008262 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008263 if (signal_pending(current))
8264 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008265 schedule();
8266 finish_wait(&resync_wait, &wq);
8267 goto try_again;
8268 }
8269 finish_wait(&resync_wait, &wq);
8270 }
8271 }
8272 } while (mddev->curr_resync < 2);
8273
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008274 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008275 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008276 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008277 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008278 */
8279 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008280 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008281 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008282 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8283 j = mddev->resync_min;
8284 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008285 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008286
NeilBrownccfcc3c2006-03-27 01:18:09 -08008287 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10008288 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008289 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008290 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008291 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008292 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008293 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008294 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008295 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008296 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008297 !test_bit(Faulty, &rdev->flags) &&
8298 !test_bit(In_sync, &rdev->flags) &&
8299 rdev->recovery_offset < j)
8300 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008301 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008302
8303 /* If there is a bitmap, we need to make sure all
8304 * writes that started before we added a spare
8305 * complete before we start doing a recovery.
8306 * Otherwise the write might complete and (via
8307 * bitmap_endwrite) set a bit in the bitmap after the
8308 * recovery has checked that bit and skipped that
8309 * region.
8310 */
8311 if (mddev->bitmap) {
8312 mddev->pers->quiesce(mddev, 1);
8313 mddev->pers->quiesce(mddev, 0);
8314 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008316
NeilBrown9d487392016-11-02 14:16:49 +11008317 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8318 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8319 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8320 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008321
NeilBrowneea1bf32009-03-31 14:27:02 +11008322 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008323
NeilBrown57afd892005-06-21 17:17:13 -07008324 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008325 for (m = 0; m < SYNC_MARKS; m++) {
8326 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008327 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008328 }
8329 last_mark = 0;
8330 mddev->resync_mark = mark[last_mark];
8331 mddev->resync_mark_cnt = mark_cnt[last_mark];
8332
8333 /*
8334 * Tune reconstruction:
8335 */
8336 window = 32*(PAGE_SIZE/512);
NeilBrown9d487392016-11-02 14:16:49 +11008337 pr_debug("md: using %dk window, over a total of %lluk.\n",
8338 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339
8340 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341 last_check = 0;
8342
8343 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008344 pr_debug("md: resuming %s of %s from checkpoint.\n",
8345 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008346 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008347 } else
8348 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008349 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008350 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8351 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008352 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008353
majianpeng7c2c57c2012-07-03 12:12:26 +10008354 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008356 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008357
NeilBrown57afd892005-06-21 17:17:13 -07008358 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008359
NeilBrown7a91ee12009-05-26 12:57:21 +10008360 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8361 ((mddev->curr_resync > mddev->curr_resync_completed &&
8362 (mddev->curr_resync - mddev->curr_resync_completed)
8363 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008364 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008365 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008366 >= mddev->resync_max - mddev->curr_resync_completed ||
8367 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008368 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008369 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008370 wait_event(mddev->recovery_wait,
8371 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008372 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008373 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8374 j > mddev->recovery_cp)
8375 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008376 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008377 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008378 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008379 }
NeilBrownacb180b2009-04-14 16:28:34 +10008380
NeilBrownc91abf52013-11-19 12:02:01 +11008381 while (j >= mddev->resync_max &&
8382 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008383 /* As this condition is controlled by user-space,
8384 * we can block indefinitely, so use '_interruptible'
8385 * to avoid triggering warnings.
8386 */
8387 flush_signals(current); /* just in case */
8388 wait_event_interruptible(mddev->recovery_wait,
8389 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008390 || test_bit(MD_RECOVERY_INTR,
8391 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008392 }
NeilBrownacb180b2009-04-14 16:28:34 +10008393
NeilBrownc91abf52013-11-19 12:02:01 +11008394 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8395 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008396
NeilBrown09314792015-02-19 16:04:40 +11008397 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008398 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008399 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008400 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008401 }
NeilBrown57afd892005-06-21 17:17:13 -07008402
8403 if (!skipped) { /* actual IO requested */
8404 io_sectors += sectors;
8405 atomic_add(sectors, &mddev->recovery_active);
8406 }
8407
NeilBrowne875ece2011-07-28 11:39:24 +10008408 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8409 break;
8410
Linus Torvalds1da177e2005-04-16 15:20:36 -07008411 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008412 if (j > max_sectors)
8413 /* when skipping, extra large numbers can be returned. */
8414 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008415 if (j > 2)
8416 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008417 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008418 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008419 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008420 * visible in /proc/mdstat
8421 */
8422 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008423
8424 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008425 continue;
8426
NeilBrown57afd892005-06-21 17:17:13 -07008427 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008428 repeat:
8429 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8430 /* step marks */
8431 int next = (last_mark+1) % SYNC_MARKS;
8432
8433 mddev->resync_mark = mark[next];
8434 mddev->resync_mark_cnt = mark_cnt[next];
8435 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008436 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008437 last_mark = next;
8438 }
8439
NeilBrownc91abf52013-11-19 12:02:01 +11008440 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442
8443 /*
8444 * this loop exits only if either when we are slower than
8445 * the 'hard' speed limit, or the system was IO-idle for
8446 * a jiffy.
8447 * the system might be non-idle CPU-wise, but we only care
8448 * about not overloading the IO subsystem. (things like an
8449 * e2fsck being done on the RAID array should execute fast)
8450 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451 cond_resched();
8452
Xiao Niac7e50a2014-08-07 09:37:41 -04008453 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8454 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008455 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008456
NeilBrown88202a02006-01-06 00:21:36 -08008457 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008458 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008459 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008460 goto repeat;
8461 }
NeilBrownac8fa412015-02-19 16:55:00 +11008462 if (!is_mddev_idle(mddev, 0)) {
8463 /*
8464 * Give other IO more of a chance.
8465 * The faster the devices, the less we wait.
8466 */
8467 wait_event(mddev->recovery_wait,
8468 !atomic_read(&mddev->recovery_active));
8469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008470 }
8471 }
NeilBrown9d487392016-11-02 14:16:49 +11008472 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8473 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8474 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008475 /*
8476 * this also signals 'finished resyncing' to md_stop
8477 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008478 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008479 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8480
NeilBrown5ed1df22015-07-24 13:27:08 +10008481 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8482 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008483 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008484 mddev->curr_resync_completed = mddev->curr_resync;
8485 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8486 }
NeilBrown09314792015-02-19 16:04:40 +11008487 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008488
NeilBrowndfc70642008-05-23 13:04:39 -07008489 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008490 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008491 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8492 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8493 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008494 pr_debug("md: checkpointing %s of %s.\n",
8495 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008496 if (test_bit(MD_RECOVERY_ERROR,
8497 &mddev->recovery))
8498 mddev->recovery_cp =
8499 mddev->curr_resync_completed;
8500 else
8501 mddev->recovery_cp =
8502 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008503 }
8504 } else
8505 mddev->recovery_cp = MaxSector;
8506 } else {
8507 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8508 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008509 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008510 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008511 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008512 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008513 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008514 !test_bit(Faulty, &rdev->flags) &&
8515 !test_bit(In_sync, &rdev->flags) &&
8516 rdev->recovery_offset < mddev->curr_resync)
8517 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008518 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008521 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008522 /* set CHANGE_PENDING here since maybe another update is needed,
8523 * so other nodes are informed. It should be harmless for normal
8524 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008525 set_mask_bits(&mddev->sb_flags, 0,
8526 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008527
NeilBrown23da4222014-12-15 12:57:01 +11008528 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008529 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8530 /* We completed so min/max setting can be forgotten if used. */
8531 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8532 mddev->resync_min = 0;
8533 mddev->resync_max = MaxSector;
8534 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8535 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008536 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008537 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008538 spin_unlock(&mddev->lock);
8539
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008541 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008542 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543}
NeilBrown29269552006-03-27 01:18:10 -08008544EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008545
NeilBrown746d3202013-04-24 11:42:41 +10008546static int remove_and_add_spares(struct mddev *mddev,
8547 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008548{
NeilBrown3cb03002011-10-11 16:45:26 +11008549 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008550 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008551 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008552 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008553
NeilBrownd787be42016-06-02 16:19:53 +10008554 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008555 if ((this == NULL || rdev == this) &&
8556 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008557 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008558 test_bit(Faulty, &rdev->flags) &&
8559 atomic_read(&rdev->nr_pending)==0) {
8560 /* Faulty non-Blocked devices with nr_pending == 0
8561 * never get nr_pending incremented,
8562 * never get Faulty cleared, and never get Blocked set.
8563 * So we can synchronize_rcu now rather than once per device
8564 */
8565 remove_some = true;
8566 set_bit(RemoveSynchronized, &rdev->flags);
8567 }
8568 }
8569
8570 if (remove_some)
8571 synchronize_rcu();
8572 rdev_for_each(rdev, mddev) {
8573 if ((this == NULL || rdev == this) &&
8574 rdev->raid_disk >= 0 &&
8575 !test_bit(Blocked, &rdev->flags) &&
8576 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008577 (!test_bit(In_sync, &rdev->flags) &&
8578 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008579 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008580 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008581 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008582 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008583 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008584 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008585 }
8586 }
NeilBrownd787be42016-06-02 16:19:53 +10008587 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8588 clear_bit(RemoveSynchronized, &rdev->flags);
8589 }
8590
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008591 if (removed && mddev->kobj.sd)
8592 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008593
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008594 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008595 goto no_add;
8596
NeilBrowndafb20f2012-03-19 12:46:39 +11008597 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008598 if (this && this != rdev)
8599 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008600 if (test_bit(Candidate, &rdev->flags))
8601 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008602 if (rdev->raid_disk >= 0 &&
8603 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008604 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008605 !test_bit(Faulty, &rdev->flags))
8606 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008607 if (rdev->raid_disk >= 0)
8608 continue;
8609 if (test_bit(Faulty, &rdev->flags))
8610 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008611 if (!test_bit(Journal, &rdev->flags)) {
8612 if (mddev->ro &&
8613 ! (rdev->saved_raid_disk >= 0 &&
8614 !test_bit(Bitmap_sync, &rdev->flags)))
8615 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008616
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008617 rdev->recovery_offset = 0;
8618 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008619 if (mddev->pers->
8620 hot_add_disk(mddev, rdev) == 0) {
8621 if (sysfs_link_rdev(mddev, rdev))
8622 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008623 if (!test_bit(Journal, &rdev->flags))
8624 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008625 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08008626 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008627 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008628 }
NeilBrown746d3202013-04-24 11:42:41 +10008629no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008630 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08008631 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008632 return spares;
8633}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008634
NeilBrownac05f252014-09-30 08:10:42 +10008635static void md_start_sync(struct work_struct *ws)
8636{
8637 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008638
NeilBrownac05f252014-09-30 08:10:42 +10008639 mddev->sync_thread = md_register_thread(md_do_sync,
8640 mddev,
8641 "resync");
8642 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11008643 pr_warn("%s: could not start resync thread...\n",
8644 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008645 /* leave the spares where they are, it shouldn't hurt */
8646 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8647 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8648 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8649 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8650 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008651 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008652 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8653 &mddev->recovery))
8654 if (mddev->sysfs_action)
8655 sysfs_notify_dirent_safe(mddev->sysfs_action);
8656 } else
8657 md_wakeup_thread(mddev->sync_thread);
8658 sysfs_notify_dirent_safe(mddev->sysfs_action);
8659 md_new_event(mddev);
8660}
8661
Linus Torvalds1da177e2005-04-16 15:20:36 -07008662/*
8663 * This routine is regularly called by all per-raid-array threads to
8664 * deal with generic issues like resync and super-block update.
8665 * Raid personalities that don't have a thread (linear/raid0) do not
8666 * need this as they never do any recovery or update the superblock.
8667 *
8668 * It does not do any resync itself, but rather "forks" off other threads
8669 * to do that as needed.
8670 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8671 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008672 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008673 * and wakeups up this thread which will reap the thread and finish up.
8674 * This thread also removes any faulty devices (with nr_pending == 0).
8675 *
8676 * The overall approach is:
8677 * 1/ if the superblock needs updating, update it.
8678 * 2/ If a recovery thread is running, don't do anything else.
8679 * 3/ If recovery has finished, clean up, possibly marking spares active.
8680 * 4/ If there are any faulty devices, remove them.
8681 * 5/ If array is degraded, try to add spares devices
8682 * 6/ If array has spares or is not in-sync, start a resync thread.
8683 */
NeilBrownfd01b882011-10-11 16:47:53 +11008684void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008685{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008686 if (mddev->suspended)
8687 return;
8688
NeilBrown5f404022005-06-21 17:17:16 -07008689 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008690 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008691
NeilBrownfca4d842005-06-21 17:17:11 -07008692 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008693 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11008694 pr_debug("md: %s in immediate safe mode\n",
8695 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07008696 mddev->safemode = 2;
8697 }
8698 flush_signals(current);
8699 }
8700
NeilBrownc89a8ee2008-08-05 15:54:13 +10008701 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8702 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008703 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08008704 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008705 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008706 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008707 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11008708 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07008709 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008710 ))
8711 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008712
NeilBrowndf5b89b2006-03-27 01:18:20 -08008713 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008714 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008715
Shaohua Liafc1f552017-08-11 20:34:45 -07008716 if (!mddev->external && mddev->safemode == 1)
NeilBrown33182d12017-08-08 16:56:36 +10008717 mddev->safemode = 0;
8718
NeilBrownc89a8ee2008-08-05 15:54:13 +10008719 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008720 struct md_rdev *rdev;
8721 if (!mddev->external && mddev->in_sync)
8722 /* 'Blocked' flag not needed as failed devices
8723 * will be recorded if array switched to read/write.
8724 * Leaving it set will prevent the device
8725 * from being removed.
8726 */
8727 rdev_for_each(rdev, mddev)
8728 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008729 /* On a read-only array we can:
8730 * - remove failed devices
8731 * - add already-in_sync devices if the array itself
8732 * is in-sync.
8733 * As we only add devices that are already in-sync,
8734 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008735 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008736 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008737 /* There is no thread, but we need to call
8738 * ->spare_active and clear saved_raid_disk
8739 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008740 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008741 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008742 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008743 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08008744 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008745 goto unlock;
8746 }
8747
Guoqing Jiang659b2542015-12-21 10:50:59 +11008748 if (mddev_is_clustered(mddev)) {
8749 struct md_rdev *rdev;
8750 /* kick the device if another node issued a
8751 * remove disk.
8752 */
8753 rdev_for_each(rdev, mddev) {
8754 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8755 rdev->raid_disk < 0)
8756 md_kick_rdev_from_array(rdev);
8757 }
8758 }
8759
NeilBrown6497709b2017-03-15 14:05:14 +11008760 if (!mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11008761 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11008762 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11008763 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07008764 }
NeilBrownfca4d842005-06-21 17:17:11 -07008765
Shaohua Li29530792016-12-08 15:48:19 -08008766 if (mddev->sb_flags)
NeilBrown850b2b42006-10-03 01:15:46 -07008767 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008768
Linus Torvalds1da177e2005-04-16 15:20:36 -07008769 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8770 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8771 /* resync/recovery still happening */
8772 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8773 goto unlock;
8774 }
8775 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008776 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008777 goto unlock;
8778 }
Neil Brown72a23c22008-06-28 08:31:41 +10008779 /* Set RUNNING before clearing NEEDED to avoid
8780 * any transients in the value of "sync_action".
8781 */
NeilBrown72f36d52012-10-11 14:25:57 +11008782 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008783 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008784 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008785 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008786 /* Clear some bits that don't mean anything, but
8787 * might be left set
8788 */
NeilBrown24dd4692005-11-08 21:39:26 -08008789 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8790 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008791
NeilBrowned209582012-04-24 10:23:14 +10008792 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8793 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008794 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008795 /* no recovery is running.
8796 * remove any failed drives, then
8797 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008798 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008799 * the personality to fail the re-add.
8800 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008801
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008802 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008803 if (mddev->pers->check_reshape == NULL ||
8804 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008805 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008806 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008807 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008808 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008809 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008810 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8811 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008812 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008813 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008814 } else if (mddev->recovery_cp < MaxSector) {
8815 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008816 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008817 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8818 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008819 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008820
Linus Torvalds1da177e2005-04-16 15:20:36 -07008821 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008822 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008823 /* We are adding a device or devices to an array
8824 * which has the bitmap stored on all devices.
8825 * So make sure all bitmap pages get written
8826 */
8827 bitmap_write_all(mddev->bitmap);
8828 }
NeilBrownac05f252014-09-30 08:10:42 +10008829 INIT_WORK(&mddev->del_work, md_start_sync);
8830 queue_work(md_misc_wq, &mddev->del_work);
8831 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008832 }
NeilBrownac05f252014-09-30 08:10:42 +10008833 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008834 if (!mddev->sync_thread) {
8835 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008836 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008837 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8838 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008839 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008840 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008841 }
NeilBrownac05f252014-09-30 08:10:42 +10008842 unlock:
8843 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008844 mddev_unlock(mddev);
NeilBrown35bfc522017-10-17 13:46:43 +11008845 } else if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
8846 /* Write superblock - thread that called mddev_suspend()
8847 * holds reconfig_mutex for us.
8848 */
8849 set_bit(MD_UPDATING_SB, &mddev->flags);
8850 smp_mb__after_atomic();
8851 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
8852 md_update_sb(mddev, 0);
8853 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
8854 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008855 }
8856}
NeilBrown6c144d32014-09-30 16:15:38 +10008857EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008859void md_reap_sync_thread(struct mddev *mddev)
8860{
8861 struct md_rdev *rdev;
8862
8863 /* resync has finished, collect result */
8864 md_unregister_thread(&mddev->sync_thread);
8865 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8866 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8867 /* success...*/
8868 /* activate any spares */
8869 if (mddev->pers->spare_active(mddev)) {
8870 sysfs_notify(&mddev->kobj, NULL,
8871 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08008872 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008873 }
8874 }
8875 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8876 mddev->pers->finish_reshape)
8877 mddev->pers->finish_reshape(mddev);
8878
8879 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008880 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008881 */
NeilBrownf4667222013-12-09 12:04:56 +11008882 if (!mddev->degraded)
8883 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008884 rdev->saved_raid_disk = -1;
8885
8886 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08008887 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008888 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8889 * clustered raid */
8890 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8891 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008892 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008893 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008894 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8895 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8896 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8897 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008898 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008899 /* flag recovery needed just to double check */
8900 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8901 sysfs_notify_dirent_safe(mddev->sysfs_action);
8902 md_new_event(mddev);
8903 if (mddev->event_work.func)
8904 queue_work(md_misc_wq, &mddev->event_work);
8905}
NeilBrown6c144d32014-09-30 16:15:38 +10008906EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008907
NeilBrownfd01b882011-10-11 16:47:53 +11008908void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008909{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008910 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008911 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008912 !test_bit(Blocked, &rdev->flags) &&
8913 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008914 msecs_to_jiffies(5000));
8915 rdev_dec_pending(rdev, mddev);
8916}
8917EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8918
NeilBrownc6563a82012-05-21 09:27:00 +10008919void md_finish_reshape(struct mddev *mddev)
8920{
8921 /* called be personality module when reshape completes. */
8922 struct md_rdev *rdev;
8923
8924 rdev_for_each(rdev, mddev) {
8925 if (rdev->data_offset > rdev->new_data_offset)
8926 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8927 else
8928 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8929 rdev->data_offset = rdev->new_data_offset;
8930 }
8931}
8932EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008933
Vishal Vermafc974ee2015-12-24 19:20:34 -07008934/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10008935
Vishal Vermafc974ee2015-12-24 19:20:34 -07008936/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11008937int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008938 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008939{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008940 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10008941 int rv;
8942 if (is_new)
8943 s += rdev->new_data_offset;
8944 else
8945 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008946 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8947 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008948 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008949 if (test_bit(ExternalBbl, &rdev->flags))
8950 sysfs_notify(&rdev->kobj, NULL,
8951 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008952 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08008953 set_mask_bits(&mddev->sb_flags, 0,
8954 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10008955 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07008956 return 1;
8957 } else
8958 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008959}
8960EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8961
NeilBrownc6563a82012-05-21 09:27:00 +10008962int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8963 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008964{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008965 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10008966 if (is_new)
8967 s += rdev->new_data_offset;
8968 else
8969 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008970 rv = badblocks_clear(&rdev->badblocks, s, sectors);
8971 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
8972 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
8973 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008974}
8975EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8976
Adrian Bunk75c96f82005-05-05 16:16:09 -07008977static int md_notify_reboot(struct notifier_block *this,
8978 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008979{
8980 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008981 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008982 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008983
NeilBrownc744a652012-03-19 12:46:37 +11008984 for_each_mddev(mddev, tmp) {
8985 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008986 if (mddev->pers)
8987 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008988 if (mddev->persistent)
8989 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008990 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008991 }
NeilBrownc744a652012-03-19 12:46:37 +11008992 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008993 }
NeilBrownc744a652012-03-19 12:46:37 +11008994 /*
8995 * certain more exotic SCSI devices are known to be
8996 * volatile wrt too early system reboots. While the
8997 * right place to handle this issue is the given
8998 * driver, we do want to have a safe RAID driver ...
8999 */
9000 if (need_delay)
9001 mdelay(1000*1);
9002
Linus Torvalds1da177e2005-04-16 15:20:36 -07009003 return NOTIFY_DONE;
9004}
9005
Adrian Bunk75c96f82005-05-05 16:16:09 -07009006static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009007 .notifier_call = md_notify_reboot,
9008 .next = NULL,
9009 .priority = INT_MAX, /* before any real devices */
9010};
9011
9012static void md_geninit(void)
9013{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009014 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009015
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07009016 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009017}
9018
Adrian Bunk75c96f82005-05-05 16:16:09 -07009019static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009020{
Tejun Heoe804ac72010-10-15 15:36:08 +02009021 int ret = -ENOMEM;
9022
Tejun Heoada609e2011-01-25 14:35:54 +01009023 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009024 if (!md_wq)
9025 goto err_wq;
9026
9027 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9028 if (!md_misc_wq)
9029 goto err_misc_wq;
9030
9031 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9032 goto err_md;
9033
9034 if ((ret = register_blkdev(0, "mdp")) < 0)
9035 goto err_mdp;
9036 mdp_major = ret;
9037
NeilBrownaf5628f2014-07-31 13:54:54 +10009038 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009039 md_probe, NULL, NULL);
9040 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009041 md_probe, NULL, NULL);
9042
Linus Torvalds1da177e2005-04-16 15:20:36 -07009043 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009044 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009045
9046 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009048
Tejun Heoe804ac72010-10-15 15:36:08 +02009049err_mdp:
9050 unregister_blkdev(MD_MAJOR, "md");
9051err_md:
9052 destroy_workqueue(md_misc_wq);
9053err_misc_wq:
9054 destroy_workqueue(md_wq);
9055err_wq:
9056 return ret;
9057}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009058
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009059static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009060{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009061 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9062 struct md_rdev *rdev2;
9063 int role, ret;
9064 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009065
Guoqing Jiang818da592017-03-01 16:42:40 +08009066 /*
9067 * If size is changed in another node then we need to
9068 * do resize as well.
9069 */
9070 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9071 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9072 if (ret)
9073 pr_info("md-cluster: resize failed\n");
9074 else
9075 bitmap_update_sb(mddev->bitmap);
9076 }
9077
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009078 /* Check for change of roles in the active devices */
9079 rdev_for_each(rdev2, mddev) {
9080 if (test_bit(Faulty, &rdev2->flags))
9081 continue;
9082
9083 /* Check if the roles changed */
9084 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009085
9086 if (test_bit(Candidate, &rdev2->flags)) {
9087 if (role == 0xfffe) {
9088 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9089 md_kick_rdev_from_array(rdev2);
9090 continue;
9091 }
9092 else
9093 clear_bit(Candidate, &rdev2->flags);
9094 }
9095
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009096 if (role != rdev2->raid_disk) {
9097 /* got activated */
9098 if (rdev2->raid_disk == -1 && role != 0xffff) {
9099 rdev2->saved_raid_disk = role;
9100 ret = remove_and_add_spares(mddev, rdev2);
9101 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009102 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009103 /* wakeup mddev->thread here, so array could
9104 * perform resync with the new activated disk */
9105 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9106 md_wakeup_thread(mddev->thread);
9107
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009108 }
9109 /* device faulty
9110 * We just want to do the minimum to mark the disk
9111 * as faulty. The recovery is performed by the
9112 * one who initiated the error.
9113 */
9114 if ((role == 0xfffe) || (role == 0xfffd)) {
9115 md_error(mddev, rdev2);
9116 clear_bit(Blocked, &rdev2->flags);
9117 }
9118 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009119 }
9120
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009121 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9122 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009123
9124 /* Finally set the event to be up to date */
9125 mddev->events = le64_to_cpu(sb->events);
9126}
9127
9128static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9129{
9130 int err;
9131 struct page *swapout = rdev->sb_page;
9132 struct mdp_superblock_1 *sb;
9133
9134 /* Store the sb page of the rdev in the swapout temporary
9135 * variable in case we err in the future
9136 */
9137 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009138 err = alloc_disk_sb(rdev);
9139 if (err == 0) {
9140 ClearPageUptodate(rdev->sb_page);
9141 rdev->sb_loaded = 0;
9142 err = super_types[mddev->major_version].
9143 load_super(rdev, NULL, mddev->minor_version);
9144 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009145 if (err < 0) {
9146 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9147 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009148 if (rdev->sb_page)
9149 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009150 rdev->sb_page = swapout;
9151 rdev->sb_loaded = 1;
9152 return err;
9153 }
9154
9155 sb = page_address(rdev->sb_page);
9156 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9157 * is not set
9158 */
9159
9160 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9161 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9162
9163 /* The other node finished recovery, call spare_active to set
9164 * device In_sync and mddev->degraded
9165 */
9166 if (rdev->recovery_offset == MaxSector &&
9167 !test_bit(In_sync, &rdev->flags) &&
9168 mddev->pers->spare_active(mddev))
9169 sysfs_notify(&mddev->kobj, NULL, "degraded");
9170
9171 put_page(swapout);
9172 return 0;
9173}
9174
9175void md_reload_sb(struct mddev *mddev, int nr)
9176{
9177 struct md_rdev *rdev;
9178 int err;
9179
9180 /* Find the rdev */
9181 rdev_for_each_rcu(rdev, mddev) {
9182 if (rdev->desc_nr == nr)
9183 break;
9184 }
9185
9186 if (!rdev || rdev->desc_nr != nr) {
9187 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9188 return;
9189 }
9190
9191 err = read_rdev(mddev, rdev);
9192 if (err < 0)
9193 return;
9194
9195 check_sb_changes(mddev, rdev);
9196
9197 /* Read all rdev's to update recovery_offset */
9198 rdev_for_each_rcu(rdev, mddev)
9199 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009200}
9201EXPORT_SYMBOL(md_reload_sb);
9202
Linus Torvalds1da177e2005-04-16 15:20:36 -07009203#ifndef MODULE
9204
9205/*
9206 * Searches all registered partitions for autorun RAID arrays
9207 * at boot time.
9208 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009209
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009210static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009211static LIST_HEAD(all_detected_devices);
9212struct detected_devices_node {
9213 struct list_head list;
9214 dev_t dev;
9215};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009216
9217void md_autodetect_dev(dev_t dev)
9218{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009219 struct detected_devices_node *node_detected_dev;
9220
9221 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9222 if (node_detected_dev) {
9223 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009224 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009225 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009226 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009228}
9229
Linus Torvalds1da177e2005-04-16 15:20:36 -07009230static void autostart_arrays(int part)
9231{
NeilBrown3cb03002011-10-11 16:45:26 +11009232 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009233 struct detected_devices_node *node_detected_dev;
9234 dev_t dev;
9235 int i_scanned, i_passed;
9236
9237 i_scanned = 0;
9238 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009239
NeilBrown9d487392016-11-02 14:16:49 +11009240 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009241
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009242 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009243 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9244 i_scanned++;
9245 node_detected_dev = list_entry(all_detected_devices.next,
9246 struct detected_devices_node, list);
9247 list_del(&node_detected_dev->list);
9248 dev = node_detected_dev->dev;
9249 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009250 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009251 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009252 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009253 if (IS_ERR(rdev))
9254 continue;
9255
NeilBrown403df472014-09-30 15:52:29 +10009256 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009257 continue;
NeilBrown403df472014-09-30 15:52:29 +10009258
NeilBrownd0fae182008-03-04 14:29:31 -08009259 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009260 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009261 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009262 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009263 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009264
NeilBrown9d487392016-11-02 14:16:49 +11009265 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009266
9267 autorun_devices(part);
9268}
9269
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009270#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009271
9272static __exit void md_exit(void)
9273{
NeilBrownfd01b882011-10-11 16:47:53 +11009274 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009275 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009276 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009277
NeilBrownaf5628f2014-07-31 13:54:54 +10009278 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009279 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009280
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009281 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009282 unregister_blkdev(mdp_major, "mdp");
9283 unregister_reboot_notifier(&md_notifier);
9284 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009285
9286 /* We cannot unload the modules while some process is
9287 * waiting for us in select() or poll() - wake them up
9288 */
9289 md_unloading = 1;
9290 while (waitqueue_active(&md_event_waiters)) {
9291 /* not safe to leave yet */
9292 wake_up(&md_event_waiters);
9293 msleep(delay);
9294 delay += delay;
9295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009296 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009297
NeilBrown29ac4aa2008-02-06 01:39:58 -08009298 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009299 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009300 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009301 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009302 /*
9303 * for_each_mddev() will call mddev_put() at the end of each
9304 * iteration. As the mddev is now fully clear, this will
9305 * schedule the mddev for destruction by a workqueue, and the
9306 * destroy_workqueue() below will wait for that to complete.
9307 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009308 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009309 destroy_workqueue(md_misc_wq);
9310 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009311}
9312
Dan Williams685784a2007-07-09 11:56:42 -07009313subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009314module_exit(md_exit)
9315
NeilBrownf91de922005-11-08 21:39:36 -08009316static int get_ro(char *buffer, struct kernel_param *kp)
9317{
9318 return sprintf(buffer, "%d", start_readonly);
9319}
9320static int set_ro(const char *val, struct kernel_param *kp)
9321{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009322 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009323}
9324
NeilBrown80ca3a42006-07-10 04:44:18 -07009325module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9326module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009327module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009328module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009329
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009331MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009332MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009333MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);