blob: 04f4f131f9d6d4410b4c63dec1a4ae1f009995c0 [file] [log] [blame]
Thomas Gleixneraf1a8892019-05-20 19:08:12 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 md.c : Multiple Devices driver for Linux
NeilBrownf72ffdd2014-09-30 14:23:59 +10004 Copyright (C) 1998, 1999, 2000 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
6 completely rewritten, based on the MD driver code from Marc Zyngier
7
8 Changes:
9
10 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
11 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
12 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
13 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
14 - kmod support by: Cyrus Durgin
15 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
16 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17
18 - lots of fixes and improvements to the RAID1/RAID5 and generic
19 RAID code (such as request based resynchronization):
20
21 Neil Brown <neilb@cse.unsw.edu.au>.
22
NeilBrown32a76272005-06-21 17:17:14 -070023 - persistent bitmap code
24 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25
NeilBrown9d487392016-11-02 14:16:49 +110026
27 Errors, Warnings, etc.
28 Please use:
29 pr_crit() for error conditions that risk data loss
30 pr_err() for error conditions that are unexpected, like an IO error
31 or internal inconsistency
32 pr_warn() for error conditions that could have been predicated, like
33 adding a device to an array when it has incompatible metadata
34 pr_info() for every interesting, very rare events, like an array starting
35 or stopping, or resync starting or stopping
36 pr_debug() for everything else.
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038*/
39
Ingo Molnar3f07c012017-02-08 18:51:30 +010040#include <linux/sched/signal.h>
NeilBrowna6fb0932005-09-09 16:23:56 -070041#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110042#include <linux/blkdev.h>
Vishal Vermafc974ee2015-12-24 19:20:34 -070043#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110045#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040046#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080047#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070048#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080049#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110050#include <linux/hdreg.h>
51#include <linux/proc_fs.h>
52#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040053#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110054#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070055#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110056#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110057#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110058#include <linux/raid/md_p.h>
59#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090060#include <linux/slab.h>
NeilBrown4ad23a972017-03-15 14:05:14 +110061#include <linux/percpu-refcount.h>
62
Shaohua Li504634f2016-11-18 09:44:08 -080063#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110064#include "md.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040065#include "md-bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050066#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110069static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#endif
71
NeilBrown01f96c02011-09-21 15:30:20 +100072/* pers_list is a list of registered personalities protected
73 * by pers_lock.
74 * pers_lock does extra service to protect accesses to
75 * mddev->thread when the mutex cannot be held.
76 */
NeilBrown2604b702006-01-06 00:20:36 -080077static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static DEFINE_SPINLOCK(pers_lock);
79
Kent Overstreet28dec872018-06-07 20:52:54 -040080static struct kobj_type md_ktype;
81
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050082struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050083EXPORT_SYMBOL(md_cluster_ops);
Christoph Hellwig2b598ee2019-04-04 18:56:14 +020084static struct module *md_cluster_mod;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050085
Bernd Schubert90b08712008-05-23 13:04:38 -070086static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020087static struct workqueue_struct *md_wq;
88static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070089
NeilBrown746d3202013-04-24 11:42:41 +100090static int remove_and_add_spares(struct mddev *mddev,
91 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110092static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
Robert Becker1e509152009-12-14 12:49:58 +110095 * Default number of read corrections we'll attempt on an rdev
96 * before ejecting it from the array. We divide the read error
97 * count by 2 for every hour elapsed between read errors.
98 */
99#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
100/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
102 * is 1000 KB/sec, so the extra system load does not show up that much.
103 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -0700104 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * subsystem is idle. There is also an 'absolute maximum' reconstruction
106 * speed limit - in case reconstruction slows down your system despite
107 * idle IO detection.
108 *
109 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800110 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
112
113static int sysctl_speed_limit_min = 1000;
114static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100115static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800116{
117 return mddev->sync_speed_min ?
118 mddev->sync_speed_min : sysctl_speed_limit_min;
119}
120
NeilBrownfd01b882011-10-11 16:47:53 +1100121static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800122{
123 return mddev->sync_speed_max ?
124 mddev->sync_speed_max : sysctl_speed_limit_max;
125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127static struct ctl_table_header *raid_table_header;
128
Joe Perches82592c32013-11-14 15:16:18 +1100129static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .procname = "speed_limit_min",
132 .data = &sysctl_speed_limit_min,
133 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700134 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800135 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 },
137 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .procname = "speed_limit_max",
139 .data = &sysctl_speed_limit_max,
140 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700141 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800142 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800144 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
Joe Perches82592c32013-11-14 15:16:18 +1100147static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 .procname = "raid",
150 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700151 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 .child = raid_table,
153 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800154 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
Joe Perches82592c32013-11-14 15:16:18 +1100157static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 .procname = "dev",
160 .maxlen = 0,
161 .mode = 0555,
162 .child = raid_dir_table,
163 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800164 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700167static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
NeilBrownf91de922005-11-08 21:39:36 -0800169static int start_readonly;
170
NeilBrown78b63502017-04-12 16:26:13 +1000171/*
172 * The original mechanism for creating an md device is to create
173 * a device node in /dev and to open it. This causes races with device-close.
174 * The preferred method is to write to the "new_array" module parameter.
175 * This can avoid races.
176 * Setting create_on_open to false disables the original mechanism
177 * so all the races disappear.
178 */
179static bool create_on_open = true;
180
NeilBrowna167f662010-10-26 18:31:13 +1100181struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100182 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100183{
Kent Overstreetafeee512018-05-20 18:25:52 -0400184 if (!mddev || !bioset_initialized(&mddev->bio_set))
NeilBrowna167f662010-10-26 18:31:13 +1100185 return bio_alloc(gfp_mask, nr_iovecs);
186
Marcos Paulo de Souza62516912019-01-14 06:31:56 -0700187 return bio_alloc_bioset(gfp_mask, nr_iovecs, &mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100188}
189EXPORT_SYMBOL_GPL(bio_alloc_mddev);
190
NeilBrown5a850712017-06-21 09:12:21 +1000191static struct bio *md_bio_alloc_sync(struct mddev *mddev)
192{
Kent Overstreetafeee512018-05-20 18:25:52 -0400193 if (!mddev || !bioset_initialized(&mddev->sync_set))
NeilBrown5a850712017-06-21 09:12:21 +1000194 return bio_alloc(GFP_NOIO, 1);
195
Kent Overstreetafeee512018-05-20 18:25:52 -0400196 return bio_alloc_bioset(GFP_NOIO, 1, &mddev->sync_set);
NeilBrown5a850712017-06-21 09:12:21 +1000197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
NeilBrownd7603b72006-01-06 00:20:30 -0800200 * We have a system wide 'event count' that is incremented
201 * on any 'interesting' event, and readers of /proc/mdstat
202 * can use 'poll' or 'select' to find out when the event
203 * count increases.
204 *
205 * Events are:
206 * start array, stop array, error, add device, remove device,
207 * start build, activate spare
208 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800209static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800210static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100211void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800212{
213 atomic_inc(&md_event_count);
214 wake_up(&md_event_waiters);
215}
NeilBrown29269552006-03-27 01:18:10 -0800216EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800217
218/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * Enables to iterate over all existing md arrays
220 * all_mddevs_lock protects this list.
221 */
222static LIST_HEAD(all_mddevs);
223static DEFINE_SPINLOCK(all_mddevs_lock);
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/*
226 * iterates through all used mddevs in the system.
227 * We take care to grab the all_mddevs_lock whenever navigating
228 * the list, and to always hold a refcount when unlocked.
229 * Any code which breaks out of this loop while own
230 * a reference to the current mddev and must mddev_put it.
231 */
NeilBrownfd01b882011-10-11 16:47:53 +1100232#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000234 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100235 _tmp = all_mddevs.next; \
236 _mddev = NULL;}); \
237 ({ if (_tmp != &all_mddevs) \
238 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100240 if (_mddev) mddev_put(_mddev); \
241 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
242 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100244 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 )
246
NeilBrown409c57f2009-03-31 14:39:39 +1100247/* Rather than calling directly into the personality make_request function,
248 * IO requests come here first so that we can check if the device is
249 * being suspended pending a reconfiguration.
250 * We hold a refcount over the call to ->make_request. By the time that
251 * call has finished, the bio has been linked into some internal structure
252 * and so is visible to ->quiesce(), so we don't need the refcount any more.
253 */
NeilBrownb3143b92017-10-17 13:46:43 +1100254static bool is_suspended(struct mddev *mddev, struct bio *bio)
255{
256 if (mddev->suspended)
257 return true;
258 if (bio_data_dir(bio) != WRITE)
259 return false;
260 if (mddev->suspend_lo >= mddev->suspend_hi)
261 return false;
262 if (bio->bi_iter.bi_sector >= mddev->suspend_hi)
263 return false;
264 if (bio_end_sector(bio) < mddev->suspend_lo)
265 return false;
266 return true;
267}
268
Shaohua Li393debc2017-09-21 10:23:35 -0700269void md_handle_request(struct mddev *mddev, struct bio *bio)
270{
271check_suspended:
272 rcu_read_lock();
NeilBrownb3143b92017-10-17 13:46:43 +1100273 if (is_suspended(mddev, bio)) {
Shaohua Li393debc2017-09-21 10:23:35 -0700274 DEFINE_WAIT(__wait);
275 for (;;) {
276 prepare_to_wait(&mddev->sb_wait, &__wait,
277 TASK_UNINTERRUPTIBLE);
NeilBrownb3143b92017-10-17 13:46:43 +1100278 if (!is_suspended(mddev, bio))
Shaohua Li393debc2017-09-21 10:23:35 -0700279 break;
280 rcu_read_unlock();
281 schedule();
282 rcu_read_lock();
283 }
284 finish_wait(&mddev->sb_wait, &__wait);
285 }
286 atomic_inc(&mddev->active_io);
287 rcu_read_unlock();
288
289 if (!mddev->pers->make_request(mddev, bio)) {
290 atomic_dec(&mddev->active_io);
291 wake_up(&mddev->sb_wait);
292 goto check_suspended;
293 }
294
295 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
296 wake_up(&mddev->sb_wait);
297}
298EXPORT_SYMBOL(md_handle_request);
299
Jens Axboedece1632015-11-05 10:41:16 -0700300static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
NeilBrown49077322010-03-25 16:20:56 +1100302 const int rw = bio_data_dir(bio);
Michael Callahanddcf35d2018-07-18 04:47:39 -0700303 const int sgrp = op_stat_group(bio_op(bio));
NeilBrownfd01b882011-10-11 16:47:53 +1100304 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500305 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100306
NeilBrownaf67c312017-06-18 14:38:57 +1000307 blk_queue_split(q, &bio);
Kent Overstreet54efd502015-04-23 22:37:18 -0700308
NeilBrown274d8cb2016-01-04 16:16:58 +1100309 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100310 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700311 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100312 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100313 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200314 if (bio_sectors(bio) != 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200315 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200316 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700317 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100318 }
NeilBrown49077322010-03-25 16:20:56 +1100319
Chris Masone91ece52011-02-07 19:21:48 -0500320 /*
321 * save the sectors now since our bio can
322 * go away inside make_request
323 */
324 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700325 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600326 bio->bi_opf &= ~REQ_NOMERGE;
Shaohua Li393debc2017-09-21 10:23:35 -0700327
328 md_handle_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100329
Mike Snitzer112f1582018-12-06 11:41:18 -0500330 part_stat_lock();
331 part_stat_inc(&mddev->gendisk->part0, ios[sgrp]);
332 part_stat_add(&mddev->gendisk->part0, sectors[sgrp], sectors);
Gu Zheng74672d02015-04-03 08:44:47 +0800333 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100334
Jens Axboedece1632015-11-05 10:41:16 -0700335 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
NeilBrown9e35b992010-04-06 14:23:02 +1000338/* mddev_suspend makes sure no new requests are submitted
339 * to the device, and that any requests that have been submitted
340 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100341 * Once mddev_detach() is called and completes, the module will be
342 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000343 */
NeilBrownfd01b882011-10-11 16:47:53 +1100344void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100345{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200346 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
NeilBrown4d5324f2017-10-19 12:17:16 +1100347 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100348 if (mddev->suspended++)
349 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100350 synchronize_rcu();
NeilBrowncc27b0c2017-06-05 16:49:39 +1000351 wake_up(&mddev->sb_wait);
NeilBrown35bfc522017-10-17 13:46:43 +1100352 set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
353 smp_mb__after_atomic();
NeilBrown409c57f2009-03-31 14:39:39 +1100354 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
355 mddev->pers->quiesce(mddev, 1);
NeilBrown35bfc522017-10-17 13:46:43 +1100356 clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
357 wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500358
359 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100360}
NeilBrown390ee602010-06-01 19:37:27 +1000361EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100362
NeilBrownfd01b882011-10-11 16:47:53 +1100363void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100364{
NeilBrown4d5324f2017-10-19 12:17:16 +1100365 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100366 if (--mddev->suspended)
367 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100368 wake_up(&mddev->sb_wait);
369 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500370
Jonathan Brassow47525e52012-05-22 13:55:29 +1000371 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500372 md_wakeup_thread(mddev->thread);
373 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100374}
NeilBrown390ee602010-06-01 19:37:27 +1000375EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100376
NeilBrownfd01b882011-10-11 16:47:53 +1100377int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000378{
NeilBrown5c675f82014-12-15 12:56:56 +1100379 struct md_personality *pers = mddev->pers;
380 int ret = 0;
381
382 rcu_read_lock();
383 if (mddev->suspended)
384 ret = 1;
385 else if (pers && pers->congested)
386 ret = pers->congested(mddev, bits);
387 rcu_read_unlock();
388 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000389}
NeilBrown5c675f82014-12-15 12:56:56 +1100390EXPORT_SYMBOL_GPL(mddev_congested);
391static int md_congested(void *data, int bits)
392{
393 struct mddev *mddev = data;
394 return mddev_congested(mddev, bits);
395}
NeilBrown3fa841d2009-09-23 18:10:29 +1000396
NeilBrowna2826aa2009-12-14 12:49:49 +1100397/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200398 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100399 */
NeilBrown4bc034d2019-03-29 10:46:16 -0700400
401static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100402{
NeilBrown4bc034d2019-03-29 10:46:16 -0700403 struct md_rdev *rdev = bio->bi_private;
404 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100405
406 rdev_dec_pending(rdev, mddev);
407
NeilBrown4bc034d2019-03-29 10:46:16 -0700408 if (atomic_dec_and_test(&mddev->flush_pending)) {
409 /* The pre-request flush has finished */
410 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100411 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700412 bio_put(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100413}
414
NeilBrown4bc034d2019-03-29 10:46:16 -0700415static void md_submit_flush_data(struct work_struct *ws);
416
417static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100418{
NeilBrown4bc034d2019-03-29 10:46:16 -0700419 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100420 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100421
NeilBrown2bc13b82019-03-29 10:46:17 -0700422 mddev->start_flush = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700423 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
424 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100425 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100426 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100427 if (rdev->raid_disk >= 0 &&
428 !test_bit(Faulty, &rdev->flags)) {
429 /* Take two references, one is dropped
430 * when request finishes, one after
431 * we reclaim rcu_read_lock
432 */
433 struct bio *bi;
434 atomic_inc(&rdev->nr_pending);
435 atomic_inc(&rdev->nr_pending);
436 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000437 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Xiao Ni5a409b42018-05-21 11:49:54 +0800438 bi->bi_end_io = md_end_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700439 bi->bi_private = rdev;
440 bio_set_dev(bi, rdev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600441 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
NeilBrown4bc034d2019-03-29 10:46:16 -0700442 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500443 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100444 rcu_read_lock();
445 rdev_dec_pending(rdev, mddev);
446 }
447 rcu_read_unlock();
NeilBrown4bc034d2019-03-29 10:46:16 -0700448 if (atomic_dec_and_test(&mddev->flush_pending))
449 queue_work(md_wq, &mddev->flush_work);
450}
NeilBrowna2826aa2009-12-14 12:49:49 +1100451
NeilBrown4bc034d2019-03-29 10:46:16 -0700452static void md_submit_flush_data(struct work_struct *ws)
453{
454 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
455 struct bio *bio = mddev->flush_bio;
456
457 /*
458 * must reset flush_bio before calling into md_handle_request to avoid a
459 * deadlock, because other bios passed md_handle_request suspend check
460 * could wait for this and below md_handle_request could wait for those
461 * bios because of suspend check
462 */
NeilBrown2bc13b82019-03-29 10:46:17 -0700463 mddev->last_flush = mddev->start_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700464 mddev->flush_bio = NULL;
465 wake_up(&mddev->sb_wait);
466
467 if (bio->bi_iter.bi_size == 0) {
468 /* an empty barrier - all done */
469 bio_endio(bio);
470 } else {
471 bio->bi_opf &= ~REQ_PREFLUSH;
472 md_handle_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100473 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100474}
NeilBrown4bc034d2019-03-29 10:46:16 -0700475
476void md_flush_request(struct mddev *mddev, struct bio *bio)
477{
NeilBrown2bc13b82019-03-29 10:46:17 -0700478 ktime_t start = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700479 spin_lock_irq(&mddev->lock);
480 wait_event_lock_irq(mddev->sb_wait,
NeilBrown2bc13b82019-03-29 10:46:17 -0700481 !mddev->flush_bio ||
482 ktime_after(mddev->last_flush, start),
NeilBrown4bc034d2019-03-29 10:46:16 -0700483 mddev->lock);
NeilBrown2bc13b82019-03-29 10:46:17 -0700484 if (!ktime_after(mddev->last_flush, start)) {
485 WARN_ON(mddev->flush_bio);
486 mddev->flush_bio = bio;
487 bio = NULL;
488 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700489 spin_unlock_irq(&mddev->lock);
490
NeilBrown2bc13b82019-03-29 10:46:17 -0700491 if (!bio) {
492 INIT_WORK(&mddev->flush_work, submit_flushes);
493 queue_work(md_wq, &mddev->flush_work);
494 } else {
495 /* flush was performed for some other bio while we waited. */
496 if (bio->bi_iter.bi_size == 0)
497 /* an empty barrier - all done */
498 bio_endio(bio);
499 else {
500 bio->bi_opf &= ~REQ_PREFLUSH;
501 mddev->pers->make_request(mddev, bio);
502 }
503 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700504}
Tejun Heoe9c74692010-09-03 11:56:18 +0200505EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100506
NeilBrownfd01b882011-10-11 16:47:53 +1100507static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 atomic_inc(&mddev->active);
510 return mddev;
511}
512
Dan Williams5fd3a172009-03-04 00:57:25 -0700513static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100514
NeilBrownfd01b882011-10-11 16:47:53 +1100515static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
518 return;
NeilBrownd3374822009-01-09 08:31:10 +1100519 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100520 mddev->ctime == 0 && !mddev->hold_active) {
521 /* Array is not configured at all, and not held active,
522 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100523 list_del_init(&mddev->all_mddevs);
Kent Overstreet28dec872018-06-07 20:52:54 -0400524
525 /*
526 * Call queue_work inside the spinlock so that
527 * flush_workqueue() after mddev_find will succeed in waiting
528 * for the work to be done.
529 */
530 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
531 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100532 }
533 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Kees Cook8376d3c2017-10-16 17:01:48 -0700536static void md_safemode_timeout(struct timer_list *t);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400537
NeilBrownfd01b882011-10-11 16:47:53 +1100538void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100539{
Kent Overstreet28dec872018-06-07 20:52:54 -0400540 kobject_init(&mddev->kobj, &md_ktype);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100541 mutex_init(&mddev->open_mutex);
542 mutex_init(&mddev->reconfig_mutex);
543 mutex_init(&mddev->bitmap_info.mutex);
544 INIT_LIST_HEAD(&mddev->disks);
545 INIT_LIST_HEAD(&mddev->all_mddevs);
Kees Cook8376d3c2017-10-16 17:01:48 -0700546 timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100547 atomic_set(&mddev->active, 1);
548 atomic_set(&mddev->openers, 0);
549 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100550 spin_lock_init(&mddev->lock);
NeilBrown4bc034d2019-03-29 10:46:16 -0700551 atomic_set(&mddev->flush_pending, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100552 init_waitqueue_head(&mddev->sb_wait);
553 init_waitqueue_head(&mddev->recovery_wait);
554 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000555 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500556 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100557 mddev->resync_min = 0;
558 mddev->resync_max = MaxSector;
559 mddev->level = LEVEL_NONE;
560}
NeilBrown390ee602010-06-01 19:37:27 +1000561EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100562
NeilBrownf72ffdd2014-09-30 14:23:59 +1000563static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
NeilBrownfd01b882011-10-11 16:47:53 +1100565 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
NeilBrown8f5f02c2011-02-16 13:58:51 +1100567 if (unit && MAJOR(unit) != MD_MAJOR)
568 unit &= ~((1<<MdpMinorShift)-1);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 retry:
571 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
NeilBrownefeb53c2009-01-09 08:31:10 +1100573 if (unit) {
574 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
575 if (mddev->unit == unit) {
576 mddev_get(mddev);
577 spin_unlock(&all_mddevs_lock);
578 kfree(new);
579 return mddev;
580 }
581
582 if (new) {
583 list_add(&new->all_mddevs, &all_mddevs);
584 spin_unlock(&all_mddevs_lock);
585 new->hold_active = UNTIL_IOCTL;
586 return new;
587 }
588 } else if (new) {
589 /* find an unused unit number */
590 static int next_minor = 512;
591 int start = next_minor;
592 int is_free = 0;
593 int dev = 0;
594 while (!is_free) {
595 dev = MKDEV(MD_MAJOR, next_minor);
596 next_minor++;
597 if (next_minor > MINORMASK)
598 next_minor = 0;
599 if (next_minor == start) {
600 /* Oh dear, all in use. */
601 spin_unlock(&all_mddevs_lock);
602 kfree(new);
603 return NULL;
604 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000605
NeilBrownefeb53c2009-01-09 08:31:10 +1100606 is_free = 1;
607 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
608 if (mddev->unit == dev) {
609 is_free = 0;
610 break;
611 }
612 }
613 new->unit = dev;
614 new->md_minor = MINOR(dev);
615 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 list_add(&new->all_mddevs, &all_mddevs);
617 spin_unlock(&all_mddevs_lock);
618 return new;
619 }
620 spin_unlock(&all_mddevs_lock);
621
NeilBrown9ffae0c2006-01-06 00:20:32 -0800622 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!new)
624 return NULL;
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 new->unit = unit;
627 if (MAJOR(unit) == MD_MAJOR)
628 new->md_minor = MINOR(unit);
629 else
630 new->md_minor = MINOR(unit) >> MdpMinorShift;
631
NeilBrownfafd7fb2010-04-01 15:55:30 +1100632 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 goto retry;
635}
636
NeilBrownb6eb1272010-04-15 10:13:47 +1000637static struct attribute_group md_redundancy_group;
638
NeilBrown5c47daf2014-12-15 12:57:01 +1100639void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
NeilBrowna64c8762010-04-14 17:15:37 +1000641 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000642 /* These cannot be removed under reconfig_mutex as
643 * an access to the files will try to take reconfig_mutex
644 * while holding the file unremovable, which leads to
645 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000646 * So hold set sysfs_active while the remove in happeing,
647 * and anything else which might set ->to_remove or my
648 * otherwise change the sysfs namespace will fail with
649 * -EBUSY if sysfs_active is still set.
650 * We set sysfs_active under reconfig_mutex and elsewhere
651 * test it under the same mutex to ensure its correct value
652 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000653 */
NeilBrowna64c8762010-04-14 17:15:37 +1000654 struct attribute_group *to_remove = mddev->to_remove;
655 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000656 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000657 mutex_unlock(&mddev->reconfig_mutex);
658
NeilBrown00bcb4a2010-06-01 19:37:23 +1000659 if (mddev->kobj.sd) {
660 if (to_remove != &md_redundancy_group)
661 sysfs_remove_group(&mddev->kobj, to_remove);
662 if (mddev->pers == NULL ||
663 mddev->pers->sync_request == NULL) {
664 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
665 if (mddev->sysfs_action)
666 sysfs_put(mddev->sysfs_action);
667 mddev->sysfs_action = NULL;
668 }
NeilBrowna64c8762010-04-14 17:15:37 +1000669 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000670 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000671 } else
672 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Chris Dunlop751e67c2011-10-19 16:48:26 +1100674 /* As we've dropped the mutex we need a spinlock to
675 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000676 */
677 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700678 md_wakeup_thread(mddev->thread);
NeilBrown4d5324f2017-10-19 12:17:16 +1100679 wake_up(&mddev->sb_wait);
NeilBrown01f96c02011-09-21 15:30:20 +1000680 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
NeilBrown5c47daf2014-12-15 12:57:01 +1100682EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500684struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100685{
686 struct md_rdev *rdev;
687
688 rdev_for_each_rcu(rdev, mddev)
689 if (rdev->desc_nr == nr)
690 return rdev;
691
692 return NULL;
693}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500694EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100695
696static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
NeilBrown3cb03002011-10-11 16:45:26 +1100698 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
NeilBrowndafb20f2012-03-19 12:46:39 +1100700 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (rdev->bdev->bd_dev == dev)
702 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return NULL;
705}
706
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100707struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
NeilBrown1ca69c42012-10-11 13:37:33 +1100708{
709 struct md_rdev *rdev;
710
711 rdev_for_each_rcu(rdev, mddev)
712 if (rdev->bdev->bd_dev == dev)
713 return rdev;
714
715 return NULL;
716}
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100717EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100718
NeilBrown84fc4b52011-10-11 16:49:58 +1100719static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800720{
NeilBrown84fc4b52011-10-11 16:49:58 +1100721 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800722 list_for_each_entry(pers, &pers_list, list) {
723 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800724 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800725 if (strcmp(pers->name, clevel)==0)
726 return pers;
727 }
NeilBrown2604b702006-01-06 00:20:36 -0800728 return NULL;
729}
730
Andre Nollb73df2d2008-07-11 22:02:23 +1000731/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100732static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100734 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000735 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
NeilBrownf72ffdd2014-09-30 14:23:59 +1000738static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100741 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000742 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return 0;
744}
745
NeilBrown545c8792012-05-22 13:54:30 +1000746void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800749 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 rdev->sb_loaded = 0;
751 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000752 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100753 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
NeilBrown2699b672011-07-28 11:31:47 +1000755 if (rdev->bb_page) {
756 put_page(rdev->bb_page);
757 rdev->bb_page = NULL;
758 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800759 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
NeilBrown545c8792012-05-22 13:54:30 +1000761EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200763static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700764{
NeilBrown3cb03002011-10-11 16:45:26 +1100765 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100766 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700767
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200768 if (bio->bi_status) {
769 pr_err("md: super_written gets error=%d\n", bio->bi_status);
NeilBrowna9701a32005-11-08 21:39:34 -0800770 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100771 if (!test_bit(Faulty, &rdev->flags)
772 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800773 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100774 set_bit(LastDev, &rdev->flags);
775 }
776 } else
777 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700778
NeilBrowna9701a32005-11-08 21:39:34 -0800779 if (atomic_dec_and_test(&mddev->pending_writes))
780 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700781 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700782 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700783}
784
NeilBrownfd01b882011-10-11 16:47:53 +1100785void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700786 sector_t sector, int size, struct page *page)
787{
788 /* write first size bytes of page to sector of rdev
789 * Increment mddev->pending_writes before returning
790 * and decrement it on completion, waking up sb_wait
791 * if zero is reached.
792 * If an error occurred, call md_error
793 */
NeilBrown46533ff2016-11-18 16:16:11 +1100794 struct bio *bio;
795 int ff = 0;
796
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +0100797 if (!page)
798 return;
799
NeilBrown46533ff2016-11-18 16:16:11 +1100800 if (test_bit(Faulty, &rdev->flags))
801 return;
802
NeilBrown5a850712017-06-21 09:12:21 +1000803 bio = md_bio_alloc_sync(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700804
Shaohua Lied3b98c2016-03-29 14:00:19 -0700805 atomic_inc(&rdev->nr_pending);
806
Christoph Hellwig74d46992017-08-23 19:10:32 +0200807 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700808 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700809 bio_add_page(bio, page, size, 0);
810 bio->bi_private = rdev;
811 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100812
813 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
814 test_bit(FailFast, &rdev->flags) &&
815 !test_bit(LastDev, &rdev->flags))
816 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +0200817 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -0800818
NeilBrown7bfa19f2005-06-21 17:17:28 -0700819 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500820 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800821}
822
NeilBrown46533ff2016-11-18 16:16:11 +1100823int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800824{
Tejun Heoe9c74692010-09-03 11:56:18 +0200825 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000826 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -0800827 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +1100828 return -EAGAIN;
829 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700830}
831
NeilBrown3cb03002011-10-11 16:45:26 +1100832int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -0500833 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
NeilBrown5a850712017-06-21 09:12:21 +1000835 struct bio *bio = md_bio_alloc_sync(rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 int ret;
837
Christoph Hellwig74d46992017-08-23 19:10:32 +0200838 if (metadata_op && rdev->meta_bdev)
839 bio_set_dev(bio, rdev->meta_bdev);
840 else
841 bio_set_dev(bio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -0500842 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100843 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700844 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000845 else if (rdev->mddev->reshape_position != MaxSector &&
846 (rdev->mddev->reshape_backwards ==
847 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700848 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100849 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700850 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500852
853 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200855 ret = !bio->bi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 bio_put(bio);
857 return ret;
858}
NeilBrowna8745db2006-01-06 00:20:34 -0800859EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
NeilBrownf72ffdd2014-09-30 14:23:59 +1000861static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (rdev->sb_loaded)
866 return 0;
867
Mike Christie796a5cf2016-06-05 14:32:07 -0500868 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 goto fail;
870 rdev->sb_loaded = 1;
871 return 0;
872
873fail:
NeilBrown9d487392016-11-02 14:16:49 +1100874 pr_err("md: disabled device %s, could not read superblock.\n",
875 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return -EINVAL;
877}
878
Amir Goldsteine6fd2092017-05-04 16:26:20 +0300879static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000881 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000882 sb1->set_uuid1 == sb2->set_uuid1 &&
883 sb1->set_uuid2 == sb2->set_uuid2 &&
884 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Amir Goldsteine6fd2092017-05-04 16:26:20 +0300887static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 int ret;
890 mdp_super_t *tmp1, *tmp2;
891
892 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
893 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
894
895 if (!tmp1 || !tmp2) {
896 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 goto abort;
898 }
899
900 *tmp1 = *sb1;
901 *tmp2 = *sb2;
902
903 /*
904 * nr_disks is not constant
905 */
906 tmp1->nr_disks = 0;
907 tmp2->nr_disks = 0;
908
Andre Nollce0c8e02008-07-11 22:02:20 +1000909 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700911 kfree(tmp1);
912 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return ret;
914}
915
NeilBrown4d167f02007-05-09 02:35:37 -0700916static u32 md_csum_fold(u32 csum)
917{
918 csum = (csum & 0xffff) + (csum >> 16);
919 return (csum & 0xffff) + (csum >> 16);
920}
921
NeilBrownf72ffdd2014-09-30 14:23:59 +1000922static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
NeilBrown4d167f02007-05-09 02:35:37 -0700924 u64 newcsum = 0;
925 u32 *sb32 = (u32*)sb;
926 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 unsigned int disk_csum, csum;
928
929 disk_csum = sb->sb_csum;
930 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700931
932 for (i = 0; i < MD_SB_BYTES/4 ; i++)
933 newcsum += sb32[i];
934 csum = (newcsum & 0xffffffff) + (newcsum>>32);
935
NeilBrown4d167f02007-05-09 02:35:37 -0700936#ifdef CONFIG_ALPHA
937 /* This used to use csum_partial, which was wrong for several
938 * reasons including that different results are returned on
939 * different architectures. It isn't critical that we get exactly
940 * the same return value as before (we always csum_fold before
941 * testing, and that removes any differences). However as we
942 * know that csum_partial always returned a 16bit value on
943 * alphas, do a fold to maximise conformity to previous behaviour.
944 */
945 sb->sb_csum = md_csum_fold(disk_csum);
946#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700948#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return csum;
950}
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952/*
953 * Handle superblock details.
954 * We want to be able to handle multiple superblock formats
955 * so we have a common interface to them all, and an array of
956 * different handlers.
957 * We rely on user-space to write the initial superblock, and support
958 * reading and updating of superblocks.
959 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100960 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * loads and validates a superblock on dev.
962 * if refdev != NULL, compare superblocks on both devices
963 * Return:
964 * 0 - dev has a superblock that is compatible with refdev
965 * 1 - dev has a superblock that is compatible and newer than refdev
966 * so dev should be used as the refdev in future
967 * -EINVAL superblock incompatible or invalid
968 * -othererror e.g. -EIO
969 *
NeilBrownfd01b882011-10-11 16:47:53 +1100970 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 * Verify that dev is acceptable into mddev.
972 * The first time, mddev->raid_disks will be 0, and data from
973 * dev should be merged in. Subsequent calls check that dev
974 * is new enough. Return 0 or -EINVAL
975 *
NeilBrownfd01b882011-10-11 16:47:53 +1100976 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 * Update the superblock for rdev with data in mddev
978 * This does not write to disc.
979 *
980 */
981
982struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000983 char *name;
984 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000985 int (*load_super)(struct md_rdev *rdev,
986 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000987 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000988 int (*validate_super)(struct mddev *mddev,
989 struct md_rdev *rdev);
990 void (*sync_super)(struct mddev *mddev,
991 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100992 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000993 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000994 int (*allow_new_offset)(struct md_rdev *rdev,
995 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996};
997
998/*
Andre Noll0894cc32009-06-18 08:49:23 +1000999 * Check that the given mddev has no bitmap.
1000 *
1001 * This function is called from the run method of all personalities that do not
1002 * support bitmaps. It prints an error message and returns non-zero if mddev
1003 * has a bitmap. Otherwise, it returns 0.
1004 *
1005 */
NeilBrownfd01b882011-10-11 16:47:53 +11001006int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001007{
NeilBrownc3d97142009-12-14 12:49:52 +11001008 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001009 return 0;
NeilBrown9d487392016-11-02 14:16:49 +11001010 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +10001011 mdname(mddev), mddev->pers->name);
1012 return 1;
1013}
1014EXPORT_SYMBOL(md_check_no_bitmap);
1015
1016/*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001017 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 */
NeilBrown3cb03002011-10-11 16:45:26 +11001019static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1022 mdp_super_t *sb;
1023 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 /*
Andre Noll0f420352008-07-11 22:02:23 +10001026 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 * it's at the end of the disk.
1028 *
1029 * It also happens to be a multiple of 4Kb.
1030 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001031 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
NeilBrown0002b272005-09-09 16:23:53 -07001033 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +11001034 if (ret)
1035 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 ret = -EINVAL;
1038
1039 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001040 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +11001043 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 goto abort;
1045 }
1046
1047 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001048 sb->minor_version < 90 ||
1049 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +11001050 pr_warn("Bad version number %d.%d on %s\n",
1051 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 goto abort;
1053 }
1054
1055 if (sb->raid_disks <= 0)
1056 goto abort;
1057
NeilBrown4d167f02007-05-09 02:35:37 -07001058 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001059 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 goto abort;
1061 }
1062
1063 rdev->preferred_minor = sb->md_minor;
1064 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001065 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001066 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001067 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 if (sb->level == LEVEL_MULTIPATH)
1070 rdev->desc_nr = -1;
1071 else
1072 rdev->desc_nr = sb->this_disk.number;
1073
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001074 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001076 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001078 mdp_super_t *refsb = page_address(refdev->sb_page);
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001079 if (!md_uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001080 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 b, bdevname(refdev->bdev,b2));
1082 goto abort;
1083 }
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001084 if (!md_sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001085 pr_warn("md: %s has same UUID but different superblock to %s\n",
1086 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 goto abort;
1088 }
1089 ev1 = md_event(sb);
1090 ev2 = md_event(refsb);
1091 if (ev1 > ev2)
1092 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001093 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 ret = 0;
1095 }
NeilBrown8190e752009-06-18 08:48:58 +10001096 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001097 /* Limit to 4TB as metadata cannot record more than that.
1098 * (not needed for Linear and RAID0 as metadata doesn't
1099 * record this size)
1100 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001101 if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001102 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
NeilBrown27a7b262011-09-10 17:21:28 +10001104 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001105 /* "this cannot possibly happen" ... */
1106 ret = -EINVAL;
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 abort:
1109 return ret;
1110}
1111
1112/*
1113 * validate_super for 0.90.0
1114 */
NeilBrownfd01b882011-10-11 16:47:53 +11001115static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001118 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001119 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
NeilBrown41158c72005-06-21 17:17:25 -07001121 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001122 clear_bit(Faulty, &rdev->flags);
1123 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001124 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001125 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (mddev->raid_disks == 0) {
1128 mddev->major_version = 0;
1129 mddev->minor_version = sb->minor_version;
1130 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001131 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001132 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 mddev->ctime = sb->ctime;
1134 mddev->utime = sb->utime;
1135 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001136 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 mddev->layout = sb->layout;
1138 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001139 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001140 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001141 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001142 mddev->bitmap_info.space = 0;
1143 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001144 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001145 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001146 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
NeilBrownf6705572006-03-27 01:18:11 -08001148 if (mddev->minor_version >= 91) {
1149 mddev->reshape_position = sb->reshape_position;
1150 mddev->delta_disks = sb->delta_disks;
1151 mddev->new_level = sb->new_level;
1152 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001153 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001154 if (mddev->delta_disks < 0)
1155 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001156 } else {
1157 mddev->reshape_position = MaxSector;
1158 mddev->delta_disks = 0;
1159 mddev->new_level = mddev->level;
1160 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001161 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001162 }
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (sb->state & (1<<MD_SB_CLEAN))
1165 mddev->recovery_cp = MaxSector;
1166 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001167 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 sb->events_lo == sb->cp_events_lo) {
1169 mddev->recovery_cp = sb->recovery_cp;
1170 } else
1171 mddev->recovery_cp = 0;
1172 }
1173
1174 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1175 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1176 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1177 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1178
1179 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001180
1181 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001182 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001183 mddev->bitmap_info.offset =
1184 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001185 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001186 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001187 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001188
NeilBrown41158c72005-06-21 17:17:25 -07001189 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001190 /* Insist on good event counter while assembling, except
1191 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001193 if (sb->disks[rdev->desc_nr].state & (
1194 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001195 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001196 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001197 } else if (mddev->bitmap) {
1198 /* if adding to array with a bitmap, then we can accept an
1199 * older device ... but not too old.
1200 */
NeilBrown41158c72005-06-21 17:17:25 -07001201 if (ev1 < mddev->bitmap->events_cleared)
1202 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001203 if (ev1 < mddev->events)
1204 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001205 } else {
1206 if (ev1 < mddev->events)
1207 /* just a hot-add of a new device, leave raid_disk at -1 */
1208 return 0;
1209 }
NeilBrown41158c72005-06-21 17:17:25 -07001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 desc = sb->disks + rdev->desc_nr;
1213
1214 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001215 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001216 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1217 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001218 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001220 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001221 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1222 /* active but not in sync implies recovery up to
1223 * reshape position. We don't know exactly where
1224 * that is, so set to zero for now */
1225 if (mddev->minor_version >= 91) {
1226 rdev->recovery_offset = 0;
1227 rdev->raid_disk = desc->raid_disk;
1228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001230 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1231 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001232 if (desc->state & (1<<MD_DISK_FAILFAST))
1233 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001234 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001235 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return 0;
1237}
1238
1239/*
1240 * sync_super for 0.90.0
1241 */
NeilBrownfd01b882011-10-11 16:47:53 +11001242static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001245 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /* make rdev->sb match mddev data..
1249 *
1250 * 1/ zero out disks
1251 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1252 * 3/ any empty disks < next_spare become removed
1253 *
1254 * disks[0] gets initialised to REMOVED because
1255 * we cannot be sure from other fields if it has
1256 * been initialised or not.
1257 */
1258 int i;
1259 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1260
NeilBrown61181562005-09-09 16:24:02 -07001261 rdev->sb_size = MD_SB_BYTES;
1262
Namhyung Kim65a06f062011-07-27 11:00:36 +10001263 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 memset(sb, 0, sizeof(*sb));
1266
1267 sb->md_magic = MD_SB_MAGIC;
1268 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 sb->patch_version = mddev->patch_version;
1270 sb->gvalid_words = 0; /* ignored */
1271 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1272 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1273 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1274 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1275
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001276 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001278 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 sb->raid_disks = mddev->raid_disks;
1280 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001281 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001282 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 sb->state = 0;
1284 sb->events_hi = (mddev->events>>32);
1285 sb->events_lo = (u32)mddev->events;
1286
NeilBrownf6705572006-03-27 01:18:11 -08001287 if (mddev->reshape_position == MaxSector)
1288 sb->minor_version = 90;
1289 else {
1290 sb->minor_version = 91;
1291 sb->reshape_position = mddev->reshape_position;
1292 sb->new_level = mddev->new_level;
1293 sb->delta_disks = mddev->delta_disks;
1294 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001295 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001296 }
1297 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (mddev->in_sync)
1299 {
1300 sb->recovery_cp = mddev->recovery_cp;
1301 sb->cp_events_hi = (mddev->events>>32);
1302 sb->cp_events_lo = (u32)mddev->events;
1303 if (mddev->recovery_cp == MaxSector)
1304 sb->state = (1<< MD_SB_CLEAN);
1305 } else
1306 sb->recovery_cp = 0;
1307
1308 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001309 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
NeilBrownc3d97142009-12-14 12:49:52 +11001311 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001312 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001315 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001317 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001318 int is_active = test_bit(In_sync, &rdev2->flags);
1319
1320 if (rdev2->raid_disk >= 0 &&
1321 sb->minor_version >= 91)
1322 /* we have nowhere to store the recovery_offset,
1323 * but if it is not below the reshape_position,
1324 * we can piggy-back on that.
1325 */
1326 is_active = 1;
1327 if (rdev2->raid_disk < 0 ||
1328 test_bit(Faulty, &rdev2->flags))
1329 is_active = 0;
1330 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001331 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001333 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001334 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 d = &sb->disks[rdev2->desc_nr];
1336 nr_disks++;
1337 d->number = rdev2->desc_nr;
1338 d->major = MAJOR(rdev2->bdev->bd_dev);
1339 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001340 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 d->raid_disk = rdev2->raid_disk;
1342 else
1343 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001344 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001346 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001348 if (test_bit(In_sync, &rdev2->flags))
1349 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 active++;
1351 working++;
1352 } else {
1353 d->state = 0;
1354 spare++;
1355 working++;
1356 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001357 if (test_bit(WriteMostly, &rdev2->flags))
1358 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001359 if (test_bit(FailFast, &rdev2->flags))
1360 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /* now set the "removed" and "faulty" bits on any missing devices */
1363 for (i=0 ; i < mddev->raid_disks ; i++) {
1364 mdp_disk_t *d = &sb->disks[i];
1365 if (d->state == 0 && d->number == 0) {
1366 d->number = i;
1367 d->raid_disk = i;
1368 d->state = (1<<MD_DISK_REMOVED);
1369 d->state |= (1<<MD_DISK_FAULTY);
1370 failed++;
1371 }
1372 }
1373 sb->nr_disks = nr_disks;
1374 sb->active_disks = active;
1375 sb->working_disks = working;
1376 sb->failed_disks = failed;
1377 sb->spare_disks = spare;
1378
1379 sb->this_disk = sb->disks[rdev->desc_nr];
1380 sb->sb_csum = calc_sb_csum(sb);
1381}
1382
1383/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001384 * rdev_size_change for 0.90.0
1385 */
1386static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001387super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001388{
Andre Noll58c0fed2009-03-31 14:33:13 +11001389 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001390 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001391 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001392 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001393 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001394 if (!num_sectors || num_sectors > rdev->sb_start)
1395 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001396 /* Limit to 4TB as metadata cannot record more than that.
1397 * 4TB == 2^32 KB, or 2*2^32 sectors.
1398 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001399 if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001400 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001401 do {
1402 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001403 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001404 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001405 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001406}
1407
NeilBrownc6563a82012-05-21 09:27:00 +10001408static int
1409super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1410{
1411 /* non-zero offset changes not possible with v0.90 */
1412 return new_offset == 0;
1413}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001414
1415/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * version 1 superblock
1417 */
1418
NeilBrownf72ffdd2014-09-30 14:23:59 +10001419static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001421 __le32 disk_csum;
1422 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 unsigned long long newcsum;
1424 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001425 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 disk_csum = sb->sb_csum;
1428 sb->sb_csum = 0;
1429 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001430 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 newcsum += le32_to_cpu(*isuper++);
1432
1433 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001434 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1437 sb->sb_csum = disk_csum;
1438 return cpu_to_le32(csum);
1439}
1440
NeilBrown3cb03002011-10-11 16:45:26 +11001441static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 struct mdp_superblock_1 *sb;
1444 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001445 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001446 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001448 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 /*
Andre Noll0f420352008-07-11 22:02:23 +10001451 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 * It is always aligned to a 4K boundary and
1453 * depeding on minor_version, it can be:
1454 * 0: At least 8K, but less than 12K, from end of device
1455 * 1: At start of device
1456 * 2: 4K from start of device.
1457 */
1458 switch(minor_version) {
1459 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001460 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001461 sb_start -= 8*2;
1462 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 break;
1464 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001465 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 break;
1467 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001468 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 break;
1470 default:
1471 return -EINVAL;
1472 }
Andre Noll0f420352008-07-11 22:02:23 +10001473 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
NeilBrown0002b272005-09-09 16:23:53 -07001475 /* superblock is rarely larger than 1K, but it can be larger,
1476 * and it is safe to read 4k, so we do that
1477 */
1478 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (ret) return ret;
1480
Namhyung Kim65a06f062011-07-27 11:00:36 +10001481 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1484 sb->major_version != cpu_to_le32(1) ||
1485 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001486 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001487 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return -EINVAL;
1489
1490 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001491 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 bdevname(rdev->bdev,b));
1493 return -EINVAL;
1494 }
1495 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001496 pr_warn("md: data_size too small on %s\n",
1497 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return -EINVAL;
1499 }
NeilBrownc6563a82012-05-21 09:27:00 +10001500 if (sb->pad0 ||
1501 sb->pad3[0] ||
1502 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1503 /* Some padding is non-zero, might be a new feature */
1504 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 rdev->preferred_minor = 0xffff;
1507 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001508 rdev->new_data_offset = rdev->data_offset;
1509 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1510 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1511 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001512 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
NeilBrown0002b272005-09-09 16:23:53 -07001514 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001515 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001516 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001517 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1518
1519 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001520 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001521 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001522 if (minor_version
1523 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1524 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001525
NeilBrown31b65a02006-07-10 04:44:14 -07001526 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1527 rdev->desc_nr = -1;
1528 else
1529 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1530
NeilBrown2699b672011-07-28 11:31:47 +10001531 if (!rdev->bb_page) {
1532 rdev->bb_page = alloc_page(GFP_KERNEL);
1533 if (!rdev->bb_page)
1534 return -ENOMEM;
1535 }
1536 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1537 rdev->badblocks.count == 0) {
1538 /* need to load the bad block list.
1539 * Currently we limit it to one page.
1540 */
1541 s32 offset;
1542 sector_t bb_sector;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001543 __le64 *bbp;
NeilBrown2699b672011-07-28 11:31:47 +10001544 int i;
1545 int sectors = le16_to_cpu(sb->bblog_size);
1546 if (sectors > (PAGE_SIZE / 512))
1547 return -EINVAL;
1548 offset = le32_to_cpu(sb->bblog_offset);
1549 if (offset == 0)
1550 return -EINVAL;
1551 bb_sector = (long long)offset;
1552 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001553 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001554 return -EIO;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001555 bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10001556 rdev->badblocks.shift = sb->bblog_shift;
1557 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1558 u64 bb = le64_to_cpu(*bbp);
1559 int count = bb & (0x3ff);
1560 u64 sector = bb >> 10;
1561 sector <<= sb->bblog_shift;
1562 count <<= sb->bblog_shift;
1563 if (bb + 1 == 0)
1564 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001565 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001566 return -EINVAL;
1567 }
NeilBrown486adf72013-04-24 11:42:44 +10001568 } else if (sb->bblog_offset != 0)
1569 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001570
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001571 if ((le32_to_cpu(sb->feature_map) &
1572 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001573 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1574 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1575 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1576 }
1577
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001578 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001579 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001580 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001582 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1585 sb->level != refsb->level ||
1586 sb->layout != refsb->layout ||
1587 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001588 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 bdevname(rdev->bdev,b),
1590 bdevname(refdev->bdev,b2));
1591 return -EINVAL;
1592 }
1593 ev1 = le64_to_cpu(sb->events);
1594 ev2 = le64_to_cpu(refsb->events);
1595
1596 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001597 ret = 1;
1598 else
1599 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
NeilBrownc6563a82012-05-21 09:27:00 +10001601 if (minor_version) {
1602 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1603 sectors -= rdev->data_offset;
1604 } else
1605 sectors = rdev->sb_start;
1606 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001608 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001609 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
NeilBrownfd01b882011-10-11 16:47:53 +11001612static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001614 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001615 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
NeilBrown41158c72005-06-21 17:17:25 -07001617 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001618 clear_bit(Faulty, &rdev->flags);
1619 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001620 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001621 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (mddev->raid_disks == 0) {
1624 mddev->major_version = 1;
1625 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001626 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001627 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001628 mddev->ctime = le64_to_cpu(sb->ctime);
1629 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001631 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 mddev->layout = le32_to_cpu(sb->layout);
1633 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001634 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001635 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001636 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001637 mddev->bitmap_info.space = 0;
1638 /* Default location for bitmap is 1K after superblock
1639 * using 3K - total of 4K
1640 */
NeilBrownc3d97142009-12-14 12:49:52 +11001641 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001642 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001643 mddev->reshape_backwards = 0;
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1646 memcpy(mddev->uuid, sb->set_uuid, 16);
1647
1648 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001649
NeilBrown71c08052005-09-09 16:23:51 -07001650 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001651 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001652 mddev->bitmap_info.offset =
1653 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001654 /* Metadata doesn't record how much space is available.
1655 * For 1.0, we assume we can use up to the superblock
1656 * if before, else to 4K beyond superblock.
1657 * For others, assume no change is possible.
1658 */
1659 if (mddev->minor_version > 0)
1660 mddev->bitmap_info.space = 0;
1661 else if (mddev->bitmap_info.offset > 0)
1662 mddev->bitmap_info.space =
1663 8 - mddev->bitmap_info.offset;
1664 else
1665 mddev->bitmap_info.space =
1666 -mddev->bitmap_info.offset;
1667 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001668
NeilBrownf6705572006-03-27 01:18:11 -08001669 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1670 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1671 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1672 mddev->new_level = le32_to_cpu(sb->new_level);
1673 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001674 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001675 if (mddev->delta_disks < 0 ||
1676 (mddev->delta_disks == 0 &&
1677 (le32_to_cpu(sb->feature_map)
1678 & MD_FEATURE_RESHAPE_BACKWARDS)))
1679 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001680 } else {
1681 mddev->reshape_position = MaxSector;
1682 mddev->delta_disks = 0;
1683 mddev->new_level = mddev->level;
1684 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001685 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001686 }
1687
Song Liu486b0f72016-08-19 15:34:01 -07001688 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001689 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001690
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001691 if (le32_to_cpu(sb->feature_map) &
1692 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001693 if (le32_to_cpu(sb->feature_map) &
1694 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1695 return -EINVAL;
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001696 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1697 (le32_to_cpu(sb->feature_map) &
1698 MD_FEATURE_MULTIPLE_PPLS))
1699 return -EINVAL;
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001700 set_bit(MD_HAS_PPL, &mddev->flags);
1701 }
NeilBrown41158c72005-06-21 17:17:25 -07001702 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001703 /* Insist of good event counter while assembling, except for
1704 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001706 if (rdev->desc_nr >= 0 &&
1707 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001708 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1709 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001710 if (ev1 < mddev->events)
1711 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001712 } else if (mddev->bitmap) {
1713 /* If adding to array with a bitmap, then we can accept an
1714 * older device, but not too old.
1715 */
NeilBrown41158c72005-06-21 17:17:25 -07001716 if (ev1 < mddev->bitmap->events_cleared)
1717 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001718 if (ev1 < mddev->events)
1719 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001720 } else {
1721 if (ev1 < mddev->events)
1722 /* just a hot-add of a new device, leave raid_disk at -1 */
1723 return 0;
1724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (mddev->level != LEVEL_MULTIPATH) {
1726 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001727 if (rdev->desc_nr < 0 ||
1728 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001729 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001730 rdev->desc_nr = -1;
1731 } else
1732 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001734 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001736 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001737 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 break;
Song Liubac624f2015-08-13 14:31:55 -07001739 case MD_DISK_ROLE_JOURNAL: /* journal device */
1740 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1741 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001742 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001743 return -EINVAL;
1744 }
1745 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001746 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001747 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001748 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 default:
NeilBrownf4667222013-12-09 12:04:56 +11001750 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001751 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001752 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001753 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001754 if (!(le32_to_cpu(sb->feature_map) &
1755 MD_FEATURE_RECOVERY_BITMAP))
1756 rdev->saved_raid_disk = -1;
1757 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001758 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 rdev->raid_disk = role;
1760 break;
1761 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001762 if (sb->devflags & WriteMostly1)
1763 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001764 if (sb->devflags & FailFast1)
1765 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001766 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1767 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001768 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001769 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return 0;
1772}
1773
NeilBrownfd01b882011-10-11 16:47:53 +11001774static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001777 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 int max_dev, i;
1779 /* make rdev->sb match mddev and rdev data. */
1780
Namhyung Kim65a06f062011-07-27 11:00:36 +10001781 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 sb->feature_map = 0;
1784 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001785 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 memset(sb->pad3, 0, sizeof(sb->pad3));
1787
1788 sb->utime = cpu_to_le64((__u64)mddev->utime);
1789 sb->events = cpu_to_le64(mddev->events);
1790 if (mddev->in_sync)
1791 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001792 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1793 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 else
1795 sb->resync_offset = cpu_to_le64(0);
1796
NeilBrown1c05b4b2006-10-21 10:24:08 -07001797 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001798
NeilBrownf0ca3402006-02-02 14:28:04 -08001799 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001800 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001801 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001802 sb->level = cpu_to_le32(mddev->level);
1803 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11001804 if (test_bit(FailFast, &rdev->flags))
1805 sb->devflags |= FailFast1;
1806 else
1807 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08001808
NeilBrownaeb9b2112011-08-25 14:43:08 +10001809 if (test_bit(WriteMostly, &rdev->flags))
1810 sb->devflags |= WriteMostly1;
1811 else
1812 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001813 sb->data_offset = cpu_to_le64(rdev->data_offset);
1814 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001815
NeilBrownc3d97142009-12-14 12:49:52 +11001816 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1817 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001818 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001819 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001820
Shaohua Lif2076e72015-10-08 21:54:12 -07001821 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001822 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001823 sb->feature_map |=
1824 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1825 sb->recovery_offset =
1826 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001827 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1828 sb->feature_map |=
1829 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001830 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001831 /* Note: recovery_offset and journal_tail share space */
1832 if (test_bit(Journal, &rdev->flags))
1833 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001834 if (test_bit(Replacement, &rdev->flags))
1835 sb->feature_map |=
1836 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001837
NeilBrownf6705572006-03-27 01:18:11 -08001838 if (mddev->reshape_position != MaxSector) {
1839 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1840 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1841 sb->new_layout = cpu_to_le32(mddev->new_layout);
1842 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1843 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001844 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001845 if (mddev->delta_disks == 0 &&
1846 mddev->reshape_backwards)
1847 sb->feature_map
1848 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001849 if (rdev->new_data_offset != rdev->data_offset) {
1850 sb->feature_map
1851 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1852 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1853 - rdev->data_offset));
1854 }
NeilBrownf6705572006-03-27 01:18:11 -08001855 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001856
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001857 if (mddev_is_clustered(mddev))
1858 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1859
NeilBrown2699b672011-07-28 11:31:47 +10001860 if (rdev->badblocks.count == 0)
1861 /* Nothing to do for bad blocks*/ ;
1862 else if (sb->bblog_offset == 0)
1863 /* Cannot record bad blocks on this device */
1864 md_error(mddev, rdev);
1865 else {
1866 struct badblocks *bb = &rdev->badblocks;
Christoph Hellwigae506402019-04-04 18:56:13 +02001867 __le64 *bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10001868 u64 *p = bb->page;
1869 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1870 if (bb->changed) {
1871 unsigned seq;
1872
1873retry:
1874 seq = read_seqbegin(&bb->lock);
1875
1876 memset(bbp, 0xff, PAGE_SIZE);
1877
1878 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001879 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001880 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1881 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001882 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001883 }
NeilBrownd0962932012-03-19 12:46:41 +11001884 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001885 if (read_seqretry(&bb->lock, seq))
1886 goto retry;
1887
1888 bb->sector = (rdev->sb_start +
1889 (int)le32_to_cpu(sb->bblog_offset));
1890 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001891 }
1892 }
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001895 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (rdev2->desc_nr+1 > max_dev)
1897 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001898
NeilBrown70471da2009-08-03 10:59:57 +10001899 if (max_dev > le32_to_cpu(sb->max_dev)) {
1900 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001901 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001902 rdev->sb_size = max_dev * 2 + 256;
1903 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1904 if (rdev->sb_size & bmask)
1905 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001906 } else
1907 max_dev = le32_to_cpu(sb->max_dev);
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 for (i=0; i<max_dev;i++)
Lidong Zhong8df72022017-06-12 10:45:55 +08001910 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001911
Song Liua97b7892015-10-08 21:54:09 -07001912 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1913 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001915 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001916 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
1917 sb->feature_map |=
1918 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
1919 else
1920 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001921 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
1922 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
1923 }
1924
NeilBrowndafb20f2012-03-19 12:46:39 +11001925 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001927 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001928 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001929 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001931 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001932 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001933 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001934 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 else
Song Liuc4d4c912015-08-13 14:31:54 -07001936 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 sb->sb_csum = calc_sb_1_csum(sb);
1940}
1941
Chris Webb0cd17fe2008-06-28 08:31:46 +10001942static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001943super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001944{
1945 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001946 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001947 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001948 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001949 if (rdev->data_offset != rdev->new_data_offset)
1950 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001951 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001952 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001953 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001954 max_sectors -= rdev->data_offset;
1955 if (!num_sectors || num_sectors > max_sectors)
1956 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001957 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001958 /* minor version 0 with bitmap we can't move */
1959 return 0;
1960 } else {
1961 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001962 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001963 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001964 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001965 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001966 if (!num_sectors || num_sectors > max_sectors)
1967 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001968 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001969 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001970 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001971 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08001972 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10001973 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11001974 do {
1975 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1976 rdev->sb_page);
1977 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001978 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001979
1980}
1981
1982static int
1983super_1_allow_new_offset(struct md_rdev *rdev,
1984 unsigned long long new_offset)
1985{
1986 /* All necessary checks on new >= old have been done */
1987 struct bitmap *bitmap;
1988 if (new_offset >= rdev->data_offset)
1989 return 1;
1990
1991 /* with 1.0 metadata, there is no metadata to tread on
1992 * so we can always move back */
1993 if (rdev->mddev->minor_version == 0)
1994 return 1;
1995
1996 /* otherwise we must be sure not to step on
1997 * any metadata, so stay:
1998 * 36K beyond start of superblock
1999 * beyond end of badblocks
2000 * beyond write-intent bitmap
2001 */
2002 if (rdev->sb_start + (32+4)*2 > new_offset)
2003 return 0;
2004 bitmap = rdev->mddev->bitmap;
2005 if (bitmap && !rdev->mddev->bitmap_info.file &&
2006 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10002007 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10002008 return 0;
2009 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2010 return 0;
2011
2012 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002013}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Adrian Bunk75c96f82005-05-05 16:16:09 -07002015static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 [0] = {
2017 .name = "0.90.0",
2018 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002019 .load_super = super_90_load,
2020 .validate_super = super_90_validate,
2021 .sync_super = super_90_sync,
2022 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002023 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 },
2025 [1] = {
2026 .name = "md-1",
2027 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002028 .load_super = super_1_load,
2029 .validate_super = super_1_validate,
2030 .sync_super = super_1_sync,
2031 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002032 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 },
2034};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
NeilBrownfd01b882011-10-11 16:47:53 +11002036static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05002037{
2038 if (mddev->sync_super) {
2039 mddev->sync_super(mddev, rdev);
2040 return;
2041 }
2042
2043 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2044
2045 super_types[mddev->major_version].sync_super(mddev, rdev);
2046}
2047
NeilBrownfd01b882011-10-11 16:47:53 +11002048static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
NeilBrown3cb03002011-10-11 16:45:26 +11002050 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
NeilBrown4b809912008-07-21 17:05:25 +10002052 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07002053 rdev_for_each_rcu(rdev, mddev1) {
2054 if (test_bit(Faulty, &rdev->flags) ||
2055 test_bit(Journal, &rdev->flags) ||
2056 rdev->raid_disk == -1)
2057 continue;
2058 rdev_for_each_rcu(rdev2, mddev2) {
2059 if (test_bit(Faulty, &rdev2->flags) ||
2060 test_bit(Journal, &rdev2->flags) ||
2061 rdev2->raid_disk == -1)
2062 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002063 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002064 rdev2->bdev->bd_contains) {
2065 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002066 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002067 }
Song Liu0b020e82015-09-03 23:00:35 -07002068 }
2069 }
NeilBrown4b809912008-07-21 17:05:25 +10002070 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return 0;
2072}
2073
2074static LIST_HEAD(pending_raid_disks);
2075
Andre Nollac5e7112009-08-03 10:59:47 +10002076/*
2077 * Try to register data integrity profile for an mddev
2078 *
2079 * This is called when an array is started and after a disk has been kicked
2080 * from the array. It only succeeds if all working and active component devices
2081 * are integrity capable with matching profiles.
2082 */
NeilBrownfd01b882011-10-11 16:47:53 +11002083int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002084{
NeilBrown3cb03002011-10-11 16:45:26 +11002085 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002086
Andre Nollac5e7112009-08-03 10:59:47 +10002087 if (list_empty(&mddev->disks))
2088 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002089 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2090 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002091 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002092 /* skip spares and non-functional disks */
2093 if (test_bit(Faulty, &rdev->flags))
2094 continue;
2095 if (rdev->raid_disk < 0)
2096 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002097 if (!reference) {
2098 /* Use the first rdev as the reference */
2099 reference = rdev;
2100 continue;
2101 }
2102 /* does this rdev's profile match the reference profile? */
2103 if (blk_integrity_compare(reference->bdev->bd_disk,
2104 rdev->bdev->bd_disk) < 0)
2105 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002106 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002107 if (!reference || !bdev_get_integrity(reference->bdev))
2108 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002109 /*
2110 * All component devices are integrity capable and have matching
2111 * profiles, register the common profile for the md device.
2112 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002113 blk_integrity_register(mddev->gendisk,
2114 bdev_get_integrity(reference->bdev));
2115
NeilBrown9d487392016-11-02 14:16:49 +11002116 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Kent Overstreetafeee512018-05-20 18:25:52 -04002117 if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002118 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002119 mdname(mddev));
2120 return -EINVAL;
2121 }
Andre Nollac5e7112009-08-03 10:59:47 +10002122 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002123}
Andre Nollac5e7112009-08-03 10:59:47 +10002124EXPORT_SYMBOL(md_integrity_register);
2125
Dan Williams1501efa2016-01-13 16:00:07 -08002126/*
2127 * Attempt to add an rdev, but only if it is consistent with the current
2128 * integrity profile
2129 */
2130int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002131{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002132 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002133 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002134
2135 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002136 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002137
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002138 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002139
2140 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002141 return 0;
2142
2143 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002144 pr_err("%s: incompatible integrity profile for %s\n",
2145 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002146 return -ENXIO;
2147 }
2148
2149 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002150}
2151EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002152
NeilBrownf72ffdd2014-09-30 14:23:59 +10002153static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002155 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002156 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002157 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Dan Williams11e2ede2008-04-30 00:52:32 -07002159 /* prevent duplicates */
2160 if (find_rdev(mddev, rdev->bdev->bd_dev))
2161 return -EEXIST;
2162
NeilBrown97b20ef2017-04-13 08:53:48 +10002163 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2164 mddev->pers)
2165 return -EROFS;
2166
Andre Nolldd8ac332009-03-31 14:33:13 +11002167 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002168 if (!test_bit(Journal, &rdev->flags) &&
2169 rdev->sectors &&
2170 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002171 if (mddev->pers) {
2172 /* Cannot change size, so fail
2173 * If mddev->level <= 0, then we don't care
2174 * about aligning sizes (e.g. linear)
2175 */
2176 if (mddev->level > 0)
2177 return -ENOSPC;
2178 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002179 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 /* Verify rdev->desc_nr is unique.
2183 * If it is -1, assign a free number, else
2184 * check number is not in use
2185 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002186 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (rdev->desc_nr < 0) {
2188 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002189 if (mddev->pers)
2190 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002191 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 choice++;
2193 rdev->desc_nr = choice;
2194 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002195 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002196 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002200 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002201 if (!test_bit(Journal, &rdev->flags) &&
2202 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002203 pr_warn("md: %s: array is limited to %d devices\n",
2204 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002205 return -EBUSY;
2206 }
NeilBrown19133a42005-11-08 21:39:35 -08002207 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002208 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002211 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002212
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002213 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002214 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002215
Tejun Heo0762b8b2008-08-25 19:56:12 +09002216 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002217 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2218 /* failure here is OK */;
2219 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002220
NeilBrown4b809912008-07-21 17:05:25 +10002221 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002222 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002223
2224 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002225 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002228
2229 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002230 pr_warn("md: failed to register dev-%s for %s\n",
2231 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002232 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
NeilBrown177a99b2008-02-06 01:39:56 -08002235static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002236{
NeilBrown3cb03002011-10-11 16:45:26 +11002237 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002238 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002239 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002240}
2241
NeilBrownf72ffdd2014-09-30 14:23:59 +10002242static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
2244 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002245
Tejun Heo49731ba2011-01-14 18:43:57 +01002246 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002247 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002248 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002250 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002251 sysfs_put(rdev->sysfs_state);
2252 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002253 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002254 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002255 * writing to 'remove' to "dev/state". We also need
2256 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002257 */
NeilBrown4b809912008-07-21 17:05:25 +10002258 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002259 INIT_WORK(&rdev->del_work, md_delayed_delete);
2260 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002261 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
2263
2264/*
2265 * prevent the device from being mounted, repartitioned or
2266 * otherwise reused by a RAID array (or any other kernel
2267 * subsystem), by bd_claiming the device.
2268 */
NeilBrown3cb03002011-10-11 16:45:26 +11002269static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 int err = 0;
2272 struct block_device *bdev;
2273 char b[BDEVNAME_SIZE];
2274
Tejun Heod4d77622010-11-13 11:55:18 +01002275 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002276 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002278 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 return PTR_ERR(bdev);
2280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 rdev->bdev = bdev;
2282 return err;
2283}
2284
NeilBrown3cb03002011-10-11 16:45:26 +11002285static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 struct block_device *bdev = rdev->bdev;
2288 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002289 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
2292void md_autodetect_dev(dev_t dev);
2293
NeilBrownf72ffdd2014-09-30 14:23:59 +10002294static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
2296 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002297
NeilBrown9d487392016-11-02 14:16:49 +11002298 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002299 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002301 if (test_bit(AutoDetected, &rdev->flags))
2302 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303#endif
2304 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002305 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306}
2307
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002308void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
2310 unbind_rdev_from_array(rdev);
2311 export_rdev(rdev);
2312}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002313EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
NeilBrownfd01b882011-10-11 16:47:53 +11002315static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
NeilBrown0638bb02014-09-25 17:43:47 +10002317 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
NeilBrown0638bb02014-09-25 17:43:47 +10002319 while (!list_empty(&mddev->disks)) {
2320 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2321 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002322 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 mddev->raid_disks = 0;
2325 mddev->major_version = 0;
2326}
2327
NeilBrown6497709b2017-03-15 14:05:14 +11002328static bool set_in_sync(struct mddev *mddev)
2329{
Shaohua Liefa4b772017-10-18 22:08:13 -07002330 lockdep_assert_held(&mddev->lock);
NeilBrown4ad23a972017-03-15 14:05:14 +11002331 if (!mddev->in_sync) {
2332 mddev->sync_checkers++;
2333 spin_unlock(&mddev->lock);
2334 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2335 spin_lock(&mddev->lock);
2336 if (!mddev->in_sync &&
2337 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002338 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002339 /*
2340 * Ensure ->in_sync is visible before we clear
2341 * ->sync_checkers.
2342 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002343 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002344 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2345 sysfs_notify_dirent_safe(mddev->sysfs_state);
2346 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002347 if (--mddev->sync_checkers == 0)
2348 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002349 }
2350 if (mddev->safemode == 1)
2351 mddev->safemode = 0;
2352 return mddev->in_sync;
2353}
2354
NeilBrownf72ffdd2014-09-30 14:23:59 +10002355static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
NeilBrown42543762006-06-26 00:27:57 -07002357 /* Update each superblock (in-memory image), but
2358 * if we are allowed to, skip spares which already
2359 * have the right event counter, or have one earlier
2360 * (which would mean they aren't being marked as dirty
2361 * with the rest of the array)
2362 */
NeilBrown3cb03002011-10-11 16:45:26 +11002363 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002364 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002365 if (rdev->sb_events == mddev->events ||
2366 (nospares &&
2367 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002368 rdev->sb_events+1 == mddev->events)) {
2369 /* Don't update this superblock */
2370 rdev->sb_loaded = 2;
2371 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002372 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002373 rdev->sb_loaded = 1;
2374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376}
2377
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002378static bool does_sb_need_changing(struct mddev *mddev)
2379{
2380 struct md_rdev *rdev;
2381 struct mdp_superblock_1 *sb;
2382 int role;
2383
2384 /* Find a good rdev */
2385 rdev_for_each(rdev, mddev)
2386 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2387 break;
2388
2389 /* No good device found. */
2390 if (!rdev)
2391 return false;
2392
2393 sb = page_address(rdev->sb_page);
2394 /* Check if a device has become faulty or a spare become active */
2395 rdev_for_each(rdev, mddev) {
2396 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2397 /* Device activated? */
2398 if (role == 0xffff && rdev->raid_disk >=0 &&
2399 !test_bit(Faulty, &rdev->flags))
2400 return true;
2401 /* Device turned faulty? */
2402 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2403 return true;
2404 }
2405
2406 /* Check if any mddev parameters have changed */
2407 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2408 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002409 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002410 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2411 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2412 return true;
2413
2414 return false;
2415}
2416
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002417void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
NeilBrown3cb03002011-10-11 16:45:26 +11002419 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002420 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002421 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002422 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002423 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
NeilBrownd87f0642013-04-24 11:42:40 +10002425 if (mddev->ro) {
2426 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002427 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002428 return;
2429 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002430
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002431repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002432 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002433 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002434 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002435 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002436 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002437 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002438 /* Has someone else has updated the sb */
2439 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002440 if (ret == 0)
2441 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002442 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2443 BIT(MD_SB_CHANGE_DEVS) |
2444 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002445 return;
2446 }
2447 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002448
NeilBrowndb0505d2017-10-17 16:18:36 +11002449 /*
2450 * First make sure individual recovery_offsets are correct
2451 * curr_resync_completed can only be used during recovery.
2452 * During reshape/resync it might use array-addresses rather
2453 * that device addresses.
2454 */
NeilBrowndafb20f2012-03-19 12:46:39 +11002455 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002456 if (rdev->raid_disk >= 0 &&
2457 mddev->delta_disks >= 0 &&
NeilBrowndb0505d2017-10-17 16:18:36 +11002458 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2459 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2460 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
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))
NeilBrown850b2b422006-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))
NeilBrown850b2b422006-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 */
NeilBrown850b2b422006-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 */
NeilBrown850b2b422006-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:
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002552 md_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
NeilBrown850b2b422006-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;
Mark Rutland6aa7de02017-10-23 14:07:29 -07002676 unsigned long flags = READ_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")) {
Yufen Yuee37e622019-04-02 14:22:14 +08002845 if (!rdev->mddev->pers)
2846 err = -EINVAL;
2847 else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
2848 rdev->saved_raid_disk >= 0) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002849 /* clear_bit is performed _after_ all the devices
2850 * have their local Faulty bit cleared. If any writes
2851 * happen in the meantime in the local node, they
2852 * will land in the local bitmap, which will be synced
2853 * by this node eventually
2854 */
2855 if (!mddev_is_clustered(rdev->mddev) ||
2856 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2857 clear_bit(Faulty, &rdev->flags);
2858 err = add_bound_rdev(rdev);
2859 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002860 } else
2861 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002862 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2863 set_bit(ExternalBbl, &rdev->flags);
2864 rdev->badblocks.shift = 0;
2865 err = 0;
2866 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2867 clear_bit(ExternalBbl, &rdev->flags);
2868 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002869 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002870 if (!err)
2871 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002872 return err ? err : len;
2873}
NeilBrown80ca3a42006-07-10 04:44:18 -07002874static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002875__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002876
2877static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002878errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002879{
2880 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2881}
2882
2883static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002884errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002885{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002886 unsigned int n;
2887 int rv;
2888
2889 rv = kstrtouint(buf, 10, &n);
2890 if (rv < 0)
2891 return rv;
2892 atomic_set(&rdev->corrected_errors, n);
2893 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002894}
2895static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002896__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002897
NeilBrown014236d2006-01-06 00:20:55 -08002898static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002899slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002900{
Shaohua Lif2076e72015-10-08 21:54:12 -07002901 if (test_bit(Journal, &rdev->flags))
2902 return sprintf(page, "journal\n");
2903 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002904 return sprintf(page, "none\n");
2905 else
2906 return sprintf(page, "%d\n", rdev->raid_disk);
2907}
2908
2909static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002910slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002911{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002912 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002913 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002914
Shaohua Lif2076e72015-10-08 21:54:12 -07002915 if (test_bit(Journal, &rdev->flags))
2916 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002917 if (strncmp(buf, "none", 4)==0)
2918 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002919 else {
2920 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2921 if (err < 0)
2922 return err;
2923 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002924 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002925 /* Setting 'slot' on an active array requires also
2926 * updating the 'rd%d' link, and communicating
2927 * with the personality with ->hot_*_disk.
2928 * For now we only support removing
2929 * failed/spare devices. This normally happens automatically,
2930 * but not when the metadata is externally managed.
2931 */
NeilBrownc303da62008-02-06 01:39:51 -08002932 if (rdev->raid_disk == -1)
2933 return -EEXIST;
2934 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002935 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002936 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002937 clear_bit(Blocked, &rdev->flags);
2938 remove_and_add_spares(rdev->mddev, rdev);
2939 if (rdev->raid_disk >= 0)
2940 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002941 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2942 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002943 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002944 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002945 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002946 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002947 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002948
2949 if (rdev->raid_disk != -1)
2950 return -EBUSY;
2951
NeilBrownc6751b22011-02-02 11:57:13 +11002952 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2953 return -EBUSY;
2954
Neil Brown6c2fce22008-06-28 08:31:31 +10002955 if (rdev->mddev->pers->hot_add_disk == NULL)
2956 return -EINVAL;
2957
NeilBrownba1b41b2011-01-14 09:14:34 +11002958 if (slot >= rdev->mddev->raid_disks &&
2959 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2960 return -ENOSPC;
2961
Neil Brown6c2fce22008-06-28 08:31:31 +10002962 rdev->raid_disk = slot;
2963 if (test_bit(In_sync, &rdev->flags))
2964 rdev->saved_raid_disk = slot;
2965 else
2966 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002967 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002968 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002969 err = rdev->mddev->pers->
2970 hot_add_disk(rdev->mddev, rdev);
2971 if (err) {
2972 rdev->raid_disk = -1;
2973 return err;
2974 } else
2975 sysfs_notify_dirent_safe(rdev->sysfs_state);
2976 if (sysfs_link_rdev(rdev->mddev, rdev))
2977 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002978 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002979 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002980 if (slot >= rdev->mddev->raid_disks &&
2981 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002982 return -ENOSPC;
2983 rdev->raid_disk = slot;
2984 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002985 clear_bit(Faulty, &rdev->flags);
2986 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002987 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002988 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002989 }
NeilBrown014236d2006-01-06 00:20:55 -08002990 return len;
2991}
2992
NeilBrown014236d2006-01-06 00:20:55 -08002993static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002994__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002995
NeilBrown93c8cad2006-01-06 00:20:56 -08002996static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002997offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002998{
Andrew Morton6961ece2006-01-06 00:20:59 -08002999 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08003000}
3001
3002static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003003offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08003004{
NeilBrownc6563a82012-05-21 09:27:00 +10003005 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003006 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003007 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10003008 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003009 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003010 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08003011 /* Must set offset before size, so overlap checks
3012 * can be sane */
3013 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08003014 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10003015 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08003016 return len;
3017}
3018
3019static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07003020__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08003021
NeilBrownc6563a82012-05-21 09:27:00 +10003022static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3023{
3024 return sprintf(page, "%llu\n",
3025 (unsigned long long)rdev->new_data_offset);
3026}
3027
3028static ssize_t new_offset_store(struct md_rdev *rdev,
3029 const char *buf, size_t len)
3030{
3031 unsigned long long new_offset;
3032 struct mddev *mddev = rdev->mddev;
3033
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003034 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10003035 return -EINVAL;
3036
NeilBrownf851b602014-12-11 10:02:10 +11003037 if (mddev->sync_thread ||
3038 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10003039 return -EBUSY;
3040 if (new_offset == rdev->data_offset)
3041 /* reset is always permitted */
3042 ;
3043 else if (new_offset > rdev->data_offset) {
3044 /* must not push array size beyond rdev_sectors */
3045 if (new_offset - rdev->data_offset
3046 + mddev->dev_sectors > rdev->sectors)
3047 return -E2BIG;
3048 }
3049 /* Metadata worries about other space details. */
3050
3051 /* decreasing the offset is inconsistent with a backwards
3052 * reshape.
3053 */
3054 if (new_offset < rdev->data_offset &&
3055 mddev->reshape_backwards)
3056 return -EINVAL;
3057 /* Increasing offset is inconsistent with forwards
3058 * reshape. reshape_direction should be set to
3059 * 'backwards' first.
3060 */
3061 if (new_offset > rdev->data_offset &&
3062 !mddev->reshape_backwards)
3063 return -EINVAL;
3064
3065 if (mddev->pers && mddev->persistent &&
3066 !super_types[mddev->major_version]
3067 .allow_new_offset(rdev, new_offset))
3068 return -E2BIG;
3069 rdev->new_data_offset = new_offset;
3070 if (new_offset > rdev->data_offset)
3071 mddev->reshape_backwards = 1;
3072 else if (new_offset < rdev->data_offset)
3073 mddev->reshape_backwards = 0;
3074
3075 return len;
3076}
3077static struct rdev_sysfs_entry rdev_new_offset =
3078__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3079
NeilBrown83303b62006-01-06 00:21:06 -08003080static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003081rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003082{
Andre Nolldd8ac332009-03-31 14:33:13 +11003083 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003084}
3085
NeilBrownc5d79ad2008-02-06 01:39:54 -08003086static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3087{
3088 /* check if two start/length pairs overlap */
3089 if (s1+l1 <= s2)
3090 return 0;
3091 if (s2+l2 <= s1)
3092 return 0;
3093 return 1;
3094}
3095
Dan Williamsb522adc2009-03-31 15:00:31 +11003096static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3097{
3098 unsigned long long blocks;
3099 sector_t new;
3100
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003101 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003102 return -EINVAL;
3103
3104 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3105 return -EINVAL; /* sector conversion overflow */
3106
3107 new = blocks * 2;
3108 if (new != blocks * 2)
3109 return -EINVAL; /* unsigned long long to sector_t overflow */
3110
3111 *sectors = new;
3112 return 0;
3113}
3114
NeilBrown83303b62006-01-06 00:21:06 -08003115static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003116rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003117{
NeilBrownfd01b882011-10-11 16:47:53 +11003118 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003119 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003120 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003121
Shaohua Lif2076e72015-10-08 21:54:12 -07003122 if (test_bit(Journal, &rdev->flags))
3123 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003124 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003125 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003126 if (rdev->data_offset != rdev->new_data_offset)
3127 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003128 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003129 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003130 sectors = super_types[my_mddev->major_version].
3131 rdev_size_change(rdev, sectors);
3132 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003133 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003134 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003135 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003136 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003137 if (!my_mddev->pers->resize)
3138 /* Cannot change size for RAID0 or Linear etc */
3139 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003140 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003141 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003142 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003143
Andre Nolldd8ac332009-03-31 14:33:13 +11003144 rdev->sectors = sectors;
3145 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003146 /* Need to check that all other rdevs with the same
3147 * ->bdev do not overlap. 'rcu' is sufficient to walk
3148 * the rdev lists safely.
3149 * This check does not provide a hard guarantee, it
3150 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003151 */
NeilBrownfd01b882011-10-11 16:47:53 +11003152 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003153 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003154 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003155
NeilBrown8b1afc32014-09-29 15:33:20 +10003156 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003157 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003158 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003159
NeilBrowndafb20f2012-03-19 12:46:39 +11003160 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003161 if (rdev->bdev == rdev2->bdev &&
3162 rdev != rdev2 &&
3163 overlaps(rdev->data_offset, rdev->sectors,
3164 rdev2->data_offset,
3165 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003166 overlap = 1;
3167 break;
3168 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003169 if (overlap) {
3170 mddev_put(mddev);
3171 break;
3172 }
3173 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003174 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003175 if (overlap) {
3176 /* Someone else could have slipped in a size
3177 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003178 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003179 * safe, and trust userspace not to race with
3180 * itself
3181 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003182 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003183 return -EBUSY;
3184 }
3185 }
NeilBrown83303b62006-01-06 00:21:06 -08003186 return len;
3187}
3188
3189static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003190__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003191
NeilBrown3cb03002011-10-11 16:45:26 +11003192static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003193{
3194 unsigned long long recovery_start = rdev->recovery_offset;
3195
3196 if (test_bit(In_sync, &rdev->flags) ||
3197 recovery_start == MaxSector)
3198 return sprintf(page, "none\n");
3199
3200 return sprintf(page, "%llu\n", recovery_start);
3201}
3202
NeilBrown3cb03002011-10-11 16:45:26 +11003203static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003204{
3205 unsigned long long recovery_start;
3206
3207 if (cmd_match(buf, "none"))
3208 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003209 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003210 return -EINVAL;
3211
3212 if (rdev->mddev->pers &&
3213 rdev->raid_disk >= 0)
3214 return -EBUSY;
3215
3216 rdev->recovery_offset = recovery_start;
3217 if (recovery_start == MaxSector)
3218 set_bit(In_sync, &rdev->flags);
3219 else
3220 clear_bit(In_sync, &rdev->flags);
3221 return len;
3222}
3223
3224static struct rdev_sysfs_entry rdev_recovery_start =
3225__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3226
Vishal Vermafc974ee2015-12-24 19:20:34 -07003227/* sysfs access to bad-blocks list.
3228 * We present two files.
3229 * 'bad-blocks' lists sector numbers and lengths of ranges that
3230 * are recorded as bad. The list is truncated to fit within
3231 * the one-page limit of sysfs.
3232 * Writing "sector length" to this file adds an acknowledged
3233 * bad block list.
3234 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3235 * been acknowledged. Writing to this file adds bad blocks
3236 * without acknowledging them. This is largely for testing.
3237 */
NeilBrown3cb03002011-10-11 16:45:26 +11003238static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003239{
3240 return badblocks_show(&rdev->badblocks, page, 0);
3241}
NeilBrown3cb03002011-10-11 16:45:26 +11003242static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003243{
NeilBrownde393cd2011-07-28 11:31:48 +10003244 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3245 /* Maybe that ack was all we needed */
3246 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3247 wake_up(&rdev->blocked_wait);
3248 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003249}
3250static struct rdev_sysfs_entry rdev_bad_blocks =
3251__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3252
NeilBrown3cb03002011-10-11 16:45:26 +11003253static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003254{
3255 return badblocks_show(&rdev->badblocks, page, 1);
3256}
NeilBrown3cb03002011-10-11 16:45:26 +11003257static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003258{
3259 return badblocks_store(&rdev->badblocks, page, len, 1);
3260}
3261static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3262__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3263
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003264static ssize_t
3265ppl_sector_show(struct md_rdev *rdev, char *page)
3266{
3267 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3268}
3269
3270static ssize_t
3271ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3272{
3273 unsigned long long sector;
3274
3275 if (kstrtoull(buf, 10, &sector) < 0)
3276 return -EINVAL;
3277 if (sector != (sector_t)sector)
3278 return -EINVAL;
3279
3280 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3281 rdev->raid_disk >= 0)
3282 return -EBUSY;
3283
3284 if (rdev->mddev->persistent) {
3285 if (rdev->mddev->major_version == 0)
3286 return -EINVAL;
3287 if ((sector > rdev->sb_start &&
3288 sector - rdev->sb_start > S16_MAX) ||
3289 (sector < rdev->sb_start &&
3290 rdev->sb_start - sector > -S16_MIN))
3291 return -EINVAL;
3292 rdev->ppl.offset = sector - rdev->sb_start;
3293 } else if (!rdev->mddev->external) {
3294 return -EBUSY;
3295 }
3296 rdev->ppl.sector = sector;
3297 return len;
3298}
3299
3300static struct rdev_sysfs_entry rdev_ppl_sector =
3301__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3302
3303static ssize_t
3304ppl_size_show(struct md_rdev *rdev, char *page)
3305{
3306 return sprintf(page, "%u\n", rdev->ppl.size);
3307}
3308
3309static ssize_t
3310ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3311{
3312 unsigned int size;
3313
3314 if (kstrtouint(buf, 10, &size) < 0)
3315 return -EINVAL;
3316
3317 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3318 rdev->raid_disk >= 0)
3319 return -EBUSY;
3320
3321 if (rdev->mddev->persistent) {
3322 if (rdev->mddev->major_version == 0)
3323 return -EINVAL;
3324 if (size > U16_MAX)
3325 return -EINVAL;
3326 } else if (!rdev->mddev->external) {
3327 return -EBUSY;
3328 }
3329 rdev->ppl.size = size;
3330 return len;
3331}
3332
3333static struct rdev_sysfs_entry rdev_ppl_size =
3334__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3335
NeilBrown86e6ffd2005-11-08 21:39:24 -08003336static struct attribute *rdev_default_attrs[] = {
3337 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003338 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003339 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003340 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003341 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003342 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003343 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003344 &rdev_bad_blocks.attr,
3345 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003346 &rdev_ppl_sector.attr,
3347 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003348 NULL,
3349};
3350static ssize_t
3351rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3352{
3353 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003354 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003355
3356 if (!entry->show)
3357 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003358 if (!rdev->mddev)
3359 return -EBUSY;
3360 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003361}
3362
3363static ssize_t
3364rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3365 const char *page, size_t length)
3366{
3367 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003368 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003369 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003370 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003371
3372 if (!entry->store)
3373 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003374 if (!capable(CAP_SYS_ADMIN))
3375 return -EACCES;
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003376 rv = mddev ? mddev_lock(mddev) : -ENODEV;
NeilBrownca388052008-02-06 01:39:55 -08003377 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003378 if (rdev->mddev == NULL)
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003379 rv = -ENODEV;
NeilBrown27c529b2008-03-04 14:29:33 -08003380 else
3381 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003382 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003383 }
3384 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003385}
3386
3387static void rdev_free(struct kobject *ko)
3388{
NeilBrown3cb03002011-10-11 16:45:26 +11003389 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003390 kfree(rdev);
3391}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003392static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003393 .show = rdev_attr_show,
3394 .store = rdev_attr_store,
3395};
3396static struct kobj_type rdev_ktype = {
3397 .release = rdev_free,
3398 .sysfs_ops = &rdev_sysfs_ops,
3399 .default_attrs = rdev_default_attrs,
3400};
3401
NeilBrown3cb03002011-10-11 16:45:26 +11003402int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003403{
3404 rdev->desc_nr = -1;
3405 rdev->saved_raid_disk = -1;
3406 rdev->raid_disk = -1;
3407 rdev->flags = 0;
3408 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003409 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003410 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003411 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003412 rdev->sb_loaded = 0;
3413 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003414 atomic_set(&rdev->nr_pending, 0);
3415 atomic_set(&rdev->read_errors, 0);
3416 atomic_set(&rdev->corrected_errors, 0);
3417
3418 INIT_LIST_HEAD(&rdev->same_set);
3419 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003420
3421 /* Add space to store bad block list.
3422 * This reserves the space even on arrays where it cannot
3423 * be used - I wonder if that matters
3424 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003425 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003426}
3427EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428/*
3429 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3430 *
3431 * mark the device faulty if:
3432 *
3433 * - the device is nonexistent (zero size)
3434 * - the device has no valid superblock
3435 *
3436 * a faulty rdev _never_ has rdev->sb set.
3437 */
NeilBrown3cb03002011-10-11 16:45:26 +11003438static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
3440 char b[BDEVNAME_SIZE];
3441 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003442 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 sector_t size;
3444
NeilBrown9ffae0c2006-01-06 00:20:32 -08003445 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003446 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
NeilBrown2230dfe2011-07-28 11:31:46 +10003449 err = md_rdev_init(rdev);
3450 if (err)
3451 goto abort_free;
3452 err = alloc_disk_sb(rdev);
3453 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 goto abort_free;
3455
NeilBrownc5d79ad2008-02-06 01:39:54 -08003456 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 if (err)
3458 goto abort_free;
3459
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003460 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003461
Mike Snitzer77304d22010-11-08 14:39:12 +01003462 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003464 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 bdevname(rdev->bdev,b));
3466 err = -EINVAL;
3467 goto abort_free;
3468 }
3469
3470 if (super_format >= 0) {
3471 err = super_types[super_format].
3472 load_super(rdev, NULL, super_minor);
3473 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003474 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003475 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003476 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 goto abort_free;
3478 }
3479 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003480 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 bdevname(rdev->bdev,b));
3482 goto abort_free;
3483 }
3484 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003485
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 return rdev;
3487
3488abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003489 if (rdev->bdev)
3490 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003491 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 kfree(rdev);
3493 return ERR_PTR(err);
3494}
3495
3496/*
3497 * Check a full RAID array for plausibility
3498 */
3499
NeilBrownf72ffdd2014-09-30 14:23:59 +10003500static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501{
3502 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003503 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 char b[BDEVNAME_SIZE];
3505
3506 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003507 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 switch (super_types[mddev->major_version].
3509 load_super(rdev, freshest, mddev->minor_version)) {
3510 case 1:
3511 freshest = rdev;
3512 break;
3513 case 0:
3514 break;
3515 default:
NeilBrown9d487392016-11-02 14:16:49 +11003516 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003518 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 }
3520
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 super_types[mddev->major_version].
3522 validate_super(mddev, freshest);
3523
3524 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003525 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003526 if (mddev->max_disks &&
3527 (rdev->desc_nr >= mddev->max_disks ||
3528 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003529 pr_warn("md: %s: %s: only %d devices permitted\n",
3530 mdname(mddev), bdevname(rdev->bdev, b),
3531 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003532 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003533 continue;
3534 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003535 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 if (super_types[mddev->major_version].
3537 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003538 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003540 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 continue;
3542 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 if (mddev->level == LEVEL_MULTIPATH) {
3545 rdev->desc_nr = i++;
3546 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003547 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003548 } else if (rdev->raid_disk >=
3549 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3550 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003551 rdev->raid_disk = -1;
3552 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 }
3554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555}
3556
NeilBrown72e02072009-12-14 12:49:55 +11003557/* Read a fixed-point number.
3558 * Numbers in sysfs attributes should be in "standard" units where
3559 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003560 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003561 * milliseconds or jiffies.
3562 * This function takes a decimal number with a possible fractional
3563 * component, and produces an integer which is the result of
3564 * multiplying that number by 10^'scale'.
3565 * all without any floating-point arithmetic.
3566 */
3567int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3568{
3569 unsigned long result = 0;
3570 long decimals = -1;
3571 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3572 if (*cp == '.')
3573 decimals = 0;
3574 else if (decimals < scale) {
3575 unsigned int value;
3576 value = *cp - '0';
3577 result = result * 10 + value;
3578 if (decimals >= 0)
3579 decimals++;
3580 }
3581 cp++;
3582 }
3583 if (*cp == '\n')
3584 cp++;
3585 if (*cp)
3586 return -EINVAL;
3587 if (decimals < 0)
3588 decimals = 0;
3589 while (decimals < scale) {
3590 result *= 10;
3591 decimals ++;
3592 }
3593 *res = result;
3594 return 0;
3595}
3596
NeilBrowneae17012005-11-08 21:39:23 -08003597static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003598safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003599{
3600 int msec = (mddev->safemode_delay*1000)/HZ;
3601 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3602}
3603static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003604safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003605{
NeilBrown16f17b32006-06-26 00:27:37 -07003606 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003607
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003608 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003609 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003610 return -EINVAL;
3611 }
3612
NeilBrown72e02072009-12-14 12:49:55 +11003613 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003614 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003615 if (msec == 0)
3616 mddev->safemode_delay = 0;
3617 else {
NeilBrown19052c02008-08-05 15:54:13 +10003618 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003619 unsigned long new_delay = (msec*HZ)/1000;
3620
3621 if (new_delay == 0)
3622 new_delay = 1;
3623 mddev->safemode_delay = new_delay;
3624 if (new_delay < old_delay || old_delay == 0)
3625 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003626 }
3627 return len;
3628}
3629static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003630__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003631
3632static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003633level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003634{
NeilBrown36d091f2014-12-15 12:56:58 +11003635 struct md_personality *p;
3636 int ret;
3637 spin_lock(&mddev->lock);
3638 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003639 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003640 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003641 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003642 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003643 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003644 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003645 else
NeilBrown36d091f2014-12-15 12:56:58 +11003646 ret = 0;
3647 spin_unlock(&mddev->lock);
3648 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003649}
3650
NeilBrownd9d166c2006-01-06 00:20:51 -08003651static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003652level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003653{
Dan Williamsf2859af2010-05-02 10:04:16 -07003654 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003655 ssize_t rv;
3656 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003657 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003658 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003659 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003660 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003661
NeilBrown67918752014-12-15 12:57:01 +11003662 if (slen == 0 || slen >= sizeof(clevel))
3663 return -EINVAL;
3664
3665 rv = mddev_lock(mddev);
3666 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003667 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003668
3669 if (mddev->pers == NULL) {
3670 strncpy(mddev->clevel, buf, slen);
3671 if (mddev->clevel[slen-1] == '\n')
3672 slen--;
3673 mddev->clevel[slen] = 0;
3674 mddev->level = LEVEL_NONE;
3675 rv = len;
3676 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003677 }
NeilBrown67918752014-12-15 12:57:01 +11003678 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003679 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003680 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003681
3682 /* request to change the personality. Need to ensure:
3683 * - array is not engaged in resync/recovery/reshape
3684 * - old personality can be suspended
3685 * - new personality will access other array.
3686 */
3687
NeilBrown67918752014-12-15 12:57:01 +11003688 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003689 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003690 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003691 mddev->reshape_position != MaxSector ||
3692 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003693 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003694
NeilBrown67918752014-12-15 12:57:01 +11003695 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003696 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003697 pr_warn("md: %s: %s does not support online personality change\n",
3698 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003699 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003700 }
3701
3702 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003703 strncpy(clevel, buf, slen);
3704 if (clevel[slen-1] == '\n')
3705 slen--;
3706 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003707 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003708 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003709
Dan Williamsf2859af2010-05-02 10:04:16 -07003710 if (request_module("md-%s", clevel) != 0)
3711 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003712 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003713 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003714 if (!pers || !try_module_get(pers->owner)) {
3715 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003716 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003717 rv = -EINVAL;
3718 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003719 }
3720 spin_unlock(&pers_lock);
3721
3722 if (pers == mddev->pers) {
3723 /* Nothing to do! */
3724 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003725 rv = len;
3726 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003727 }
3728 if (!pers->takeover) {
3729 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003730 pr_warn("md: %s: %s does not support personality takeover\n",
3731 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003732 rv = -EINVAL;
3733 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003734 }
3735
NeilBrowndafb20f2012-03-19 12:46:39 +11003736 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003737 rdev->new_raid_disk = rdev->raid_disk;
3738
NeilBrown245f46c2009-03-31 14:39:39 +11003739 /* ->takeover must set new_* and/or delta_disks
3740 * if it succeeds, and may set them when it fails.
3741 */
3742 priv = pers->takeover(mddev);
3743 if (IS_ERR(priv)) {
3744 mddev->new_level = mddev->level;
3745 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003746 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003747 mddev->raid_disks -= mddev->delta_disks;
3748 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003749 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003750 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003751 pr_warn("md: %s: %s would not accept array\n",
3752 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003753 rv = PTR_ERR(priv);
3754 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003755 }
3756
3757 /* Looks like we have a winner */
3758 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003759 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003760
3761 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003762 oldpers = mddev->pers;
3763 oldpriv = mddev->private;
3764 mddev->pers = pers;
3765 mddev->private = priv;
3766 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3767 mddev->level = mddev->new_level;
3768 mddev->layout = mddev->new_layout;
3769 mddev->chunk_sectors = mddev->new_chunk_sectors;
3770 mddev->delta_disks = 0;
3771 mddev->reshape_backwards = 0;
3772 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003773 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003774
NeilBrowndb721d32014-12-15 12:56:58 +11003775 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003776 mddev->external) {
3777 /* We are converting from a no-redundancy array
3778 * to a redundancy array and metadata is managed
3779 * externally so we need to be sure that writes
3780 * won't block due to a need to transition
3781 * clean->dirty
3782 * until external management is started.
3783 */
3784 mddev->in_sync = 0;
3785 mddev->safemode_delay = 0;
3786 mddev->safemode = 0;
3787 }
3788
NeilBrowndb721d32014-12-15 12:56:58 +11003789 oldpers->free(mddev, oldpriv);
3790
3791 if (oldpers->sync_request == NULL &&
3792 pers->sync_request != NULL) {
3793 /* need to add the md_redundancy_group */
3794 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11003795 pr_warn("md: cannot register extra attributes for %s\n",
3796 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11003797 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3798 }
3799 if (oldpers->sync_request != NULL &&
3800 pers->sync_request == NULL) {
3801 /* need to remove the md_redundancy_group */
3802 if (mddev->to_remove == NULL)
3803 mddev->to_remove = &md_redundancy_group;
3804 }
3805
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003806 module_put(oldpers->owner);
3807
NeilBrowndafb20f2012-03-19 12:46:39 +11003808 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003809 if (rdev->raid_disk < 0)
3810 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003811 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003812 rdev->new_raid_disk = -1;
3813 if (rdev->new_raid_disk == rdev->raid_disk)
3814 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003815 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003816 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003817 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003818 if (rdev->raid_disk < 0)
3819 continue;
3820 if (rdev->new_raid_disk == rdev->raid_disk)
3821 continue;
3822 rdev->raid_disk = rdev->new_raid_disk;
3823 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003824 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003825 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003826 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11003827 pr_warn("md: cannot register rd%d for %s after level change\n",
3828 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003829 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003830 }
3831
NeilBrowndb721d32014-12-15 12:56:58 +11003832 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003833 /* this is now an array without redundancy, so
3834 * it must always be in_sync
3835 */
3836 mddev->in_sync = 1;
3837 del_timer_sync(&mddev->safemode_timer);
3838 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003839 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003840 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08003841 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003842 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003843 if (!mddev->thread)
3844 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003845 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003846 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003847 rv = len;
3848out_unlock:
3849 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003850 return rv;
3851}
3852
3853static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003854__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003855
NeilBrownd4dbd022006-06-26 00:27:59 -07003856static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003857layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003858{
3859 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003860 if (mddev->reshape_position != MaxSector &&
3861 mddev->layout != mddev->new_layout)
3862 return sprintf(page, "%d (%d)\n",
3863 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003864 return sprintf(page, "%d\n", mddev->layout);
3865}
3866
3867static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003868layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003869{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003870 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003871 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003872
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003873 err = kstrtouint(buf, 10, &n);
3874 if (err < 0)
3875 return err;
NeilBrown67918752014-12-15 12:57:01 +11003876 err = mddev_lock(mddev);
3877 if (err)
3878 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003879
NeilBrownb3546032009-03-31 14:56:41 +11003880 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003881 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003882 err = -EBUSY;
3883 else if (mddev->ro)
3884 err = -EROFS;
3885 else {
3886 mddev->new_layout = n;
3887 err = mddev->pers->check_reshape(mddev);
3888 if (err)
3889 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003890 }
NeilBrownb3546032009-03-31 14:56:41 +11003891 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003892 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003893 if (mddev->reshape_position == MaxSector)
3894 mddev->layout = n;
3895 }
NeilBrown67918752014-12-15 12:57:01 +11003896 mddev_unlock(mddev);
3897 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003898}
3899static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003900__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003901
NeilBrowneae17012005-11-08 21:39:23 -08003902static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003903raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003904{
NeilBrownbb636542005-11-08 21:39:45 -08003905 if (mddev->raid_disks == 0)
3906 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003907 if (mddev->reshape_position != MaxSector &&
3908 mddev->delta_disks != 0)
3909 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3910 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003911 return sprintf(page, "%d\n", mddev->raid_disks);
3912}
3913
NeilBrownfd01b882011-10-11 16:47:53 +11003914static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003915
3916static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003917raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003918{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003919 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003920 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003921
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003922 err = kstrtouint(buf, 10, &n);
3923 if (err < 0)
3924 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003925
NeilBrown67918752014-12-15 12:57:01 +11003926 err = mddev_lock(mddev);
3927 if (err)
3928 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003929 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003930 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003931 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003932 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003933 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003934
NeilBrown67918752014-12-15 12:57:01 +11003935 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003936 rdev_for_each(rdev, mddev) {
3937 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 if (olddisks > n &&
3941 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003942 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003943 }
NeilBrown67918752014-12-15 12:57:01 +11003944 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003945 mddev->delta_disks = n - olddisks;
3946 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003947 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003948 } else
NeilBrownda943b992006-01-06 00:20:54 -08003949 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003950out_unlock:
3951 mddev_unlock(mddev);
3952 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003953}
3954static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003955__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003956
NeilBrown24dd4692005-11-08 21:39:26 -08003957static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003958chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003959{
NeilBrown08a02ec2007-05-09 02:35:38 -07003960 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003961 mddev->chunk_sectors != mddev->new_chunk_sectors)
3962 return sprintf(page, "%d (%d)\n",
3963 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003964 mddev->chunk_sectors << 9);
3965 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003966}
3967
3968static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003969chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003970{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003971 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003972 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003973
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003974 err = kstrtoul(buf, 10, &n);
3975 if (err < 0)
3976 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003977
NeilBrown67918752014-12-15 12:57:01 +11003978 err = mddev_lock(mddev);
3979 if (err)
3980 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003981 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003982 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003983 err = -EBUSY;
3984 else if (mddev->ro)
3985 err = -EROFS;
3986 else {
3987 mddev->new_chunk_sectors = n >> 9;
3988 err = mddev->pers->check_reshape(mddev);
3989 if (err)
3990 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003991 }
NeilBrownb3546032009-03-31 14:56:41 +11003992 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003993 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003994 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003995 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003996 }
NeilBrown67918752014-12-15 12:57:01 +11003997 mddev_unlock(mddev);
3998 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003999}
4000static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004001__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08004002
NeilBrowna94213b2006-06-26 00:28:00 -07004003static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004004resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07004005{
NeilBrownd1a7c502009-03-31 15:24:32 +11004006 if (mddev->recovery_cp == MaxSector)
4007 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07004008 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4009}
4010
4011static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004012resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07004013{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004014 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11004015 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004016
4017 if (cmd_match(buf, "none"))
4018 n = MaxSector;
4019 else {
4020 err = kstrtoull(buf, 10, &n);
4021 if (err < 0)
4022 return err;
4023 if (n != (sector_t)n)
4024 return -EINVAL;
4025 }
NeilBrowna94213b2006-06-26 00:28:00 -07004026
NeilBrown67918752014-12-15 12:57:01 +11004027 err = mddev_lock(mddev);
4028 if (err)
4029 return err;
NeilBrownb0986362011-05-11 15:52:21 +10004030 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11004031 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07004032
NeilBrown67918752014-12-15 12:57:01 +11004033 if (!err) {
4034 mddev->recovery_cp = n;
4035 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08004036 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004037 }
4038 mddev_unlock(mddev);
4039 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07004040}
4041static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10004042__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4043 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07004044
NeilBrown9e653b62006-06-26 00:27:58 -07004045/*
4046 * The array state can be:
4047 *
4048 * clear
4049 * No devices, no size, no level
4050 * Equivalent to STOP_ARRAY ioctl
4051 * inactive
4052 * May have some settings, but array is not active
4053 * all IO results in error
4054 * When written, doesn't tear down array, but just stops it
4055 * suspended (not supported yet)
4056 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01004057 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07004058 * readonly
4059 * no resync can happen. no superblocks get written.
4060 * write requests fail
4061 * read-auto
4062 * like readonly, but behaves like 'clean' on a write request.
4063 *
4064 * clean - no pending writes, but otherwise active.
4065 * When written to inactive array, starts without resync
4066 * If a write request arrives then
4067 * if metadata is known, mark 'dirty' and switch to 'active'.
4068 * if not known, block and switch to write-pending
4069 * If written to an active array that has pending writes, then fails.
4070 * active
4071 * fully active: IO and resync can be happening.
4072 * When written to inactive array, starts with resync
4073 *
4074 * write-pending
4075 * clean, but writes are blocked waiting for 'active' to be written.
4076 *
4077 * active-idle
4078 * like active, but no writes have been seen for a while (100msec).
4079 *
4080 */
4081enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4082 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004083static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004084 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4085 "write-pending", "active-idle", NULL };
4086
4087static int match_word(const char *word, char **list)
4088{
4089 int n;
4090 for (n=0; list[n]; n++)
4091 if (cmd_match(word, list[n]))
4092 break;
4093 return n;
4094}
4095
4096static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004097array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004098{
4099 enum array_state st = inactive;
4100
4101 if (mddev->pers)
4102 switch(mddev->ro) {
4103 case 1:
4104 st = readonly;
4105 break;
4106 case 2:
4107 st = read_auto;
4108 break;
4109 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004110 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004111 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004112 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004113 else if (mddev->in_sync)
4114 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004115 else if (mddev->safemode)
4116 st = active_idle;
4117 else
4118 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004119 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004120 }
4121 else {
4122 if (list_empty(&mddev->disks) &&
4123 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004124 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004125 st = clear;
4126 else
4127 st = inactive;
4128 }
4129 return sprintf(page, "%s\n", array_states[st]);
4130}
4131
NeilBrownf72ffdd2014-09-30 14:23:59 +10004132static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4133static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4134static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004135static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004136
4137static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004138array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004139{
NeilBrown6497709b2017-03-15 14:05:14 +11004140 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004141 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004142
4143 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4144 /* don't take reconfig_mutex when toggling between
4145 * clean and active
4146 */
4147 spin_lock(&mddev->lock);
4148 if (st == active) {
4149 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004150 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004151 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004152 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004153 } else /* st == clean */ {
4154 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004155 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004156 err = -EBUSY;
4157 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004158 if (!err)
4159 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004160 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004161 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004162 }
4163 err = mddev_lock(mddev);
4164 if (err)
4165 return err;
4166 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004167 switch(st) {
4168 case bad_word:
4169 break;
4170 case clear:
4171 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004172 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004173 break;
4174 case inactive:
4175 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004176 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004177 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004178 else
NeilBrowne6910632008-02-06 01:39:51 -08004179 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004180 break;
4181 case suspended:
4182 break; /* not supported yet */
4183 case readonly:
4184 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004185 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004186 else {
4187 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004188 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004189 err = do_md_run(mddev);
4190 }
4191 break;
4192 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004193 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004194 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004195 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004196 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004197 err = restart_array(mddev);
4198 if (err == 0) {
4199 mddev->ro = 2;
4200 set_disk_ro(mddev->gendisk, 0);
4201 }
NeilBrown9e653b62006-06-26 00:27:58 -07004202 } else {
4203 mddev->ro = 2;
4204 err = do_md_run(mddev);
4205 }
4206 break;
4207 case clean:
4208 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004209 err = restart_array(mddev);
4210 if (err)
4211 break;
NeilBrown85572d72014-12-15 12:56:56 +11004212 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004213 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004214 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004215 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004216 } else
4217 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004218 break;
4219 case active:
4220 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004221 err = restart_array(mddev);
4222 if (err)
4223 break;
Shaohua Li29530792016-12-08 15:48:19 -08004224 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004225 wake_up(&mddev->sb_wait);
4226 err = 0;
4227 } else {
4228 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004229 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004230 err = do_md_run(mddev);
4231 }
4232 break;
4233 case write_pending:
4234 case active_idle:
4235 /* these cannot be set */
4236 break;
4237 }
NeilBrown67918752014-12-15 12:57:01 +11004238
4239 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004240 if (mddev->hold_active == UNTIL_IOCTL)
4241 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004242 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004243 }
NeilBrown67918752014-12-15 12:57:01 +11004244 mddev_unlock(mddev);
4245 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004246}
NeilBrown80ca3a42006-07-10 04:44:18 -07004247static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004248__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004249
NeilBrown6d7ff7382006-01-06 00:21:16 -08004250static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004251max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004252 return sprintf(page, "%d\n",
4253 atomic_read(&mddev->max_corr_read_errors));
4254}
4255
4256static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004257max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004258{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004259 unsigned int n;
4260 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004261
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004262 rv = kstrtouint(buf, 10, &n);
4263 if (rv < 0)
4264 return rv;
4265 atomic_set(&mddev->max_corr_read_errors, n);
4266 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004267}
4268
4269static struct md_sysfs_entry max_corr_read_errors =
4270__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4271 max_corrected_read_errors_store);
4272
4273static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004274null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004275{
4276 return -EINVAL;
4277}
4278
4279static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004280new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004281{
4282 /* buf must be %d:%d\n? giving major and minor numbers */
4283 /* The new device is added to the array.
4284 * If the array has a persistent superblock, we read the
4285 * superblock to initialise info and check validity.
4286 * Otherwise, only checking done is that in bind_rdev_to_array,
4287 * which mainly checks size.
4288 */
4289 char *e;
4290 int major = simple_strtoul(buf, &e, 10);
4291 int minor;
4292 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004293 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004294 int err;
4295
4296 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4297 return -EINVAL;
4298 minor = simple_strtoul(e+1, &e, 10);
4299 if (*e && *e != '\n')
4300 return -EINVAL;
4301 dev = MKDEV(major, minor);
4302 if (major != MAJOR(dev) ||
4303 minor != MINOR(dev))
4304 return -EOVERFLOW;
4305
NeilBrown67918752014-12-15 12:57:01 +11004306 flush_workqueue(md_misc_wq);
4307
4308 err = mddev_lock(mddev);
4309 if (err)
4310 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004311 if (mddev->persistent) {
4312 rdev = md_import_device(dev, mddev->major_version,
4313 mddev->minor_version);
4314 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004315 struct md_rdev *rdev0
4316 = list_entry(mddev->disks.next,
4317 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004318 err = super_types[mddev->major_version]
4319 .load_super(rdev, rdev0, mddev->minor_version);
4320 if (err < 0)
4321 goto out;
4322 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004323 } else if (mddev->external)
4324 rdev = md_import_device(dev, -2, -1);
4325 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004326 rdev = md_import_device(dev, -1, -1);
4327
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004328 if (IS_ERR(rdev)) {
4329 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004330 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004331 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004332 err = bind_rdev_to_array(rdev, mddev);
4333 out:
4334 if (err)
4335 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004336 mddev_unlock(mddev);
Alexey Obitotskiy5492c462017-07-28 15:49:25 +02004337 if (!err)
4338 md_new_event(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004339 return err ? err : len;
4340}
4341
4342static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004343__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004344
4345static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004346bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004347{
4348 char *end;
4349 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004350 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004351
NeilBrown67918752014-12-15 12:57:01 +11004352 err = mddev_lock(mddev);
4353 if (err)
4354 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004355 if (!mddev->bitmap)
4356 goto out;
4357 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4358 while (*buf) {
4359 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4360 if (buf == end) break;
4361 if (*end == '-') { /* range */
4362 buf = end + 1;
4363 end_chunk = simple_strtoul(buf, &end, 0);
4364 if (buf == end) break;
4365 }
4366 if (*end && !isspace(*end)) break;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004367 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004368 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004369 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004370 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
Paul Clements9b1d1da2006-10-03 01:15:49 -07004371out:
NeilBrown67918752014-12-15 12:57:01 +11004372 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004373 return len;
4374}
4375
4376static struct md_sysfs_entry md_bitmap =
4377__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4378
4379static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004380size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004381{
Andre Noll58c0fed2009-03-31 14:33:13 +11004382 return sprintf(page, "%llu\n",
4383 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004384}
4385
NeilBrownfd01b882011-10-11 16:47:53 +11004386static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004387
4388static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004389size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004390{
4391 /* If array is inactive, we can reduce the component size, but
4392 * not increase it (except from 0).
4393 * If array is active, we can try an on-line resize
4394 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004395 sector_t sectors;
4396 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004397
Andre Noll58c0fed2009-03-31 14:33:13 +11004398 if (err < 0)
4399 return err;
NeilBrown67918752014-12-15 12:57:01 +11004400 err = mddev_lock(mddev);
4401 if (err)
4402 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004403 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004404 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004405 if (err == 0)
4406 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004407 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004408 if (mddev->dev_sectors == 0 ||
4409 mddev->dev_sectors > sectors)
4410 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004411 else
4412 err = -ENOSPC;
4413 }
NeilBrown67918752014-12-15 12:57:01 +11004414 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004415 return err ? err : len;
4416}
4417
4418static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004419__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004420
Masanari Iida83f0d772012-10-30 00:18:08 +09004421/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004422 * This is one of
4423 * 'none' for arrays with no metadata (good luck...)
4424 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004425 * or N.M for internally known formats
4426 */
4427static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004428metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004429{
4430 if (mddev->persistent)
4431 return sprintf(page, "%d.%d\n",
4432 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004433 else if (mddev->external)
4434 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004435 else
4436 return sprintf(page, "none\n");
4437}
4438
4439static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004440metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004441{
4442 int major, minor;
4443 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004444 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004445 /* Changing the details of 'external' metadata is
4446 * always permitted. Otherwise there must be
4447 * no devices attached to the array.
4448 */
NeilBrown67918752014-12-15 12:57:01 +11004449
4450 err = mddev_lock(mddev);
4451 if (err)
4452 return err;
4453 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004454 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4455 ;
4456 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004457 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004458
NeilBrown67918752014-12-15 12:57:01 +11004459 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004460 if (cmd_match(buf, "none")) {
4461 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004462 mddev->external = 0;
4463 mddev->major_version = 0;
4464 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004465 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004466 }
4467 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004468 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004469 if (namelen >= sizeof(mddev->metadata_type))
4470 namelen = sizeof(mddev->metadata_type)-1;
4471 strncpy(mddev->metadata_type, buf+9, namelen);
4472 mddev->metadata_type[namelen] = 0;
4473 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4474 mddev->metadata_type[--namelen] = 0;
4475 mddev->persistent = 0;
4476 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004477 mddev->major_version = 0;
4478 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004479 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004480 }
4481 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004482 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004483 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004484 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004485 buf = e+1;
4486 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004487 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004488 goto out_unlock;
4489 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004490 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004491 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004492 mddev->major_version = major;
4493 mddev->minor_version = minor;
4494 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004495 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004496 err = 0;
4497out_unlock:
4498 mddev_unlock(mddev);
4499 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004500}
4501
4502static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004503__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004504
NeilBrowna35b0d62006-01-06 00:20:49 -08004505static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004506action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004507{
NeilBrown7eec3142005-11-08 21:39:44 -08004508 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004509 unsigned long recovery = mddev->recovery;
4510 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004511 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004512 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4513 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4514 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004515 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004516 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4517 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004518 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004519 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004520 type = "check";
4521 else
4522 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004523 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004524 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004525 else if (mddev->reshape_position != MaxSector)
4526 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004527 }
4528 return sprintf(page, "%s\n", type);
4529}
4530
4531static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004532action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004533{
NeilBrown7eec3142005-11-08 21:39:44 -08004534 if (!mddev->pers || !mddev->pers->sync_request)
4535 return -EINVAL;
4536
NeilBrownb6a9ce62009-05-26 09:41:17 +10004537
4538 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004539 if (cmd_match(page, "frozen"))
4540 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4541 else
4542 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004543 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4544 mddev_lock(mddev) == 0) {
4545 flush_workqueue(md_misc_wq);
4546 if (mddev->sync_thread) {
4547 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004548 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004549 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004550 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004551 }
NeilBrown312045e2015-12-21 11:01:21 +11004552 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004553 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004554 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004555 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004556 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004557 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004558 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004559 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004560 int err;
4561 if (mddev->pers->start_reshape == NULL)
4562 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004563 err = mddev_lock(mddev);
4564 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004565 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4566 err = -EBUSY;
4567 else {
4568 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4569 err = mddev->pers->start_reshape(mddev);
4570 }
NeilBrown67918752014-12-15 12:57:01 +11004571 mddev_unlock(mddev);
4572 }
NeilBrown16484bf2006-03-27 01:18:13 -08004573 if (err)
4574 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004575 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004576 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004577 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004578 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004579 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004580 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004581 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004582 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4583 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004584 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004585 if (mddev->ro == 2) {
4586 /* A write to sync_action is enough to justify
4587 * canceling read-auto mode
4588 */
4589 mddev->ro = 0;
4590 md_wakeup_thread(mddev->sync_thread);
4591 }
NeilBrown03c902e2006-01-06 00:20:46 -08004592 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004593 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004594 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004595 return len;
4596}
4597
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004598static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004599__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004600
4601static ssize_t
4602last_sync_action_show(struct mddev *mddev, char *page)
4603{
4604 return sprintf(page, "%s\n", mddev->last_sync_action);
4605}
4606
4607static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4608
NeilBrown9d888832005-11-08 21:39:26 -08004609static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004610mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004611{
4612 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004613 (unsigned long long)
4614 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004615}
4616
NeilBrown80ca3a42006-07-10 04:44:18 -07004617static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004618
NeilBrown88202a02006-01-06 00:21:36 -08004619static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004620sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004621{
4622 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4623 mddev->sync_speed_min ? "local": "system");
4624}
4625
4626static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004627sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004628{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004629 unsigned int min;
4630 int rv;
4631
NeilBrown88202a02006-01-06 00:21:36 -08004632 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004633 min = 0;
4634 } else {
4635 rv = kstrtouint(buf, 10, &min);
4636 if (rv < 0)
4637 return rv;
4638 if (min == 0)
4639 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004640 }
NeilBrown88202a02006-01-06 00:21:36 -08004641 mddev->sync_speed_min = min;
4642 return len;
4643}
4644
4645static struct md_sysfs_entry md_sync_min =
4646__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4647
4648static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004649sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004650{
4651 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4652 mddev->sync_speed_max ? "local": "system");
4653}
4654
4655static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004656sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004657{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004658 unsigned int max;
4659 int rv;
4660
NeilBrown88202a02006-01-06 00:21:36 -08004661 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004662 max = 0;
4663 } else {
4664 rv = kstrtouint(buf, 10, &max);
4665 if (rv < 0)
4666 return rv;
4667 if (max == 0)
4668 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004669 }
NeilBrown88202a02006-01-06 00:21:36 -08004670 mddev->sync_speed_max = max;
4671 return len;
4672}
4673
4674static struct md_sysfs_entry md_sync_max =
4675__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4676
Iustin Popd7f3d292007-10-16 23:30:54 -07004677static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004678degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004679{
4680 return sprintf(page, "%d\n", mddev->degraded);
4681}
4682static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004683
4684static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004685sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004686{
4687 return sprintf(page, "%d\n", mddev->parallel_resync);
4688}
4689
4690static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004691sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004692{
4693 long n;
4694
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004695 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004696 return -EINVAL;
4697
4698 if (n != 0 && n != 1)
4699 return -EINVAL;
4700
4701 mddev->parallel_resync = n;
4702
4703 if (mddev->sync_thread)
4704 wake_up(&resync_wait);
4705
4706 return len;
4707}
4708
4709/* force parallel resync, even with shared block devices */
4710static struct md_sysfs_entry md_sync_force_parallel =
4711__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4712 sync_force_parallel_show, sync_force_parallel_store);
4713
4714static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004715sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004716{
4717 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004718 if (mddev->curr_resync == 0)
4719 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004720 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4721 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004722 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004723 db = resync - mddev->resync_mark_cnt;
4724 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004725}
4726
NeilBrown80ca3a42006-07-10 04:44:18 -07004727static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004728
4729static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004730sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004731{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004732 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004733
NeilBrownacb180b2009-04-14 16:28:34 +10004734 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4735 return sprintf(page, "none\n");
4736
NeilBrown72f36d52012-10-11 14:25:57 +11004737 if (mddev->curr_resync == 1 ||
4738 mddev->curr_resync == 2)
4739 return sprintf(page, "delayed\n");
4740
NeilBrownc804cde2012-05-21 09:28:33 +10004741 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4742 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004743 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004744 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004745 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004746
NeilBrownacb180b2009-04-14 16:28:34 +10004747 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004748 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004749}
4750
NeilBrown750f1992014-09-30 08:53:05 +10004751static struct md_sysfs_entry md_sync_completed =
4752 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004753
NeilBrowne464eaf2006-03-27 01:18:14 -08004754static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004755min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004756{
4757 return sprintf(page, "%llu\n",
4758 (unsigned long long)mddev->resync_min);
4759}
4760static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004761min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004762{
4763 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004764 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004765
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004766 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004767 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004768
4769 spin_lock(&mddev->lock);
4770 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004771 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004772 goto out_unlock;
4773
4774 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004775 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004776 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004777
NeilBrown50c37b12015-03-23 17:36:38 +11004778 /* Round down to multiple of 4K for safety */
4779 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004780 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004781
NeilBrown23da4222014-12-15 12:57:01 +11004782out_unlock:
4783 spin_unlock(&mddev->lock);
4784 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004785}
4786
4787static struct md_sysfs_entry md_min_sync =
4788__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4789
4790static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004791max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004792{
4793 if (mddev->resync_max == MaxSector)
4794 return sprintf(page, "max\n");
4795 else
4796 return sprintf(page, "%llu\n",
4797 (unsigned long long)mddev->resync_max);
4798}
4799static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004800max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004801{
NeilBrown23da4222014-12-15 12:57:01 +11004802 int err;
4803 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004804 if (strncmp(buf, "max", 3) == 0)
4805 mddev->resync_max = MaxSector;
4806 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004807 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004808 int chunk;
4809
4810 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004811 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004812 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004813 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004814 goto out_unlock;
4815
4816 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004817 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004818 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004819 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004820 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004821
4822 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004823 chunk = mddev->chunk_sectors;
4824 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004825 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004826
4827 err = -EINVAL;
4828 if (sector_div(temp, chunk))
4829 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004830 }
4831 mddev->resync_max = max;
4832 }
4833 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004834 err = 0;
4835out_unlock:
4836 spin_unlock(&mddev->lock);
4837 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004838}
4839
4840static struct md_sysfs_entry md_max_sync =
4841__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4842
4843static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004844suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004845{
4846 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4847}
4848
4849static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004850suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004851{
NeilBrownb03e0cc2017-10-19 12:49:15 +11004852 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004853 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004854
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004855 err = kstrtoull(buf, 10, &new);
4856 if (err < 0)
4857 return err;
4858 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004859 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004860
NeilBrown67918752014-12-15 12:57:01 +11004861 err = mddev_lock(mddev);
4862 if (err)
4863 return err;
4864 err = -EINVAL;
4865 if (mddev->pers == NULL ||
4866 mddev->pers->quiesce == NULL)
4867 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004868 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11004869 mddev->suspend_lo = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004870 mddev_resume(mddev);
4871
NeilBrown67918752014-12-15 12:57:01 +11004872 err = 0;
4873unlock:
4874 mddev_unlock(mddev);
4875 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004876}
4877static struct md_sysfs_entry md_suspend_lo =
4878__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4879
NeilBrowne464eaf2006-03-27 01:18:14 -08004880static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004881suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004882{
4883 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4884}
4885
4886static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004887suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004888{
NeilBrownb03e0cc2017-10-19 12:49:15 +11004889 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004890 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004891
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004892 err = kstrtoull(buf, 10, &new);
4893 if (err < 0)
4894 return err;
4895 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004896 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004897
NeilBrown67918752014-12-15 12:57:01 +11004898 err = mddev_lock(mddev);
4899 if (err)
4900 return err;
4901 err = -EINVAL;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004902 if (mddev->pers == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004903 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004904
4905 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11004906 mddev->suspend_hi = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004907 mddev_resume(mddev);
4908
NeilBrown67918752014-12-15 12:57:01 +11004909 err = 0;
4910unlock:
4911 mddev_unlock(mddev);
4912 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004913}
4914static struct md_sysfs_entry md_suspend_hi =
4915__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4916
NeilBrown08a02ec2007-05-09 02:35:38 -07004917static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004918reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004919{
4920 if (mddev->reshape_position != MaxSector)
4921 return sprintf(page, "%llu\n",
4922 (unsigned long long)mddev->reshape_position);
4923 strcpy(page, "none\n");
4924 return 5;
4925}
4926
4927static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004928reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004929{
NeilBrownc6563a82012-05-21 09:27:00 +10004930 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004931 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004932 int err;
NeilBrown67918752014-12-15 12:57:01 +11004933
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004934 err = kstrtoull(buf, 10, &new);
4935 if (err < 0)
4936 return err;
4937 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004938 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004939 err = mddev_lock(mddev);
4940 if (err)
4941 return err;
4942 err = -EBUSY;
4943 if (mddev->pers)
4944 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004945 mddev->reshape_position = new;
4946 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004947 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004948 mddev->new_level = mddev->level;
4949 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004950 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004951 rdev_for_each(rdev, mddev)
4952 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004953 err = 0;
4954unlock:
4955 mddev_unlock(mddev);
4956 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004957}
4958
4959static struct md_sysfs_entry md_reshape_position =
4960__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4961 reshape_position_store);
4962
Dan Williamsb522adc2009-03-31 15:00:31 +11004963static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004964reshape_direction_show(struct mddev *mddev, char *page)
4965{
4966 return sprintf(page, "%s\n",
4967 mddev->reshape_backwards ? "backwards" : "forwards");
4968}
4969
4970static ssize_t
4971reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4972{
4973 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004974 int err;
4975
NeilBrown2c810cd2012-05-21 09:27:00 +10004976 if (cmd_match(buf, "forwards"))
4977 backwards = 0;
4978 else if (cmd_match(buf, "backwards"))
4979 backwards = 1;
4980 else
4981 return -EINVAL;
4982 if (mddev->reshape_backwards == backwards)
4983 return len;
4984
NeilBrown67918752014-12-15 12:57:01 +11004985 err = mddev_lock(mddev);
4986 if (err)
4987 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004988 /* check if we are allowed to change */
4989 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004990 err = -EBUSY;
4991 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004992 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004993 err = -EINVAL;
4994 else
4995 mddev->reshape_backwards = backwards;
4996 mddev_unlock(mddev);
4997 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004998}
4999
5000static struct md_sysfs_entry md_reshape_direction =
5001__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5002 reshape_direction_store);
5003
5004static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005005array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11005006{
5007 if (mddev->external_size)
5008 return sprintf(page, "%llu\n",
5009 (unsigned long long)mddev->array_sectors/2);
5010 else
5011 return sprintf(page, "default\n");
5012}
5013
5014static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005015array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11005016{
5017 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11005018 int err;
5019
5020 err = mddev_lock(mddev);
5021 if (err)
5022 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11005023
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005024 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08005025 if (mddev_is_clustered(mddev)) {
5026 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005027 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08005028 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005029
Dan Williamsb522adc2009-03-31 15:00:31 +11005030 if (strncmp(buf, "default", 7) == 0) {
5031 if (mddev->pers)
5032 sectors = mddev->pers->size(mddev, 0, 0);
5033 else
5034 sectors = mddev->array_sectors;
5035
5036 mddev->external_size = 0;
5037 } else {
5038 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11005039 err = -EINVAL;
5040 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5041 err = -E2BIG;
5042 else
5043 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11005044 }
5045
NeilBrown67918752014-12-15 12:57:01 +11005046 if (!err) {
5047 mddev->array_sectors = sectors;
5048 if (mddev->pers) {
5049 set_capacity(mddev->gendisk, mddev->array_sectors);
5050 revalidate_disk(mddev->gendisk);
5051 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11005052 }
NeilBrown67918752014-12-15 12:57:01 +11005053 mddev_unlock(mddev);
5054 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11005055}
5056
5057static struct md_sysfs_entry md_array_size =
5058__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5059 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005060
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005061static ssize_t
5062consistency_policy_show(struct mddev *mddev, char *page)
5063{
5064 int ret;
5065
5066 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5067 ret = sprintf(page, "journal\n");
5068 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5069 ret = sprintf(page, "ppl\n");
5070 } else if (mddev->bitmap) {
5071 ret = sprintf(page, "bitmap\n");
5072 } else if (mddev->pers) {
5073 if (mddev->pers->sync_request)
5074 ret = sprintf(page, "resync\n");
5075 else
5076 ret = sprintf(page, "none\n");
5077 } else {
5078 ret = sprintf(page, "unknown\n");
5079 }
5080
5081 return ret;
5082}
5083
5084static ssize_t
5085consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5086{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005087 int err = 0;
5088
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005089 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005090 if (mddev->pers->change_consistency_policy)
5091 err = mddev->pers->change_consistency_policy(mddev, buf);
5092 else
5093 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005094 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5095 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005096 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005097 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005098 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005099
5100 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005101}
5102
5103static struct md_sysfs_entry md_consistency_policy =
5104__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5105 consistency_policy_store);
5106
NeilBrowneae17012005-11-08 21:39:23 -08005107static struct attribute *md_default_attrs[] = {
5108 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005109 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005110 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005111 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005112 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005113 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005114 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005115 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005116 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005117 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005118 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005119 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005120 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005121 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005122 &md_consistency_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005123 NULL,
5124};
5125
5126static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005127 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005128 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005129 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005130 &md_sync_min.attr,
5131 &md_sync_max.attr,
5132 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005133 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005134 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005135 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005136 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005137 &md_suspend_lo.attr,
5138 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005139 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005140 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005141 NULL,
5142};
NeilBrown411036f2005-11-08 21:39:40 -08005143static struct attribute_group md_redundancy_group = {
5144 .name = NULL,
5145 .attrs = md_redundancy_attrs,
5146};
5147
NeilBrowneae17012005-11-08 21:39:23 -08005148static ssize_t
5149md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5150{
5151 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005152 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005153 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005154
5155 if (!entry->show)
5156 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005157 spin_lock(&all_mddevs_lock);
5158 if (list_empty(&mddev->all_mddevs)) {
5159 spin_unlock(&all_mddevs_lock);
5160 return -EBUSY;
5161 }
5162 mddev_get(mddev);
5163 spin_unlock(&all_mddevs_lock);
5164
NeilBrownb7b17c92014-12-15 12:56:59 +11005165 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005166 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005167 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005168}
5169
5170static ssize_t
5171md_attr_store(struct kobject *kobj, struct attribute *attr,
5172 const char *page, size_t length)
5173{
5174 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005175 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005176 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005177
5178 if (!entry->store)
5179 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005180 if (!capable(CAP_SYS_ADMIN))
5181 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005182 spin_lock(&all_mddevs_lock);
5183 if (list_empty(&mddev->all_mddevs)) {
5184 spin_unlock(&all_mddevs_lock);
5185 return -EBUSY;
5186 }
5187 mddev_get(mddev);
5188 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005189 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005190 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005191 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005192}
5193
5194static void md_free(struct kobject *ko)
5195{
NeilBrownfd01b882011-10-11 16:47:53 +11005196 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005197
5198 if (mddev->sysfs_state)
5199 sysfs_put(mddev->sysfs_state);
5200
Bart Van Assched8115c352018-02-28 10:15:29 -08005201 if (mddev->gendisk)
5202 del_gendisk(mddev->gendisk);
NeilBrown6cd18e72015-04-27 14:12:22 +10005203 if (mddev->queue)
5204 blk_cleanup_queue(mddev->queue);
Bart Van Assched8115c352018-02-28 10:15:29 -08005205 if (mddev->gendisk)
NeilBrowna21d1502009-01-09 08:31:09 +11005206 put_disk(mddev->gendisk);
NeilBrown4ad23a972017-03-15 14:05:14 +11005207 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005208
Kent Overstreet28dec872018-06-07 20:52:54 -04005209 bioset_exit(&mddev->bio_set);
5210 bioset_exit(&mddev->sync_set);
NeilBrowneae17012005-11-08 21:39:23 -08005211 kfree(mddev);
5212}
5213
Emese Revfy52cf25d2010-01-19 02:58:23 +01005214static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005215 .show = md_attr_show,
5216 .store = md_attr_store,
5217};
5218static struct kobj_type md_ktype = {
5219 .release = md_free,
5220 .sysfs_ops = &md_sysfs_ops,
5221 .default_attrs = md_default_attrs,
5222};
5223
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224int mdp_major = 0;
5225
Dan Williams5fd3a172009-03-04 00:57:25 -07005226static void mddev_delayed_delete(struct work_struct *ws)
5227{
NeilBrownfd01b882011-10-11 16:47:53 +11005228 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005229
NeilBrown43a70502009-12-14 12:49:55 +11005230 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005231 kobject_del(&mddev->kobj);
5232 kobject_put(&mddev->kobj);
5233}
5234
NeilBrown4ad23a972017-03-15 14:05:14 +11005235static void no_op(struct percpu_ref *r) {}
5236
NeilBrowna415c0f2017-06-05 16:05:13 +10005237int mddev_init_writes_pending(struct mddev *mddev)
5238{
5239 if (mddev->writes_pending.percpu_count_ptr)
5240 return 0;
5241 if (percpu_ref_init(&mddev->writes_pending, no_op, 0, GFP_KERNEL) < 0)
5242 return -ENOMEM;
5243 /* We want to start with the refcount at zero */
5244 percpu_ref_put(&mddev->writes_pending);
5245 return 0;
5246}
5247EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5248
NeilBrownefeb53c2009-01-09 08:31:10 +11005249static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250{
NeilBrown039b7222017-04-12 16:26:13 +10005251 /*
5252 * If dev is zero, name is the name of a device to allocate with
5253 * an arbitrary minor number. It will be "md_???"
5254 * If dev is non-zero it must be a device number with a MAJOR of
5255 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5256 * the device is being created by opening a node in /dev.
5257 * If "name" is not NULL, the device is being created by
5258 * writing to /sys/module/md_mod/parameters/new_array.
5259 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005260 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005261 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005263 int partitioned;
5264 int shift;
5265 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005266 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267
5268 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005269 return -ENODEV;
5270
5271 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5272 shift = partitioned ? MdpMinorShift : 0;
5273 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274
Tejun Heoe804ac72010-10-15 15:36:08 +02005275 /* wait for any previous instance of this device to be
5276 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005277 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005278 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005279
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005280 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005281 error = -EEXIST;
5282 if (mddev->gendisk)
5283 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005284
NeilBrown039b7222017-04-12 16:26:13 +10005285 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005286 /* Need to ensure that 'name' is not a duplicate.
5287 */
NeilBrownfd01b882011-10-11 16:47:53 +11005288 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005289 spin_lock(&all_mddevs_lock);
5290
5291 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5292 if (mddev2->gendisk &&
5293 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5294 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005295 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005296 }
5297 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 }
NeilBrown039b7222017-04-12 16:26:13 +10005299 if (name && dev)
5300 /*
5301 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5302 */
5303 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005304
NeilBrown0909dc42009-07-01 12:27:21 +10005305 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005306 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005307 if (!mddev->queue)
5308 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005309 mddev->queue->queuedata = mddev;
5310
NeilBrown409c57f2009-03-31 14:39:39 +11005311 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005312 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005313
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 disk = alloc_disk(1 << shift);
5315 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005316 blk_cleanup_queue(mddev->queue);
5317 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005318 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005320 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005322 if (name)
5323 strcpy(disk->disk_name, name);
5324 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005326 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 disk->fops = &md_fops;
5329 disk->private_data = mddev;
5330 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005331 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005332 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005333 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005334 * remove it now.
5335 */
5336 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005338 /* As soon as we call add_disk(), another thread could get
5339 * through to md_open, so make sure it doesn't get too far
5340 */
5341 mutex_lock(&mddev->open_mutex);
5342 add_disk(disk);
5343
Kent Overstreet28dec872018-06-07 20:52:54 -04005344 error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005345 if (error) {
5346 /* This isn't possible, but as kobject_init_and_add is marked
5347 * __must_check, we must do something with the result
5348 */
NeilBrown9d487392016-11-02 14:16:49 +11005349 pr_debug("md: cannot register %s/md - name in use\n",
5350 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005351 error = 0;
5352 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005353 if (mddev->kobj.sd &&
5354 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005355 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005356 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005357 abort:
5358 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005359 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005360 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005361 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005362 }
NeilBrownd3374822009-01-09 08:31:10 +11005363 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005364 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005365}
5366
5367static struct kobject *md_probe(dev_t dev, int *part, void *data)
5368{
NeilBrown78b63502017-04-12 16:26:13 +10005369 if (create_on_open)
5370 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 return NULL;
5372}
5373
Kees Cooke4dca7b2017-10-17 19:04:42 -07005374static int add_named_array(const char *val, const struct kernel_param *kp)
NeilBrownefeb53c2009-01-09 08:31:10 +11005375{
NeilBrown039b7222017-04-12 16:26:13 +10005376 /*
5377 * val must be "md_*" or "mdNNN".
5378 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005379 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005380 * For "mdNNN" we allocate an array with the minor number NNN
5381 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005382 */
5383 int len = strlen(val);
5384 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005385 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005386
5387 while (len && val[len-1] == '\n')
5388 len--;
5389 if (len >= DISK_NAME_LEN)
5390 return -E2BIG;
5391 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005392 if (strncmp(buf, "md_", 3) == 0)
5393 return md_alloc(0, buf);
5394 if (strncmp(buf, "md", 2) == 0 &&
5395 isdigit(buf[2]) &&
5396 kstrtoul(buf+2, 10, &devnum) == 0 &&
5397 devnum <= MINORMASK)
5398 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5399
5400 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005401}
5402
Kees Cook8376d3c2017-10-16 17:01:48 -07005403static void md_safemode_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404{
Kees Cook8376d3c2017-10-16 17:01:48 -07005405 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
NeilBrown4ad23a972017-03-15 14:05:14 +11005407 mddev->safemode = 1;
5408 if (mddev->external)
5409 sysfs_notify_dirent_safe(mddev->sysfs_state);
5410
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 md_wakeup_thread(mddev->thread);
5412}
5413
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005414static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415
NeilBrownfd01b882011-10-11 16:47:53 +11005416int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417{
NeilBrown2604b702006-01-06 00:20:36 -08005418 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005419 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005420 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421
NeilBrowna757e642005-04-16 15:26:42 -07005422 if (list_empty(&mddev->disks))
5423 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
5426 if (mddev->pers)
5427 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005428 /* Cannot run until previous stop completes properly */
5429 if (mddev->sysfs_active)
5430 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005431
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 /*
5433 * Analyze all RAID superblock(s)
5434 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005435 if (!mddev->raid_disks) {
5436 if (!mddev->persistent)
5437 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005438 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
NeilBrownd9d166c2006-01-06 00:20:51 -08005441 if (mddev->level != LEVEL_NONE)
5442 request_module("md-level-%d", mddev->level);
5443 else if (mddev->clevel[0])
5444 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
5446 /*
5447 * Drop all container device buffers, from now on
5448 * the only valid external interface is through the md
5449 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 */
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005451 mddev->has_superblocks = false;
NeilBrowndafb20f2012-03-19 12:46:39 +11005452 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005453 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 continue;
5455 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005456 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005457 if (mddev->ro != 1 &&
5458 (bdev_read_only(rdev->bdev) ||
5459 bdev_read_only(rdev->meta_bdev))) {
5460 mddev->ro = 1;
5461 if (mddev->gendisk)
5462 set_disk_ro(mddev->gendisk, 1);
5463 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005464
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005465 if (rdev->sb_page)
5466 mddev->has_superblocks = true;
5467
NeilBrownf0d76d72007-07-17 04:06:12 -07005468 /* perform some consistency tests on the device.
5469 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005470 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005471 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005472 if (rdev->meta_bdev) {
5473 /* Nothing to check */;
5474 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005475 if (mddev->dev_sectors &&
5476 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005477 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005478 pr_warn("md: %s: data overlaps metadata\n",
5479 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005480 return -EINVAL;
5481 }
5482 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005483 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005484 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005485 pr_warn("md: %s: metadata overlaps data\n",
5486 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005487 return -EINVAL;
5488 }
5489 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005490 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 }
5492
Kent Overstreetafeee512018-05-20 18:25:52 -04005493 if (!bioset_initialized(&mddev->bio_set)) {
5494 err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5495 if (err)
5496 return err;
Ming Lei10273172017-02-14 23:29:00 +08005497 }
Kent Overstreetafeee512018-05-20 18:25:52 -04005498 if (!bioset_initialized(&mddev->sync_set)) {
5499 err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5500 if (err)
Kent Overstreet28dec872018-06-07 20:52:54 -04005501 return err;
NeilBrown5a850712017-06-21 09:12:21 +10005502 }
NeilBrowna167f662010-10-26 18:31:13 +11005503
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005505 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005506 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005508 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005509 pr_warn("md: personality for level %d is not loaded!\n",
5510 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005511 else
NeilBrown9d487392016-11-02 14:16:49 +11005512 pr_warn("md: personality for level %s is not loaded!\n",
5513 mddev->clevel);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005514 err = -EINVAL;
5515 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005518 if (mddev->level != pers->level) {
5519 mddev->level = pers->level;
5520 mddev->new_level = pers->level;
5521 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005522 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
NeilBrownf6705572006-03-27 01:18:11 -08005524 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005525 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005526 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005527 module_put(pers->owner);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005528 err = -EINVAL;
5529 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08005530 }
5531
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005532 if (pers->sync_request) {
5533 /* Warn if this is a potentially silly
5534 * configuration.
5535 */
5536 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005537 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005538 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005539
NeilBrowndafb20f2012-03-19 12:46:39 +11005540 rdev_for_each(rdev, mddev)
5541 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005542 if (rdev < rdev2 &&
5543 rdev->bdev->bd_contains ==
5544 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005545 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5546 mdname(mddev),
5547 bdevname(rdev->bdev,b),
5548 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005549 warned = 1;
5550 }
5551 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005552
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005553 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005554 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005555 }
5556
NeilBrown657390d2005-08-26 18:34:16 -07005557 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005558 /* may be over-ridden by personality */
5559 mddev->resync_max_sectors = mddev->dev_sectors;
5560
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005561 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562
NeilBrown0f9552b52009-12-30 12:08:50 +11005563 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005564 mddev->ro = 2; /* read-only, but switch on first write */
5565
NeilBrown36d091f2014-12-15 12:56:58 +11005566 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005567 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005568 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005569 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005570 WARN_ONCE(!mddev->external_size,
5571 "%s: default size too small, but 'external_size' not in effect?\n",
5572 __func__);
5573 pr_warn("md: invalid array_size %llu > default size %llu\n",
5574 (unsigned long long)mddev->array_sectors / 2,
5575 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005576 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005577 }
NeilBrown36d091f2014-12-15 12:56:58 +11005578 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005579 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005580 struct bitmap *bitmap;
5581
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005582 bitmap = md_bitmap_create(mddev, -1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005583 if (IS_ERR(bitmap)) {
5584 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005585 pr_warn("%s: failed to create bitmap (%d)\n",
5586 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005587 } else
5588 mddev->bitmap = bitmap;
5589
NeilBrownb15c2e52006-01-06 00:20:16 -08005590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005592 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005593 if (mddev->private)
5594 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005595 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005596 module_put(pers->owner);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005597 md_bitmap_destroy(mddev);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005598 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 }
NeilBrown5c675f82014-12-15 12:56:56 +11005600 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005601 bool nonrot = true;
5602
5603 rdev_for_each(rdev, mddev) {
5604 if (rdev->raid_disk >= 0 &&
5605 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5606 nonrot = false;
5607 break;
5608 }
5609 }
5610 if (mddev->degraded)
5611 nonrot = false;
5612 if (nonrot)
Bart Van Assche8b904b52018-03-07 17:10:10 -08005613 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
Shaohua Libb086a82016-09-30 09:45:40 -07005614 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08005615 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005616 mddev->queue->backing_dev_info->congested_data = mddev;
5617 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005618 }
NeilBrown36d091f2014-12-15 12:56:58 +11005619 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005620 if (mddev->kobj.sd &&
5621 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005622 pr_warn("md: cannot register extra attributes for %s\n",
5623 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005624 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005625 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005626 mddev->ro = 0;
5627
Robert Becker1e509152009-12-14 12:49:58 +11005628 atomic_set(&mddev->max_corr_read_errors,
5629 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005631 if (mddev_is_clustered(mddev))
5632 mddev->safemode_delay = 0;
5633 else
5634 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005636 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005637 spin_lock(&mddev->lock);
5638 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005639 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005640 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005641 if (rdev->raid_disk >= 0)
5642 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005643 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005644
NeilBrowna4a3d262015-07-17 11:57:30 +10005645 if (mddev->degraded && !mddev->ro)
5646 /* This ensures that recovering status is reported immediately
5647 * via sysfs - until a lack of spares is confirmed.
5648 */
5649 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005651
Shaohua Li29530792016-12-08 15:48:19 -08005652 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07005653 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654
NeilBrownd7603b72006-01-06 00:20:30 -08005655 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005656 sysfs_notify_dirent_safe(mddev->sysfs_state);
5657 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005658 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 return 0;
Xiao Nib1261942018-01-24 12:17:38 +08005660
5661abort:
NeilBrown4bc034d2019-03-29 10:46:16 -07005662 bioset_exit(&mddev->bio_set);
5663 bioset_exit(&mddev->sync_set);
Xiao Nib1261942018-01-24 12:17:38 +08005664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665}
NeilBrown390ee602010-06-01 19:37:27 +10005666EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
NeilBrownfd01b882011-10-11 16:47:53 +11005668static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005669{
5670 int err;
5671
5672 err = md_run(mddev);
5673 if (err)
5674 goto out;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005675 err = md_bitmap_load(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005676 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005677 md_bitmap_destroy(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005678 goto out;
5679 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005680
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005681 if (mddev_is_clustered(mddev))
5682 md_allow_write(mddev);
5683
Song Liud5d885f2017-11-19 22:17:01 -08005684 /* run start up tasks that require md_thread */
5685 md_start(mddev);
5686
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005687 md_wakeup_thread(mddev->thread);
5688 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5689
NeilBrownfe60b012010-03-29 11:10:42 +11005690 set_capacity(mddev->gendisk, mddev->array_sectors);
5691 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005692 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005693 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5694out:
5695 return err;
5696}
5697
Song Liud5d885f2017-11-19 22:17:01 -08005698int md_start(struct mddev *mddev)
5699{
5700 int ret = 0;
5701
5702 if (mddev->pers->start) {
5703 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
5704 md_wakeup_thread(mddev->thread);
5705 ret = mddev->pers->start(mddev);
5706 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
5707 md_wakeup_thread(mddev->sync_thread);
5708 }
5709 return ret;
5710}
5711EXPORT_SYMBOL_GPL(md_start);
5712
NeilBrownfd01b882011-10-11 16:47:53 +11005713static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714{
5715 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10005716 struct md_rdev *rdev;
5717 bool has_journal = false;
5718 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719
Andre Noll80fab1d2008-07-11 22:02:21 +10005720 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005722 return -ENXIO;
5723 if (!mddev->pers)
5724 return -EINVAL;
5725 if (!mddev->ro)
5726 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005727
NeilBrown97b20ef2017-04-13 08:53:48 +10005728 rcu_read_lock();
5729 rdev_for_each_rcu(rdev, mddev) {
5730 if (test_bit(Journal, &rdev->flags) &&
5731 !test_bit(Faulty, &rdev->flags))
5732 has_journal = true;
5733 if (bdev_read_only(rdev->bdev))
5734 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07005735 }
NeilBrown97b20ef2017-04-13 08:53:48 +10005736 rcu_read_unlock();
5737 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
5738 /* Don't restart rw with journal missing/faulty */
5739 return -EINVAL;
5740 if (has_readonly)
5741 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07005742
Andre Noll80fab1d2008-07-11 22:02:21 +10005743 mddev->safemode = 0;
5744 mddev->ro = 0;
5745 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11005746 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10005747 /* Kick recovery or resync if necessary */
5748 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5749 md_wakeup_thread(mddev->thread);
5750 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005751 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753}
5754
NeilBrownfd01b882011-10-11 16:47:53 +11005755static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005756{
5757 mddev->array_sectors = 0;
5758 mddev->external_size = 0;
5759 mddev->dev_sectors = 0;
5760 mddev->raid_disks = 0;
5761 mddev->recovery_cp = 0;
5762 mddev->resync_min = 0;
5763 mddev->resync_max = MaxSector;
5764 mddev->reshape_position = MaxSector;
5765 mddev->external = 0;
5766 mddev->persistent = 0;
5767 mddev->level = LEVEL_NONE;
5768 mddev->clevel[0] = 0;
5769 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08005770 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005771 mddev->ro = 0;
5772 mddev->metadata_type[0] = 0;
5773 mddev->chunk_sectors = 0;
5774 mddev->ctime = mddev->utime = 0;
5775 mddev->layout = 0;
5776 mddev->max_disks = 0;
5777 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005778 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005779 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005780 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005781 mddev->new_level = LEVEL_NONE;
5782 mddev->new_layout = 0;
5783 mddev->new_chunk_sectors = 0;
5784 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005785 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005786 mddev->suspend_lo = mddev->suspend_hi = 0;
5787 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5788 mddev->recovery = 0;
5789 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005790 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005791 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005792 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005793 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005794 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005795 mddev->bitmap_info.offset = 0;
5796 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005797 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005798 mddev->bitmap_info.chunksize = 0;
5799 mddev->bitmap_info.daemon_sleep = 0;
5800 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005801 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005802}
5803
NeilBrownfd01b882011-10-11 16:47:53 +11005804static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005805{
NeilBrown6b6204e2013-05-09 09:48:30 +10005806 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005807 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005808 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005809 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005810 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005811 }
5812
5813 del_timer_sync(&mddev->safemode_timer);
5814
Shaohua Li034e33f2016-11-21 10:29:19 -08005815 if (mddev->pers && mddev->pers->quiesce) {
5816 mddev->pers->quiesce(mddev, 1);
5817 mddev->pers->quiesce(mddev, 0);
5818 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005819 md_bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005820
NeilBrownb6d428c2013-04-24 11:42:42 +10005821 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005822 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08005823 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11005824 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005825 if (!mddev_is_clustered(mddev))
5826 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005827 md_update_sb(mddev, 1);
5828 }
5829}
NeilBrowndefad612011-01-14 09:14:33 +11005830
NeilBrownfd01b882011-10-11 16:47:53 +11005831void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005832{
NeilBrown29f097c2013-11-14 17:54:51 +11005833 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005834 __md_stop_writes(mddev);
5835 mddev_unlock(mddev);
5836}
NeilBrown390ee602010-06-01 19:37:27 +10005837EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005838
NeilBrown5aa61f42014-12-15 12:56:57 +11005839static void mddev_detach(struct mddev *mddev)
5840{
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005841 md_bitmap_wait_behind_writes(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005842 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005843 mddev->pers->quiesce(mddev, 1);
5844 mddev->pers->quiesce(mddev, 0);
5845 }
5846 md_unregister_thread(&mddev->thread);
5847 if (mddev->queue)
5848 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5849}
5850
NeilBrown5eff3c42012-11-19 10:47:48 +11005851static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005852{
NeilBrown36d091f2014-12-15 12:56:58 +11005853 struct md_personality *pers = mddev->pers;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005854 md_bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11005855 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005856 /* Ensure ->event_work is done */
5857 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005858 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005859 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005860 spin_unlock(&mddev->lock);
5861 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005862 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005863 if (pers->sync_request && mddev->to_remove == NULL)
5864 mddev->to_remove = &md_redundancy_group;
5865 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005866 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Jack Wang6aaa58c2018-10-19 16:21:31 +02005867}
5868
5869void md_stop(struct mddev *mddev)
5870{
5871 /* stop the array and free an attached data structures.
5872 * This is called from dm-raid
5873 */
5874 __md_stop(mddev);
Kent Overstreetafeee512018-05-20 18:25:52 -04005875 bioset_exit(&mddev->bio_set);
5876 bioset_exit(&mddev->sync_set);
NeilBrown5eff3c42012-11-19 10:47:48 +11005877}
5878
NeilBrown390ee602010-06-01 19:37:27 +10005879EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005880
NeilBrowna05b7ea2012-07-19 15:59:18 +10005881static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005882{
5883 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005884 int did_freeze = 0;
5885
5886 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5887 did_freeze = 1;
5888 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5889 md_wakeup_thread(mddev->thread);
5890 }
NeilBrownf851b602014-12-11 10:02:10 +11005891 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005892 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005893 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005894 /* Thread might be blocked waiting for metadata update
5895 * which will now never happen */
5896 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005897
Shaohua Li29530792016-12-08 15:48:19 -08005898 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10005899 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005900 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005901 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5902 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005903 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005904 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005905 mddev_lock_nointr(mddev);
5906
NeilBrowna4bd82d2010-03-29 13:23:10 +11005907 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005908 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005909 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005910 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005911 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005912 if (did_freeze) {
5913 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005914 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005915 md_wakeup_thread(mddev->thread);
5916 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005917 err = -EBUSY;
5918 goto out;
5919 }
5920 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005921 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005922
5923 err = -ENXIO;
5924 if (mddev->ro==1)
5925 goto out;
5926 mddev->ro = 1;
5927 set_disk_ro(mddev->gendisk, 1);
5928 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005929 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5930 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005931 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005932 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005933 }
5934out:
5935 mutex_unlock(&mddev->open_mutex);
5936 return err;
5937}
5938
NeilBrown9e653b62006-06-26 00:27:58 -07005939/* mode:
5940 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005941 * 2 - stop but do not disassemble array
5942 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005943static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005944 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005947 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005948 int did_freeze = 0;
5949
5950 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5951 did_freeze = 1;
5952 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5953 md_wakeup_thread(mddev->thread);
5954 }
NeilBrownf851b602014-12-11 10:02:10 +11005955 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005956 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005957 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005958 /* Thread might be blocked waiting for metadata update
5959 * which will now never happen */
5960 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005961
NeilBrown30b8feb2013-11-14 15:16:17 +11005962 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005963 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5964 !test_bit(MD_RECOVERY_RUNNING,
5965 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005966 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967
NeilBrownc8c00a62009-08-10 12:50:52 +10005968 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005969 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005970 mddev->sysfs_active ||
5971 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005972 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005973 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005974 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005975 if (did_freeze) {
5976 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005977 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005978 md_wakeup_thread(mddev->thread);
5979 }
NeilBrown260fa032013-08-27 16:44:13 +10005980 return -EBUSY;
5981 }
NeilBrown6e17b022010-08-07 21:41:19 +10005982 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005983 if (mddev->ro)
5984 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005985
NeilBrowndefad612011-01-14 09:14:33 +11005986 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005987 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01005988 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005989
NeilBrowna4bd82d2010-03-29 13:23:10 +11005990 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005991 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005992
NeilBrowndafb20f2012-03-19 12:46:39 +11005993 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005994 if (rdev->raid_disk >= 0)
5995 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005996
NeilBrowna4bd82d2010-03-29 13:23:10 +11005997 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005998 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005999 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006000 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08006001
NeilBrowna4bd82d2010-03-29 13:23:10 +11006002 if (mddev->ro)
6003 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10006004 } else
6005 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 /*
6007 * Free resources if final stop
6008 */
NeilBrown9e653b62006-06-26 00:27:58 -07006009 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006010 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011
NeilBrownc3d97142009-12-14 12:49:52 +11006012 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11006013 struct file *f = mddev->bitmap_info.file;
6014 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11006015 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11006016 spin_unlock(&mddev->lock);
6017 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08006018 }
NeilBrownc3d97142009-12-14 12:49:52 +11006019 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08006020
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 export_array(mddev);
6022
NeilBrown6177b472010-03-29 11:37:13 +11006023 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11006024 if (mddev->hold_active == UNTIL_STOP)
6025 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006026 }
NeilBrownd7603b72006-01-06 00:20:30 -08006027 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006028 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10006029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030}
6031
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006032#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11006033static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034{
NeilBrown3cb03002011-10-11 16:45:26 +11006035 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 int err;
6037
NeilBrowna757e642005-04-16 15:26:42 -07006038 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
NeilBrown9d487392016-11-02 14:16:49 +11006041 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042
NeilBrowndafb20f2012-03-19 12:46:39 +11006043 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11006045 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 }
NeilBrown9d487392016-11-02 14:16:49 +11006047 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
NeilBrownd710e132008-10-13 11:55:12 +11006049 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006051 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10006052 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 }
6054}
6055
6056/*
6057 * lets try to run arrays based on all disks that have arrived
6058 * until now. (those are in pending_raid_disks)
6059 *
6060 * the method: pick the first pending disk, collect all disks with
6061 * the same UUID, remove all from the pending list and put them into
6062 * the 'same_array' list. Then order this list based on superblock
6063 * update time (freshest comes first), kick out 'old' disks and
6064 * compare superblocks. If everything's fine then run it.
6065 *
6066 * If "unit" is allocated, then bump its reference count
6067 */
6068static void autorun_devices(int part)
6069{
NeilBrown3cb03002011-10-11 16:45:26 +11006070 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006071 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 char b[BDEVNAME_SIZE];
6073
NeilBrown9d487392016-11-02 14:16:49 +11006074 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07006076 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08006078 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11006080 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
NeilBrown9d487392016-11-02 14:16:49 +11006082 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006084 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006086 pr_debug("md: adding %s ...\n",
6087 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 list_move(&rdev->same_set, &candidates);
6089 }
6090 /*
6091 * now we have a set of devices, with all of them having
6092 * mostly sane superblocks. It's time to allocate the
6093 * mddev.
6094 */
NeilBrowne8703fe2006-10-03 01:15:59 -07006095 if (part) {
6096 dev = MKDEV(mdp_major,
6097 rdev0->preferred_minor << MdpMinorShift);
6098 unit = MINOR(dev) >> MdpMinorShift;
6099 } else {
6100 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6101 unit = MINOR(dev);
6102 }
6103 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006104 pr_warn("md: unit number in %s is bad: %d\n",
6105 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 break;
6107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108
6109 md_probe(dev, NULL, NULL);
6110 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006111 if (!mddev || !mddev->gendisk) {
6112 if (mddev)
6113 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 break;
6115 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006116 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006117 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 else if (mddev->raid_disks || mddev->major_version
6119 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006120 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 mdname(mddev), bdevname(rdev0->bdev,b));
6122 mddev_unlock(mddev);
6123 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006124 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006125 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006126 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 list_del_init(&rdev->same_set);
6128 if (bind_rdev_to_array(rdev, mddev))
6129 export_rdev(rdev);
6130 }
6131 autorun_array(mddev);
6132 mddev_unlock(mddev);
6133 }
6134 /* on success, candidates will be empty, on error
6135 * it won't...
6136 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006137 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006138 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 mddev_put(mddev);
6142 }
NeilBrown9d487392016-11-02 14:16:49 +11006143 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006145#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146
NeilBrownf72ffdd2014-09-30 14:23:59 +10006147static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148{
6149 mdu_version_t ver;
6150
6151 ver.major = MD_MAJOR_VERSION;
6152 ver.minor = MD_MINOR_VERSION;
6153 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6154
6155 if (copy_to_user(arg, &ver, sizeof(ver)))
6156 return -EFAULT;
6157
6158 return 0;
6159}
6160
NeilBrownf72ffdd2014-09-30 14:23:59 +10006161static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162{
6163 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006164 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006165 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166
NeilBrown1ca69c42012-10-11 13:37:33 +11006167 nr = working = insync = failed = spare = 0;
6168 rcu_read_lock();
6169 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006171 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 failed++;
6173 else {
6174 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006175 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006176 insync++;
Song Liub347af82016-08-11 17:14:45 -07006177 else if (test_bit(Journal, &rdev->flags))
6178 /* TODO: add journal count to md_u.h */
6179 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 else
6181 spare++;
6182 }
6183 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006184 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185
6186 info.major_version = mddev->major_version;
6187 info.minor_version = mddev->minor_version;
6188 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006189 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006191 info.size = mddev->dev_sectors / 2;
6192 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006193 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 info.nr_disks = nr;
6195 info.raid_disks = mddev->raid_disks;
6196 info.md_minor = mddev->md_minor;
6197 info.not_persistent= !mddev->persistent;
6198
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006199 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 info.state = 0;
6201 if (mddev->in_sync)
6202 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006203 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006204 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006205 if (mddev_is_clustered(mddev))
6206 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006207 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 info.working_disks = working;
6209 info.failed_disks = failed;
6210 info.spare_disks = spare;
6211
6212 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006213 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214
6215 if (copy_to_user(arg, &info, sizeof(info)))
6216 return -EFAULT;
6217
6218 return 0;
6219}
6220
NeilBrownf72ffdd2014-09-30 14:23:59 +10006221static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006222{
6223 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006224 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006225 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006226
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006227 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006228 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006229 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006230
NeilBrown32a76272005-06-21 17:17:14 -07006231 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006232 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006233 /* bitmap enabled */
6234 if (mddev->bitmap_info.file) {
6235 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6236 sizeof(file->pathname));
6237 if (IS_ERR(ptr))
6238 err = PTR_ERR(ptr);
6239 else
6240 memmove(file->pathname, ptr,
6241 sizeof(file->pathname)-(ptr-file->pathname));
6242 }
NeilBrown4af1a042014-12-15 12:57:00 +11006243 spin_unlock(&mddev->lock);
6244
6245 if (err == 0 &&
6246 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006247 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006248
NeilBrown32a76272005-06-21 17:17:14 -07006249 kfree(file);
6250 return err;
6251}
6252
NeilBrownf72ffdd2014-09-30 14:23:59 +10006253static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254{
6255 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006256 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257
6258 if (copy_from_user(&info, arg, sizeof(info)))
6259 return -EFAULT;
6260
NeilBrown1ca69c42012-10-11 13:37:33 +11006261 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006262 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 if (rdev) {
6264 info.major = MAJOR(rdev->bdev->bd_dev);
6265 info.minor = MINOR(rdev->bdev->bd_dev);
6266 info.raid_disk = rdev->raid_disk;
6267 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006268 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006270 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 info.state |= (1<<MD_DISK_ACTIVE);
6272 info.state |= (1<<MD_DISK_SYNC);
6273 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006274 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006275 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006276 if (test_bit(WriteMostly, &rdev->flags))
6277 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006278 if (test_bit(FailFast, &rdev->flags))
6279 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 } else {
6281 info.major = info.minor = 0;
6282 info.raid_disk = -1;
6283 info.state = (1<<MD_DISK_REMOVED);
6284 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006285 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286
6287 if (copy_to_user(arg, &info, sizeof(info)))
6288 return -EFAULT;
6289
6290 return 0;
6291}
6292
NeilBrownf72ffdd2014-09-30 14:23:59 +10006293static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294{
6295 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006296 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297 dev_t dev = MKDEV(info->major,info->minor);
6298
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006299 if (mddev_is_clustered(mddev) &&
6300 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006301 pr_warn("%s: Cannot add to clustered mddev.\n",
6302 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006303 return -EINVAL;
6304 }
6305
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6307 return -EOVERFLOW;
6308
6309 if (!mddev->raid_disks) {
6310 int err;
6311 /* expecting a device which has a superblock */
6312 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6313 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006314 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315 PTR_ERR(rdev));
6316 return PTR_ERR(rdev);
6317 }
6318 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006319 struct md_rdev *rdev0
6320 = list_entry(mddev->disks.next,
6321 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006322 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 .load_super(rdev, rdev0, mddev->minor_version);
6324 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006325 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006326 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 bdevname(rdev0->bdev,b2));
6328 export_rdev(rdev);
6329 return -EINVAL;
6330 }
6331 }
6332 err = bind_rdev_to_array(rdev, mddev);
6333 if (err)
6334 export_rdev(rdev);
6335 return err;
6336 }
6337
6338 /*
6339 * add_new_disk can be used once the array is assembled
6340 * to add "hot spares". They must already have a superblock
6341 * written
6342 */
6343 if (mddev->pers) {
6344 int err;
6345 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006346 pr_warn("%s: personality does not support diskops!\n",
6347 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 return -EINVAL;
6349 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006350 if (mddev->persistent)
6351 rdev = md_import_device(dev, mddev->major_version,
6352 mddev->minor_version);
6353 else
6354 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006356 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 PTR_ERR(rdev));
6358 return PTR_ERR(rdev);
6359 }
NeilBrown1a855a02010-12-09 16:36:28 +11006360 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006361 if (!mddev->persistent) {
6362 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006363 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006364 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006365 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006366 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006367 } else
NeilBrown41158c72005-06-21 17:17:25 -07006368 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006369 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006370 } else
6371 super_types[mddev->major_version].
6372 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006373 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006374 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006375 /* This was a hot-add request, but events doesn't
6376 * match, so reject it.
6377 */
6378 export_rdev(rdev);
6379 return -EINVAL;
6380 }
6381
NeilBrownb2d444d2005-11-08 21:39:31 -08006382 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006383 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6384 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006385 else
6386 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006387 if (info->state & (1<<MD_DISK_FAILFAST))
6388 set_bit(FailFast, &rdev->flags);
6389 else
6390 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006391
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006392 if (info->state & (1<<MD_DISK_JOURNAL)) {
6393 struct md_rdev *rdev2;
6394 bool has_journal = false;
6395
6396 /* make sure no existing journal disk */
6397 rdev_for_each(rdev2, mddev) {
6398 if (test_bit(Journal, &rdev2->flags)) {
6399 has_journal = true;
6400 break;
6401 }
6402 }
NeilBrown230b55f2017-10-17 14:24:09 +11006403 if (has_journal || mddev->bitmap) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006404 export_rdev(rdev);
6405 return -EBUSY;
6406 }
Song Liubac624f2015-08-13 14:31:55 -07006407 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006408 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006409 /*
6410 * check whether the device shows up in other nodes
6411 */
6412 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006413 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006414 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006415 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006416 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006417 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006418 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006419 export_rdev(rdev);
6420 return err;
6421 }
6422 }
6423 }
6424
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 rdev->raid_disk = -1;
6426 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006427
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 if (err)
6429 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006430
6431 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006432 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6433 if (!err) {
6434 err = md_cluster_ops->new_disk_ack(mddev,
6435 err == 0);
6436 if (err)
6437 md_kick_rdev_from_array(rdev);
6438 }
6439 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006440 if (err)
6441 md_cluster_ops->add_new_disk_cancel(mddev);
6442 else
6443 err = add_bound_rdev(rdev);
6444 }
6445
6446 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006447 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006448
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 return err;
6450 }
6451
6452 /* otherwise, add_new_disk is only allowed
6453 * for major_version==0 superblocks
6454 */
6455 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006456 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 return -EINVAL;
6458 }
6459
6460 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6461 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006462 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006464 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 PTR_ERR(rdev));
6466 return PTR_ERR(rdev);
6467 }
6468 rdev->desc_nr = info->number;
6469 if (info->raid_disk < mddev->raid_disks)
6470 rdev->raid_disk = info->raid_disk;
6471 else
6472 rdev->raid_disk = -1;
6473
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006475 if (info->state & (1<<MD_DISK_SYNC))
6476 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006478 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6479 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006480 if (info->state & (1<<MD_DISK_FAILFAST))
6481 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006482
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006484 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006485 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6486 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006487 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006488 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489
NeilBrown2bf071b2006-01-06 00:20:55 -08006490 err = bind_rdev_to_array(rdev, mddev);
6491 if (err) {
6492 export_rdev(rdev);
6493 return err;
6494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 }
6496
6497 return 0;
6498}
6499
NeilBrownf72ffdd2014-09-30 14:23:59 +10006500static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501{
6502 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006503 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504
Yufen Yuc42a0e22018-05-04 18:08:10 +08006505 if (!mddev->pers)
6506 return -ENODEV;
6507
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 rdev = find_rdev(mddev, dev);
6509 if (!rdev)
6510 return -ENXIO;
6511
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006512 if (rdev->raid_disk < 0)
6513 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006514
NeilBrown3ea8929d2013-04-24 11:42:41 +10006515 clear_bit(Blocked, &rdev->flags);
6516 remove_and_add_spares(mddev, rdev);
6517
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 if (rdev->raid_disk >= 0)
6519 goto busy;
6520
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006521kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006522 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006523 md_cluster_ops->remove_disk(mddev, rdev);
6524
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006525 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006526 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006527 if (mddev->thread)
6528 md_wakeup_thread(mddev->thread);
6529 else
6530 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006531 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532
6533 return 0;
6534busy:
NeilBrown9d487392016-11-02 14:16:49 +11006535 pr_debug("md: cannot remove active disk %s from %s ...\n",
6536 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 return -EBUSY;
6538}
6539
NeilBrownf72ffdd2014-09-30 14:23:59 +10006540static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541{
6542 char b[BDEVNAME_SIZE];
6543 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006544 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545
6546 if (!mddev->pers)
6547 return -ENODEV;
6548
6549 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006550 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 mdname(mddev));
6552 return -EINVAL;
6553 }
6554 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006555 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 mdname(mddev));
6557 return -EINVAL;
6558 }
6559
NeilBrownd710e132008-10-13 11:55:12 +11006560 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006562 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 PTR_ERR(rdev));
6564 return -EINVAL;
6565 }
6566
6567 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006568 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006570 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571
NeilBrown8190e752009-06-18 08:48:58 +10006572 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573
NeilBrownb2d444d2005-11-08 21:39:31 -08006574 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006575 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 bdevname(rdev->bdev,b), mdname(mddev));
6577 err = -EINVAL;
6578 goto abort_export;
6579 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006580
NeilBrownb2d444d2005-11-08 21:39:31 -08006581 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006583 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006584 err = bind_rdev_to_array(rdev, mddev);
6585 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006586 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587
6588 /*
6589 * The rest should better be atomic, we can have disk failures
6590 * noticed in interrupt contexts ...
6591 */
6592
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593 rdev->raid_disk = -1;
6594
Shaohua Li29530792016-12-08 15:48:19 -08006595 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006596 if (!mddev->thread)
6597 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 /*
6599 * Kick recovery, maybe this spare has to be added to the
6600 * array immediately.
6601 */
6602 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6603 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006604 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 return 0;
6606
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607abort_export:
6608 export_rdev(rdev);
6609 return err;
6610}
6611
NeilBrownfd01b882011-10-11 16:47:53 +11006612static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006613{
NeilBrown035328c2014-04-09 12:25:40 +10006614 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006615
NeilBrown36fa3062005-09-09 16:23:45 -07006616 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006617 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006618 return -EBUSY;
6619 if (mddev->recovery || mddev->sync_thread)
6620 return -EBUSY;
6621 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006622 }
6623
NeilBrown36fa3062005-09-09 16:23:45 -07006624 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006625 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006626 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006627
NeilBrown1e594bb2014-12-15 12:57:00 +11006628 if (mddev->bitmap || mddev->bitmap_info.file)
6629 return -EEXIST; /* cannot add when bitmap is present */
6630 f = fget(fd);
6631
6632 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006633 pr_warn("%s: error: failed to get bitmap file\n",
6634 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006635 return -EBADF;
6636 }
6637
NeilBrown1e594bb2014-12-15 12:57:00 +11006638 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006639 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006640 pr_warn("%s: error: bitmap file must be a regular file\n",
6641 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006642 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006643 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006644 pr_warn("%s: error: bitmap file must open for write\n",
6645 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006646 err = -EBADF;
6647 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006648 pr_warn("%s: error: bitmap file is already in use\n",
6649 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006650 err = -EBUSY;
6651 }
6652 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006653 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006654 return err;
6655 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006656 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006657 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006658 } else if (mddev->bitmap == NULL)
6659 return -ENOENT; /* cannot remove what isn't there */
6660 err = 0;
6661 if (mddev->pers) {
NeilBrown69e51b42010-06-01 19:37:35 +10006662 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006663 struct bitmap *bitmap;
6664
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006665 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006666 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006667 if (!IS_ERR(bitmap)) {
6668 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006669 err = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006670 } else
6671 err = PTR_ERR(bitmap);
NeilBrown52a0d492017-10-17 13:46:43 +11006672 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006673 md_bitmap_destroy(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11006674 fd = -1;
6675 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006676 mddev_resume(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11006677 } else if (fd < 0) {
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006678 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006679 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006680 mddev_resume(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006681 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006682 }
6683 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006684 struct file *f = mddev->bitmap_info.file;
6685 if (f) {
6686 spin_lock(&mddev->lock);
6687 mddev->bitmap_info.file = NULL;
6688 spin_unlock(&mddev->lock);
6689 fput(f);
6690 }
NeilBrown36fa3062005-09-09 16:23:45 -07006691 }
6692
NeilBrown32a76272005-06-21 17:17:14 -07006693 return err;
6694}
6695
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696/*
6697 * set_array_info is used two different ways
6698 * The original usage is when creating a new array.
6699 * In this usage, raid_disks is > 0 and it together with
6700 * level, size, not_persistent,layout,chunksize determine the
6701 * shape of the array.
6702 * This will always create an array with a type-0.90.0 superblock.
6703 * The newer usage is when assembling an array.
6704 * In this case raid_disks will be 0, and the major_version field is
6705 * use to determine which style super-blocks are to be found on the devices.
6706 * The minor and patch _version numbers are also kept incase the
6707 * super_block handler wishes to interpret them.
6708 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006709static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710{
6711
6712 if (info->raid_disks == 0) {
6713 /* just setting version number for superblock loading */
6714 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006715 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 super_types[info->major_version].name == NULL) {
6717 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11006718 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719 info->major_version);
6720 return -EINVAL;
6721 }
6722 mddev->major_version = info->major_version;
6723 mddev->minor_version = info->minor_version;
6724 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006725 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006726 /* ensure mddev_put doesn't delete this now that there
6727 * is some minimal configuration.
6728 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006729 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730 return 0;
6731 }
6732 mddev->major_version = MD_MAJOR_VERSION;
6733 mddev->minor_version = MD_MINOR_VERSION;
6734 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006735 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736
6737 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006738 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006739 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 mddev->raid_disks = info->raid_disks;
6741 /* don't set md_minor, it is determined by which /dev/md* was
6742 * openned
6743 */
6744 if (info->state & (1<<MD_SB_CLEAN))
6745 mddev->recovery_cp = MaxSector;
6746 else
6747 mddev->recovery_cp = 0;
6748 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006749 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750
6751 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006752 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753
Shaohua Li29530792016-12-08 15:48:19 -08006754 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11006755 mddev->max_disks = MD_SB_DISKS;
6756 mddev->flags = 0;
6757 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006758 }
6759 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760
NeilBrownc3d97142009-12-14 12:49:52 +11006761 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006762 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006763 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006764
NeilBrownf6705572006-03-27 01:18:11 -08006765 mddev->reshape_position = MaxSector;
6766
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 /*
6768 * Generate a 128 bit UUID
6769 */
6770 get_random_bytes(mddev->uuid, 16);
6771
NeilBrownf6705572006-03-27 01:18:11 -08006772 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006773 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006774 mddev->new_layout = mddev->layout;
6775 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006776 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006777
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 return 0;
6779}
6780
NeilBrownfd01b882011-10-11 16:47:53 +11006781void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006782{
Shaohua Liefa4b772017-10-18 22:08:13 -07006783 lockdep_assert_held(&mddev->reconfig_mutex);
Dan Williamsb522adc2009-03-31 15:00:31 +11006784
6785 if (mddev->external_size)
6786 return;
6787
Dan Williams1f403622009-03-31 14:59:03 +11006788 mddev->array_sectors = array_sectors;
6789}
6790EXPORT_SYMBOL(md_set_array_sectors);
6791
NeilBrownfd01b882011-10-11 16:47:53 +11006792static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006793{
NeilBrown3cb03002011-10-11 16:45:26 +11006794 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006795 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006796 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08006797 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006798
NeilBrowna35b0d62006-01-06 00:20:49 -08006799 if (mddev->pers->resize == NULL)
6800 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006801 /* The "num_sectors" is the number of sectors of each device that
6802 * is used. This can only make sense for arrays with redundancy.
6803 * linear and raid0 always use whatever space is available. We can only
6804 * consider changing this number if no resync or reconstruction is
6805 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006806 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006807 * of each device. If num_sectors is zero, we find the largest size
6808 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006809 */
NeilBrownf851b602014-12-11 10:02:10 +11006810 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6811 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006812 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006813 if (mddev->ro)
6814 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006815
NeilBrowndafb20f2012-03-19 12:46:39 +11006816 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006817 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006818
Andre Nolld71f9f82008-07-11 22:02:22 +10006819 if (fit && (num_sectors == 0 || num_sectors > avail))
6820 num_sectors = avail;
6821 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006822 return -ENOSPC;
6823 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006824 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08006825 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08006826 if (mddev_is_clustered(mddev))
6827 md_cluster_ops->update_size(mddev, old_dev_sectors);
6828 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08006829 set_capacity(mddev->gendisk, mddev->array_sectors);
6830 revalidate_disk(mddev->gendisk);
6831 }
6832 }
NeilBrowna35b0d62006-01-06 00:20:49 -08006833 return rv;
6834}
6835
NeilBrownfd01b882011-10-11 16:47:53 +11006836static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006837{
6838 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006839 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006840 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006841 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006842 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006843 if (mddev->ro)
6844 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006845 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006846 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006847 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006848 if (mddev->sync_thread ||
6849 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6850 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006851 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006852
6853 rdev_for_each(rdev, mddev) {
6854 if (mddev->raid_disks < raid_disks &&
6855 rdev->data_offset < rdev->new_data_offset)
6856 return -EINVAL;
6857 if (mddev->raid_disks > raid_disks &&
6858 rdev->data_offset > rdev->new_data_offset)
6859 return -EINVAL;
6860 }
6861
NeilBrown63c70c42006-03-27 01:18:13 -08006862 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006863 if (mddev->delta_disks < 0)
6864 mddev->reshape_backwards = 1;
6865 else if (mddev->delta_disks > 0)
6866 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006867
6868 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006869 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006870 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006871 mddev->reshape_backwards = 0;
6872 }
NeilBrownda943b992006-01-06 00:20:54 -08006873 return rv;
6874}
6875
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876/*
6877 * update_array_info is used to change the configuration of an
6878 * on-line array.
6879 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6880 * fields in the info are checked against the array.
6881 * Any differences that cannot be handled will cause an error.
6882 * Normally, only one change can be managed at a time.
6883 */
NeilBrownfd01b882011-10-11 16:47:53 +11006884static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885{
6886 int rv = 0;
6887 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006888 int state = 0;
6889
6890 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006891 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006892 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893
6894 if (mddev->major_version != info->major_version ||
6895 mddev->minor_version != info->minor_version ||
6896/* mddev->patch_version != info->patch_version || */
6897 mddev->ctime != info->ctime ||
6898 mddev->level != info->level ||
6899/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006900 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006901 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006902 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6903 ((state^info->state) & 0xfffffe00)
6904 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 return -EINVAL;
6906 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006907 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6908 cnt++;
6909 if (mddev->raid_disks != info->raid_disks)
6910 cnt++;
6911 if (mddev->layout != info->layout)
6912 cnt++;
6913 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6914 cnt++;
6915 if (cnt == 0)
6916 return 0;
6917 if (cnt > 1)
6918 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919
6920 if (mddev->layout != info->layout) {
6921 /* Change layout
6922 * we don't need to do anything at the md level, the
6923 * personality will take care of it all.
6924 */
NeilBrown50ac1682009-06-18 08:47:55 +10006925 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006927 else {
6928 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006929 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006930 if (rv)
6931 mddev->new_layout = mddev->layout;
6932 return rv;
6933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006935 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006936 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937
NeilBrownda943b992006-01-06 00:20:54 -08006938 if (mddev->raid_disks != info->raid_disks)
6939 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940
NeilBrown36fa3062005-09-09 16:23:45 -07006941 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006942 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6943 rv = -EINVAL;
6944 goto err;
6945 }
6946 if (mddev->recovery || mddev->sync_thread) {
6947 rv = -EBUSY;
6948 goto err;
6949 }
NeilBrown36fa3062005-09-09 16:23:45 -07006950 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006951 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006952 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006953 if (mddev->bitmap) {
6954 rv = -EEXIST;
6955 goto err;
6956 }
6957 if (mddev->bitmap_info.default_offset == 0) {
6958 rv = -EINVAL;
6959 goto err;
6960 }
NeilBrownc3d97142009-12-14 12:49:52 +11006961 mddev->bitmap_info.offset =
6962 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006963 mddev->bitmap_info.space =
6964 mddev->bitmap_info.default_space;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006965 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006966 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006967 if (!IS_ERR(bitmap)) {
6968 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006969 rv = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006970 } else
6971 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006972 if (rv)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006973 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006974 mddev_resume(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006975 } else {
6976 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006977 if (!mddev->bitmap) {
6978 rv = -ENOENT;
6979 goto err;
6980 }
6981 if (mddev->bitmap->storage.file) {
6982 rv = -EINVAL;
6983 goto err;
6984 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006985 if (mddev->bitmap_info.nodes) {
6986 /* hold PW on all the bitmap lock */
6987 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006988 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 +11006989 rv = -EPERM;
6990 md_cluster_ops->unlock_all_bitmaps(mddev);
6991 goto err;
6992 }
6993
6994 mddev->bitmap_info.nodes = 0;
6995 md_cluster_ops->leave(mddev);
6996 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006997 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006998 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006999 mddev_resume(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11007000 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007001 }
7002 }
NeilBrown850b2b422006-10-03 01:15:46 -07007003 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007004 return rv;
7005err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006 return rv;
7007}
7008
NeilBrownfd01b882011-10-11 16:47:53 +11007009static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010{
NeilBrown3cb03002011-10-11 16:45:26 +11007011 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11007012 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013
7014 if (mddev->pers == NULL)
7015 return -ENODEV;
7016
NeilBrown1ca69c42012-10-11 13:37:33 +11007017 rcu_read_lock();
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01007018 rdev = md_find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11007020 err = -ENODEV;
7021 else {
7022 md_error(mddev, rdev);
7023 if (!test_bit(Faulty, &rdev->flags))
7024 err = -EBUSY;
7025 }
7026 rcu_read_unlock();
7027 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028}
7029
Andre Noll2f9618c2008-04-25 18:57:58 +02007030/*
7031 * We have a problem here : there is no easy way to give a CHS
7032 * virtual geometry. We currently pretend that we have a 2 heads
7033 * 4 sectors (with a BIG number of cylinders...). This drives
7034 * dosfs just mad... ;-)
7035 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007036static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7037{
NeilBrownfd01b882011-10-11 16:47:53 +11007038 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007039
7040 geo->heads = 2;
7041 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11007042 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007043 return 0;
7044}
7045
Nicolas Schichancb335f82014-01-15 16:58:52 +01007046static inline bool md_ioctl_valid(unsigned int cmd)
7047{
7048 switch (cmd) {
7049 case ADD_NEW_DISK:
7050 case BLKROSET:
7051 case GET_ARRAY_INFO:
7052 case GET_BITMAP_FILE:
7053 case GET_DISK_INFO:
7054 case HOT_ADD_DISK:
7055 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007056 case RAID_AUTORUN:
7057 case RAID_VERSION:
7058 case RESTART_ARRAY_RW:
7059 case RUN_ARRAY:
7060 case SET_ARRAY_INFO:
7061 case SET_BITMAP_FILE:
7062 case SET_DISK_FAULTY:
7063 case STOP_ARRAY:
7064 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007065 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007066 return true;
7067 default:
7068 return false;
7069 }
7070}
7071
Al Viroa39907f2008-03-02 10:31:15 -05007072static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 unsigned int cmd, unsigned long arg)
7074{
7075 int err = 0;
7076 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11007077 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10007078 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08007079 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080
Nicolas Schichancb335f82014-01-15 16:58:52 +01007081 if (!md_ioctl_valid(cmd))
7082 return -ENOTTY;
7083
NeilBrown506c9e42011-12-23 10:17:26 +11007084 switch (cmd) {
7085 case RAID_VERSION:
7086 case GET_ARRAY_INFO:
7087 case GET_DISK_INFO:
7088 break;
7089 default:
7090 if (!capable(CAP_SYS_ADMIN))
7091 return -EACCES;
7092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093
7094 /*
7095 * Commands dealing with the RAID driver but not any
7096 * particular array:
7097 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007098 switch (cmd) {
7099 case RAID_VERSION:
7100 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007101 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11007104 case RAID_AUTORUN:
7105 err = 0;
7106 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007107 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11007109 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110 }
7111
7112 /*
7113 * Commands creating/starting a new array:
7114 */
7115
Al Viroa39907f2008-03-02 10:31:15 -05007116 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117
7118 if (!mddev) {
7119 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 }
7122
NeilBrown1ca69c42012-10-11 13:37:33 +11007123 /* Some actions do not requires the mutex */
7124 switch (cmd) {
7125 case GET_ARRAY_INFO:
7126 if (!mddev->raid_disks && !mddev->external)
7127 err = -ENODEV;
7128 else
7129 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007130 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007131
7132 case GET_DISK_INFO:
7133 if (!mddev->raid_disks && !mddev->external)
7134 err = -ENODEV;
7135 else
7136 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007137 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007138
7139 case SET_DISK_FAULTY:
7140 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007141 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007142
7143 case GET_BITMAP_FILE:
7144 err = get_bitmap_file(mddev, argp);
7145 goto out;
7146
NeilBrown1ca69c42012-10-11 13:37:33 +11007147 }
7148
NeilBrowna7a3f082012-12-11 13:35:54 +11007149 if (cmd == ADD_NEW_DISK)
7150 /* need to ensure md_delayed_delete() has completed */
7151 flush_workqueue(md_misc_wq);
7152
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007153 if (cmd == HOT_REMOVE_DISK)
7154 /* need to ensure recovery thread has run */
7155 wait_event_interruptible_timeout(mddev->sb_wait,
7156 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007157 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007158 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007159 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7160 /* Need to flush page cache, and ensure no-one else opens
7161 * and writes
7162 */
7163 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007164 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007165 mutex_unlock(&mddev->open_mutex);
7166 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007167 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007168 }
NeilBrown065e5192017-04-06 11:16:33 +08007169 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007170 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007171 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007172 mutex_unlock(&mddev->open_mutex);
7173 sync_blockdev(bdev);
7174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175 err = mddev_lock(mddev);
7176 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007177 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7178 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007179 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180 }
7181
NeilBrownc02c0ae2012-12-11 13:39:21 +11007182 if (cmd == SET_ARRAY_INFO) {
7183 mdu_array_info_t info;
7184 if (!arg)
7185 memset(&info, 0, sizeof(info));
7186 else if (copy_from_user(&info, argp, sizeof(info))) {
7187 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007188 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007189 }
7190 if (mddev->pers) {
7191 err = update_array_info(mddev, &info);
7192 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007193 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007194 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007196 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007197 }
7198 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007199 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007200 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007201 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007202 }
7203 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007204 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007205 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007206 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007207 }
7208 err = set_array_info(mddev, &info);
7209 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007210 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007211 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007212 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007213 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214 }
7215
7216 /*
7217 * Commands querying/configuring an existing array:
7218 */
NeilBrown32a76272005-06-21 17:17:14 -07007219 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007220 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007221 if ((!mddev->raid_disks && !mddev->external)
7222 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7223 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7224 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007226 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 }
7228
7229 /*
7230 * Commands even a read-only array can execute:
7231 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007232 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007233 case RESTART_ARRAY_RW:
7234 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007235 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007236
7237 case STOP_ARRAY:
7238 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007239 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007240
7241 case STOP_ARRAY_RO:
7242 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007243 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007244
NeilBrown3ea8929d2013-04-24 11:42:41 +10007245 case HOT_REMOVE_DISK:
7246 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007247 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007248
NeilBrown7ceb17e2013-04-24 11:42:42 +10007249 case ADD_NEW_DISK:
7250 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007251 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007252 * So require mddev->pers and MD_DISK_SYNC.
7253 */
7254 if (mddev->pers) {
7255 mdu_disk_info_t info;
7256 if (copy_from_user(&info, argp, sizeof(info)))
7257 err = -EFAULT;
7258 else if (!(info.state & (1<<MD_DISK_SYNC)))
7259 /* Need to clear read-only for this */
7260 break;
7261 else
7262 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007263 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007264 }
7265 break;
7266
NeilBrownc02c0ae2012-12-11 13:39:21 +11007267 case BLKROSET:
7268 if (get_user(ro, (int __user *)(arg))) {
7269 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007270 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007271 }
7272 err = -EINVAL;
7273
7274 /* if the bdev is going readonly the value of mddev->ro
7275 * does not matter, no writes are coming
7276 */
7277 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007278 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007279
NeilBrownc02c0ae2012-12-11 13:39:21 +11007280 /* are we are already prepared for writes? */
7281 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007282 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007283
7284 /* transitioning to readauto need only happen for
7285 * arrays that call md_write_start
7286 */
7287 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007289 if (err == 0) {
7290 mddev->ro = 2;
7291 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007292 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007293 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007294 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295 }
7296
7297 /*
7298 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007299 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300 */
NeilBrown326eb172014-09-30 15:36:28 +10007301 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007302 if (mddev->ro == 2) {
7303 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007304 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007305 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007306 /* mddev_unlock will wake thread */
7307 /* If a device failed while we were read-only, we
7308 * need to make sure the metadata is updated now.
7309 */
Shaohua Li29530792016-12-08 15:48:19 -08007310 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007311 mddev_unlock(mddev);
7312 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007313 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7314 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007315 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007316 }
NeilBrownf91de922005-11-08 21:39:36 -08007317 } else {
7318 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007319 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007321 }
7322
NeilBrownc02c0ae2012-12-11 13:39:21 +11007323 switch (cmd) {
7324 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007326 mdu_disk_info_t info;
7327 if (copy_from_user(&info, argp, sizeof(info)))
7328 err = -EFAULT;
7329 else
7330 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007331 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007334 case CLUSTERED_DISK_NACK:
7335 if (mddev_is_clustered(mddev))
7336 md_cluster_ops->new_disk_ack(mddev, false);
7337 else
7338 err = -EINVAL;
7339 goto unlock;
7340
NeilBrownc02c0ae2012-12-11 13:39:21 +11007341 case HOT_ADD_DISK:
7342 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007343 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344
NeilBrownc02c0ae2012-12-11 13:39:21 +11007345 case RUN_ARRAY:
7346 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007347 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348
NeilBrownc02c0ae2012-12-11 13:39:21 +11007349 case SET_BITMAP_FILE:
7350 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007351 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007352
NeilBrownc02c0ae2012-12-11 13:39:21 +11007353 default:
7354 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007355 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356 }
7357
NeilBrown3adc28d2014-09-30 15:46:41 +10007358unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007359 if (mddev->hold_active == UNTIL_IOCTL &&
7360 err != -EINVAL)
7361 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007363out:
NeilBrown065e5192017-04-06 11:16:33 +08007364 if(did_set_md_closing)
7365 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366 return err;
7367}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007368#ifdef CONFIG_COMPAT
7369static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7370 unsigned int cmd, unsigned long arg)
7371{
7372 switch (cmd) {
7373 case HOT_REMOVE_DISK:
7374 case HOT_ADD_DISK:
7375 case SET_DISK_FAULTY:
7376 case SET_BITMAP_FILE:
7377 /* These take in integer arg, do not convert */
7378 break;
7379 default:
7380 arg = (unsigned long)compat_ptr(arg);
7381 break;
7382 }
7383
7384 return md_ioctl(bdev, mode, cmd, arg);
7385}
7386#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387
Al Viroa39907f2008-03-02 10:31:15 -05007388static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389{
7390 /*
7391 * Succeed if we can lock the mddev, which confirms that
7392 * it isn't being stopped right now.
7393 */
NeilBrownfd01b882011-10-11 16:47:53 +11007394 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395 int err;
7396
Yuanhan Liu0c098222012-05-22 13:55:32 +10007397 if (!mddev)
7398 return -ENODEV;
7399
NeilBrownd3374822009-01-09 08:31:10 +11007400 if (mddev->gendisk != bdev->bd_disk) {
7401 /* we are racing with mddev_put which is discarding this
7402 * bd_disk.
7403 */
7404 mddev_put(mddev);
7405 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007406 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007407 /* Then retry the open from the top */
7408 return -ERESTARTSYS;
7409 }
7410 BUG_ON(mddev != bdev->bd_disk->private_data);
7411
NeilBrownc8c00a62009-08-10 12:50:52 +10007412 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413 goto out;
7414
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007415 if (test_bit(MD_CLOSING, &mddev->flags)) {
7416 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007417 err = -ENODEV;
7418 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007419 }
7420
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007422 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007423 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007425 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007427 if (err)
7428 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429 return err;
7430}
7431
Al Virodb2a1442013-05-05 21:52:57 -04007432static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007434 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007436 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007437 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007440
7441static int md_media_changed(struct gendisk *disk)
7442{
NeilBrownfd01b882011-10-11 16:47:53 +11007443 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007444
7445 return mddev->changed;
7446}
7447
7448static int md_revalidate(struct gendisk *disk)
7449{
NeilBrownfd01b882011-10-11 16:47:53 +11007450 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007451
7452 mddev->changed = 0;
7453 return 0;
7454}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007455static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456{
7457 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007458 .open = md_open,
7459 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007460 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007461#ifdef CONFIG_COMPAT
7462 .compat_ioctl = md_compat_ioctl,
7463#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007464 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007465 .media_changed = md_media_changed,
7466 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467};
7468
NeilBrownf72ffdd2014-09-30 14:23:59 +10007469static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470{
NeilBrown2b8bf342011-10-11 16:48:23 +11007471 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 /*
7474 * md_thread is a 'system-thread', it's priority should be very
7475 * high. We avoid resource deadlocks individually in each
7476 * raid personality. (RAID5 does preallocation) We also use RR and
7477 * the very same RT priority as kswapd, thus we will never get
7478 * into a priority inversion deadlock.
7479 *
7480 * we definitely have to have equal or higher priority than
7481 * bdflush, otherwise bdflush will deadlock if there are too
7482 * many dirty RAID5 blocks.
7483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484
NeilBrown6985c432005-10-19 21:23:47 -07007485 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007486 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487
NeilBrown93588e22005-11-15 00:09:12 -08007488 /* We need to wait INTERRUPTIBLE so that
7489 * we don't add to the load-average.
7490 * That means we need to be sure no signals are
7491 * pending
7492 */
7493 if (signal_pending(current))
7494 flush_signals(current);
7495
7496 wait_event_interruptible_timeout
7497 (thread->wqueue,
7498 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007499 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007500 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501
NeilBrown6c987912011-01-14 09:13:53 +11007502 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007503 if (kthread_should_park())
7504 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007505 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007506 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007508
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509 return 0;
7510}
7511
NeilBrown2b8bf342011-10-11 16:48:23 +11007512void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513{
7514 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007515 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Guoqing Jiangd1d90142017-10-09 10:32:48 +08007516 set_bit(THREAD_WAKEUP, &thread->flags);
7517 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 }
7519}
NeilBrown6c144d32014-09-30 16:15:38 +10007520EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521
Shaohua Li4ed87312012-10-11 13:34:00 +11007522struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7523 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524{
NeilBrown2b8bf342011-10-11 16:48:23 +11007525 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526
NeilBrown2b8bf342011-10-11 16:48:23 +11007527 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528 if (!thread)
7529 return NULL;
7530
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531 init_waitqueue_head(&thread->wqueue);
7532
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533 thread->run = run;
7534 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007535 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007536 thread->tsk = kthread_run(md_thread, thread,
7537 "%s_%s",
7538 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007539 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007540 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541 kfree(thread);
7542 return NULL;
7543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544 return thread;
7545}
NeilBrown6c144d32014-09-30 16:15:38 +10007546EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547
NeilBrown2b8bf342011-10-11 16:48:23 +11007548void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549{
NeilBrown2b8bf342011-10-11 16:48:23 +11007550 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007551 if (!thread)
7552 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007553 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007554 /* Locking ensures that mddev_unlock does not wake_up a
7555 * non-existent thread
7556 */
7557 spin_lock(&pers_lock);
7558 *threadp = NULL;
7559 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007560
7561 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562 kfree(thread);
7563}
NeilBrown6c144d32014-09-30 16:15:38 +10007564EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565
NeilBrownfd01b882011-10-11 16:47:53 +11007566void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567{
NeilBrownb2d444d2005-11-08 21:39:31 -08007568 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007570
NeilBrownde393cd2011-07-28 11:31:48 +10007571 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572 return;
7573 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007574 if (mddev->degraded)
7575 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007576 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7578 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7579 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007580 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007581 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007582 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583}
NeilBrown6c144d32014-09-30 16:15:38 +10007584EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585
7586/* seq_file implementation /proc/mdstat */
7587
7588static void status_unused(struct seq_file *seq)
7589{
7590 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007591 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592
7593 seq_printf(seq, "unused devices: ");
7594
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007595 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596 char b[BDEVNAME_SIZE];
7597 i++;
7598 seq_printf(seq, "%s ",
7599 bdevname(rdev->bdev,b));
7600 }
7601 if (!i)
7602 seq_printf(seq, "<none>");
7603
7604 seq_printf(seq, "\n");
7605}
7606
NeilBrownf7851be2015-07-02 17:12:58 +10007607static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007608{
NeilBrowndd71cf62009-05-07 12:49:35 +10007609 sector_t max_sectors, resync, res;
7610 unsigned long dt, db;
7611 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007612 int scale;
7613 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614
NeilBrownc804cde2012-05-21 09:28:33 +10007615 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7616 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007617 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007619 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620
NeilBrownf7851be2015-07-02 17:12:58 +10007621 resync = mddev->curr_resync;
7622 if (resync <= 3) {
7623 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7624 /* Still cleaning up */
7625 resync = max_sectors;
Nate Daileyd2e2ec82017-11-30 11:33:30 -05007626 } else if (resync > max_sectors)
7627 resync = max_sectors;
7628 else
NeilBrownf7851be2015-07-02 17:12:58 +10007629 resync -= atomic_read(&mddev->recovery_active);
7630
7631 if (resync == 0) {
Guoqing Jiang0357ba22018-07-02 16:26:25 +08007632 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
7633 struct md_rdev *rdev;
7634
7635 rdev_for_each(rdev, mddev)
7636 if (rdev->raid_disk >= 0 &&
7637 !test_bit(Faulty, &rdev->flags) &&
7638 rdev->recovery_offset != MaxSector &&
7639 rdev->recovery_offset) {
7640 seq_printf(seq, "\trecover=REMOTE");
7641 return 1;
7642 }
7643 if (mddev->reshape_position != MaxSector)
7644 seq_printf(seq, "\treshape=REMOTE");
7645 else
7646 seq_printf(seq, "\tresync=REMOTE");
7647 return 1;
7648 }
NeilBrownf7851be2015-07-02 17:12:58 +10007649 if (mddev->recovery_cp < MaxSector) {
7650 seq_printf(seq, "\tresync=PENDING");
7651 return 1;
7652 }
7653 return 0;
7654 }
7655 if (resync < 3) {
7656 seq_printf(seq, "\tresync=DELAYED");
7657 return 1;
7658 }
7659
NeilBrown403df472014-09-30 15:52:29 +10007660 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007661 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007662 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007663 * u32, as those are the requirements for sector_div.
7664 * Thus 'scale' must be at least 10
7665 */
7666 scale = 10;
7667 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007668 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007669 scale++;
7670 }
7671 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007672 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007673
7674 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007675 {
NeilBrown4588b422006-03-27 01:18:04 -08007676 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677 seq_printf(seq, "[");
7678 for (i = 0; i < x; i++)
7679 seq_printf(seq, "=");
7680 seq_printf(seq, ">");
7681 for (i = 0; i < y; i++)
7682 seq_printf(seq, ".");
7683 seq_printf(seq, "] ");
7684 }
NeilBrown4588b422006-03-27 01:18:04 -08007685 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007686 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7687 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007688 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7689 "check" :
7690 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7691 "resync" : "recovery"))),
7692 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007693 (unsigned long long) resync/2,
7694 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007695
7696 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697 * dt: time from mark until now
7698 * db: blocks written from mark until now
7699 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007700 *
7701 * rt is a sector_t, so could be 32bit or 64bit.
7702 * So we divide before multiply in case it is 32bit and close
7703 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007704 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007705 * near the end of resync when the number of remaining sectors
7706 * is close to 'db'.
7707 * We then divide rt by 32 after multiplying by db to compensate.
7708 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709 */
7710 dt = ((jiffies - mddev->resync_mark) / HZ);
7711 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007712 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7713 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714
NeilBrowndd71cf62009-05-07 12:49:35 +10007715 rt = max_sectors - resync; /* number of remaining sectors */
7716 sector_div(rt, db/32+1);
7717 rt *= dt;
7718 rt >>= 5;
7719
7720 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7721 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007722
NeilBrownff4e8d92006-07-10 04:44:16 -07007723 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007724 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725}
7726
7727static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7728{
7729 struct list_head *tmp;
7730 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007731 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007732
7733 if (l >= 0x10000)
7734 return NULL;
7735 if (!l--)
7736 /* header */
7737 return (void*)1;
7738
7739 spin_lock(&all_mddevs_lock);
7740 list_for_each(tmp,&all_mddevs)
7741 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007742 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743 mddev_get(mddev);
7744 spin_unlock(&all_mddevs_lock);
7745 return mddev;
7746 }
7747 spin_unlock(&all_mddevs_lock);
7748 if (!l--)
7749 return (void*)2;/* tail */
7750 return NULL;
7751}
7752
7753static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7754{
7755 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007756 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007757
Linus Torvalds1da177e2005-04-16 15:20:36 -07007758 ++*pos;
7759 if (v == (void*)2)
7760 return NULL;
7761
7762 spin_lock(&all_mddevs_lock);
7763 if (v == (void*)1)
7764 tmp = all_mddevs.next;
7765 else
7766 tmp = mddev->all_mddevs.next;
7767 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007768 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769 else {
7770 next_mddev = (void*)2;
7771 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007773 spin_unlock(&all_mddevs_lock);
7774
7775 if (v != (void*)1)
7776 mddev_put(mddev);
7777 return next_mddev;
7778
7779}
7780
7781static void md_seq_stop(struct seq_file *seq, void *v)
7782{
NeilBrownfd01b882011-10-11 16:47:53 +11007783 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007784
7785 if (mddev && v != (void*)1 && v != (void*)2)
7786 mddev_put(mddev);
7787}
7788
7789static int md_seq_show(struct seq_file *seq, void *v)
7790{
NeilBrownfd01b882011-10-11 16:47:53 +11007791 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007792 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007793 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007794
7795 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007796 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007797 seq_printf(seq, "Personalities : ");
7798 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007799 list_for_each_entry(pers, &pers_list, list)
7800 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801
7802 spin_unlock(&pers_lock);
7803 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007804 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007805 return 0;
7806 }
7807 if (v == (void*)2) {
7808 status_unused(seq);
7809 return 0;
7810 }
7811
NeilBrown36d091f2014-12-15 12:56:58 +11007812 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7814 seq_printf(seq, "%s : %sactive", mdname(mddev),
7815 mddev->pers ? "" : "in");
7816 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007817 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007818 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007819 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007820 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821 seq_printf(seq, " %s", mddev->pers->name);
7822 }
7823
Andre Nolldd8ac332009-03-31 14:33:13 +11007824 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007825 rcu_read_lock();
7826 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827 char b[BDEVNAME_SIZE];
7828 seq_printf(seq, " %s[%d]",
7829 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007830 if (test_bit(WriteMostly, &rdev->flags))
7831 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007832 if (test_bit(Journal, &rdev->flags))
7833 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007834 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835 seq_printf(seq, "(F)");
7836 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007837 }
7838 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007839 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007840 if (test_bit(Replacement, &rdev->flags))
7841 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007842 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007844 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845
7846 if (!list_empty(&mddev->disks)) {
7847 if (mddev->pers)
7848 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007849 (unsigned long long)
7850 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851 else
7852 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007853 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007855 if (mddev->persistent) {
7856 if (mddev->major_version != 0 ||
7857 mddev->minor_version != 90) {
7858 seq_printf(seq," super %d.%d",
7859 mddev->major_version,
7860 mddev->minor_version);
7861 }
NeilBrowne6910632008-02-06 01:39:51 -08007862 } else if (mddev->external)
7863 seq_printf(seq, " super external:%s",
7864 mddev->metadata_type);
7865 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007866 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867
7868 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007869 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007870 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007871 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007872 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007873 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007874 }
NeilBrown32a76272005-06-21 17:17:14 -07007875 } else
7876 seq_printf(seq, "\n ");
7877
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007878 md_bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879
7880 seq_printf(seq, "\n");
7881 }
NeilBrown36d091f2014-12-15 12:56:58 +11007882 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007883
Linus Torvalds1da177e2005-04-16 15:20:36 -07007884 return 0;
7885}
7886
Jan Engelhardt110518b2009-05-07 12:49:37 +10007887static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888 .start = md_seq_start,
7889 .next = md_seq_next,
7890 .stop = md_seq_stop,
7891 .show = md_seq_show,
7892};
7893
7894static int md_seq_open(struct inode *inode, struct file *file)
7895{
Kay Sieversf1514632011-07-12 20:48:39 +02007896 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897 int error;
7898
7899 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007900 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007901 return error;
7902
7903 seq = file->private_data;
7904 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905 return error;
7906}
7907
NeilBrowne2f23b62014-04-09 14:33:51 +10007908static int md_unloading;
Al Viroafc9a422017-07-03 06:39:46 -04007909static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
NeilBrownd7603b72006-01-06 00:20:30 -08007910{
Kay Sieversf1514632011-07-12 20:48:39 +02007911 struct seq_file *seq = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -04007912 __poll_t mask;
NeilBrownd7603b72006-01-06 00:20:30 -08007913
NeilBrowne2f23b62014-04-09 14:33:51 +10007914 if (md_unloading)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007915 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007916 poll_wait(filp, &md_event_waiters, wait);
7917
7918 /* always allow read */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007919 mask = EPOLLIN | EPOLLRDNORM;
NeilBrownd7603b72006-01-06 00:20:30 -08007920
Kay Sieversf1514632011-07-12 20:48:39 +02007921 if (seq->poll_event != atomic_read(&md_event_count))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007922 mask |= EPOLLERR | EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007923 return mask;
7924}
7925
Arjan van de Venfa027c22007-02-12 00:55:33 -08007926static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007927 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007928 .open = md_seq_open,
7929 .read = seq_read,
7930 .llseek = seq_lseek,
Cihangir Akturk26e13042017-07-29 19:52:45 +03007931 .release = seq_release,
NeilBrownd7603b72006-01-06 00:20:30 -08007932 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007933};
7934
NeilBrown84fc4b52011-10-11 16:49:58 +11007935int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936{
NeilBrown9d487392016-11-02 14:16:49 +11007937 pr_debug("md: %s personality registered for level %d\n",
7938 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007939 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007940 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941 spin_unlock(&pers_lock);
7942 return 0;
7943}
NeilBrown6c144d32014-09-30 16:15:38 +10007944EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945
NeilBrown84fc4b52011-10-11 16:49:58 +11007946int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007947{
NeilBrown9d487392016-11-02 14:16:49 +11007948 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007950 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007951 spin_unlock(&pers_lock);
7952 return 0;
7953}
NeilBrown6c144d32014-09-30 16:15:38 +10007954EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007955
NeilBrown6022e752015-08-13 12:32:55 +10007956int register_md_cluster_operations(struct md_cluster_operations *ops,
7957 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007958{
NeilBrown6022e752015-08-13 12:32:55 +10007959 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007960 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007961 if (md_cluster_ops != NULL)
7962 ret = -EALREADY;
7963 else {
7964 md_cluster_ops = ops;
7965 md_cluster_mod = module;
7966 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007967 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007968 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007969}
7970EXPORT_SYMBOL(register_md_cluster_operations);
7971
7972int unregister_md_cluster_operations(void)
7973{
7974 spin_lock(&pers_lock);
7975 md_cluster_ops = NULL;
7976 spin_unlock(&pers_lock);
7977 return 0;
7978}
7979EXPORT_SYMBOL(unregister_md_cluster_operations);
7980
7981int md_setup_cluster(struct mddev *mddev, int nodes)
7982{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007983 if (!md_cluster_ops)
7984 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007985 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007986 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007987 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11007988 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007989 spin_unlock(&pers_lock);
7990 return -ENOENT;
7991 }
7992 spin_unlock(&pers_lock);
7993
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007994 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007995}
7996
7997void md_cluster_stop(struct mddev *mddev)
7998{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007999 if (!md_cluster_ops)
8000 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008001 md_cluster_ops->leave(mddev);
8002 module_put(md_cluster_mod);
8003}
8004
NeilBrownfd01b882011-10-11 16:47:53 +11008005static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006{
NeilBrownf72ffdd2014-09-30 14:23:59 +10008007 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008008 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11008009 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008010
8011 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10008012 rcu_read_lock();
8013 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008014 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Michael Callahan59767fb2018-07-18 04:47:37 -07008015 curr_events = (int)part_stat_read_accum(&disk->part0, sectors) -
NeilBrowneea1bf32009-03-31 14:27:02 +11008016 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07008017 /* sync IO will cause sync_io to increase before the disk_stats
8018 * as sync_io is counted when a request starts, and
8019 * disk_stats is counted when it completes.
8020 * So resync activity will cause curr_events to be smaller than
8021 * when there was no such activity.
8022 * non-sync IO will cause disk_stat to increase without
8023 * increasing sync_io so curr_events will (eventually)
8024 * be larger than it was before. Once it becomes
8025 * substantially larger, the test below will cause
8026 * the array to appear non-idle, and resync will slow
8027 * down.
8028 * If there is a lot of outstanding resync activity when
8029 * we set last_event to curr_events, then all that activity
8030 * completing might cause the array to appear non-idle
8031 * and resync will be slowed down even though there might
8032 * not have been non-resync activity. This will only
8033 * happen once though. 'last_events' will soon reflect
8034 * the state where there is little or no outstanding
8035 * resync requests, and further resync activity will
8036 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08008037 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008038 */
NeilBrowneea1bf32009-03-31 14:27:02 +11008039 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008040 rdev->last_events = curr_events;
8041 idle = 0;
8042 }
8043 }
NeilBrown4b809912008-07-21 17:05:25 +10008044 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045 return idle;
8046}
8047
NeilBrownfd01b882011-10-11 16:47:53 +11008048void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008049{
8050 /* another "blocks" (512byte) blocks have been synced */
8051 atomic_sub(blocks, &mddev->recovery_active);
8052 wake_up(&mddev->recovery_wait);
8053 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07008054 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08008055 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056 md_wakeup_thread(mddev->thread);
8057 // stop recovery, signal do_sync ....
8058 }
8059}
NeilBrown6c144d32014-09-30 16:15:38 +10008060EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008061
NeilBrown06d91a52005-06-21 17:17:12 -07008062/* md_write_start(mddev, bi)
8063 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07008064 * in superblock) before writing, schedule a superblock update
8065 * and wait for it to complete.
NeilBrowncc27b0c2017-06-05 16:49:39 +10008066 * A return value of 'false' means that the write wasn't recorded
8067 * and cannot proceed as the array is being suspend.
NeilBrown06d91a52005-06-21 17:17:12 -07008068 */
NeilBrowncc27b0c2017-06-05 16:49:39 +10008069bool md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070{
Neil Brown0fd62b82008-06-28 08:31:36 +10008071 int did_change = 0;
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008072
NeilBrown06d91a52005-06-21 17:17:12 -07008073 if (bio_data_dir(bi) != WRITE)
NeilBrowncc27b0c2017-06-05 16:49:39 +10008074 return true;
NeilBrown06d91a52005-06-21 17:17:12 -07008075
NeilBrownf91de922005-11-08 21:39:36 -08008076 BUG_ON(mddev->ro == 1);
8077 if (mddev->ro == 2) {
8078 /* need to switch to read/write */
8079 mddev->ro = 0;
8080 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8081 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08008082 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008083 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08008084 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008085 rcu_read_lock();
8086 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11008087 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07008088 if (mddev->safemode == 1)
8089 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11008090 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
NeilBrown81fe48e2017-08-08 16:56:36 +10008091 if (mddev->in_sync || mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11008092 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07008093 if (mddev->in_sync) {
8094 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008095 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8096 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07008097 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008098 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07008099 }
NeilBrown85572d72014-12-15 12:56:56 +11008100 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07008101 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008102 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10008103 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008104 sysfs_notify_dirent_safe(mddev->sysfs_state);
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008105 if (!mddev->has_superblocks)
8106 return true;
NeilBrown09a44cc2008-05-23 13:04:36 -07008107 wait_event(mddev->sb_wait,
NeilBrownd47c8ad2017-10-05 16:23:16 +11008108 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8109 mddev->suspended);
NeilBrowncc27b0c2017-06-05 16:49:39 +10008110 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8111 percpu_ref_put(&mddev->writes_pending);
8112 return false;
8113 }
8114 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008115}
NeilBrown6c144d32014-09-30 16:15:38 +10008116EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008117
NeilBrown49728052017-03-15 14:05:12 +11008118/* md_write_inc can only be called when md_write_start() has
8119 * already been called at least once of the current request.
8120 * It increments the counter and is useful when a single request
8121 * is split into several parts. Each part causes an increment and
8122 * so needs a matching md_write_end().
8123 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8124 * a spinlocked region.
8125 */
8126void md_write_inc(struct mddev *mddev, struct bio *bi)
8127{
8128 if (bio_data_dir(bi) != WRITE)
8129 return;
8130 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008131 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008132}
8133EXPORT_SYMBOL(md_write_inc);
8134
NeilBrownfd01b882011-10-11 16:47:53 +11008135void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008136{
NeilBrown4ad23a972017-03-15 14:05:14 +11008137 percpu_ref_put(&mddev->writes_pending);
8138
8139 if (mddev->safemode == 2)
8140 md_wakeup_thread(mddev->thread);
8141 else if (mddev->safemode_delay)
8142 /* The roundup() ensures this only performs locking once
8143 * every ->safemode_delay jiffies
8144 */
8145 mod_timer(&mddev->safemode_timer,
8146 roundup(jiffies, mddev->safemode_delay) +
8147 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008148}
NeilBrown4ad23a972017-03-15 14:05:14 +11008149
NeilBrown6c144d32014-09-30 16:15:38 +10008150EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151
NeilBrown2a2275d2007-01-26 00:57:11 -08008152/* md_allow_write(mddev)
8153 * Calling this ensures that the array is marked 'active' so that writes
8154 * may proceed without blocking. It is important to call this before
8155 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8156 * Must be called with mddev_lock held.
8157 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008158void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008159{
8160 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008161 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008162 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008163 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008164 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008165 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008166
NeilBrown85572d72014-12-15 12:56:56 +11008167 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008168 if (mddev->in_sync) {
8169 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008170 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8171 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008172 if (mddev->safemode_delay &&
8173 mddev->safemode == 0)
8174 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008175 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008176 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008177 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008178 /* wait for the dirty state to be recorded in the metadata */
8179 wait_event(mddev->sb_wait,
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008180 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008181 } else
NeilBrown85572d72014-12-15 12:56:56 +11008182 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008183}
8184EXPORT_SYMBOL_GPL(md_allow_write);
8185
Linus Torvalds1da177e2005-04-16 15:20:36 -07008186#define SYNC_MARKS 10
8187#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008188#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008189void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008190{
Shaohua Li4ed87312012-10-11 13:34:00 +11008191 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008192 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193 unsigned int currspeed = 0,
8194 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008195 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008197 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008198 sector_t mark_cnt[SYNC_MARKS];
8199 int last_mark,m;
8200 struct list_head *tmp;
8201 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008202 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008203 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008204 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008205 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008206 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008207
8208 /* just incase thread restarts... */
Song Liud5d885f2017-11-19 22:17:01 -08008209 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8210 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008211 return;
NeilBrown3991b312014-05-28 13:39:23 +10008212 if (mddev->ro) {/* never try to sync a read-only array */
8213 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008214 return;
NeilBrown3991b312014-05-28 13:39:23 +10008215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008217 if (mddev_is_clustered(mddev)) {
8218 ret = md_cluster_ops->resync_start(mddev);
8219 if (ret)
8220 goto skip;
8221
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008222 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008223 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8224 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8225 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8226 && ((unsigned long long)mddev->curr_resync_completed
8227 < (unsigned long long)mddev->resync_max_sectors))
8228 goto skip;
8229 }
8230
NeilBrown61df9d92006-10-03 01:15:57 -07008231 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008232 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008233 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008234 action = "check";
8235 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008236 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008237 action = "repair";
8238 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008239 desc = "resync";
8240 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8241 desc = "reshape";
8242 else
8243 desc = "recovery";
8244
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008245 mddev->last_sync_action = action ?: desc;
8246
Linus Torvalds1da177e2005-04-16 15:20:36 -07008247 /* we overload curr_resync somewhat here.
8248 * 0 == not engaged in resync at all
8249 * 2 == checking that there is no conflict with another sync
8250 * 1 == like 2, but have yielded to allow conflicting resync to
8251 * commense
8252 * other == active in resync - this many blocks
8253 *
8254 * Before starting a resync we must have set curr_resync to
8255 * 2, and then checked that every "conflicting" array has curr_resync
8256 * less than ours. When we find one that is the same or higher
8257 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8258 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8259 * This will mean we have to start checking from the beginning again.
8260 *
8261 */
8262
8263 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008264 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008265 mddev->curr_resync = 2;
8266
8267 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008268 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008270 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008271 if (mddev2 == mddev)
8272 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008273 if (!mddev->parallel_resync
8274 && mddev2->curr_resync
8275 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008276 DEFINE_WAIT(wq);
8277 if (mddev < mddev2 && mddev->curr_resync == 2) {
8278 /* arbitrarily yield */
8279 mddev->curr_resync = 1;
8280 wake_up(&resync_wait);
8281 }
8282 if (mddev > mddev2 && mddev->curr_resync == 1)
8283 /* no need to wait here, we can wait the next
8284 * time 'round when curr_resync == 2
8285 */
8286 continue;
NeilBrown97441972008-09-19 11:49:54 +10008287 /* We need to wait 'interruptible' so as not to
8288 * contribute to the load average, and not to
8289 * be caught by 'softlockup'
8290 */
8291 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008292 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008293 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008294 if (mddev2_minor != mddev2->md_minor) {
8295 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008296 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8297 desc, mdname(mddev),
8298 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008300 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008301 if (signal_pending(current))
8302 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008303 schedule();
8304 finish_wait(&resync_wait, &wq);
8305 goto try_again;
8306 }
8307 finish_wait(&resync_wait, &wq);
8308 }
8309 }
8310 } while (mddev->curr_resync < 2);
8311
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008312 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008313 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008314 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008315 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008316 */
8317 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008318 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008319 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008320 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8321 j = mddev->resync_min;
8322 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008323 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008324
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008325 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
NeilBrownc804cde2012-05-21 09:28:33 +10008326 max_sectors = mddev->resync_max_sectors;
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008327 /*
8328 * If the original node aborts reshaping then we continue the
8329 * reshaping, so set j again to avoid restart reshape from the
8330 * first beginning
8331 */
8332 if (mddev_is_clustered(mddev) &&
8333 mddev->reshape_position != MaxSector)
8334 j = mddev->reshape_position;
8335 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008336 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008337 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008338 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008339 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008340 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008341 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008342 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008343 !test_bit(Faulty, &rdev->flags) &&
8344 !test_bit(In_sync, &rdev->flags) &&
8345 rdev->recovery_offset < j)
8346 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008347 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008348
8349 /* If there is a bitmap, we need to make sure all
8350 * writes that started before we added a spare
8351 * complete before we start doing a recovery.
8352 * Otherwise the write might complete and (via
8353 * bitmap_endwrite) set a bit in the bitmap after the
8354 * recovery has checked that bit and skipped that
8355 * region.
8356 */
8357 if (mddev->bitmap) {
8358 mddev->pers->quiesce(mddev, 1);
8359 mddev->pers->quiesce(mddev, 0);
8360 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362
NeilBrown9d487392016-11-02 14:16:49 +11008363 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8364 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8365 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8366 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008367
NeilBrowneea1bf32009-03-31 14:27:02 +11008368 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008369
NeilBrown57afd892005-06-21 17:17:13 -07008370 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008371 for (m = 0; m < SYNC_MARKS; m++) {
8372 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008373 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374 }
8375 last_mark = 0;
8376 mddev->resync_mark = mark[last_mark];
8377 mddev->resync_mark_cnt = mark_cnt[last_mark];
8378
8379 /*
8380 * Tune reconstruction:
8381 */
8382 window = 32*(PAGE_SIZE/512);
NeilBrown9d487392016-11-02 14:16:49 +11008383 pr_debug("md: using %dk window, over a total of %lluk.\n",
8384 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008385
8386 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008387 last_check = 0;
8388
8389 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008390 pr_debug("md: resuming %s of %s from checkpoint.\n",
8391 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008392 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008393 } else
8394 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008395 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008396 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8397 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008398 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008399
majianpeng7c2c57c2012-07-03 12:12:26 +10008400 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008401 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008402 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008403
NeilBrown57afd892005-06-21 17:17:13 -07008404 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008405
NeilBrown7a91ee12009-05-26 12:57:21 +10008406 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8407 ((mddev->curr_resync > mddev->curr_resync_completed &&
8408 (mddev->curr_resync - mddev->curr_resync_completed)
8409 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008410 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008411 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008412 >= mddev->resync_max - mddev->curr_resync_completed ||
8413 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008414 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008415 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008416 wait_event(mddev->recovery_wait,
8417 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008418 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008419 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8420 j > mddev->recovery_cp)
8421 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008422 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008423 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008424 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008425 }
NeilBrownacb180b2009-04-14 16:28:34 +10008426
NeilBrownc91abf52013-11-19 12:02:01 +11008427 while (j >= mddev->resync_max &&
8428 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008429 /* As this condition is controlled by user-space,
8430 * we can block indefinitely, so use '_interruptible'
8431 * to avoid triggering warnings.
8432 */
8433 flush_signals(current); /* just in case */
8434 wait_event_interruptible(mddev->recovery_wait,
8435 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008436 || test_bit(MD_RECOVERY_INTR,
8437 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008438 }
NeilBrownacb180b2009-04-14 16:28:34 +10008439
NeilBrownc91abf52013-11-19 12:02:01 +11008440 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8441 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008442
NeilBrown09314792015-02-19 16:04:40 +11008443 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008444 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008445 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008446 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008447 }
NeilBrown57afd892005-06-21 17:17:13 -07008448
8449 if (!skipped) { /* actual IO requested */
8450 io_sectors += sectors;
8451 atomic_add(sectors, &mddev->recovery_active);
8452 }
8453
NeilBrowne875ece2011-07-28 11:39:24 +10008454 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8455 break;
8456
Linus Torvalds1da177e2005-04-16 15:20:36 -07008457 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008458 if (j > max_sectors)
8459 /* when skipping, extra large numbers can be returned. */
8460 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008461 if (j > 2)
8462 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008463 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008464 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008465 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008466 * visible in /proc/mdstat
8467 */
8468 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008469
8470 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471 continue;
8472
NeilBrown57afd892005-06-21 17:17:13 -07008473 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008474 repeat:
8475 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8476 /* step marks */
8477 int next = (last_mark+1) % SYNC_MARKS;
8478
8479 mddev->resync_mark = mark[next];
8480 mddev->resync_mark_cnt = mark_cnt[next];
8481 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008482 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008483 last_mark = next;
8484 }
8485
NeilBrownc91abf52013-11-19 12:02:01 +11008486 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8487 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008488
8489 /*
8490 * this loop exits only if either when we are slower than
8491 * the 'hard' speed limit, or the system was IO-idle for
8492 * a jiffy.
8493 * the system might be non-idle CPU-wise, but we only care
8494 * about not overloading the IO subsystem. (things like an
8495 * e2fsck being done on the RAID array should execute fast)
8496 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008497 cond_resched();
8498
Xiao Niac7e50a2014-08-07 09:37:41 -04008499 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8500 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008501 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008502
NeilBrown88202a02006-01-06 00:21:36 -08008503 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008504 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008505 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008506 goto repeat;
8507 }
NeilBrownac8fa412015-02-19 16:55:00 +11008508 if (!is_mddev_idle(mddev, 0)) {
8509 /*
8510 * Give other IO more of a chance.
8511 * The faster the devices, the less we wait.
8512 */
8513 wait_event(mddev->recovery_wait,
8514 !atomic_read(&mddev->recovery_active));
8515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516 }
8517 }
NeilBrown9d487392016-11-02 14:16:49 +11008518 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8519 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8520 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008521 /*
8522 * this also signals 'finished resyncing' to md_stop
8523 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008524 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008525 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8526
NeilBrown5ed1df22015-07-24 13:27:08 +10008527 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8528 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008529 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008530 mddev->curr_resync_completed = mddev->curr_resync;
8531 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8532 }
NeilBrown09314792015-02-19 16:04:40 +11008533 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534
NeilBrowndfc70642008-05-23 13:04:39 -07008535 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008536 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008537 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8538 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8539 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008540 pr_debug("md: checkpointing %s of %s.\n",
8541 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008542 if (test_bit(MD_RECOVERY_ERROR,
8543 &mddev->recovery))
8544 mddev->recovery_cp =
8545 mddev->curr_resync_completed;
8546 else
8547 mddev->recovery_cp =
8548 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008549 }
8550 } else
8551 mddev->recovery_cp = MaxSector;
8552 } else {
8553 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8554 mddev->curr_resync = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11008555 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8556 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
8557 rcu_read_lock();
8558 rdev_for_each_rcu(rdev, mddev)
8559 if (rdev->raid_disk >= 0 &&
8560 mddev->delta_disks >= 0 &&
8561 !test_bit(Journal, &rdev->flags) &&
8562 !test_bit(Faulty, &rdev->flags) &&
8563 !test_bit(In_sync, &rdev->flags) &&
8564 rdev->recovery_offset < mddev->curr_resync)
8565 rdev->recovery_offset = mddev->curr_resync;
8566 rcu_read_unlock();
8567 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008569 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008570 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008571 /* set CHANGE_PENDING here since maybe another update is needed,
8572 * so other nodes are informed. It should be harmless for normal
8573 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008574 set_mask_bits(&mddev->sb_flags, 0,
8575 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008576
BingJing Chang88763912018-02-22 13:34:46 +08008577 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8578 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8579 mddev->delta_disks > 0 &&
8580 mddev->pers->finish_reshape &&
8581 mddev->pers->size &&
8582 mddev->queue) {
8583 mddev_lock_nointr(mddev);
8584 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
8585 mddev_unlock(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008586 if (!mddev_is_clustered(mddev)) {
8587 set_capacity(mddev->gendisk, mddev->array_sectors);
8588 revalidate_disk(mddev->gendisk);
8589 }
BingJing Chang88763912018-02-22 13:34:46 +08008590 }
8591
NeilBrown23da4222014-12-15 12:57:01 +11008592 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008593 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8594 /* We completed so min/max setting can be forgotten if used. */
8595 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8596 mddev->resync_min = 0;
8597 mddev->resync_max = MaxSector;
8598 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8599 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008600 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008601 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008602 spin_unlock(&mddev->lock);
8603
Linus Torvalds1da177e2005-04-16 15:20:36 -07008604 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008605 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008606 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008607}
NeilBrown29269552006-03-27 01:18:10 -08008608EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008609
NeilBrown746d3202013-04-24 11:42:41 +10008610static int remove_and_add_spares(struct mddev *mddev,
8611 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008612{
NeilBrown3cb03002011-10-11 16:45:26 +11008613 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008614 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008615 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008616 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008617
NeilBrown39772f02018-02-03 09:19:30 +11008618 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
8619 /* Mustn't remove devices when resync thread is running */
8620 return 0;
8621
NeilBrownd787be42016-06-02 16:19:53 +10008622 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008623 if ((this == NULL || rdev == this) &&
8624 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008625 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008626 test_bit(Faulty, &rdev->flags) &&
8627 atomic_read(&rdev->nr_pending)==0) {
8628 /* Faulty non-Blocked devices with nr_pending == 0
8629 * never get nr_pending incremented,
8630 * never get Faulty cleared, and never get Blocked set.
8631 * So we can synchronize_rcu now rather than once per device
8632 */
8633 remove_some = true;
8634 set_bit(RemoveSynchronized, &rdev->flags);
8635 }
8636 }
8637
8638 if (remove_some)
8639 synchronize_rcu();
8640 rdev_for_each(rdev, mddev) {
8641 if ((this == NULL || rdev == this) &&
8642 rdev->raid_disk >= 0 &&
8643 !test_bit(Blocked, &rdev->flags) &&
8644 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008645 (!test_bit(In_sync, &rdev->flags) &&
8646 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008647 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008648 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008649 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008650 sysfs_unlink_rdev(mddev, rdev);
NeilBrown011abdc2018-04-26 14:46:29 +10008651 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008652 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008653 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008654 }
8655 }
NeilBrownd787be42016-06-02 16:19:53 +10008656 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8657 clear_bit(RemoveSynchronized, &rdev->flags);
8658 }
8659
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008660 if (removed && mddev->kobj.sd)
8661 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008662
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008663 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008664 goto no_add;
8665
NeilBrowndafb20f2012-03-19 12:46:39 +11008666 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008667 if (this && this != rdev)
8668 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008669 if (test_bit(Candidate, &rdev->flags))
8670 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008671 if (rdev->raid_disk >= 0 &&
8672 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008673 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008674 !test_bit(Faulty, &rdev->flags))
8675 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008676 if (rdev->raid_disk >= 0)
8677 continue;
8678 if (test_bit(Faulty, &rdev->flags))
8679 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008680 if (!test_bit(Journal, &rdev->flags)) {
8681 if (mddev->ro &&
8682 ! (rdev->saved_raid_disk >= 0 &&
8683 !test_bit(Bitmap_sync, &rdev->flags)))
8684 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008685
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008686 rdev->recovery_offset = 0;
8687 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008688 if (mddev->pers->
8689 hot_add_disk(mddev, rdev) == 0) {
8690 if (sysfs_link_rdev(mddev, rdev))
8691 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008692 if (!test_bit(Journal, &rdev->flags))
8693 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008694 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08008695 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008696 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008697 }
NeilBrown746d3202013-04-24 11:42:41 +10008698no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008699 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08008700 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008701 return spares;
8702}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008703
NeilBrownac05f252014-09-30 08:10:42 +10008704static void md_start_sync(struct work_struct *ws)
8705{
8706 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008707
NeilBrownac05f252014-09-30 08:10:42 +10008708 mddev->sync_thread = md_register_thread(md_do_sync,
8709 mddev,
8710 "resync");
8711 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11008712 pr_warn("%s: could not start resync thread...\n",
8713 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008714 /* leave the spares where they are, it shouldn't hurt */
8715 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8716 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8717 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8718 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8719 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008720 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008721 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8722 &mddev->recovery))
8723 if (mddev->sysfs_action)
8724 sysfs_notify_dirent_safe(mddev->sysfs_action);
8725 } else
8726 md_wakeup_thread(mddev->sync_thread);
8727 sysfs_notify_dirent_safe(mddev->sysfs_action);
8728 md_new_event(mddev);
8729}
8730
Linus Torvalds1da177e2005-04-16 15:20:36 -07008731/*
8732 * This routine is regularly called by all per-raid-array threads to
8733 * deal with generic issues like resync and super-block update.
8734 * Raid personalities that don't have a thread (linear/raid0) do not
8735 * need this as they never do any recovery or update the superblock.
8736 *
8737 * It does not do any resync itself, but rather "forks" off other threads
8738 * to do that as needed.
8739 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8740 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008741 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008742 * and wakeups up this thread which will reap the thread and finish up.
8743 * This thread also removes any faulty devices (with nr_pending == 0).
8744 *
8745 * The overall approach is:
8746 * 1/ if the superblock needs updating, update it.
8747 * 2/ If a recovery thread is running, don't do anything else.
8748 * 3/ If recovery has finished, clean up, possibly marking spares active.
8749 * 4/ If there are any faulty devices, remove them.
8750 * 5/ If array is degraded, try to add spares devices
8751 * 6/ If array has spares or is not in-sync, start a resync thread.
8752 */
NeilBrownfd01b882011-10-11 16:47:53 +11008753void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008754{
NeilBrown059421e2018-10-03 15:04:41 +10008755 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
8756 /* Write superblock - thread that called mddev_suspend()
8757 * holds reconfig_mutex for us.
8758 */
8759 set_bit(MD_UPDATING_SB, &mddev->flags);
8760 smp_mb__after_atomic();
8761 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
8762 md_update_sb(mddev, 0);
8763 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
8764 wake_up(&mddev->sb_wait);
8765 }
8766
Jonathan Brassow68866e42011-06-08 15:10:08 +10008767 if (mddev->suspended)
8768 return;
8769
NeilBrown5f404022005-06-21 17:17:16 -07008770 if (mddev->bitmap)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008771 md_bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008772
NeilBrownfca4d842005-06-21 17:17:11 -07008773 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008774 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11008775 pr_debug("md: %s in immediate safe mode\n",
8776 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07008777 mddev->safemode = 2;
8778 }
8779 flush_signals(current);
8780 }
8781
NeilBrownc89a8ee2008-08-05 15:54:13 +10008782 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8783 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008784 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08008785 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008786 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008787 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008788 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11008789 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07008790 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008791 ))
8792 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008793
NeilBrowndf5b89b2006-03-27 01:18:20 -08008794 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008795 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008796
Shaohua Liafc1f552017-08-11 20:34:45 -07008797 if (!mddev->external && mddev->safemode == 1)
NeilBrown33182d12017-08-08 16:56:36 +10008798 mddev->safemode = 0;
8799
NeilBrownc89a8ee2008-08-05 15:54:13 +10008800 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008801 struct md_rdev *rdev;
8802 if (!mddev->external && mddev->in_sync)
8803 /* 'Blocked' flag not needed as failed devices
8804 * will be recorded if array switched to read/write.
8805 * Leaving it set will prevent the device
8806 * from being removed.
8807 */
8808 rdev_for_each(rdev, mddev)
8809 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008810 /* On a read-only array we can:
8811 * - remove failed devices
8812 * - add already-in_sync devices if the array itself
8813 * is in-sync.
8814 * As we only add devices that are already in-sync,
8815 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008816 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008817 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008818 /* There is no thread, but we need to call
8819 * ->spare_active and clear saved_raid_disk
8820 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008821 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008822 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008823 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008824 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08008825 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008826 goto unlock;
8827 }
8828
Guoqing Jiang659b2542015-12-21 10:50:59 +11008829 if (mddev_is_clustered(mddev)) {
8830 struct md_rdev *rdev;
8831 /* kick the device if another node issued a
8832 * remove disk.
8833 */
8834 rdev_for_each(rdev, mddev) {
8835 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8836 rdev->raid_disk < 0)
8837 md_kick_rdev_from_array(rdev);
8838 }
8839 }
8840
NeilBrown6497709b2017-03-15 14:05:14 +11008841 if (!mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11008842 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11008843 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11008844 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07008845 }
NeilBrownfca4d842005-06-21 17:17:11 -07008846
Shaohua Li29530792016-12-08 15:48:19 -08008847 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07008848 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008849
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8851 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8852 /* resync/recovery still happening */
8853 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8854 goto unlock;
8855 }
8856 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008857 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858 goto unlock;
8859 }
Neil Brown72a23c22008-06-28 08:31:41 +10008860 /* Set RUNNING before clearing NEEDED to avoid
8861 * any transients in the value of "sync_action".
8862 */
NeilBrown72f36d52012-10-11 14:25:57 +11008863 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008864 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008865 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008866 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008867 /* Clear some bits that don't mean anything, but
8868 * might be left set
8869 */
NeilBrown24dd4692005-11-08 21:39:26 -08008870 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8871 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008872
NeilBrowned209582012-04-24 10:23:14 +10008873 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8874 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008875 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008876 /* no recovery is running.
8877 * remove any failed drives, then
8878 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008879 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008880 * the personality to fail the re-add.
8881 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008882
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008883 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008884 if (mddev->pers->check_reshape == NULL ||
8885 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008886 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008887 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008888 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008889 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008890 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008891 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8892 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008893 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008894 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008895 } else if (mddev->recovery_cp < MaxSector) {
8896 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008897 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008898 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8899 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008900 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008901
Linus Torvalds1da177e2005-04-16 15:20:36 -07008902 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008903 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008904 /* We are adding a device or devices to an array
8905 * which has the bitmap stored on all devices.
8906 * So make sure all bitmap pages get written
8907 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008908 md_bitmap_write_all(mddev->bitmap);
NeilBrowna654b9d82005-06-21 17:17:27 -07008909 }
NeilBrownac05f252014-09-30 08:10:42 +10008910 INIT_WORK(&mddev->del_work, md_start_sync);
8911 queue_work(md_misc_wq, &mddev->del_work);
8912 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008913 }
NeilBrownac05f252014-09-30 08:10:42 +10008914 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008915 if (!mddev->sync_thread) {
8916 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008917 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008918 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8919 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008920 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008921 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008922 }
NeilBrownac05f252014-09-30 08:10:42 +10008923 unlock:
8924 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925 mddev_unlock(mddev);
8926 }
8927}
NeilBrown6c144d32014-09-30 16:15:38 +10008928EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008929
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008930void md_reap_sync_thread(struct mddev *mddev)
8931{
8932 struct md_rdev *rdev;
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008933 sector_t old_dev_sectors = mddev->dev_sectors;
8934 bool is_reshaped = false;
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008935
8936 /* resync has finished, collect result */
8937 md_unregister_thread(&mddev->sync_thread);
8938 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8939 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8940 /* success...*/
8941 /* activate any spares */
8942 if (mddev->pers->spare_active(mddev)) {
8943 sysfs_notify(&mddev->kobj, NULL,
8944 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08008945 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008946 }
8947 }
8948 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008949 mddev->pers->finish_reshape) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008950 mddev->pers->finish_reshape(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008951 if (mddev_is_clustered(mddev))
8952 is_reshaped = true;
8953 }
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008954
8955 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008956 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008957 */
NeilBrownf4667222013-12-09 12:04:56 +11008958 if (!mddev->degraded)
8959 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008960 rdev->saved_raid_disk = -1;
8961
8962 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08008963 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008964 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8965 * clustered raid */
8966 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8967 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008968 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008969 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008970 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8971 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8972 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8973 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008974 /*
8975 * We call md_cluster_ops->update_size here because sync_size could
8976 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
8977 * so it is time to update size across cluster.
8978 */
8979 if (mddev_is_clustered(mddev) && is_reshaped
8980 && !test_bit(MD_CLOSING, &mddev->flags))
8981 md_cluster_ops->update_size(mddev, old_dev_sectors);
NeilBrownf851b602014-12-11 10:02:10 +11008982 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008983 /* flag recovery needed just to double check */
8984 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8985 sysfs_notify_dirent_safe(mddev->sysfs_action);
8986 md_new_event(mddev);
8987 if (mddev->event_work.func)
8988 queue_work(md_misc_wq, &mddev->event_work);
8989}
NeilBrown6c144d32014-09-30 16:15:38 +10008990EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008991
NeilBrownfd01b882011-10-11 16:47:53 +11008992void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008993{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008994 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008995 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008996 !test_bit(Blocked, &rdev->flags) &&
8997 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008998 msecs_to_jiffies(5000));
8999 rdev_dec_pending(rdev, mddev);
9000}
9001EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9002
NeilBrownc6563a82012-05-21 09:27:00 +10009003void md_finish_reshape(struct mddev *mddev)
9004{
9005 /* called be personality module when reshape completes. */
9006 struct md_rdev *rdev;
9007
9008 rdev_for_each(rdev, mddev) {
9009 if (rdev->data_offset > rdev->new_data_offset)
9010 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9011 else
9012 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9013 rdev->data_offset = rdev->new_data_offset;
9014 }
9015}
9016EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10009017
Vishal Vermafc974ee2015-12-24 19:20:34 -07009018/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10009019
Vishal Vermafc974ee2015-12-24 19:20:34 -07009020/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11009021int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10009022 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009023{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04009024 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10009025 int rv;
9026 if (is_new)
9027 s += rdev->new_data_offset;
9028 else
9029 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07009030 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9031 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10009032 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009033 if (test_bit(ExternalBbl, &rdev->flags))
9034 sysfs_notify(&rdev->kobj, NULL,
9035 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11009036 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08009037 set_mask_bits(&mddev->sb_flags, 0,
9038 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10009039 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07009040 return 1;
9041 } else
9042 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10009043}
9044EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9045
NeilBrownc6563a82012-05-21 09:27:00 +10009046int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9047 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009048{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009049 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10009050 if (is_new)
9051 s += rdev->new_data_offset;
9052 else
9053 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009054 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9055 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
9056 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
9057 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10009058}
9059EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9060
Adrian Bunk75c96f82005-05-05 16:16:09 -07009061static int md_notify_reboot(struct notifier_block *this,
9062 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009063{
9064 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009065 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009066 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009067
NeilBrownc744a652012-03-19 12:46:37 +11009068 for_each_mddev(mddev, tmp) {
9069 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009070 if (mddev->pers)
9071 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009072 if (mddev->persistent)
9073 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009074 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009075 }
NeilBrownc744a652012-03-19 12:46:37 +11009076 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009077 }
NeilBrownc744a652012-03-19 12:46:37 +11009078 /*
9079 * certain more exotic SCSI devices are known to be
9080 * volatile wrt too early system reboots. While the
9081 * right place to handle this issue is the given
9082 * driver, we do want to have a safe RAID driver ...
9083 */
9084 if (need_delay)
9085 mdelay(1000*1);
9086
Linus Torvalds1da177e2005-04-16 15:20:36 -07009087 return NOTIFY_DONE;
9088}
9089
Adrian Bunk75c96f82005-05-05 16:16:09 -07009090static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009091 .notifier_call = md_notify_reboot,
9092 .next = NULL,
9093 .priority = INT_MAX, /* before any real devices */
9094};
9095
9096static void md_geninit(void)
9097{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009098 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009099
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07009100 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009101}
9102
Adrian Bunk75c96f82005-05-05 16:16:09 -07009103static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104{
Tejun Heoe804ac72010-10-15 15:36:08 +02009105 int ret = -ENOMEM;
9106
Tejun Heoada609e2011-01-25 14:35:54 +01009107 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009108 if (!md_wq)
9109 goto err_wq;
9110
9111 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9112 if (!md_misc_wq)
9113 goto err_misc_wq;
9114
9115 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9116 goto err_md;
9117
9118 if ((ret = register_blkdev(0, "mdp")) < 0)
9119 goto err_mdp;
9120 mdp_major = ret;
9121
NeilBrownaf5628f2014-07-31 13:54:54 +10009122 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009123 md_probe, NULL, NULL);
9124 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009125 md_probe, NULL, NULL);
9126
Linus Torvalds1da177e2005-04-16 15:20:36 -07009127 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009128 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009129
9130 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009132
Tejun Heoe804ac72010-10-15 15:36:08 +02009133err_mdp:
9134 unregister_blkdev(MD_MAJOR, "md");
9135err_md:
9136 destroy_workqueue(md_misc_wq);
9137err_misc_wq:
9138 destroy_workqueue(md_wq);
9139err_wq:
9140 return ret;
9141}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009142
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009143static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009144{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009145 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9146 struct md_rdev *rdev2;
9147 int role, ret;
9148 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009149
Guoqing Jiang818da592017-03-01 16:42:40 +08009150 /*
9151 * If size is changed in another node then we need to
9152 * do resize as well.
9153 */
9154 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9155 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9156 if (ret)
9157 pr_info("md-cluster: resize failed\n");
9158 else
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009159 md_bitmap_update_sb(mddev->bitmap);
Guoqing Jiang818da592017-03-01 16:42:40 +08009160 }
9161
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009162 /* Check for change of roles in the active devices */
9163 rdev_for_each(rdev2, mddev) {
9164 if (test_bit(Faulty, &rdev2->flags))
9165 continue;
9166
9167 /* Check if the roles changed */
9168 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009169
9170 if (test_bit(Candidate, &rdev2->flags)) {
9171 if (role == 0xfffe) {
9172 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9173 md_kick_rdev_from_array(rdev2);
9174 continue;
9175 }
9176 else
9177 clear_bit(Candidate, &rdev2->flags);
9178 }
9179
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009180 if (role != rdev2->raid_disk) {
Guoqing Jiangca1e98e2018-10-18 16:37:45 +08009181 /*
9182 * got activated except reshape is happening.
9183 */
9184 if (rdev2->raid_disk == -1 && role != 0xffff &&
9185 !(le32_to_cpu(sb->feature_map) &
9186 MD_FEATURE_RESHAPE_ACTIVE)) {
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009187 rdev2->saved_raid_disk = role;
9188 ret = remove_and_add_spares(mddev, rdev2);
9189 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009190 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009191 /* wakeup mddev->thread here, so array could
9192 * perform resync with the new activated disk */
9193 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9194 md_wakeup_thread(mddev->thread);
9195
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009196 }
9197 /* device faulty
9198 * We just want to do the minimum to mark the disk
9199 * as faulty. The recovery is performed by the
9200 * one who initiated the error.
9201 */
9202 if ((role == 0xfffe) || (role == 0xfffd)) {
9203 md_error(mddev, rdev2);
9204 clear_bit(Blocked, &rdev2->flags);
9205 }
9206 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009207 }
9208
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009209 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9210 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009211
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009212 /*
9213 * Since mddev->delta_disks has already updated in update_raid_disks,
9214 * so it is time to check reshape.
9215 */
9216 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9217 (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9218 /*
9219 * reshape is happening in the remote node, we need to
9220 * update reshape_position and call start_reshape.
9221 */
Christoph Hellwiged4d0a4e2019-04-04 18:56:10 +02009222 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009223 if (mddev->pers->update_reshape_pos)
9224 mddev->pers->update_reshape_pos(mddev);
9225 if (mddev->pers->start_reshape)
9226 mddev->pers->start_reshape(mddev);
9227 } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9228 mddev->reshape_position != MaxSector &&
9229 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9230 /* reshape is just done in another node. */
9231 mddev->reshape_position = MaxSector;
9232 if (mddev->pers->update_reshape_pos)
9233 mddev->pers->update_reshape_pos(mddev);
9234 }
9235
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009236 /* Finally set the event to be up to date */
9237 mddev->events = le64_to_cpu(sb->events);
9238}
9239
9240static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9241{
9242 int err;
9243 struct page *swapout = rdev->sb_page;
9244 struct mdp_superblock_1 *sb;
9245
9246 /* Store the sb page of the rdev in the swapout temporary
9247 * variable in case we err in the future
9248 */
9249 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009250 err = alloc_disk_sb(rdev);
9251 if (err == 0) {
9252 ClearPageUptodate(rdev->sb_page);
9253 rdev->sb_loaded = 0;
9254 err = super_types[mddev->major_version].
9255 load_super(rdev, NULL, mddev->minor_version);
9256 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009257 if (err < 0) {
9258 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9259 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009260 if (rdev->sb_page)
9261 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009262 rdev->sb_page = swapout;
9263 rdev->sb_loaded = 1;
9264 return err;
9265 }
9266
9267 sb = page_address(rdev->sb_page);
9268 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9269 * is not set
9270 */
9271
9272 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9273 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9274
9275 /* The other node finished recovery, call spare_active to set
9276 * device In_sync and mddev->degraded
9277 */
9278 if (rdev->recovery_offset == MaxSector &&
9279 !test_bit(In_sync, &rdev->flags) &&
9280 mddev->pers->spare_active(mddev))
9281 sysfs_notify(&mddev->kobj, NULL, "degraded");
9282
9283 put_page(swapout);
9284 return 0;
9285}
9286
9287void md_reload_sb(struct mddev *mddev, int nr)
9288{
9289 struct md_rdev *rdev;
9290 int err;
9291
9292 /* Find the rdev */
9293 rdev_for_each_rcu(rdev, mddev) {
9294 if (rdev->desc_nr == nr)
9295 break;
9296 }
9297
9298 if (!rdev || rdev->desc_nr != nr) {
9299 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9300 return;
9301 }
9302
9303 err = read_rdev(mddev, rdev);
9304 if (err < 0)
9305 return;
9306
9307 check_sb_changes(mddev, rdev);
9308
9309 /* Read all rdev's to update recovery_offset */
Guoqing Jiang0ea99242018-04-09 17:01:21 +08009310 rdev_for_each_rcu(rdev, mddev) {
9311 if (!test_bit(Faulty, &rdev->flags))
9312 read_rdev(mddev, rdev);
9313 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009314}
9315EXPORT_SYMBOL(md_reload_sb);
9316
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317#ifndef MODULE
9318
9319/*
9320 * Searches all registered partitions for autorun RAID arrays
9321 * at boot time.
9322 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009323
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009324static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009325static LIST_HEAD(all_detected_devices);
9326struct detected_devices_node {
9327 struct list_head list;
9328 dev_t dev;
9329};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330
9331void md_autodetect_dev(dev_t dev)
9332{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009333 struct detected_devices_node *node_detected_dev;
9334
9335 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9336 if (node_detected_dev) {
9337 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009338 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009339 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009340 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009342}
9343
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344static void autostart_arrays(int part)
9345{
NeilBrown3cb03002011-10-11 16:45:26 +11009346 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009347 struct detected_devices_node *node_detected_dev;
9348 dev_t dev;
9349 int i_scanned, i_passed;
9350
9351 i_scanned = 0;
9352 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009353
NeilBrown9d487392016-11-02 14:16:49 +11009354 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009355
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009356 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009357 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9358 i_scanned++;
9359 node_detected_dev = list_entry(all_detected_devices.next,
9360 struct detected_devices_node, list);
9361 list_del(&node_detected_dev->list);
9362 dev = node_detected_dev->dev;
9363 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009364 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009365 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009366 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009367 if (IS_ERR(rdev))
9368 continue;
9369
NeilBrown403df472014-09-30 15:52:29 +10009370 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009371 continue;
NeilBrown403df472014-09-30 15:52:29 +10009372
NeilBrownd0fae182008-03-04 14:29:31 -08009373 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009374 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009375 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009376 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009377 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009378
NeilBrown9d487392016-11-02 14:16:49 +11009379 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009380
9381 autorun_devices(part);
9382}
9383
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009384#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009385
9386static __exit void md_exit(void)
9387{
NeilBrownfd01b882011-10-11 16:47:53 +11009388 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009389 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009390 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009391
NeilBrownaf5628f2014-07-31 13:54:54 +10009392 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009393 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009394
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009395 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009396 unregister_blkdev(mdp_major, "mdp");
9397 unregister_reboot_notifier(&md_notifier);
9398 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009399
9400 /* We cannot unload the modules while some process is
9401 * waiting for us in select() or poll() - wake them up
9402 */
9403 md_unloading = 1;
9404 while (waitqueue_active(&md_event_waiters)) {
9405 /* not safe to leave yet */
9406 wake_up(&md_event_waiters);
9407 msleep(delay);
9408 delay += delay;
9409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009410 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009411
NeilBrown29ac4aa2008-02-06 01:39:58 -08009412 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009413 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009414 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009415 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009416 /*
9417 * for_each_mddev() will call mddev_put() at the end of each
9418 * iteration. As the mddev is now fully clear, this will
9419 * schedule the mddev for destruction by a workqueue, and the
9420 * destroy_workqueue() below will wait for that to complete.
9421 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009422 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009423 destroy_workqueue(md_misc_wq);
9424 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009425}
9426
Dan Williams685784a2007-07-09 11:56:42 -07009427subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428module_exit(md_exit)
9429
Kees Cooke4dca7b2017-10-17 19:04:42 -07009430static int get_ro(char *buffer, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009431{
9432 return sprintf(buffer, "%d", start_readonly);
9433}
Kees Cooke4dca7b2017-10-17 19:04:42 -07009434static int set_ro(const char *val, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009435{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009436 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009437}
9438
NeilBrown80ca3a42006-07-10 04:44:18 -07009439module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9440module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009441module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009442module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009443
Linus Torvalds1da177e2005-04-16 15:20:36 -07009444MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009445MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009446MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009447MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);