blob: 05ffffb8b7694ac8583f2285d483d4997a06aaec [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
NeilBrownf72ffdd2014-09-30 14:23:59 +10003 Copyright (C) 1998, 1999, 2000 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
NeilBrown9d487392016-11-02 14:16:49 +110033
34 Errors, Warnings, etc.
35 Please use:
36 pr_crit() for error conditions that risk data loss
37 pr_err() for error conditions that are unexpected, like an IO error
38 or internal inconsistency
39 pr_warn() for error conditions that could have been predicated, like
40 adding a device to an array when it has incompatible metadata
41 pr_info() for every interesting, very rare events, like an array starting
42 or stopping, or resync starting or stopping
43 pr_debug() for everything else.
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045*/
46
Ingo Molnar3f07c012017-02-08 18:51:30 +010047#include <linux/sched/signal.h>
NeilBrowna6fb0932005-09-09 16:23:56 -070048#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110049#include <linux/blkdev.h>
Vishal Vermafc974ee2015-12-24 19:20:34 -070050#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040053#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080054#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070055#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080056#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110057#include <linux/hdreg.h>
58#include <linux/proc_fs.h>
59#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040060#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110061#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070062#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110063#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110064#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110065#include <linux/raid/md_p.h>
66#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090067#include <linux/slab.h>
NeilBrown4ad23a972017-03-15 14:05:14 +110068#include <linux/percpu-refcount.h>
69
Shaohua Li504634f2016-11-18 09:44:08 -080070#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110071#include "md.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040072#include "md-bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050073#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110076static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#endif
78
NeilBrown01f96c02011-09-21 15:30:20 +100079/* pers_list is a list of registered personalities protected
80 * by pers_lock.
81 * pers_lock does extra service to protect accesses to
82 * mddev->thread when the mutex cannot be held.
83 */
NeilBrown2604b702006-01-06 00:20:36 -080084static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static DEFINE_SPINLOCK(pers_lock);
86
Kent Overstreet28dec872018-06-07 20:52:54 -040087static struct kobj_type md_ktype;
88
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050089struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050090EXPORT_SYMBOL(md_cluster_ops);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050091struct module *md_cluster_mod;
92EXPORT_SYMBOL(md_cluster_mod);
93
Bernd Schubert90b08712008-05-23 13:04:38 -070094static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020095static struct workqueue_struct *md_wq;
96static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070097
NeilBrown746d3202013-04-24 11:42:41 +100098static int remove_and_add_spares(struct mddev *mddev,
99 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +1100100static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +1000101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/*
Robert Becker1e509152009-12-14 12:49:58 +1100103 * Default number of read corrections we'll attempt on an rdev
104 * before ejecting it from the array. We divide the read error
105 * count by 2 for every hour elapsed between read errors.
106 */
107#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
108/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
110 * is 1000 KB/sec, so the extra system load does not show up that much.
111 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -0700112 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * subsystem is idle. There is also an 'absolute maximum' reconstruction
114 * speed limit - in case reconstruction slows down your system despite
115 * idle IO detection.
116 *
117 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800118 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 */
120
121static int sysctl_speed_limit_min = 1000;
122static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100123static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800124{
125 return mddev->sync_speed_min ?
126 mddev->sync_speed_min : sysctl_speed_limit_min;
127}
128
NeilBrownfd01b882011-10-11 16:47:53 +1100129static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800130{
131 return mddev->sync_speed_max ?
132 mddev->sync_speed_max : sysctl_speed_limit_max;
133}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Xiao Ni5a409b42018-05-21 11:49:54 +0800135static void * flush_info_alloc(gfp_t gfp_flags, void *data)
136{
137 return kzalloc(sizeof(struct flush_info), gfp_flags);
138}
139static void flush_info_free(void *flush_info, void *data)
140{
141 kfree(flush_info);
142}
143
144static void * flush_bio_alloc(gfp_t gfp_flags, void *data)
145{
146 return kzalloc(sizeof(struct flush_bio), gfp_flags);
147}
148static void flush_bio_free(void *flush_bio, void *data)
149{
150 kfree(flush_bio);
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static struct ctl_table_header *raid_table_header;
154
Joe Perches82592c32013-11-14 15:16:18 +1100155static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 .procname = "speed_limit_min",
158 .data = &sysctl_speed_limit_min,
159 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700160 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800161 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 },
163 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 .procname = "speed_limit_max",
165 .data = &sysctl_speed_limit_max,
166 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700167 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800168 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800170 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
Joe Perches82592c32013-11-14 15:16:18 +1100173static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 .procname = "raid",
176 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700177 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 .child = raid_table,
179 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800180 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
Joe Perches82592c32013-11-14 15:16:18 +1100183static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 .procname = "dev",
186 .maxlen = 0,
187 .mode = 0555,
188 .child = raid_dir_table,
189 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800190 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191};
192
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700193static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
NeilBrownf91de922005-11-08 21:39:36 -0800195static int start_readonly;
196
NeilBrown78b63502017-04-12 16:26:13 +1000197/*
198 * The original mechanism for creating an md device is to create
199 * a device node in /dev and to open it. This causes races with device-close.
200 * The preferred method is to write to the "new_array" module parameter.
201 * This can avoid races.
202 * Setting create_on_open to false disables the original mechanism
203 * so all the races disappear.
204 */
205static bool create_on_open = true;
206
NeilBrowna167f662010-10-26 18:31:13 +1100207struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100208 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100209{
Kent Overstreetafeee512018-05-20 18:25:52 -0400210 if (!mddev || !bioset_initialized(&mddev->bio_set))
NeilBrowna167f662010-10-26 18:31:13 +1100211 return bio_alloc(gfp_mask, nr_iovecs);
212
Marcos Paulo de Souza62516912019-01-14 06:31:56 -0700213 return bio_alloc_bioset(gfp_mask, nr_iovecs, &mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100214}
215EXPORT_SYMBOL_GPL(bio_alloc_mddev);
216
NeilBrown5a850712017-06-21 09:12:21 +1000217static struct bio *md_bio_alloc_sync(struct mddev *mddev)
218{
Kent Overstreetafeee512018-05-20 18:25:52 -0400219 if (!mddev || !bioset_initialized(&mddev->sync_set))
NeilBrown5a850712017-06-21 09:12:21 +1000220 return bio_alloc(GFP_NOIO, 1);
221
Kent Overstreetafeee512018-05-20 18:25:52 -0400222 return bio_alloc_bioset(GFP_NOIO, 1, &mddev->sync_set);
NeilBrown5a850712017-06-21 09:12:21 +1000223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/*
NeilBrownd7603b72006-01-06 00:20:30 -0800226 * We have a system wide 'event count' that is incremented
227 * on any 'interesting' event, and readers of /proc/mdstat
228 * can use 'poll' or 'select' to find out when the event
229 * count increases.
230 *
231 * Events are:
232 * start array, stop array, error, add device, remove device,
233 * start build, activate spare
234 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800235static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800236static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100237void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800238{
239 atomic_inc(&md_event_count);
240 wake_up(&md_event_waiters);
241}
NeilBrown29269552006-03-27 01:18:10 -0800242EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800243
244/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 * Enables to iterate over all existing md arrays
246 * all_mddevs_lock protects this list.
247 */
248static LIST_HEAD(all_mddevs);
249static DEFINE_SPINLOCK(all_mddevs_lock);
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*
252 * iterates through all used mddevs in the system.
253 * We take care to grab the all_mddevs_lock whenever navigating
254 * the list, and to always hold a refcount when unlocked.
255 * Any code which breaks out of this loop while own
256 * a reference to the current mddev and must mddev_put it.
257 */
NeilBrownfd01b882011-10-11 16:47:53 +1100258#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000260 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100261 _tmp = all_mddevs.next; \
262 _mddev = NULL;}); \
263 ({ if (_tmp != &all_mddevs) \
264 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100266 if (_mddev) mddev_put(_mddev); \
267 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
268 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100270 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 )
272
NeilBrown409c57f2009-03-31 14:39:39 +1100273/* Rather than calling directly into the personality make_request function,
274 * IO requests come here first so that we can check if the device is
275 * being suspended pending a reconfiguration.
276 * We hold a refcount over the call to ->make_request. By the time that
277 * call has finished, the bio has been linked into some internal structure
278 * and so is visible to ->quiesce(), so we don't need the refcount any more.
279 */
NeilBrownb3143b92017-10-17 13:46:43 +1100280static bool is_suspended(struct mddev *mddev, struct bio *bio)
281{
282 if (mddev->suspended)
283 return true;
284 if (bio_data_dir(bio) != WRITE)
285 return false;
286 if (mddev->suspend_lo >= mddev->suspend_hi)
287 return false;
288 if (bio->bi_iter.bi_sector >= mddev->suspend_hi)
289 return false;
290 if (bio_end_sector(bio) < mddev->suspend_lo)
291 return false;
292 return true;
293}
294
Shaohua Li393debc2017-09-21 10:23:35 -0700295void md_handle_request(struct mddev *mddev, struct bio *bio)
296{
297check_suspended:
298 rcu_read_lock();
NeilBrownb3143b92017-10-17 13:46:43 +1100299 if (is_suspended(mddev, bio)) {
Shaohua Li393debc2017-09-21 10:23:35 -0700300 DEFINE_WAIT(__wait);
301 for (;;) {
302 prepare_to_wait(&mddev->sb_wait, &__wait,
303 TASK_UNINTERRUPTIBLE);
NeilBrownb3143b92017-10-17 13:46:43 +1100304 if (!is_suspended(mddev, bio))
Shaohua Li393debc2017-09-21 10:23:35 -0700305 break;
306 rcu_read_unlock();
307 schedule();
308 rcu_read_lock();
309 }
310 finish_wait(&mddev->sb_wait, &__wait);
311 }
312 atomic_inc(&mddev->active_io);
313 rcu_read_unlock();
314
315 if (!mddev->pers->make_request(mddev, bio)) {
316 atomic_dec(&mddev->active_io);
317 wake_up(&mddev->sb_wait);
318 goto check_suspended;
319 }
320
321 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
322 wake_up(&mddev->sb_wait);
323}
324EXPORT_SYMBOL(md_handle_request);
325
Jens Axboedece1632015-11-05 10:41:16 -0700326static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
NeilBrown49077322010-03-25 16:20:56 +1100328 const int rw = bio_data_dir(bio);
Michael Callahanddcf35d2018-07-18 04:47:39 -0700329 const int sgrp = op_stat_group(bio_op(bio));
NeilBrownfd01b882011-10-11 16:47:53 +1100330 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500331 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100332
NeilBrownaf67c312017-06-18 14:38:57 +1000333 blk_queue_split(q, &bio);
Kent Overstreet54efd502015-04-23 22:37:18 -0700334
NeilBrown274d8cb2016-01-04 16:16:58 +1100335 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100336 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700337 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100338 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100339 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200340 if (bio_sectors(bio) != 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200341 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200342 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700343 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100344 }
NeilBrown49077322010-03-25 16:20:56 +1100345
Chris Masone91ece52011-02-07 19:21:48 -0500346 /*
347 * save the sectors now since our bio can
348 * go away inside make_request
349 */
350 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700351 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600352 bio->bi_opf &= ~REQ_NOMERGE;
Shaohua Li393debc2017-09-21 10:23:35 -0700353
354 md_handle_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100355
Mike Snitzer112f1582018-12-06 11:41:18 -0500356 part_stat_lock();
357 part_stat_inc(&mddev->gendisk->part0, ios[sgrp]);
358 part_stat_add(&mddev->gendisk->part0, sectors[sgrp], sectors);
Gu Zheng74672d02015-04-03 08:44:47 +0800359 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100360
Jens Axboedece1632015-11-05 10:41:16 -0700361 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
NeilBrown9e35b992010-04-06 14:23:02 +1000364/* mddev_suspend makes sure no new requests are submitted
365 * to the device, and that any requests that have been submitted
366 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100367 * Once mddev_detach() is called and completes, the module will be
368 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000369 */
NeilBrownfd01b882011-10-11 16:47:53 +1100370void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100371{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200372 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
NeilBrown4d5324f2017-10-19 12:17:16 +1100373 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100374 if (mddev->suspended++)
375 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100376 synchronize_rcu();
NeilBrowncc27b0c2017-06-05 16:49:39 +1000377 wake_up(&mddev->sb_wait);
NeilBrown35bfc522017-10-17 13:46:43 +1100378 set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
379 smp_mb__after_atomic();
NeilBrown409c57f2009-03-31 14:39:39 +1100380 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
381 mddev->pers->quiesce(mddev, 1);
NeilBrown35bfc522017-10-17 13:46:43 +1100382 clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
383 wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500384
385 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100386}
NeilBrown390ee602010-06-01 19:37:27 +1000387EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100388
NeilBrownfd01b882011-10-11 16:47:53 +1100389void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100390{
NeilBrown4d5324f2017-10-19 12:17:16 +1100391 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100392 if (--mddev->suspended)
393 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100394 wake_up(&mddev->sb_wait);
395 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500396
Jonathan Brassow47525e52012-05-22 13:55:29 +1000397 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500398 md_wakeup_thread(mddev->thread);
399 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100400}
NeilBrown390ee602010-06-01 19:37:27 +1000401EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100402
NeilBrownfd01b882011-10-11 16:47:53 +1100403int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000404{
NeilBrown5c675f82014-12-15 12:56:56 +1100405 struct md_personality *pers = mddev->pers;
406 int ret = 0;
407
408 rcu_read_lock();
409 if (mddev->suspended)
410 ret = 1;
411 else if (pers && pers->congested)
412 ret = pers->congested(mddev, bits);
413 rcu_read_unlock();
414 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000415}
NeilBrown5c675f82014-12-15 12:56:56 +1100416EXPORT_SYMBOL_GPL(mddev_congested);
417static int md_congested(void *data, int bits)
418{
419 struct mddev *mddev = data;
420 return mddev_congested(mddev, bits);
421}
NeilBrown3fa841d2009-09-23 18:10:29 +1000422
NeilBrowna2826aa2009-12-14 12:49:49 +1100423/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200424 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100425 */
Xiao Ni5a409b42018-05-21 11:49:54 +0800426static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100427{
Xiao Ni5a409b42018-05-21 11:49:54 +0800428 struct flush_info *fi = container_of(ws, struct flush_info, flush_work);
429 struct mddev *mddev = fi->mddev;
430 struct bio *bio = fi->bio;
431
432 bio->bi_opf &= ~REQ_PREFLUSH;
433 md_handle_request(mddev, bio);
434
435 mempool_free(fi, mddev->flush_pool);
436}
437
438static void md_end_flush(struct bio *fbio)
439{
440 struct flush_bio *fb = fbio->bi_private;
441 struct md_rdev *rdev = fb->rdev;
442 struct flush_info *fi = fb->fi;
443 struct bio *bio = fi->bio;
444 struct mddev *mddev = fi->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100445
446 rdev_dec_pending(rdev, mddev);
447
Xiao Ni5a409b42018-05-21 11:49:54 +0800448 if (atomic_dec_and_test(&fi->flush_pending)) {
Xiao Niaf9b9262018-10-20 08:09:25 +0800449 if (bio->bi_iter.bi_size == 0) {
Xiao Ni5a409b42018-05-21 11:49:54 +0800450 /* an empty barrier - all done */
451 bio_endio(bio);
Xiao Niaf9b9262018-10-20 08:09:25 +0800452 mempool_free(fi, mddev->flush_pool);
453 } else {
Xiao Ni5a409b42018-05-21 11:49:54 +0800454 INIT_WORK(&fi->flush_work, submit_flushes);
455 queue_work(md_wq, &fi->flush_work);
456 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100457 }
Xiao Ni5a409b42018-05-21 11:49:54 +0800458
459 mempool_free(fb, mddev->flush_bio_pool);
460 bio_put(fbio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100461}
462
Xiao Ni5a409b42018-05-21 11:49:54 +0800463void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100464{
NeilBrown3cb03002011-10-11 16:45:26 +1100465 struct md_rdev *rdev;
Xiao Ni5a409b42018-05-21 11:49:54 +0800466 struct flush_info *fi;
NeilBrowna2826aa2009-12-14 12:49:49 +1100467
Xiao Ni5a409b42018-05-21 11:49:54 +0800468 fi = mempool_alloc(mddev->flush_pool, GFP_NOIO);
469
470 fi->bio = bio;
471 fi->mddev = mddev;
472 atomic_set(&fi->flush_pending, 1);
473
NeilBrowna2826aa2009-12-14 12:49:49 +1100474 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100475 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100476 if (rdev->raid_disk >= 0 &&
477 !test_bit(Faulty, &rdev->flags)) {
478 /* Take two references, one is dropped
479 * when request finishes, one after
480 * we reclaim rcu_read_lock
481 */
482 struct bio *bi;
Xiao Ni5a409b42018-05-21 11:49:54 +0800483 struct flush_bio *fb;
NeilBrowna2826aa2009-12-14 12:49:49 +1100484 atomic_inc(&rdev->nr_pending);
485 atomic_inc(&rdev->nr_pending);
486 rcu_read_unlock();
Xiao Ni5a409b42018-05-21 11:49:54 +0800487
488 fb = mempool_alloc(mddev->flush_bio_pool, GFP_NOIO);
489 fb->fi = fi;
490 fb->rdev = rdev;
491
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000492 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Christoph Hellwig74d46992017-08-23 19:10:32 +0200493 bio_set_dev(bi, rdev->bdev);
Xiao Ni5a409b42018-05-21 11:49:54 +0800494 bi->bi_end_io = md_end_flush;
495 bi->bi_private = fb;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600496 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
Xiao Ni5a409b42018-05-21 11:49:54 +0800497
498 atomic_inc(&fi->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500499 submit_bio(bi);
Xiao Ni5a409b42018-05-21 11:49:54 +0800500
NeilBrowna2826aa2009-12-14 12:49:49 +1100501 rcu_read_lock();
502 rdev_dec_pending(rdev, mddev);
503 }
504 rcu_read_unlock();
NeilBrowna2826aa2009-12-14 12:49:49 +1100505
Xiao Ni5a409b42018-05-21 11:49:54 +0800506 if (atomic_dec_and_test(&fi->flush_pending)) {
Xiao Niaf9b9262018-10-20 08:09:25 +0800507 if (bio->bi_iter.bi_size == 0) {
Xiao Ni5a409b42018-05-21 11:49:54 +0800508 /* an empty barrier - all done */
509 bio_endio(bio);
Xiao Niaf9b9262018-10-20 08:09:25 +0800510 mempool_free(fi, mddev->flush_pool);
511 } else {
Xiao Ni5a409b42018-05-21 11:49:54 +0800512 INIT_WORK(&fi->flush_work, submit_flushes);
513 queue_work(md_wq, &fi->flush_work);
514 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100515 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100516}
Tejun Heoe9c74692010-09-03 11:56:18 +0200517EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100518
NeilBrownfd01b882011-10-11 16:47:53 +1100519static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 atomic_inc(&mddev->active);
522 return mddev;
523}
524
Dan Williams5fd3a172009-03-04 00:57:25 -0700525static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100526
NeilBrownfd01b882011-10-11 16:47:53 +1100527static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
530 return;
NeilBrownd3374822009-01-09 08:31:10 +1100531 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100532 mddev->ctime == 0 && !mddev->hold_active) {
533 /* Array is not configured at all, and not held active,
534 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100535 list_del_init(&mddev->all_mddevs);
Kent Overstreet28dec872018-06-07 20:52:54 -0400536
537 /*
538 * Call queue_work inside the spinlock so that
539 * flush_workqueue() after mddev_find will succeed in waiting
540 * for the work to be done.
541 */
542 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
543 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100544 }
545 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Kees Cook8376d3c2017-10-16 17:01:48 -0700548static void md_safemode_timeout(struct timer_list *t);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400549
NeilBrownfd01b882011-10-11 16:47:53 +1100550void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100551{
Kent Overstreet28dec872018-06-07 20:52:54 -0400552 kobject_init(&mddev->kobj, &md_ktype);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100553 mutex_init(&mddev->open_mutex);
554 mutex_init(&mddev->reconfig_mutex);
555 mutex_init(&mddev->bitmap_info.mutex);
556 INIT_LIST_HEAD(&mddev->disks);
557 INIT_LIST_HEAD(&mddev->all_mddevs);
Kees Cook8376d3c2017-10-16 17:01:48 -0700558 timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100559 atomic_set(&mddev->active, 1);
560 atomic_set(&mddev->openers, 0);
561 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100562 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100563 init_waitqueue_head(&mddev->sb_wait);
564 init_waitqueue_head(&mddev->recovery_wait);
565 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000566 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500567 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100568 mddev->resync_min = 0;
569 mddev->resync_max = MaxSector;
570 mddev->level = LEVEL_NONE;
571}
NeilBrown390ee602010-06-01 19:37:27 +1000572EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100573
NeilBrownf72ffdd2014-09-30 14:23:59 +1000574static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
NeilBrownfd01b882011-10-11 16:47:53 +1100576 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
NeilBrown8f5f02c2011-02-16 13:58:51 +1100578 if (unit && MAJOR(unit) != MD_MAJOR)
579 unit &= ~((1<<MdpMinorShift)-1);
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 retry:
582 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
NeilBrownefeb53c2009-01-09 08:31:10 +1100584 if (unit) {
585 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
586 if (mddev->unit == unit) {
587 mddev_get(mddev);
588 spin_unlock(&all_mddevs_lock);
589 kfree(new);
590 return mddev;
591 }
592
593 if (new) {
594 list_add(&new->all_mddevs, &all_mddevs);
595 spin_unlock(&all_mddevs_lock);
596 new->hold_active = UNTIL_IOCTL;
597 return new;
598 }
599 } else if (new) {
600 /* find an unused unit number */
601 static int next_minor = 512;
602 int start = next_minor;
603 int is_free = 0;
604 int dev = 0;
605 while (!is_free) {
606 dev = MKDEV(MD_MAJOR, next_minor);
607 next_minor++;
608 if (next_minor > MINORMASK)
609 next_minor = 0;
610 if (next_minor == start) {
611 /* Oh dear, all in use. */
612 spin_unlock(&all_mddevs_lock);
613 kfree(new);
614 return NULL;
615 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000616
NeilBrownefeb53c2009-01-09 08:31:10 +1100617 is_free = 1;
618 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
619 if (mddev->unit == dev) {
620 is_free = 0;
621 break;
622 }
623 }
624 new->unit = dev;
625 new->md_minor = MINOR(dev);
626 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 list_add(&new->all_mddevs, &all_mddevs);
628 spin_unlock(&all_mddevs_lock);
629 return new;
630 }
631 spin_unlock(&all_mddevs_lock);
632
NeilBrown9ffae0c2006-01-06 00:20:32 -0800633 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (!new)
635 return NULL;
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 new->unit = unit;
638 if (MAJOR(unit) == MD_MAJOR)
639 new->md_minor = MINOR(unit);
640 else
641 new->md_minor = MINOR(unit) >> MdpMinorShift;
642
NeilBrownfafd7fb2010-04-01 15:55:30 +1100643 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 goto retry;
646}
647
NeilBrownb6eb1272010-04-15 10:13:47 +1000648static struct attribute_group md_redundancy_group;
649
NeilBrown5c47daf2014-12-15 12:57:01 +1100650void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
NeilBrowna64c8762010-04-14 17:15:37 +1000652 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000653 /* These cannot be removed under reconfig_mutex as
654 * an access to the files will try to take reconfig_mutex
655 * while holding the file unremovable, which leads to
656 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000657 * So hold set sysfs_active while the remove in happeing,
658 * and anything else which might set ->to_remove or my
659 * otherwise change the sysfs namespace will fail with
660 * -EBUSY if sysfs_active is still set.
661 * We set sysfs_active under reconfig_mutex and elsewhere
662 * test it under the same mutex to ensure its correct value
663 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000664 */
NeilBrowna64c8762010-04-14 17:15:37 +1000665 struct attribute_group *to_remove = mddev->to_remove;
666 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000667 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000668 mutex_unlock(&mddev->reconfig_mutex);
669
NeilBrown00bcb4a2010-06-01 19:37:23 +1000670 if (mddev->kobj.sd) {
671 if (to_remove != &md_redundancy_group)
672 sysfs_remove_group(&mddev->kobj, to_remove);
673 if (mddev->pers == NULL ||
674 mddev->pers->sync_request == NULL) {
675 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
676 if (mddev->sysfs_action)
677 sysfs_put(mddev->sysfs_action);
678 mddev->sysfs_action = NULL;
679 }
NeilBrowna64c8762010-04-14 17:15:37 +1000680 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000681 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000682 } else
683 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Chris Dunlop751e67c2011-10-19 16:48:26 +1100685 /* As we've dropped the mutex we need a spinlock to
686 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000687 */
688 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700689 md_wakeup_thread(mddev->thread);
NeilBrown4d5324f2017-10-19 12:17:16 +1100690 wake_up(&mddev->sb_wait);
NeilBrown01f96c02011-09-21 15:30:20 +1000691 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
NeilBrown5c47daf2014-12-15 12:57:01 +1100693EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500695struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100696{
697 struct md_rdev *rdev;
698
699 rdev_for_each_rcu(rdev, mddev)
700 if (rdev->desc_nr == nr)
701 return rdev;
702
703 return NULL;
704}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500705EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100706
707static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
NeilBrown3cb03002011-10-11 16:45:26 +1100709 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
NeilBrowndafb20f2012-03-19 12:46:39 +1100711 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (rdev->bdev->bd_dev == dev)
713 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return NULL;
716}
717
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100718struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
NeilBrown1ca69c42012-10-11 13:37:33 +1100719{
720 struct md_rdev *rdev;
721
722 rdev_for_each_rcu(rdev, mddev)
723 if (rdev->bdev->bd_dev == dev)
724 return rdev;
725
726 return NULL;
727}
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100728EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100729
NeilBrown84fc4b52011-10-11 16:49:58 +1100730static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800731{
NeilBrown84fc4b52011-10-11 16:49:58 +1100732 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800733 list_for_each_entry(pers, &pers_list, list) {
734 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800735 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800736 if (strcmp(pers->name, clevel)==0)
737 return pers;
738 }
NeilBrown2604b702006-01-06 00:20:36 -0800739 return NULL;
740}
741
Andre Nollb73df2d2008-07-11 22:02:23 +1000742/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100743static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100745 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000746 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
NeilBrownf72ffdd2014-09-30 14:23:59 +1000749static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100752 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000753 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return 0;
755}
756
NeilBrown545c8792012-05-22 13:54:30 +1000757void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800760 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 rdev->sb_loaded = 0;
762 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000763 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100764 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
NeilBrown2699b672011-07-28 11:31:47 +1000766 if (rdev->bb_page) {
767 put_page(rdev->bb_page);
768 rdev->bb_page = NULL;
769 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800770 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
NeilBrown545c8792012-05-22 13:54:30 +1000772EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200774static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700775{
NeilBrown3cb03002011-10-11 16:45:26 +1100776 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100777 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700778
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200779 if (bio->bi_status) {
780 pr_err("md: super_written gets error=%d\n", bio->bi_status);
NeilBrowna9701a32005-11-08 21:39:34 -0800781 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100782 if (!test_bit(Faulty, &rdev->flags)
783 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800784 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100785 set_bit(LastDev, &rdev->flags);
786 }
787 } else
788 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700789
NeilBrowna9701a32005-11-08 21:39:34 -0800790 if (atomic_dec_and_test(&mddev->pending_writes))
791 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700792 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700793 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700794}
795
NeilBrownfd01b882011-10-11 16:47:53 +1100796void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700797 sector_t sector, int size, struct page *page)
798{
799 /* write first size bytes of page to sector of rdev
800 * Increment mddev->pending_writes before returning
801 * and decrement it on completion, waking up sb_wait
802 * if zero is reached.
803 * If an error occurred, call md_error
804 */
NeilBrown46533ff2016-11-18 16:16:11 +1100805 struct bio *bio;
806 int ff = 0;
807
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +0100808 if (!page)
809 return;
810
NeilBrown46533ff2016-11-18 16:16:11 +1100811 if (test_bit(Faulty, &rdev->flags))
812 return;
813
NeilBrown5a850712017-06-21 09:12:21 +1000814 bio = md_bio_alloc_sync(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700815
Shaohua Lied3b98c2016-03-29 14:00:19 -0700816 atomic_inc(&rdev->nr_pending);
817
Christoph Hellwig74d46992017-08-23 19:10:32 +0200818 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700819 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700820 bio_add_page(bio, page, size, 0);
821 bio->bi_private = rdev;
822 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100823
824 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
825 test_bit(FailFast, &rdev->flags) &&
826 !test_bit(LastDev, &rdev->flags))
827 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +0200828 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -0800829
NeilBrown7bfa19f2005-06-21 17:17:28 -0700830 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500831 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800832}
833
NeilBrown46533ff2016-11-18 16:16:11 +1100834int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800835{
Tejun Heoe9c74692010-09-03 11:56:18 +0200836 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000837 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -0800838 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +1100839 return -EAGAIN;
840 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700841}
842
NeilBrown3cb03002011-10-11 16:45:26 +1100843int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -0500844 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
NeilBrown5a850712017-06-21 09:12:21 +1000846 struct bio *bio = md_bio_alloc_sync(rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 int ret;
848
Christoph Hellwig74d46992017-08-23 19:10:32 +0200849 if (metadata_op && rdev->meta_bdev)
850 bio_set_dev(bio, rdev->meta_bdev);
851 else
852 bio_set_dev(bio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -0500853 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100854 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700855 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000856 else if (rdev->mddev->reshape_position != MaxSector &&
857 (rdev->mddev->reshape_backwards ==
858 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700859 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100860 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700861 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500863
864 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200866 ret = !bio->bi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 bio_put(bio);
868 return ret;
869}
NeilBrowna8745db2006-01-06 00:20:34 -0800870EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
NeilBrownf72ffdd2014-09-30 14:23:59 +1000872static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (rdev->sb_loaded)
877 return 0;
878
Mike Christie796a5cf2016-06-05 14:32:07 -0500879 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 goto fail;
881 rdev->sb_loaded = 1;
882 return 0;
883
884fail:
NeilBrown9d487392016-11-02 14:16:49 +1100885 pr_err("md: disabled device %s, could not read superblock.\n",
886 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return -EINVAL;
888}
889
Amir Goldsteine6fd2092017-05-04 16:26:20 +0300890static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000892 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000893 sb1->set_uuid1 == sb2->set_uuid1 &&
894 sb1->set_uuid2 == sb2->set_uuid2 &&
895 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
Amir Goldsteine6fd2092017-05-04 16:26:20 +0300898static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
900 int ret;
901 mdp_super_t *tmp1, *tmp2;
902
903 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
904 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
905
906 if (!tmp1 || !tmp2) {
907 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 goto abort;
909 }
910
911 *tmp1 = *sb1;
912 *tmp2 = *sb2;
913
914 /*
915 * nr_disks is not constant
916 */
917 tmp1->nr_disks = 0;
918 tmp2->nr_disks = 0;
919
Andre Nollce0c8e02008-07-11 22:02:20 +1000920 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700922 kfree(tmp1);
923 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return ret;
925}
926
NeilBrown4d167f02007-05-09 02:35:37 -0700927static u32 md_csum_fold(u32 csum)
928{
929 csum = (csum & 0xffff) + (csum >> 16);
930 return (csum & 0xffff) + (csum >> 16);
931}
932
NeilBrownf72ffdd2014-09-30 14:23:59 +1000933static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
NeilBrown4d167f02007-05-09 02:35:37 -0700935 u64 newcsum = 0;
936 u32 *sb32 = (u32*)sb;
937 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 unsigned int disk_csum, csum;
939
940 disk_csum = sb->sb_csum;
941 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700942
943 for (i = 0; i < MD_SB_BYTES/4 ; i++)
944 newcsum += sb32[i];
945 csum = (newcsum & 0xffffffff) + (newcsum>>32);
946
NeilBrown4d167f02007-05-09 02:35:37 -0700947#ifdef CONFIG_ALPHA
948 /* This used to use csum_partial, which was wrong for several
949 * reasons including that different results are returned on
950 * different architectures. It isn't critical that we get exactly
951 * the same return value as before (we always csum_fold before
952 * testing, and that removes any differences). However as we
953 * know that csum_partial always returned a 16bit value on
954 * alphas, do a fold to maximise conformity to previous behaviour.
955 */
956 sb->sb_csum = md_csum_fold(disk_csum);
957#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700959#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return csum;
961}
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963/*
964 * Handle superblock details.
965 * We want to be able to handle multiple superblock formats
966 * so we have a common interface to them all, and an array of
967 * different handlers.
968 * We rely on user-space to write the initial superblock, and support
969 * reading and updating of superblocks.
970 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100971 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 * loads and validates a superblock on dev.
973 * if refdev != NULL, compare superblocks on both devices
974 * Return:
975 * 0 - dev has a superblock that is compatible with refdev
976 * 1 - dev has a superblock that is compatible and newer than refdev
977 * so dev should be used as the refdev in future
978 * -EINVAL superblock incompatible or invalid
979 * -othererror e.g. -EIO
980 *
NeilBrownfd01b882011-10-11 16:47:53 +1100981 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * Verify that dev is acceptable into mddev.
983 * The first time, mddev->raid_disks will be 0, and data from
984 * dev should be merged in. Subsequent calls check that dev
985 * is new enough. Return 0 or -EINVAL
986 *
NeilBrownfd01b882011-10-11 16:47:53 +1100987 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 * Update the superblock for rdev with data in mddev
989 * This does not write to disc.
990 *
991 */
992
993struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000994 char *name;
995 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000996 int (*load_super)(struct md_rdev *rdev,
997 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000998 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000999 int (*validate_super)(struct mddev *mddev,
1000 struct md_rdev *rdev);
1001 void (*sync_super)(struct mddev *mddev,
1002 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001003 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001004 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +10001005 int (*allow_new_offset)(struct md_rdev *rdev,
1006 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007};
1008
1009/*
Andre Noll0894cc32009-06-18 08:49:23 +10001010 * Check that the given mddev has no bitmap.
1011 *
1012 * This function is called from the run method of all personalities that do not
1013 * support bitmaps. It prints an error message and returns non-zero if mddev
1014 * has a bitmap. Otherwise, it returns 0.
1015 *
1016 */
NeilBrownfd01b882011-10-11 16:47:53 +11001017int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001018{
NeilBrownc3d97142009-12-14 12:49:52 +11001019 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001020 return 0;
NeilBrown9d487392016-11-02 14:16:49 +11001021 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +10001022 mdname(mddev), mddev->pers->name);
1023 return 1;
1024}
1025EXPORT_SYMBOL(md_check_no_bitmap);
1026
1027/*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001028 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 */
NeilBrown3cb03002011-10-11 16:45:26 +11001030static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1033 mdp_super_t *sb;
1034 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /*
Andre Noll0f420352008-07-11 22:02:23 +10001037 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 * it's at the end of the disk.
1039 *
1040 * It also happens to be a multiple of 4Kb.
1041 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001042 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
NeilBrown0002b272005-09-09 16:23:53 -07001044 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +11001045 if (ret)
1046 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 ret = -EINVAL;
1049
1050 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001051 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +11001054 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 goto abort;
1056 }
1057
1058 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001059 sb->minor_version < 90 ||
1060 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +11001061 pr_warn("Bad version number %d.%d on %s\n",
1062 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 goto abort;
1064 }
1065
1066 if (sb->raid_disks <= 0)
1067 goto abort;
1068
NeilBrown4d167f02007-05-09 02:35:37 -07001069 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001070 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 goto abort;
1072 }
1073
1074 rdev->preferred_minor = sb->md_minor;
1075 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001076 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001077 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001078 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 if (sb->level == LEVEL_MULTIPATH)
1081 rdev->desc_nr = -1;
1082 else
1083 rdev->desc_nr = sb->this_disk.number;
1084
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001085 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001087 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001089 mdp_super_t *refsb = page_address(refdev->sb_page);
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001090 if (!md_uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001091 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 b, bdevname(refdev->bdev,b2));
1093 goto abort;
1094 }
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001095 if (!md_sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001096 pr_warn("md: %s has same UUID but different superblock to %s\n",
1097 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 goto abort;
1099 }
1100 ev1 = md_event(sb);
1101 ev2 = md_event(refsb);
1102 if (ev1 > ev2)
1103 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001104 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 ret = 0;
1106 }
NeilBrown8190e752009-06-18 08:48:58 +10001107 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001108 /* Limit to 4TB as metadata cannot record more than that.
1109 * (not needed for Linear and RAID0 as metadata doesn't
1110 * record this size)
1111 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001112 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1113 sb->level >= 1)
1114 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
NeilBrown27a7b262011-09-10 17:21:28 +10001116 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001117 /* "this cannot possibly happen" ... */
1118 ret = -EINVAL;
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 abort:
1121 return ret;
1122}
1123
1124/*
1125 * validate_super for 0.90.0
1126 */
NeilBrownfd01b882011-10-11 16:47:53 +11001127static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001130 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001131 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
NeilBrown41158c72005-06-21 17:17:25 -07001133 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001134 clear_bit(Faulty, &rdev->flags);
1135 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001136 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001137 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (mddev->raid_disks == 0) {
1140 mddev->major_version = 0;
1141 mddev->minor_version = sb->minor_version;
1142 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001143 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001144 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 mddev->ctime = sb->ctime;
1146 mddev->utime = sb->utime;
1147 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001148 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 mddev->layout = sb->layout;
1150 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001151 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001152 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001153 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001154 mddev->bitmap_info.space = 0;
1155 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001156 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001157 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001158 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
NeilBrownf6705572006-03-27 01:18:11 -08001160 if (mddev->minor_version >= 91) {
1161 mddev->reshape_position = sb->reshape_position;
1162 mddev->delta_disks = sb->delta_disks;
1163 mddev->new_level = sb->new_level;
1164 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001165 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001166 if (mddev->delta_disks < 0)
1167 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001168 } else {
1169 mddev->reshape_position = MaxSector;
1170 mddev->delta_disks = 0;
1171 mddev->new_level = mddev->level;
1172 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001173 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001174 }
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (sb->state & (1<<MD_SB_CLEAN))
1177 mddev->recovery_cp = MaxSector;
1178 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001179 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 sb->events_lo == sb->cp_events_lo) {
1181 mddev->recovery_cp = sb->recovery_cp;
1182 } else
1183 mddev->recovery_cp = 0;
1184 }
1185
1186 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1187 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1188 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1189 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1190
1191 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001192
1193 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001194 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001195 mddev->bitmap_info.offset =
1196 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001197 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001198 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001199 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001200
NeilBrown41158c72005-06-21 17:17:25 -07001201 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001202 /* Insist on good event counter while assembling, except
1203 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001205 if (sb->disks[rdev->desc_nr].state & (
1206 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001207 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001208 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001209 } else if (mddev->bitmap) {
1210 /* if adding to array with a bitmap, then we can accept an
1211 * older device ... but not too old.
1212 */
NeilBrown41158c72005-06-21 17:17:25 -07001213 if (ev1 < mddev->bitmap->events_cleared)
1214 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001215 if (ev1 < mddev->events)
1216 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001217 } else {
1218 if (ev1 < mddev->events)
1219 /* just a hot-add of a new device, leave raid_disk at -1 */
1220 return 0;
1221 }
NeilBrown41158c72005-06-21 17:17:25 -07001222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 desc = sb->disks + rdev->desc_nr;
1225
1226 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001227 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001228 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1229 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001230 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001232 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001233 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1234 /* active but not in sync implies recovery up to
1235 * reshape position. We don't know exactly where
1236 * that is, so set to zero for now */
1237 if (mddev->minor_version >= 91) {
1238 rdev->recovery_offset = 0;
1239 rdev->raid_disk = desc->raid_disk;
1240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001242 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1243 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001244 if (desc->state & (1<<MD_DISK_FAILFAST))
1245 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001246 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001247 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return 0;
1249}
1250
1251/*
1252 * sync_super for 0.90.0
1253 */
NeilBrownfd01b882011-10-11 16:47:53 +11001254static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001257 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 /* make rdev->sb match mddev data..
1261 *
1262 * 1/ zero out disks
1263 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1264 * 3/ any empty disks < next_spare become removed
1265 *
1266 * disks[0] gets initialised to REMOVED because
1267 * we cannot be sure from other fields if it has
1268 * been initialised or not.
1269 */
1270 int i;
1271 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1272
NeilBrown61181562005-09-09 16:24:02 -07001273 rdev->sb_size = MD_SB_BYTES;
1274
Namhyung Kim65a06f062011-07-27 11:00:36 +10001275 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 memset(sb, 0, sizeof(*sb));
1278
1279 sb->md_magic = MD_SB_MAGIC;
1280 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 sb->patch_version = mddev->patch_version;
1282 sb->gvalid_words = 0; /* ignored */
1283 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1284 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1285 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1286 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1287
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001288 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001290 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 sb->raid_disks = mddev->raid_disks;
1292 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001293 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001294 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 sb->state = 0;
1296 sb->events_hi = (mddev->events>>32);
1297 sb->events_lo = (u32)mddev->events;
1298
NeilBrownf6705572006-03-27 01:18:11 -08001299 if (mddev->reshape_position == MaxSector)
1300 sb->minor_version = 90;
1301 else {
1302 sb->minor_version = 91;
1303 sb->reshape_position = mddev->reshape_position;
1304 sb->new_level = mddev->new_level;
1305 sb->delta_disks = mddev->delta_disks;
1306 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001307 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001308 }
1309 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (mddev->in_sync)
1311 {
1312 sb->recovery_cp = mddev->recovery_cp;
1313 sb->cp_events_hi = (mddev->events>>32);
1314 sb->cp_events_lo = (u32)mddev->events;
1315 if (mddev->recovery_cp == MaxSector)
1316 sb->state = (1<< MD_SB_CLEAN);
1317 } else
1318 sb->recovery_cp = 0;
1319
1320 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001321 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
NeilBrownc3d97142009-12-14 12:49:52 +11001323 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001324 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001327 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001329 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001330 int is_active = test_bit(In_sync, &rdev2->flags);
1331
1332 if (rdev2->raid_disk >= 0 &&
1333 sb->minor_version >= 91)
1334 /* we have nowhere to store the recovery_offset,
1335 * but if it is not below the reshape_position,
1336 * we can piggy-back on that.
1337 */
1338 is_active = 1;
1339 if (rdev2->raid_disk < 0 ||
1340 test_bit(Faulty, &rdev2->flags))
1341 is_active = 0;
1342 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001343 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001345 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001346 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 d = &sb->disks[rdev2->desc_nr];
1348 nr_disks++;
1349 d->number = rdev2->desc_nr;
1350 d->major = MAJOR(rdev2->bdev->bd_dev);
1351 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001352 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 d->raid_disk = rdev2->raid_disk;
1354 else
1355 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001356 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001358 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001360 if (test_bit(In_sync, &rdev2->flags))
1361 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 active++;
1363 working++;
1364 } else {
1365 d->state = 0;
1366 spare++;
1367 working++;
1368 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001369 if (test_bit(WriteMostly, &rdev2->flags))
1370 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001371 if (test_bit(FailFast, &rdev2->flags))
1372 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /* now set the "removed" and "faulty" bits on any missing devices */
1375 for (i=0 ; i < mddev->raid_disks ; i++) {
1376 mdp_disk_t *d = &sb->disks[i];
1377 if (d->state == 0 && d->number == 0) {
1378 d->number = i;
1379 d->raid_disk = i;
1380 d->state = (1<<MD_DISK_REMOVED);
1381 d->state |= (1<<MD_DISK_FAULTY);
1382 failed++;
1383 }
1384 }
1385 sb->nr_disks = nr_disks;
1386 sb->active_disks = active;
1387 sb->working_disks = working;
1388 sb->failed_disks = failed;
1389 sb->spare_disks = spare;
1390
1391 sb->this_disk = sb->disks[rdev->desc_nr];
1392 sb->sb_csum = calc_sb_csum(sb);
1393}
1394
1395/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001396 * rdev_size_change for 0.90.0
1397 */
1398static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001399super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001400{
Andre Noll58c0fed2009-03-31 14:33:13 +11001401 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001402 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001403 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001404 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001405 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001406 if (!num_sectors || num_sectors > rdev->sb_start)
1407 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001408 /* Limit to 4TB as metadata cannot record more than that.
1409 * 4TB == 2^32 KB, or 2*2^32 sectors.
1410 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001411 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1412 rdev->mddev->level >= 1)
1413 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001414 do {
1415 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001416 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001417 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001418 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001419}
1420
NeilBrownc6563a82012-05-21 09:27:00 +10001421static int
1422super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1423{
1424 /* non-zero offset changes not possible with v0.90 */
1425 return new_offset == 0;
1426}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001427
1428/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 * version 1 superblock
1430 */
1431
NeilBrownf72ffdd2014-09-30 14:23:59 +10001432static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001434 __le32 disk_csum;
1435 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 unsigned long long newcsum;
1437 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001438 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 disk_csum = sb->sb_csum;
1441 sb->sb_csum = 0;
1442 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001443 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 newcsum += le32_to_cpu(*isuper++);
1445
1446 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001447 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1450 sb->sb_csum = disk_csum;
1451 return cpu_to_le32(csum);
1452}
1453
NeilBrown3cb03002011-10-11 16:45:26 +11001454static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
1456 struct mdp_superblock_1 *sb;
1457 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001458 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001459 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001461 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 /*
Andre Noll0f420352008-07-11 22:02:23 +10001464 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 * It is always aligned to a 4K boundary and
1466 * depeding on minor_version, it can be:
1467 * 0: At least 8K, but less than 12K, from end of device
1468 * 1: At start of device
1469 * 2: 4K from start of device.
1470 */
1471 switch(minor_version) {
1472 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001473 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001474 sb_start -= 8*2;
1475 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 break;
1477 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001478 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 break;
1480 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001481 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 break;
1483 default:
1484 return -EINVAL;
1485 }
Andre Noll0f420352008-07-11 22:02:23 +10001486 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
NeilBrown0002b272005-09-09 16:23:53 -07001488 /* superblock is rarely larger than 1K, but it can be larger,
1489 * and it is safe to read 4k, so we do that
1490 */
1491 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (ret) return ret;
1493
Namhyung Kim65a06f062011-07-27 11:00:36 +10001494 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1497 sb->major_version != cpu_to_le32(1) ||
1498 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001499 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001500 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return -EINVAL;
1502
1503 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001504 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 bdevname(rdev->bdev,b));
1506 return -EINVAL;
1507 }
1508 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001509 pr_warn("md: data_size too small on %s\n",
1510 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return -EINVAL;
1512 }
NeilBrownc6563a82012-05-21 09:27:00 +10001513 if (sb->pad0 ||
1514 sb->pad3[0] ||
1515 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1516 /* Some padding is non-zero, might be a new feature */
1517 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 rdev->preferred_minor = 0xffff;
1520 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001521 rdev->new_data_offset = rdev->data_offset;
1522 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1523 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1524 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001525 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
NeilBrown0002b272005-09-09 16:23:53 -07001527 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001528 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001529 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001530 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1531
1532 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001533 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001534 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001535 if (minor_version
1536 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1537 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001538
NeilBrown31b65a02006-07-10 04:44:14 -07001539 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1540 rdev->desc_nr = -1;
1541 else
1542 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1543
NeilBrown2699b672011-07-28 11:31:47 +10001544 if (!rdev->bb_page) {
1545 rdev->bb_page = alloc_page(GFP_KERNEL);
1546 if (!rdev->bb_page)
1547 return -ENOMEM;
1548 }
1549 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1550 rdev->badblocks.count == 0) {
1551 /* need to load the bad block list.
1552 * Currently we limit it to one page.
1553 */
1554 s32 offset;
1555 sector_t bb_sector;
1556 u64 *bbp;
1557 int i;
1558 int sectors = le16_to_cpu(sb->bblog_size);
1559 if (sectors > (PAGE_SIZE / 512))
1560 return -EINVAL;
1561 offset = le32_to_cpu(sb->bblog_offset);
1562 if (offset == 0)
1563 return -EINVAL;
1564 bb_sector = (long long)offset;
1565 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001566 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001567 return -EIO;
1568 bbp = (u64 *)page_address(rdev->bb_page);
1569 rdev->badblocks.shift = sb->bblog_shift;
1570 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1571 u64 bb = le64_to_cpu(*bbp);
1572 int count = bb & (0x3ff);
1573 u64 sector = bb >> 10;
1574 sector <<= sb->bblog_shift;
1575 count <<= sb->bblog_shift;
1576 if (bb + 1 == 0)
1577 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001578 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001579 return -EINVAL;
1580 }
NeilBrown486adf72013-04-24 11:42:44 +10001581 } else if (sb->bblog_offset != 0)
1582 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001583
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001584 if ((le32_to_cpu(sb->feature_map) &
1585 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001586 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1587 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1588 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1589 }
1590
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001591 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001592 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001593 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001595 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1598 sb->level != refsb->level ||
1599 sb->layout != refsb->layout ||
1600 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001601 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 bdevname(rdev->bdev,b),
1603 bdevname(refdev->bdev,b2));
1604 return -EINVAL;
1605 }
1606 ev1 = le64_to_cpu(sb->events);
1607 ev2 = le64_to_cpu(refsb->events);
1608
1609 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001610 ret = 1;
1611 else
1612 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
NeilBrownc6563a82012-05-21 09:27:00 +10001614 if (minor_version) {
1615 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1616 sectors -= rdev->data_offset;
1617 } else
1618 sectors = rdev->sb_start;
1619 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001621 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001622 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
NeilBrownfd01b882011-10-11 16:47:53 +11001625static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001627 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001628 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
NeilBrown41158c72005-06-21 17:17:25 -07001630 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001631 clear_bit(Faulty, &rdev->flags);
1632 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001633 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001634 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (mddev->raid_disks == 0) {
1637 mddev->major_version = 1;
1638 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001639 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001640 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001641 mddev->ctime = le64_to_cpu(sb->ctime);
1642 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001644 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 mddev->layout = le32_to_cpu(sb->layout);
1646 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001647 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001648 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001649 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001650 mddev->bitmap_info.space = 0;
1651 /* Default location for bitmap is 1K after superblock
1652 * using 3K - total of 4K
1653 */
NeilBrownc3d97142009-12-14 12:49:52 +11001654 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001655 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001656 mddev->reshape_backwards = 0;
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1659 memcpy(mddev->uuid, sb->set_uuid, 16);
1660
1661 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001662
NeilBrown71c08052005-09-09 16:23:51 -07001663 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001664 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001665 mddev->bitmap_info.offset =
1666 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001667 /* Metadata doesn't record how much space is available.
1668 * For 1.0, we assume we can use up to the superblock
1669 * if before, else to 4K beyond superblock.
1670 * For others, assume no change is possible.
1671 */
1672 if (mddev->minor_version > 0)
1673 mddev->bitmap_info.space = 0;
1674 else if (mddev->bitmap_info.offset > 0)
1675 mddev->bitmap_info.space =
1676 8 - mddev->bitmap_info.offset;
1677 else
1678 mddev->bitmap_info.space =
1679 -mddev->bitmap_info.offset;
1680 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001681
NeilBrownf6705572006-03-27 01:18:11 -08001682 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1683 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1684 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1685 mddev->new_level = le32_to_cpu(sb->new_level);
1686 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001687 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001688 if (mddev->delta_disks < 0 ||
1689 (mddev->delta_disks == 0 &&
1690 (le32_to_cpu(sb->feature_map)
1691 & MD_FEATURE_RESHAPE_BACKWARDS)))
1692 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001693 } else {
1694 mddev->reshape_position = MaxSector;
1695 mddev->delta_disks = 0;
1696 mddev->new_level = mddev->level;
1697 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001698 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001699 }
1700
Song Liu486b0f72016-08-19 15:34:01 -07001701 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001702 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001703
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001704 if (le32_to_cpu(sb->feature_map) &
1705 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001706 if (le32_to_cpu(sb->feature_map) &
1707 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1708 return -EINVAL;
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001709 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1710 (le32_to_cpu(sb->feature_map) &
1711 MD_FEATURE_MULTIPLE_PPLS))
1712 return -EINVAL;
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001713 set_bit(MD_HAS_PPL, &mddev->flags);
1714 }
NeilBrown41158c72005-06-21 17:17:25 -07001715 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001716 /* Insist of good event counter while assembling, except for
1717 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001719 if (rdev->desc_nr >= 0 &&
1720 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001721 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1722 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001723 if (ev1 < mddev->events)
1724 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001725 } else if (mddev->bitmap) {
1726 /* If adding to array with a bitmap, then we can accept an
1727 * older device, but not too old.
1728 */
NeilBrown41158c72005-06-21 17:17:25 -07001729 if (ev1 < mddev->bitmap->events_cleared)
1730 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001731 if (ev1 < mddev->events)
1732 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001733 } else {
1734 if (ev1 < mddev->events)
1735 /* just a hot-add of a new device, leave raid_disk at -1 */
1736 return 0;
1737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (mddev->level != LEVEL_MULTIPATH) {
1739 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001740 if (rdev->desc_nr < 0 ||
1741 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001742 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001743 rdev->desc_nr = -1;
1744 } else
1745 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001747 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001749 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001750 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 break;
Song Liubac624f2015-08-13 14:31:55 -07001752 case MD_DISK_ROLE_JOURNAL: /* journal device */
1753 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1754 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001755 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001756 return -EINVAL;
1757 }
1758 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001759 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001760 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001761 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 default:
NeilBrownf4667222013-12-09 12:04:56 +11001763 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001764 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001765 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001766 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001767 if (!(le32_to_cpu(sb->feature_map) &
1768 MD_FEATURE_RECOVERY_BITMAP))
1769 rdev->saved_raid_disk = -1;
1770 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001771 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 rdev->raid_disk = role;
1773 break;
1774 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001775 if (sb->devflags & WriteMostly1)
1776 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001777 if (sb->devflags & FailFast1)
1778 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001779 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1780 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001781 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001782 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return 0;
1785}
1786
NeilBrownfd01b882011-10-11 16:47:53 +11001787static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001790 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 int max_dev, i;
1792 /* make rdev->sb match mddev and rdev data. */
1793
Namhyung Kim65a06f062011-07-27 11:00:36 +10001794 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 sb->feature_map = 0;
1797 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001798 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 memset(sb->pad3, 0, sizeof(sb->pad3));
1800
1801 sb->utime = cpu_to_le64((__u64)mddev->utime);
1802 sb->events = cpu_to_le64(mddev->events);
1803 if (mddev->in_sync)
1804 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001805 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1806 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 else
1808 sb->resync_offset = cpu_to_le64(0);
1809
NeilBrown1c05b4b2006-10-21 10:24:08 -07001810 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001811
NeilBrownf0ca3402006-02-02 14:28:04 -08001812 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001813 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001814 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001815 sb->level = cpu_to_le32(mddev->level);
1816 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11001817 if (test_bit(FailFast, &rdev->flags))
1818 sb->devflags |= FailFast1;
1819 else
1820 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08001821
NeilBrownaeb9b2112011-08-25 14:43:08 +10001822 if (test_bit(WriteMostly, &rdev->flags))
1823 sb->devflags |= WriteMostly1;
1824 else
1825 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001826 sb->data_offset = cpu_to_le64(rdev->data_offset);
1827 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001828
NeilBrownc3d97142009-12-14 12:49:52 +11001829 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1830 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001831 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001832 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001833
Shaohua Lif2076e72015-10-08 21:54:12 -07001834 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001835 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001836 sb->feature_map |=
1837 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1838 sb->recovery_offset =
1839 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001840 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1841 sb->feature_map |=
1842 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001843 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001844 /* Note: recovery_offset and journal_tail share space */
1845 if (test_bit(Journal, &rdev->flags))
1846 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001847 if (test_bit(Replacement, &rdev->flags))
1848 sb->feature_map |=
1849 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001850
NeilBrownf6705572006-03-27 01:18:11 -08001851 if (mddev->reshape_position != MaxSector) {
1852 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1853 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1854 sb->new_layout = cpu_to_le32(mddev->new_layout);
1855 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1856 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001857 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001858 if (mddev->delta_disks == 0 &&
1859 mddev->reshape_backwards)
1860 sb->feature_map
1861 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001862 if (rdev->new_data_offset != rdev->data_offset) {
1863 sb->feature_map
1864 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1865 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1866 - rdev->data_offset));
1867 }
NeilBrownf6705572006-03-27 01:18:11 -08001868 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001869
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001870 if (mddev_is_clustered(mddev))
1871 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1872
NeilBrown2699b672011-07-28 11:31:47 +10001873 if (rdev->badblocks.count == 0)
1874 /* Nothing to do for bad blocks*/ ;
1875 else if (sb->bblog_offset == 0)
1876 /* Cannot record bad blocks on this device */
1877 md_error(mddev, rdev);
1878 else {
1879 struct badblocks *bb = &rdev->badblocks;
1880 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1881 u64 *p = bb->page;
1882 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1883 if (bb->changed) {
1884 unsigned seq;
1885
1886retry:
1887 seq = read_seqbegin(&bb->lock);
1888
1889 memset(bbp, 0xff, PAGE_SIZE);
1890
1891 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001892 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001893 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1894 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001895 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001896 }
NeilBrownd0962932012-03-19 12:46:41 +11001897 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001898 if (read_seqretry(&bb->lock, seq))
1899 goto retry;
1900
1901 bb->sector = (rdev->sb_start +
1902 (int)le32_to_cpu(sb->bblog_offset));
1903 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001904 }
1905 }
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001908 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (rdev2->desc_nr+1 > max_dev)
1910 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001911
NeilBrown70471da2009-08-03 10:59:57 +10001912 if (max_dev > le32_to_cpu(sb->max_dev)) {
1913 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001914 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001915 rdev->sb_size = max_dev * 2 + 256;
1916 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1917 if (rdev->sb_size & bmask)
1918 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001919 } else
1920 max_dev = le32_to_cpu(sb->max_dev);
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 for (i=0; i<max_dev;i++)
Lidong Zhong8df72022017-06-12 10:45:55 +08001923 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001924
Song Liua97b7892015-10-08 21:54:09 -07001925 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1926 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001928 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001929 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
1930 sb->feature_map |=
1931 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
1932 else
1933 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001934 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
1935 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
1936 }
1937
NeilBrowndafb20f2012-03-19 12:46:39 +11001938 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001940 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001941 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001942 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001944 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001945 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001946 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001947 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 else
Song Liuc4d4c912015-08-13 14:31:54 -07001949 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 sb->sb_csum = calc_sb_1_csum(sb);
1953}
1954
Chris Webb0cd17fe2008-06-28 08:31:46 +10001955static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001956super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001957{
1958 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001959 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001960 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001961 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001962 if (rdev->data_offset != rdev->new_data_offset)
1963 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001964 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001965 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001966 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001967 max_sectors -= rdev->data_offset;
1968 if (!num_sectors || num_sectors > max_sectors)
1969 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001970 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001971 /* minor version 0 with bitmap we can't move */
1972 return 0;
1973 } else {
1974 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001975 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001976 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001977 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001978 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001979 if (!num_sectors || num_sectors > max_sectors)
1980 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001981 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001982 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001983 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001984 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08001985 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10001986 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11001987 do {
1988 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1989 rdev->sb_page);
1990 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001991 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001992
1993}
1994
1995static int
1996super_1_allow_new_offset(struct md_rdev *rdev,
1997 unsigned long long new_offset)
1998{
1999 /* All necessary checks on new >= old have been done */
2000 struct bitmap *bitmap;
2001 if (new_offset >= rdev->data_offset)
2002 return 1;
2003
2004 /* with 1.0 metadata, there is no metadata to tread on
2005 * so we can always move back */
2006 if (rdev->mddev->minor_version == 0)
2007 return 1;
2008
2009 /* otherwise we must be sure not to step on
2010 * any metadata, so stay:
2011 * 36K beyond start of superblock
2012 * beyond end of badblocks
2013 * beyond write-intent bitmap
2014 */
2015 if (rdev->sb_start + (32+4)*2 > new_offset)
2016 return 0;
2017 bitmap = rdev->mddev->bitmap;
2018 if (bitmap && !rdev->mddev->bitmap_info.file &&
2019 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10002020 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10002021 return 0;
2022 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2023 return 0;
2024
2025 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002026}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Adrian Bunk75c96f82005-05-05 16:16:09 -07002028static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 [0] = {
2030 .name = "0.90.0",
2031 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002032 .load_super = super_90_load,
2033 .validate_super = super_90_validate,
2034 .sync_super = super_90_sync,
2035 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002036 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 },
2038 [1] = {
2039 .name = "md-1",
2040 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002041 .load_super = super_1_load,
2042 .validate_super = super_1_validate,
2043 .sync_super = super_1_sync,
2044 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002045 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 },
2047};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
NeilBrownfd01b882011-10-11 16:47:53 +11002049static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05002050{
2051 if (mddev->sync_super) {
2052 mddev->sync_super(mddev, rdev);
2053 return;
2054 }
2055
2056 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2057
2058 super_types[mddev->major_version].sync_super(mddev, rdev);
2059}
2060
NeilBrownfd01b882011-10-11 16:47:53 +11002061static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
NeilBrown3cb03002011-10-11 16:45:26 +11002063 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
NeilBrown4b809912008-07-21 17:05:25 +10002065 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07002066 rdev_for_each_rcu(rdev, mddev1) {
2067 if (test_bit(Faulty, &rdev->flags) ||
2068 test_bit(Journal, &rdev->flags) ||
2069 rdev->raid_disk == -1)
2070 continue;
2071 rdev_for_each_rcu(rdev2, mddev2) {
2072 if (test_bit(Faulty, &rdev2->flags) ||
2073 test_bit(Journal, &rdev2->flags) ||
2074 rdev2->raid_disk == -1)
2075 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002076 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002077 rdev2->bdev->bd_contains) {
2078 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002079 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002080 }
Song Liu0b020e82015-09-03 23:00:35 -07002081 }
2082 }
NeilBrown4b809912008-07-21 17:05:25 +10002083 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return 0;
2085}
2086
2087static LIST_HEAD(pending_raid_disks);
2088
Andre Nollac5e7112009-08-03 10:59:47 +10002089/*
2090 * Try to register data integrity profile for an mddev
2091 *
2092 * This is called when an array is started and after a disk has been kicked
2093 * from the array. It only succeeds if all working and active component devices
2094 * are integrity capable with matching profiles.
2095 */
NeilBrownfd01b882011-10-11 16:47:53 +11002096int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002097{
NeilBrown3cb03002011-10-11 16:45:26 +11002098 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002099
Andre Nollac5e7112009-08-03 10:59:47 +10002100 if (list_empty(&mddev->disks))
2101 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002102 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2103 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002104 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002105 /* skip spares and non-functional disks */
2106 if (test_bit(Faulty, &rdev->flags))
2107 continue;
2108 if (rdev->raid_disk < 0)
2109 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002110 if (!reference) {
2111 /* Use the first rdev as the reference */
2112 reference = rdev;
2113 continue;
2114 }
2115 /* does this rdev's profile match the reference profile? */
2116 if (blk_integrity_compare(reference->bdev->bd_disk,
2117 rdev->bdev->bd_disk) < 0)
2118 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002119 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002120 if (!reference || !bdev_get_integrity(reference->bdev))
2121 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002122 /*
2123 * All component devices are integrity capable and have matching
2124 * profiles, register the common profile for the md device.
2125 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002126 blk_integrity_register(mddev->gendisk,
2127 bdev_get_integrity(reference->bdev));
2128
NeilBrown9d487392016-11-02 14:16:49 +11002129 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Kent Overstreetafeee512018-05-20 18:25:52 -04002130 if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002131 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002132 mdname(mddev));
2133 return -EINVAL;
2134 }
Andre Nollac5e7112009-08-03 10:59:47 +10002135 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002136}
Andre Nollac5e7112009-08-03 10:59:47 +10002137EXPORT_SYMBOL(md_integrity_register);
2138
Dan Williams1501efa2016-01-13 16:00:07 -08002139/*
2140 * Attempt to add an rdev, but only if it is consistent with the current
2141 * integrity profile
2142 */
2143int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002144{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002145 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002146 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002147
2148 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002149 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002150
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002151 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002152
2153 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002154 return 0;
2155
2156 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002157 pr_err("%s: incompatible integrity profile for %s\n",
2158 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002159 return -ENXIO;
2160 }
2161
2162 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002163}
2164EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002165
NeilBrownf72ffdd2014-09-30 14:23:59 +10002166static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002168 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002169 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002170 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Dan Williams11e2ede2008-04-30 00:52:32 -07002172 /* prevent duplicates */
2173 if (find_rdev(mddev, rdev->bdev->bd_dev))
2174 return -EEXIST;
2175
NeilBrown97b20ef2017-04-13 08:53:48 +10002176 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2177 mddev->pers)
2178 return -EROFS;
2179
Andre Nolldd8ac332009-03-31 14:33:13 +11002180 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002181 if (!test_bit(Journal, &rdev->flags) &&
2182 rdev->sectors &&
2183 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002184 if (mddev->pers) {
2185 /* Cannot change size, so fail
2186 * If mddev->level <= 0, then we don't care
2187 * about aligning sizes (e.g. linear)
2188 */
2189 if (mddev->level > 0)
2190 return -ENOSPC;
2191 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002192 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 /* Verify rdev->desc_nr is unique.
2196 * If it is -1, assign a free number, else
2197 * check number is not in use
2198 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002199 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (rdev->desc_nr < 0) {
2201 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002202 if (mddev->pers)
2203 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002204 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 choice++;
2206 rdev->desc_nr = choice;
2207 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002208 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002209 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002213 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002214 if (!test_bit(Journal, &rdev->flags) &&
2215 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002216 pr_warn("md: %s: array is limited to %d devices\n",
2217 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002218 return -EBUSY;
2219 }
NeilBrown19133a42005-11-08 21:39:35 -08002220 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002221 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002224 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002225
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002226 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002227 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002228
Tejun Heo0762b8b2008-08-25 19:56:12 +09002229 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002230 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2231 /* failure here is OK */;
2232 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002233
NeilBrown4b809912008-07-21 17:05:25 +10002234 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002235 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002236
2237 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002238 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002241
2242 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002243 pr_warn("md: failed to register dev-%s for %s\n",
2244 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002245 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
NeilBrown177a99b2008-02-06 01:39:56 -08002248static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002249{
NeilBrown3cb03002011-10-11 16:45:26 +11002250 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002251 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002252 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002253}
2254
NeilBrownf72ffdd2014-09-30 14:23:59 +10002255static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002258
Tejun Heo49731ba2011-01-14 18:43:57 +01002259 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002260 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002261 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002263 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002264 sysfs_put(rdev->sysfs_state);
2265 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002266 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002267 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002268 * writing to 'remove' to "dev/state". We also need
2269 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002270 */
NeilBrown4b809912008-07-21 17:05:25 +10002271 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002272 INIT_WORK(&rdev->del_work, md_delayed_delete);
2273 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002274 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275}
2276
2277/*
2278 * prevent the device from being mounted, repartitioned or
2279 * otherwise reused by a RAID array (or any other kernel
2280 * subsystem), by bd_claiming the device.
2281 */
NeilBrown3cb03002011-10-11 16:45:26 +11002282static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
2284 int err = 0;
2285 struct block_device *bdev;
2286 char b[BDEVNAME_SIZE];
2287
Tejun Heod4d77622010-11-13 11:55:18 +01002288 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002289 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002291 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return PTR_ERR(bdev);
2293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 rdev->bdev = bdev;
2295 return err;
2296}
2297
NeilBrown3cb03002011-10-11 16:45:26 +11002298static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
2300 struct block_device *bdev = rdev->bdev;
2301 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002302 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
2305void md_autodetect_dev(dev_t dev);
2306
NeilBrownf72ffdd2014-09-30 14:23:59 +10002307static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
2309 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002310
NeilBrown9d487392016-11-02 14:16:49 +11002311 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002312 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002314 if (test_bit(AutoDetected, &rdev->flags))
2315 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316#endif
2317 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002318 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319}
2320
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002321void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
2323 unbind_rdev_from_array(rdev);
2324 export_rdev(rdev);
2325}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002326EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
NeilBrownfd01b882011-10-11 16:47:53 +11002328static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
NeilBrown0638bb02014-09-25 17:43:47 +10002330 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
NeilBrown0638bb02014-09-25 17:43:47 +10002332 while (!list_empty(&mddev->disks)) {
2333 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2334 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002335 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 mddev->raid_disks = 0;
2338 mddev->major_version = 0;
2339}
2340
NeilBrown6497709b2017-03-15 14:05:14 +11002341static bool set_in_sync(struct mddev *mddev)
2342{
Shaohua Liefa4b772017-10-18 22:08:13 -07002343 lockdep_assert_held(&mddev->lock);
NeilBrown4ad23a972017-03-15 14:05:14 +11002344 if (!mddev->in_sync) {
2345 mddev->sync_checkers++;
2346 spin_unlock(&mddev->lock);
2347 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2348 spin_lock(&mddev->lock);
2349 if (!mddev->in_sync &&
2350 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002351 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002352 /*
2353 * Ensure ->in_sync is visible before we clear
2354 * ->sync_checkers.
2355 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002356 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002357 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2358 sysfs_notify_dirent_safe(mddev->sysfs_state);
2359 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002360 if (--mddev->sync_checkers == 0)
2361 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002362 }
2363 if (mddev->safemode == 1)
2364 mddev->safemode = 0;
2365 return mddev->in_sync;
2366}
2367
NeilBrownf72ffdd2014-09-30 14:23:59 +10002368static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
NeilBrown42543762006-06-26 00:27:57 -07002370 /* Update each superblock (in-memory image), but
2371 * if we are allowed to, skip spares which already
2372 * have the right event counter, or have one earlier
2373 * (which would mean they aren't being marked as dirty
2374 * with the rest of the array)
2375 */
NeilBrown3cb03002011-10-11 16:45:26 +11002376 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002377 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002378 if (rdev->sb_events == mddev->events ||
2379 (nospares &&
2380 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002381 rdev->sb_events+1 == mddev->events)) {
2382 /* Don't update this superblock */
2383 rdev->sb_loaded = 2;
2384 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002385 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002386 rdev->sb_loaded = 1;
2387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 }
2389}
2390
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002391static bool does_sb_need_changing(struct mddev *mddev)
2392{
2393 struct md_rdev *rdev;
2394 struct mdp_superblock_1 *sb;
2395 int role;
2396
2397 /* Find a good rdev */
2398 rdev_for_each(rdev, mddev)
2399 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2400 break;
2401
2402 /* No good device found. */
2403 if (!rdev)
2404 return false;
2405
2406 sb = page_address(rdev->sb_page);
2407 /* Check if a device has become faulty or a spare become active */
2408 rdev_for_each(rdev, mddev) {
2409 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2410 /* Device activated? */
2411 if (role == 0xffff && rdev->raid_disk >=0 &&
2412 !test_bit(Faulty, &rdev->flags))
2413 return true;
2414 /* Device turned faulty? */
2415 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2416 return true;
2417 }
2418
2419 /* Check if any mddev parameters have changed */
2420 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2421 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002422 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002423 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2424 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2425 return true;
2426
2427 return false;
2428}
2429
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002430void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
NeilBrown3cb03002011-10-11 16:45:26 +11002432 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002433 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002434 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002435 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002436 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
NeilBrownd87f0642013-04-24 11:42:40 +10002438 if (mddev->ro) {
2439 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002440 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002441 return;
2442 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002443
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002444repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002445 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002446 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002447 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002448 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002449 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002450 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002451 /* Has someone else has updated the sb */
2452 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002453 if (ret == 0)
2454 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002455 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2456 BIT(MD_SB_CHANGE_DEVS) |
2457 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002458 return;
2459 }
2460 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002461
NeilBrowndb0505d2017-10-17 16:18:36 +11002462 /*
2463 * First make sure individual recovery_offsets are correct
2464 * curr_resync_completed can only be used during recovery.
2465 * During reshape/resync it might use array-addresses rather
2466 * that device addresses.
2467 */
NeilBrowndafb20f2012-03-19 12:46:39 +11002468 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002469 if (rdev->raid_disk >= 0 &&
2470 mddev->delta_disks >= 0 &&
NeilBrowndb0505d2017-10-17 16:18:36 +11002471 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2472 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2473 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002474 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002475 !test_bit(In_sync, &rdev->flags) &&
2476 mddev->curr_resync_completed > rdev->recovery_offset)
2477 rdev->recovery_offset = mddev->curr_resync_completed;
2478
NeilBrownf72ffdd2014-09-30 14:23:59 +10002479 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002480 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002481 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2482 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002483 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002484 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002485 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002486 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002487 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002488 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002489 md_error(mddev, rdev);
2490 }
2491 clear_bit(Blocked, &rdev->flags);
2492 clear_bit(BlockedBadBlocks, &rdev->flags);
2493 wake_up(&rdev->blocked_wait);
2494 }
2495 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002496 wake_up(&mddev->sb_wait);
2497 return;
2498 }
2499
NeilBrown85572d72014-12-15 12:56:56 +11002500 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002501
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002502 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002503
Shaohua Li29530792016-12-08 15:48:19 -08002504 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002505 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002506 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002507 /* just a clean<-> dirty transition, possibly leave spares alone,
2508 * though if events isn't the right even/odd, we will have to do
2509 * spares after all
2510 */
2511 nospares = 1;
2512 if (force_change)
2513 nospares = 0;
2514 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002515 /* If the array is degraded, then skipping spares is both
2516 * dangerous and fairly pointless.
2517 * Dangerous because a device that was removed from the array
2518 * might have a event_count that still looks up-to-date,
2519 * so it can be re-added without a resync.
2520 * Pointless because if there are any spares to skip,
2521 * then a recovery will happen and soon that array won't
2522 * be degraded any more and the spare can go back to sleep then.
2523 */
NeilBrown850b2b422006-10-03 01:15:46 -07002524 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002525
NeilBrown06d91a52005-06-21 17:17:12 -07002526 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002527
2528 /* If this is just a dirty<->clean transition, and the array is clean
2529 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002530 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002531 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002532 && mddev->can_decrease_events
2533 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002534 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002535 mddev->can_decrease_events = 0;
2536 } else {
NeilBrown42543762006-06-26 00:27:57 -07002537 /* otherwise we have to go forward and ... */
2538 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002539 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
NeilBrown403df472014-09-30 15:52:29 +10002542 /*
2543 * This 64-bit counter should never wrap.
2544 * Either we are in around ~1 trillion A.C., assuming
2545 * 1 reboot per second, or we have a bug...
2546 */
2547 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002548
NeilBrowndafb20f2012-03-19 12:46:39 +11002549 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002550 if (rdev->badblocks.changed)
2551 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002552 if (test_bit(Faulty, &rdev->flags))
2553 set_bit(FaultRecorded, &rdev->flags);
2554 }
NeilBrown2699b672011-07-28 11:31:47 +10002555
NeilBrowne6910632008-02-06 01:39:51 -08002556 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002557 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
NeilBrown36a4e1f2011-10-07 14:23:17 +11002559 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2560 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Shaohua Li504634f2016-11-18 09:44:08 -08002562 if (mddev->queue)
2563 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002564rewrite:
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002565 md_bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002566 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002568
NeilBrown42543762006-06-26 00:27:57 -07002569 if (rdev->sb_loaded != 1)
2570 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
NeilBrownf4667222013-12-09 12:04:56 +11002572 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002573 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002574 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002575 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002576 pr_debug("md: (write) %s's sb offset: %llu\n",
2577 bdevname(rdev->bdev, b),
2578 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002579 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002580 if (rdev->badblocks.size) {
2581 md_super_write(mddev, rdev,
2582 rdev->badblocks.sector,
2583 rdev->badblocks.size << 9,
2584 rdev->bb_page);
2585 rdev->badblocks.size = 0;
2586 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002587
NeilBrownf4667222013-12-09 12:04:56 +11002588 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002589 pr_debug("md: %s (skipping faulty)\n",
2590 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002591
NeilBrown7bfa19f2005-06-21 17:17:28 -07002592 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 /* only need to write one superblock... */
2594 break;
2595 }
NeilBrown46533ff2016-11-18 16:16:11 +11002596 if (md_super_wait(mddev) < 0)
2597 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002598 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002599
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002600 if (mddev_is_clustered(mddev) && ret == 0)
2601 md_cluster_ops->metadata_update_finish(mddev);
2602
NeilBrown850b2b422006-10-03 01:15:46 -07002603 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002604 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2605 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002606 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002607 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002608 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002609 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2610 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002611
NeilBrowndafb20f2012-03-19 12:46:39 +11002612 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002613 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2614 clear_bit(Blocked, &rdev->flags);
2615
2616 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002617 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002618 clear_bit(BlockedBadBlocks, &rdev->flags);
2619 wake_up(&rdev->blocked_wait);
2620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002622EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002624static int add_bound_rdev(struct md_rdev *rdev)
2625{
2626 struct mddev *mddev = rdev->mddev;
2627 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002628 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002629
Shaohua Li87d4d912016-01-06 14:37:14 -08002630 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002631 /* If there is hot_add_disk but no hot_remove_disk
2632 * then added disks for geometry changes,
2633 * and should be added immediately.
2634 */
2635 super_types[mddev->major_version].
2636 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002637 if (add_journal)
2638 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002639 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002640 if (add_journal)
2641 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002642 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002643 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002644 return err;
2645 }
2646 }
2647 sysfs_notify_dirent_safe(rdev->sysfs_state);
2648
Shaohua Li29530792016-12-08 15:48:19 -08002649 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002650 if (mddev->degraded)
2651 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2652 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2653 md_new_event(mddev);
2654 md_wakeup_thread(mddev->thread);
2655 return 0;
2656}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Andre Noll7f6ce762008-03-23 18:34:54 +01002658/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002659 * We want to accept with case. For this we use cmd_match.
2660 */
2661static int cmd_match(const char *cmd, const char *str)
2662{
2663 /* See if cmd, written into a sysfs file, matches
2664 * str. They must either be the same, or cmd can
2665 * have a trailing newline
2666 */
2667 while (*cmd && *str && *cmd == *str) {
2668 cmd++;
2669 str++;
2670 }
2671 if (*cmd == '\n')
2672 cmd++;
2673 if (*str || *cmd)
2674 return 0;
2675 return 1;
2676}
2677
NeilBrown86e6ffd2005-11-08 21:39:24 -08002678struct rdev_sysfs_entry {
2679 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002680 ssize_t (*show)(struct md_rdev *, char *);
2681 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002682};
2683
2684static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002685state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002686{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002687 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002688 size_t len = 0;
Mark Rutland6aa7de02017-10-23 14:07:29 -07002689 unsigned long flags = READ_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002690
NeilBrown758bfc82014-12-15 12:56:59 +11002691 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002692 (!test_bit(ExternalBbl, &flags) &&
2693 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002694 len += sprintf(page+len, "faulty%s", sep);
2695 if (test_bit(In_sync, &flags))
2696 len += sprintf(page+len, "in_sync%s", sep);
2697 if (test_bit(Journal, &flags))
2698 len += sprintf(page+len, "journal%s", sep);
2699 if (test_bit(WriteMostly, &flags))
2700 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002701 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002702 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002703 && !test_bit(Faulty, &flags)))
2704 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002705 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002706 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002707 !test_bit(In_sync, &flags))
2708 len += sprintf(page+len, "spare%s", sep);
2709 if (test_bit(WriteErrorSeen, &flags))
2710 len += sprintf(page+len, "write_error%s", sep);
2711 if (test_bit(WantReplacement, &flags))
2712 len += sprintf(page+len, "want_replacement%s", sep);
2713 if (test_bit(Replacement, &flags))
2714 len += sprintf(page+len, "replacement%s", sep);
2715 if (test_bit(ExternalBbl, &flags))
2716 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002717 if (test_bit(FailFast, &flags))
2718 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002719
2720 if (len)
2721 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002722
NeilBrown86e6ffd2005-11-08 21:39:24 -08002723 return len+sprintf(page+len, "\n");
2724}
2725
NeilBrown45dc2de2006-06-26 00:27:58 -07002726static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002727state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002728{
2729 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002730 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002731 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002732 * writemostly - sets write_mostly
2733 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002734 * blocked - sets the Blocked flags
2735 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002736 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002737 * -insync - clear Insync for a device with a slot assigned,
2738 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002739 * write_error - sets WriteErrorSeen
2740 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002741 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002742 */
2743 int err = -EINVAL;
2744 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2745 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002746 if (test_bit(Faulty, &rdev->flags))
2747 err = 0;
2748 else
2749 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002750 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002751 if (rdev->mddev->pers) {
2752 clear_bit(Blocked, &rdev->flags);
2753 remove_and_add_spares(rdev->mddev, rdev);
2754 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002755 if (rdev->raid_disk >= 0)
2756 err = -EBUSY;
2757 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002758 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002759 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002760 if (mddev_is_clustered(mddev))
2761 err = md_cluster_ops->remove_disk(mddev, rdev);
2762
2763 if (err == 0) {
2764 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002765 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002766 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002767 md_wakeup_thread(mddev->thread);
2768 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002769 md_new_event(mddev);
2770 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002771 }
NeilBrownf6556752006-06-26 00:28:01 -07002772 } else if (cmd_match(buf, "writemostly")) {
2773 set_bit(WriteMostly, &rdev->flags);
2774 err = 0;
2775 } else if (cmd_match(buf, "-writemostly")) {
2776 clear_bit(WriteMostly, &rdev->flags);
2777 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002778 } else if (cmd_match(buf, "blocked")) {
2779 set_bit(Blocked, &rdev->flags);
2780 err = 0;
2781 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002782 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002783 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002784 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002785 /* metadata handler doesn't understand badblocks,
2786 * so we need to fail the device
2787 */
2788 md_error(rdev->mddev, rdev);
2789 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002790 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002791 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002792 wake_up(&rdev->blocked_wait);
2793 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2794 md_wakeup_thread(rdev->mddev->thread);
2795
2796 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002797 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2798 set_bit(In_sync, &rdev->flags);
2799 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11002800 } else if (cmd_match(buf, "failfast")) {
2801 set_bit(FailFast, &rdev->flags);
2802 err = 0;
2803 } else if (cmd_match(buf, "-failfast")) {
2804 clear_bit(FailFast, &rdev->flags);
2805 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002806 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2807 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002808 if (rdev->mddev->pers == NULL) {
2809 clear_bit(In_sync, &rdev->flags);
2810 rdev->saved_raid_disk = rdev->raid_disk;
2811 rdev->raid_disk = -1;
2812 err = 0;
2813 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002814 } else if (cmd_match(buf, "write_error")) {
2815 set_bit(WriteErrorSeen, &rdev->flags);
2816 err = 0;
2817 } else if (cmd_match(buf, "-write_error")) {
2818 clear_bit(WriteErrorSeen, &rdev->flags);
2819 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002820 } else if (cmd_match(buf, "want_replacement")) {
2821 /* Any non-spare device that is not a replacement can
2822 * become want_replacement at any time, but we then need to
2823 * check if recovery is needed.
2824 */
2825 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002826 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002827 !test_bit(Replacement, &rdev->flags))
2828 set_bit(WantReplacement, &rdev->flags);
2829 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2830 md_wakeup_thread(rdev->mddev->thread);
2831 err = 0;
2832 } else if (cmd_match(buf, "-want_replacement")) {
2833 /* Clearing 'want_replacement' is always allowed.
2834 * Once replacements starts it is too late though.
2835 */
2836 err = 0;
2837 clear_bit(WantReplacement, &rdev->flags);
2838 } else if (cmd_match(buf, "replacement")) {
2839 /* Can only set a device as a replacement when array has not
2840 * yet been started. Once running, replacement is automatic
2841 * from spares, or by assigning 'slot'.
2842 */
2843 if (rdev->mddev->pers)
2844 err = -EBUSY;
2845 else {
2846 set_bit(Replacement, &rdev->flags);
2847 err = 0;
2848 }
2849 } else if (cmd_match(buf, "-replacement")) {
2850 /* Similarly, can only clear Replacement before start */
2851 if (rdev->mddev->pers)
2852 err = -EBUSY;
2853 else {
2854 clear_bit(Replacement, &rdev->flags);
2855 err = 0;
2856 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002857 } else if (cmd_match(buf, "re-add")) {
NeilBrown011abdc2018-04-26 14:46:29 +10002858 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
2859 rdev->saved_raid_disk >= 0) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002860 /* clear_bit is performed _after_ all the devices
2861 * have their local Faulty bit cleared. If any writes
2862 * happen in the meantime in the local node, they
2863 * will land in the local bitmap, which will be synced
2864 * by this node eventually
2865 */
2866 if (!mddev_is_clustered(rdev->mddev) ||
2867 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2868 clear_bit(Faulty, &rdev->flags);
2869 err = add_bound_rdev(rdev);
2870 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002871 } else
2872 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002873 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2874 set_bit(ExternalBbl, &rdev->flags);
2875 rdev->badblocks.shift = 0;
2876 err = 0;
2877 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2878 clear_bit(ExternalBbl, &rdev->flags);
2879 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002880 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002881 if (!err)
2882 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002883 return err ? err : len;
2884}
NeilBrown80ca3a42006-07-10 04:44:18 -07002885static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002886__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002887
2888static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002889errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002890{
2891 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2892}
2893
2894static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002895errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002896{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002897 unsigned int n;
2898 int rv;
2899
2900 rv = kstrtouint(buf, 10, &n);
2901 if (rv < 0)
2902 return rv;
2903 atomic_set(&rdev->corrected_errors, n);
2904 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002905}
2906static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002907__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002908
NeilBrown014236d2006-01-06 00:20:55 -08002909static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002910slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002911{
Shaohua Lif2076e72015-10-08 21:54:12 -07002912 if (test_bit(Journal, &rdev->flags))
2913 return sprintf(page, "journal\n");
2914 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002915 return sprintf(page, "none\n");
2916 else
2917 return sprintf(page, "%d\n", rdev->raid_disk);
2918}
2919
2920static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002921slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002922{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002923 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002924 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002925
Shaohua Lif2076e72015-10-08 21:54:12 -07002926 if (test_bit(Journal, &rdev->flags))
2927 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002928 if (strncmp(buf, "none", 4)==0)
2929 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002930 else {
2931 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2932 if (err < 0)
2933 return err;
2934 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002935 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002936 /* Setting 'slot' on an active array requires also
2937 * updating the 'rd%d' link, and communicating
2938 * with the personality with ->hot_*_disk.
2939 * For now we only support removing
2940 * failed/spare devices. This normally happens automatically,
2941 * but not when the metadata is externally managed.
2942 */
NeilBrownc303da62008-02-06 01:39:51 -08002943 if (rdev->raid_disk == -1)
2944 return -EEXIST;
2945 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002946 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002947 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002948 clear_bit(Blocked, &rdev->flags);
2949 remove_and_add_spares(rdev->mddev, rdev);
2950 if (rdev->raid_disk >= 0)
2951 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002952 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2953 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002954 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002955 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002956 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002957 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002958 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002959
2960 if (rdev->raid_disk != -1)
2961 return -EBUSY;
2962
NeilBrownc6751b22011-02-02 11:57:13 +11002963 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2964 return -EBUSY;
2965
Neil Brown6c2fce22008-06-28 08:31:31 +10002966 if (rdev->mddev->pers->hot_add_disk == NULL)
2967 return -EINVAL;
2968
NeilBrownba1b41b2011-01-14 09:14:34 +11002969 if (slot >= rdev->mddev->raid_disks &&
2970 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2971 return -ENOSPC;
2972
Neil Brown6c2fce22008-06-28 08:31:31 +10002973 rdev->raid_disk = slot;
2974 if (test_bit(In_sync, &rdev->flags))
2975 rdev->saved_raid_disk = slot;
2976 else
2977 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002978 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002979 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002980 err = rdev->mddev->pers->
2981 hot_add_disk(rdev->mddev, rdev);
2982 if (err) {
2983 rdev->raid_disk = -1;
2984 return err;
2985 } else
2986 sysfs_notify_dirent_safe(rdev->sysfs_state);
2987 if (sysfs_link_rdev(rdev->mddev, rdev))
2988 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002989 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002990 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002991 if (slot >= rdev->mddev->raid_disks &&
2992 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002993 return -ENOSPC;
2994 rdev->raid_disk = slot;
2995 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002996 clear_bit(Faulty, &rdev->flags);
2997 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002998 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002999 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08003000 }
NeilBrown014236d2006-01-06 00:20:55 -08003001 return len;
3002}
3003
NeilBrown014236d2006-01-06 00:20:55 -08003004static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07003005__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08003006
NeilBrown93c8cad2006-01-06 00:20:56 -08003007static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003008offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08003009{
Andrew Morton6961ece2006-01-06 00:20:59 -08003010 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08003011}
3012
3013static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003014offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08003015{
NeilBrownc6563a82012-05-21 09:27:00 +10003016 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003017 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003018 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10003019 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003020 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003021 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08003022 /* Must set offset before size, so overlap checks
3023 * can be sane */
3024 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08003025 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10003026 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08003027 return len;
3028}
3029
3030static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07003031__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08003032
NeilBrownc6563a82012-05-21 09:27:00 +10003033static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3034{
3035 return sprintf(page, "%llu\n",
3036 (unsigned long long)rdev->new_data_offset);
3037}
3038
3039static ssize_t new_offset_store(struct md_rdev *rdev,
3040 const char *buf, size_t len)
3041{
3042 unsigned long long new_offset;
3043 struct mddev *mddev = rdev->mddev;
3044
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003045 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10003046 return -EINVAL;
3047
NeilBrownf851b602014-12-11 10:02:10 +11003048 if (mddev->sync_thread ||
3049 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10003050 return -EBUSY;
3051 if (new_offset == rdev->data_offset)
3052 /* reset is always permitted */
3053 ;
3054 else if (new_offset > rdev->data_offset) {
3055 /* must not push array size beyond rdev_sectors */
3056 if (new_offset - rdev->data_offset
3057 + mddev->dev_sectors > rdev->sectors)
3058 return -E2BIG;
3059 }
3060 /* Metadata worries about other space details. */
3061
3062 /* decreasing the offset is inconsistent with a backwards
3063 * reshape.
3064 */
3065 if (new_offset < rdev->data_offset &&
3066 mddev->reshape_backwards)
3067 return -EINVAL;
3068 /* Increasing offset is inconsistent with forwards
3069 * reshape. reshape_direction should be set to
3070 * 'backwards' first.
3071 */
3072 if (new_offset > rdev->data_offset &&
3073 !mddev->reshape_backwards)
3074 return -EINVAL;
3075
3076 if (mddev->pers && mddev->persistent &&
3077 !super_types[mddev->major_version]
3078 .allow_new_offset(rdev, new_offset))
3079 return -E2BIG;
3080 rdev->new_data_offset = new_offset;
3081 if (new_offset > rdev->data_offset)
3082 mddev->reshape_backwards = 1;
3083 else if (new_offset < rdev->data_offset)
3084 mddev->reshape_backwards = 0;
3085
3086 return len;
3087}
3088static struct rdev_sysfs_entry rdev_new_offset =
3089__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3090
NeilBrown83303b62006-01-06 00:21:06 -08003091static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003092rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003093{
Andre Nolldd8ac332009-03-31 14:33:13 +11003094 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003095}
3096
NeilBrownc5d79ad2008-02-06 01:39:54 -08003097static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3098{
3099 /* check if two start/length pairs overlap */
3100 if (s1+l1 <= s2)
3101 return 0;
3102 if (s2+l2 <= s1)
3103 return 0;
3104 return 1;
3105}
3106
Dan Williamsb522adc2009-03-31 15:00:31 +11003107static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3108{
3109 unsigned long long blocks;
3110 sector_t new;
3111
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003112 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003113 return -EINVAL;
3114
3115 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3116 return -EINVAL; /* sector conversion overflow */
3117
3118 new = blocks * 2;
3119 if (new != blocks * 2)
3120 return -EINVAL; /* unsigned long long to sector_t overflow */
3121
3122 *sectors = new;
3123 return 0;
3124}
3125
NeilBrown83303b62006-01-06 00:21:06 -08003126static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003127rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003128{
NeilBrownfd01b882011-10-11 16:47:53 +11003129 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003130 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003131 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003132
Shaohua Lif2076e72015-10-08 21:54:12 -07003133 if (test_bit(Journal, &rdev->flags))
3134 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003135 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003136 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003137 if (rdev->data_offset != rdev->new_data_offset)
3138 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003139 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003140 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003141 sectors = super_types[my_mddev->major_version].
3142 rdev_size_change(rdev, sectors);
3143 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003144 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003145 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003146 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003147 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003148 if (!my_mddev->pers->resize)
3149 /* Cannot change size for RAID0 or Linear etc */
3150 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003151 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003152 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003153 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003154
Andre Nolldd8ac332009-03-31 14:33:13 +11003155 rdev->sectors = sectors;
3156 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003157 /* Need to check that all other rdevs with the same
3158 * ->bdev do not overlap. 'rcu' is sufficient to walk
3159 * the rdev lists safely.
3160 * This check does not provide a hard guarantee, it
3161 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003162 */
NeilBrownfd01b882011-10-11 16:47:53 +11003163 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003164 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003165 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003166
NeilBrown8b1afc32014-09-29 15:33:20 +10003167 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003168 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003169 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003170
NeilBrowndafb20f2012-03-19 12:46:39 +11003171 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003172 if (rdev->bdev == rdev2->bdev &&
3173 rdev != rdev2 &&
3174 overlaps(rdev->data_offset, rdev->sectors,
3175 rdev2->data_offset,
3176 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003177 overlap = 1;
3178 break;
3179 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003180 if (overlap) {
3181 mddev_put(mddev);
3182 break;
3183 }
3184 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003185 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003186 if (overlap) {
3187 /* Someone else could have slipped in a size
3188 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003189 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003190 * safe, and trust userspace not to race with
3191 * itself
3192 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003193 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003194 return -EBUSY;
3195 }
3196 }
NeilBrown83303b62006-01-06 00:21:06 -08003197 return len;
3198}
3199
3200static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003201__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003202
NeilBrown3cb03002011-10-11 16:45:26 +11003203static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003204{
3205 unsigned long long recovery_start = rdev->recovery_offset;
3206
3207 if (test_bit(In_sync, &rdev->flags) ||
3208 recovery_start == MaxSector)
3209 return sprintf(page, "none\n");
3210
3211 return sprintf(page, "%llu\n", recovery_start);
3212}
3213
NeilBrown3cb03002011-10-11 16:45:26 +11003214static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003215{
3216 unsigned long long recovery_start;
3217
3218 if (cmd_match(buf, "none"))
3219 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003220 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003221 return -EINVAL;
3222
3223 if (rdev->mddev->pers &&
3224 rdev->raid_disk >= 0)
3225 return -EBUSY;
3226
3227 rdev->recovery_offset = recovery_start;
3228 if (recovery_start == MaxSector)
3229 set_bit(In_sync, &rdev->flags);
3230 else
3231 clear_bit(In_sync, &rdev->flags);
3232 return len;
3233}
3234
3235static struct rdev_sysfs_entry rdev_recovery_start =
3236__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3237
Vishal Vermafc974ee2015-12-24 19:20:34 -07003238/* sysfs access to bad-blocks list.
3239 * We present two files.
3240 * 'bad-blocks' lists sector numbers and lengths of ranges that
3241 * are recorded as bad. The list is truncated to fit within
3242 * the one-page limit of sysfs.
3243 * Writing "sector length" to this file adds an acknowledged
3244 * bad block list.
3245 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3246 * been acknowledged. Writing to this file adds bad blocks
3247 * without acknowledging them. This is largely for testing.
3248 */
NeilBrown3cb03002011-10-11 16:45:26 +11003249static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003250{
3251 return badblocks_show(&rdev->badblocks, page, 0);
3252}
NeilBrown3cb03002011-10-11 16:45:26 +11003253static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003254{
NeilBrownde393cd2011-07-28 11:31:48 +10003255 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3256 /* Maybe that ack was all we needed */
3257 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3258 wake_up(&rdev->blocked_wait);
3259 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003260}
3261static struct rdev_sysfs_entry rdev_bad_blocks =
3262__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3263
NeilBrown3cb03002011-10-11 16:45:26 +11003264static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003265{
3266 return badblocks_show(&rdev->badblocks, page, 1);
3267}
NeilBrown3cb03002011-10-11 16:45:26 +11003268static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003269{
3270 return badblocks_store(&rdev->badblocks, page, len, 1);
3271}
3272static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3273__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3274
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003275static ssize_t
3276ppl_sector_show(struct md_rdev *rdev, char *page)
3277{
3278 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3279}
3280
3281static ssize_t
3282ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3283{
3284 unsigned long long sector;
3285
3286 if (kstrtoull(buf, 10, &sector) < 0)
3287 return -EINVAL;
3288 if (sector != (sector_t)sector)
3289 return -EINVAL;
3290
3291 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3292 rdev->raid_disk >= 0)
3293 return -EBUSY;
3294
3295 if (rdev->mddev->persistent) {
3296 if (rdev->mddev->major_version == 0)
3297 return -EINVAL;
3298 if ((sector > rdev->sb_start &&
3299 sector - rdev->sb_start > S16_MAX) ||
3300 (sector < rdev->sb_start &&
3301 rdev->sb_start - sector > -S16_MIN))
3302 return -EINVAL;
3303 rdev->ppl.offset = sector - rdev->sb_start;
3304 } else if (!rdev->mddev->external) {
3305 return -EBUSY;
3306 }
3307 rdev->ppl.sector = sector;
3308 return len;
3309}
3310
3311static struct rdev_sysfs_entry rdev_ppl_sector =
3312__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3313
3314static ssize_t
3315ppl_size_show(struct md_rdev *rdev, char *page)
3316{
3317 return sprintf(page, "%u\n", rdev->ppl.size);
3318}
3319
3320static ssize_t
3321ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3322{
3323 unsigned int size;
3324
3325 if (kstrtouint(buf, 10, &size) < 0)
3326 return -EINVAL;
3327
3328 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3329 rdev->raid_disk >= 0)
3330 return -EBUSY;
3331
3332 if (rdev->mddev->persistent) {
3333 if (rdev->mddev->major_version == 0)
3334 return -EINVAL;
3335 if (size > U16_MAX)
3336 return -EINVAL;
3337 } else if (!rdev->mddev->external) {
3338 return -EBUSY;
3339 }
3340 rdev->ppl.size = size;
3341 return len;
3342}
3343
3344static struct rdev_sysfs_entry rdev_ppl_size =
3345__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3346
NeilBrown86e6ffd2005-11-08 21:39:24 -08003347static struct attribute *rdev_default_attrs[] = {
3348 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003349 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003350 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003351 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003352 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003353 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003354 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003355 &rdev_bad_blocks.attr,
3356 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003357 &rdev_ppl_sector.attr,
3358 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003359 NULL,
3360};
3361static ssize_t
3362rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3363{
3364 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003365 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003366
3367 if (!entry->show)
3368 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003369 if (!rdev->mddev)
3370 return -EBUSY;
3371 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003372}
3373
3374static ssize_t
3375rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3376 const char *page, size_t length)
3377{
3378 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003379 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003380 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003381 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003382
3383 if (!entry->store)
3384 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003385 if (!capable(CAP_SYS_ADMIN))
3386 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003387 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003388 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003389 if (rdev->mddev == NULL)
3390 rv = -EBUSY;
3391 else
3392 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003393 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003394 }
3395 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003396}
3397
3398static void rdev_free(struct kobject *ko)
3399{
NeilBrown3cb03002011-10-11 16:45:26 +11003400 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003401 kfree(rdev);
3402}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003403static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003404 .show = rdev_attr_show,
3405 .store = rdev_attr_store,
3406};
3407static struct kobj_type rdev_ktype = {
3408 .release = rdev_free,
3409 .sysfs_ops = &rdev_sysfs_ops,
3410 .default_attrs = rdev_default_attrs,
3411};
3412
NeilBrown3cb03002011-10-11 16:45:26 +11003413int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003414{
3415 rdev->desc_nr = -1;
3416 rdev->saved_raid_disk = -1;
3417 rdev->raid_disk = -1;
3418 rdev->flags = 0;
3419 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003420 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003421 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003422 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003423 rdev->sb_loaded = 0;
3424 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003425 atomic_set(&rdev->nr_pending, 0);
3426 atomic_set(&rdev->read_errors, 0);
3427 atomic_set(&rdev->corrected_errors, 0);
3428
3429 INIT_LIST_HEAD(&rdev->same_set);
3430 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003431
3432 /* Add space to store bad block list.
3433 * This reserves the space even on arrays where it cannot
3434 * be used - I wonder if that matters
3435 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003436 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003437}
3438EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439/*
3440 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3441 *
3442 * mark the device faulty if:
3443 *
3444 * - the device is nonexistent (zero size)
3445 * - the device has no valid superblock
3446 *
3447 * a faulty rdev _never_ has rdev->sb set.
3448 */
NeilBrown3cb03002011-10-11 16:45:26 +11003449static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450{
3451 char b[BDEVNAME_SIZE];
3452 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003453 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 sector_t size;
3455
NeilBrown9ffae0c2006-01-06 00:20:32 -08003456 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003457 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
NeilBrown2230dfe2011-07-28 11:31:46 +10003460 err = md_rdev_init(rdev);
3461 if (err)
3462 goto abort_free;
3463 err = alloc_disk_sb(rdev);
3464 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 goto abort_free;
3466
NeilBrownc5d79ad2008-02-06 01:39:54 -08003467 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 if (err)
3469 goto abort_free;
3470
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003471 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003472
Mike Snitzer77304d22010-11-08 14:39:12 +01003473 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003475 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 bdevname(rdev->bdev,b));
3477 err = -EINVAL;
3478 goto abort_free;
3479 }
3480
3481 if (super_format >= 0) {
3482 err = super_types[super_format].
3483 load_super(rdev, NULL, super_minor);
3484 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003485 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003486 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003487 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 goto abort_free;
3489 }
3490 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003491 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 bdevname(rdev->bdev,b));
3493 goto abort_free;
3494 }
3495 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003496
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return rdev;
3498
3499abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003500 if (rdev->bdev)
3501 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003502 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 kfree(rdev);
3504 return ERR_PTR(err);
3505}
3506
3507/*
3508 * Check a full RAID array for plausibility
3509 */
3510
NeilBrownf72ffdd2014-09-30 14:23:59 +10003511static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512{
3513 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003514 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 char b[BDEVNAME_SIZE];
3516
3517 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003518 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 switch (super_types[mddev->major_version].
3520 load_super(rdev, freshest, mddev->minor_version)) {
3521 case 1:
3522 freshest = rdev;
3523 break;
3524 case 0:
3525 break;
3526 default:
NeilBrown9d487392016-11-02 14:16:49 +11003527 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003529 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 }
3531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 super_types[mddev->major_version].
3533 validate_super(mddev, freshest);
3534
3535 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003536 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003537 if (mddev->max_disks &&
3538 (rdev->desc_nr >= mddev->max_disks ||
3539 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003540 pr_warn("md: %s: %s: only %d devices permitted\n",
3541 mdname(mddev), bdevname(rdev->bdev, b),
3542 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003543 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003544 continue;
3545 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003546 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 if (super_types[mddev->major_version].
3548 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003549 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003551 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 continue;
3553 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 if (mddev->level == LEVEL_MULTIPATH) {
3556 rdev->desc_nr = i++;
3557 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003558 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003559 } else if (rdev->raid_disk >=
3560 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3561 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003562 rdev->raid_disk = -1;
3563 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 }
3565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
3567
NeilBrown72e02072009-12-14 12:49:55 +11003568/* Read a fixed-point number.
3569 * Numbers in sysfs attributes should be in "standard" units where
3570 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003571 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003572 * milliseconds or jiffies.
3573 * This function takes a decimal number with a possible fractional
3574 * component, and produces an integer which is the result of
3575 * multiplying that number by 10^'scale'.
3576 * all without any floating-point arithmetic.
3577 */
3578int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3579{
3580 unsigned long result = 0;
3581 long decimals = -1;
3582 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3583 if (*cp == '.')
3584 decimals = 0;
3585 else if (decimals < scale) {
3586 unsigned int value;
3587 value = *cp - '0';
3588 result = result * 10 + value;
3589 if (decimals >= 0)
3590 decimals++;
3591 }
3592 cp++;
3593 }
3594 if (*cp == '\n')
3595 cp++;
3596 if (*cp)
3597 return -EINVAL;
3598 if (decimals < 0)
3599 decimals = 0;
3600 while (decimals < scale) {
3601 result *= 10;
3602 decimals ++;
3603 }
3604 *res = result;
3605 return 0;
3606}
3607
NeilBrowneae17012005-11-08 21:39:23 -08003608static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003609safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003610{
3611 int msec = (mddev->safemode_delay*1000)/HZ;
3612 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3613}
3614static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003615safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003616{
NeilBrown16f17b32006-06-26 00:27:37 -07003617 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003618
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003619 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003620 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003621 return -EINVAL;
3622 }
3623
NeilBrown72e02072009-12-14 12:49:55 +11003624 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003625 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003626 if (msec == 0)
3627 mddev->safemode_delay = 0;
3628 else {
NeilBrown19052c02008-08-05 15:54:13 +10003629 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003630 unsigned long new_delay = (msec*HZ)/1000;
3631
3632 if (new_delay == 0)
3633 new_delay = 1;
3634 mddev->safemode_delay = new_delay;
3635 if (new_delay < old_delay || old_delay == 0)
3636 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003637 }
3638 return len;
3639}
3640static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003641__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003642
3643static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003644level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003645{
NeilBrown36d091f2014-12-15 12:56:58 +11003646 struct md_personality *p;
3647 int ret;
3648 spin_lock(&mddev->lock);
3649 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003650 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003651 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003652 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003653 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003654 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003655 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003656 else
NeilBrown36d091f2014-12-15 12:56:58 +11003657 ret = 0;
3658 spin_unlock(&mddev->lock);
3659 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003660}
3661
NeilBrownd9d166c2006-01-06 00:20:51 -08003662static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003663level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003664{
Dan Williamsf2859af2010-05-02 10:04:16 -07003665 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003666 ssize_t rv;
3667 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003668 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003669 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003670 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003671 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003672
NeilBrown67918752014-12-15 12:57:01 +11003673 if (slen == 0 || slen >= sizeof(clevel))
3674 return -EINVAL;
3675
3676 rv = mddev_lock(mddev);
3677 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003678 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003679
3680 if (mddev->pers == NULL) {
3681 strncpy(mddev->clevel, buf, slen);
3682 if (mddev->clevel[slen-1] == '\n')
3683 slen--;
3684 mddev->clevel[slen] = 0;
3685 mddev->level = LEVEL_NONE;
3686 rv = len;
3687 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003688 }
NeilBrown67918752014-12-15 12:57:01 +11003689 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003690 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003691 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003692
3693 /* request to change the personality. Need to ensure:
3694 * - array is not engaged in resync/recovery/reshape
3695 * - old personality can be suspended
3696 * - new personality will access other array.
3697 */
3698
NeilBrown67918752014-12-15 12:57:01 +11003699 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003700 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003701 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003702 mddev->reshape_position != MaxSector ||
3703 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003704 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003705
NeilBrown67918752014-12-15 12:57:01 +11003706 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003707 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003708 pr_warn("md: %s: %s does not support online personality change\n",
3709 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003710 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003711 }
3712
3713 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003714 strncpy(clevel, buf, slen);
3715 if (clevel[slen-1] == '\n')
3716 slen--;
3717 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003718 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003719 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003720
Dan Williamsf2859af2010-05-02 10:04:16 -07003721 if (request_module("md-%s", clevel) != 0)
3722 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003723 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003724 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003725 if (!pers || !try_module_get(pers->owner)) {
3726 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003727 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003728 rv = -EINVAL;
3729 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003730 }
3731 spin_unlock(&pers_lock);
3732
3733 if (pers == mddev->pers) {
3734 /* Nothing to do! */
3735 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003736 rv = len;
3737 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003738 }
3739 if (!pers->takeover) {
3740 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003741 pr_warn("md: %s: %s does not support personality takeover\n",
3742 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003743 rv = -EINVAL;
3744 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003745 }
3746
NeilBrowndafb20f2012-03-19 12:46:39 +11003747 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003748 rdev->new_raid_disk = rdev->raid_disk;
3749
NeilBrown245f46c2009-03-31 14:39:39 +11003750 /* ->takeover must set new_* and/or delta_disks
3751 * if it succeeds, and may set them when it fails.
3752 */
3753 priv = pers->takeover(mddev);
3754 if (IS_ERR(priv)) {
3755 mddev->new_level = mddev->level;
3756 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003757 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003758 mddev->raid_disks -= mddev->delta_disks;
3759 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003760 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003761 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003762 pr_warn("md: %s: %s would not accept array\n",
3763 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003764 rv = PTR_ERR(priv);
3765 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003766 }
3767
3768 /* Looks like we have a winner */
3769 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003770 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003771
3772 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003773 oldpers = mddev->pers;
3774 oldpriv = mddev->private;
3775 mddev->pers = pers;
3776 mddev->private = priv;
3777 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3778 mddev->level = mddev->new_level;
3779 mddev->layout = mddev->new_layout;
3780 mddev->chunk_sectors = mddev->new_chunk_sectors;
3781 mddev->delta_disks = 0;
3782 mddev->reshape_backwards = 0;
3783 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003784 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003785
NeilBrowndb721d32014-12-15 12:56:58 +11003786 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003787 mddev->external) {
3788 /* We are converting from a no-redundancy array
3789 * to a redundancy array and metadata is managed
3790 * externally so we need to be sure that writes
3791 * won't block due to a need to transition
3792 * clean->dirty
3793 * until external management is started.
3794 */
3795 mddev->in_sync = 0;
3796 mddev->safemode_delay = 0;
3797 mddev->safemode = 0;
3798 }
3799
NeilBrowndb721d32014-12-15 12:56:58 +11003800 oldpers->free(mddev, oldpriv);
3801
3802 if (oldpers->sync_request == NULL &&
3803 pers->sync_request != NULL) {
3804 /* need to add the md_redundancy_group */
3805 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11003806 pr_warn("md: cannot register extra attributes for %s\n",
3807 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11003808 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3809 }
3810 if (oldpers->sync_request != NULL &&
3811 pers->sync_request == NULL) {
3812 /* need to remove the md_redundancy_group */
3813 if (mddev->to_remove == NULL)
3814 mddev->to_remove = &md_redundancy_group;
3815 }
3816
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003817 module_put(oldpers->owner);
3818
NeilBrowndafb20f2012-03-19 12:46:39 +11003819 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003820 if (rdev->raid_disk < 0)
3821 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003822 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003823 rdev->new_raid_disk = -1;
3824 if (rdev->new_raid_disk == rdev->raid_disk)
3825 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003826 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003827 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003828 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003829 if (rdev->raid_disk < 0)
3830 continue;
3831 if (rdev->new_raid_disk == rdev->raid_disk)
3832 continue;
3833 rdev->raid_disk = rdev->new_raid_disk;
3834 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003835 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003836 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003837 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11003838 pr_warn("md: cannot register rd%d for %s after level change\n",
3839 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003840 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003841 }
3842
NeilBrowndb721d32014-12-15 12:56:58 +11003843 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003844 /* this is now an array without redundancy, so
3845 * it must always be in_sync
3846 */
3847 mddev->in_sync = 1;
3848 del_timer_sync(&mddev->safemode_timer);
3849 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003850 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003851 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08003852 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003853 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003854 if (!mddev->thread)
3855 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003856 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003857 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003858 rv = len;
3859out_unlock:
3860 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003861 return rv;
3862}
3863
3864static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003865__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003866
NeilBrownd4dbd022006-06-26 00:27:59 -07003867static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003868layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003869{
3870 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003871 if (mddev->reshape_position != MaxSector &&
3872 mddev->layout != mddev->new_layout)
3873 return sprintf(page, "%d (%d)\n",
3874 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003875 return sprintf(page, "%d\n", mddev->layout);
3876}
3877
3878static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003879layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003880{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003881 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003882 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003883
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003884 err = kstrtouint(buf, 10, &n);
3885 if (err < 0)
3886 return err;
NeilBrown67918752014-12-15 12:57:01 +11003887 err = mddev_lock(mddev);
3888 if (err)
3889 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003890
NeilBrownb3546032009-03-31 14:56:41 +11003891 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003892 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003893 err = -EBUSY;
3894 else if (mddev->ro)
3895 err = -EROFS;
3896 else {
3897 mddev->new_layout = n;
3898 err = mddev->pers->check_reshape(mddev);
3899 if (err)
3900 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003901 }
NeilBrownb3546032009-03-31 14:56:41 +11003902 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003903 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003904 if (mddev->reshape_position == MaxSector)
3905 mddev->layout = n;
3906 }
NeilBrown67918752014-12-15 12:57:01 +11003907 mddev_unlock(mddev);
3908 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003909}
3910static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003911__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003912
NeilBrowneae17012005-11-08 21:39:23 -08003913static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003914raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003915{
NeilBrownbb636542005-11-08 21:39:45 -08003916 if (mddev->raid_disks == 0)
3917 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003918 if (mddev->reshape_position != MaxSector &&
3919 mddev->delta_disks != 0)
3920 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3921 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003922 return sprintf(page, "%d\n", mddev->raid_disks);
3923}
3924
NeilBrownfd01b882011-10-11 16:47:53 +11003925static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003926
3927static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003928raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003929{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003930 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003931 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003932
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003933 err = kstrtouint(buf, 10, &n);
3934 if (err < 0)
3935 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003936
NeilBrown67918752014-12-15 12:57:01 +11003937 err = mddev_lock(mddev);
3938 if (err)
3939 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003940 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003941 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003942 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003943 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003944 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003945
NeilBrown67918752014-12-15 12:57:01 +11003946 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003947 rdev_for_each(rdev, mddev) {
3948 if (olddisks < n &&
3949 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003950 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003951 if (olddisks > n &&
3952 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003953 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003954 }
NeilBrown67918752014-12-15 12:57:01 +11003955 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003956 mddev->delta_disks = n - olddisks;
3957 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003958 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003959 } else
NeilBrownda943b992006-01-06 00:20:54 -08003960 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003961out_unlock:
3962 mddev_unlock(mddev);
3963 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003964}
3965static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003966__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003967
NeilBrown24dd4692005-11-08 21:39:26 -08003968static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003969chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003970{
NeilBrown08a02ec2007-05-09 02:35:38 -07003971 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003972 mddev->chunk_sectors != mddev->new_chunk_sectors)
3973 return sprintf(page, "%d (%d)\n",
3974 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003975 mddev->chunk_sectors << 9);
3976 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003977}
3978
3979static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003980chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003981{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003982 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003983 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003984
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003985 err = kstrtoul(buf, 10, &n);
3986 if (err < 0)
3987 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003988
NeilBrown67918752014-12-15 12:57:01 +11003989 err = mddev_lock(mddev);
3990 if (err)
3991 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003992 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003993 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003994 err = -EBUSY;
3995 else if (mddev->ro)
3996 err = -EROFS;
3997 else {
3998 mddev->new_chunk_sectors = n >> 9;
3999 err = mddev->pers->check_reshape(mddev);
4000 if (err)
4001 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10004002 }
NeilBrownb3546032009-03-31 14:56:41 +11004003 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10004004 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004005 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10004006 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004007 }
NeilBrown67918752014-12-15 12:57:01 +11004008 mddev_unlock(mddev);
4009 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08004010}
4011static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004012__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08004013
NeilBrowna94213b2006-06-26 00:28:00 -07004014static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004015resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07004016{
NeilBrownd1a7c502009-03-31 15:24:32 +11004017 if (mddev->recovery_cp == MaxSector)
4018 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07004019 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4020}
4021
4022static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004023resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07004024{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004025 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11004026 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004027
4028 if (cmd_match(buf, "none"))
4029 n = MaxSector;
4030 else {
4031 err = kstrtoull(buf, 10, &n);
4032 if (err < 0)
4033 return err;
4034 if (n != (sector_t)n)
4035 return -EINVAL;
4036 }
NeilBrowna94213b2006-06-26 00:28:00 -07004037
NeilBrown67918752014-12-15 12:57:01 +11004038 err = mddev_lock(mddev);
4039 if (err)
4040 return err;
NeilBrownb0986362011-05-11 15:52:21 +10004041 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11004042 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07004043
NeilBrown67918752014-12-15 12:57:01 +11004044 if (!err) {
4045 mddev->recovery_cp = n;
4046 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08004047 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004048 }
4049 mddev_unlock(mddev);
4050 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07004051}
4052static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10004053__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4054 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07004055
NeilBrown9e653b62006-06-26 00:27:58 -07004056/*
4057 * The array state can be:
4058 *
4059 * clear
4060 * No devices, no size, no level
4061 * Equivalent to STOP_ARRAY ioctl
4062 * inactive
4063 * May have some settings, but array is not active
4064 * all IO results in error
4065 * When written, doesn't tear down array, but just stops it
4066 * suspended (not supported yet)
4067 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01004068 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07004069 * readonly
4070 * no resync can happen. no superblocks get written.
4071 * write requests fail
4072 * read-auto
4073 * like readonly, but behaves like 'clean' on a write request.
4074 *
4075 * clean - no pending writes, but otherwise active.
4076 * When written to inactive array, starts without resync
4077 * If a write request arrives then
4078 * if metadata is known, mark 'dirty' and switch to 'active'.
4079 * if not known, block and switch to write-pending
4080 * If written to an active array that has pending writes, then fails.
4081 * active
4082 * fully active: IO and resync can be happening.
4083 * When written to inactive array, starts with resync
4084 *
4085 * write-pending
4086 * clean, but writes are blocked waiting for 'active' to be written.
4087 *
4088 * active-idle
4089 * like active, but no writes have been seen for a while (100msec).
4090 *
4091 */
4092enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4093 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004094static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004095 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4096 "write-pending", "active-idle", NULL };
4097
4098static int match_word(const char *word, char **list)
4099{
4100 int n;
4101 for (n=0; list[n]; n++)
4102 if (cmd_match(word, list[n]))
4103 break;
4104 return n;
4105}
4106
4107static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004108array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004109{
4110 enum array_state st = inactive;
4111
4112 if (mddev->pers)
4113 switch(mddev->ro) {
4114 case 1:
4115 st = readonly;
4116 break;
4117 case 2:
4118 st = read_auto;
4119 break;
4120 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004121 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004122 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004123 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004124 else if (mddev->in_sync)
4125 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004126 else if (mddev->safemode)
4127 st = active_idle;
4128 else
4129 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004130 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004131 }
4132 else {
4133 if (list_empty(&mddev->disks) &&
4134 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004135 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004136 st = clear;
4137 else
4138 st = inactive;
4139 }
4140 return sprintf(page, "%s\n", array_states[st]);
4141}
4142
NeilBrownf72ffdd2014-09-30 14:23:59 +10004143static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4144static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4145static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004146static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004147
4148static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004149array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004150{
NeilBrown6497709b2017-03-15 14:05:14 +11004151 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004152 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004153
4154 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4155 /* don't take reconfig_mutex when toggling between
4156 * clean and active
4157 */
4158 spin_lock(&mddev->lock);
4159 if (st == active) {
4160 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004161 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004162 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004163 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004164 } else /* st == clean */ {
4165 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004166 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004167 err = -EBUSY;
4168 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004169 if (!err)
4170 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004171 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004172 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004173 }
4174 err = mddev_lock(mddev);
4175 if (err)
4176 return err;
4177 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004178 switch(st) {
4179 case bad_word:
4180 break;
4181 case clear:
4182 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004183 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004184 break;
4185 case inactive:
4186 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004187 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004188 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004189 else
NeilBrowne6910632008-02-06 01:39:51 -08004190 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004191 break;
4192 case suspended:
4193 break; /* not supported yet */
4194 case readonly:
4195 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004196 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004197 else {
4198 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004199 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004200 err = do_md_run(mddev);
4201 }
4202 break;
4203 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004204 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004205 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004206 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004207 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004208 err = restart_array(mddev);
4209 if (err == 0) {
4210 mddev->ro = 2;
4211 set_disk_ro(mddev->gendisk, 0);
4212 }
NeilBrown9e653b62006-06-26 00:27:58 -07004213 } else {
4214 mddev->ro = 2;
4215 err = do_md_run(mddev);
4216 }
4217 break;
4218 case clean:
4219 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004220 err = restart_array(mddev);
4221 if (err)
4222 break;
NeilBrown85572d72014-12-15 12:56:56 +11004223 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004224 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004225 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004226 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004227 } else
4228 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004229 break;
4230 case active:
4231 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004232 err = restart_array(mddev);
4233 if (err)
4234 break;
Shaohua Li29530792016-12-08 15:48:19 -08004235 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004236 wake_up(&mddev->sb_wait);
4237 err = 0;
4238 } else {
4239 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004240 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004241 err = do_md_run(mddev);
4242 }
4243 break;
4244 case write_pending:
4245 case active_idle:
4246 /* these cannot be set */
4247 break;
4248 }
NeilBrown67918752014-12-15 12:57:01 +11004249
4250 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004251 if (mddev->hold_active == UNTIL_IOCTL)
4252 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004253 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004254 }
NeilBrown67918752014-12-15 12:57:01 +11004255 mddev_unlock(mddev);
4256 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004257}
NeilBrown80ca3a42006-07-10 04:44:18 -07004258static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004259__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004260
NeilBrown6d7ff7382006-01-06 00:21:16 -08004261static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004262max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004263 return sprintf(page, "%d\n",
4264 atomic_read(&mddev->max_corr_read_errors));
4265}
4266
4267static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004268max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004269{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004270 unsigned int n;
4271 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004272
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004273 rv = kstrtouint(buf, 10, &n);
4274 if (rv < 0)
4275 return rv;
4276 atomic_set(&mddev->max_corr_read_errors, n);
4277 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004278}
4279
4280static struct md_sysfs_entry max_corr_read_errors =
4281__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4282 max_corrected_read_errors_store);
4283
4284static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004285null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004286{
4287 return -EINVAL;
4288}
4289
4290static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004291new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004292{
4293 /* buf must be %d:%d\n? giving major and minor numbers */
4294 /* The new device is added to the array.
4295 * If the array has a persistent superblock, we read the
4296 * superblock to initialise info and check validity.
4297 * Otherwise, only checking done is that in bind_rdev_to_array,
4298 * which mainly checks size.
4299 */
4300 char *e;
4301 int major = simple_strtoul(buf, &e, 10);
4302 int minor;
4303 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004304 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004305 int err;
4306
4307 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4308 return -EINVAL;
4309 minor = simple_strtoul(e+1, &e, 10);
4310 if (*e && *e != '\n')
4311 return -EINVAL;
4312 dev = MKDEV(major, minor);
4313 if (major != MAJOR(dev) ||
4314 minor != MINOR(dev))
4315 return -EOVERFLOW;
4316
NeilBrown67918752014-12-15 12:57:01 +11004317 flush_workqueue(md_misc_wq);
4318
4319 err = mddev_lock(mddev);
4320 if (err)
4321 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004322 if (mddev->persistent) {
4323 rdev = md_import_device(dev, mddev->major_version,
4324 mddev->minor_version);
4325 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004326 struct md_rdev *rdev0
4327 = list_entry(mddev->disks.next,
4328 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004329 err = super_types[mddev->major_version]
4330 .load_super(rdev, rdev0, mddev->minor_version);
4331 if (err < 0)
4332 goto out;
4333 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004334 } else if (mddev->external)
4335 rdev = md_import_device(dev, -2, -1);
4336 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004337 rdev = md_import_device(dev, -1, -1);
4338
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004339 if (IS_ERR(rdev)) {
4340 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004341 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004342 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004343 err = bind_rdev_to_array(rdev, mddev);
4344 out:
4345 if (err)
4346 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004347 mddev_unlock(mddev);
Alexey Obitotskiy5492c462017-07-28 15:49:25 +02004348 if (!err)
4349 md_new_event(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004350 return err ? err : len;
4351}
4352
4353static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004354__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004355
4356static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004357bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004358{
4359 char *end;
4360 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004361 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004362
NeilBrown67918752014-12-15 12:57:01 +11004363 err = mddev_lock(mddev);
4364 if (err)
4365 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004366 if (!mddev->bitmap)
4367 goto out;
4368 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4369 while (*buf) {
4370 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4371 if (buf == end) break;
4372 if (*end == '-') { /* range */
4373 buf = end + 1;
4374 end_chunk = simple_strtoul(buf, &end, 0);
4375 if (buf == end) break;
4376 }
4377 if (*end && !isspace(*end)) break;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004378 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004379 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004380 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004381 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
Paul Clements9b1d1da2006-10-03 01:15:49 -07004382out:
NeilBrown67918752014-12-15 12:57:01 +11004383 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004384 return len;
4385}
4386
4387static struct md_sysfs_entry md_bitmap =
4388__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4389
4390static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004391size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004392{
Andre Noll58c0fed2009-03-31 14:33:13 +11004393 return sprintf(page, "%llu\n",
4394 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004395}
4396
NeilBrownfd01b882011-10-11 16:47:53 +11004397static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004398
4399static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004400size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004401{
4402 /* If array is inactive, we can reduce the component size, but
4403 * not increase it (except from 0).
4404 * If array is active, we can try an on-line resize
4405 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004406 sector_t sectors;
4407 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004408
Andre Noll58c0fed2009-03-31 14:33:13 +11004409 if (err < 0)
4410 return err;
NeilBrown67918752014-12-15 12:57:01 +11004411 err = mddev_lock(mddev);
4412 if (err)
4413 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004414 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004415 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004416 if (err == 0)
4417 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004418 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004419 if (mddev->dev_sectors == 0 ||
4420 mddev->dev_sectors > sectors)
4421 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004422 else
4423 err = -ENOSPC;
4424 }
NeilBrown67918752014-12-15 12:57:01 +11004425 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004426 return err ? err : len;
4427}
4428
4429static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004430__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004431
Masanari Iida83f0d772012-10-30 00:18:08 +09004432/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004433 * This is one of
4434 * 'none' for arrays with no metadata (good luck...)
4435 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004436 * or N.M for internally known formats
4437 */
4438static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004439metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004440{
4441 if (mddev->persistent)
4442 return sprintf(page, "%d.%d\n",
4443 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004444 else if (mddev->external)
4445 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004446 else
4447 return sprintf(page, "none\n");
4448}
4449
4450static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004451metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004452{
4453 int major, minor;
4454 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004455 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004456 /* Changing the details of 'external' metadata is
4457 * always permitted. Otherwise there must be
4458 * no devices attached to the array.
4459 */
NeilBrown67918752014-12-15 12:57:01 +11004460
4461 err = mddev_lock(mddev);
4462 if (err)
4463 return err;
4464 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004465 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4466 ;
4467 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004468 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004469
NeilBrown67918752014-12-15 12:57:01 +11004470 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004471 if (cmd_match(buf, "none")) {
4472 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004473 mddev->external = 0;
4474 mddev->major_version = 0;
4475 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004476 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004477 }
4478 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004479 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004480 if (namelen >= sizeof(mddev->metadata_type))
4481 namelen = sizeof(mddev->metadata_type)-1;
4482 strncpy(mddev->metadata_type, buf+9, namelen);
4483 mddev->metadata_type[namelen] = 0;
4484 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4485 mddev->metadata_type[--namelen] = 0;
4486 mddev->persistent = 0;
4487 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004488 mddev->major_version = 0;
4489 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004490 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004491 }
4492 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004493 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004494 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004495 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004496 buf = e+1;
4497 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004498 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004499 goto out_unlock;
4500 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004501 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004502 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004503 mddev->major_version = major;
4504 mddev->minor_version = minor;
4505 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004506 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004507 err = 0;
4508out_unlock:
4509 mddev_unlock(mddev);
4510 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004511}
4512
4513static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004514__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004515
NeilBrowna35b0d62006-01-06 00:20:49 -08004516static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004517action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004518{
NeilBrown7eec3142005-11-08 21:39:44 -08004519 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004520 unsigned long recovery = mddev->recovery;
4521 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004522 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004523 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4524 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4525 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004526 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004527 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4528 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004529 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004530 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004531 type = "check";
4532 else
4533 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004534 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004535 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004536 else if (mddev->reshape_position != MaxSector)
4537 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004538 }
4539 return sprintf(page, "%s\n", type);
4540}
4541
4542static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004543action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004544{
NeilBrown7eec3142005-11-08 21:39:44 -08004545 if (!mddev->pers || !mddev->pers->sync_request)
4546 return -EINVAL;
4547
NeilBrownb6a9ce62009-05-26 09:41:17 +10004548
4549 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004550 if (cmd_match(page, "frozen"))
4551 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4552 else
4553 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004554 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4555 mddev_lock(mddev) == 0) {
4556 flush_workqueue(md_misc_wq);
4557 if (mddev->sync_thread) {
4558 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004559 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004560 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004561 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004562 }
NeilBrown312045e2015-12-21 11:01:21 +11004563 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004564 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004565 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004566 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004567 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004568 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004569 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004570 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004571 int err;
4572 if (mddev->pers->start_reshape == NULL)
4573 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004574 err = mddev_lock(mddev);
4575 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004576 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4577 err = -EBUSY;
4578 else {
4579 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4580 err = mddev->pers->start_reshape(mddev);
4581 }
NeilBrown67918752014-12-15 12:57:01 +11004582 mddev_unlock(mddev);
4583 }
NeilBrown16484bf2006-03-27 01:18:13 -08004584 if (err)
4585 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004586 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004587 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004588 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004589 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004590 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004591 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004592 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004593 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4594 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004595 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004596 if (mddev->ro == 2) {
4597 /* A write to sync_action is enough to justify
4598 * canceling read-auto mode
4599 */
4600 mddev->ro = 0;
4601 md_wakeup_thread(mddev->sync_thread);
4602 }
NeilBrown03c902e2006-01-06 00:20:46 -08004603 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004604 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004605 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004606 return len;
4607}
4608
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004609static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004610__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004611
4612static ssize_t
4613last_sync_action_show(struct mddev *mddev, char *page)
4614{
4615 return sprintf(page, "%s\n", mddev->last_sync_action);
4616}
4617
4618static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4619
NeilBrown9d888832005-11-08 21:39:26 -08004620static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004621mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004622{
4623 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004624 (unsigned long long)
4625 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004626}
4627
NeilBrown80ca3a42006-07-10 04:44:18 -07004628static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004629
NeilBrown88202a02006-01-06 00:21:36 -08004630static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004631sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004632{
4633 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4634 mddev->sync_speed_min ? "local": "system");
4635}
4636
4637static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004638sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004639{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004640 unsigned int min;
4641 int rv;
4642
NeilBrown88202a02006-01-06 00:21:36 -08004643 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004644 min = 0;
4645 } else {
4646 rv = kstrtouint(buf, 10, &min);
4647 if (rv < 0)
4648 return rv;
4649 if (min == 0)
4650 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004651 }
NeilBrown88202a02006-01-06 00:21:36 -08004652 mddev->sync_speed_min = min;
4653 return len;
4654}
4655
4656static struct md_sysfs_entry md_sync_min =
4657__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4658
4659static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004660sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004661{
4662 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4663 mddev->sync_speed_max ? "local": "system");
4664}
4665
4666static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004667sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004668{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004669 unsigned int max;
4670 int rv;
4671
NeilBrown88202a02006-01-06 00:21:36 -08004672 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004673 max = 0;
4674 } else {
4675 rv = kstrtouint(buf, 10, &max);
4676 if (rv < 0)
4677 return rv;
4678 if (max == 0)
4679 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004680 }
NeilBrown88202a02006-01-06 00:21:36 -08004681 mddev->sync_speed_max = max;
4682 return len;
4683}
4684
4685static struct md_sysfs_entry md_sync_max =
4686__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4687
Iustin Popd7f3d292007-10-16 23:30:54 -07004688static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004689degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004690{
4691 return sprintf(page, "%d\n", mddev->degraded);
4692}
4693static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004694
4695static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004696sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004697{
4698 return sprintf(page, "%d\n", mddev->parallel_resync);
4699}
4700
4701static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004702sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004703{
4704 long n;
4705
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004706 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004707 return -EINVAL;
4708
4709 if (n != 0 && n != 1)
4710 return -EINVAL;
4711
4712 mddev->parallel_resync = n;
4713
4714 if (mddev->sync_thread)
4715 wake_up(&resync_wait);
4716
4717 return len;
4718}
4719
4720/* force parallel resync, even with shared block devices */
4721static struct md_sysfs_entry md_sync_force_parallel =
4722__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4723 sync_force_parallel_show, sync_force_parallel_store);
4724
4725static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004726sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004727{
4728 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004729 if (mddev->curr_resync == 0)
4730 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004731 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4732 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004733 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004734 db = resync - mddev->resync_mark_cnt;
4735 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004736}
4737
NeilBrown80ca3a42006-07-10 04:44:18 -07004738static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004739
4740static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004741sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004742{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004743 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004744
NeilBrownacb180b2009-04-14 16:28:34 +10004745 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4746 return sprintf(page, "none\n");
4747
NeilBrown72f36d52012-10-11 14:25:57 +11004748 if (mddev->curr_resync == 1 ||
4749 mddev->curr_resync == 2)
4750 return sprintf(page, "delayed\n");
4751
NeilBrownc804cde2012-05-21 09:28:33 +10004752 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4753 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004754 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004755 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004756 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004757
NeilBrownacb180b2009-04-14 16:28:34 +10004758 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004759 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004760}
4761
NeilBrown750f1992014-09-30 08:53:05 +10004762static struct md_sysfs_entry md_sync_completed =
4763 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004764
NeilBrowne464eaf2006-03-27 01:18:14 -08004765static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004766min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004767{
4768 return sprintf(page, "%llu\n",
4769 (unsigned long long)mddev->resync_min);
4770}
4771static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004772min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004773{
4774 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004775 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004776
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004777 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004778 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004779
4780 spin_lock(&mddev->lock);
4781 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004782 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004783 goto out_unlock;
4784
4785 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004786 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004787 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004788
NeilBrown50c37b12015-03-23 17:36:38 +11004789 /* Round down to multiple of 4K for safety */
4790 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004791 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004792
NeilBrown23da4222014-12-15 12:57:01 +11004793out_unlock:
4794 spin_unlock(&mddev->lock);
4795 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004796}
4797
4798static struct md_sysfs_entry md_min_sync =
4799__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4800
4801static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004802max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004803{
4804 if (mddev->resync_max == MaxSector)
4805 return sprintf(page, "max\n");
4806 else
4807 return sprintf(page, "%llu\n",
4808 (unsigned long long)mddev->resync_max);
4809}
4810static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004811max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004812{
NeilBrown23da4222014-12-15 12:57:01 +11004813 int err;
4814 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004815 if (strncmp(buf, "max", 3) == 0)
4816 mddev->resync_max = MaxSector;
4817 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004818 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004819 int chunk;
4820
4821 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004822 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004823 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004824 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004825 goto out_unlock;
4826
4827 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004828 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004829 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004830 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004831 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004832
4833 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004834 chunk = mddev->chunk_sectors;
4835 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004836 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004837
4838 err = -EINVAL;
4839 if (sector_div(temp, chunk))
4840 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004841 }
4842 mddev->resync_max = max;
4843 }
4844 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004845 err = 0;
4846out_unlock:
4847 spin_unlock(&mddev->lock);
4848 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004849}
4850
4851static struct md_sysfs_entry md_max_sync =
4852__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4853
4854static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004855suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004856{
4857 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4858}
4859
4860static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004861suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004862{
NeilBrownb03e0cc2017-10-19 12:49:15 +11004863 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004864 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004865
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004866 err = kstrtoull(buf, 10, &new);
4867 if (err < 0)
4868 return err;
4869 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004870 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004871
NeilBrown67918752014-12-15 12:57:01 +11004872 err = mddev_lock(mddev);
4873 if (err)
4874 return err;
4875 err = -EINVAL;
4876 if (mddev->pers == NULL ||
4877 mddev->pers->quiesce == NULL)
4878 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004879 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11004880 mddev->suspend_lo = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004881 mddev_resume(mddev);
4882
NeilBrown67918752014-12-15 12:57:01 +11004883 err = 0;
4884unlock:
4885 mddev_unlock(mddev);
4886 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004887}
4888static struct md_sysfs_entry md_suspend_lo =
4889__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4890
NeilBrowne464eaf2006-03-27 01:18:14 -08004891static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004892suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004893{
4894 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4895}
4896
4897static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004898suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004899{
NeilBrownb03e0cc2017-10-19 12:49:15 +11004900 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004901 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004902
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004903 err = kstrtoull(buf, 10, &new);
4904 if (err < 0)
4905 return err;
4906 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004907 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004908
NeilBrown67918752014-12-15 12:57:01 +11004909 err = mddev_lock(mddev);
4910 if (err)
4911 return err;
4912 err = -EINVAL;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004913 if (mddev->pers == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004914 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004915
4916 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11004917 mddev->suspend_hi = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004918 mddev_resume(mddev);
4919
NeilBrown67918752014-12-15 12:57:01 +11004920 err = 0;
4921unlock:
4922 mddev_unlock(mddev);
4923 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004924}
4925static struct md_sysfs_entry md_suspend_hi =
4926__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4927
NeilBrown08a02ec2007-05-09 02:35:38 -07004928static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004929reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004930{
4931 if (mddev->reshape_position != MaxSector)
4932 return sprintf(page, "%llu\n",
4933 (unsigned long long)mddev->reshape_position);
4934 strcpy(page, "none\n");
4935 return 5;
4936}
4937
4938static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004939reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004940{
NeilBrownc6563a82012-05-21 09:27:00 +10004941 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004942 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004943 int err;
NeilBrown67918752014-12-15 12:57:01 +11004944
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004945 err = kstrtoull(buf, 10, &new);
4946 if (err < 0)
4947 return err;
4948 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004949 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004950 err = mddev_lock(mddev);
4951 if (err)
4952 return err;
4953 err = -EBUSY;
4954 if (mddev->pers)
4955 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004956 mddev->reshape_position = new;
4957 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004958 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004959 mddev->new_level = mddev->level;
4960 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004961 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004962 rdev_for_each(rdev, mddev)
4963 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004964 err = 0;
4965unlock:
4966 mddev_unlock(mddev);
4967 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004968}
4969
4970static struct md_sysfs_entry md_reshape_position =
4971__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4972 reshape_position_store);
4973
Dan Williamsb522adc2009-03-31 15:00:31 +11004974static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004975reshape_direction_show(struct mddev *mddev, char *page)
4976{
4977 return sprintf(page, "%s\n",
4978 mddev->reshape_backwards ? "backwards" : "forwards");
4979}
4980
4981static ssize_t
4982reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4983{
4984 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004985 int err;
4986
NeilBrown2c810cd2012-05-21 09:27:00 +10004987 if (cmd_match(buf, "forwards"))
4988 backwards = 0;
4989 else if (cmd_match(buf, "backwards"))
4990 backwards = 1;
4991 else
4992 return -EINVAL;
4993 if (mddev->reshape_backwards == backwards)
4994 return len;
4995
NeilBrown67918752014-12-15 12:57:01 +11004996 err = mddev_lock(mddev);
4997 if (err)
4998 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004999 /* check if we are allowed to change */
5000 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11005001 err = -EBUSY;
5002 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10005003 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11005004 err = -EINVAL;
5005 else
5006 mddev->reshape_backwards = backwards;
5007 mddev_unlock(mddev);
5008 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10005009}
5010
5011static struct md_sysfs_entry md_reshape_direction =
5012__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5013 reshape_direction_store);
5014
5015static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005016array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11005017{
5018 if (mddev->external_size)
5019 return sprintf(page, "%llu\n",
5020 (unsigned long long)mddev->array_sectors/2);
5021 else
5022 return sprintf(page, "default\n");
5023}
5024
5025static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005026array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11005027{
5028 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11005029 int err;
5030
5031 err = mddev_lock(mddev);
5032 if (err)
5033 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11005034
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005035 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08005036 if (mddev_is_clustered(mddev)) {
5037 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005038 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08005039 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005040
Dan Williamsb522adc2009-03-31 15:00:31 +11005041 if (strncmp(buf, "default", 7) == 0) {
5042 if (mddev->pers)
5043 sectors = mddev->pers->size(mddev, 0, 0);
5044 else
5045 sectors = mddev->array_sectors;
5046
5047 mddev->external_size = 0;
5048 } else {
5049 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11005050 err = -EINVAL;
5051 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5052 err = -E2BIG;
5053 else
5054 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11005055 }
5056
NeilBrown67918752014-12-15 12:57:01 +11005057 if (!err) {
5058 mddev->array_sectors = sectors;
5059 if (mddev->pers) {
5060 set_capacity(mddev->gendisk, mddev->array_sectors);
5061 revalidate_disk(mddev->gendisk);
5062 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11005063 }
NeilBrown67918752014-12-15 12:57:01 +11005064 mddev_unlock(mddev);
5065 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11005066}
5067
5068static struct md_sysfs_entry md_array_size =
5069__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5070 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005071
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005072static ssize_t
5073consistency_policy_show(struct mddev *mddev, char *page)
5074{
5075 int ret;
5076
5077 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5078 ret = sprintf(page, "journal\n");
5079 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5080 ret = sprintf(page, "ppl\n");
5081 } else if (mddev->bitmap) {
5082 ret = sprintf(page, "bitmap\n");
5083 } else if (mddev->pers) {
5084 if (mddev->pers->sync_request)
5085 ret = sprintf(page, "resync\n");
5086 else
5087 ret = sprintf(page, "none\n");
5088 } else {
5089 ret = sprintf(page, "unknown\n");
5090 }
5091
5092 return ret;
5093}
5094
5095static ssize_t
5096consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5097{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005098 int err = 0;
5099
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005100 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005101 if (mddev->pers->change_consistency_policy)
5102 err = mddev->pers->change_consistency_policy(mddev, buf);
5103 else
5104 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005105 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5106 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005107 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005108 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005109 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005110
5111 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005112}
5113
5114static struct md_sysfs_entry md_consistency_policy =
5115__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5116 consistency_policy_store);
5117
NeilBrowneae17012005-11-08 21:39:23 -08005118static struct attribute *md_default_attrs[] = {
5119 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005120 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005121 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005122 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005123 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005124 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005125 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005126 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005127 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005128 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005129 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005130 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005131 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005132 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005133 &md_consistency_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005134 NULL,
5135};
5136
5137static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005138 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005139 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005140 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005141 &md_sync_min.attr,
5142 &md_sync_max.attr,
5143 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005144 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005145 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005146 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005147 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005148 &md_suspend_lo.attr,
5149 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005150 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005151 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005152 NULL,
5153};
NeilBrown411036f2005-11-08 21:39:40 -08005154static struct attribute_group md_redundancy_group = {
5155 .name = NULL,
5156 .attrs = md_redundancy_attrs,
5157};
5158
NeilBrowneae17012005-11-08 21:39:23 -08005159static ssize_t
5160md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5161{
5162 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005163 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005164 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005165
5166 if (!entry->show)
5167 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005168 spin_lock(&all_mddevs_lock);
5169 if (list_empty(&mddev->all_mddevs)) {
5170 spin_unlock(&all_mddevs_lock);
5171 return -EBUSY;
5172 }
5173 mddev_get(mddev);
5174 spin_unlock(&all_mddevs_lock);
5175
NeilBrownb7b17c92014-12-15 12:56:59 +11005176 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005177 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005178 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005179}
5180
5181static ssize_t
5182md_attr_store(struct kobject *kobj, struct attribute *attr,
5183 const char *page, size_t length)
5184{
5185 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005186 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005187 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005188
5189 if (!entry->store)
5190 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005191 if (!capable(CAP_SYS_ADMIN))
5192 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005193 spin_lock(&all_mddevs_lock);
5194 if (list_empty(&mddev->all_mddevs)) {
5195 spin_unlock(&all_mddevs_lock);
5196 return -EBUSY;
5197 }
5198 mddev_get(mddev);
5199 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005200 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005201 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005202 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005203}
5204
5205static void md_free(struct kobject *ko)
5206{
NeilBrownfd01b882011-10-11 16:47:53 +11005207 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005208
5209 if (mddev->sysfs_state)
5210 sysfs_put(mddev->sysfs_state);
5211
Bart Van Assched8115c352018-02-28 10:15:29 -08005212 if (mddev->gendisk)
5213 del_gendisk(mddev->gendisk);
NeilBrown6cd18e72015-04-27 14:12:22 +10005214 if (mddev->queue)
5215 blk_cleanup_queue(mddev->queue);
Bart Van Assched8115c352018-02-28 10:15:29 -08005216 if (mddev->gendisk)
NeilBrowna21d1502009-01-09 08:31:09 +11005217 put_disk(mddev->gendisk);
NeilBrown4ad23a972017-03-15 14:05:14 +11005218 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005219
Kent Overstreet28dec872018-06-07 20:52:54 -04005220 bioset_exit(&mddev->bio_set);
5221 bioset_exit(&mddev->sync_set);
NeilBrowneae17012005-11-08 21:39:23 -08005222 kfree(mddev);
5223}
5224
Emese Revfy52cf25d2010-01-19 02:58:23 +01005225static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005226 .show = md_attr_show,
5227 .store = md_attr_store,
5228};
5229static struct kobj_type md_ktype = {
5230 .release = md_free,
5231 .sysfs_ops = &md_sysfs_ops,
5232 .default_attrs = md_default_attrs,
5233};
5234
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235int mdp_major = 0;
5236
Dan Williams5fd3a172009-03-04 00:57:25 -07005237static void mddev_delayed_delete(struct work_struct *ws)
5238{
NeilBrownfd01b882011-10-11 16:47:53 +11005239 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005240
NeilBrown43a70502009-12-14 12:49:55 +11005241 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005242 kobject_del(&mddev->kobj);
5243 kobject_put(&mddev->kobj);
5244}
5245
NeilBrown4ad23a972017-03-15 14:05:14 +11005246static void no_op(struct percpu_ref *r) {}
5247
NeilBrowna415c0f2017-06-05 16:05:13 +10005248int mddev_init_writes_pending(struct mddev *mddev)
5249{
5250 if (mddev->writes_pending.percpu_count_ptr)
5251 return 0;
5252 if (percpu_ref_init(&mddev->writes_pending, no_op, 0, GFP_KERNEL) < 0)
5253 return -ENOMEM;
5254 /* We want to start with the refcount at zero */
5255 percpu_ref_put(&mddev->writes_pending);
5256 return 0;
5257}
5258EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5259
NeilBrownefeb53c2009-01-09 08:31:10 +11005260static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261{
NeilBrown039b7222017-04-12 16:26:13 +10005262 /*
5263 * If dev is zero, name is the name of a device to allocate with
5264 * an arbitrary minor number. It will be "md_???"
5265 * If dev is non-zero it must be a device number with a MAJOR of
5266 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5267 * the device is being created by opening a node in /dev.
5268 * If "name" is not NULL, the device is being created by
5269 * writing to /sys/module/md_mod/parameters/new_array.
5270 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005271 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005272 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005274 int partitioned;
5275 int shift;
5276 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005277 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278
5279 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005280 return -ENODEV;
5281
5282 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5283 shift = partitioned ? MdpMinorShift : 0;
5284 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285
Tejun Heoe804ac72010-10-15 15:36:08 +02005286 /* wait for any previous instance of this device to be
5287 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005288 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005289 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005290
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005291 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005292 error = -EEXIST;
5293 if (mddev->gendisk)
5294 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005295
NeilBrown039b7222017-04-12 16:26:13 +10005296 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005297 /* Need to ensure that 'name' is not a duplicate.
5298 */
NeilBrownfd01b882011-10-11 16:47:53 +11005299 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005300 spin_lock(&all_mddevs_lock);
5301
5302 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5303 if (mddev2->gendisk &&
5304 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5305 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005306 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005307 }
5308 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 }
NeilBrown039b7222017-04-12 16:26:13 +10005310 if (name && dev)
5311 /*
5312 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5313 */
5314 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005315
NeilBrown0909dc42009-07-01 12:27:21 +10005316 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005317 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005318 if (!mddev->queue)
5319 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005320 mddev->queue->queuedata = mddev;
5321
NeilBrown409c57f2009-03-31 14:39:39 +11005322 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005323 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005324
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 disk = alloc_disk(1 << shift);
5326 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005327 blk_cleanup_queue(mddev->queue);
5328 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005329 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005331 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005333 if (name)
5334 strcpy(disk->disk_name, name);
5335 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005337 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 disk->fops = &md_fops;
5340 disk->private_data = mddev;
5341 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005342 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005343 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005344 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005345 * remove it now.
5346 */
5347 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005349 /* As soon as we call add_disk(), another thread could get
5350 * through to md_open, so make sure it doesn't get too far
5351 */
5352 mutex_lock(&mddev->open_mutex);
5353 add_disk(disk);
5354
Kent Overstreet28dec872018-06-07 20:52:54 -04005355 error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005356 if (error) {
5357 /* This isn't possible, but as kobject_init_and_add is marked
5358 * __must_check, we must do something with the result
5359 */
NeilBrown9d487392016-11-02 14:16:49 +11005360 pr_debug("md: cannot register %s/md - name in use\n",
5361 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005362 error = 0;
5363 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005364 if (mddev->kobj.sd &&
5365 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005366 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005367 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005368 abort:
5369 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005370 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005371 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005372 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005373 }
NeilBrownd3374822009-01-09 08:31:10 +11005374 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005375 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005376}
5377
5378static struct kobject *md_probe(dev_t dev, int *part, void *data)
5379{
NeilBrown78b63502017-04-12 16:26:13 +10005380 if (create_on_open)
5381 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 return NULL;
5383}
5384
Kees Cooke4dca7b2017-10-17 19:04:42 -07005385static int add_named_array(const char *val, const struct kernel_param *kp)
NeilBrownefeb53c2009-01-09 08:31:10 +11005386{
NeilBrown039b7222017-04-12 16:26:13 +10005387 /*
5388 * val must be "md_*" or "mdNNN".
5389 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005390 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005391 * For "mdNNN" we allocate an array with the minor number NNN
5392 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005393 */
5394 int len = strlen(val);
5395 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005396 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005397
5398 while (len && val[len-1] == '\n')
5399 len--;
5400 if (len >= DISK_NAME_LEN)
5401 return -E2BIG;
5402 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005403 if (strncmp(buf, "md_", 3) == 0)
5404 return md_alloc(0, buf);
5405 if (strncmp(buf, "md", 2) == 0 &&
5406 isdigit(buf[2]) &&
5407 kstrtoul(buf+2, 10, &devnum) == 0 &&
5408 devnum <= MINORMASK)
5409 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5410
5411 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005412}
5413
Kees Cook8376d3c2017-10-16 17:01:48 -07005414static void md_safemode_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415{
Kees Cook8376d3c2017-10-16 17:01:48 -07005416 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
NeilBrown4ad23a972017-03-15 14:05:14 +11005418 mddev->safemode = 1;
5419 if (mddev->external)
5420 sysfs_notify_dirent_safe(mddev->sysfs_state);
5421
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 md_wakeup_thread(mddev->thread);
5423}
5424
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005425static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
NeilBrownfd01b882011-10-11 16:47:53 +11005427int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428{
NeilBrown2604b702006-01-06 00:20:36 -08005429 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005430 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005431 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
NeilBrowna757e642005-04-16 15:26:42 -07005433 if (list_empty(&mddev->disks))
5434 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
5437 if (mddev->pers)
5438 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005439 /* Cannot run until previous stop completes properly */
5440 if (mddev->sysfs_active)
5441 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005442
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 /*
5444 * Analyze all RAID superblock(s)
5445 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005446 if (!mddev->raid_disks) {
5447 if (!mddev->persistent)
5448 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005449 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451
NeilBrownd9d166c2006-01-06 00:20:51 -08005452 if (mddev->level != LEVEL_NONE)
5453 request_module("md-level-%d", mddev->level);
5454 else if (mddev->clevel[0])
5455 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456
5457 /*
5458 * Drop all container device buffers, from now on
5459 * the only valid external interface is through the md
5460 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 */
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005462 mddev->has_superblocks = false;
NeilBrowndafb20f2012-03-19 12:46:39 +11005463 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005464 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 continue;
5466 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005467 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005468 if (mddev->ro != 1 &&
5469 (bdev_read_only(rdev->bdev) ||
5470 bdev_read_only(rdev->meta_bdev))) {
5471 mddev->ro = 1;
5472 if (mddev->gendisk)
5473 set_disk_ro(mddev->gendisk, 1);
5474 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005475
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005476 if (rdev->sb_page)
5477 mddev->has_superblocks = true;
5478
NeilBrownf0d76d72007-07-17 04:06:12 -07005479 /* perform some consistency tests on the device.
5480 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005481 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005482 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005483 if (rdev->meta_bdev) {
5484 /* Nothing to check */;
5485 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005486 if (mddev->dev_sectors &&
5487 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005488 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005489 pr_warn("md: %s: data overlaps metadata\n",
5490 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005491 return -EINVAL;
5492 }
5493 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005494 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005495 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005496 pr_warn("md: %s: metadata overlaps data\n",
5497 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005498 return -EINVAL;
5499 }
5500 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005501 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 }
5503
Kent Overstreetafeee512018-05-20 18:25:52 -04005504 if (!bioset_initialized(&mddev->bio_set)) {
5505 err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5506 if (err)
5507 return err;
Ming Lei10273172017-02-14 23:29:00 +08005508 }
Kent Overstreetafeee512018-05-20 18:25:52 -04005509 if (!bioset_initialized(&mddev->sync_set)) {
5510 err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5511 if (err)
Kent Overstreet28dec872018-06-07 20:52:54 -04005512 return err;
NeilBrown5a850712017-06-21 09:12:21 +10005513 }
Xiao Ni5a409b42018-05-21 11:49:54 +08005514 if (mddev->flush_pool == NULL) {
5515 mddev->flush_pool = mempool_create(NR_FLUSH_INFOS, flush_info_alloc,
5516 flush_info_free, mddev);
5517 if (!mddev->flush_pool) {
5518 err = -ENOMEM;
5519 goto abort;
5520 }
5521 }
5522 if (mddev->flush_bio_pool == NULL) {
5523 mddev->flush_bio_pool = mempool_create(NR_FLUSH_BIOS, flush_bio_alloc,
5524 flush_bio_free, mddev);
5525 if (!mddev->flush_bio_pool) {
5526 err = -ENOMEM;
5527 goto abort;
5528 }
5529 }
NeilBrowna167f662010-10-26 18:31:13 +11005530
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005532 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005533 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005535 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005536 pr_warn("md: personality for level %d is not loaded!\n",
5537 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005538 else
NeilBrown9d487392016-11-02 14:16:49 +11005539 pr_warn("md: personality for level %s is not loaded!\n",
5540 mddev->clevel);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005541 err = -EINVAL;
5542 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005545 if (mddev->level != pers->level) {
5546 mddev->level = pers->level;
5547 mddev->new_level = pers->level;
5548 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005549 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
NeilBrownf6705572006-03-27 01:18:11 -08005551 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005552 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005553 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005554 module_put(pers->owner);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005555 err = -EINVAL;
5556 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08005557 }
5558
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005559 if (pers->sync_request) {
5560 /* Warn if this is a potentially silly
5561 * configuration.
5562 */
5563 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005564 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005565 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005566
NeilBrowndafb20f2012-03-19 12:46:39 +11005567 rdev_for_each(rdev, mddev)
5568 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005569 if (rdev < rdev2 &&
5570 rdev->bdev->bd_contains ==
5571 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005572 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5573 mdname(mddev),
5574 bdevname(rdev->bdev,b),
5575 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005576 warned = 1;
5577 }
5578 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005579
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005580 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005581 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005582 }
5583
NeilBrown657390d2005-08-26 18:34:16 -07005584 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005585 /* may be over-ridden by personality */
5586 mddev->resync_max_sectors = mddev->dev_sectors;
5587
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005588 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589
NeilBrown0f9552b52009-12-30 12:08:50 +11005590 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005591 mddev->ro = 2; /* read-only, but switch on first write */
5592
NeilBrown36d091f2014-12-15 12:56:58 +11005593 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005594 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005595 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005596 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005597 WARN_ONCE(!mddev->external_size,
5598 "%s: default size too small, but 'external_size' not in effect?\n",
5599 __func__);
5600 pr_warn("md: invalid array_size %llu > default size %llu\n",
5601 (unsigned long long)mddev->array_sectors / 2,
5602 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005603 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005604 }
NeilBrown36d091f2014-12-15 12:56:58 +11005605 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005606 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005607 struct bitmap *bitmap;
5608
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005609 bitmap = md_bitmap_create(mddev, -1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005610 if (IS_ERR(bitmap)) {
5611 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005612 pr_warn("%s: failed to create bitmap (%d)\n",
5613 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005614 } else
5615 mddev->bitmap = bitmap;
5616
NeilBrownb15c2e52006-01-06 00:20:16 -08005617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005619 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005620 if (mddev->private)
5621 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005622 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005623 module_put(pers->owner);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005624 md_bitmap_destroy(mddev);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005625 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 }
NeilBrown5c675f82014-12-15 12:56:56 +11005627 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005628 bool nonrot = true;
5629
5630 rdev_for_each(rdev, mddev) {
5631 if (rdev->raid_disk >= 0 &&
5632 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5633 nonrot = false;
5634 break;
5635 }
5636 }
5637 if (mddev->degraded)
5638 nonrot = false;
5639 if (nonrot)
Bart Van Assche8b904b52018-03-07 17:10:10 -08005640 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
Shaohua Libb086a82016-09-30 09:45:40 -07005641 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08005642 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005643 mddev->queue->backing_dev_info->congested_data = mddev;
5644 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005645 }
NeilBrown36d091f2014-12-15 12:56:58 +11005646 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005647 if (mddev->kobj.sd &&
5648 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005649 pr_warn("md: cannot register extra attributes for %s\n",
5650 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005651 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005652 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005653 mddev->ro = 0;
5654
Robert Becker1e509152009-12-14 12:49:58 +11005655 atomic_set(&mddev->max_corr_read_errors,
5656 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005658 if (mddev_is_clustered(mddev))
5659 mddev->safemode_delay = 0;
5660 else
5661 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005663 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005664 spin_lock(&mddev->lock);
5665 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005666 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005667 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005668 if (rdev->raid_disk >= 0)
5669 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005670 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005671
NeilBrowna4a3d262015-07-17 11:57:30 +10005672 if (mddev->degraded && !mddev->ro)
5673 /* This ensures that recovering status is reported immediately
5674 * via sysfs - until a lack of spares is confirmed.
5675 */
5676 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005678
Shaohua Li29530792016-12-08 15:48:19 -08005679 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07005680 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
NeilBrownd7603b72006-01-06 00:20:30 -08005682 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005683 sysfs_notify_dirent_safe(mddev->sysfs_state);
5684 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005685 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 return 0;
Xiao Nib1261942018-01-24 12:17:38 +08005687
5688abort:
Chengguang Xu37b22c22018-11-14 07:33:09 +08005689 mempool_destroy(mddev->flush_bio_pool);
5690 mddev->flush_bio_pool = NULL;
5691 mempool_destroy(mddev->flush_pool);
5692 mddev->flush_pool = NULL;
Xiao Nib1261942018-01-24 12:17:38 +08005693
5694 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695}
NeilBrown390ee602010-06-01 19:37:27 +10005696EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
NeilBrownfd01b882011-10-11 16:47:53 +11005698static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005699{
5700 int err;
5701
5702 err = md_run(mddev);
5703 if (err)
5704 goto out;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005705 err = md_bitmap_load(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005706 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005707 md_bitmap_destroy(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005708 goto out;
5709 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005710
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005711 if (mddev_is_clustered(mddev))
5712 md_allow_write(mddev);
5713
Song Liud5d885f2017-11-19 22:17:01 -08005714 /* run start up tasks that require md_thread */
5715 md_start(mddev);
5716
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005717 md_wakeup_thread(mddev->thread);
5718 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5719
NeilBrownfe60b012010-03-29 11:10:42 +11005720 set_capacity(mddev->gendisk, mddev->array_sectors);
5721 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005722 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005723 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5724out:
5725 return err;
5726}
5727
Song Liud5d885f2017-11-19 22:17:01 -08005728int md_start(struct mddev *mddev)
5729{
5730 int ret = 0;
5731
5732 if (mddev->pers->start) {
5733 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
5734 md_wakeup_thread(mddev->thread);
5735 ret = mddev->pers->start(mddev);
5736 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
5737 md_wakeup_thread(mddev->sync_thread);
5738 }
5739 return ret;
5740}
5741EXPORT_SYMBOL_GPL(md_start);
5742
NeilBrownfd01b882011-10-11 16:47:53 +11005743static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744{
5745 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10005746 struct md_rdev *rdev;
5747 bool has_journal = false;
5748 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749
Andre Noll80fab1d2008-07-11 22:02:21 +10005750 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005752 return -ENXIO;
5753 if (!mddev->pers)
5754 return -EINVAL;
5755 if (!mddev->ro)
5756 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005757
NeilBrown97b20ef2017-04-13 08:53:48 +10005758 rcu_read_lock();
5759 rdev_for_each_rcu(rdev, mddev) {
5760 if (test_bit(Journal, &rdev->flags) &&
5761 !test_bit(Faulty, &rdev->flags))
5762 has_journal = true;
5763 if (bdev_read_only(rdev->bdev))
5764 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07005765 }
NeilBrown97b20ef2017-04-13 08:53:48 +10005766 rcu_read_unlock();
5767 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
5768 /* Don't restart rw with journal missing/faulty */
5769 return -EINVAL;
5770 if (has_readonly)
5771 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07005772
Andre Noll80fab1d2008-07-11 22:02:21 +10005773 mddev->safemode = 0;
5774 mddev->ro = 0;
5775 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11005776 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10005777 /* Kick recovery or resync if necessary */
5778 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5779 md_wakeup_thread(mddev->thread);
5780 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005781 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005782 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783}
5784
NeilBrownfd01b882011-10-11 16:47:53 +11005785static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005786{
5787 mddev->array_sectors = 0;
5788 mddev->external_size = 0;
5789 mddev->dev_sectors = 0;
5790 mddev->raid_disks = 0;
5791 mddev->recovery_cp = 0;
5792 mddev->resync_min = 0;
5793 mddev->resync_max = MaxSector;
5794 mddev->reshape_position = MaxSector;
5795 mddev->external = 0;
5796 mddev->persistent = 0;
5797 mddev->level = LEVEL_NONE;
5798 mddev->clevel[0] = 0;
5799 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08005800 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005801 mddev->ro = 0;
5802 mddev->metadata_type[0] = 0;
5803 mddev->chunk_sectors = 0;
5804 mddev->ctime = mddev->utime = 0;
5805 mddev->layout = 0;
5806 mddev->max_disks = 0;
5807 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005808 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005809 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005810 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005811 mddev->new_level = LEVEL_NONE;
5812 mddev->new_layout = 0;
5813 mddev->new_chunk_sectors = 0;
5814 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005815 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005816 mddev->suspend_lo = mddev->suspend_hi = 0;
5817 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5818 mddev->recovery = 0;
5819 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005820 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005821 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005822 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005823 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005824 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005825 mddev->bitmap_info.offset = 0;
5826 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005827 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005828 mddev->bitmap_info.chunksize = 0;
5829 mddev->bitmap_info.daemon_sleep = 0;
5830 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005831 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005832}
5833
NeilBrownfd01b882011-10-11 16:47:53 +11005834static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005835{
NeilBrown6b6204e2013-05-09 09:48:30 +10005836 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005837 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005838 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005839 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005840 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005841 }
5842
5843 del_timer_sync(&mddev->safemode_timer);
5844
Shaohua Li034e33f2016-11-21 10:29:19 -08005845 if (mddev->pers && mddev->pers->quiesce) {
5846 mddev->pers->quiesce(mddev, 1);
5847 mddev->pers->quiesce(mddev, 0);
5848 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005849 md_bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005850
NeilBrownb6d428c2013-04-24 11:42:42 +10005851 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005852 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08005853 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11005854 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005855 if (!mddev_is_clustered(mddev))
5856 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005857 md_update_sb(mddev, 1);
5858 }
5859}
NeilBrowndefad612011-01-14 09:14:33 +11005860
NeilBrownfd01b882011-10-11 16:47:53 +11005861void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005862{
NeilBrown29f097c2013-11-14 17:54:51 +11005863 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005864 __md_stop_writes(mddev);
5865 mddev_unlock(mddev);
5866}
NeilBrown390ee602010-06-01 19:37:27 +10005867EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005868
NeilBrown5aa61f42014-12-15 12:56:57 +11005869static void mddev_detach(struct mddev *mddev)
5870{
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005871 md_bitmap_wait_behind_writes(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005872 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005873 mddev->pers->quiesce(mddev, 1);
5874 mddev->pers->quiesce(mddev, 0);
5875 }
5876 md_unregister_thread(&mddev->thread);
5877 if (mddev->queue)
5878 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5879}
5880
NeilBrown5eff3c42012-11-19 10:47:48 +11005881static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005882{
NeilBrown36d091f2014-12-15 12:56:58 +11005883 struct md_personality *pers = mddev->pers;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005884 md_bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11005885 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005886 /* Ensure ->event_work is done */
5887 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005888 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005889 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005890 spin_unlock(&mddev->lock);
5891 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005892 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005893 if (pers->sync_request && mddev->to_remove == NULL)
5894 mddev->to_remove = &md_redundancy_group;
5895 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005896 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Xiao Ni5a409b42018-05-21 11:49:54 +08005897 if (mddev->flush_bio_pool) {
5898 mempool_destroy(mddev->flush_bio_pool);
5899 mddev->flush_bio_pool = NULL;
5900 }
5901 if (mddev->flush_pool) {
5902 mempool_destroy(mddev->flush_pool);
5903 mddev->flush_pool = NULL;
5904 }
Jack Wang6aaa58c2018-10-19 16:21:31 +02005905}
5906
5907void md_stop(struct mddev *mddev)
5908{
5909 /* stop the array and free an attached data structures.
5910 * This is called from dm-raid
5911 */
5912 __md_stop(mddev);
Kent Overstreetafeee512018-05-20 18:25:52 -04005913 bioset_exit(&mddev->bio_set);
5914 bioset_exit(&mddev->sync_set);
NeilBrown5eff3c42012-11-19 10:47:48 +11005915}
5916
NeilBrown390ee602010-06-01 19:37:27 +10005917EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005918
NeilBrowna05b7ea2012-07-19 15:59:18 +10005919static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005920{
5921 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005922 int did_freeze = 0;
5923
5924 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5925 did_freeze = 1;
5926 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5927 md_wakeup_thread(mddev->thread);
5928 }
NeilBrownf851b602014-12-11 10:02:10 +11005929 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005930 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005931 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005932 /* Thread might be blocked waiting for metadata update
5933 * which will now never happen */
5934 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005935
Shaohua Li29530792016-12-08 15:48:19 -08005936 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10005937 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005938 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005939 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5940 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005941 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005942 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005943 mddev_lock_nointr(mddev);
5944
NeilBrowna4bd82d2010-03-29 13:23:10 +11005945 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005946 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005947 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005948 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005949 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005950 if (did_freeze) {
5951 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005952 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005953 md_wakeup_thread(mddev->thread);
5954 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005955 err = -EBUSY;
5956 goto out;
5957 }
5958 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005959 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005960
5961 err = -ENXIO;
5962 if (mddev->ro==1)
5963 goto out;
5964 mddev->ro = 1;
5965 set_disk_ro(mddev->gendisk, 1);
5966 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005967 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5968 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005969 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005970 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005971 }
5972out:
5973 mutex_unlock(&mddev->open_mutex);
5974 return err;
5975}
5976
NeilBrown9e653b62006-06-26 00:27:58 -07005977/* mode:
5978 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005979 * 2 - stop but do not disassemble array
5980 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005981static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005982 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005985 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005986 int did_freeze = 0;
5987
5988 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5989 did_freeze = 1;
5990 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5991 md_wakeup_thread(mddev->thread);
5992 }
NeilBrownf851b602014-12-11 10:02:10 +11005993 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005994 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005995 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005996 /* Thread might be blocked waiting for metadata update
5997 * which will now never happen */
5998 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005999
NeilBrown30b8feb2013-11-14 15:16:17 +11006000 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006001 wait_event(resync_wait, (mddev->sync_thread == NULL &&
6002 !test_bit(MD_RECOVERY_RUNNING,
6003 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11006004 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
NeilBrownc8c00a62009-08-10 12:50:52 +10006006 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006007 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006008 mddev->sysfs_active ||
6009 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006010 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006011 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10006012 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11006013 if (did_freeze) {
6014 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006015 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006016 md_wakeup_thread(mddev->thread);
6017 }
NeilBrown260fa032013-08-27 16:44:13 +10006018 return -EBUSY;
6019 }
NeilBrown6e17b022010-08-07 21:41:19 +10006020 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11006021 if (mddev->ro)
6022 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11006023
NeilBrowndefad612011-01-14 09:14:33 +11006024 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11006025 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01006026 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006027
NeilBrowna4bd82d2010-03-29 13:23:10 +11006028 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10006029 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08006030
NeilBrowndafb20f2012-03-19 12:46:39 +11006031 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10006032 if (rdev->raid_disk >= 0)
6033 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10006034
NeilBrowna4bd82d2010-03-29 13:23:10 +11006035 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10006036 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006037 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006038 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08006039
NeilBrowna4bd82d2010-03-29 13:23:10 +11006040 if (mddev->ro)
6041 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10006042 } else
6043 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 /*
6045 * Free resources if final stop
6046 */
NeilBrown9e653b62006-06-26 00:27:58 -07006047 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006048 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049
NeilBrownc3d97142009-12-14 12:49:52 +11006050 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11006051 struct file *f = mddev->bitmap_info.file;
6052 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11006053 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11006054 spin_unlock(&mddev->lock);
6055 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08006056 }
NeilBrownc3d97142009-12-14 12:49:52 +11006057 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08006058
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 export_array(mddev);
6060
NeilBrown6177b472010-03-29 11:37:13 +11006061 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11006062 if (mddev->hold_active == UNTIL_STOP)
6063 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006064 }
NeilBrownd7603b72006-01-06 00:20:30 -08006065 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006066 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10006067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068}
6069
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006070#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11006071static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072{
NeilBrown3cb03002011-10-11 16:45:26 +11006073 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 int err;
6075
NeilBrowna757e642005-04-16 15:26:42 -07006076 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078
NeilBrown9d487392016-11-02 14:16:49 +11006079 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
NeilBrowndafb20f2012-03-19 12:46:39 +11006081 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11006083 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 }
NeilBrown9d487392016-11-02 14:16:49 +11006085 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
NeilBrownd710e132008-10-13 11:55:12 +11006087 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006089 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10006090 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 }
6092}
6093
6094/*
6095 * lets try to run arrays based on all disks that have arrived
6096 * until now. (those are in pending_raid_disks)
6097 *
6098 * the method: pick the first pending disk, collect all disks with
6099 * the same UUID, remove all from the pending list and put them into
6100 * the 'same_array' list. Then order this list based on superblock
6101 * update time (freshest comes first), kick out 'old' disks and
6102 * compare superblocks. If everything's fine then run it.
6103 *
6104 * If "unit" is allocated, then bump its reference count
6105 */
6106static void autorun_devices(int part)
6107{
NeilBrown3cb03002011-10-11 16:45:26 +11006108 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006109 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 char b[BDEVNAME_SIZE];
6111
NeilBrown9d487392016-11-02 14:16:49 +11006112 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07006114 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08006116 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11006118 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119
NeilBrown9d487392016-11-02 14:16:49 +11006120 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006122 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006124 pr_debug("md: adding %s ...\n",
6125 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 list_move(&rdev->same_set, &candidates);
6127 }
6128 /*
6129 * now we have a set of devices, with all of them having
6130 * mostly sane superblocks. It's time to allocate the
6131 * mddev.
6132 */
NeilBrowne8703fe2006-10-03 01:15:59 -07006133 if (part) {
6134 dev = MKDEV(mdp_major,
6135 rdev0->preferred_minor << MdpMinorShift);
6136 unit = MINOR(dev) >> MdpMinorShift;
6137 } else {
6138 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6139 unit = MINOR(dev);
6140 }
6141 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006142 pr_warn("md: unit number in %s is bad: %d\n",
6143 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 break;
6145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146
6147 md_probe(dev, NULL, NULL);
6148 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006149 if (!mddev || !mddev->gendisk) {
6150 if (mddev)
6151 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 break;
6153 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006154 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006155 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 else if (mddev->raid_disks || mddev->major_version
6157 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006158 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 mdname(mddev), bdevname(rdev0->bdev,b));
6160 mddev_unlock(mddev);
6161 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006162 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006163 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006164 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 list_del_init(&rdev->same_set);
6166 if (bind_rdev_to_array(rdev, mddev))
6167 export_rdev(rdev);
6168 }
6169 autorun_array(mddev);
6170 mddev_unlock(mddev);
6171 }
6172 /* on success, candidates will be empty, on error
6173 * it won't...
6174 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006175 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006176 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 mddev_put(mddev);
6180 }
NeilBrown9d487392016-11-02 14:16:49 +11006181 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006183#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184
NeilBrownf72ffdd2014-09-30 14:23:59 +10006185static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186{
6187 mdu_version_t ver;
6188
6189 ver.major = MD_MAJOR_VERSION;
6190 ver.minor = MD_MINOR_VERSION;
6191 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6192
6193 if (copy_to_user(arg, &ver, sizeof(ver)))
6194 return -EFAULT;
6195
6196 return 0;
6197}
6198
NeilBrownf72ffdd2014-09-30 14:23:59 +10006199static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200{
6201 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006202 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006203 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204
NeilBrown1ca69c42012-10-11 13:37:33 +11006205 nr = working = insync = failed = spare = 0;
6206 rcu_read_lock();
6207 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006209 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 failed++;
6211 else {
6212 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006213 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006214 insync++;
Song Liub347af82016-08-11 17:14:45 -07006215 else if (test_bit(Journal, &rdev->flags))
6216 /* TODO: add journal count to md_u.h */
6217 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 else
6219 spare++;
6220 }
6221 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006222 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223
6224 info.major_version = mddev->major_version;
6225 info.minor_version = mddev->minor_version;
6226 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006227 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006229 info.size = mddev->dev_sectors / 2;
6230 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006231 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 info.nr_disks = nr;
6233 info.raid_disks = mddev->raid_disks;
6234 info.md_minor = mddev->md_minor;
6235 info.not_persistent= !mddev->persistent;
6236
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006237 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 info.state = 0;
6239 if (mddev->in_sync)
6240 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006241 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006242 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006243 if (mddev_is_clustered(mddev))
6244 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006245 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 info.working_disks = working;
6247 info.failed_disks = failed;
6248 info.spare_disks = spare;
6249
6250 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006251 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252
6253 if (copy_to_user(arg, &info, sizeof(info)))
6254 return -EFAULT;
6255
6256 return 0;
6257}
6258
NeilBrownf72ffdd2014-09-30 14:23:59 +10006259static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006260{
6261 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006262 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006263 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006264
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006265 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006266 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006267 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006268
NeilBrown32a76272005-06-21 17:17:14 -07006269 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006270 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006271 /* bitmap enabled */
6272 if (mddev->bitmap_info.file) {
6273 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6274 sizeof(file->pathname));
6275 if (IS_ERR(ptr))
6276 err = PTR_ERR(ptr);
6277 else
6278 memmove(file->pathname, ptr,
6279 sizeof(file->pathname)-(ptr-file->pathname));
6280 }
NeilBrown4af1a042014-12-15 12:57:00 +11006281 spin_unlock(&mddev->lock);
6282
6283 if (err == 0 &&
6284 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006285 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006286
NeilBrown32a76272005-06-21 17:17:14 -07006287 kfree(file);
6288 return err;
6289}
6290
NeilBrownf72ffdd2014-09-30 14:23:59 +10006291static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292{
6293 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006294 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295
6296 if (copy_from_user(&info, arg, sizeof(info)))
6297 return -EFAULT;
6298
NeilBrown1ca69c42012-10-11 13:37:33 +11006299 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006300 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 if (rdev) {
6302 info.major = MAJOR(rdev->bdev->bd_dev);
6303 info.minor = MINOR(rdev->bdev->bd_dev);
6304 info.raid_disk = rdev->raid_disk;
6305 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006306 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006308 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 info.state |= (1<<MD_DISK_ACTIVE);
6310 info.state |= (1<<MD_DISK_SYNC);
6311 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006312 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006313 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006314 if (test_bit(WriteMostly, &rdev->flags))
6315 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006316 if (test_bit(FailFast, &rdev->flags))
6317 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 } else {
6319 info.major = info.minor = 0;
6320 info.raid_disk = -1;
6321 info.state = (1<<MD_DISK_REMOVED);
6322 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006323 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324
6325 if (copy_to_user(arg, &info, sizeof(info)))
6326 return -EFAULT;
6327
6328 return 0;
6329}
6330
NeilBrownf72ffdd2014-09-30 14:23:59 +10006331static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332{
6333 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006334 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 dev_t dev = MKDEV(info->major,info->minor);
6336
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006337 if (mddev_is_clustered(mddev) &&
6338 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006339 pr_warn("%s: Cannot add to clustered mddev.\n",
6340 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006341 return -EINVAL;
6342 }
6343
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6345 return -EOVERFLOW;
6346
6347 if (!mddev->raid_disks) {
6348 int err;
6349 /* expecting a device which has a superblock */
6350 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6351 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006352 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 PTR_ERR(rdev));
6354 return PTR_ERR(rdev);
6355 }
6356 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006357 struct md_rdev *rdev0
6358 = list_entry(mddev->disks.next,
6359 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006360 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 .load_super(rdev, rdev0, mddev->minor_version);
6362 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006363 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006364 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 bdevname(rdev0->bdev,b2));
6366 export_rdev(rdev);
6367 return -EINVAL;
6368 }
6369 }
6370 err = bind_rdev_to_array(rdev, mddev);
6371 if (err)
6372 export_rdev(rdev);
6373 return err;
6374 }
6375
6376 /*
6377 * add_new_disk can be used once the array is assembled
6378 * to add "hot spares". They must already have a superblock
6379 * written
6380 */
6381 if (mddev->pers) {
6382 int err;
6383 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006384 pr_warn("%s: personality does not support diskops!\n",
6385 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 return -EINVAL;
6387 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006388 if (mddev->persistent)
6389 rdev = md_import_device(dev, mddev->major_version,
6390 mddev->minor_version);
6391 else
6392 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006394 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 PTR_ERR(rdev));
6396 return PTR_ERR(rdev);
6397 }
NeilBrown1a855a02010-12-09 16:36:28 +11006398 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006399 if (!mddev->persistent) {
6400 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006401 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006402 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006403 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006404 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006405 } else
NeilBrown41158c72005-06-21 17:17:25 -07006406 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006407 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006408 } else
6409 super_types[mddev->major_version].
6410 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006411 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006412 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006413 /* This was a hot-add request, but events doesn't
6414 * match, so reject it.
6415 */
6416 export_rdev(rdev);
6417 return -EINVAL;
6418 }
6419
NeilBrownb2d444d2005-11-08 21:39:31 -08006420 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006421 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6422 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006423 else
6424 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006425 if (info->state & (1<<MD_DISK_FAILFAST))
6426 set_bit(FailFast, &rdev->flags);
6427 else
6428 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006429
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006430 if (info->state & (1<<MD_DISK_JOURNAL)) {
6431 struct md_rdev *rdev2;
6432 bool has_journal = false;
6433
6434 /* make sure no existing journal disk */
6435 rdev_for_each(rdev2, mddev) {
6436 if (test_bit(Journal, &rdev2->flags)) {
6437 has_journal = true;
6438 break;
6439 }
6440 }
NeilBrown230b55f2017-10-17 14:24:09 +11006441 if (has_journal || mddev->bitmap) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006442 export_rdev(rdev);
6443 return -EBUSY;
6444 }
Song Liubac624f2015-08-13 14:31:55 -07006445 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006446 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006447 /*
6448 * check whether the device shows up in other nodes
6449 */
6450 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006451 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006452 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006453 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006454 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006455 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006456 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006457 export_rdev(rdev);
6458 return err;
6459 }
6460 }
6461 }
6462
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 rdev->raid_disk = -1;
6464 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006465
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 if (err)
6467 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006468
6469 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006470 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6471 if (!err) {
6472 err = md_cluster_ops->new_disk_ack(mddev,
6473 err == 0);
6474 if (err)
6475 md_kick_rdev_from_array(rdev);
6476 }
6477 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006478 if (err)
6479 md_cluster_ops->add_new_disk_cancel(mddev);
6480 else
6481 err = add_bound_rdev(rdev);
6482 }
6483
6484 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006485 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006486
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 return err;
6488 }
6489
6490 /* otherwise, add_new_disk is only allowed
6491 * for major_version==0 superblocks
6492 */
6493 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006494 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 return -EINVAL;
6496 }
6497
6498 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6499 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006500 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006502 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503 PTR_ERR(rdev));
6504 return PTR_ERR(rdev);
6505 }
6506 rdev->desc_nr = info->number;
6507 if (info->raid_disk < mddev->raid_disks)
6508 rdev->raid_disk = info->raid_disk;
6509 else
6510 rdev->raid_disk = -1;
6511
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006513 if (info->state & (1<<MD_DISK_SYNC))
6514 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006516 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6517 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006518 if (info->state & (1<<MD_DISK_FAILFAST))
6519 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006520
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006522 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006523 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6524 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006525 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006526 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527
NeilBrown2bf071b2006-01-06 00:20:55 -08006528 err = bind_rdev_to_array(rdev, mddev);
6529 if (err) {
6530 export_rdev(rdev);
6531 return err;
6532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533 }
6534
6535 return 0;
6536}
6537
NeilBrownf72ffdd2014-09-30 14:23:59 +10006538static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539{
6540 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006541 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542
Yufen Yuc42a0e22018-05-04 18:08:10 +08006543 if (!mddev->pers)
6544 return -ENODEV;
6545
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546 rdev = find_rdev(mddev, dev);
6547 if (!rdev)
6548 return -ENXIO;
6549
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006550 if (rdev->raid_disk < 0)
6551 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006552
NeilBrown3ea8929d2013-04-24 11:42:41 +10006553 clear_bit(Blocked, &rdev->flags);
6554 remove_and_add_spares(mddev, rdev);
6555
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 if (rdev->raid_disk >= 0)
6557 goto busy;
6558
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006559kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006560 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006561 md_cluster_ops->remove_disk(mddev, rdev);
6562
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006563 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006564 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006565 if (mddev->thread)
6566 md_wakeup_thread(mddev->thread);
6567 else
6568 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006569 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570
6571 return 0;
6572busy:
NeilBrown9d487392016-11-02 14:16:49 +11006573 pr_debug("md: cannot remove active disk %s from %s ...\n",
6574 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575 return -EBUSY;
6576}
6577
NeilBrownf72ffdd2014-09-30 14:23:59 +10006578static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579{
6580 char b[BDEVNAME_SIZE];
6581 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006582 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583
6584 if (!mddev->pers)
6585 return -ENODEV;
6586
6587 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006588 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 mdname(mddev));
6590 return -EINVAL;
6591 }
6592 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006593 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 mdname(mddev));
6595 return -EINVAL;
6596 }
6597
NeilBrownd710e132008-10-13 11:55:12 +11006598 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006600 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 PTR_ERR(rdev));
6602 return -EINVAL;
6603 }
6604
6605 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006606 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006608 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609
NeilBrown8190e752009-06-18 08:48:58 +10006610 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611
NeilBrownb2d444d2005-11-08 21:39:31 -08006612 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006613 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614 bdevname(rdev->bdev,b), mdname(mddev));
6615 err = -EINVAL;
6616 goto abort_export;
6617 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006618
NeilBrownb2d444d2005-11-08 21:39:31 -08006619 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006621 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006622 err = bind_rdev_to_array(rdev, mddev);
6623 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006624 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625
6626 /*
6627 * The rest should better be atomic, we can have disk failures
6628 * noticed in interrupt contexts ...
6629 */
6630
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 rdev->raid_disk = -1;
6632
Shaohua Li29530792016-12-08 15:48:19 -08006633 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006634 if (!mddev->thread)
6635 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 /*
6637 * Kick recovery, maybe this spare has to be added to the
6638 * array immediately.
6639 */
6640 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6641 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006642 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 return 0;
6644
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645abort_export:
6646 export_rdev(rdev);
6647 return err;
6648}
6649
NeilBrownfd01b882011-10-11 16:47:53 +11006650static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006651{
NeilBrown035328c2014-04-09 12:25:40 +10006652 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006653
NeilBrown36fa3062005-09-09 16:23:45 -07006654 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006655 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006656 return -EBUSY;
6657 if (mddev->recovery || mddev->sync_thread)
6658 return -EBUSY;
6659 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006660 }
6661
NeilBrown36fa3062005-09-09 16:23:45 -07006662 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006663 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006664 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006665
NeilBrown1e594bb2014-12-15 12:57:00 +11006666 if (mddev->bitmap || mddev->bitmap_info.file)
6667 return -EEXIST; /* cannot add when bitmap is present */
6668 f = fget(fd);
6669
6670 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006671 pr_warn("%s: error: failed to get bitmap file\n",
6672 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006673 return -EBADF;
6674 }
6675
NeilBrown1e594bb2014-12-15 12:57:00 +11006676 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006677 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006678 pr_warn("%s: error: bitmap file must be a regular file\n",
6679 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006680 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006681 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006682 pr_warn("%s: error: bitmap file must open for write\n",
6683 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006684 err = -EBADF;
6685 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006686 pr_warn("%s: error: bitmap file is already in use\n",
6687 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006688 err = -EBUSY;
6689 }
6690 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006691 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006692 return err;
6693 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006694 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006695 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006696 } else if (mddev->bitmap == NULL)
6697 return -ENOENT; /* cannot remove what isn't there */
6698 err = 0;
6699 if (mddev->pers) {
NeilBrown69e51b42010-06-01 19:37:35 +10006700 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006701 struct bitmap *bitmap;
6702
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006703 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006704 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006705 if (!IS_ERR(bitmap)) {
6706 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006707 err = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006708 } else
6709 err = PTR_ERR(bitmap);
NeilBrown52a0d492017-10-17 13:46:43 +11006710 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006711 md_bitmap_destroy(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11006712 fd = -1;
6713 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006714 mddev_resume(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11006715 } else if (fd < 0) {
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006716 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006717 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006718 mddev_resume(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006719 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006720 }
6721 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006722 struct file *f = mddev->bitmap_info.file;
6723 if (f) {
6724 spin_lock(&mddev->lock);
6725 mddev->bitmap_info.file = NULL;
6726 spin_unlock(&mddev->lock);
6727 fput(f);
6728 }
NeilBrown36fa3062005-09-09 16:23:45 -07006729 }
6730
NeilBrown32a76272005-06-21 17:17:14 -07006731 return err;
6732}
6733
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734/*
6735 * set_array_info is used two different ways
6736 * The original usage is when creating a new array.
6737 * In this usage, raid_disks is > 0 and it together with
6738 * level, size, not_persistent,layout,chunksize determine the
6739 * shape of the array.
6740 * This will always create an array with a type-0.90.0 superblock.
6741 * The newer usage is when assembling an array.
6742 * In this case raid_disks will be 0, and the major_version field is
6743 * use to determine which style super-blocks are to be found on the devices.
6744 * The minor and patch _version numbers are also kept incase the
6745 * super_block handler wishes to interpret them.
6746 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006747static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748{
6749
6750 if (info->raid_disks == 0) {
6751 /* just setting version number for superblock loading */
6752 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006753 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 super_types[info->major_version].name == NULL) {
6755 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11006756 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 info->major_version);
6758 return -EINVAL;
6759 }
6760 mddev->major_version = info->major_version;
6761 mddev->minor_version = info->minor_version;
6762 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006763 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006764 /* ensure mddev_put doesn't delete this now that there
6765 * is some minimal configuration.
6766 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006767 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 return 0;
6769 }
6770 mddev->major_version = MD_MAJOR_VERSION;
6771 mddev->minor_version = MD_MINOR_VERSION;
6772 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006773 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774
6775 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006776 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006777 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 mddev->raid_disks = info->raid_disks;
6779 /* don't set md_minor, it is determined by which /dev/md* was
6780 * openned
6781 */
6782 if (info->state & (1<<MD_SB_CLEAN))
6783 mddev->recovery_cp = MaxSector;
6784 else
6785 mddev->recovery_cp = 0;
6786 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006787 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788
6789 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006790 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791
Shaohua Li29530792016-12-08 15:48:19 -08006792 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11006793 mddev->max_disks = MD_SB_DISKS;
6794 mddev->flags = 0;
6795 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006796 }
6797 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798
NeilBrownc3d97142009-12-14 12:49:52 +11006799 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006800 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006801 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006802
NeilBrownf6705572006-03-27 01:18:11 -08006803 mddev->reshape_position = MaxSector;
6804
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 /*
6806 * Generate a 128 bit UUID
6807 */
6808 get_random_bytes(mddev->uuid, 16);
6809
NeilBrownf6705572006-03-27 01:18:11 -08006810 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006811 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006812 mddev->new_layout = mddev->layout;
6813 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006814 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006815
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 return 0;
6817}
6818
NeilBrownfd01b882011-10-11 16:47:53 +11006819void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006820{
Shaohua Liefa4b772017-10-18 22:08:13 -07006821 lockdep_assert_held(&mddev->reconfig_mutex);
Dan Williamsb522adc2009-03-31 15:00:31 +11006822
6823 if (mddev->external_size)
6824 return;
6825
Dan Williams1f403622009-03-31 14:59:03 +11006826 mddev->array_sectors = array_sectors;
6827}
6828EXPORT_SYMBOL(md_set_array_sectors);
6829
NeilBrownfd01b882011-10-11 16:47:53 +11006830static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006831{
NeilBrown3cb03002011-10-11 16:45:26 +11006832 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006833 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006834 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08006835 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006836
NeilBrowna35b0d62006-01-06 00:20:49 -08006837 if (mddev->pers->resize == NULL)
6838 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006839 /* The "num_sectors" is the number of sectors of each device that
6840 * is used. This can only make sense for arrays with redundancy.
6841 * linear and raid0 always use whatever space is available. We can only
6842 * consider changing this number if no resync or reconstruction is
6843 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006844 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006845 * of each device. If num_sectors is zero, we find the largest size
6846 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006847 */
NeilBrownf851b602014-12-11 10:02:10 +11006848 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6849 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006850 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006851 if (mddev->ro)
6852 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006853
NeilBrowndafb20f2012-03-19 12:46:39 +11006854 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006855 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006856
Andre Nolld71f9f82008-07-11 22:02:22 +10006857 if (fit && (num_sectors == 0 || num_sectors > avail))
6858 num_sectors = avail;
6859 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006860 return -ENOSPC;
6861 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006862 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08006863 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08006864 if (mddev_is_clustered(mddev))
6865 md_cluster_ops->update_size(mddev, old_dev_sectors);
6866 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08006867 set_capacity(mddev->gendisk, mddev->array_sectors);
6868 revalidate_disk(mddev->gendisk);
6869 }
6870 }
NeilBrowna35b0d62006-01-06 00:20:49 -08006871 return rv;
6872}
6873
NeilBrownfd01b882011-10-11 16:47:53 +11006874static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006875{
6876 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006877 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006878 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006879 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006880 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006881 if (mddev->ro)
6882 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006883 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006884 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006885 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006886 if (mddev->sync_thread ||
6887 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6888 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006889 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006890
6891 rdev_for_each(rdev, mddev) {
6892 if (mddev->raid_disks < raid_disks &&
6893 rdev->data_offset < rdev->new_data_offset)
6894 return -EINVAL;
6895 if (mddev->raid_disks > raid_disks &&
6896 rdev->data_offset > rdev->new_data_offset)
6897 return -EINVAL;
6898 }
6899
NeilBrown63c70c42006-03-27 01:18:13 -08006900 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006901 if (mddev->delta_disks < 0)
6902 mddev->reshape_backwards = 1;
6903 else if (mddev->delta_disks > 0)
6904 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006905
6906 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006907 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006908 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006909 mddev->reshape_backwards = 0;
6910 }
NeilBrownda943b992006-01-06 00:20:54 -08006911 return rv;
6912}
6913
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914/*
6915 * update_array_info is used to change the configuration of an
6916 * on-line array.
6917 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6918 * fields in the info are checked against the array.
6919 * Any differences that cannot be handled will cause an error.
6920 * Normally, only one change can be managed at a time.
6921 */
NeilBrownfd01b882011-10-11 16:47:53 +11006922static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923{
6924 int rv = 0;
6925 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006926 int state = 0;
6927
6928 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006929 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006930 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931
6932 if (mddev->major_version != info->major_version ||
6933 mddev->minor_version != info->minor_version ||
6934/* mddev->patch_version != info->patch_version || */
6935 mddev->ctime != info->ctime ||
6936 mddev->level != info->level ||
6937/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006938 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006939 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006940 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6941 ((state^info->state) & 0xfffffe00)
6942 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 return -EINVAL;
6944 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006945 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6946 cnt++;
6947 if (mddev->raid_disks != info->raid_disks)
6948 cnt++;
6949 if (mddev->layout != info->layout)
6950 cnt++;
6951 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6952 cnt++;
6953 if (cnt == 0)
6954 return 0;
6955 if (cnt > 1)
6956 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957
6958 if (mddev->layout != info->layout) {
6959 /* Change layout
6960 * we don't need to do anything at the md level, the
6961 * personality will take care of it all.
6962 */
NeilBrown50ac1682009-06-18 08:47:55 +10006963 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006965 else {
6966 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006967 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006968 if (rv)
6969 mddev->new_layout = mddev->layout;
6970 return rv;
6971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006973 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006974 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975
NeilBrownda943b992006-01-06 00:20:54 -08006976 if (mddev->raid_disks != info->raid_disks)
6977 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978
NeilBrown36fa3062005-09-09 16:23:45 -07006979 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006980 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6981 rv = -EINVAL;
6982 goto err;
6983 }
6984 if (mddev->recovery || mddev->sync_thread) {
6985 rv = -EBUSY;
6986 goto err;
6987 }
NeilBrown36fa3062005-09-09 16:23:45 -07006988 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006989 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006990 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006991 if (mddev->bitmap) {
6992 rv = -EEXIST;
6993 goto err;
6994 }
6995 if (mddev->bitmap_info.default_offset == 0) {
6996 rv = -EINVAL;
6997 goto err;
6998 }
NeilBrownc3d97142009-12-14 12:49:52 +11006999 mddev->bitmap_info.offset =
7000 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10007001 mddev->bitmap_info.space =
7002 mddev->bitmap_info.default_space;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007003 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007004 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007005 if (!IS_ERR(bitmap)) {
7006 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007007 rv = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007008 } else
7009 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07007010 if (rv)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007011 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007012 mddev_resume(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07007013 } else {
7014 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007015 if (!mddev->bitmap) {
7016 rv = -ENOENT;
7017 goto err;
7018 }
7019 if (mddev->bitmap->storage.file) {
7020 rv = -EINVAL;
7021 goto err;
7022 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11007023 if (mddev->bitmap_info.nodes) {
7024 /* hold PW on all the bitmap lock */
7025 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11007026 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 +11007027 rv = -EPERM;
7028 md_cluster_ops->unlock_all_bitmaps(mddev);
7029 goto err;
7030 }
7031
7032 mddev->bitmap_info.nodes = 0;
7033 md_cluster_ops->leave(mddev);
7034 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007035 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007036 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007037 mddev_resume(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11007038 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007039 }
7040 }
NeilBrown850b2b422006-10-03 01:15:46 -07007041 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007042 return rv;
7043err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044 return rv;
7045}
7046
NeilBrownfd01b882011-10-11 16:47:53 +11007047static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048{
NeilBrown3cb03002011-10-11 16:45:26 +11007049 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11007050 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051
7052 if (mddev->pers == NULL)
7053 return -ENODEV;
7054
NeilBrown1ca69c42012-10-11 13:37:33 +11007055 rcu_read_lock();
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01007056 rdev = md_find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11007058 err = -ENODEV;
7059 else {
7060 md_error(mddev, rdev);
7061 if (!test_bit(Faulty, &rdev->flags))
7062 err = -EBUSY;
7063 }
7064 rcu_read_unlock();
7065 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066}
7067
Andre Noll2f9618c2008-04-25 18:57:58 +02007068/*
7069 * We have a problem here : there is no easy way to give a CHS
7070 * virtual geometry. We currently pretend that we have a 2 heads
7071 * 4 sectors (with a BIG number of cylinders...). This drives
7072 * dosfs just mad... ;-)
7073 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007074static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7075{
NeilBrownfd01b882011-10-11 16:47:53 +11007076 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007077
7078 geo->heads = 2;
7079 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11007080 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007081 return 0;
7082}
7083
Nicolas Schichancb335f82014-01-15 16:58:52 +01007084static inline bool md_ioctl_valid(unsigned int cmd)
7085{
7086 switch (cmd) {
7087 case ADD_NEW_DISK:
7088 case BLKROSET:
7089 case GET_ARRAY_INFO:
7090 case GET_BITMAP_FILE:
7091 case GET_DISK_INFO:
7092 case HOT_ADD_DISK:
7093 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007094 case RAID_AUTORUN:
7095 case RAID_VERSION:
7096 case RESTART_ARRAY_RW:
7097 case RUN_ARRAY:
7098 case SET_ARRAY_INFO:
7099 case SET_BITMAP_FILE:
7100 case SET_DISK_FAULTY:
7101 case STOP_ARRAY:
7102 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007103 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007104 return true;
7105 default:
7106 return false;
7107 }
7108}
7109
Al Viroa39907f2008-03-02 10:31:15 -05007110static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111 unsigned int cmd, unsigned long arg)
7112{
7113 int err = 0;
7114 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11007115 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10007116 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08007117 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118
Nicolas Schichancb335f82014-01-15 16:58:52 +01007119 if (!md_ioctl_valid(cmd))
7120 return -ENOTTY;
7121
NeilBrown506c9e42011-12-23 10:17:26 +11007122 switch (cmd) {
7123 case RAID_VERSION:
7124 case GET_ARRAY_INFO:
7125 case GET_DISK_INFO:
7126 break;
7127 default:
7128 if (!capable(CAP_SYS_ADMIN))
7129 return -EACCES;
7130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131
7132 /*
7133 * Commands dealing with the RAID driver but not any
7134 * particular array:
7135 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007136 switch (cmd) {
7137 case RAID_VERSION:
7138 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007139 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11007142 case RAID_AUTORUN:
7143 err = 0;
7144 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007145 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11007147 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148 }
7149
7150 /*
7151 * Commands creating/starting a new array:
7152 */
7153
Al Viroa39907f2008-03-02 10:31:15 -05007154 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155
7156 if (!mddev) {
7157 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007158 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 }
7160
NeilBrown1ca69c42012-10-11 13:37:33 +11007161 /* Some actions do not requires the mutex */
7162 switch (cmd) {
7163 case GET_ARRAY_INFO:
7164 if (!mddev->raid_disks && !mddev->external)
7165 err = -ENODEV;
7166 else
7167 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007168 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007169
7170 case GET_DISK_INFO:
7171 if (!mddev->raid_disks && !mddev->external)
7172 err = -ENODEV;
7173 else
7174 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007175 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007176
7177 case SET_DISK_FAULTY:
7178 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007179 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007180
7181 case GET_BITMAP_FILE:
7182 err = get_bitmap_file(mddev, argp);
7183 goto out;
7184
NeilBrown1ca69c42012-10-11 13:37:33 +11007185 }
7186
NeilBrowna7a3f082012-12-11 13:35:54 +11007187 if (cmd == ADD_NEW_DISK)
7188 /* need to ensure md_delayed_delete() has completed */
7189 flush_workqueue(md_misc_wq);
7190
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007191 if (cmd == HOT_REMOVE_DISK)
7192 /* need to ensure recovery thread has run */
7193 wait_event_interruptible_timeout(mddev->sb_wait,
7194 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007195 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007196 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007197 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7198 /* Need to flush page cache, and ensure no-one else opens
7199 * and writes
7200 */
7201 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007202 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007203 mutex_unlock(&mddev->open_mutex);
7204 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007205 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007206 }
NeilBrown065e5192017-04-06 11:16:33 +08007207 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007208 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007209 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007210 mutex_unlock(&mddev->open_mutex);
7211 sync_blockdev(bdev);
7212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 err = mddev_lock(mddev);
7214 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007215 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7216 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007217 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 }
7219
NeilBrownc02c0ae2012-12-11 13:39:21 +11007220 if (cmd == SET_ARRAY_INFO) {
7221 mdu_array_info_t info;
7222 if (!arg)
7223 memset(&info, 0, sizeof(info));
7224 else if (copy_from_user(&info, argp, sizeof(info))) {
7225 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007226 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007227 }
7228 if (mddev->pers) {
7229 err = update_array_info(mddev, &info);
7230 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007231 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007232 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007234 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007235 }
7236 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007237 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007238 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007239 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007240 }
7241 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007242 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007243 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007244 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007245 }
7246 err = set_array_info(mddev, &info);
7247 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007248 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007249 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007250 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007251 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 }
7253
7254 /*
7255 * Commands querying/configuring an existing array:
7256 */
NeilBrown32a76272005-06-21 17:17:14 -07007257 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007258 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007259 if ((!mddev->raid_disks && !mddev->external)
7260 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7261 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7262 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007264 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 }
7266
7267 /*
7268 * Commands even a read-only array can execute:
7269 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007270 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007271 case RESTART_ARRAY_RW:
7272 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007273 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007274
7275 case STOP_ARRAY:
7276 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007277 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007278
7279 case STOP_ARRAY_RO:
7280 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007281 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007282
NeilBrown3ea8929d2013-04-24 11:42:41 +10007283 case HOT_REMOVE_DISK:
7284 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007285 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007286
NeilBrown7ceb17e2013-04-24 11:42:42 +10007287 case ADD_NEW_DISK:
7288 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007289 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007290 * So require mddev->pers and MD_DISK_SYNC.
7291 */
7292 if (mddev->pers) {
7293 mdu_disk_info_t info;
7294 if (copy_from_user(&info, argp, sizeof(info)))
7295 err = -EFAULT;
7296 else if (!(info.state & (1<<MD_DISK_SYNC)))
7297 /* Need to clear read-only for this */
7298 break;
7299 else
7300 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007301 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007302 }
7303 break;
7304
NeilBrownc02c0ae2012-12-11 13:39:21 +11007305 case BLKROSET:
7306 if (get_user(ro, (int __user *)(arg))) {
7307 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007308 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007309 }
7310 err = -EINVAL;
7311
7312 /* if the bdev is going readonly the value of mddev->ro
7313 * does not matter, no writes are coming
7314 */
7315 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007316 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007317
NeilBrownc02c0ae2012-12-11 13:39:21 +11007318 /* are we are already prepared for writes? */
7319 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007320 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007321
7322 /* transitioning to readauto need only happen for
7323 * arrays that call md_write_start
7324 */
7325 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007327 if (err == 0) {
7328 mddev->ro = 2;
7329 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007330 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007331 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007332 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 }
7334
7335 /*
7336 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007337 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338 */
NeilBrown326eb172014-09-30 15:36:28 +10007339 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007340 if (mddev->ro == 2) {
7341 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007342 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007343 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007344 /* mddev_unlock will wake thread */
7345 /* If a device failed while we were read-only, we
7346 * need to make sure the metadata is updated now.
7347 */
Shaohua Li29530792016-12-08 15:48:19 -08007348 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007349 mddev_unlock(mddev);
7350 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007351 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7352 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007353 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007354 }
NeilBrownf91de922005-11-08 21:39:36 -08007355 } else {
7356 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007357 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359 }
7360
NeilBrownc02c0ae2012-12-11 13:39:21 +11007361 switch (cmd) {
7362 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007364 mdu_disk_info_t info;
7365 if (copy_from_user(&info, argp, sizeof(info)))
7366 err = -EFAULT;
7367 else
7368 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007369 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007372 case CLUSTERED_DISK_NACK:
7373 if (mddev_is_clustered(mddev))
7374 md_cluster_ops->new_disk_ack(mddev, false);
7375 else
7376 err = -EINVAL;
7377 goto unlock;
7378
NeilBrownc02c0ae2012-12-11 13:39:21 +11007379 case HOT_ADD_DISK:
7380 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007381 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382
NeilBrownc02c0ae2012-12-11 13:39:21 +11007383 case RUN_ARRAY:
7384 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007385 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386
NeilBrownc02c0ae2012-12-11 13:39:21 +11007387 case SET_BITMAP_FILE:
7388 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007389 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007390
NeilBrownc02c0ae2012-12-11 13:39:21 +11007391 default:
7392 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007393 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394 }
7395
NeilBrown3adc28d2014-09-30 15:46:41 +10007396unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007397 if (mddev->hold_active == UNTIL_IOCTL &&
7398 err != -EINVAL)
7399 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007401out:
NeilBrown065e5192017-04-06 11:16:33 +08007402 if(did_set_md_closing)
7403 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 return err;
7405}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007406#ifdef CONFIG_COMPAT
7407static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7408 unsigned int cmd, unsigned long arg)
7409{
7410 switch (cmd) {
7411 case HOT_REMOVE_DISK:
7412 case HOT_ADD_DISK:
7413 case SET_DISK_FAULTY:
7414 case SET_BITMAP_FILE:
7415 /* These take in integer arg, do not convert */
7416 break;
7417 default:
7418 arg = (unsigned long)compat_ptr(arg);
7419 break;
7420 }
7421
7422 return md_ioctl(bdev, mode, cmd, arg);
7423}
7424#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425
Al Viroa39907f2008-03-02 10:31:15 -05007426static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427{
7428 /*
7429 * Succeed if we can lock the mddev, which confirms that
7430 * it isn't being stopped right now.
7431 */
NeilBrownfd01b882011-10-11 16:47:53 +11007432 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 int err;
7434
Yuanhan Liu0c098222012-05-22 13:55:32 +10007435 if (!mddev)
7436 return -ENODEV;
7437
NeilBrownd3374822009-01-09 08:31:10 +11007438 if (mddev->gendisk != bdev->bd_disk) {
7439 /* we are racing with mddev_put which is discarding this
7440 * bd_disk.
7441 */
7442 mddev_put(mddev);
7443 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007444 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007445 /* Then retry the open from the top */
7446 return -ERESTARTSYS;
7447 }
7448 BUG_ON(mddev != bdev->bd_disk->private_data);
7449
NeilBrownc8c00a62009-08-10 12:50:52 +10007450 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451 goto out;
7452
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007453 if (test_bit(MD_CLOSING, &mddev->flags)) {
7454 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007455 err = -ENODEV;
7456 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007457 }
7458
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007460 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007461 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007463 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007465 if (err)
7466 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467 return err;
7468}
7469
Al Virodb2a1442013-05-05 21:52:57 -04007470static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007472 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007474 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007475 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007478
7479static int md_media_changed(struct gendisk *disk)
7480{
NeilBrownfd01b882011-10-11 16:47:53 +11007481 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007482
7483 return mddev->changed;
7484}
7485
7486static int md_revalidate(struct gendisk *disk)
7487{
NeilBrownfd01b882011-10-11 16:47:53 +11007488 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007489
7490 mddev->changed = 0;
7491 return 0;
7492}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007493static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494{
7495 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007496 .open = md_open,
7497 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007498 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007499#ifdef CONFIG_COMPAT
7500 .compat_ioctl = md_compat_ioctl,
7501#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007502 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007503 .media_changed = md_media_changed,
7504 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505};
7506
NeilBrownf72ffdd2014-09-30 14:23:59 +10007507static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508{
NeilBrown2b8bf342011-10-11 16:48:23 +11007509 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511 /*
7512 * md_thread is a 'system-thread', it's priority should be very
7513 * high. We avoid resource deadlocks individually in each
7514 * raid personality. (RAID5 does preallocation) We also use RR and
7515 * the very same RT priority as kswapd, thus we will never get
7516 * into a priority inversion deadlock.
7517 *
7518 * we definitely have to have equal or higher priority than
7519 * bdflush, otherwise bdflush will deadlock if there are too
7520 * many dirty RAID5 blocks.
7521 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522
NeilBrown6985c432005-10-19 21:23:47 -07007523 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007524 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525
NeilBrown93588e22005-11-15 00:09:12 -08007526 /* We need to wait INTERRUPTIBLE so that
7527 * we don't add to the load-average.
7528 * That means we need to be sure no signals are
7529 * pending
7530 */
7531 if (signal_pending(current))
7532 flush_signals(current);
7533
7534 wait_event_interruptible_timeout
7535 (thread->wqueue,
7536 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007537 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007538 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539
NeilBrown6c987912011-01-14 09:13:53 +11007540 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007541 if (kthread_should_park())
7542 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007543 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007544 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007546
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 return 0;
7548}
7549
NeilBrown2b8bf342011-10-11 16:48:23 +11007550void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551{
7552 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007553 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Guoqing Jiangd1d90142017-10-09 10:32:48 +08007554 set_bit(THREAD_WAKEUP, &thread->flags);
7555 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556 }
7557}
NeilBrown6c144d32014-09-30 16:15:38 +10007558EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559
Shaohua Li4ed87312012-10-11 13:34:00 +11007560struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7561 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562{
NeilBrown2b8bf342011-10-11 16:48:23 +11007563 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564
NeilBrown2b8bf342011-10-11 16:48:23 +11007565 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566 if (!thread)
7567 return NULL;
7568
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 init_waitqueue_head(&thread->wqueue);
7570
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571 thread->run = run;
7572 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007573 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007574 thread->tsk = kthread_run(md_thread, thread,
7575 "%s_%s",
7576 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007577 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007578 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579 kfree(thread);
7580 return NULL;
7581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582 return thread;
7583}
NeilBrown6c144d32014-09-30 16:15:38 +10007584EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585
NeilBrown2b8bf342011-10-11 16:48:23 +11007586void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587{
NeilBrown2b8bf342011-10-11 16:48:23 +11007588 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007589 if (!thread)
7590 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007591 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007592 /* Locking ensures that mddev_unlock does not wake_up a
7593 * non-existent thread
7594 */
7595 spin_lock(&pers_lock);
7596 *threadp = NULL;
7597 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007598
7599 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007600 kfree(thread);
7601}
NeilBrown6c144d32014-09-30 16:15:38 +10007602EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603
NeilBrownfd01b882011-10-11 16:47:53 +11007604void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605{
NeilBrownb2d444d2005-11-08 21:39:31 -08007606 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007608
NeilBrownde393cd2011-07-28 11:31:48 +10007609 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 return;
7611 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007612 if (mddev->degraded)
7613 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007614 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7616 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7617 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007618 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007619 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007620 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621}
NeilBrown6c144d32014-09-30 16:15:38 +10007622EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623
7624/* seq_file implementation /proc/mdstat */
7625
7626static void status_unused(struct seq_file *seq)
7627{
7628 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007629 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630
7631 seq_printf(seq, "unused devices: ");
7632
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007633 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634 char b[BDEVNAME_SIZE];
7635 i++;
7636 seq_printf(seq, "%s ",
7637 bdevname(rdev->bdev,b));
7638 }
7639 if (!i)
7640 seq_printf(seq, "<none>");
7641
7642 seq_printf(seq, "\n");
7643}
7644
NeilBrownf7851be2015-07-02 17:12:58 +10007645static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646{
NeilBrowndd71cf62009-05-07 12:49:35 +10007647 sector_t max_sectors, resync, res;
7648 unsigned long dt, db;
7649 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007650 int scale;
7651 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652
NeilBrownc804cde2012-05-21 09:28:33 +10007653 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7654 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007655 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007657 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658
NeilBrownf7851be2015-07-02 17:12:58 +10007659 resync = mddev->curr_resync;
7660 if (resync <= 3) {
7661 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7662 /* Still cleaning up */
7663 resync = max_sectors;
Nate Daileyd2e2ec82017-11-30 11:33:30 -05007664 } else if (resync > max_sectors)
7665 resync = max_sectors;
7666 else
NeilBrownf7851be2015-07-02 17:12:58 +10007667 resync -= atomic_read(&mddev->recovery_active);
7668
7669 if (resync == 0) {
Guoqing Jiang0357ba22018-07-02 16:26:25 +08007670 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
7671 struct md_rdev *rdev;
7672
7673 rdev_for_each(rdev, mddev)
7674 if (rdev->raid_disk >= 0 &&
7675 !test_bit(Faulty, &rdev->flags) &&
7676 rdev->recovery_offset != MaxSector &&
7677 rdev->recovery_offset) {
7678 seq_printf(seq, "\trecover=REMOTE");
7679 return 1;
7680 }
7681 if (mddev->reshape_position != MaxSector)
7682 seq_printf(seq, "\treshape=REMOTE");
7683 else
7684 seq_printf(seq, "\tresync=REMOTE");
7685 return 1;
7686 }
NeilBrownf7851be2015-07-02 17:12:58 +10007687 if (mddev->recovery_cp < MaxSector) {
7688 seq_printf(seq, "\tresync=PENDING");
7689 return 1;
7690 }
7691 return 0;
7692 }
7693 if (resync < 3) {
7694 seq_printf(seq, "\tresync=DELAYED");
7695 return 1;
7696 }
7697
NeilBrown403df472014-09-30 15:52:29 +10007698 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007699 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007700 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007701 * u32, as those are the requirements for sector_div.
7702 * Thus 'scale' must be at least 10
7703 */
7704 scale = 10;
7705 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007706 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007707 scale++;
7708 }
7709 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007710 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007711
7712 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713 {
NeilBrown4588b422006-03-27 01:18:04 -08007714 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715 seq_printf(seq, "[");
7716 for (i = 0; i < x; i++)
7717 seq_printf(seq, "=");
7718 seq_printf(seq, ">");
7719 for (i = 0; i < y; i++)
7720 seq_printf(seq, ".");
7721 seq_printf(seq, "] ");
7722 }
NeilBrown4588b422006-03-27 01:18:04 -08007723 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007724 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7725 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007726 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7727 "check" :
7728 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7729 "resync" : "recovery"))),
7730 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007731 (unsigned long long) resync/2,
7732 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733
7734 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007735 * dt: time from mark until now
7736 * db: blocks written from mark until now
7737 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007738 *
7739 * rt is a sector_t, so could be 32bit or 64bit.
7740 * So we divide before multiply in case it is 32bit and close
7741 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007742 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007743 * near the end of resync when the number of remaining sectors
7744 * is close to 'db'.
7745 * We then divide rt by 32 after multiplying by db to compensate.
7746 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007747 */
7748 dt = ((jiffies - mddev->resync_mark) / HZ);
7749 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007750 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7751 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007752
NeilBrowndd71cf62009-05-07 12:49:35 +10007753 rt = max_sectors - resync; /* number of remaining sectors */
7754 sector_div(rt, db/32+1);
7755 rt *= dt;
7756 rt >>= 5;
7757
7758 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7759 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760
NeilBrownff4e8d92006-07-10 04:44:16 -07007761 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007762 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007763}
7764
7765static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7766{
7767 struct list_head *tmp;
7768 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007769 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770
7771 if (l >= 0x10000)
7772 return NULL;
7773 if (!l--)
7774 /* header */
7775 return (void*)1;
7776
7777 spin_lock(&all_mddevs_lock);
7778 list_for_each(tmp,&all_mddevs)
7779 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007780 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781 mddev_get(mddev);
7782 spin_unlock(&all_mddevs_lock);
7783 return mddev;
7784 }
7785 spin_unlock(&all_mddevs_lock);
7786 if (!l--)
7787 return (void*)2;/* tail */
7788 return NULL;
7789}
7790
7791static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7792{
7793 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007794 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007795
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796 ++*pos;
7797 if (v == (void*)2)
7798 return NULL;
7799
7800 spin_lock(&all_mddevs_lock);
7801 if (v == (void*)1)
7802 tmp = all_mddevs.next;
7803 else
7804 tmp = mddev->all_mddevs.next;
7805 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007806 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807 else {
7808 next_mddev = (void*)2;
7809 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811 spin_unlock(&all_mddevs_lock);
7812
7813 if (v != (void*)1)
7814 mddev_put(mddev);
7815 return next_mddev;
7816
7817}
7818
7819static void md_seq_stop(struct seq_file *seq, void *v)
7820{
NeilBrownfd01b882011-10-11 16:47:53 +11007821 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822
7823 if (mddev && v != (void*)1 && v != (void*)2)
7824 mddev_put(mddev);
7825}
7826
7827static int md_seq_show(struct seq_file *seq, void *v)
7828{
NeilBrownfd01b882011-10-11 16:47:53 +11007829 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007830 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007831 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832
7833 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007834 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835 seq_printf(seq, "Personalities : ");
7836 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007837 list_for_each_entry(pers, &pers_list, list)
7838 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839
7840 spin_unlock(&pers_lock);
7841 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007842 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843 return 0;
7844 }
7845 if (v == (void*)2) {
7846 status_unused(seq);
7847 return 0;
7848 }
7849
NeilBrown36d091f2014-12-15 12:56:58 +11007850 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7852 seq_printf(seq, "%s : %sactive", mdname(mddev),
7853 mddev->pers ? "" : "in");
7854 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007855 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007857 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007858 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007859 seq_printf(seq, " %s", mddev->pers->name);
7860 }
7861
Andre Nolldd8ac332009-03-31 14:33:13 +11007862 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007863 rcu_read_lock();
7864 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865 char b[BDEVNAME_SIZE];
7866 seq_printf(seq, " %s[%d]",
7867 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007868 if (test_bit(WriteMostly, &rdev->flags))
7869 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007870 if (test_bit(Journal, &rdev->flags))
7871 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007872 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873 seq_printf(seq, "(F)");
7874 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007875 }
7876 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007877 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007878 if (test_bit(Replacement, &rdev->flags))
7879 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007880 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007882 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883
7884 if (!list_empty(&mddev->disks)) {
7885 if (mddev->pers)
7886 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007887 (unsigned long long)
7888 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 else
7890 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007891 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007893 if (mddev->persistent) {
7894 if (mddev->major_version != 0 ||
7895 mddev->minor_version != 90) {
7896 seq_printf(seq," super %d.%d",
7897 mddev->major_version,
7898 mddev->minor_version);
7899 }
NeilBrowne6910632008-02-06 01:39:51 -08007900 } else if (mddev->external)
7901 seq_printf(seq, " super external:%s",
7902 mddev->metadata_type);
7903 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007904 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905
7906 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007907 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007908 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007909 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007910 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007911 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007912 }
NeilBrown32a76272005-06-21 17:17:14 -07007913 } else
7914 seq_printf(seq, "\n ");
7915
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007916 md_bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917
7918 seq_printf(seq, "\n");
7919 }
NeilBrown36d091f2014-12-15 12:56:58 +11007920 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007921
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922 return 0;
7923}
7924
Jan Engelhardt110518b2009-05-07 12:49:37 +10007925static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007926 .start = md_seq_start,
7927 .next = md_seq_next,
7928 .stop = md_seq_stop,
7929 .show = md_seq_show,
7930};
7931
7932static int md_seq_open(struct inode *inode, struct file *file)
7933{
Kay Sieversf1514632011-07-12 20:48:39 +02007934 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935 int error;
7936
7937 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007938 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007939 return error;
7940
7941 seq = file->private_data;
7942 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007943 return error;
7944}
7945
NeilBrowne2f23b62014-04-09 14:33:51 +10007946static int md_unloading;
Al Viroafc9a422017-07-03 06:39:46 -04007947static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
NeilBrownd7603b72006-01-06 00:20:30 -08007948{
Kay Sieversf1514632011-07-12 20:48:39 +02007949 struct seq_file *seq = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -04007950 __poll_t mask;
NeilBrownd7603b72006-01-06 00:20:30 -08007951
NeilBrowne2f23b62014-04-09 14:33:51 +10007952 if (md_unloading)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007953 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007954 poll_wait(filp, &md_event_waiters, wait);
7955
7956 /* always allow read */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007957 mask = EPOLLIN | EPOLLRDNORM;
NeilBrownd7603b72006-01-06 00:20:30 -08007958
Kay Sieversf1514632011-07-12 20:48:39 +02007959 if (seq->poll_event != atomic_read(&md_event_count))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007960 mask |= EPOLLERR | EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007961 return mask;
7962}
7963
Arjan van de Venfa027c22007-02-12 00:55:33 -08007964static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007965 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966 .open = md_seq_open,
7967 .read = seq_read,
7968 .llseek = seq_lseek,
Cihangir Akturk26e13042017-07-29 19:52:45 +03007969 .release = seq_release,
NeilBrownd7603b72006-01-06 00:20:30 -08007970 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007971};
7972
NeilBrown84fc4b52011-10-11 16:49:58 +11007973int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007974{
NeilBrown9d487392016-11-02 14:16:49 +11007975 pr_debug("md: %s personality registered for level %d\n",
7976 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007977 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007978 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979 spin_unlock(&pers_lock);
7980 return 0;
7981}
NeilBrown6c144d32014-09-30 16:15:38 +10007982EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007983
NeilBrown84fc4b52011-10-11 16:49:58 +11007984int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985{
NeilBrown9d487392016-11-02 14:16:49 +11007986 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007987 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007988 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989 spin_unlock(&pers_lock);
7990 return 0;
7991}
NeilBrown6c144d32014-09-30 16:15:38 +10007992EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993
NeilBrown6022e752015-08-13 12:32:55 +10007994int register_md_cluster_operations(struct md_cluster_operations *ops,
7995 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007996{
NeilBrown6022e752015-08-13 12:32:55 +10007997 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007998 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007999 if (md_cluster_ops != NULL)
8000 ret = -EALREADY;
8001 else {
8002 md_cluster_ops = ops;
8003 md_cluster_mod = module;
8004 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008005 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008006 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008007}
8008EXPORT_SYMBOL(register_md_cluster_operations);
8009
8010int unregister_md_cluster_operations(void)
8011{
8012 spin_lock(&pers_lock);
8013 md_cluster_ops = NULL;
8014 spin_unlock(&pers_lock);
8015 return 0;
8016}
8017EXPORT_SYMBOL(unregister_md_cluster_operations);
8018
8019int md_setup_cluster(struct mddev *mddev, int nodes)
8020{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008021 if (!md_cluster_ops)
8022 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008023 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008024 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008025 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11008026 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008027 spin_unlock(&pers_lock);
8028 return -ENOENT;
8029 }
8030 spin_unlock(&pers_lock);
8031
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05008032 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008033}
8034
8035void md_cluster_stop(struct mddev *mddev)
8036{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05008037 if (!md_cluster_ops)
8038 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008039 md_cluster_ops->leave(mddev);
8040 module_put(md_cluster_mod);
8041}
8042
NeilBrownfd01b882011-10-11 16:47:53 +11008043static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008044{
NeilBrownf72ffdd2014-09-30 14:23:59 +10008045 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008046 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11008047 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008048
8049 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10008050 rcu_read_lock();
8051 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008052 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Michael Callahan59767fb2018-07-18 04:47:37 -07008053 curr_events = (int)part_stat_read_accum(&disk->part0, sectors) -
NeilBrowneea1bf32009-03-31 14:27:02 +11008054 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07008055 /* sync IO will cause sync_io to increase before the disk_stats
8056 * as sync_io is counted when a request starts, and
8057 * disk_stats is counted when it completes.
8058 * So resync activity will cause curr_events to be smaller than
8059 * when there was no such activity.
8060 * non-sync IO will cause disk_stat to increase without
8061 * increasing sync_io so curr_events will (eventually)
8062 * be larger than it was before. Once it becomes
8063 * substantially larger, the test below will cause
8064 * the array to appear non-idle, and resync will slow
8065 * down.
8066 * If there is a lot of outstanding resync activity when
8067 * we set last_event to curr_events, then all that activity
8068 * completing might cause the array to appear non-idle
8069 * and resync will be slowed down even though there might
8070 * not have been non-resync activity. This will only
8071 * happen once though. 'last_events' will soon reflect
8072 * the state where there is little or no outstanding
8073 * resync requests, and further resync activity will
8074 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08008075 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008076 */
NeilBrowneea1bf32009-03-31 14:27:02 +11008077 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008078 rdev->last_events = curr_events;
8079 idle = 0;
8080 }
8081 }
NeilBrown4b809912008-07-21 17:05:25 +10008082 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008083 return idle;
8084}
8085
NeilBrownfd01b882011-10-11 16:47:53 +11008086void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008087{
8088 /* another "blocks" (512byte) blocks have been synced */
8089 atomic_sub(blocks, &mddev->recovery_active);
8090 wake_up(&mddev->recovery_wait);
8091 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07008092 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08008093 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008094 md_wakeup_thread(mddev->thread);
8095 // stop recovery, signal do_sync ....
8096 }
8097}
NeilBrown6c144d32014-09-30 16:15:38 +10008098EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099
NeilBrown06d91a52005-06-21 17:17:12 -07008100/* md_write_start(mddev, bi)
8101 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07008102 * in superblock) before writing, schedule a superblock update
8103 * and wait for it to complete.
NeilBrowncc27b0c2017-06-05 16:49:39 +10008104 * A return value of 'false' means that the write wasn't recorded
8105 * and cannot proceed as the array is being suspend.
NeilBrown06d91a52005-06-21 17:17:12 -07008106 */
NeilBrowncc27b0c2017-06-05 16:49:39 +10008107bool md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008108{
Neil Brown0fd62b82008-06-28 08:31:36 +10008109 int did_change = 0;
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008110
NeilBrown06d91a52005-06-21 17:17:12 -07008111 if (bio_data_dir(bi) != WRITE)
NeilBrowncc27b0c2017-06-05 16:49:39 +10008112 return true;
NeilBrown06d91a52005-06-21 17:17:12 -07008113
NeilBrownf91de922005-11-08 21:39:36 -08008114 BUG_ON(mddev->ro == 1);
8115 if (mddev->ro == 2) {
8116 /* need to switch to read/write */
8117 mddev->ro = 0;
8118 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8119 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08008120 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008121 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08008122 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008123 rcu_read_lock();
8124 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11008125 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07008126 if (mddev->safemode == 1)
8127 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11008128 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
NeilBrown81fe48e2017-08-08 16:56:36 +10008129 if (mddev->in_sync || mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11008130 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07008131 if (mddev->in_sync) {
8132 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008133 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8134 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07008135 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008136 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07008137 }
NeilBrown85572d72014-12-15 12:56:56 +11008138 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07008139 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008140 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10008141 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008142 sysfs_notify_dirent_safe(mddev->sysfs_state);
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008143 if (!mddev->has_superblocks)
8144 return true;
NeilBrown09a44cc2008-05-23 13:04:36 -07008145 wait_event(mddev->sb_wait,
NeilBrownd47c8ad2017-10-05 16:23:16 +11008146 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8147 mddev->suspended);
NeilBrowncc27b0c2017-06-05 16:49:39 +10008148 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8149 percpu_ref_put(&mddev->writes_pending);
8150 return false;
8151 }
8152 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008153}
NeilBrown6c144d32014-09-30 16:15:38 +10008154EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155
NeilBrown49728052017-03-15 14:05:12 +11008156/* md_write_inc can only be called when md_write_start() has
8157 * already been called at least once of the current request.
8158 * It increments the counter and is useful when a single request
8159 * is split into several parts. Each part causes an increment and
8160 * so needs a matching md_write_end().
8161 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8162 * a spinlocked region.
8163 */
8164void md_write_inc(struct mddev *mddev, struct bio *bi)
8165{
8166 if (bio_data_dir(bi) != WRITE)
8167 return;
8168 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008169 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008170}
8171EXPORT_SYMBOL(md_write_inc);
8172
NeilBrownfd01b882011-10-11 16:47:53 +11008173void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008174{
NeilBrown4ad23a972017-03-15 14:05:14 +11008175 percpu_ref_put(&mddev->writes_pending);
8176
8177 if (mddev->safemode == 2)
8178 md_wakeup_thread(mddev->thread);
8179 else if (mddev->safemode_delay)
8180 /* The roundup() ensures this only performs locking once
8181 * every ->safemode_delay jiffies
8182 */
8183 mod_timer(&mddev->safemode_timer,
8184 roundup(jiffies, mddev->safemode_delay) +
8185 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008186}
NeilBrown4ad23a972017-03-15 14:05:14 +11008187
NeilBrown6c144d32014-09-30 16:15:38 +10008188EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189
NeilBrown2a2275d2007-01-26 00:57:11 -08008190/* md_allow_write(mddev)
8191 * Calling this ensures that the array is marked 'active' so that writes
8192 * may proceed without blocking. It is important to call this before
8193 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8194 * Must be called with mddev_lock held.
8195 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008196void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008197{
8198 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008199 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008200 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008201 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008202 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008203 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008204
NeilBrown85572d72014-12-15 12:56:56 +11008205 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008206 if (mddev->in_sync) {
8207 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008208 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8209 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008210 if (mddev->safemode_delay &&
8211 mddev->safemode == 0)
8212 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008213 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008214 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008215 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008216 /* wait for the dirty state to be recorded in the metadata */
8217 wait_event(mddev->sb_wait,
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008218 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008219 } else
NeilBrown85572d72014-12-15 12:56:56 +11008220 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008221}
8222EXPORT_SYMBOL_GPL(md_allow_write);
8223
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224#define SYNC_MARKS 10
8225#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008226#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008227void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008228{
Shaohua Li4ed87312012-10-11 13:34:00 +11008229 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008230 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008231 unsigned int currspeed = 0,
8232 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008233 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008234 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008235 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008236 sector_t mark_cnt[SYNC_MARKS];
8237 int last_mark,m;
8238 struct list_head *tmp;
8239 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008240 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008241 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008242 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008243 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008244 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008245
8246 /* just incase thread restarts... */
Song Liud5d885f2017-11-19 22:17:01 -08008247 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8248 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249 return;
NeilBrown3991b312014-05-28 13:39:23 +10008250 if (mddev->ro) {/* never try to sync a read-only array */
8251 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008252 return;
NeilBrown3991b312014-05-28 13:39:23 +10008253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008254
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008255 if (mddev_is_clustered(mddev)) {
8256 ret = md_cluster_ops->resync_start(mddev);
8257 if (ret)
8258 goto skip;
8259
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008260 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008261 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8262 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8263 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8264 && ((unsigned long long)mddev->curr_resync_completed
8265 < (unsigned long long)mddev->resync_max_sectors))
8266 goto skip;
8267 }
8268
NeilBrown61df9d92006-10-03 01:15:57 -07008269 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008270 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008271 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008272 action = "check";
8273 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008274 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008275 action = "repair";
8276 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008277 desc = "resync";
8278 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8279 desc = "reshape";
8280 else
8281 desc = "recovery";
8282
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008283 mddev->last_sync_action = action ?: desc;
8284
Linus Torvalds1da177e2005-04-16 15:20:36 -07008285 /* we overload curr_resync somewhat here.
8286 * 0 == not engaged in resync at all
8287 * 2 == checking that there is no conflict with another sync
8288 * 1 == like 2, but have yielded to allow conflicting resync to
8289 * commense
8290 * other == active in resync - this many blocks
8291 *
8292 * Before starting a resync we must have set curr_resync to
8293 * 2, and then checked that every "conflicting" array has curr_resync
8294 * less than ours. When we find one that is the same or higher
8295 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8296 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8297 * This will mean we have to start checking from the beginning again.
8298 *
8299 */
8300
8301 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008302 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008303 mddev->curr_resync = 2;
8304
8305 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008306 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008307 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008308 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008309 if (mddev2 == mddev)
8310 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008311 if (!mddev->parallel_resync
8312 && mddev2->curr_resync
8313 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008314 DEFINE_WAIT(wq);
8315 if (mddev < mddev2 && mddev->curr_resync == 2) {
8316 /* arbitrarily yield */
8317 mddev->curr_resync = 1;
8318 wake_up(&resync_wait);
8319 }
8320 if (mddev > mddev2 && mddev->curr_resync == 1)
8321 /* no need to wait here, we can wait the next
8322 * time 'round when curr_resync == 2
8323 */
8324 continue;
NeilBrown97441972008-09-19 11:49:54 +10008325 /* We need to wait 'interruptible' so as not to
8326 * contribute to the load average, and not to
8327 * be caught by 'softlockup'
8328 */
8329 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008330 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008331 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008332 if (mddev2_minor != mddev2->md_minor) {
8333 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008334 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8335 desc, mdname(mddev),
8336 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008339 if (signal_pending(current))
8340 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341 schedule();
8342 finish_wait(&resync_wait, &wq);
8343 goto try_again;
8344 }
8345 finish_wait(&resync_wait, &wq);
8346 }
8347 }
8348 } while (mddev->curr_resync < 2);
8349
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008350 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008351 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008352 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008353 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008354 */
8355 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008356 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008357 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008358 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8359 j = mddev->resync_min;
8360 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008361 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008362
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008363 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
NeilBrownc804cde2012-05-21 09:28:33 +10008364 max_sectors = mddev->resync_max_sectors;
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008365 /*
8366 * If the original node aborts reshaping then we continue the
8367 * reshaping, so set j again to avoid restart reshape from the
8368 * first beginning
8369 */
8370 if (mddev_is_clustered(mddev) &&
8371 mddev->reshape_position != MaxSector)
8372 j = mddev->reshape_position;
8373 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008375 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008376 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008377 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008378 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008379 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008380 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008381 !test_bit(Faulty, &rdev->flags) &&
8382 !test_bit(In_sync, &rdev->flags) &&
8383 rdev->recovery_offset < j)
8384 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008385 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008386
8387 /* If there is a bitmap, we need to make sure all
8388 * writes that started before we added a spare
8389 * complete before we start doing a recovery.
8390 * Otherwise the write might complete and (via
8391 * bitmap_endwrite) set a bit in the bitmap after the
8392 * recovery has checked that bit and skipped that
8393 * region.
8394 */
8395 if (mddev->bitmap) {
8396 mddev->pers->quiesce(mddev, 1);
8397 mddev->pers->quiesce(mddev, 0);
8398 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008400
NeilBrown9d487392016-11-02 14:16:49 +11008401 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8402 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8403 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8404 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008405
NeilBrowneea1bf32009-03-31 14:27:02 +11008406 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008407
NeilBrown57afd892005-06-21 17:17:13 -07008408 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008409 for (m = 0; m < SYNC_MARKS; m++) {
8410 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008411 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008412 }
8413 last_mark = 0;
8414 mddev->resync_mark = mark[last_mark];
8415 mddev->resync_mark_cnt = mark_cnt[last_mark];
8416
8417 /*
8418 * Tune reconstruction:
8419 */
8420 window = 32*(PAGE_SIZE/512);
NeilBrown9d487392016-11-02 14:16:49 +11008421 pr_debug("md: using %dk window, over a total of %lluk.\n",
8422 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008423
8424 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008425 last_check = 0;
8426
8427 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008428 pr_debug("md: resuming %s of %s from checkpoint.\n",
8429 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008430 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008431 } else
8432 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008433 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008434 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8435 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008436 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008437
majianpeng7c2c57c2012-07-03 12:12:26 +10008438 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008440 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008441
NeilBrown57afd892005-06-21 17:17:13 -07008442 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008443
NeilBrown7a91ee12009-05-26 12:57:21 +10008444 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8445 ((mddev->curr_resync > mddev->curr_resync_completed &&
8446 (mddev->curr_resync - mddev->curr_resync_completed)
8447 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008448 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008449 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008450 >= mddev->resync_max - mddev->curr_resync_completed ||
8451 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008452 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008453 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008454 wait_event(mddev->recovery_wait,
8455 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008456 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008457 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8458 j > mddev->recovery_cp)
8459 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008460 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008461 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008462 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008463 }
NeilBrownacb180b2009-04-14 16:28:34 +10008464
NeilBrownc91abf52013-11-19 12:02:01 +11008465 while (j >= mddev->resync_max &&
8466 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008467 /* As this condition is controlled by user-space,
8468 * we can block indefinitely, so use '_interruptible'
8469 * to avoid triggering warnings.
8470 */
8471 flush_signals(current); /* just in case */
8472 wait_event_interruptible(mddev->recovery_wait,
8473 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008474 || test_bit(MD_RECOVERY_INTR,
8475 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008476 }
NeilBrownacb180b2009-04-14 16:28:34 +10008477
NeilBrownc91abf52013-11-19 12:02:01 +11008478 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8479 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008480
NeilBrown09314792015-02-19 16:04:40 +11008481 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008482 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008483 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008484 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485 }
NeilBrown57afd892005-06-21 17:17:13 -07008486
8487 if (!skipped) { /* actual IO requested */
8488 io_sectors += sectors;
8489 atomic_add(sectors, &mddev->recovery_active);
8490 }
8491
NeilBrowne875ece2011-07-28 11:39:24 +10008492 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8493 break;
8494
Linus Torvalds1da177e2005-04-16 15:20:36 -07008495 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008496 if (j > max_sectors)
8497 /* when skipping, extra large numbers can be returned. */
8498 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008499 if (j > 2)
8500 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008501 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008502 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008503 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008504 * visible in /proc/mdstat
8505 */
8506 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008507
8508 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008509 continue;
8510
NeilBrown57afd892005-06-21 17:17:13 -07008511 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008512 repeat:
8513 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8514 /* step marks */
8515 int next = (last_mark+1) % SYNC_MARKS;
8516
8517 mddev->resync_mark = mark[next];
8518 mddev->resync_mark_cnt = mark_cnt[next];
8519 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008520 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008521 last_mark = next;
8522 }
8523
NeilBrownc91abf52013-11-19 12:02:01 +11008524 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8525 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008526
8527 /*
8528 * this loop exits only if either when we are slower than
8529 * the 'hard' speed limit, or the system was IO-idle for
8530 * a jiffy.
8531 * the system might be non-idle CPU-wise, but we only care
8532 * about not overloading the IO subsystem. (things like an
8533 * e2fsck being done on the RAID array should execute fast)
8534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008535 cond_resched();
8536
Xiao Niac7e50a2014-08-07 09:37:41 -04008537 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8538 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008539 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540
NeilBrown88202a02006-01-06 00:21:36 -08008541 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008542 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008543 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008544 goto repeat;
8545 }
NeilBrownac8fa412015-02-19 16:55:00 +11008546 if (!is_mddev_idle(mddev, 0)) {
8547 /*
8548 * Give other IO more of a chance.
8549 * The faster the devices, the less we wait.
8550 */
8551 wait_event(mddev->recovery_wait,
8552 !atomic_read(&mddev->recovery_active));
8553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008554 }
8555 }
NeilBrown9d487392016-11-02 14:16:49 +11008556 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8557 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8558 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008559 /*
8560 * this also signals 'finished resyncing' to md_stop
8561 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008562 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008563 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8564
NeilBrown5ed1df22015-07-24 13:27:08 +10008565 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8566 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008567 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008568 mddev->curr_resync_completed = mddev->curr_resync;
8569 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8570 }
NeilBrown09314792015-02-19 16:04:40 +11008571 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008572
NeilBrowndfc70642008-05-23 13:04:39 -07008573 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008574 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008575 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8576 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8577 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008578 pr_debug("md: checkpointing %s of %s.\n",
8579 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008580 if (test_bit(MD_RECOVERY_ERROR,
8581 &mddev->recovery))
8582 mddev->recovery_cp =
8583 mddev->curr_resync_completed;
8584 else
8585 mddev->recovery_cp =
8586 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008587 }
8588 } else
8589 mddev->recovery_cp = MaxSector;
8590 } else {
8591 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8592 mddev->curr_resync = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11008593 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8594 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
8595 rcu_read_lock();
8596 rdev_for_each_rcu(rdev, mddev)
8597 if (rdev->raid_disk >= 0 &&
8598 mddev->delta_disks >= 0 &&
8599 !test_bit(Journal, &rdev->flags) &&
8600 !test_bit(Faulty, &rdev->flags) &&
8601 !test_bit(In_sync, &rdev->flags) &&
8602 rdev->recovery_offset < mddev->curr_resync)
8603 rdev->recovery_offset = mddev->curr_resync;
8604 rcu_read_unlock();
8605 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008607 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008608 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008609 /* set CHANGE_PENDING here since maybe another update is needed,
8610 * so other nodes are informed. It should be harmless for normal
8611 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008612 set_mask_bits(&mddev->sb_flags, 0,
8613 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008614
BingJing Chang88763912018-02-22 13:34:46 +08008615 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8616 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8617 mddev->delta_disks > 0 &&
8618 mddev->pers->finish_reshape &&
8619 mddev->pers->size &&
8620 mddev->queue) {
8621 mddev_lock_nointr(mddev);
8622 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
8623 mddev_unlock(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008624 if (!mddev_is_clustered(mddev)) {
8625 set_capacity(mddev->gendisk, mddev->array_sectors);
8626 revalidate_disk(mddev->gendisk);
8627 }
BingJing Chang88763912018-02-22 13:34:46 +08008628 }
8629
NeilBrown23da4222014-12-15 12:57:01 +11008630 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008631 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8632 /* We completed so min/max setting can be forgotten if used. */
8633 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8634 mddev->resync_min = 0;
8635 mddev->resync_max = MaxSector;
8636 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8637 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008638 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008640 spin_unlock(&mddev->lock);
8641
Linus Torvalds1da177e2005-04-16 15:20:36 -07008642 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008643 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008644 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645}
NeilBrown29269552006-03-27 01:18:10 -08008646EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647
NeilBrown746d3202013-04-24 11:42:41 +10008648static int remove_and_add_spares(struct mddev *mddev,
8649 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008650{
NeilBrown3cb03002011-10-11 16:45:26 +11008651 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008652 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008653 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008654 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008655
NeilBrown39772f02018-02-03 09:19:30 +11008656 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
8657 /* Mustn't remove devices when resync thread is running */
8658 return 0;
8659
NeilBrownd787be42016-06-02 16:19:53 +10008660 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008661 if ((this == NULL || rdev == this) &&
8662 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008663 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008664 test_bit(Faulty, &rdev->flags) &&
8665 atomic_read(&rdev->nr_pending)==0) {
8666 /* Faulty non-Blocked devices with nr_pending == 0
8667 * never get nr_pending incremented,
8668 * never get Faulty cleared, and never get Blocked set.
8669 * So we can synchronize_rcu now rather than once per device
8670 */
8671 remove_some = true;
8672 set_bit(RemoveSynchronized, &rdev->flags);
8673 }
8674 }
8675
8676 if (remove_some)
8677 synchronize_rcu();
8678 rdev_for_each(rdev, mddev) {
8679 if ((this == NULL || rdev == this) &&
8680 rdev->raid_disk >= 0 &&
8681 !test_bit(Blocked, &rdev->flags) &&
8682 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008683 (!test_bit(In_sync, &rdev->flags) &&
8684 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008685 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008686 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008687 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008688 sysfs_unlink_rdev(mddev, rdev);
NeilBrown011abdc2018-04-26 14:46:29 +10008689 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008690 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008691 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008692 }
8693 }
NeilBrownd787be42016-06-02 16:19:53 +10008694 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8695 clear_bit(RemoveSynchronized, &rdev->flags);
8696 }
8697
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008698 if (removed && mddev->kobj.sd)
8699 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008700
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008701 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008702 goto no_add;
8703
NeilBrowndafb20f2012-03-19 12:46:39 +11008704 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008705 if (this && this != rdev)
8706 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008707 if (test_bit(Candidate, &rdev->flags))
8708 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008709 if (rdev->raid_disk >= 0 &&
8710 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008711 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008712 !test_bit(Faulty, &rdev->flags))
8713 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008714 if (rdev->raid_disk >= 0)
8715 continue;
8716 if (test_bit(Faulty, &rdev->flags))
8717 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008718 if (!test_bit(Journal, &rdev->flags)) {
8719 if (mddev->ro &&
8720 ! (rdev->saved_raid_disk >= 0 &&
8721 !test_bit(Bitmap_sync, &rdev->flags)))
8722 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008723
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008724 rdev->recovery_offset = 0;
8725 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008726 if (mddev->pers->
8727 hot_add_disk(mddev, rdev) == 0) {
8728 if (sysfs_link_rdev(mddev, rdev))
8729 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008730 if (!test_bit(Journal, &rdev->flags))
8731 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008732 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08008733 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008734 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008735 }
NeilBrown746d3202013-04-24 11:42:41 +10008736no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008737 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08008738 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008739 return spares;
8740}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008741
NeilBrownac05f252014-09-30 08:10:42 +10008742static void md_start_sync(struct work_struct *ws)
8743{
8744 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008745
NeilBrownac05f252014-09-30 08:10:42 +10008746 mddev->sync_thread = md_register_thread(md_do_sync,
8747 mddev,
8748 "resync");
8749 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11008750 pr_warn("%s: could not start resync thread...\n",
8751 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008752 /* leave the spares where they are, it shouldn't hurt */
8753 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8754 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8755 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8756 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8757 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008758 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008759 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8760 &mddev->recovery))
8761 if (mddev->sysfs_action)
8762 sysfs_notify_dirent_safe(mddev->sysfs_action);
8763 } else
8764 md_wakeup_thread(mddev->sync_thread);
8765 sysfs_notify_dirent_safe(mddev->sysfs_action);
8766 md_new_event(mddev);
8767}
8768
Linus Torvalds1da177e2005-04-16 15:20:36 -07008769/*
8770 * This routine is regularly called by all per-raid-array threads to
8771 * deal with generic issues like resync and super-block update.
8772 * Raid personalities that don't have a thread (linear/raid0) do not
8773 * need this as they never do any recovery or update the superblock.
8774 *
8775 * It does not do any resync itself, but rather "forks" off other threads
8776 * to do that as needed.
8777 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8778 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008779 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008780 * and wakeups up this thread which will reap the thread and finish up.
8781 * This thread also removes any faulty devices (with nr_pending == 0).
8782 *
8783 * The overall approach is:
8784 * 1/ if the superblock needs updating, update it.
8785 * 2/ If a recovery thread is running, don't do anything else.
8786 * 3/ If recovery has finished, clean up, possibly marking spares active.
8787 * 4/ If there are any faulty devices, remove them.
8788 * 5/ If array is degraded, try to add spares devices
8789 * 6/ If array has spares or is not in-sync, start a resync thread.
8790 */
NeilBrownfd01b882011-10-11 16:47:53 +11008791void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008792{
NeilBrown059421e2018-10-03 15:04:41 +10008793 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
8794 /* Write superblock - thread that called mddev_suspend()
8795 * holds reconfig_mutex for us.
8796 */
8797 set_bit(MD_UPDATING_SB, &mddev->flags);
8798 smp_mb__after_atomic();
8799 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
8800 md_update_sb(mddev, 0);
8801 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
8802 wake_up(&mddev->sb_wait);
8803 }
8804
Jonathan Brassow68866e42011-06-08 15:10:08 +10008805 if (mddev->suspended)
8806 return;
8807
NeilBrown5f404022005-06-21 17:17:16 -07008808 if (mddev->bitmap)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008809 md_bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008810
NeilBrownfca4d842005-06-21 17:17:11 -07008811 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008812 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11008813 pr_debug("md: %s in immediate safe mode\n",
8814 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07008815 mddev->safemode = 2;
8816 }
8817 flush_signals(current);
8818 }
8819
NeilBrownc89a8ee2008-08-05 15:54:13 +10008820 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8821 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008822 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08008823 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008824 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008825 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008826 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11008827 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07008828 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008829 ))
8830 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008831
NeilBrowndf5b89b2006-03-27 01:18:20 -08008832 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008833 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008834
Shaohua Liafc1f552017-08-11 20:34:45 -07008835 if (!mddev->external && mddev->safemode == 1)
NeilBrown33182d12017-08-08 16:56:36 +10008836 mddev->safemode = 0;
8837
NeilBrownc89a8ee2008-08-05 15:54:13 +10008838 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008839 struct md_rdev *rdev;
8840 if (!mddev->external && mddev->in_sync)
8841 /* 'Blocked' flag not needed as failed devices
8842 * will be recorded if array switched to read/write.
8843 * Leaving it set will prevent the device
8844 * from being removed.
8845 */
8846 rdev_for_each(rdev, mddev)
8847 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008848 /* On a read-only array we can:
8849 * - remove failed devices
8850 * - add already-in_sync devices if the array itself
8851 * is in-sync.
8852 * As we only add devices that are already in-sync,
8853 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008854 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008855 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008856 /* There is no thread, but we need to call
8857 * ->spare_active and clear saved_raid_disk
8858 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008859 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008860 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008861 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008862 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08008863 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008864 goto unlock;
8865 }
8866
Guoqing Jiang659b2542015-12-21 10:50:59 +11008867 if (mddev_is_clustered(mddev)) {
8868 struct md_rdev *rdev;
8869 /* kick the device if another node issued a
8870 * remove disk.
8871 */
8872 rdev_for_each(rdev, mddev) {
8873 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8874 rdev->raid_disk < 0)
8875 md_kick_rdev_from_array(rdev);
8876 }
8877 }
8878
NeilBrown6497709b2017-03-15 14:05:14 +11008879 if (!mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11008880 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11008881 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11008882 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07008883 }
NeilBrownfca4d842005-06-21 17:17:11 -07008884
Shaohua Li29530792016-12-08 15:48:19 -08008885 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07008886 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008887
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8889 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8890 /* resync/recovery still happening */
8891 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8892 goto unlock;
8893 }
8894 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008895 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008896 goto unlock;
8897 }
Neil Brown72a23c22008-06-28 08:31:41 +10008898 /* Set RUNNING before clearing NEEDED to avoid
8899 * any transients in the value of "sync_action".
8900 */
NeilBrown72f36d52012-10-11 14:25:57 +11008901 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008902 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008903 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008904 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008905 /* Clear some bits that don't mean anything, but
8906 * might be left set
8907 */
NeilBrown24dd4692005-11-08 21:39:26 -08008908 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8909 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008910
NeilBrowned209582012-04-24 10:23:14 +10008911 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8912 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008913 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008914 /* no recovery is running.
8915 * remove any failed drives, then
8916 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008917 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008918 * the personality to fail the re-add.
8919 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008920
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008921 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008922 if (mddev->pers->check_reshape == NULL ||
8923 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008924 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008925 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008926 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008927 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008928 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008929 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8930 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008931 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008932 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008933 } else if (mddev->recovery_cp < MaxSector) {
8934 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008935 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008936 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8937 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008938 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008939
Linus Torvalds1da177e2005-04-16 15:20:36 -07008940 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008941 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008942 /* We are adding a device or devices to an array
8943 * which has the bitmap stored on all devices.
8944 * So make sure all bitmap pages get written
8945 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008946 md_bitmap_write_all(mddev->bitmap);
NeilBrowna654b9d82005-06-21 17:17:27 -07008947 }
NeilBrownac05f252014-09-30 08:10:42 +10008948 INIT_WORK(&mddev->del_work, md_start_sync);
8949 queue_work(md_misc_wq, &mddev->del_work);
8950 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008951 }
NeilBrownac05f252014-09-30 08:10:42 +10008952 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008953 if (!mddev->sync_thread) {
8954 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008955 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008956 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8957 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008958 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008959 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008960 }
NeilBrownac05f252014-09-30 08:10:42 +10008961 unlock:
8962 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008963 mddev_unlock(mddev);
8964 }
8965}
NeilBrown6c144d32014-09-30 16:15:38 +10008966EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008967
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008968void md_reap_sync_thread(struct mddev *mddev)
8969{
8970 struct md_rdev *rdev;
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008971 sector_t old_dev_sectors = mddev->dev_sectors;
8972 bool is_reshaped = false;
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008973
8974 /* resync has finished, collect result */
8975 md_unregister_thread(&mddev->sync_thread);
8976 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8977 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8978 /* success...*/
8979 /* activate any spares */
8980 if (mddev->pers->spare_active(mddev)) {
8981 sysfs_notify(&mddev->kobj, NULL,
8982 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08008983 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008984 }
8985 }
8986 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008987 mddev->pers->finish_reshape) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008988 mddev->pers->finish_reshape(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008989 if (mddev_is_clustered(mddev))
8990 is_reshaped = true;
8991 }
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008992
8993 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008994 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008995 */
NeilBrownf4667222013-12-09 12:04:56 +11008996 if (!mddev->degraded)
8997 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008998 rdev->saved_raid_disk = -1;
8999
9000 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08009001 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04009002 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9003 * clustered raid */
9004 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9005 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009006 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10009007 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009008 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9009 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9010 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9011 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009012 /*
9013 * We call md_cluster_ops->update_size here because sync_size could
9014 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9015 * so it is time to update size across cluster.
9016 */
9017 if (mddev_is_clustered(mddev) && is_reshaped
9018 && !test_bit(MD_CLOSING, &mddev->flags))
9019 md_cluster_ops->update_size(mddev, old_dev_sectors);
NeilBrownf851b602014-12-11 10:02:10 +11009020 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009021 /* flag recovery needed just to double check */
9022 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9023 sysfs_notify_dirent_safe(mddev->sysfs_action);
9024 md_new_event(mddev);
9025 if (mddev->event_work.func)
9026 queue_work(md_misc_wq, &mddev->event_work);
9027}
NeilBrown6c144d32014-09-30 16:15:38 +10009028EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009029
NeilBrownfd01b882011-10-11 16:47:53 +11009030void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07009031{
NeilBrown00bcb4a2010-06-01 19:37:23 +10009032 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07009033 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10009034 !test_bit(Blocked, &rdev->flags) &&
9035 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07009036 msecs_to_jiffies(5000));
9037 rdev_dec_pending(rdev, mddev);
9038}
9039EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9040
NeilBrownc6563a82012-05-21 09:27:00 +10009041void md_finish_reshape(struct mddev *mddev)
9042{
9043 /* called be personality module when reshape completes. */
9044 struct md_rdev *rdev;
9045
9046 rdev_for_each(rdev, mddev) {
9047 if (rdev->data_offset > rdev->new_data_offset)
9048 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9049 else
9050 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9051 rdev->data_offset = rdev->new_data_offset;
9052 }
9053}
9054EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10009055
Vishal Vermafc974ee2015-12-24 19:20:34 -07009056/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10009057
Vishal Vermafc974ee2015-12-24 19:20:34 -07009058/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11009059int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10009060 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009061{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04009062 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10009063 int rv;
9064 if (is_new)
9065 s += rdev->new_data_offset;
9066 else
9067 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07009068 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9069 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10009070 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009071 if (test_bit(ExternalBbl, &rdev->flags))
9072 sysfs_notify(&rdev->kobj, NULL,
9073 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11009074 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08009075 set_mask_bits(&mddev->sb_flags, 0,
9076 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10009077 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07009078 return 1;
9079 } else
9080 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10009081}
9082EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9083
NeilBrownc6563a82012-05-21 09:27:00 +10009084int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9085 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009086{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009087 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10009088 if (is_new)
9089 s += rdev->new_data_offset;
9090 else
9091 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009092 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9093 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
9094 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
9095 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10009096}
9097EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9098
Adrian Bunk75c96f82005-05-05 16:16:09 -07009099static int md_notify_reboot(struct notifier_block *this,
9100 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009101{
9102 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009103 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009104 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009105
NeilBrownc744a652012-03-19 12:46:37 +11009106 for_each_mddev(mddev, tmp) {
9107 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009108 if (mddev->pers)
9109 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009110 if (mddev->persistent)
9111 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009112 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009113 }
NeilBrownc744a652012-03-19 12:46:37 +11009114 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009115 }
NeilBrownc744a652012-03-19 12:46:37 +11009116 /*
9117 * certain more exotic SCSI devices are known to be
9118 * volatile wrt too early system reboots. While the
9119 * right place to handle this issue is the given
9120 * driver, we do want to have a safe RAID driver ...
9121 */
9122 if (need_delay)
9123 mdelay(1000*1);
9124
Linus Torvalds1da177e2005-04-16 15:20:36 -07009125 return NOTIFY_DONE;
9126}
9127
Adrian Bunk75c96f82005-05-05 16:16:09 -07009128static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009129 .notifier_call = md_notify_reboot,
9130 .next = NULL,
9131 .priority = INT_MAX, /* before any real devices */
9132};
9133
9134static void md_geninit(void)
9135{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009136 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009137
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07009138 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009139}
9140
Adrian Bunk75c96f82005-05-05 16:16:09 -07009141static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009142{
Tejun Heoe804ac72010-10-15 15:36:08 +02009143 int ret = -ENOMEM;
9144
Tejun Heoada609e2011-01-25 14:35:54 +01009145 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009146 if (!md_wq)
9147 goto err_wq;
9148
9149 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9150 if (!md_misc_wq)
9151 goto err_misc_wq;
9152
9153 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9154 goto err_md;
9155
9156 if ((ret = register_blkdev(0, "mdp")) < 0)
9157 goto err_mdp;
9158 mdp_major = ret;
9159
NeilBrownaf5628f2014-07-31 13:54:54 +10009160 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009161 md_probe, NULL, NULL);
9162 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009163 md_probe, NULL, NULL);
9164
Linus Torvalds1da177e2005-04-16 15:20:36 -07009165 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009166 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009167
9168 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009170
Tejun Heoe804ac72010-10-15 15:36:08 +02009171err_mdp:
9172 unregister_blkdev(MD_MAJOR, "md");
9173err_md:
9174 destroy_workqueue(md_misc_wq);
9175err_misc_wq:
9176 destroy_workqueue(md_wq);
9177err_wq:
9178 return ret;
9179}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009180
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009181static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009182{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009183 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9184 struct md_rdev *rdev2;
9185 int role, ret;
9186 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009187
Guoqing Jiang818da592017-03-01 16:42:40 +08009188 /*
9189 * If size is changed in another node then we need to
9190 * do resize as well.
9191 */
9192 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9193 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9194 if (ret)
9195 pr_info("md-cluster: resize failed\n");
9196 else
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009197 md_bitmap_update_sb(mddev->bitmap);
Guoqing Jiang818da592017-03-01 16:42:40 +08009198 }
9199
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009200 /* Check for change of roles in the active devices */
9201 rdev_for_each(rdev2, mddev) {
9202 if (test_bit(Faulty, &rdev2->flags))
9203 continue;
9204
9205 /* Check if the roles changed */
9206 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009207
9208 if (test_bit(Candidate, &rdev2->flags)) {
9209 if (role == 0xfffe) {
9210 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9211 md_kick_rdev_from_array(rdev2);
9212 continue;
9213 }
9214 else
9215 clear_bit(Candidate, &rdev2->flags);
9216 }
9217
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009218 if (role != rdev2->raid_disk) {
Guoqing Jiangca1e98e2018-10-18 16:37:45 +08009219 /*
9220 * got activated except reshape is happening.
9221 */
9222 if (rdev2->raid_disk == -1 && role != 0xffff &&
9223 !(le32_to_cpu(sb->feature_map) &
9224 MD_FEATURE_RESHAPE_ACTIVE)) {
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009225 rdev2->saved_raid_disk = role;
9226 ret = remove_and_add_spares(mddev, rdev2);
9227 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009228 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009229 /* wakeup mddev->thread here, so array could
9230 * perform resync with the new activated disk */
9231 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9232 md_wakeup_thread(mddev->thread);
9233
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009234 }
9235 /* device faulty
9236 * We just want to do the minimum to mark the disk
9237 * as faulty. The recovery is performed by the
9238 * one who initiated the error.
9239 */
9240 if ((role == 0xfffe) || (role == 0xfffd)) {
9241 md_error(mddev, rdev2);
9242 clear_bit(Blocked, &rdev2->flags);
9243 }
9244 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009245 }
9246
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009247 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9248 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009249
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009250 /*
9251 * Since mddev->delta_disks has already updated in update_raid_disks,
9252 * so it is time to check reshape.
9253 */
9254 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9255 (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9256 /*
9257 * reshape is happening in the remote node, we need to
9258 * update reshape_position and call start_reshape.
9259 */
9260 mddev->reshape_position = sb->reshape_position;
9261 if (mddev->pers->update_reshape_pos)
9262 mddev->pers->update_reshape_pos(mddev);
9263 if (mddev->pers->start_reshape)
9264 mddev->pers->start_reshape(mddev);
9265 } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9266 mddev->reshape_position != MaxSector &&
9267 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9268 /* reshape is just done in another node. */
9269 mddev->reshape_position = MaxSector;
9270 if (mddev->pers->update_reshape_pos)
9271 mddev->pers->update_reshape_pos(mddev);
9272 }
9273
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009274 /* Finally set the event to be up to date */
9275 mddev->events = le64_to_cpu(sb->events);
9276}
9277
9278static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9279{
9280 int err;
9281 struct page *swapout = rdev->sb_page;
9282 struct mdp_superblock_1 *sb;
9283
9284 /* Store the sb page of the rdev in the swapout temporary
9285 * variable in case we err in the future
9286 */
9287 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009288 err = alloc_disk_sb(rdev);
9289 if (err == 0) {
9290 ClearPageUptodate(rdev->sb_page);
9291 rdev->sb_loaded = 0;
9292 err = super_types[mddev->major_version].
9293 load_super(rdev, NULL, mddev->minor_version);
9294 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009295 if (err < 0) {
9296 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9297 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009298 if (rdev->sb_page)
9299 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009300 rdev->sb_page = swapout;
9301 rdev->sb_loaded = 1;
9302 return err;
9303 }
9304
9305 sb = page_address(rdev->sb_page);
9306 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9307 * is not set
9308 */
9309
9310 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9311 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9312
9313 /* The other node finished recovery, call spare_active to set
9314 * device In_sync and mddev->degraded
9315 */
9316 if (rdev->recovery_offset == MaxSector &&
9317 !test_bit(In_sync, &rdev->flags) &&
9318 mddev->pers->spare_active(mddev))
9319 sysfs_notify(&mddev->kobj, NULL, "degraded");
9320
9321 put_page(swapout);
9322 return 0;
9323}
9324
9325void md_reload_sb(struct mddev *mddev, int nr)
9326{
9327 struct md_rdev *rdev;
9328 int err;
9329
9330 /* Find the rdev */
9331 rdev_for_each_rcu(rdev, mddev) {
9332 if (rdev->desc_nr == nr)
9333 break;
9334 }
9335
9336 if (!rdev || rdev->desc_nr != nr) {
9337 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9338 return;
9339 }
9340
9341 err = read_rdev(mddev, rdev);
9342 if (err < 0)
9343 return;
9344
9345 check_sb_changes(mddev, rdev);
9346
9347 /* Read all rdev's to update recovery_offset */
Guoqing Jiang0ea99242018-04-09 17:01:21 +08009348 rdev_for_each_rcu(rdev, mddev) {
9349 if (!test_bit(Faulty, &rdev->flags))
9350 read_rdev(mddev, rdev);
9351 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009352}
9353EXPORT_SYMBOL(md_reload_sb);
9354
Linus Torvalds1da177e2005-04-16 15:20:36 -07009355#ifndef MODULE
9356
9357/*
9358 * Searches all registered partitions for autorun RAID arrays
9359 * at boot time.
9360 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009361
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009362static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009363static LIST_HEAD(all_detected_devices);
9364struct detected_devices_node {
9365 struct list_head list;
9366 dev_t dev;
9367};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009368
9369void md_autodetect_dev(dev_t dev)
9370{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009371 struct detected_devices_node *node_detected_dev;
9372
9373 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9374 if (node_detected_dev) {
9375 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009376 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009377 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009378 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009380}
9381
Linus Torvalds1da177e2005-04-16 15:20:36 -07009382static void autostart_arrays(int part)
9383{
NeilBrown3cb03002011-10-11 16:45:26 +11009384 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009385 struct detected_devices_node *node_detected_dev;
9386 dev_t dev;
9387 int i_scanned, i_passed;
9388
9389 i_scanned = 0;
9390 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009391
NeilBrown9d487392016-11-02 14:16:49 +11009392 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009393
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009394 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009395 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9396 i_scanned++;
9397 node_detected_dev = list_entry(all_detected_devices.next,
9398 struct detected_devices_node, list);
9399 list_del(&node_detected_dev->list);
9400 dev = node_detected_dev->dev;
9401 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009402 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009403 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009404 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009405 if (IS_ERR(rdev))
9406 continue;
9407
NeilBrown403df472014-09-30 15:52:29 +10009408 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009409 continue;
NeilBrown403df472014-09-30 15:52:29 +10009410
NeilBrownd0fae182008-03-04 14:29:31 -08009411 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009412 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009413 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009414 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009415 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009416
NeilBrown9d487392016-11-02 14:16:49 +11009417 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009418
9419 autorun_devices(part);
9420}
9421
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009422#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009423
9424static __exit void md_exit(void)
9425{
NeilBrownfd01b882011-10-11 16:47:53 +11009426 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009427 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009428 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009429
NeilBrownaf5628f2014-07-31 13:54:54 +10009430 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009431 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009432
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009433 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009434 unregister_blkdev(mdp_major, "mdp");
9435 unregister_reboot_notifier(&md_notifier);
9436 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009437
9438 /* We cannot unload the modules while some process is
9439 * waiting for us in select() or poll() - wake them up
9440 */
9441 md_unloading = 1;
9442 while (waitqueue_active(&md_event_waiters)) {
9443 /* not safe to leave yet */
9444 wake_up(&md_event_waiters);
9445 msleep(delay);
9446 delay += delay;
9447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009448 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009449
NeilBrown29ac4aa2008-02-06 01:39:58 -08009450 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009451 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009452 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009453 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009454 /*
9455 * for_each_mddev() will call mddev_put() at the end of each
9456 * iteration. As the mddev is now fully clear, this will
9457 * schedule the mddev for destruction by a workqueue, and the
9458 * destroy_workqueue() below will wait for that to complete.
9459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009460 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009461 destroy_workqueue(md_misc_wq);
9462 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009463}
9464
Dan Williams685784a2007-07-09 11:56:42 -07009465subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009466module_exit(md_exit)
9467
Kees Cooke4dca7b2017-10-17 19:04:42 -07009468static int get_ro(char *buffer, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009469{
9470 return sprintf(buffer, "%d", start_readonly);
9471}
Kees Cooke4dca7b2017-10-17 19:04:42 -07009472static int set_ro(const char *val, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009473{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009474 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009475}
9476
NeilBrown80ca3a42006-07-10 04:44:18 -07009477module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9478module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009479module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009480module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009481
Linus Torvalds1da177e2005-04-16 15:20:36 -07009482MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009483MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009484MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009485MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);