blob: e07096c4ff20f73a31f344e460a6501aac99c3ba [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{
210 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100211
Kent Overstreetafeee512018-05-20 18:25:52 -0400212 if (!mddev || !bioset_initialized(&mddev->bio_set))
NeilBrowna167f662010-10-26 18:31:13 +1100213 return bio_alloc(gfp_mask, nr_iovecs);
214
Kent Overstreetafeee512018-05-20 18:25:52 -0400215 b = bio_alloc_bioset(gfp_mask, nr_iovecs, &mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100216 if (!b)
217 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100218 return b;
219}
220EXPORT_SYMBOL_GPL(bio_alloc_mddev);
221
NeilBrown5a850712017-06-21 09:12:21 +1000222static struct bio *md_bio_alloc_sync(struct mddev *mddev)
223{
Kent Overstreetafeee512018-05-20 18:25:52 -0400224 if (!mddev || !bioset_initialized(&mddev->sync_set))
NeilBrown5a850712017-06-21 09:12:21 +1000225 return bio_alloc(GFP_NOIO, 1);
226
Kent Overstreetafeee512018-05-20 18:25:52 -0400227 return bio_alloc_bioset(GFP_NOIO, 1, &mddev->sync_set);
NeilBrown5a850712017-06-21 09:12:21 +1000228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/*
NeilBrownd7603b72006-01-06 00:20:30 -0800231 * We have a system wide 'event count' that is incremented
232 * on any 'interesting' event, and readers of /proc/mdstat
233 * can use 'poll' or 'select' to find out when the event
234 * count increases.
235 *
236 * Events are:
237 * start array, stop array, error, add device, remove device,
238 * start build, activate spare
239 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800240static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800241static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100242void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800243{
244 atomic_inc(&md_event_count);
245 wake_up(&md_event_waiters);
246}
NeilBrown29269552006-03-27 01:18:10 -0800247EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800248
249/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 * Enables to iterate over all existing md arrays
251 * all_mddevs_lock protects this list.
252 */
253static LIST_HEAD(all_mddevs);
254static DEFINE_SPINLOCK(all_mddevs_lock);
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/*
257 * iterates through all used mddevs in the system.
258 * We take care to grab the all_mddevs_lock whenever navigating
259 * the list, and to always hold a refcount when unlocked.
260 * Any code which breaks out of this loop while own
261 * a reference to the current mddev and must mddev_put it.
262 */
NeilBrownfd01b882011-10-11 16:47:53 +1100263#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000265 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100266 _tmp = all_mddevs.next; \
267 _mddev = NULL;}); \
268 ({ if (_tmp != &all_mddevs) \
269 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100271 if (_mddev) mddev_put(_mddev); \
272 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
273 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100275 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 )
277
NeilBrown409c57f2009-03-31 14:39:39 +1100278/* Rather than calling directly into the personality make_request function,
279 * IO requests come here first so that we can check if the device is
280 * being suspended pending a reconfiguration.
281 * We hold a refcount over the call to ->make_request. By the time that
282 * call has finished, the bio has been linked into some internal structure
283 * and so is visible to ->quiesce(), so we don't need the refcount any more.
284 */
NeilBrownb3143b92017-10-17 13:46:43 +1100285static bool is_suspended(struct mddev *mddev, struct bio *bio)
286{
287 if (mddev->suspended)
288 return true;
289 if (bio_data_dir(bio) != WRITE)
290 return false;
291 if (mddev->suspend_lo >= mddev->suspend_hi)
292 return false;
293 if (bio->bi_iter.bi_sector >= mddev->suspend_hi)
294 return false;
295 if (bio_end_sector(bio) < mddev->suspend_lo)
296 return false;
297 return true;
298}
299
Shaohua Li393debc2017-09-21 10:23:35 -0700300void md_handle_request(struct mddev *mddev, struct bio *bio)
301{
302check_suspended:
303 rcu_read_lock();
NeilBrownb3143b92017-10-17 13:46:43 +1100304 if (is_suspended(mddev, bio)) {
Shaohua Li393debc2017-09-21 10:23:35 -0700305 DEFINE_WAIT(__wait);
306 for (;;) {
307 prepare_to_wait(&mddev->sb_wait, &__wait,
308 TASK_UNINTERRUPTIBLE);
NeilBrownb3143b92017-10-17 13:46:43 +1100309 if (!is_suspended(mddev, bio))
Shaohua Li393debc2017-09-21 10:23:35 -0700310 break;
311 rcu_read_unlock();
312 schedule();
313 rcu_read_lock();
314 }
315 finish_wait(&mddev->sb_wait, &__wait);
316 }
317 atomic_inc(&mddev->active_io);
318 rcu_read_unlock();
319
320 if (!mddev->pers->make_request(mddev, bio)) {
321 atomic_dec(&mddev->active_io);
322 wake_up(&mddev->sb_wait);
323 goto check_suspended;
324 }
325
326 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
327 wake_up(&mddev->sb_wait);
328}
329EXPORT_SYMBOL(md_handle_request);
330
Jens Axboedece1632015-11-05 10:41:16 -0700331static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
NeilBrown49077322010-03-25 16:20:56 +1100333 const int rw = bio_data_dir(bio);
Michael Callahanddcf35d2018-07-18 04:47:39 -0700334 const int sgrp = op_stat_group(bio_op(bio));
NeilBrownfd01b882011-10-11 16:47:53 +1100335 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500336 unsigned int sectors;
Gu Zheng74672d02015-04-03 08:44:47 +0800337 int cpu;
NeilBrown49077322010-03-25 16:20:56 +1100338
NeilBrownaf67c312017-06-18 14:38:57 +1000339 blk_queue_split(q, &bio);
Kent Overstreet54efd502015-04-23 22:37:18 -0700340
NeilBrown274d8cb2016-01-04 16:16:58 +1100341 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100342 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700343 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100344 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100345 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200346 if (bio_sectors(bio) != 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200347 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200348 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700349 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100350 }
NeilBrown49077322010-03-25 16:20:56 +1100351
Chris Masone91ece52011-02-07 19:21:48 -0500352 /*
353 * save the sectors now since our bio can
354 * go away inside make_request
355 */
356 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700357 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600358 bio->bi_opf &= ~REQ_NOMERGE;
Shaohua Li393debc2017-09-21 10:23:35 -0700359
360 md_handle_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100361
Gu Zheng74672d02015-04-03 08:44:47 +0800362 cpu = part_stat_lock();
Michael Callahanddcf35d2018-07-18 04:47:39 -0700363 part_stat_inc(cpu, &mddev->gendisk->part0, ios[sgrp]);
364 part_stat_add(cpu, &mddev->gendisk->part0, sectors[sgrp], sectors);
Gu Zheng74672d02015-04-03 08:44:47 +0800365 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100366
Jens Axboedece1632015-11-05 10:41:16 -0700367 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
NeilBrown9e35b992010-04-06 14:23:02 +1000370/* mddev_suspend makes sure no new requests are submitted
371 * to the device, and that any requests that have been submitted
372 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100373 * Once mddev_detach() is called and completes, the module will be
374 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000375 */
NeilBrownfd01b882011-10-11 16:47:53 +1100376void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100377{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200378 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
NeilBrown4d5324f2017-10-19 12:17:16 +1100379 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100380 if (mddev->suspended++)
381 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100382 synchronize_rcu();
NeilBrowncc27b0c2017-06-05 16:49:39 +1000383 wake_up(&mddev->sb_wait);
NeilBrown35bfc522017-10-17 13:46:43 +1100384 set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
385 smp_mb__after_atomic();
NeilBrown409c57f2009-03-31 14:39:39 +1100386 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
387 mddev->pers->quiesce(mddev, 1);
NeilBrown35bfc522017-10-17 13:46:43 +1100388 clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
389 wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500390
391 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100392}
NeilBrown390ee602010-06-01 19:37:27 +1000393EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100394
NeilBrownfd01b882011-10-11 16:47:53 +1100395void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100396{
NeilBrown4d5324f2017-10-19 12:17:16 +1100397 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100398 if (--mddev->suspended)
399 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100400 wake_up(&mddev->sb_wait);
401 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500402
Jonathan Brassow47525e52012-05-22 13:55:29 +1000403 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500404 md_wakeup_thread(mddev->thread);
405 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100406}
NeilBrown390ee602010-06-01 19:37:27 +1000407EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100408
NeilBrownfd01b882011-10-11 16:47:53 +1100409int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000410{
NeilBrown5c675f82014-12-15 12:56:56 +1100411 struct md_personality *pers = mddev->pers;
412 int ret = 0;
413
414 rcu_read_lock();
415 if (mddev->suspended)
416 ret = 1;
417 else if (pers && pers->congested)
418 ret = pers->congested(mddev, bits);
419 rcu_read_unlock();
420 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000421}
NeilBrown5c675f82014-12-15 12:56:56 +1100422EXPORT_SYMBOL_GPL(mddev_congested);
423static int md_congested(void *data, int bits)
424{
425 struct mddev *mddev = data;
426 return mddev_congested(mddev, bits);
427}
NeilBrown3fa841d2009-09-23 18:10:29 +1000428
NeilBrowna2826aa2009-12-14 12:49:49 +1100429/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200430 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100431 */
Xiao Ni5a409b42018-05-21 11:49:54 +0800432static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100433{
Xiao Ni5a409b42018-05-21 11:49:54 +0800434 struct flush_info *fi = container_of(ws, struct flush_info, flush_work);
435 struct mddev *mddev = fi->mddev;
436 struct bio *bio = fi->bio;
437
438 bio->bi_opf &= ~REQ_PREFLUSH;
439 md_handle_request(mddev, bio);
440
441 mempool_free(fi, mddev->flush_pool);
442}
443
444static void md_end_flush(struct bio *fbio)
445{
446 struct flush_bio *fb = fbio->bi_private;
447 struct md_rdev *rdev = fb->rdev;
448 struct flush_info *fi = fb->fi;
449 struct bio *bio = fi->bio;
450 struct mddev *mddev = fi->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100451
452 rdev_dec_pending(rdev, mddev);
453
Xiao Ni5a409b42018-05-21 11:49:54 +0800454 if (atomic_dec_and_test(&fi->flush_pending)) {
455 if (bio->bi_iter.bi_size == 0)
456 /* an empty barrier - all done */
457 bio_endio(bio);
458 else {
459 INIT_WORK(&fi->flush_work, submit_flushes);
460 queue_work(md_wq, &fi->flush_work);
461 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100462 }
Xiao Ni5a409b42018-05-21 11:49:54 +0800463
464 mempool_free(fb, mddev->flush_bio_pool);
465 bio_put(fbio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100466}
467
Xiao Ni5a409b42018-05-21 11:49:54 +0800468void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100469{
NeilBrown3cb03002011-10-11 16:45:26 +1100470 struct md_rdev *rdev;
Xiao Ni5a409b42018-05-21 11:49:54 +0800471 struct flush_info *fi;
NeilBrowna2826aa2009-12-14 12:49:49 +1100472
Xiao Ni5a409b42018-05-21 11:49:54 +0800473 fi = mempool_alloc(mddev->flush_pool, GFP_NOIO);
474
475 fi->bio = bio;
476 fi->mddev = mddev;
477 atomic_set(&fi->flush_pending, 1);
478
NeilBrowna2826aa2009-12-14 12:49:49 +1100479 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100480 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100481 if (rdev->raid_disk >= 0 &&
482 !test_bit(Faulty, &rdev->flags)) {
483 /* Take two references, one is dropped
484 * when request finishes, one after
485 * we reclaim rcu_read_lock
486 */
487 struct bio *bi;
Xiao Ni5a409b42018-05-21 11:49:54 +0800488 struct flush_bio *fb;
NeilBrowna2826aa2009-12-14 12:49:49 +1100489 atomic_inc(&rdev->nr_pending);
490 atomic_inc(&rdev->nr_pending);
491 rcu_read_unlock();
Xiao Ni5a409b42018-05-21 11:49:54 +0800492
493 fb = mempool_alloc(mddev->flush_bio_pool, GFP_NOIO);
494 fb->fi = fi;
495 fb->rdev = rdev;
496
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000497 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Christoph Hellwig74d46992017-08-23 19:10:32 +0200498 bio_set_dev(bi, rdev->bdev);
Xiao Ni5a409b42018-05-21 11:49:54 +0800499 bi->bi_end_io = md_end_flush;
500 bi->bi_private = fb;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600501 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
Xiao Ni5a409b42018-05-21 11:49:54 +0800502
503 atomic_inc(&fi->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500504 submit_bio(bi);
Xiao Ni5a409b42018-05-21 11:49:54 +0800505
NeilBrowna2826aa2009-12-14 12:49:49 +1100506 rcu_read_lock();
507 rdev_dec_pending(rdev, mddev);
508 }
509 rcu_read_unlock();
NeilBrowna2826aa2009-12-14 12:49:49 +1100510
Xiao Ni5a409b42018-05-21 11:49:54 +0800511 if (atomic_dec_and_test(&fi->flush_pending)) {
512 if (bio->bi_iter.bi_size == 0)
513 /* an empty barrier - all done */
514 bio_endio(bio);
515 else {
516 INIT_WORK(&fi->flush_work, submit_flushes);
517 queue_work(md_wq, &fi->flush_work);
518 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100519 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100520}
Tejun Heoe9c74692010-09-03 11:56:18 +0200521EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100522
NeilBrownfd01b882011-10-11 16:47:53 +1100523static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 atomic_inc(&mddev->active);
526 return mddev;
527}
528
Dan Williams5fd3a172009-03-04 00:57:25 -0700529static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100530
NeilBrownfd01b882011-10-11 16:47:53 +1100531static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
534 return;
NeilBrownd3374822009-01-09 08:31:10 +1100535 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100536 mddev->ctime == 0 && !mddev->hold_active) {
537 /* Array is not configured at all, and not held active,
538 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100539 list_del_init(&mddev->all_mddevs);
Kent Overstreet28dec872018-06-07 20:52:54 -0400540
541 /*
542 * Call queue_work inside the spinlock so that
543 * flush_workqueue() after mddev_find will succeed in waiting
544 * for the work to be done.
545 */
546 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
547 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100548 }
549 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Kees Cook8376d3c2017-10-16 17:01:48 -0700552static void md_safemode_timeout(struct timer_list *t);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400553
NeilBrownfd01b882011-10-11 16:47:53 +1100554void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100555{
Kent Overstreet28dec872018-06-07 20:52:54 -0400556 kobject_init(&mddev->kobj, &md_ktype);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100557 mutex_init(&mddev->open_mutex);
558 mutex_init(&mddev->reconfig_mutex);
559 mutex_init(&mddev->bitmap_info.mutex);
560 INIT_LIST_HEAD(&mddev->disks);
561 INIT_LIST_HEAD(&mddev->all_mddevs);
Kees Cook8376d3c2017-10-16 17:01:48 -0700562 timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100563 atomic_set(&mddev->active, 1);
564 atomic_set(&mddev->openers, 0);
565 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100566 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100567 init_waitqueue_head(&mddev->sb_wait);
568 init_waitqueue_head(&mddev->recovery_wait);
569 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000570 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500571 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100572 mddev->resync_min = 0;
573 mddev->resync_max = MaxSector;
574 mddev->level = LEVEL_NONE;
575}
NeilBrown390ee602010-06-01 19:37:27 +1000576EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100577
NeilBrownf72ffdd2014-09-30 14:23:59 +1000578static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
NeilBrownfd01b882011-10-11 16:47:53 +1100580 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
NeilBrown8f5f02c2011-02-16 13:58:51 +1100582 if (unit && MAJOR(unit) != MD_MAJOR)
583 unit &= ~((1<<MdpMinorShift)-1);
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 retry:
586 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
NeilBrownefeb53c2009-01-09 08:31:10 +1100588 if (unit) {
589 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
590 if (mddev->unit == unit) {
591 mddev_get(mddev);
592 spin_unlock(&all_mddevs_lock);
593 kfree(new);
594 return mddev;
595 }
596
597 if (new) {
598 list_add(&new->all_mddevs, &all_mddevs);
599 spin_unlock(&all_mddevs_lock);
600 new->hold_active = UNTIL_IOCTL;
601 return new;
602 }
603 } else if (new) {
604 /* find an unused unit number */
605 static int next_minor = 512;
606 int start = next_minor;
607 int is_free = 0;
608 int dev = 0;
609 while (!is_free) {
610 dev = MKDEV(MD_MAJOR, next_minor);
611 next_minor++;
612 if (next_minor > MINORMASK)
613 next_minor = 0;
614 if (next_minor == start) {
615 /* Oh dear, all in use. */
616 spin_unlock(&all_mddevs_lock);
617 kfree(new);
618 return NULL;
619 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000620
NeilBrownefeb53c2009-01-09 08:31:10 +1100621 is_free = 1;
622 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
623 if (mddev->unit == dev) {
624 is_free = 0;
625 break;
626 }
627 }
628 new->unit = dev;
629 new->md_minor = MINOR(dev);
630 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 list_add(&new->all_mddevs, &all_mddevs);
632 spin_unlock(&all_mddevs_lock);
633 return new;
634 }
635 spin_unlock(&all_mddevs_lock);
636
NeilBrown9ffae0c2006-01-06 00:20:32 -0800637 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (!new)
639 return NULL;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 new->unit = unit;
642 if (MAJOR(unit) == MD_MAJOR)
643 new->md_minor = MINOR(unit);
644 else
645 new->md_minor = MINOR(unit) >> MdpMinorShift;
646
NeilBrownfafd7fb2010-04-01 15:55:30 +1100647 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 goto retry;
650}
651
NeilBrownb6eb1272010-04-15 10:13:47 +1000652static struct attribute_group md_redundancy_group;
653
NeilBrown5c47daf2014-12-15 12:57:01 +1100654void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
NeilBrowna64c8762010-04-14 17:15:37 +1000656 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000657 /* These cannot be removed under reconfig_mutex as
658 * an access to the files will try to take reconfig_mutex
659 * while holding the file unremovable, which leads to
660 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000661 * So hold set sysfs_active while the remove in happeing,
662 * and anything else which might set ->to_remove or my
663 * otherwise change the sysfs namespace will fail with
664 * -EBUSY if sysfs_active is still set.
665 * We set sysfs_active under reconfig_mutex and elsewhere
666 * test it under the same mutex to ensure its correct value
667 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000668 */
NeilBrowna64c8762010-04-14 17:15:37 +1000669 struct attribute_group *to_remove = mddev->to_remove;
670 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000671 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000672 mutex_unlock(&mddev->reconfig_mutex);
673
NeilBrown00bcb4a2010-06-01 19:37:23 +1000674 if (mddev->kobj.sd) {
675 if (to_remove != &md_redundancy_group)
676 sysfs_remove_group(&mddev->kobj, to_remove);
677 if (mddev->pers == NULL ||
678 mddev->pers->sync_request == NULL) {
679 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
680 if (mddev->sysfs_action)
681 sysfs_put(mddev->sysfs_action);
682 mddev->sysfs_action = NULL;
683 }
NeilBrowna64c8762010-04-14 17:15:37 +1000684 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000685 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000686 } else
687 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Chris Dunlop751e67c2011-10-19 16:48:26 +1100689 /* As we've dropped the mutex we need a spinlock to
690 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000691 */
692 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700693 md_wakeup_thread(mddev->thread);
NeilBrown4d5324f2017-10-19 12:17:16 +1100694 wake_up(&mddev->sb_wait);
NeilBrown01f96c02011-09-21 15:30:20 +1000695 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
NeilBrown5c47daf2014-12-15 12:57:01 +1100697EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500699struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100700{
701 struct md_rdev *rdev;
702
703 rdev_for_each_rcu(rdev, mddev)
704 if (rdev->desc_nr == nr)
705 return rdev;
706
707 return NULL;
708}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500709EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100710
711static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
NeilBrown3cb03002011-10-11 16:45:26 +1100713 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
NeilBrowndafb20f2012-03-19 12:46:39 +1100715 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (rdev->bdev->bd_dev == dev)
717 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return NULL;
720}
721
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100722struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
NeilBrown1ca69c42012-10-11 13:37:33 +1100723{
724 struct md_rdev *rdev;
725
726 rdev_for_each_rcu(rdev, mddev)
727 if (rdev->bdev->bd_dev == dev)
728 return rdev;
729
730 return NULL;
731}
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100732EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100733
NeilBrown84fc4b52011-10-11 16:49:58 +1100734static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800735{
NeilBrown84fc4b52011-10-11 16:49:58 +1100736 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800737 list_for_each_entry(pers, &pers_list, list) {
738 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800739 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800740 if (strcmp(pers->name, clevel)==0)
741 return pers;
742 }
NeilBrown2604b702006-01-06 00:20:36 -0800743 return NULL;
744}
745
Andre Nollb73df2d2008-07-11 22:02:23 +1000746/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100747static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100749 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000750 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
NeilBrownf72ffdd2014-09-30 14:23:59 +1000753static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100756 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000757 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return 0;
759}
760
NeilBrown545c8792012-05-22 13:54:30 +1000761void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800764 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 rdev->sb_loaded = 0;
766 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000767 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100768 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
NeilBrown2699b672011-07-28 11:31:47 +1000770 if (rdev->bb_page) {
771 put_page(rdev->bb_page);
772 rdev->bb_page = NULL;
773 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800774 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
NeilBrown545c8792012-05-22 13:54:30 +1000776EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200778static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700779{
NeilBrown3cb03002011-10-11 16:45:26 +1100780 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100781 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700782
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200783 if (bio->bi_status) {
784 pr_err("md: super_written gets error=%d\n", bio->bi_status);
NeilBrowna9701a32005-11-08 21:39:34 -0800785 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100786 if (!test_bit(Faulty, &rdev->flags)
787 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800788 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100789 set_bit(LastDev, &rdev->flags);
790 }
791 } else
792 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700793
NeilBrowna9701a32005-11-08 21:39:34 -0800794 if (atomic_dec_and_test(&mddev->pending_writes))
795 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700796 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700797 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700798}
799
NeilBrownfd01b882011-10-11 16:47:53 +1100800void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700801 sector_t sector, int size, struct page *page)
802{
803 /* write first size bytes of page to sector of rdev
804 * Increment mddev->pending_writes before returning
805 * and decrement it on completion, waking up sb_wait
806 * if zero is reached.
807 * If an error occurred, call md_error
808 */
NeilBrown46533ff2016-11-18 16:16:11 +1100809 struct bio *bio;
810 int ff = 0;
811
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +0100812 if (!page)
813 return;
814
NeilBrown46533ff2016-11-18 16:16:11 +1100815 if (test_bit(Faulty, &rdev->flags))
816 return;
817
NeilBrown5a850712017-06-21 09:12:21 +1000818 bio = md_bio_alloc_sync(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700819
Shaohua Lied3b98c2016-03-29 14:00:19 -0700820 atomic_inc(&rdev->nr_pending);
821
Christoph Hellwig74d46992017-08-23 19:10:32 +0200822 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700823 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700824 bio_add_page(bio, page, size, 0);
825 bio->bi_private = rdev;
826 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100827
828 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
829 test_bit(FailFast, &rdev->flags) &&
830 !test_bit(LastDev, &rdev->flags))
831 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +0200832 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -0800833
NeilBrown7bfa19f2005-06-21 17:17:28 -0700834 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500835 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800836}
837
NeilBrown46533ff2016-11-18 16:16:11 +1100838int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800839{
Tejun Heoe9c74692010-09-03 11:56:18 +0200840 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000841 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -0800842 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +1100843 return -EAGAIN;
844 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700845}
846
NeilBrown3cb03002011-10-11 16:45:26 +1100847int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -0500848 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
NeilBrown5a850712017-06-21 09:12:21 +1000850 struct bio *bio = md_bio_alloc_sync(rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 int ret;
852
Christoph Hellwig74d46992017-08-23 19:10:32 +0200853 if (metadata_op && rdev->meta_bdev)
854 bio_set_dev(bio, rdev->meta_bdev);
855 else
856 bio_set_dev(bio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -0500857 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100858 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700859 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000860 else if (rdev->mddev->reshape_position != MaxSector &&
861 (rdev->mddev->reshape_backwards ==
862 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700863 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100864 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700865 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500867
868 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200870 ret = !bio->bi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 bio_put(bio);
872 return ret;
873}
NeilBrowna8745db2006-01-06 00:20:34 -0800874EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
NeilBrownf72ffdd2014-09-30 14:23:59 +1000876static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (rdev->sb_loaded)
881 return 0;
882
Mike Christie796a5cf2016-06-05 14:32:07 -0500883 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 goto fail;
885 rdev->sb_loaded = 1;
886 return 0;
887
888fail:
NeilBrown9d487392016-11-02 14:16:49 +1100889 pr_err("md: disabled device %s, could not read superblock.\n",
890 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return -EINVAL;
892}
893
Amir Goldsteine6fd2092017-05-04 16:26:20 +0300894static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000896 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000897 sb1->set_uuid1 == sb2->set_uuid1 &&
898 sb1->set_uuid2 == sb2->set_uuid2 &&
899 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
Amir Goldsteine6fd2092017-05-04 16:26:20 +0300902static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 int ret;
905 mdp_super_t *tmp1, *tmp2;
906
907 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
908 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
909
910 if (!tmp1 || !tmp2) {
911 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 goto abort;
913 }
914
915 *tmp1 = *sb1;
916 *tmp2 = *sb2;
917
918 /*
919 * nr_disks is not constant
920 */
921 tmp1->nr_disks = 0;
922 tmp2->nr_disks = 0;
923
Andre Nollce0c8e02008-07-11 22:02:20 +1000924 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700926 kfree(tmp1);
927 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return ret;
929}
930
NeilBrown4d167f02007-05-09 02:35:37 -0700931static u32 md_csum_fold(u32 csum)
932{
933 csum = (csum & 0xffff) + (csum >> 16);
934 return (csum & 0xffff) + (csum >> 16);
935}
936
NeilBrownf72ffdd2014-09-30 14:23:59 +1000937static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
NeilBrown4d167f02007-05-09 02:35:37 -0700939 u64 newcsum = 0;
940 u32 *sb32 = (u32*)sb;
941 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 unsigned int disk_csum, csum;
943
944 disk_csum = sb->sb_csum;
945 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700946
947 for (i = 0; i < MD_SB_BYTES/4 ; i++)
948 newcsum += sb32[i];
949 csum = (newcsum & 0xffffffff) + (newcsum>>32);
950
NeilBrown4d167f02007-05-09 02:35:37 -0700951#ifdef CONFIG_ALPHA
952 /* This used to use csum_partial, which was wrong for several
953 * reasons including that different results are returned on
954 * different architectures. It isn't critical that we get exactly
955 * the same return value as before (we always csum_fold before
956 * testing, and that removes any differences). However as we
957 * know that csum_partial always returned a 16bit value on
958 * alphas, do a fold to maximise conformity to previous behaviour.
959 */
960 sb->sb_csum = md_csum_fold(disk_csum);
961#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700963#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return csum;
965}
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967/*
968 * Handle superblock details.
969 * We want to be able to handle multiple superblock formats
970 * so we have a common interface to them all, and an array of
971 * different handlers.
972 * We rely on user-space to write the initial superblock, and support
973 * reading and updating of superblocks.
974 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100975 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 * loads and validates a superblock on dev.
977 * if refdev != NULL, compare superblocks on both devices
978 * Return:
979 * 0 - dev has a superblock that is compatible with refdev
980 * 1 - dev has a superblock that is compatible and newer than refdev
981 * so dev should be used as the refdev in future
982 * -EINVAL superblock incompatible or invalid
983 * -othererror e.g. -EIO
984 *
NeilBrownfd01b882011-10-11 16:47:53 +1100985 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * Verify that dev is acceptable into mddev.
987 * The first time, mddev->raid_disks will be 0, and data from
988 * dev should be merged in. Subsequent calls check that dev
989 * is new enough. Return 0 or -EINVAL
990 *
NeilBrownfd01b882011-10-11 16:47:53 +1100991 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * Update the superblock for rdev with data in mddev
993 * This does not write to disc.
994 *
995 */
996
997struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000998 char *name;
999 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +10001000 int (*load_super)(struct md_rdev *rdev,
1001 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001002 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +10001003 int (*validate_super)(struct mddev *mddev,
1004 struct md_rdev *rdev);
1005 void (*sync_super)(struct mddev *mddev,
1006 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001007 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001008 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +10001009 int (*allow_new_offset)(struct md_rdev *rdev,
1010 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011};
1012
1013/*
Andre Noll0894cc32009-06-18 08:49:23 +10001014 * Check that the given mddev has no bitmap.
1015 *
1016 * This function is called from the run method of all personalities that do not
1017 * support bitmaps. It prints an error message and returns non-zero if mddev
1018 * has a bitmap. Otherwise, it returns 0.
1019 *
1020 */
NeilBrownfd01b882011-10-11 16:47:53 +11001021int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001022{
NeilBrownc3d97142009-12-14 12:49:52 +11001023 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001024 return 0;
NeilBrown9d487392016-11-02 14:16:49 +11001025 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +10001026 mdname(mddev), mddev->pers->name);
1027 return 1;
1028}
1029EXPORT_SYMBOL(md_check_no_bitmap);
1030
1031/*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001032 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 */
NeilBrown3cb03002011-10-11 16:45:26 +11001034static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1037 mdp_super_t *sb;
1038 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 /*
Andre Noll0f420352008-07-11 22:02:23 +10001041 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 * it's at the end of the disk.
1043 *
1044 * It also happens to be a multiple of 4Kb.
1045 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001046 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
NeilBrown0002b272005-09-09 16:23:53 -07001048 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +11001049 if (ret)
1050 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 ret = -EINVAL;
1053
1054 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001055 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +11001058 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 goto abort;
1060 }
1061
1062 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001063 sb->minor_version < 90 ||
1064 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +11001065 pr_warn("Bad version number %d.%d on %s\n",
1066 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 goto abort;
1068 }
1069
1070 if (sb->raid_disks <= 0)
1071 goto abort;
1072
NeilBrown4d167f02007-05-09 02:35:37 -07001073 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001074 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 goto abort;
1076 }
1077
1078 rdev->preferred_minor = sb->md_minor;
1079 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001080 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001081 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001082 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 if (sb->level == LEVEL_MULTIPATH)
1085 rdev->desc_nr = -1;
1086 else
1087 rdev->desc_nr = sb->this_disk.number;
1088
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001089 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001091 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001093 mdp_super_t *refsb = page_address(refdev->sb_page);
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001094 if (!md_uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001095 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 b, bdevname(refdev->bdev,b2));
1097 goto abort;
1098 }
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001099 if (!md_sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001100 pr_warn("md: %s has same UUID but different superblock to %s\n",
1101 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 goto abort;
1103 }
1104 ev1 = md_event(sb);
1105 ev2 = md_event(refsb);
1106 if (ev1 > ev2)
1107 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001108 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 ret = 0;
1110 }
NeilBrown8190e752009-06-18 08:48:58 +10001111 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001112 /* Limit to 4TB as metadata cannot record more than that.
1113 * (not needed for Linear and RAID0 as metadata doesn't
1114 * record this size)
1115 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001116 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1117 sb->level >= 1)
1118 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
NeilBrown27a7b262011-09-10 17:21:28 +10001120 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001121 /* "this cannot possibly happen" ... */
1122 ret = -EINVAL;
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 abort:
1125 return ret;
1126}
1127
1128/*
1129 * validate_super for 0.90.0
1130 */
NeilBrownfd01b882011-10-11 16:47:53 +11001131static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001134 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001135 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
NeilBrown41158c72005-06-21 17:17:25 -07001137 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001138 clear_bit(Faulty, &rdev->flags);
1139 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001140 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001141 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (mddev->raid_disks == 0) {
1144 mddev->major_version = 0;
1145 mddev->minor_version = sb->minor_version;
1146 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001147 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001148 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 mddev->ctime = sb->ctime;
1150 mddev->utime = sb->utime;
1151 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001152 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 mddev->layout = sb->layout;
1154 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001155 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001156 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001157 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001158 mddev->bitmap_info.space = 0;
1159 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001160 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001161 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001162 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
NeilBrownf6705572006-03-27 01:18:11 -08001164 if (mddev->minor_version >= 91) {
1165 mddev->reshape_position = sb->reshape_position;
1166 mddev->delta_disks = sb->delta_disks;
1167 mddev->new_level = sb->new_level;
1168 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001169 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001170 if (mddev->delta_disks < 0)
1171 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001172 } else {
1173 mddev->reshape_position = MaxSector;
1174 mddev->delta_disks = 0;
1175 mddev->new_level = mddev->level;
1176 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001177 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001178 }
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (sb->state & (1<<MD_SB_CLEAN))
1181 mddev->recovery_cp = MaxSector;
1182 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001183 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 sb->events_lo == sb->cp_events_lo) {
1185 mddev->recovery_cp = sb->recovery_cp;
1186 } else
1187 mddev->recovery_cp = 0;
1188 }
1189
1190 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1191 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1192 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1193 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1194
1195 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001196
1197 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001198 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001199 mddev->bitmap_info.offset =
1200 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001201 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001202 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001203 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001204
NeilBrown41158c72005-06-21 17:17:25 -07001205 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001206 /* Insist on good event counter while assembling, except
1207 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001209 if (sb->disks[rdev->desc_nr].state & (
1210 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001211 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001212 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001213 } else if (mddev->bitmap) {
1214 /* if adding to array with a bitmap, then we can accept an
1215 * older device ... but not too old.
1216 */
NeilBrown41158c72005-06-21 17:17:25 -07001217 if (ev1 < mddev->bitmap->events_cleared)
1218 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001219 if (ev1 < mddev->events)
1220 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001221 } else {
1222 if (ev1 < mddev->events)
1223 /* just a hot-add of a new device, leave raid_disk at -1 */
1224 return 0;
1225 }
NeilBrown41158c72005-06-21 17:17:25 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 desc = sb->disks + rdev->desc_nr;
1229
1230 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001231 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001232 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1233 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001234 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001236 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001237 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1238 /* active but not in sync implies recovery up to
1239 * reshape position. We don't know exactly where
1240 * that is, so set to zero for now */
1241 if (mddev->minor_version >= 91) {
1242 rdev->recovery_offset = 0;
1243 rdev->raid_disk = desc->raid_disk;
1244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001246 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1247 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001248 if (desc->state & (1<<MD_DISK_FAILFAST))
1249 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001250 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001251 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return 0;
1253}
1254
1255/*
1256 * sync_super for 0.90.0
1257 */
NeilBrownfd01b882011-10-11 16:47:53 +11001258static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
1260 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001261 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /* make rdev->sb match mddev data..
1265 *
1266 * 1/ zero out disks
1267 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1268 * 3/ any empty disks < next_spare become removed
1269 *
1270 * disks[0] gets initialised to REMOVED because
1271 * we cannot be sure from other fields if it has
1272 * been initialised or not.
1273 */
1274 int i;
1275 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1276
NeilBrown61181562005-09-09 16:24:02 -07001277 rdev->sb_size = MD_SB_BYTES;
1278
Namhyung Kim65a06f062011-07-27 11:00:36 +10001279 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 memset(sb, 0, sizeof(*sb));
1282
1283 sb->md_magic = MD_SB_MAGIC;
1284 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 sb->patch_version = mddev->patch_version;
1286 sb->gvalid_words = 0; /* ignored */
1287 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1288 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1289 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1290 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1291
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001292 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001294 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 sb->raid_disks = mddev->raid_disks;
1296 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001297 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001298 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 sb->state = 0;
1300 sb->events_hi = (mddev->events>>32);
1301 sb->events_lo = (u32)mddev->events;
1302
NeilBrownf6705572006-03-27 01:18:11 -08001303 if (mddev->reshape_position == MaxSector)
1304 sb->minor_version = 90;
1305 else {
1306 sb->minor_version = 91;
1307 sb->reshape_position = mddev->reshape_position;
1308 sb->new_level = mddev->new_level;
1309 sb->delta_disks = mddev->delta_disks;
1310 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001311 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001312 }
1313 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (mddev->in_sync)
1315 {
1316 sb->recovery_cp = mddev->recovery_cp;
1317 sb->cp_events_hi = (mddev->events>>32);
1318 sb->cp_events_lo = (u32)mddev->events;
1319 if (mddev->recovery_cp == MaxSector)
1320 sb->state = (1<< MD_SB_CLEAN);
1321 } else
1322 sb->recovery_cp = 0;
1323
1324 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001325 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
NeilBrownc3d97142009-12-14 12:49:52 +11001327 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001328 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001331 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001333 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001334 int is_active = test_bit(In_sync, &rdev2->flags);
1335
1336 if (rdev2->raid_disk >= 0 &&
1337 sb->minor_version >= 91)
1338 /* we have nowhere to store the recovery_offset,
1339 * but if it is not below the reshape_position,
1340 * we can piggy-back on that.
1341 */
1342 is_active = 1;
1343 if (rdev2->raid_disk < 0 ||
1344 test_bit(Faulty, &rdev2->flags))
1345 is_active = 0;
1346 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001347 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001349 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001350 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 d = &sb->disks[rdev2->desc_nr];
1352 nr_disks++;
1353 d->number = rdev2->desc_nr;
1354 d->major = MAJOR(rdev2->bdev->bd_dev);
1355 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001356 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 d->raid_disk = rdev2->raid_disk;
1358 else
1359 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001360 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001362 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001364 if (test_bit(In_sync, &rdev2->flags))
1365 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 active++;
1367 working++;
1368 } else {
1369 d->state = 0;
1370 spare++;
1371 working++;
1372 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001373 if (test_bit(WriteMostly, &rdev2->flags))
1374 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001375 if (test_bit(FailFast, &rdev2->flags))
1376 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /* now set the "removed" and "faulty" bits on any missing devices */
1379 for (i=0 ; i < mddev->raid_disks ; i++) {
1380 mdp_disk_t *d = &sb->disks[i];
1381 if (d->state == 0 && d->number == 0) {
1382 d->number = i;
1383 d->raid_disk = i;
1384 d->state = (1<<MD_DISK_REMOVED);
1385 d->state |= (1<<MD_DISK_FAULTY);
1386 failed++;
1387 }
1388 }
1389 sb->nr_disks = nr_disks;
1390 sb->active_disks = active;
1391 sb->working_disks = working;
1392 sb->failed_disks = failed;
1393 sb->spare_disks = spare;
1394
1395 sb->this_disk = sb->disks[rdev->desc_nr];
1396 sb->sb_csum = calc_sb_csum(sb);
1397}
1398
1399/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001400 * rdev_size_change for 0.90.0
1401 */
1402static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001403super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001404{
Andre Noll58c0fed2009-03-31 14:33:13 +11001405 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001406 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001407 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001408 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001409 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001410 if (!num_sectors || num_sectors > rdev->sb_start)
1411 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001412 /* Limit to 4TB as metadata cannot record more than that.
1413 * 4TB == 2^32 KB, or 2*2^32 sectors.
1414 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001415 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1416 rdev->mddev->level >= 1)
1417 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001418 do {
1419 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001420 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001421 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001422 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001423}
1424
NeilBrownc6563a82012-05-21 09:27:00 +10001425static int
1426super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1427{
1428 /* non-zero offset changes not possible with v0.90 */
1429 return new_offset == 0;
1430}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001431
1432/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 * version 1 superblock
1434 */
1435
NeilBrownf72ffdd2014-09-30 14:23:59 +10001436static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001438 __le32 disk_csum;
1439 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 unsigned long long newcsum;
1441 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001442 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 disk_csum = sb->sb_csum;
1445 sb->sb_csum = 0;
1446 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001447 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 newcsum += le32_to_cpu(*isuper++);
1449
1450 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001451 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1454 sb->sb_csum = disk_csum;
1455 return cpu_to_le32(csum);
1456}
1457
NeilBrown3cb03002011-10-11 16:45:26 +11001458static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 struct mdp_superblock_1 *sb;
1461 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001462 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001463 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001465 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 /*
Andre Noll0f420352008-07-11 22:02:23 +10001468 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 * It is always aligned to a 4K boundary and
1470 * depeding on minor_version, it can be:
1471 * 0: At least 8K, but less than 12K, from end of device
1472 * 1: At start of device
1473 * 2: 4K from start of device.
1474 */
1475 switch(minor_version) {
1476 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001477 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001478 sb_start -= 8*2;
1479 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 break;
1481 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001482 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 break;
1484 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001485 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 break;
1487 default:
1488 return -EINVAL;
1489 }
Andre Noll0f420352008-07-11 22:02:23 +10001490 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
NeilBrown0002b272005-09-09 16:23:53 -07001492 /* superblock is rarely larger than 1K, but it can be larger,
1493 * and it is safe to read 4k, so we do that
1494 */
1495 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (ret) return ret;
1497
Namhyung Kim65a06f062011-07-27 11:00:36 +10001498 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1501 sb->major_version != cpu_to_le32(1) ||
1502 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001503 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001504 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return -EINVAL;
1506
1507 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001508 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 bdevname(rdev->bdev,b));
1510 return -EINVAL;
1511 }
1512 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001513 pr_warn("md: data_size too small on %s\n",
1514 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return -EINVAL;
1516 }
NeilBrownc6563a82012-05-21 09:27:00 +10001517 if (sb->pad0 ||
1518 sb->pad3[0] ||
1519 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1520 /* Some padding is non-zero, might be a new feature */
1521 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 rdev->preferred_minor = 0xffff;
1524 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001525 rdev->new_data_offset = rdev->data_offset;
1526 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1527 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1528 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001529 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
NeilBrown0002b272005-09-09 16:23:53 -07001531 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001532 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001533 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001534 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1535
1536 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001537 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001538 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001539 if (minor_version
1540 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1541 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001542
NeilBrown31b65a02006-07-10 04:44:14 -07001543 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1544 rdev->desc_nr = -1;
1545 else
1546 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1547
NeilBrown2699b672011-07-28 11:31:47 +10001548 if (!rdev->bb_page) {
1549 rdev->bb_page = alloc_page(GFP_KERNEL);
1550 if (!rdev->bb_page)
1551 return -ENOMEM;
1552 }
1553 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1554 rdev->badblocks.count == 0) {
1555 /* need to load the bad block list.
1556 * Currently we limit it to one page.
1557 */
1558 s32 offset;
1559 sector_t bb_sector;
1560 u64 *bbp;
1561 int i;
1562 int sectors = le16_to_cpu(sb->bblog_size);
1563 if (sectors > (PAGE_SIZE / 512))
1564 return -EINVAL;
1565 offset = le32_to_cpu(sb->bblog_offset);
1566 if (offset == 0)
1567 return -EINVAL;
1568 bb_sector = (long long)offset;
1569 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001570 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001571 return -EIO;
1572 bbp = (u64 *)page_address(rdev->bb_page);
1573 rdev->badblocks.shift = sb->bblog_shift;
1574 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1575 u64 bb = le64_to_cpu(*bbp);
1576 int count = bb & (0x3ff);
1577 u64 sector = bb >> 10;
1578 sector <<= sb->bblog_shift;
1579 count <<= sb->bblog_shift;
1580 if (bb + 1 == 0)
1581 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001582 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001583 return -EINVAL;
1584 }
NeilBrown486adf72013-04-24 11:42:44 +10001585 } else if (sb->bblog_offset != 0)
1586 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001587
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001588 if ((le32_to_cpu(sb->feature_map) &
1589 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001590 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1591 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1592 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1593 }
1594
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001595 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001596 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001597 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001599 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1602 sb->level != refsb->level ||
1603 sb->layout != refsb->layout ||
1604 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001605 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 bdevname(rdev->bdev,b),
1607 bdevname(refdev->bdev,b2));
1608 return -EINVAL;
1609 }
1610 ev1 = le64_to_cpu(sb->events);
1611 ev2 = le64_to_cpu(refsb->events);
1612
1613 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001614 ret = 1;
1615 else
1616 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
NeilBrownc6563a82012-05-21 09:27:00 +10001618 if (minor_version) {
1619 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1620 sectors -= rdev->data_offset;
1621 } else
1622 sectors = rdev->sb_start;
1623 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001625 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001626 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
NeilBrownfd01b882011-10-11 16:47:53 +11001629static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001631 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001632 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
NeilBrown41158c72005-06-21 17:17:25 -07001634 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001635 clear_bit(Faulty, &rdev->flags);
1636 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001637 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001638 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (mddev->raid_disks == 0) {
1641 mddev->major_version = 1;
1642 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001643 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001644 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001645 mddev->ctime = le64_to_cpu(sb->ctime);
1646 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001648 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 mddev->layout = le32_to_cpu(sb->layout);
1650 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001651 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001652 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001653 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001654 mddev->bitmap_info.space = 0;
1655 /* Default location for bitmap is 1K after superblock
1656 * using 3K - total of 4K
1657 */
NeilBrownc3d97142009-12-14 12:49:52 +11001658 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001659 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001660 mddev->reshape_backwards = 0;
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1663 memcpy(mddev->uuid, sb->set_uuid, 16);
1664
1665 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001666
NeilBrown71c08052005-09-09 16:23:51 -07001667 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001668 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001669 mddev->bitmap_info.offset =
1670 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001671 /* Metadata doesn't record how much space is available.
1672 * For 1.0, we assume we can use up to the superblock
1673 * if before, else to 4K beyond superblock.
1674 * For others, assume no change is possible.
1675 */
1676 if (mddev->minor_version > 0)
1677 mddev->bitmap_info.space = 0;
1678 else if (mddev->bitmap_info.offset > 0)
1679 mddev->bitmap_info.space =
1680 8 - mddev->bitmap_info.offset;
1681 else
1682 mddev->bitmap_info.space =
1683 -mddev->bitmap_info.offset;
1684 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001685
NeilBrownf6705572006-03-27 01:18:11 -08001686 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1687 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1688 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1689 mddev->new_level = le32_to_cpu(sb->new_level);
1690 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001691 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001692 if (mddev->delta_disks < 0 ||
1693 (mddev->delta_disks == 0 &&
1694 (le32_to_cpu(sb->feature_map)
1695 & MD_FEATURE_RESHAPE_BACKWARDS)))
1696 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001697 } else {
1698 mddev->reshape_position = MaxSector;
1699 mddev->delta_disks = 0;
1700 mddev->new_level = mddev->level;
1701 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001702 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001703 }
1704
Song Liu486b0f72016-08-19 15:34:01 -07001705 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001706 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001707
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001708 if (le32_to_cpu(sb->feature_map) &
1709 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001710 if (le32_to_cpu(sb->feature_map) &
1711 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1712 return -EINVAL;
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001713 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1714 (le32_to_cpu(sb->feature_map) &
1715 MD_FEATURE_MULTIPLE_PPLS))
1716 return -EINVAL;
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001717 set_bit(MD_HAS_PPL, &mddev->flags);
1718 }
NeilBrown41158c72005-06-21 17:17:25 -07001719 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001720 /* Insist of good event counter while assembling, except for
1721 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001723 if (rdev->desc_nr >= 0 &&
1724 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001725 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1726 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001727 if (ev1 < mddev->events)
1728 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001729 } else if (mddev->bitmap) {
1730 /* If adding to array with a bitmap, then we can accept an
1731 * older device, but not too old.
1732 */
NeilBrown41158c72005-06-21 17:17:25 -07001733 if (ev1 < mddev->bitmap->events_cleared)
1734 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001735 if (ev1 < mddev->events)
1736 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001737 } else {
1738 if (ev1 < mddev->events)
1739 /* just a hot-add of a new device, leave raid_disk at -1 */
1740 return 0;
1741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (mddev->level != LEVEL_MULTIPATH) {
1743 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001744 if (rdev->desc_nr < 0 ||
1745 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001746 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001747 rdev->desc_nr = -1;
1748 } else
1749 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001751 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001753 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001754 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 break;
Song Liubac624f2015-08-13 14:31:55 -07001756 case MD_DISK_ROLE_JOURNAL: /* journal device */
1757 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1758 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001759 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001760 return -EINVAL;
1761 }
1762 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001763 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001764 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001765 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 default:
NeilBrownf4667222013-12-09 12:04:56 +11001767 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001768 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001769 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001770 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001771 if (!(le32_to_cpu(sb->feature_map) &
1772 MD_FEATURE_RECOVERY_BITMAP))
1773 rdev->saved_raid_disk = -1;
1774 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001775 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 rdev->raid_disk = role;
1777 break;
1778 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001779 if (sb->devflags & WriteMostly1)
1780 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001781 if (sb->devflags & FailFast1)
1782 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001783 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1784 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001785 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001786 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return 0;
1789}
1790
NeilBrownfd01b882011-10-11 16:47:53 +11001791static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
1793 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001794 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 int max_dev, i;
1796 /* make rdev->sb match mddev and rdev data. */
1797
Namhyung Kim65a06f062011-07-27 11:00:36 +10001798 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 sb->feature_map = 0;
1801 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001802 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 memset(sb->pad3, 0, sizeof(sb->pad3));
1804
1805 sb->utime = cpu_to_le64((__u64)mddev->utime);
1806 sb->events = cpu_to_le64(mddev->events);
1807 if (mddev->in_sync)
1808 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001809 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1810 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 else
1812 sb->resync_offset = cpu_to_le64(0);
1813
NeilBrown1c05b4b2006-10-21 10:24:08 -07001814 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001815
NeilBrownf0ca3402006-02-02 14:28:04 -08001816 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001817 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001818 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001819 sb->level = cpu_to_le32(mddev->level);
1820 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11001821 if (test_bit(FailFast, &rdev->flags))
1822 sb->devflags |= FailFast1;
1823 else
1824 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08001825
NeilBrownaeb9b2112011-08-25 14:43:08 +10001826 if (test_bit(WriteMostly, &rdev->flags))
1827 sb->devflags |= WriteMostly1;
1828 else
1829 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001830 sb->data_offset = cpu_to_le64(rdev->data_offset);
1831 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001832
NeilBrownc3d97142009-12-14 12:49:52 +11001833 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1834 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001835 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001836 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001837
Shaohua Lif2076e72015-10-08 21:54:12 -07001838 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001839 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001840 sb->feature_map |=
1841 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1842 sb->recovery_offset =
1843 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001844 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1845 sb->feature_map |=
1846 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001847 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001848 /* Note: recovery_offset and journal_tail share space */
1849 if (test_bit(Journal, &rdev->flags))
1850 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001851 if (test_bit(Replacement, &rdev->flags))
1852 sb->feature_map |=
1853 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001854
NeilBrownf6705572006-03-27 01:18:11 -08001855 if (mddev->reshape_position != MaxSector) {
1856 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1857 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1858 sb->new_layout = cpu_to_le32(mddev->new_layout);
1859 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1860 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001861 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001862 if (mddev->delta_disks == 0 &&
1863 mddev->reshape_backwards)
1864 sb->feature_map
1865 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001866 if (rdev->new_data_offset != rdev->data_offset) {
1867 sb->feature_map
1868 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1869 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1870 - rdev->data_offset));
1871 }
NeilBrownf6705572006-03-27 01:18:11 -08001872 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001873
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001874 if (mddev_is_clustered(mddev))
1875 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1876
NeilBrown2699b672011-07-28 11:31:47 +10001877 if (rdev->badblocks.count == 0)
1878 /* Nothing to do for bad blocks*/ ;
1879 else if (sb->bblog_offset == 0)
1880 /* Cannot record bad blocks on this device */
1881 md_error(mddev, rdev);
1882 else {
1883 struct badblocks *bb = &rdev->badblocks;
1884 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1885 u64 *p = bb->page;
1886 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1887 if (bb->changed) {
1888 unsigned seq;
1889
1890retry:
1891 seq = read_seqbegin(&bb->lock);
1892
1893 memset(bbp, 0xff, PAGE_SIZE);
1894
1895 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001896 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001897 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1898 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001899 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001900 }
NeilBrownd0962932012-03-19 12:46:41 +11001901 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001902 if (read_seqretry(&bb->lock, seq))
1903 goto retry;
1904
1905 bb->sector = (rdev->sb_start +
1906 (int)le32_to_cpu(sb->bblog_offset));
1907 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001908 }
1909 }
1910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001912 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (rdev2->desc_nr+1 > max_dev)
1914 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001915
NeilBrown70471da2009-08-03 10:59:57 +10001916 if (max_dev > le32_to_cpu(sb->max_dev)) {
1917 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001918 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001919 rdev->sb_size = max_dev * 2 + 256;
1920 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1921 if (rdev->sb_size & bmask)
1922 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001923 } else
1924 max_dev = le32_to_cpu(sb->max_dev);
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 for (i=0; i<max_dev;i++)
Lidong Zhong8df72022017-06-12 10:45:55 +08001927 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001928
Song Liua97b7892015-10-08 21:54:09 -07001929 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1930 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001932 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001933 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
1934 sb->feature_map |=
1935 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
1936 else
1937 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001938 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
1939 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
1940 }
1941
NeilBrowndafb20f2012-03-19 12:46:39 +11001942 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001944 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001945 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001946 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001948 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001949 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001950 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001951 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 else
Song Liuc4d4c912015-08-13 14:31:54 -07001953 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 sb->sb_csum = calc_sb_1_csum(sb);
1957}
1958
Chris Webb0cd17fe2008-06-28 08:31:46 +10001959static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001960super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001961{
1962 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001963 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001964 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001965 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001966 if (rdev->data_offset != rdev->new_data_offset)
1967 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001968 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001969 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001970 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001971 max_sectors -= rdev->data_offset;
1972 if (!num_sectors || num_sectors > max_sectors)
1973 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001974 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001975 /* minor version 0 with bitmap we can't move */
1976 return 0;
1977 } else {
1978 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001979 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001980 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001981 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001982 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001983 if (!num_sectors || num_sectors > max_sectors)
1984 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001985 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001986 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001987 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001988 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08001989 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10001990 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11001991 do {
1992 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1993 rdev->sb_page);
1994 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001995 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001996
1997}
1998
1999static int
2000super_1_allow_new_offset(struct md_rdev *rdev,
2001 unsigned long long new_offset)
2002{
2003 /* All necessary checks on new >= old have been done */
2004 struct bitmap *bitmap;
2005 if (new_offset >= rdev->data_offset)
2006 return 1;
2007
2008 /* with 1.0 metadata, there is no metadata to tread on
2009 * so we can always move back */
2010 if (rdev->mddev->minor_version == 0)
2011 return 1;
2012
2013 /* otherwise we must be sure not to step on
2014 * any metadata, so stay:
2015 * 36K beyond start of superblock
2016 * beyond end of badblocks
2017 * beyond write-intent bitmap
2018 */
2019 if (rdev->sb_start + (32+4)*2 > new_offset)
2020 return 0;
2021 bitmap = rdev->mddev->bitmap;
2022 if (bitmap && !rdev->mddev->bitmap_info.file &&
2023 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10002024 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10002025 return 0;
2026 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2027 return 0;
2028
2029 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002030}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Adrian Bunk75c96f82005-05-05 16:16:09 -07002032static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 [0] = {
2034 .name = "0.90.0",
2035 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002036 .load_super = super_90_load,
2037 .validate_super = super_90_validate,
2038 .sync_super = super_90_sync,
2039 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002040 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 },
2042 [1] = {
2043 .name = "md-1",
2044 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002045 .load_super = super_1_load,
2046 .validate_super = super_1_validate,
2047 .sync_super = super_1_sync,
2048 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002049 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 },
2051};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
NeilBrownfd01b882011-10-11 16:47:53 +11002053static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05002054{
2055 if (mddev->sync_super) {
2056 mddev->sync_super(mddev, rdev);
2057 return;
2058 }
2059
2060 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2061
2062 super_types[mddev->major_version].sync_super(mddev, rdev);
2063}
2064
NeilBrownfd01b882011-10-11 16:47:53 +11002065static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
NeilBrown3cb03002011-10-11 16:45:26 +11002067 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
NeilBrown4b809912008-07-21 17:05:25 +10002069 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07002070 rdev_for_each_rcu(rdev, mddev1) {
2071 if (test_bit(Faulty, &rdev->flags) ||
2072 test_bit(Journal, &rdev->flags) ||
2073 rdev->raid_disk == -1)
2074 continue;
2075 rdev_for_each_rcu(rdev2, mddev2) {
2076 if (test_bit(Faulty, &rdev2->flags) ||
2077 test_bit(Journal, &rdev2->flags) ||
2078 rdev2->raid_disk == -1)
2079 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002080 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002081 rdev2->bdev->bd_contains) {
2082 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002083 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002084 }
Song Liu0b020e82015-09-03 23:00:35 -07002085 }
2086 }
NeilBrown4b809912008-07-21 17:05:25 +10002087 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return 0;
2089}
2090
2091static LIST_HEAD(pending_raid_disks);
2092
Andre Nollac5e7112009-08-03 10:59:47 +10002093/*
2094 * Try to register data integrity profile for an mddev
2095 *
2096 * This is called when an array is started and after a disk has been kicked
2097 * from the array. It only succeeds if all working and active component devices
2098 * are integrity capable with matching profiles.
2099 */
NeilBrownfd01b882011-10-11 16:47:53 +11002100int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002101{
NeilBrown3cb03002011-10-11 16:45:26 +11002102 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002103
Andre Nollac5e7112009-08-03 10:59:47 +10002104 if (list_empty(&mddev->disks))
2105 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002106 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2107 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002108 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002109 /* skip spares and non-functional disks */
2110 if (test_bit(Faulty, &rdev->flags))
2111 continue;
2112 if (rdev->raid_disk < 0)
2113 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002114 if (!reference) {
2115 /* Use the first rdev as the reference */
2116 reference = rdev;
2117 continue;
2118 }
2119 /* does this rdev's profile match the reference profile? */
2120 if (blk_integrity_compare(reference->bdev->bd_disk,
2121 rdev->bdev->bd_disk) < 0)
2122 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002123 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002124 if (!reference || !bdev_get_integrity(reference->bdev))
2125 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002126 /*
2127 * All component devices are integrity capable and have matching
2128 * profiles, register the common profile for the md device.
2129 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002130 blk_integrity_register(mddev->gendisk,
2131 bdev_get_integrity(reference->bdev));
2132
NeilBrown9d487392016-11-02 14:16:49 +11002133 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Kent Overstreetafeee512018-05-20 18:25:52 -04002134 if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002135 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002136 mdname(mddev));
2137 return -EINVAL;
2138 }
Andre Nollac5e7112009-08-03 10:59:47 +10002139 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002140}
Andre Nollac5e7112009-08-03 10:59:47 +10002141EXPORT_SYMBOL(md_integrity_register);
2142
Dan Williams1501efa2016-01-13 16:00:07 -08002143/*
2144 * Attempt to add an rdev, but only if it is consistent with the current
2145 * integrity profile
2146 */
2147int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002148{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002149 struct blk_integrity *bi_rdev;
2150 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002151 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002152
2153 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002154 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002155
2156 bi_rdev = bdev_get_integrity(rdev->bdev);
2157 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002158
2159 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002160 return 0;
2161
2162 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002163 pr_err("%s: incompatible integrity profile for %s\n",
2164 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002165 return -ENXIO;
2166 }
2167
2168 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002169}
2170EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002171
NeilBrownf72ffdd2014-09-30 14:23:59 +10002172static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002174 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002175 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002176 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Dan Williams11e2ede2008-04-30 00:52:32 -07002178 /* prevent duplicates */
2179 if (find_rdev(mddev, rdev->bdev->bd_dev))
2180 return -EEXIST;
2181
NeilBrown97b20ef2017-04-13 08:53:48 +10002182 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2183 mddev->pers)
2184 return -EROFS;
2185
Andre Nolldd8ac332009-03-31 14:33:13 +11002186 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002187 if (!test_bit(Journal, &rdev->flags) &&
2188 rdev->sectors &&
2189 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002190 if (mddev->pers) {
2191 /* Cannot change size, so fail
2192 * If mddev->level <= 0, then we don't care
2193 * about aligning sizes (e.g. linear)
2194 */
2195 if (mddev->level > 0)
2196 return -ENOSPC;
2197 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002198 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 /* Verify rdev->desc_nr is unique.
2202 * If it is -1, assign a free number, else
2203 * check number is not in use
2204 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002205 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (rdev->desc_nr < 0) {
2207 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002208 if (mddev->pers)
2209 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002210 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 choice++;
2212 rdev->desc_nr = choice;
2213 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002214 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002215 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002219 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002220 if (!test_bit(Journal, &rdev->flags) &&
2221 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002222 pr_warn("md: %s: array is limited to %d devices\n",
2223 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002224 return -EBUSY;
2225 }
NeilBrown19133a42005-11-08 21:39:35 -08002226 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002227 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002230 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002231
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002232 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002233 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002234
Tejun Heo0762b8b2008-08-25 19:56:12 +09002235 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002236 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2237 /* failure here is OK */;
2238 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002239
NeilBrown4b809912008-07-21 17:05:25 +10002240 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002241 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002242
2243 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002244 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002247
2248 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002249 pr_warn("md: failed to register dev-%s for %s\n",
2250 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002251 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
NeilBrown177a99b2008-02-06 01:39:56 -08002254static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002255{
NeilBrown3cb03002011-10-11 16:45:26 +11002256 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002257 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002258 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002259}
2260
NeilBrownf72ffdd2014-09-30 14:23:59 +10002261static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
2263 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002264
Tejun Heo49731ba2011-01-14 18:43:57 +01002265 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002266 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002267 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002269 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002270 sysfs_put(rdev->sysfs_state);
2271 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002272 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002273 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002274 * writing to 'remove' to "dev/state". We also need
2275 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002276 */
NeilBrown4b809912008-07-21 17:05:25 +10002277 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002278 INIT_WORK(&rdev->del_work, md_delayed_delete);
2279 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002280 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
2282
2283/*
2284 * prevent the device from being mounted, repartitioned or
2285 * otherwise reused by a RAID array (or any other kernel
2286 * subsystem), by bd_claiming the device.
2287 */
NeilBrown3cb03002011-10-11 16:45:26 +11002288static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
2290 int err = 0;
2291 struct block_device *bdev;
2292 char b[BDEVNAME_SIZE];
2293
Tejun Heod4d77622010-11-13 11:55:18 +01002294 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002295 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002297 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return PTR_ERR(bdev);
2299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 rdev->bdev = bdev;
2301 return err;
2302}
2303
NeilBrown3cb03002011-10-11 16:45:26 +11002304static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
2306 struct block_device *bdev = rdev->bdev;
2307 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002308 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
2311void md_autodetect_dev(dev_t dev);
2312
NeilBrownf72ffdd2014-09-30 14:23:59 +10002313static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
2315 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002316
NeilBrown9d487392016-11-02 14:16:49 +11002317 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002318 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002320 if (test_bit(AutoDetected, &rdev->flags))
2321 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322#endif
2323 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002324 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002327void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
2329 unbind_rdev_from_array(rdev);
2330 export_rdev(rdev);
2331}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002332EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
NeilBrownfd01b882011-10-11 16:47:53 +11002334static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
NeilBrown0638bb02014-09-25 17:43:47 +10002336 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
NeilBrown0638bb02014-09-25 17:43:47 +10002338 while (!list_empty(&mddev->disks)) {
2339 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2340 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002341 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 mddev->raid_disks = 0;
2344 mddev->major_version = 0;
2345}
2346
NeilBrown6497709b2017-03-15 14:05:14 +11002347static bool set_in_sync(struct mddev *mddev)
2348{
Shaohua Liefa4b772017-10-18 22:08:13 -07002349 lockdep_assert_held(&mddev->lock);
NeilBrown4ad23a972017-03-15 14:05:14 +11002350 if (!mddev->in_sync) {
2351 mddev->sync_checkers++;
2352 spin_unlock(&mddev->lock);
2353 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2354 spin_lock(&mddev->lock);
2355 if (!mddev->in_sync &&
2356 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002357 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002358 /*
2359 * Ensure ->in_sync is visible before we clear
2360 * ->sync_checkers.
2361 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002362 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002363 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2364 sysfs_notify_dirent_safe(mddev->sysfs_state);
2365 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002366 if (--mddev->sync_checkers == 0)
2367 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002368 }
2369 if (mddev->safemode == 1)
2370 mddev->safemode = 0;
2371 return mddev->in_sync;
2372}
2373
NeilBrownf72ffdd2014-09-30 14:23:59 +10002374static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
NeilBrown42543762006-06-26 00:27:57 -07002376 /* Update each superblock (in-memory image), but
2377 * if we are allowed to, skip spares which already
2378 * have the right event counter, or have one earlier
2379 * (which would mean they aren't being marked as dirty
2380 * with the rest of the array)
2381 */
NeilBrown3cb03002011-10-11 16:45:26 +11002382 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002383 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002384 if (rdev->sb_events == mddev->events ||
2385 (nospares &&
2386 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002387 rdev->sb_events+1 == mddev->events)) {
2388 /* Don't update this superblock */
2389 rdev->sb_loaded = 2;
2390 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002391 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002392 rdev->sb_loaded = 1;
2393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
2395}
2396
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002397static bool does_sb_need_changing(struct mddev *mddev)
2398{
2399 struct md_rdev *rdev;
2400 struct mdp_superblock_1 *sb;
2401 int role;
2402
2403 /* Find a good rdev */
2404 rdev_for_each(rdev, mddev)
2405 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2406 break;
2407
2408 /* No good device found. */
2409 if (!rdev)
2410 return false;
2411
2412 sb = page_address(rdev->sb_page);
2413 /* Check if a device has become faulty or a spare become active */
2414 rdev_for_each(rdev, mddev) {
2415 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2416 /* Device activated? */
2417 if (role == 0xffff && rdev->raid_disk >=0 &&
2418 !test_bit(Faulty, &rdev->flags))
2419 return true;
2420 /* Device turned faulty? */
2421 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2422 return true;
2423 }
2424
2425 /* Check if any mddev parameters have changed */
2426 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2427 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002428 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002429 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2430 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2431 return true;
2432
2433 return false;
2434}
2435
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002436void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
NeilBrown3cb03002011-10-11 16:45:26 +11002438 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002439 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002440 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002441 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002442 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
NeilBrownd87f0642013-04-24 11:42:40 +10002444 if (mddev->ro) {
2445 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002446 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002447 return;
2448 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002449
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002450repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002451 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002452 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002453 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002454 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002455 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002456 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002457 /* Has someone else has updated the sb */
2458 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002459 if (ret == 0)
2460 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002461 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2462 BIT(MD_SB_CHANGE_DEVS) |
2463 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002464 return;
2465 }
2466 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002467
NeilBrowndb0505d2017-10-17 16:18:36 +11002468 /*
2469 * First make sure individual recovery_offsets are correct
2470 * curr_resync_completed can only be used during recovery.
2471 * During reshape/resync it might use array-addresses rather
2472 * that device addresses.
2473 */
NeilBrowndafb20f2012-03-19 12:46:39 +11002474 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002475 if (rdev->raid_disk >= 0 &&
2476 mddev->delta_disks >= 0 &&
NeilBrowndb0505d2017-10-17 16:18:36 +11002477 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2478 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2479 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002480 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002481 !test_bit(In_sync, &rdev->flags) &&
2482 mddev->curr_resync_completed > rdev->recovery_offset)
2483 rdev->recovery_offset = mddev->curr_resync_completed;
2484
NeilBrownf72ffdd2014-09-30 14:23:59 +10002485 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002486 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002487 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2488 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002489 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002490 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002491 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002492 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002493 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002494 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002495 md_error(mddev, rdev);
2496 }
2497 clear_bit(Blocked, &rdev->flags);
2498 clear_bit(BlockedBadBlocks, &rdev->flags);
2499 wake_up(&rdev->blocked_wait);
2500 }
2501 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002502 wake_up(&mddev->sb_wait);
2503 return;
2504 }
2505
NeilBrown85572d72014-12-15 12:56:56 +11002506 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002507
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002508 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002509
Shaohua Li29530792016-12-08 15:48:19 -08002510 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002511 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002512 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002513 /* just a clean<-> dirty transition, possibly leave spares alone,
2514 * though if events isn't the right even/odd, we will have to do
2515 * spares after all
2516 */
2517 nospares = 1;
2518 if (force_change)
2519 nospares = 0;
2520 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002521 /* If the array is degraded, then skipping spares is both
2522 * dangerous and fairly pointless.
2523 * Dangerous because a device that was removed from the array
2524 * might have a event_count that still looks up-to-date,
2525 * so it can be re-added without a resync.
2526 * Pointless because if there are any spares to skip,
2527 * then a recovery will happen and soon that array won't
2528 * be degraded any more and the spare can go back to sleep then.
2529 */
NeilBrown850b2b422006-10-03 01:15:46 -07002530 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002531
NeilBrown06d91a52005-06-21 17:17:12 -07002532 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002533
2534 /* If this is just a dirty<->clean transition, and the array is clean
2535 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002536 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002537 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002538 && mddev->can_decrease_events
2539 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002540 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002541 mddev->can_decrease_events = 0;
2542 } else {
NeilBrown42543762006-06-26 00:27:57 -07002543 /* otherwise we have to go forward and ... */
2544 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002545 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
NeilBrown403df472014-09-30 15:52:29 +10002548 /*
2549 * This 64-bit counter should never wrap.
2550 * Either we are in around ~1 trillion A.C., assuming
2551 * 1 reboot per second, or we have a bug...
2552 */
2553 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002554
NeilBrowndafb20f2012-03-19 12:46:39 +11002555 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002556 if (rdev->badblocks.changed)
2557 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002558 if (test_bit(Faulty, &rdev->flags))
2559 set_bit(FaultRecorded, &rdev->flags);
2560 }
NeilBrown2699b672011-07-28 11:31:47 +10002561
NeilBrowne6910632008-02-06 01:39:51 -08002562 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002563 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
NeilBrown36a4e1f2011-10-07 14:23:17 +11002565 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2566 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Shaohua Li504634f2016-11-18 09:44:08 -08002568 if (mddev->queue)
2569 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002570rewrite:
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002571 md_bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002572 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002574
NeilBrown42543762006-06-26 00:27:57 -07002575 if (rdev->sb_loaded != 1)
2576 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
NeilBrownf4667222013-12-09 12:04:56 +11002578 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002579 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002580 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002581 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002582 pr_debug("md: (write) %s's sb offset: %llu\n",
2583 bdevname(rdev->bdev, b),
2584 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002585 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002586 if (rdev->badblocks.size) {
2587 md_super_write(mddev, rdev,
2588 rdev->badblocks.sector,
2589 rdev->badblocks.size << 9,
2590 rdev->bb_page);
2591 rdev->badblocks.size = 0;
2592 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002593
NeilBrownf4667222013-12-09 12:04:56 +11002594 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002595 pr_debug("md: %s (skipping faulty)\n",
2596 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002597
NeilBrown7bfa19f2005-06-21 17:17:28 -07002598 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 /* only need to write one superblock... */
2600 break;
2601 }
NeilBrown46533ff2016-11-18 16:16:11 +11002602 if (md_super_wait(mddev) < 0)
2603 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002604 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002605
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002606 if (mddev_is_clustered(mddev) && ret == 0)
2607 md_cluster_ops->metadata_update_finish(mddev);
2608
NeilBrown850b2b422006-10-03 01:15:46 -07002609 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002610 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2611 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002612 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002613 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002614 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002615 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2616 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002617
NeilBrowndafb20f2012-03-19 12:46:39 +11002618 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002619 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2620 clear_bit(Blocked, &rdev->flags);
2621
2622 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002623 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002624 clear_bit(BlockedBadBlocks, &rdev->flags);
2625 wake_up(&rdev->blocked_wait);
2626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002628EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002630static int add_bound_rdev(struct md_rdev *rdev)
2631{
2632 struct mddev *mddev = rdev->mddev;
2633 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002634 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002635
Shaohua Li87d4d912016-01-06 14:37:14 -08002636 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002637 /* If there is hot_add_disk but no hot_remove_disk
2638 * then added disks for geometry changes,
2639 * and should be added immediately.
2640 */
2641 super_types[mddev->major_version].
2642 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002643 if (add_journal)
2644 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002645 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002646 if (add_journal)
2647 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002648 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002649 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002650 return err;
2651 }
2652 }
2653 sysfs_notify_dirent_safe(rdev->sysfs_state);
2654
Shaohua Li29530792016-12-08 15:48:19 -08002655 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002656 if (mddev->degraded)
2657 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2658 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2659 md_new_event(mddev);
2660 md_wakeup_thread(mddev->thread);
2661 return 0;
2662}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Andre Noll7f6ce762008-03-23 18:34:54 +01002664/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002665 * We want to accept with case. For this we use cmd_match.
2666 */
2667static int cmd_match(const char *cmd, const char *str)
2668{
2669 /* See if cmd, written into a sysfs file, matches
2670 * str. They must either be the same, or cmd can
2671 * have a trailing newline
2672 */
2673 while (*cmd && *str && *cmd == *str) {
2674 cmd++;
2675 str++;
2676 }
2677 if (*cmd == '\n')
2678 cmd++;
2679 if (*str || *cmd)
2680 return 0;
2681 return 1;
2682}
2683
NeilBrown86e6ffd2005-11-08 21:39:24 -08002684struct rdev_sysfs_entry {
2685 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002686 ssize_t (*show)(struct md_rdev *, char *);
2687 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002688};
2689
2690static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002691state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002692{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002693 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002694 size_t len = 0;
Mark Rutland6aa7de02017-10-23 14:07:29 -07002695 unsigned long flags = READ_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002696
NeilBrown758bfc82014-12-15 12:56:59 +11002697 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002698 (!test_bit(ExternalBbl, &flags) &&
2699 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002700 len += sprintf(page+len, "faulty%s", sep);
2701 if (test_bit(In_sync, &flags))
2702 len += sprintf(page+len, "in_sync%s", sep);
2703 if (test_bit(Journal, &flags))
2704 len += sprintf(page+len, "journal%s", sep);
2705 if (test_bit(WriteMostly, &flags))
2706 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002707 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002708 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002709 && !test_bit(Faulty, &flags)))
2710 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002711 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002712 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002713 !test_bit(In_sync, &flags))
2714 len += sprintf(page+len, "spare%s", sep);
2715 if (test_bit(WriteErrorSeen, &flags))
2716 len += sprintf(page+len, "write_error%s", sep);
2717 if (test_bit(WantReplacement, &flags))
2718 len += sprintf(page+len, "want_replacement%s", sep);
2719 if (test_bit(Replacement, &flags))
2720 len += sprintf(page+len, "replacement%s", sep);
2721 if (test_bit(ExternalBbl, &flags))
2722 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002723 if (test_bit(FailFast, &flags))
2724 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002725
2726 if (len)
2727 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002728
NeilBrown86e6ffd2005-11-08 21:39:24 -08002729 return len+sprintf(page+len, "\n");
2730}
2731
NeilBrown45dc2de2006-06-26 00:27:58 -07002732static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002733state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002734{
2735 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002736 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002737 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002738 * writemostly - sets write_mostly
2739 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002740 * blocked - sets the Blocked flags
2741 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002742 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002743 * -insync - clear Insync for a device with a slot assigned,
2744 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002745 * write_error - sets WriteErrorSeen
2746 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002747 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002748 */
2749 int err = -EINVAL;
2750 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2751 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002752 if (test_bit(Faulty, &rdev->flags))
2753 err = 0;
2754 else
2755 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002756 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002757 if (rdev->mddev->pers) {
2758 clear_bit(Blocked, &rdev->flags);
2759 remove_and_add_spares(rdev->mddev, rdev);
2760 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002761 if (rdev->raid_disk >= 0)
2762 err = -EBUSY;
2763 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002764 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002765 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002766 if (mddev_is_clustered(mddev))
2767 err = md_cluster_ops->remove_disk(mddev, rdev);
2768
2769 if (err == 0) {
2770 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002771 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002772 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002773 md_wakeup_thread(mddev->thread);
2774 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002775 md_new_event(mddev);
2776 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002777 }
NeilBrownf6556752006-06-26 00:28:01 -07002778 } else if (cmd_match(buf, "writemostly")) {
2779 set_bit(WriteMostly, &rdev->flags);
2780 err = 0;
2781 } else if (cmd_match(buf, "-writemostly")) {
2782 clear_bit(WriteMostly, &rdev->flags);
2783 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002784 } else if (cmd_match(buf, "blocked")) {
2785 set_bit(Blocked, &rdev->flags);
2786 err = 0;
2787 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002788 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002789 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002790 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002791 /* metadata handler doesn't understand badblocks,
2792 * so we need to fail the device
2793 */
2794 md_error(rdev->mddev, rdev);
2795 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002796 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002797 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002798 wake_up(&rdev->blocked_wait);
2799 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2800 md_wakeup_thread(rdev->mddev->thread);
2801
2802 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002803 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2804 set_bit(In_sync, &rdev->flags);
2805 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11002806 } else if (cmd_match(buf, "failfast")) {
2807 set_bit(FailFast, &rdev->flags);
2808 err = 0;
2809 } else if (cmd_match(buf, "-failfast")) {
2810 clear_bit(FailFast, &rdev->flags);
2811 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002812 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2813 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002814 if (rdev->mddev->pers == NULL) {
2815 clear_bit(In_sync, &rdev->flags);
2816 rdev->saved_raid_disk = rdev->raid_disk;
2817 rdev->raid_disk = -1;
2818 err = 0;
2819 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002820 } else if (cmd_match(buf, "write_error")) {
2821 set_bit(WriteErrorSeen, &rdev->flags);
2822 err = 0;
2823 } else if (cmd_match(buf, "-write_error")) {
2824 clear_bit(WriteErrorSeen, &rdev->flags);
2825 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002826 } else if (cmd_match(buf, "want_replacement")) {
2827 /* Any non-spare device that is not a replacement can
2828 * become want_replacement at any time, but we then need to
2829 * check if recovery is needed.
2830 */
2831 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002832 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002833 !test_bit(Replacement, &rdev->flags))
2834 set_bit(WantReplacement, &rdev->flags);
2835 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2836 md_wakeup_thread(rdev->mddev->thread);
2837 err = 0;
2838 } else if (cmd_match(buf, "-want_replacement")) {
2839 /* Clearing 'want_replacement' is always allowed.
2840 * Once replacements starts it is too late though.
2841 */
2842 err = 0;
2843 clear_bit(WantReplacement, &rdev->flags);
2844 } else if (cmd_match(buf, "replacement")) {
2845 /* Can only set a device as a replacement when array has not
2846 * yet been started. Once running, replacement is automatic
2847 * from spares, or by assigning 'slot'.
2848 */
2849 if (rdev->mddev->pers)
2850 err = -EBUSY;
2851 else {
2852 set_bit(Replacement, &rdev->flags);
2853 err = 0;
2854 }
2855 } else if (cmd_match(buf, "-replacement")) {
2856 /* Similarly, can only clear Replacement before start */
2857 if (rdev->mddev->pers)
2858 err = -EBUSY;
2859 else {
2860 clear_bit(Replacement, &rdev->flags);
2861 err = 0;
2862 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002863 } else if (cmd_match(buf, "re-add")) {
NeilBrown011abdc2018-04-26 14:46:29 +10002864 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
2865 rdev->saved_raid_disk >= 0) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002866 /* clear_bit is performed _after_ all the devices
2867 * have their local Faulty bit cleared. If any writes
2868 * happen in the meantime in the local node, they
2869 * will land in the local bitmap, which will be synced
2870 * by this node eventually
2871 */
2872 if (!mddev_is_clustered(rdev->mddev) ||
2873 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2874 clear_bit(Faulty, &rdev->flags);
2875 err = add_bound_rdev(rdev);
2876 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002877 } else
2878 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002879 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2880 set_bit(ExternalBbl, &rdev->flags);
2881 rdev->badblocks.shift = 0;
2882 err = 0;
2883 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2884 clear_bit(ExternalBbl, &rdev->flags);
2885 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002886 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002887 if (!err)
2888 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002889 return err ? err : len;
2890}
NeilBrown80ca3a42006-07-10 04:44:18 -07002891static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002892__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002893
2894static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002895errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002896{
2897 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2898}
2899
2900static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002901errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002902{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002903 unsigned int n;
2904 int rv;
2905
2906 rv = kstrtouint(buf, 10, &n);
2907 if (rv < 0)
2908 return rv;
2909 atomic_set(&rdev->corrected_errors, n);
2910 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002911}
2912static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002913__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002914
NeilBrown014236d2006-01-06 00:20:55 -08002915static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002916slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002917{
Shaohua Lif2076e72015-10-08 21:54:12 -07002918 if (test_bit(Journal, &rdev->flags))
2919 return sprintf(page, "journal\n");
2920 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002921 return sprintf(page, "none\n");
2922 else
2923 return sprintf(page, "%d\n", rdev->raid_disk);
2924}
2925
2926static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002927slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002928{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002929 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002930 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002931
Shaohua Lif2076e72015-10-08 21:54:12 -07002932 if (test_bit(Journal, &rdev->flags))
2933 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002934 if (strncmp(buf, "none", 4)==0)
2935 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002936 else {
2937 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2938 if (err < 0)
2939 return err;
2940 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002941 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002942 /* Setting 'slot' on an active array requires also
2943 * updating the 'rd%d' link, and communicating
2944 * with the personality with ->hot_*_disk.
2945 * For now we only support removing
2946 * failed/spare devices. This normally happens automatically,
2947 * but not when the metadata is externally managed.
2948 */
NeilBrownc303da62008-02-06 01:39:51 -08002949 if (rdev->raid_disk == -1)
2950 return -EEXIST;
2951 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002952 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002953 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002954 clear_bit(Blocked, &rdev->flags);
2955 remove_and_add_spares(rdev->mddev, rdev);
2956 if (rdev->raid_disk >= 0)
2957 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002958 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2959 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002960 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002961 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002962 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002963 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002964 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002965
2966 if (rdev->raid_disk != -1)
2967 return -EBUSY;
2968
NeilBrownc6751b22011-02-02 11:57:13 +11002969 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2970 return -EBUSY;
2971
Neil Brown6c2fce22008-06-28 08:31:31 +10002972 if (rdev->mddev->pers->hot_add_disk == NULL)
2973 return -EINVAL;
2974
NeilBrownba1b41b2011-01-14 09:14:34 +11002975 if (slot >= rdev->mddev->raid_disks &&
2976 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2977 return -ENOSPC;
2978
Neil Brown6c2fce22008-06-28 08:31:31 +10002979 rdev->raid_disk = slot;
2980 if (test_bit(In_sync, &rdev->flags))
2981 rdev->saved_raid_disk = slot;
2982 else
2983 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002984 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002985 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002986 err = rdev->mddev->pers->
2987 hot_add_disk(rdev->mddev, rdev);
2988 if (err) {
2989 rdev->raid_disk = -1;
2990 return err;
2991 } else
2992 sysfs_notify_dirent_safe(rdev->sysfs_state);
2993 if (sysfs_link_rdev(rdev->mddev, rdev))
2994 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002995 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002996 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002997 if (slot >= rdev->mddev->raid_disks &&
2998 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002999 return -ENOSPC;
3000 rdev->raid_disk = slot;
3001 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08003002 clear_bit(Faulty, &rdev->flags);
3003 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08003004 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003005 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08003006 }
NeilBrown014236d2006-01-06 00:20:55 -08003007 return len;
3008}
3009
NeilBrown014236d2006-01-06 00:20:55 -08003010static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07003011__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08003012
NeilBrown93c8cad2006-01-06 00:20:56 -08003013static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003014offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08003015{
Andrew Morton6961ece2006-01-06 00:20:59 -08003016 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08003017}
3018
3019static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003020offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08003021{
NeilBrownc6563a82012-05-21 09:27:00 +10003022 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003023 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003024 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10003025 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003026 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003027 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08003028 /* Must set offset before size, so overlap checks
3029 * can be sane */
3030 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08003031 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10003032 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08003033 return len;
3034}
3035
3036static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07003037__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08003038
NeilBrownc6563a82012-05-21 09:27:00 +10003039static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3040{
3041 return sprintf(page, "%llu\n",
3042 (unsigned long long)rdev->new_data_offset);
3043}
3044
3045static ssize_t new_offset_store(struct md_rdev *rdev,
3046 const char *buf, size_t len)
3047{
3048 unsigned long long new_offset;
3049 struct mddev *mddev = rdev->mddev;
3050
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003051 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10003052 return -EINVAL;
3053
NeilBrownf851b602014-12-11 10:02:10 +11003054 if (mddev->sync_thread ||
3055 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10003056 return -EBUSY;
3057 if (new_offset == rdev->data_offset)
3058 /* reset is always permitted */
3059 ;
3060 else if (new_offset > rdev->data_offset) {
3061 /* must not push array size beyond rdev_sectors */
3062 if (new_offset - rdev->data_offset
3063 + mddev->dev_sectors > rdev->sectors)
3064 return -E2BIG;
3065 }
3066 /* Metadata worries about other space details. */
3067
3068 /* decreasing the offset is inconsistent with a backwards
3069 * reshape.
3070 */
3071 if (new_offset < rdev->data_offset &&
3072 mddev->reshape_backwards)
3073 return -EINVAL;
3074 /* Increasing offset is inconsistent with forwards
3075 * reshape. reshape_direction should be set to
3076 * 'backwards' first.
3077 */
3078 if (new_offset > rdev->data_offset &&
3079 !mddev->reshape_backwards)
3080 return -EINVAL;
3081
3082 if (mddev->pers && mddev->persistent &&
3083 !super_types[mddev->major_version]
3084 .allow_new_offset(rdev, new_offset))
3085 return -E2BIG;
3086 rdev->new_data_offset = new_offset;
3087 if (new_offset > rdev->data_offset)
3088 mddev->reshape_backwards = 1;
3089 else if (new_offset < rdev->data_offset)
3090 mddev->reshape_backwards = 0;
3091
3092 return len;
3093}
3094static struct rdev_sysfs_entry rdev_new_offset =
3095__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3096
NeilBrown83303b62006-01-06 00:21:06 -08003097static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003098rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003099{
Andre Nolldd8ac332009-03-31 14:33:13 +11003100 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003101}
3102
NeilBrownc5d79ad2008-02-06 01:39:54 -08003103static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3104{
3105 /* check if two start/length pairs overlap */
3106 if (s1+l1 <= s2)
3107 return 0;
3108 if (s2+l2 <= s1)
3109 return 0;
3110 return 1;
3111}
3112
Dan Williamsb522adc2009-03-31 15:00:31 +11003113static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3114{
3115 unsigned long long blocks;
3116 sector_t new;
3117
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003118 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003119 return -EINVAL;
3120
3121 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3122 return -EINVAL; /* sector conversion overflow */
3123
3124 new = blocks * 2;
3125 if (new != blocks * 2)
3126 return -EINVAL; /* unsigned long long to sector_t overflow */
3127
3128 *sectors = new;
3129 return 0;
3130}
3131
NeilBrown83303b62006-01-06 00:21:06 -08003132static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003133rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003134{
NeilBrownfd01b882011-10-11 16:47:53 +11003135 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003136 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003137 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003138
Shaohua Lif2076e72015-10-08 21:54:12 -07003139 if (test_bit(Journal, &rdev->flags))
3140 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003141 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003142 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003143 if (rdev->data_offset != rdev->new_data_offset)
3144 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003145 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003146 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003147 sectors = super_types[my_mddev->major_version].
3148 rdev_size_change(rdev, sectors);
3149 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003150 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003151 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003152 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003153 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003154 if (!my_mddev->pers->resize)
3155 /* Cannot change size for RAID0 or Linear etc */
3156 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003157 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003158 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003159 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003160
Andre Nolldd8ac332009-03-31 14:33:13 +11003161 rdev->sectors = sectors;
3162 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003163 /* Need to check that all other rdevs with the same
3164 * ->bdev do not overlap. 'rcu' is sufficient to walk
3165 * the rdev lists safely.
3166 * This check does not provide a hard guarantee, it
3167 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003168 */
NeilBrownfd01b882011-10-11 16:47:53 +11003169 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003170 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003171 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003172
NeilBrown8b1afc32014-09-29 15:33:20 +10003173 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003174 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003175 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003176
NeilBrowndafb20f2012-03-19 12:46:39 +11003177 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003178 if (rdev->bdev == rdev2->bdev &&
3179 rdev != rdev2 &&
3180 overlaps(rdev->data_offset, rdev->sectors,
3181 rdev2->data_offset,
3182 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003183 overlap = 1;
3184 break;
3185 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003186 if (overlap) {
3187 mddev_put(mddev);
3188 break;
3189 }
3190 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003191 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003192 if (overlap) {
3193 /* Someone else could have slipped in a size
3194 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003195 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003196 * safe, and trust userspace not to race with
3197 * itself
3198 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003199 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003200 return -EBUSY;
3201 }
3202 }
NeilBrown83303b62006-01-06 00:21:06 -08003203 return len;
3204}
3205
3206static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003207__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003208
NeilBrown3cb03002011-10-11 16:45:26 +11003209static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003210{
3211 unsigned long long recovery_start = rdev->recovery_offset;
3212
3213 if (test_bit(In_sync, &rdev->flags) ||
3214 recovery_start == MaxSector)
3215 return sprintf(page, "none\n");
3216
3217 return sprintf(page, "%llu\n", recovery_start);
3218}
3219
NeilBrown3cb03002011-10-11 16:45:26 +11003220static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003221{
3222 unsigned long long recovery_start;
3223
3224 if (cmd_match(buf, "none"))
3225 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003226 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003227 return -EINVAL;
3228
3229 if (rdev->mddev->pers &&
3230 rdev->raid_disk >= 0)
3231 return -EBUSY;
3232
3233 rdev->recovery_offset = recovery_start;
3234 if (recovery_start == MaxSector)
3235 set_bit(In_sync, &rdev->flags);
3236 else
3237 clear_bit(In_sync, &rdev->flags);
3238 return len;
3239}
3240
3241static struct rdev_sysfs_entry rdev_recovery_start =
3242__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3243
Vishal Vermafc974ee2015-12-24 19:20:34 -07003244/* sysfs access to bad-blocks list.
3245 * We present two files.
3246 * 'bad-blocks' lists sector numbers and lengths of ranges that
3247 * are recorded as bad. The list is truncated to fit within
3248 * the one-page limit of sysfs.
3249 * Writing "sector length" to this file adds an acknowledged
3250 * bad block list.
3251 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3252 * been acknowledged. Writing to this file adds bad blocks
3253 * without acknowledging them. This is largely for testing.
3254 */
NeilBrown3cb03002011-10-11 16:45:26 +11003255static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003256{
3257 return badblocks_show(&rdev->badblocks, page, 0);
3258}
NeilBrown3cb03002011-10-11 16:45:26 +11003259static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003260{
NeilBrownde393cd2011-07-28 11:31:48 +10003261 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3262 /* Maybe that ack was all we needed */
3263 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3264 wake_up(&rdev->blocked_wait);
3265 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003266}
3267static struct rdev_sysfs_entry rdev_bad_blocks =
3268__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3269
NeilBrown3cb03002011-10-11 16:45:26 +11003270static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003271{
3272 return badblocks_show(&rdev->badblocks, page, 1);
3273}
NeilBrown3cb03002011-10-11 16:45:26 +11003274static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003275{
3276 return badblocks_store(&rdev->badblocks, page, len, 1);
3277}
3278static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3279__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3280
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003281static ssize_t
3282ppl_sector_show(struct md_rdev *rdev, char *page)
3283{
3284 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3285}
3286
3287static ssize_t
3288ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3289{
3290 unsigned long long sector;
3291
3292 if (kstrtoull(buf, 10, &sector) < 0)
3293 return -EINVAL;
3294 if (sector != (sector_t)sector)
3295 return -EINVAL;
3296
3297 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3298 rdev->raid_disk >= 0)
3299 return -EBUSY;
3300
3301 if (rdev->mddev->persistent) {
3302 if (rdev->mddev->major_version == 0)
3303 return -EINVAL;
3304 if ((sector > rdev->sb_start &&
3305 sector - rdev->sb_start > S16_MAX) ||
3306 (sector < rdev->sb_start &&
3307 rdev->sb_start - sector > -S16_MIN))
3308 return -EINVAL;
3309 rdev->ppl.offset = sector - rdev->sb_start;
3310 } else if (!rdev->mddev->external) {
3311 return -EBUSY;
3312 }
3313 rdev->ppl.sector = sector;
3314 return len;
3315}
3316
3317static struct rdev_sysfs_entry rdev_ppl_sector =
3318__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3319
3320static ssize_t
3321ppl_size_show(struct md_rdev *rdev, char *page)
3322{
3323 return sprintf(page, "%u\n", rdev->ppl.size);
3324}
3325
3326static ssize_t
3327ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3328{
3329 unsigned int size;
3330
3331 if (kstrtouint(buf, 10, &size) < 0)
3332 return -EINVAL;
3333
3334 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3335 rdev->raid_disk >= 0)
3336 return -EBUSY;
3337
3338 if (rdev->mddev->persistent) {
3339 if (rdev->mddev->major_version == 0)
3340 return -EINVAL;
3341 if (size > U16_MAX)
3342 return -EINVAL;
3343 } else if (!rdev->mddev->external) {
3344 return -EBUSY;
3345 }
3346 rdev->ppl.size = size;
3347 return len;
3348}
3349
3350static struct rdev_sysfs_entry rdev_ppl_size =
3351__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3352
NeilBrown86e6ffd2005-11-08 21:39:24 -08003353static struct attribute *rdev_default_attrs[] = {
3354 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003355 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003356 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003357 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003358 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003359 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003360 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003361 &rdev_bad_blocks.attr,
3362 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003363 &rdev_ppl_sector.attr,
3364 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003365 NULL,
3366};
3367static ssize_t
3368rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3369{
3370 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003371 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003372
3373 if (!entry->show)
3374 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003375 if (!rdev->mddev)
3376 return -EBUSY;
3377 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003378}
3379
3380static ssize_t
3381rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3382 const char *page, size_t length)
3383{
3384 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003385 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003386 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003387 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003388
3389 if (!entry->store)
3390 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003391 if (!capable(CAP_SYS_ADMIN))
3392 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003393 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003394 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003395 if (rdev->mddev == NULL)
3396 rv = -EBUSY;
3397 else
3398 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003399 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003400 }
3401 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003402}
3403
3404static void rdev_free(struct kobject *ko)
3405{
NeilBrown3cb03002011-10-11 16:45:26 +11003406 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003407 kfree(rdev);
3408}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003409static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003410 .show = rdev_attr_show,
3411 .store = rdev_attr_store,
3412};
3413static struct kobj_type rdev_ktype = {
3414 .release = rdev_free,
3415 .sysfs_ops = &rdev_sysfs_ops,
3416 .default_attrs = rdev_default_attrs,
3417};
3418
NeilBrown3cb03002011-10-11 16:45:26 +11003419int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003420{
3421 rdev->desc_nr = -1;
3422 rdev->saved_raid_disk = -1;
3423 rdev->raid_disk = -1;
3424 rdev->flags = 0;
3425 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003426 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003427 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003428 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003429 rdev->sb_loaded = 0;
3430 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003431 atomic_set(&rdev->nr_pending, 0);
3432 atomic_set(&rdev->read_errors, 0);
3433 atomic_set(&rdev->corrected_errors, 0);
3434
3435 INIT_LIST_HEAD(&rdev->same_set);
3436 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003437
3438 /* Add space to store bad block list.
3439 * This reserves the space even on arrays where it cannot
3440 * be used - I wonder if that matters
3441 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003442 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003443}
3444EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445/*
3446 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3447 *
3448 * mark the device faulty if:
3449 *
3450 * - the device is nonexistent (zero size)
3451 * - the device has no valid superblock
3452 *
3453 * a faulty rdev _never_ has rdev->sb set.
3454 */
NeilBrown3cb03002011-10-11 16:45:26 +11003455static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
3457 char b[BDEVNAME_SIZE];
3458 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003459 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 sector_t size;
3461
NeilBrown9ffae0c2006-01-06 00:20:32 -08003462 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003463 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
NeilBrown2230dfe2011-07-28 11:31:46 +10003466 err = md_rdev_init(rdev);
3467 if (err)
3468 goto abort_free;
3469 err = alloc_disk_sb(rdev);
3470 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 goto abort_free;
3472
NeilBrownc5d79ad2008-02-06 01:39:54 -08003473 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 if (err)
3475 goto abort_free;
3476
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003477 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003478
Mike Snitzer77304d22010-11-08 14:39:12 +01003479 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003481 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 bdevname(rdev->bdev,b));
3483 err = -EINVAL;
3484 goto abort_free;
3485 }
3486
3487 if (super_format >= 0) {
3488 err = super_types[super_format].
3489 load_super(rdev, NULL, super_minor);
3490 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003491 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003492 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003493 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 goto abort_free;
3495 }
3496 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003497 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 bdevname(rdev->bdev,b));
3499 goto abort_free;
3500 }
3501 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 return rdev;
3504
3505abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003506 if (rdev->bdev)
3507 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003508 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 kfree(rdev);
3510 return ERR_PTR(err);
3511}
3512
3513/*
3514 * Check a full RAID array for plausibility
3515 */
3516
NeilBrownf72ffdd2014-09-30 14:23:59 +10003517static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518{
3519 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003520 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 char b[BDEVNAME_SIZE];
3522
3523 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003524 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 switch (super_types[mddev->major_version].
3526 load_super(rdev, freshest, mddev->minor_version)) {
3527 case 1:
3528 freshest = rdev;
3529 break;
3530 case 0:
3531 break;
3532 default:
NeilBrown9d487392016-11-02 14:16:49 +11003533 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003535 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 }
3537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 super_types[mddev->major_version].
3539 validate_super(mddev, freshest);
3540
3541 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003542 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003543 if (mddev->max_disks &&
3544 (rdev->desc_nr >= mddev->max_disks ||
3545 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003546 pr_warn("md: %s: %s: only %d devices permitted\n",
3547 mdname(mddev), bdevname(rdev->bdev, b),
3548 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003549 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003550 continue;
3551 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003552 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 if (super_types[mddev->major_version].
3554 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003555 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003557 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 continue;
3559 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 if (mddev->level == LEVEL_MULTIPATH) {
3562 rdev->desc_nr = i++;
3563 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003564 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003565 } else if (rdev->raid_disk >=
3566 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3567 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003568 rdev->raid_disk = -1;
3569 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 }
3571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572}
3573
NeilBrown72e02072009-12-14 12:49:55 +11003574/* Read a fixed-point number.
3575 * Numbers in sysfs attributes should be in "standard" units where
3576 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003577 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003578 * milliseconds or jiffies.
3579 * This function takes a decimal number with a possible fractional
3580 * component, and produces an integer which is the result of
3581 * multiplying that number by 10^'scale'.
3582 * all without any floating-point arithmetic.
3583 */
3584int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3585{
3586 unsigned long result = 0;
3587 long decimals = -1;
3588 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3589 if (*cp == '.')
3590 decimals = 0;
3591 else if (decimals < scale) {
3592 unsigned int value;
3593 value = *cp - '0';
3594 result = result * 10 + value;
3595 if (decimals >= 0)
3596 decimals++;
3597 }
3598 cp++;
3599 }
3600 if (*cp == '\n')
3601 cp++;
3602 if (*cp)
3603 return -EINVAL;
3604 if (decimals < 0)
3605 decimals = 0;
3606 while (decimals < scale) {
3607 result *= 10;
3608 decimals ++;
3609 }
3610 *res = result;
3611 return 0;
3612}
3613
NeilBrowneae17012005-11-08 21:39:23 -08003614static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003615safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003616{
3617 int msec = (mddev->safemode_delay*1000)/HZ;
3618 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3619}
3620static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003621safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003622{
NeilBrown16f17b32006-06-26 00:27:37 -07003623 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003624
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003625 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003626 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003627 return -EINVAL;
3628 }
3629
NeilBrown72e02072009-12-14 12:49:55 +11003630 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003631 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003632 if (msec == 0)
3633 mddev->safemode_delay = 0;
3634 else {
NeilBrown19052c02008-08-05 15:54:13 +10003635 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003636 unsigned long new_delay = (msec*HZ)/1000;
3637
3638 if (new_delay == 0)
3639 new_delay = 1;
3640 mddev->safemode_delay = new_delay;
3641 if (new_delay < old_delay || old_delay == 0)
3642 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003643 }
3644 return len;
3645}
3646static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003647__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003648
3649static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003650level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003651{
NeilBrown36d091f2014-12-15 12:56:58 +11003652 struct md_personality *p;
3653 int ret;
3654 spin_lock(&mddev->lock);
3655 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003656 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003657 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003658 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003659 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003660 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003661 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003662 else
NeilBrown36d091f2014-12-15 12:56:58 +11003663 ret = 0;
3664 spin_unlock(&mddev->lock);
3665 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003666}
3667
NeilBrownd9d166c2006-01-06 00:20:51 -08003668static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003669level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003670{
Dan Williamsf2859af2010-05-02 10:04:16 -07003671 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003672 ssize_t rv;
3673 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003674 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003675 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003676 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003677 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003678
NeilBrown67918752014-12-15 12:57:01 +11003679 if (slen == 0 || slen >= sizeof(clevel))
3680 return -EINVAL;
3681
3682 rv = mddev_lock(mddev);
3683 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003684 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003685
3686 if (mddev->pers == NULL) {
3687 strncpy(mddev->clevel, buf, slen);
3688 if (mddev->clevel[slen-1] == '\n')
3689 slen--;
3690 mddev->clevel[slen] = 0;
3691 mddev->level = LEVEL_NONE;
3692 rv = len;
3693 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003694 }
NeilBrown67918752014-12-15 12:57:01 +11003695 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003696 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003697 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003698
3699 /* request to change the personality. Need to ensure:
3700 * - array is not engaged in resync/recovery/reshape
3701 * - old personality can be suspended
3702 * - new personality will access other array.
3703 */
3704
NeilBrown67918752014-12-15 12:57:01 +11003705 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003706 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003707 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003708 mddev->reshape_position != MaxSector ||
3709 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003710 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003711
NeilBrown67918752014-12-15 12:57:01 +11003712 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003713 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003714 pr_warn("md: %s: %s does not support online personality change\n",
3715 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003716 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003717 }
3718
3719 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003720 strncpy(clevel, buf, slen);
3721 if (clevel[slen-1] == '\n')
3722 slen--;
3723 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003724 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003725 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003726
Dan Williamsf2859af2010-05-02 10:04:16 -07003727 if (request_module("md-%s", clevel) != 0)
3728 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003729 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003730 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003731 if (!pers || !try_module_get(pers->owner)) {
3732 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003733 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003734 rv = -EINVAL;
3735 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003736 }
3737 spin_unlock(&pers_lock);
3738
3739 if (pers == mddev->pers) {
3740 /* Nothing to do! */
3741 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003742 rv = len;
3743 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003744 }
3745 if (!pers->takeover) {
3746 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003747 pr_warn("md: %s: %s does not support personality takeover\n",
3748 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003749 rv = -EINVAL;
3750 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003751 }
3752
NeilBrowndafb20f2012-03-19 12:46:39 +11003753 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003754 rdev->new_raid_disk = rdev->raid_disk;
3755
NeilBrown245f46c2009-03-31 14:39:39 +11003756 /* ->takeover must set new_* and/or delta_disks
3757 * if it succeeds, and may set them when it fails.
3758 */
3759 priv = pers->takeover(mddev);
3760 if (IS_ERR(priv)) {
3761 mddev->new_level = mddev->level;
3762 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003763 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003764 mddev->raid_disks -= mddev->delta_disks;
3765 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003766 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003767 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003768 pr_warn("md: %s: %s would not accept array\n",
3769 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003770 rv = PTR_ERR(priv);
3771 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003772 }
3773
3774 /* Looks like we have a winner */
3775 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003776 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003777
3778 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003779 oldpers = mddev->pers;
3780 oldpriv = mddev->private;
3781 mddev->pers = pers;
3782 mddev->private = priv;
3783 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3784 mddev->level = mddev->new_level;
3785 mddev->layout = mddev->new_layout;
3786 mddev->chunk_sectors = mddev->new_chunk_sectors;
3787 mddev->delta_disks = 0;
3788 mddev->reshape_backwards = 0;
3789 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003790 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003791
NeilBrowndb721d32014-12-15 12:56:58 +11003792 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003793 mddev->external) {
3794 /* We are converting from a no-redundancy array
3795 * to a redundancy array and metadata is managed
3796 * externally so we need to be sure that writes
3797 * won't block due to a need to transition
3798 * clean->dirty
3799 * until external management is started.
3800 */
3801 mddev->in_sync = 0;
3802 mddev->safemode_delay = 0;
3803 mddev->safemode = 0;
3804 }
3805
NeilBrowndb721d32014-12-15 12:56:58 +11003806 oldpers->free(mddev, oldpriv);
3807
3808 if (oldpers->sync_request == NULL &&
3809 pers->sync_request != NULL) {
3810 /* need to add the md_redundancy_group */
3811 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11003812 pr_warn("md: cannot register extra attributes for %s\n",
3813 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11003814 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3815 }
3816 if (oldpers->sync_request != NULL &&
3817 pers->sync_request == NULL) {
3818 /* need to remove the md_redundancy_group */
3819 if (mddev->to_remove == NULL)
3820 mddev->to_remove = &md_redundancy_group;
3821 }
3822
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003823 module_put(oldpers->owner);
3824
NeilBrowndafb20f2012-03-19 12:46:39 +11003825 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003826 if (rdev->raid_disk < 0)
3827 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003828 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003829 rdev->new_raid_disk = -1;
3830 if (rdev->new_raid_disk == rdev->raid_disk)
3831 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003832 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003833 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003834 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003835 if (rdev->raid_disk < 0)
3836 continue;
3837 if (rdev->new_raid_disk == rdev->raid_disk)
3838 continue;
3839 rdev->raid_disk = rdev->new_raid_disk;
3840 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003841 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003842 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003843 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11003844 pr_warn("md: cannot register rd%d for %s after level change\n",
3845 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003846 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003847 }
3848
NeilBrowndb721d32014-12-15 12:56:58 +11003849 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003850 /* this is now an array without redundancy, so
3851 * it must always be in_sync
3852 */
3853 mddev->in_sync = 1;
3854 del_timer_sync(&mddev->safemode_timer);
3855 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003856 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003857 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08003858 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003859 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003860 if (!mddev->thread)
3861 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003862 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003863 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003864 rv = len;
3865out_unlock:
3866 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003867 return rv;
3868}
3869
3870static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003871__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003872
NeilBrownd4dbd022006-06-26 00:27:59 -07003873static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003874layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003875{
3876 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003877 if (mddev->reshape_position != MaxSector &&
3878 mddev->layout != mddev->new_layout)
3879 return sprintf(page, "%d (%d)\n",
3880 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003881 return sprintf(page, "%d\n", mddev->layout);
3882}
3883
3884static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003885layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003886{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003887 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003888 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003889
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003890 err = kstrtouint(buf, 10, &n);
3891 if (err < 0)
3892 return err;
NeilBrown67918752014-12-15 12:57:01 +11003893 err = mddev_lock(mddev);
3894 if (err)
3895 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003896
NeilBrownb3546032009-03-31 14:56:41 +11003897 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003898 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003899 err = -EBUSY;
3900 else if (mddev->ro)
3901 err = -EROFS;
3902 else {
3903 mddev->new_layout = n;
3904 err = mddev->pers->check_reshape(mddev);
3905 if (err)
3906 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003907 }
NeilBrownb3546032009-03-31 14:56:41 +11003908 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003909 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003910 if (mddev->reshape_position == MaxSector)
3911 mddev->layout = n;
3912 }
NeilBrown67918752014-12-15 12:57:01 +11003913 mddev_unlock(mddev);
3914 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003915}
3916static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003917__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003918
NeilBrowneae17012005-11-08 21:39:23 -08003919static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003920raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003921{
NeilBrownbb636542005-11-08 21:39:45 -08003922 if (mddev->raid_disks == 0)
3923 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003924 if (mddev->reshape_position != MaxSector &&
3925 mddev->delta_disks != 0)
3926 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3927 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003928 return sprintf(page, "%d\n", mddev->raid_disks);
3929}
3930
NeilBrownfd01b882011-10-11 16:47:53 +11003931static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003932
3933static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003934raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003935{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003936 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003937 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003938
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003939 err = kstrtouint(buf, 10, &n);
3940 if (err < 0)
3941 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003942
NeilBrown67918752014-12-15 12:57:01 +11003943 err = mddev_lock(mddev);
3944 if (err)
3945 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003946 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003947 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003948 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003949 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003950 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003951
NeilBrown67918752014-12-15 12:57:01 +11003952 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003953 rdev_for_each(rdev, mddev) {
3954 if (olddisks < n &&
3955 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003956 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003957 if (olddisks > n &&
3958 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003959 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003960 }
NeilBrown67918752014-12-15 12:57:01 +11003961 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003962 mddev->delta_disks = n - olddisks;
3963 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003964 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003965 } else
NeilBrownda943b992006-01-06 00:20:54 -08003966 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003967out_unlock:
3968 mddev_unlock(mddev);
3969 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003970}
3971static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003972__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003973
NeilBrown24dd4692005-11-08 21:39:26 -08003974static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003975chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003976{
NeilBrown08a02ec2007-05-09 02:35:38 -07003977 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003978 mddev->chunk_sectors != mddev->new_chunk_sectors)
3979 return sprintf(page, "%d (%d)\n",
3980 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003981 mddev->chunk_sectors << 9);
3982 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003983}
3984
3985static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003986chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003987{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003988 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003989 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003990
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003991 err = kstrtoul(buf, 10, &n);
3992 if (err < 0)
3993 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003994
NeilBrown67918752014-12-15 12:57:01 +11003995 err = mddev_lock(mddev);
3996 if (err)
3997 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003998 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003999 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004000 err = -EBUSY;
4001 else if (mddev->ro)
4002 err = -EROFS;
4003 else {
4004 mddev->new_chunk_sectors = n >> 9;
4005 err = mddev->pers->check_reshape(mddev);
4006 if (err)
4007 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10004008 }
NeilBrownb3546032009-03-31 14:56:41 +11004009 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10004010 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004011 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10004012 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004013 }
NeilBrown67918752014-12-15 12:57:01 +11004014 mddev_unlock(mddev);
4015 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08004016}
4017static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004018__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08004019
NeilBrowna94213b2006-06-26 00:28:00 -07004020static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004021resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07004022{
NeilBrownd1a7c502009-03-31 15:24:32 +11004023 if (mddev->recovery_cp == MaxSector)
4024 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07004025 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4026}
4027
4028static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004029resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07004030{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004031 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11004032 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004033
4034 if (cmd_match(buf, "none"))
4035 n = MaxSector;
4036 else {
4037 err = kstrtoull(buf, 10, &n);
4038 if (err < 0)
4039 return err;
4040 if (n != (sector_t)n)
4041 return -EINVAL;
4042 }
NeilBrowna94213b2006-06-26 00:28:00 -07004043
NeilBrown67918752014-12-15 12:57:01 +11004044 err = mddev_lock(mddev);
4045 if (err)
4046 return err;
NeilBrownb0986362011-05-11 15:52:21 +10004047 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11004048 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07004049
NeilBrown67918752014-12-15 12:57:01 +11004050 if (!err) {
4051 mddev->recovery_cp = n;
4052 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08004053 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004054 }
4055 mddev_unlock(mddev);
4056 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07004057}
4058static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10004059__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4060 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07004061
NeilBrown9e653b62006-06-26 00:27:58 -07004062/*
4063 * The array state can be:
4064 *
4065 * clear
4066 * No devices, no size, no level
4067 * Equivalent to STOP_ARRAY ioctl
4068 * inactive
4069 * May have some settings, but array is not active
4070 * all IO results in error
4071 * When written, doesn't tear down array, but just stops it
4072 * suspended (not supported yet)
4073 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01004074 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07004075 * readonly
4076 * no resync can happen. no superblocks get written.
4077 * write requests fail
4078 * read-auto
4079 * like readonly, but behaves like 'clean' on a write request.
4080 *
4081 * clean - no pending writes, but otherwise active.
4082 * When written to inactive array, starts without resync
4083 * If a write request arrives then
4084 * if metadata is known, mark 'dirty' and switch to 'active'.
4085 * if not known, block and switch to write-pending
4086 * If written to an active array that has pending writes, then fails.
4087 * active
4088 * fully active: IO and resync can be happening.
4089 * When written to inactive array, starts with resync
4090 *
4091 * write-pending
4092 * clean, but writes are blocked waiting for 'active' to be written.
4093 *
4094 * active-idle
4095 * like active, but no writes have been seen for a while (100msec).
4096 *
4097 */
4098enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4099 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004100static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004101 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4102 "write-pending", "active-idle", NULL };
4103
4104static int match_word(const char *word, char **list)
4105{
4106 int n;
4107 for (n=0; list[n]; n++)
4108 if (cmd_match(word, list[n]))
4109 break;
4110 return n;
4111}
4112
4113static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004114array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004115{
4116 enum array_state st = inactive;
4117
4118 if (mddev->pers)
4119 switch(mddev->ro) {
4120 case 1:
4121 st = readonly;
4122 break;
4123 case 2:
4124 st = read_auto;
4125 break;
4126 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004127 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004128 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004129 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004130 else if (mddev->in_sync)
4131 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004132 else if (mddev->safemode)
4133 st = active_idle;
4134 else
4135 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004136 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004137 }
4138 else {
4139 if (list_empty(&mddev->disks) &&
4140 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004141 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004142 st = clear;
4143 else
4144 st = inactive;
4145 }
4146 return sprintf(page, "%s\n", array_states[st]);
4147}
4148
NeilBrownf72ffdd2014-09-30 14:23:59 +10004149static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4150static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4151static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004152static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004153
4154static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004155array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004156{
NeilBrown6497709b2017-03-15 14:05:14 +11004157 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004158 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004159
4160 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4161 /* don't take reconfig_mutex when toggling between
4162 * clean and active
4163 */
4164 spin_lock(&mddev->lock);
4165 if (st == active) {
4166 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004167 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004168 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004169 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004170 } else /* st == clean */ {
4171 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004172 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004173 err = -EBUSY;
4174 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004175 if (!err)
4176 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004177 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004178 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004179 }
4180 err = mddev_lock(mddev);
4181 if (err)
4182 return err;
4183 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004184 switch(st) {
4185 case bad_word:
4186 break;
4187 case clear:
4188 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004189 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004190 break;
4191 case inactive:
4192 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004193 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004194 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004195 else
NeilBrowne6910632008-02-06 01:39:51 -08004196 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004197 break;
4198 case suspended:
4199 break; /* not supported yet */
4200 case readonly:
4201 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004202 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004203 else {
4204 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004205 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004206 err = do_md_run(mddev);
4207 }
4208 break;
4209 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004210 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004211 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004212 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004213 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004214 err = restart_array(mddev);
4215 if (err == 0) {
4216 mddev->ro = 2;
4217 set_disk_ro(mddev->gendisk, 0);
4218 }
NeilBrown9e653b62006-06-26 00:27:58 -07004219 } else {
4220 mddev->ro = 2;
4221 err = do_md_run(mddev);
4222 }
4223 break;
4224 case clean:
4225 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004226 err = restart_array(mddev);
4227 if (err)
4228 break;
NeilBrown85572d72014-12-15 12:56:56 +11004229 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004230 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004231 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004232 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004233 } else
4234 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004235 break;
4236 case active:
4237 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004238 err = restart_array(mddev);
4239 if (err)
4240 break;
Shaohua Li29530792016-12-08 15:48:19 -08004241 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004242 wake_up(&mddev->sb_wait);
4243 err = 0;
4244 } else {
4245 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004246 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004247 err = do_md_run(mddev);
4248 }
4249 break;
4250 case write_pending:
4251 case active_idle:
4252 /* these cannot be set */
4253 break;
4254 }
NeilBrown67918752014-12-15 12:57:01 +11004255
4256 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004257 if (mddev->hold_active == UNTIL_IOCTL)
4258 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004259 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004260 }
NeilBrown67918752014-12-15 12:57:01 +11004261 mddev_unlock(mddev);
4262 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004263}
NeilBrown80ca3a42006-07-10 04:44:18 -07004264static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004265__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004266
NeilBrown6d7ff7382006-01-06 00:21:16 -08004267static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004268max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004269 return sprintf(page, "%d\n",
4270 atomic_read(&mddev->max_corr_read_errors));
4271}
4272
4273static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004274max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004275{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004276 unsigned int n;
4277 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004278
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004279 rv = kstrtouint(buf, 10, &n);
4280 if (rv < 0)
4281 return rv;
4282 atomic_set(&mddev->max_corr_read_errors, n);
4283 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004284}
4285
4286static struct md_sysfs_entry max_corr_read_errors =
4287__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4288 max_corrected_read_errors_store);
4289
4290static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004291null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004292{
4293 return -EINVAL;
4294}
4295
4296static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004297new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004298{
4299 /* buf must be %d:%d\n? giving major and minor numbers */
4300 /* The new device is added to the array.
4301 * If the array has a persistent superblock, we read the
4302 * superblock to initialise info and check validity.
4303 * Otherwise, only checking done is that in bind_rdev_to_array,
4304 * which mainly checks size.
4305 */
4306 char *e;
4307 int major = simple_strtoul(buf, &e, 10);
4308 int minor;
4309 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004310 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004311 int err;
4312
4313 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4314 return -EINVAL;
4315 minor = simple_strtoul(e+1, &e, 10);
4316 if (*e && *e != '\n')
4317 return -EINVAL;
4318 dev = MKDEV(major, minor);
4319 if (major != MAJOR(dev) ||
4320 minor != MINOR(dev))
4321 return -EOVERFLOW;
4322
NeilBrown67918752014-12-15 12:57:01 +11004323 flush_workqueue(md_misc_wq);
4324
4325 err = mddev_lock(mddev);
4326 if (err)
4327 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004328 if (mddev->persistent) {
4329 rdev = md_import_device(dev, mddev->major_version,
4330 mddev->minor_version);
4331 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004332 struct md_rdev *rdev0
4333 = list_entry(mddev->disks.next,
4334 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004335 err = super_types[mddev->major_version]
4336 .load_super(rdev, rdev0, mddev->minor_version);
4337 if (err < 0)
4338 goto out;
4339 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004340 } else if (mddev->external)
4341 rdev = md_import_device(dev, -2, -1);
4342 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004343 rdev = md_import_device(dev, -1, -1);
4344
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004345 if (IS_ERR(rdev)) {
4346 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004347 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004348 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004349 err = bind_rdev_to_array(rdev, mddev);
4350 out:
4351 if (err)
4352 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004353 mddev_unlock(mddev);
Alexey Obitotskiy5492c462017-07-28 15:49:25 +02004354 if (!err)
4355 md_new_event(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004356 return err ? err : len;
4357}
4358
4359static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004360__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004361
4362static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004363bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004364{
4365 char *end;
4366 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004367 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004368
NeilBrown67918752014-12-15 12:57:01 +11004369 err = mddev_lock(mddev);
4370 if (err)
4371 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004372 if (!mddev->bitmap)
4373 goto out;
4374 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4375 while (*buf) {
4376 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4377 if (buf == end) break;
4378 if (*end == '-') { /* range */
4379 buf = end + 1;
4380 end_chunk = simple_strtoul(buf, &end, 0);
4381 if (buf == end) break;
4382 }
4383 if (*end && !isspace(*end)) break;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004384 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004385 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004386 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004387 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
Paul Clements9b1d1da2006-10-03 01:15:49 -07004388out:
NeilBrown67918752014-12-15 12:57:01 +11004389 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004390 return len;
4391}
4392
4393static struct md_sysfs_entry md_bitmap =
4394__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4395
4396static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004397size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004398{
Andre Noll58c0fed2009-03-31 14:33:13 +11004399 return sprintf(page, "%llu\n",
4400 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004401}
4402
NeilBrownfd01b882011-10-11 16:47:53 +11004403static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004404
4405static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004406size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004407{
4408 /* If array is inactive, we can reduce the component size, but
4409 * not increase it (except from 0).
4410 * If array is active, we can try an on-line resize
4411 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004412 sector_t sectors;
4413 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004414
Andre Noll58c0fed2009-03-31 14:33:13 +11004415 if (err < 0)
4416 return err;
NeilBrown67918752014-12-15 12:57:01 +11004417 err = mddev_lock(mddev);
4418 if (err)
4419 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004420 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004421 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004422 if (err == 0)
4423 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004424 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004425 if (mddev->dev_sectors == 0 ||
4426 mddev->dev_sectors > sectors)
4427 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004428 else
4429 err = -ENOSPC;
4430 }
NeilBrown67918752014-12-15 12:57:01 +11004431 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004432 return err ? err : len;
4433}
4434
4435static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004436__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004437
Masanari Iida83f0d772012-10-30 00:18:08 +09004438/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004439 * This is one of
4440 * 'none' for arrays with no metadata (good luck...)
4441 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004442 * or N.M for internally known formats
4443 */
4444static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004445metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004446{
4447 if (mddev->persistent)
4448 return sprintf(page, "%d.%d\n",
4449 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004450 else if (mddev->external)
4451 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004452 else
4453 return sprintf(page, "none\n");
4454}
4455
4456static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004457metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004458{
4459 int major, minor;
4460 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004461 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004462 /* Changing the details of 'external' metadata is
4463 * always permitted. Otherwise there must be
4464 * no devices attached to the array.
4465 */
NeilBrown67918752014-12-15 12:57:01 +11004466
4467 err = mddev_lock(mddev);
4468 if (err)
4469 return err;
4470 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004471 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4472 ;
4473 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004474 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004475
NeilBrown67918752014-12-15 12:57:01 +11004476 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004477 if (cmd_match(buf, "none")) {
4478 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004479 mddev->external = 0;
4480 mddev->major_version = 0;
4481 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004482 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004483 }
4484 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004485 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004486 if (namelen >= sizeof(mddev->metadata_type))
4487 namelen = sizeof(mddev->metadata_type)-1;
4488 strncpy(mddev->metadata_type, buf+9, namelen);
4489 mddev->metadata_type[namelen] = 0;
4490 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4491 mddev->metadata_type[--namelen] = 0;
4492 mddev->persistent = 0;
4493 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004494 mddev->major_version = 0;
4495 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004496 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004497 }
4498 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004499 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004500 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004501 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004502 buf = e+1;
4503 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004504 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004505 goto out_unlock;
4506 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004507 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004508 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004509 mddev->major_version = major;
4510 mddev->minor_version = minor;
4511 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004512 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004513 err = 0;
4514out_unlock:
4515 mddev_unlock(mddev);
4516 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004517}
4518
4519static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004520__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004521
NeilBrowna35b0d62006-01-06 00:20:49 -08004522static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004523action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004524{
NeilBrown7eec3142005-11-08 21:39:44 -08004525 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004526 unsigned long recovery = mddev->recovery;
4527 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004528 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004529 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4530 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4531 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004532 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004533 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4534 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004535 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004536 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004537 type = "check";
4538 else
4539 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004540 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004541 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004542 else if (mddev->reshape_position != MaxSector)
4543 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004544 }
4545 return sprintf(page, "%s\n", type);
4546}
4547
4548static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004549action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004550{
NeilBrown7eec3142005-11-08 21:39:44 -08004551 if (!mddev->pers || !mddev->pers->sync_request)
4552 return -EINVAL;
4553
NeilBrownb6a9ce62009-05-26 09:41:17 +10004554
4555 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004556 if (cmd_match(page, "frozen"))
4557 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4558 else
4559 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004560 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4561 mddev_lock(mddev) == 0) {
4562 flush_workqueue(md_misc_wq);
4563 if (mddev->sync_thread) {
4564 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004565 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004566 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004567 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004568 }
NeilBrown312045e2015-12-21 11:01:21 +11004569 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004570 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004571 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004572 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004573 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004574 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004575 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004576 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004577 int err;
4578 if (mddev->pers->start_reshape == NULL)
4579 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004580 err = mddev_lock(mddev);
4581 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004582 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4583 err = -EBUSY;
4584 else {
4585 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4586 err = mddev->pers->start_reshape(mddev);
4587 }
NeilBrown67918752014-12-15 12:57:01 +11004588 mddev_unlock(mddev);
4589 }
NeilBrown16484bf2006-03-27 01:18:13 -08004590 if (err)
4591 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004592 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004593 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004594 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004595 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004596 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004597 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004598 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004599 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4600 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004601 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004602 if (mddev->ro == 2) {
4603 /* A write to sync_action is enough to justify
4604 * canceling read-auto mode
4605 */
4606 mddev->ro = 0;
4607 md_wakeup_thread(mddev->sync_thread);
4608 }
NeilBrown03c902e2006-01-06 00:20:46 -08004609 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004610 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004611 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004612 return len;
4613}
4614
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004615static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004616__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004617
4618static ssize_t
4619last_sync_action_show(struct mddev *mddev, char *page)
4620{
4621 return sprintf(page, "%s\n", mddev->last_sync_action);
4622}
4623
4624static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4625
NeilBrown9d888832005-11-08 21:39:26 -08004626static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004627mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004628{
4629 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004630 (unsigned long long)
4631 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004632}
4633
NeilBrown80ca3a42006-07-10 04:44:18 -07004634static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004635
NeilBrown88202a02006-01-06 00:21:36 -08004636static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004637sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004638{
4639 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4640 mddev->sync_speed_min ? "local": "system");
4641}
4642
4643static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004644sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004645{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004646 unsigned int min;
4647 int rv;
4648
NeilBrown88202a02006-01-06 00:21:36 -08004649 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004650 min = 0;
4651 } else {
4652 rv = kstrtouint(buf, 10, &min);
4653 if (rv < 0)
4654 return rv;
4655 if (min == 0)
4656 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004657 }
NeilBrown88202a02006-01-06 00:21:36 -08004658 mddev->sync_speed_min = min;
4659 return len;
4660}
4661
4662static struct md_sysfs_entry md_sync_min =
4663__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4664
4665static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004666sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004667{
4668 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4669 mddev->sync_speed_max ? "local": "system");
4670}
4671
4672static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004673sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004674{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004675 unsigned int max;
4676 int rv;
4677
NeilBrown88202a02006-01-06 00:21:36 -08004678 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004679 max = 0;
4680 } else {
4681 rv = kstrtouint(buf, 10, &max);
4682 if (rv < 0)
4683 return rv;
4684 if (max == 0)
4685 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004686 }
NeilBrown88202a02006-01-06 00:21:36 -08004687 mddev->sync_speed_max = max;
4688 return len;
4689}
4690
4691static struct md_sysfs_entry md_sync_max =
4692__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4693
Iustin Popd7f3d292007-10-16 23:30:54 -07004694static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004695degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004696{
4697 return sprintf(page, "%d\n", mddev->degraded);
4698}
4699static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004700
4701static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004702sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004703{
4704 return sprintf(page, "%d\n", mddev->parallel_resync);
4705}
4706
4707static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004708sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004709{
4710 long n;
4711
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004712 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004713 return -EINVAL;
4714
4715 if (n != 0 && n != 1)
4716 return -EINVAL;
4717
4718 mddev->parallel_resync = n;
4719
4720 if (mddev->sync_thread)
4721 wake_up(&resync_wait);
4722
4723 return len;
4724}
4725
4726/* force parallel resync, even with shared block devices */
4727static struct md_sysfs_entry md_sync_force_parallel =
4728__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4729 sync_force_parallel_show, sync_force_parallel_store);
4730
4731static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004732sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004733{
4734 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004735 if (mddev->curr_resync == 0)
4736 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004737 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4738 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004739 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004740 db = resync - mddev->resync_mark_cnt;
4741 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004742}
4743
NeilBrown80ca3a42006-07-10 04:44:18 -07004744static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004745
4746static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004747sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004748{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004749 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004750
NeilBrownacb180b2009-04-14 16:28:34 +10004751 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4752 return sprintf(page, "none\n");
4753
NeilBrown72f36d52012-10-11 14:25:57 +11004754 if (mddev->curr_resync == 1 ||
4755 mddev->curr_resync == 2)
4756 return sprintf(page, "delayed\n");
4757
NeilBrownc804cde2012-05-21 09:28:33 +10004758 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4759 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004760 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004761 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004762 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004763
NeilBrownacb180b2009-04-14 16:28:34 +10004764 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004765 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004766}
4767
NeilBrown750f1992014-09-30 08:53:05 +10004768static struct md_sysfs_entry md_sync_completed =
4769 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004770
NeilBrowne464eaf2006-03-27 01:18:14 -08004771static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004772min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004773{
4774 return sprintf(page, "%llu\n",
4775 (unsigned long long)mddev->resync_min);
4776}
4777static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004778min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004779{
4780 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004781 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004782
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004783 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004784 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004785
4786 spin_lock(&mddev->lock);
4787 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004788 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004789 goto out_unlock;
4790
4791 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004792 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004793 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004794
NeilBrown50c37b12015-03-23 17:36:38 +11004795 /* Round down to multiple of 4K for safety */
4796 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004797 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004798
NeilBrown23da4222014-12-15 12:57:01 +11004799out_unlock:
4800 spin_unlock(&mddev->lock);
4801 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004802}
4803
4804static struct md_sysfs_entry md_min_sync =
4805__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4806
4807static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004808max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004809{
4810 if (mddev->resync_max == MaxSector)
4811 return sprintf(page, "max\n");
4812 else
4813 return sprintf(page, "%llu\n",
4814 (unsigned long long)mddev->resync_max);
4815}
4816static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004817max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004818{
NeilBrown23da4222014-12-15 12:57:01 +11004819 int err;
4820 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004821 if (strncmp(buf, "max", 3) == 0)
4822 mddev->resync_max = MaxSector;
4823 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004824 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004825 int chunk;
4826
4827 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004828 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004829 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004830 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004831 goto out_unlock;
4832
4833 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004834 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004835 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004836 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004837 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004838
4839 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004840 chunk = mddev->chunk_sectors;
4841 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004842 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004843
4844 err = -EINVAL;
4845 if (sector_div(temp, chunk))
4846 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004847 }
4848 mddev->resync_max = max;
4849 }
4850 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004851 err = 0;
4852out_unlock:
4853 spin_unlock(&mddev->lock);
4854 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004855}
4856
4857static struct md_sysfs_entry md_max_sync =
4858__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4859
4860static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004861suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004862{
4863 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4864}
4865
4866static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004867suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004868{
NeilBrownb03e0cc2017-10-19 12:49:15 +11004869 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004870 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004871
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004872 err = kstrtoull(buf, 10, &new);
4873 if (err < 0)
4874 return err;
4875 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004876 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004877
NeilBrown67918752014-12-15 12:57:01 +11004878 err = mddev_lock(mddev);
4879 if (err)
4880 return err;
4881 err = -EINVAL;
4882 if (mddev->pers == NULL ||
4883 mddev->pers->quiesce == NULL)
4884 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004885 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11004886 mddev->suspend_lo = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004887 mddev_resume(mddev);
4888
NeilBrown67918752014-12-15 12:57:01 +11004889 err = 0;
4890unlock:
4891 mddev_unlock(mddev);
4892 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004893}
4894static struct md_sysfs_entry md_suspend_lo =
4895__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4896
NeilBrowne464eaf2006-03-27 01:18:14 -08004897static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004898suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004899{
4900 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4901}
4902
4903static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004904suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004905{
NeilBrownb03e0cc2017-10-19 12:49:15 +11004906 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004907 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004908
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004909 err = kstrtoull(buf, 10, &new);
4910 if (err < 0)
4911 return err;
4912 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004913 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004914
NeilBrown67918752014-12-15 12:57:01 +11004915 err = mddev_lock(mddev);
4916 if (err)
4917 return err;
4918 err = -EINVAL;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004919 if (mddev->pers == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004920 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004921
4922 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11004923 mddev->suspend_hi = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004924 mddev_resume(mddev);
4925
NeilBrown67918752014-12-15 12:57:01 +11004926 err = 0;
4927unlock:
4928 mddev_unlock(mddev);
4929 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004930}
4931static struct md_sysfs_entry md_suspend_hi =
4932__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4933
NeilBrown08a02ec2007-05-09 02:35:38 -07004934static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004935reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004936{
4937 if (mddev->reshape_position != MaxSector)
4938 return sprintf(page, "%llu\n",
4939 (unsigned long long)mddev->reshape_position);
4940 strcpy(page, "none\n");
4941 return 5;
4942}
4943
4944static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004945reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004946{
NeilBrownc6563a82012-05-21 09:27:00 +10004947 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004948 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004949 int err;
NeilBrown67918752014-12-15 12:57:01 +11004950
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004951 err = kstrtoull(buf, 10, &new);
4952 if (err < 0)
4953 return err;
4954 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004955 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004956 err = mddev_lock(mddev);
4957 if (err)
4958 return err;
4959 err = -EBUSY;
4960 if (mddev->pers)
4961 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004962 mddev->reshape_position = new;
4963 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004964 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004965 mddev->new_level = mddev->level;
4966 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004967 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004968 rdev_for_each(rdev, mddev)
4969 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004970 err = 0;
4971unlock:
4972 mddev_unlock(mddev);
4973 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004974}
4975
4976static struct md_sysfs_entry md_reshape_position =
4977__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4978 reshape_position_store);
4979
Dan Williamsb522adc2009-03-31 15:00:31 +11004980static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004981reshape_direction_show(struct mddev *mddev, char *page)
4982{
4983 return sprintf(page, "%s\n",
4984 mddev->reshape_backwards ? "backwards" : "forwards");
4985}
4986
4987static ssize_t
4988reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4989{
4990 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004991 int err;
4992
NeilBrown2c810cd2012-05-21 09:27:00 +10004993 if (cmd_match(buf, "forwards"))
4994 backwards = 0;
4995 else if (cmd_match(buf, "backwards"))
4996 backwards = 1;
4997 else
4998 return -EINVAL;
4999 if (mddev->reshape_backwards == backwards)
5000 return len;
5001
NeilBrown67918752014-12-15 12:57:01 +11005002 err = mddev_lock(mddev);
5003 if (err)
5004 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10005005 /* check if we are allowed to change */
5006 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11005007 err = -EBUSY;
5008 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10005009 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11005010 err = -EINVAL;
5011 else
5012 mddev->reshape_backwards = backwards;
5013 mddev_unlock(mddev);
5014 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10005015}
5016
5017static struct md_sysfs_entry md_reshape_direction =
5018__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5019 reshape_direction_store);
5020
5021static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005022array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11005023{
5024 if (mddev->external_size)
5025 return sprintf(page, "%llu\n",
5026 (unsigned long long)mddev->array_sectors/2);
5027 else
5028 return sprintf(page, "default\n");
5029}
5030
5031static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005032array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11005033{
5034 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11005035 int err;
5036
5037 err = mddev_lock(mddev);
5038 if (err)
5039 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11005040
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005041 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08005042 if (mddev_is_clustered(mddev)) {
5043 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005044 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08005045 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005046
Dan Williamsb522adc2009-03-31 15:00:31 +11005047 if (strncmp(buf, "default", 7) == 0) {
5048 if (mddev->pers)
5049 sectors = mddev->pers->size(mddev, 0, 0);
5050 else
5051 sectors = mddev->array_sectors;
5052
5053 mddev->external_size = 0;
5054 } else {
5055 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11005056 err = -EINVAL;
5057 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5058 err = -E2BIG;
5059 else
5060 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11005061 }
5062
NeilBrown67918752014-12-15 12:57:01 +11005063 if (!err) {
5064 mddev->array_sectors = sectors;
5065 if (mddev->pers) {
5066 set_capacity(mddev->gendisk, mddev->array_sectors);
5067 revalidate_disk(mddev->gendisk);
5068 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11005069 }
NeilBrown67918752014-12-15 12:57:01 +11005070 mddev_unlock(mddev);
5071 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11005072}
5073
5074static struct md_sysfs_entry md_array_size =
5075__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5076 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005077
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005078static ssize_t
5079consistency_policy_show(struct mddev *mddev, char *page)
5080{
5081 int ret;
5082
5083 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5084 ret = sprintf(page, "journal\n");
5085 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5086 ret = sprintf(page, "ppl\n");
5087 } else if (mddev->bitmap) {
5088 ret = sprintf(page, "bitmap\n");
5089 } else if (mddev->pers) {
5090 if (mddev->pers->sync_request)
5091 ret = sprintf(page, "resync\n");
5092 else
5093 ret = sprintf(page, "none\n");
5094 } else {
5095 ret = sprintf(page, "unknown\n");
5096 }
5097
5098 return ret;
5099}
5100
5101static ssize_t
5102consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5103{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005104 int err = 0;
5105
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005106 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005107 if (mddev->pers->change_consistency_policy)
5108 err = mddev->pers->change_consistency_policy(mddev, buf);
5109 else
5110 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005111 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5112 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005113 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005114 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005115 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005116
5117 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005118}
5119
5120static struct md_sysfs_entry md_consistency_policy =
5121__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5122 consistency_policy_store);
5123
NeilBrowneae17012005-11-08 21:39:23 -08005124static struct attribute *md_default_attrs[] = {
5125 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005126 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005127 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005128 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005129 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005130 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005131 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005132 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005133 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005134 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005135 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005136 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005137 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005138 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005139 &md_consistency_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005140 NULL,
5141};
5142
5143static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005144 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005145 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005146 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005147 &md_sync_min.attr,
5148 &md_sync_max.attr,
5149 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005150 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005151 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005152 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005153 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005154 &md_suspend_lo.attr,
5155 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005156 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005157 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005158 NULL,
5159};
NeilBrown411036f2005-11-08 21:39:40 -08005160static struct attribute_group md_redundancy_group = {
5161 .name = NULL,
5162 .attrs = md_redundancy_attrs,
5163};
5164
NeilBrowneae17012005-11-08 21:39:23 -08005165static ssize_t
5166md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5167{
5168 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005169 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005170 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005171
5172 if (!entry->show)
5173 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005174 spin_lock(&all_mddevs_lock);
5175 if (list_empty(&mddev->all_mddevs)) {
5176 spin_unlock(&all_mddevs_lock);
5177 return -EBUSY;
5178 }
5179 mddev_get(mddev);
5180 spin_unlock(&all_mddevs_lock);
5181
NeilBrownb7b17c92014-12-15 12:56:59 +11005182 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005183 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005184 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005185}
5186
5187static ssize_t
5188md_attr_store(struct kobject *kobj, struct attribute *attr,
5189 const char *page, size_t length)
5190{
5191 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005192 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005193 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005194
5195 if (!entry->store)
5196 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005197 if (!capable(CAP_SYS_ADMIN))
5198 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005199 spin_lock(&all_mddevs_lock);
5200 if (list_empty(&mddev->all_mddevs)) {
5201 spin_unlock(&all_mddevs_lock);
5202 return -EBUSY;
5203 }
5204 mddev_get(mddev);
5205 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005206 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005207 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005208 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005209}
5210
5211static void md_free(struct kobject *ko)
5212{
NeilBrownfd01b882011-10-11 16:47:53 +11005213 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005214
5215 if (mddev->sysfs_state)
5216 sysfs_put(mddev->sysfs_state);
5217
Bart Van Assched8115c352018-02-28 10:15:29 -08005218 if (mddev->gendisk)
5219 del_gendisk(mddev->gendisk);
NeilBrown6cd18e72015-04-27 14:12:22 +10005220 if (mddev->queue)
5221 blk_cleanup_queue(mddev->queue);
Bart Van Assched8115c352018-02-28 10:15:29 -08005222 if (mddev->gendisk)
NeilBrowna21d1502009-01-09 08:31:09 +11005223 put_disk(mddev->gendisk);
NeilBrown4ad23a972017-03-15 14:05:14 +11005224 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005225
Kent Overstreet28dec872018-06-07 20:52:54 -04005226 bioset_exit(&mddev->bio_set);
5227 bioset_exit(&mddev->sync_set);
NeilBrowneae17012005-11-08 21:39:23 -08005228 kfree(mddev);
5229}
5230
Emese Revfy52cf25d2010-01-19 02:58:23 +01005231static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005232 .show = md_attr_show,
5233 .store = md_attr_store,
5234};
5235static struct kobj_type md_ktype = {
5236 .release = md_free,
5237 .sysfs_ops = &md_sysfs_ops,
5238 .default_attrs = md_default_attrs,
5239};
5240
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241int mdp_major = 0;
5242
Dan Williams5fd3a172009-03-04 00:57:25 -07005243static void mddev_delayed_delete(struct work_struct *ws)
5244{
NeilBrownfd01b882011-10-11 16:47:53 +11005245 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005246
NeilBrown43a70502009-12-14 12:49:55 +11005247 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005248 kobject_del(&mddev->kobj);
5249 kobject_put(&mddev->kobj);
5250}
5251
NeilBrown4ad23a972017-03-15 14:05:14 +11005252static void no_op(struct percpu_ref *r) {}
5253
NeilBrowna415c0f2017-06-05 16:05:13 +10005254int mddev_init_writes_pending(struct mddev *mddev)
5255{
5256 if (mddev->writes_pending.percpu_count_ptr)
5257 return 0;
5258 if (percpu_ref_init(&mddev->writes_pending, no_op, 0, GFP_KERNEL) < 0)
5259 return -ENOMEM;
5260 /* We want to start with the refcount at zero */
5261 percpu_ref_put(&mddev->writes_pending);
5262 return 0;
5263}
5264EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5265
NeilBrownefeb53c2009-01-09 08:31:10 +11005266static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267{
NeilBrown039b7222017-04-12 16:26:13 +10005268 /*
5269 * If dev is zero, name is the name of a device to allocate with
5270 * an arbitrary minor number. It will be "md_???"
5271 * If dev is non-zero it must be a device number with a MAJOR of
5272 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5273 * the device is being created by opening a node in /dev.
5274 * If "name" is not NULL, the device is being created by
5275 * writing to /sys/module/md_mod/parameters/new_array.
5276 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005277 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005278 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005280 int partitioned;
5281 int shift;
5282 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005283 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284
5285 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005286 return -ENODEV;
5287
5288 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5289 shift = partitioned ? MdpMinorShift : 0;
5290 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291
Tejun Heoe804ac72010-10-15 15:36:08 +02005292 /* wait for any previous instance of this device to be
5293 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005294 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005295 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005296
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005297 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005298 error = -EEXIST;
5299 if (mddev->gendisk)
5300 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005301
NeilBrown039b7222017-04-12 16:26:13 +10005302 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005303 /* Need to ensure that 'name' is not a duplicate.
5304 */
NeilBrownfd01b882011-10-11 16:47:53 +11005305 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005306 spin_lock(&all_mddevs_lock);
5307
5308 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5309 if (mddev2->gendisk &&
5310 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5311 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005312 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005313 }
5314 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 }
NeilBrown039b7222017-04-12 16:26:13 +10005316 if (name && dev)
5317 /*
5318 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5319 */
5320 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005321
NeilBrown0909dc42009-07-01 12:27:21 +10005322 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005323 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005324 if (!mddev->queue)
5325 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005326 mddev->queue->queuedata = mddev;
5327
NeilBrown409c57f2009-03-31 14:39:39 +11005328 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005329 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005330
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 disk = alloc_disk(1 << shift);
5332 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005333 blk_cleanup_queue(mddev->queue);
5334 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005335 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005337 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005339 if (name)
5340 strcpy(disk->disk_name, name);
5341 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005343 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 disk->fops = &md_fops;
5346 disk->private_data = mddev;
5347 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005348 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005349 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005350 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005351 * remove it now.
5352 */
5353 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005355 /* As soon as we call add_disk(), another thread could get
5356 * through to md_open, so make sure it doesn't get too far
5357 */
5358 mutex_lock(&mddev->open_mutex);
5359 add_disk(disk);
5360
Kent Overstreet28dec872018-06-07 20:52:54 -04005361 error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005362 if (error) {
5363 /* This isn't possible, but as kobject_init_and_add is marked
5364 * __must_check, we must do something with the result
5365 */
NeilBrown9d487392016-11-02 14:16:49 +11005366 pr_debug("md: cannot register %s/md - name in use\n",
5367 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005368 error = 0;
5369 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005370 if (mddev->kobj.sd &&
5371 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005372 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005373 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005374 abort:
5375 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005376 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005377 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005378 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005379 }
NeilBrownd3374822009-01-09 08:31:10 +11005380 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005381 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005382}
5383
5384static struct kobject *md_probe(dev_t dev, int *part, void *data)
5385{
NeilBrown78b63502017-04-12 16:26:13 +10005386 if (create_on_open)
5387 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 return NULL;
5389}
5390
Kees Cooke4dca7b2017-10-17 19:04:42 -07005391static int add_named_array(const char *val, const struct kernel_param *kp)
NeilBrownefeb53c2009-01-09 08:31:10 +11005392{
NeilBrown039b7222017-04-12 16:26:13 +10005393 /*
5394 * val must be "md_*" or "mdNNN".
5395 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005396 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005397 * For "mdNNN" we allocate an array with the minor number NNN
5398 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005399 */
5400 int len = strlen(val);
5401 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005402 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005403
5404 while (len && val[len-1] == '\n')
5405 len--;
5406 if (len >= DISK_NAME_LEN)
5407 return -E2BIG;
5408 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005409 if (strncmp(buf, "md_", 3) == 0)
5410 return md_alloc(0, buf);
5411 if (strncmp(buf, "md", 2) == 0 &&
5412 isdigit(buf[2]) &&
5413 kstrtoul(buf+2, 10, &devnum) == 0 &&
5414 devnum <= MINORMASK)
5415 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5416
5417 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005418}
5419
Kees Cook8376d3c2017-10-16 17:01:48 -07005420static void md_safemode_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421{
Kees Cook8376d3c2017-10-16 17:01:48 -07005422 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423
NeilBrown4ad23a972017-03-15 14:05:14 +11005424 mddev->safemode = 1;
5425 if (mddev->external)
5426 sysfs_notify_dirent_safe(mddev->sysfs_state);
5427
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 md_wakeup_thread(mddev->thread);
5429}
5430
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005431static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
NeilBrownfd01b882011-10-11 16:47:53 +11005433int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434{
NeilBrown2604b702006-01-06 00:20:36 -08005435 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005436 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005437 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438
NeilBrowna757e642005-04-16 15:26:42 -07005439 if (list_empty(&mddev->disks))
5440 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
5443 if (mddev->pers)
5444 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005445 /* Cannot run until previous stop completes properly */
5446 if (mddev->sysfs_active)
5447 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005448
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 /*
5450 * Analyze all RAID superblock(s)
5451 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005452 if (!mddev->raid_disks) {
5453 if (!mddev->persistent)
5454 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005455 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457
NeilBrownd9d166c2006-01-06 00:20:51 -08005458 if (mddev->level != LEVEL_NONE)
5459 request_module("md-level-%d", mddev->level);
5460 else if (mddev->clevel[0])
5461 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
5463 /*
5464 * Drop all container device buffers, from now on
5465 * the only valid external interface is through the md
5466 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 */
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005468 mddev->has_superblocks = false;
NeilBrowndafb20f2012-03-19 12:46:39 +11005469 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005470 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 continue;
5472 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005473 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005474 if (mddev->ro != 1 &&
5475 (bdev_read_only(rdev->bdev) ||
5476 bdev_read_only(rdev->meta_bdev))) {
5477 mddev->ro = 1;
5478 if (mddev->gendisk)
5479 set_disk_ro(mddev->gendisk, 1);
5480 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005481
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005482 if (rdev->sb_page)
5483 mddev->has_superblocks = true;
5484
NeilBrownf0d76d72007-07-17 04:06:12 -07005485 /* perform some consistency tests on the device.
5486 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005487 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005488 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005489 if (rdev->meta_bdev) {
5490 /* Nothing to check */;
5491 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005492 if (mddev->dev_sectors &&
5493 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005494 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005495 pr_warn("md: %s: data overlaps metadata\n",
5496 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005497 return -EINVAL;
5498 }
5499 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005500 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005501 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005502 pr_warn("md: %s: metadata overlaps data\n",
5503 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005504 return -EINVAL;
5505 }
5506 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005507 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 }
5509
Kent Overstreetafeee512018-05-20 18:25:52 -04005510 if (!bioset_initialized(&mddev->bio_set)) {
5511 err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5512 if (err)
5513 return err;
Ming Lei10273172017-02-14 23:29:00 +08005514 }
Kent Overstreetafeee512018-05-20 18:25:52 -04005515 if (!bioset_initialized(&mddev->sync_set)) {
5516 err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5517 if (err)
Kent Overstreet28dec872018-06-07 20:52:54 -04005518 return err;
NeilBrown5a850712017-06-21 09:12:21 +10005519 }
Xiao Ni5a409b42018-05-21 11:49:54 +08005520 if (mddev->flush_pool == NULL) {
5521 mddev->flush_pool = mempool_create(NR_FLUSH_INFOS, flush_info_alloc,
5522 flush_info_free, mddev);
5523 if (!mddev->flush_pool) {
5524 err = -ENOMEM;
5525 goto abort;
5526 }
5527 }
5528 if (mddev->flush_bio_pool == NULL) {
5529 mddev->flush_bio_pool = mempool_create(NR_FLUSH_BIOS, flush_bio_alloc,
5530 flush_bio_free, mddev);
5531 if (!mddev->flush_bio_pool) {
5532 err = -ENOMEM;
5533 goto abort;
5534 }
5535 }
NeilBrowna167f662010-10-26 18:31:13 +11005536
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005538 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005539 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005541 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005542 pr_warn("md: personality for level %d is not loaded!\n",
5543 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005544 else
NeilBrown9d487392016-11-02 14:16:49 +11005545 pr_warn("md: personality for level %s is not loaded!\n",
5546 mddev->clevel);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005547 err = -EINVAL;
5548 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005551 if (mddev->level != pers->level) {
5552 mddev->level = pers->level;
5553 mddev->new_level = pers->level;
5554 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005555 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
NeilBrownf6705572006-03-27 01:18:11 -08005557 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005558 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005559 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005560 module_put(pers->owner);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005561 err = -EINVAL;
5562 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08005563 }
5564
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005565 if (pers->sync_request) {
5566 /* Warn if this is a potentially silly
5567 * configuration.
5568 */
5569 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005570 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005571 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005572
NeilBrowndafb20f2012-03-19 12:46:39 +11005573 rdev_for_each(rdev, mddev)
5574 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005575 if (rdev < rdev2 &&
5576 rdev->bdev->bd_contains ==
5577 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005578 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5579 mdname(mddev),
5580 bdevname(rdev->bdev,b),
5581 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005582 warned = 1;
5583 }
5584 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005585
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005586 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005587 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005588 }
5589
NeilBrown657390d2005-08-26 18:34:16 -07005590 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005591 /* may be over-ridden by personality */
5592 mddev->resync_max_sectors = mddev->dev_sectors;
5593
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005594 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
NeilBrown0f9552b52009-12-30 12:08:50 +11005596 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005597 mddev->ro = 2; /* read-only, but switch on first write */
5598
NeilBrown36d091f2014-12-15 12:56:58 +11005599 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005600 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005601 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005602 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005603 WARN_ONCE(!mddev->external_size,
5604 "%s: default size too small, but 'external_size' not in effect?\n",
5605 __func__);
5606 pr_warn("md: invalid array_size %llu > default size %llu\n",
5607 (unsigned long long)mddev->array_sectors / 2,
5608 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005609 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005610 }
NeilBrown36d091f2014-12-15 12:56:58 +11005611 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005612 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005613 struct bitmap *bitmap;
5614
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005615 bitmap = md_bitmap_create(mddev, -1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005616 if (IS_ERR(bitmap)) {
5617 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005618 pr_warn("%s: failed to create bitmap (%d)\n",
5619 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005620 } else
5621 mddev->bitmap = bitmap;
5622
NeilBrownb15c2e52006-01-06 00:20:16 -08005623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005625 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005626 if (mddev->private)
5627 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005628 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005629 module_put(pers->owner);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005630 md_bitmap_destroy(mddev);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005631 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 }
NeilBrown5c675f82014-12-15 12:56:56 +11005633 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005634 bool nonrot = true;
5635
5636 rdev_for_each(rdev, mddev) {
5637 if (rdev->raid_disk >= 0 &&
5638 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5639 nonrot = false;
5640 break;
5641 }
5642 }
5643 if (mddev->degraded)
5644 nonrot = false;
5645 if (nonrot)
Bart Van Assche8b904b52018-03-07 17:10:10 -08005646 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
Shaohua Libb086a82016-09-30 09:45:40 -07005647 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08005648 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005649 mddev->queue->backing_dev_info->congested_data = mddev;
5650 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005651 }
NeilBrown36d091f2014-12-15 12:56:58 +11005652 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005653 if (mddev->kobj.sd &&
5654 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005655 pr_warn("md: cannot register extra attributes for %s\n",
5656 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005657 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005658 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005659 mddev->ro = 0;
5660
Robert Becker1e509152009-12-14 12:49:58 +11005661 atomic_set(&mddev->max_corr_read_errors,
5662 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005664 if (mddev_is_clustered(mddev))
5665 mddev->safemode_delay = 0;
5666 else
5667 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005669 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005670 spin_lock(&mddev->lock);
5671 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005672 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005673 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005674 if (rdev->raid_disk >= 0)
5675 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005676 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005677
NeilBrowna4a3d262015-07-17 11:57:30 +10005678 if (mddev->degraded && !mddev->ro)
5679 /* This ensures that recovering status is reported immediately
5680 * via sysfs - until a lack of spares is confirmed.
5681 */
5682 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005684
Shaohua Li29530792016-12-08 15:48:19 -08005685 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07005686 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687
NeilBrownd7603b72006-01-06 00:20:30 -08005688 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005689 sysfs_notify_dirent_safe(mddev->sysfs_state);
5690 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005691 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 return 0;
Xiao Nib1261942018-01-24 12:17:38 +08005693
5694abort:
Xiao Ni5a409b42018-05-21 11:49:54 +08005695 if (mddev->flush_bio_pool) {
5696 mempool_destroy(mddev->flush_bio_pool);
5697 mddev->flush_bio_pool = NULL;
5698 }
5699 if (mddev->flush_pool){
5700 mempool_destroy(mddev->flush_pool);
5701 mddev->flush_pool = NULL;
5702 }
Xiao Nib1261942018-01-24 12:17:38 +08005703
5704 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705}
NeilBrown390ee602010-06-01 19:37:27 +10005706EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707
NeilBrownfd01b882011-10-11 16:47:53 +11005708static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005709{
5710 int err;
5711
5712 err = md_run(mddev);
5713 if (err)
5714 goto out;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005715 err = md_bitmap_load(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005716 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005717 md_bitmap_destroy(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005718 goto out;
5719 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005720
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005721 if (mddev_is_clustered(mddev))
5722 md_allow_write(mddev);
5723
Song Liud5d885f2017-11-19 22:17:01 -08005724 /* run start up tasks that require md_thread */
5725 md_start(mddev);
5726
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005727 md_wakeup_thread(mddev->thread);
5728 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5729
NeilBrownfe60b012010-03-29 11:10:42 +11005730 set_capacity(mddev->gendisk, mddev->array_sectors);
5731 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005732 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005733 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5734out:
5735 return err;
5736}
5737
Song Liud5d885f2017-11-19 22:17:01 -08005738int md_start(struct mddev *mddev)
5739{
5740 int ret = 0;
5741
5742 if (mddev->pers->start) {
5743 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
5744 md_wakeup_thread(mddev->thread);
5745 ret = mddev->pers->start(mddev);
5746 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
5747 md_wakeup_thread(mddev->sync_thread);
5748 }
5749 return ret;
5750}
5751EXPORT_SYMBOL_GPL(md_start);
5752
NeilBrownfd01b882011-10-11 16:47:53 +11005753static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754{
5755 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10005756 struct md_rdev *rdev;
5757 bool has_journal = false;
5758 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759
Andre Noll80fab1d2008-07-11 22:02:21 +10005760 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005762 return -ENXIO;
5763 if (!mddev->pers)
5764 return -EINVAL;
5765 if (!mddev->ro)
5766 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005767
NeilBrown97b20ef2017-04-13 08:53:48 +10005768 rcu_read_lock();
5769 rdev_for_each_rcu(rdev, mddev) {
5770 if (test_bit(Journal, &rdev->flags) &&
5771 !test_bit(Faulty, &rdev->flags))
5772 has_journal = true;
5773 if (bdev_read_only(rdev->bdev))
5774 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07005775 }
NeilBrown97b20ef2017-04-13 08:53:48 +10005776 rcu_read_unlock();
5777 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
5778 /* Don't restart rw with journal missing/faulty */
5779 return -EINVAL;
5780 if (has_readonly)
5781 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07005782
Andre Noll80fab1d2008-07-11 22:02:21 +10005783 mddev->safemode = 0;
5784 mddev->ro = 0;
5785 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11005786 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10005787 /* Kick recovery or resync if necessary */
5788 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5789 md_wakeup_thread(mddev->thread);
5790 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005791 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793}
5794
NeilBrownfd01b882011-10-11 16:47:53 +11005795static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005796{
5797 mddev->array_sectors = 0;
5798 mddev->external_size = 0;
5799 mddev->dev_sectors = 0;
5800 mddev->raid_disks = 0;
5801 mddev->recovery_cp = 0;
5802 mddev->resync_min = 0;
5803 mddev->resync_max = MaxSector;
5804 mddev->reshape_position = MaxSector;
5805 mddev->external = 0;
5806 mddev->persistent = 0;
5807 mddev->level = LEVEL_NONE;
5808 mddev->clevel[0] = 0;
5809 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08005810 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005811 mddev->ro = 0;
5812 mddev->metadata_type[0] = 0;
5813 mddev->chunk_sectors = 0;
5814 mddev->ctime = mddev->utime = 0;
5815 mddev->layout = 0;
5816 mddev->max_disks = 0;
5817 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005818 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005819 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005820 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005821 mddev->new_level = LEVEL_NONE;
5822 mddev->new_layout = 0;
5823 mddev->new_chunk_sectors = 0;
5824 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005825 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005826 mddev->suspend_lo = mddev->suspend_hi = 0;
5827 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5828 mddev->recovery = 0;
5829 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005830 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005831 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005832 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005833 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005834 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005835 mddev->bitmap_info.offset = 0;
5836 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005837 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005838 mddev->bitmap_info.chunksize = 0;
5839 mddev->bitmap_info.daemon_sleep = 0;
5840 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005841 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005842}
5843
NeilBrownfd01b882011-10-11 16:47:53 +11005844static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005845{
NeilBrown6b6204e2013-05-09 09:48:30 +10005846 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005847 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005848 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005849 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005850 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005851 }
5852
5853 del_timer_sync(&mddev->safemode_timer);
5854
Shaohua Li034e33f2016-11-21 10:29:19 -08005855 if (mddev->pers && mddev->pers->quiesce) {
5856 mddev->pers->quiesce(mddev, 1);
5857 mddev->pers->quiesce(mddev, 0);
5858 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005859 md_bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005860
NeilBrownb6d428c2013-04-24 11:42:42 +10005861 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005862 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08005863 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11005864 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005865 if (!mddev_is_clustered(mddev))
5866 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005867 md_update_sb(mddev, 1);
5868 }
5869}
NeilBrowndefad612011-01-14 09:14:33 +11005870
NeilBrownfd01b882011-10-11 16:47:53 +11005871void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005872{
NeilBrown29f097c2013-11-14 17:54:51 +11005873 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005874 __md_stop_writes(mddev);
5875 mddev_unlock(mddev);
5876}
NeilBrown390ee602010-06-01 19:37:27 +10005877EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005878
NeilBrown5aa61f42014-12-15 12:56:57 +11005879static void mddev_detach(struct mddev *mddev)
5880{
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005881 md_bitmap_wait_behind_writes(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005882 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005883 mddev->pers->quiesce(mddev, 1);
5884 mddev->pers->quiesce(mddev, 0);
5885 }
5886 md_unregister_thread(&mddev->thread);
5887 if (mddev->queue)
5888 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5889}
5890
NeilBrown5eff3c42012-11-19 10:47:48 +11005891static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005892{
NeilBrown36d091f2014-12-15 12:56:58 +11005893 struct md_personality *pers = mddev->pers;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005894 md_bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11005895 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005896 /* Ensure ->event_work is done */
5897 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005898 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005899 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005900 spin_unlock(&mddev->lock);
5901 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005902 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005903 if (pers->sync_request && mddev->to_remove == NULL)
5904 mddev->to_remove = &md_redundancy_group;
5905 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005906 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005907}
NeilBrown5eff3c42012-11-19 10:47:48 +11005908
5909void md_stop(struct mddev *mddev)
5910{
5911 /* stop the array and free an attached data structures.
5912 * This is called from dm-raid
5913 */
5914 __md_stop(mddev);
Xiao Ni5a409b42018-05-21 11:49:54 +08005915 if (mddev->flush_bio_pool) {
5916 mempool_destroy(mddev->flush_bio_pool);
5917 mddev->flush_bio_pool = NULL;
5918 }
5919 if (mddev->flush_pool) {
5920 mempool_destroy(mddev->flush_pool);
5921 mddev->flush_pool = NULL;
5922 }
Kent Overstreetafeee512018-05-20 18:25:52 -04005923 bioset_exit(&mddev->bio_set);
5924 bioset_exit(&mddev->sync_set);
NeilBrown5eff3c42012-11-19 10:47:48 +11005925}
5926
NeilBrown390ee602010-06-01 19:37:27 +10005927EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005928
NeilBrowna05b7ea2012-07-19 15:59:18 +10005929static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005930{
5931 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005932 int did_freeze = 0;
5933
5934 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5935 did_freeze = 1;
5936 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5937 md_wakeup_thread(mddev->thread);
5938 }
NeilBrownf851b602014-12-11 10:02:10 +11005939 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005940 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005941 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005942 /* Thread might be blocked waiting for metadata update
5943 * which will now never happen */
5944 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005945
Shaohua Li29530792016-12-08 15:48:19 -08005946 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10005947 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005948 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005949 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5950 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005951 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005952 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005953 mddev_lock_nointr(mddev);
5954
NeilBrowna4bd82d2010-03-29 13:23:10 +11005955 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005956 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005957 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005958 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005959 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005960 if (did_freeze) {
5961 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005962 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005963 md_wakeup_thread(mddev->thread);
5964 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005965 err = -EBUSY;
5966 goto out;
5967 }
5968 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005969 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005970
5971 err = -ENXIO;
5972 if (mddev->ro==1)
5973 goto out;
5974 mddev->ro = 1;
5975 set_disk_ro(mddev->gendisk, 1);
5976 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005977 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5978 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005979 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005980 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005981 }
5982out:
5983 mutex_unlock(&mddev->open_mutex);
5984 return err;
5985}
5986
NeilBrown9e653b62006-06-26 00:27:58 -07005987/* mode:
5988 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005989 * 2 - stop but do not disassemble array
5990 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005991static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005992 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005995 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005996 int did_freeze = 0;
5997
5998 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5999 did_freeze = 1;
6000 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6001 md_wakeup_thread(mddev->thread);
6002 }
NeilBrownf851b602014-12-11 10:02:10 +11006003 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006004 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006005 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006006 /* Thread might be blocked waiting for metadata update
6007 * which will now never happen */
6008 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006009
NeilBrown30b8feb2013-11-14 15:16:17 +11006010 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006011 wait_event(resync_wait, (mddev->sync_thread == NULL &&
6012 !test_bit(MD_RECOVERY_RUNNING,
6013 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11006014 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015
NeilBrownc8c00a62009-08-10 12:50:52 +10006016 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006017 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006018 mddev->sysfs_active ||
6019 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006020 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006021 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10006022 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11006023 if (did_freeze) {
6024 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006025 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006026 md_wakeup_thread(mddev->thread);
6027 }
NeilBrown260fa032013-08-27 16:44:13 +10006028 return -EBUSY;
6029 }
NeilBrown6e17b022010-08-07 21:41:19 +10006030 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11006031 if (mddev->ro)
6032 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11006033
NeilBrowndefad612011-01-14 09:14:33 +11006034 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11006035 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01006036 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006037
NeilBrowna4bd82d2010-03-29 13:23:10 +11006038 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10006039 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08006040
NeilBrowndafb20f2012-03-19 12:46:39 +11006041 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10006042 if (rdev->raid_disk >= 0)
6043 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10006044
NeilBrowna4bd82d2010-03-29 13:23:10 +11006045 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10006046 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006047 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006048 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08006049
NeilBrowna4bd82d2010-03-29 13:23:10 +11006050 if (mddev->ro)
6051 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10006052 } else
6053 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 /*
6055 * Free resources if final stop
6056 */
NeilBrown9e653b62006-06-26 00:27:58 -07006057 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006058 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
NeilBrownc3d97142009-12-14 12:49:52 +11006060 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11006061 struct file *f = mddev->bitmap_info.file;
6062 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11006063 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11006064 spin_unlock(&mddev->lock);
6065 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08006066 }
NeilBrownc3d97142009-12-14 12:49:52 +11006067 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08006068
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 export_array(mddev);
6070
NeilBrown6177b472010-03-29 11:37:13 +11006071 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11006072 if (mddev->hold_active == UNTIL_STOP)
6073 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006074 }
NeilBrownd7603b72006-01-06 00:20:30 -08006075 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006076 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10006077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078}
6079
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006080#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11006081static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082{
NeilBrown3cb03002011-10-11 16:45:26 +11006083 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 int err;
6085
NeilBrowna757e642005-04-16 15:26:42 -07006086 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088
NeilBrown9d487392016-11-02 14:16:49 +11006089 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090
NeilBrowndafb20f2012-03-19 12:46:39 +11006091 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11006093 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 }
NeilBrown9d487392016-11-02 14:16:49 +11006095 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096
NeilBrownd710e132008-10-13 11:55:12 +11006097 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006099 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10006100 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 }
6102}
6103
6104/*
6105 * lets try to run arrays based on all disks that have arrived
6106 * until now. (those are in pending_raid_disks)
6107 *
6108 * the method: pick the first pending disk, collect all disks with
6109 * the same UUID, remove all from the pending list and put them into
6110 * the 'same_array' list. Then order this list based on superblock
6111 * update time (freshest comes first), kick out 'old' disks and
6112 * compare superblocks. If everything's fine then run it.
6113 *
6114 * If "unit" is allocated, then bump its reference count
6115 */
6116static void autorun_devices(int part)
6117{
NeilBrown3cb03002011-10-11 16:45:26 +11006118 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006119 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 char b[BDEVNAME_SIZE];
6121
NeilBrown9d487392016-11-02 14:16:49 +11006122 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07006124 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08006126 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11006128 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129
NeilBrown9d487392016-11-02 14:16:49 +11006130 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006132 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006134 pr_debug("md: adding %s ...\n",
6135 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 list_move(&rdev->same_set, &candidates);
6137 }
6138 /*
6139 * now we have a set of devices, with all of them having
6140 * mostly sane superblocks. It's time to allocate the
6141 * mddev.
6142 */
NeilBrowne8703fe2006-10-03 01:15:59 -07006143 if (part) {
6144 dev = MKDEV(mdp_major,
6145 rdev0->preferred_minor << MdpMinorShift);
6146 unit = MINOR(dev) >> MdpMinorShift;
6147 } else {
6148 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6149 unit = MINOR(dev);
6150 }
6151 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006152 pr_warn("md: unit number in %s is bad: %d\n",
6153 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 break;
6155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156
6157 md_probe(dev, NULL, NULL);
6158 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006159 if (!mddev || !mddev->gendisk) {
6160 if (mddev)
6161 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 break;
6163 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006164 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006165 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 else if (mddev->raid_disks || mddev->major_version
6167 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006168 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 mdname(mddev), bdevname(rdev0->bdev,b));
6170 mddev_unlock(mddev);
6171 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006172 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006173 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006174 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 list_del_init(&rdev->same_set);
6176 if (bind_rdev_to_array(rdev, mddev))
6177 export_rdev(rdev);
6178 }
6179 autorun_array(mddev);
6180 mddev_unlock(mddev);
6181 }
6182 /* on success, candidates will be empty, on error
6183 * it won't...
6184 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006185 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006186 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 mddev_put(mddev);
6190 }
NeilBrown9d487392016-11-02 14:16:49 +11006191 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006193#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194
NeilBrownf72ffdd2014-09-30 14:23:59 +10006195static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196{
6197 mdu_version_t ver;
6198
6199 ver.major = MD_MAJOR_VERSION;
6200 ver.minor = MD_MINOR_VERSION;
6201 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6202
6203 if (copy_to_user(arg, &ver, sizeof(ver)))
6204 return -EFAULT;
6205
6206 return 0;
6207}
6208
NeilBrownf72ffdd2014-09-30 14:23:59 +10006209static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210{
6211 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006212 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006213 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214
NeilBrown1ca69c42012-10-11 13:37:33 +11006215 nr = working = insync = failed = spare = 0;
6216 rcu_read_lock();
6217 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006219 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 failed++;
6221 else {
6222 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006223 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006224 insync++;
Song Liub347af82016-08-11 17:14:45 -07006225 else if (test_bit(Journal, &rdev->flags))
6226 /* TODO: add journal count to md_u.h */
6227 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 else
6229 spare++;
6230 }
6231 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006232 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233
6234 info.major_version = mddev->major_version;
6235 info.minor_version = mddev->minor_version;
6236 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006237 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006239 info.size = mddev->dev_sectors / 2;
6240 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006241 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 info.nr_disks = nr;
6243 info.raid_disks = mddev->raid_disks;
6244 info.md_minor = mddev->md_minor;
6245 info.not_persistent= !mddev->persistent;
6246
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006247 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 info.state = 0;
6249 if (mddev->in_sync)
6250 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006251 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006252 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006253 if (mddev_is_clustered(mddev))
6254 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006255 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 info.working_disks = working;
6257 info.failed_disks = failed;
6258 info.spare_disks = spare;
6259
6260 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006261 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262
6263 if (copy_to_user(arg, &info, sizeof(info)))
6264 return -EFAULT;
6265
6266 return 0;
6267}
6268
NeilBrownf72ffdd2014-09-30 14:23:59 +10006269static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006270{
6271 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006272 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006273 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006274
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006275 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006276 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006277 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006278
NeilBrown32a76272005-06-21 17:17:14 -07006279 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006280 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006281 /* bitmap enabled */
6282 if (mddev->bitmap_info.file) {
6283 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6284 sizeof(file->pathname));
6285 if (IS_ERR(ptr))
6286 err = PTR_ERR(ptr);
6287 else
6288 memmove(file->pathname, ptr,
6289 sizeof(file->pathname)-(ptr-file->pathname));
6290 }
NeilBrown4af1a042014-12-15 12:57:00 +11006291 spin_unlock(&mddev->lock);
6292
6293 if (err == 0 &&
6294 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006295 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006296
NeilBrown32a76272005-06-21 17:17:14 -07006297 kfree(file);
6298 return err;
6299}
6300
NeilBrownf72ffdd2014-09-30 14:23:59 +10006301static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302{
6303 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006304 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305
6306 if (copy_from_user(&info, arg, sizeof(info)))
6307 return -EFAULT;
6308
NeilBrown1ca69c42012-10-11 13:37:33 +11006309 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006310 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 if (rdev) {
6312 info.major = MAJOR(rdev->bdev->bd_dev);
6313 info.minor = MINOR(rdev->bdev->bd_dev);
6314 info.raid_disk = rdev->raid_disk;
6315 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006316 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006318 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 info.state |= (1<<MD_DISK_ACTIVE);
6320 info.state |= (1<<MD_DISK_SYNC);
6321 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006322 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006323 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006324 if (test_bit(WriteMostly, &rdev->flags))
6325 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006326 if (test_bit(FailFast, &rdev->flags))
6327 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 } else {
6329 info.major = info.minor = 0;
6330 info.raid_disk = -1;
6331 info.state = (1<<MD_DISK_REMOVED);
6332 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006333 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334
6335 if (copy_to_user(arg, &info, sizeof(info)))
6336 return -EFAULT;
6337
6338 return 0;
6339}
6340
NeilBrownf72ffdd2014-09-30 14:23:59 +10006341static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342{
6343 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006344 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 dev_t dev = MKDEV(info->major,info->minor);
6346
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006347 if (mddev_is_clustered(mddev) &&
6348 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006349 pr_warn("%s: Cannot add to clustered mddev.\n",
6350 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006351 return -EINVAL;
6352 }
6353
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6355 return -EOVERFLOW;
6356
6357 if (!mddev->raid_disks) {
6358 int err;
6359 /* expecting a device which has a superblock */
6360 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6361 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006362 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 PTR_ERR(rdev));
6364 return PTR_ERR(rdev);
6365 }
6366 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006367 struct md_rdev *rdev0
6368 = list_entry(mddev->disks.next,
6369 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006370 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 .load_super(rdev, rdev0, mddev->minor_version);
6372 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006373 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006374 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 bdevname(rdev0->bdev,b2));
6376 export_rdev(rdev);
6377 return -EINVAL;
6378 }
6379 }
6380 err = bind_rdev_to_array(rdev, mddev);
6381 if (err)
6382 export_rdev(rdev);
6383 return err;
6384 }
6385
6386 /*
6387 * add_new_disk can be used once the array is assembled
6388 * to add "hot spares". They must already have a superblock
6389 * written
6390 */
6391 if (mddev->pers) {
6392 int err;
6393 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006394 pr_warn("%s: personality does not support diskops!\n",
6395 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 return -EINVAL;
6397 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006398 if (mddev->persistent)
6399 rdev = md_import_device(dev, mddev->major_version,
6400 mddev->minor_version);
6401 else
6402 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006404 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 PTR_ERR(rdev));
6406 return PTR_ERR(rdev);
6407 }
NeilBrown1a855a02010-12-09 16:36:28 +11006408 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006409 if (!mddev->persistent) {
6410 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006411 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006412 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006413 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006414 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006415 } else
NeilBrown41158c72005-06-21 17:17:25 -07006416 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006417 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006418 } else
6419 super_types[mddev->major_version].
6420 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006421 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006422 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006423 /* This was a hot-add request, but events doesn't
6424 * match, so reject it.
6425 */
6426 export_rdev(rdev);
6427 return -EINVAL;
6428 }
6429
NeilBrownb2d444d2005-11-08 21:39:31 -08006430 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006431 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6432 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006433 else
6434 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006435 if (info->state & (1<<MD_DISK_FAILFAST))
6436 set_bit(FailFast, &rdev->flags);
6437 else
6438 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006439
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006440 if (info->state & (1<<MD_DISK_JOURNAL)) {
6441 struct md_rdev *rdev2;
6442 bool has_journal = false;
6443
6444 /* make sure no existing journal disk */
6445 rdev_for_each(rdev2, mddev) {
6446 if (test_bit(Journal, &rdev2->flags)) {
6447 has_journal = true;
6448 break;
6449 }
6450 }
NeilBrown230b55f2017-10-17 14:24:09 +11006451 if (has_journal || mddev->bitmap) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006452 export_rdev(rdev);
6453 return -EBUSY;
6454 }
Song Liubac624f2015-08-13 14:31:55 -07006455 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006456 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006457 /*
6458 * check whether the device shows up in other nodes
6459 */
6460 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006461 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006462 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006463 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006464 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006465 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006466 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006467 export_rdev(rdev);
6468 return err;
6469 }
6470 }
6471 }
6472
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 rdev->raid_disk = -1;
6474 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006475
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 if (err)
6477 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006478
6479 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006480 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6481 if (!err) {
6482 err = md_cluster_ops->new_disk_ack(mddev,
6483 err == 0);
6484 if (err)
6485 md_kick_rdev_from_array(rdev);
6486 }
6487 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006488 if (err)
6489 md_cluster_ops->add_new_disk_cancel(mddev);
6490 else
6491 err = add_bound_rdev(rdev);
6492 }
6493
6494 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006495 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006496
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497 return err;
6498 }
6499
6500 /* otherwise, add_new_disk is only allowed
6501 * for major_version==0 superblocks
6502 */
6503 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006504 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 return -EINVAL;
6506 }
6507
6508 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6509 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006510 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006512 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513 PTR_ERR(rdev));
6514 return PTR_ERR(rdev);
6515 }
6516 rdev->desc_nr = info->number;
6517 if (info->raid_disk < mddev->raid_disks)
6518 rdev->raid_disk = info->raid_disk;
6519 else
6520 rdev->raid_disk = -1;
6521
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006523 if (info->state & (1<<MD_DISK_SYNC))
6524 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006526 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6527 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006528 if (info->state & (1<<MD_DISK_FAILFAST))
6529 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006530
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006532 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006533 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6534 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006535 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006536 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537
NeilBrown2bf071b2006-01-06 00:20:55 -08006538 err = bind_rdev_to_array(rdev, mddev);
6539 if (err) {
6540 export_rdev(rdev);
6541 return err;
6542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 }
6544
6545 return 0;
6546}
6547
NeilBrownf72ffdd2014-09-30 14:23:59 +10006548static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549{
6550 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006551 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552
Yufen Yuc42a0e22018-05-04 18:08:10 +08006553 if (!mddev->pers)
6554 return -ENODEV;
6555
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 rdev = find_rdev(mddev, dev);
6557 if (!rdev)
6558 return -ENXIO;
6559
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006560 if (rdev->raid_disk < 0)
6561 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006562
NeilBrown3ea8929d2013-04-24 11:42:41 +10006563 clear_bit(Blocked, &rdev->flags);
6564 remove_and_add_spares(mddev, rdev);
6565
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566 if (rdev->raid_disk >= 0)
6567 goto busy;
6568
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006569kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006570 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006571 md_cluster_ops->remove_disk(mddev, rdev);
6572
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006573 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006574 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006575 if (mddev->thread)
6576 md_wakeup_thread(mddev->thread);
6577 else
6578 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006579 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580
6581 return 0;
6582busy:
NeilBrown9d487392016-11-02 14:16:49 +11006583 pr_debug("md: cannot remove active disk %s from %s ...\n",
6584 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 return -EBUSY;
6586}
6587
NeilBrownf72ffdd2014-09-30 14:23:59 +10006588static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589{
6590 char b[BDEVNAME_SIZE];
6591 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006592 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593
6594 if (!mddev->pers)
6595 return -ENODEV;
6596
6597 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006598 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599 mdname(mddev));
6600 return -EINVAL;
6601 }
6602 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006603 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 mdname(mddev));
6605 return -EINVAL;
6606 }
6607
NeilBrownd710e132008-10-13 11:55:12 +11006608 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006610 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 PTR_ERR(rdev));
6612 return -EINVAL;
6613 }
6614
6615 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006616 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006618 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619
NeilBrown8190e752009-06-18 08:48:58 +10006620 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621
NeilBrownb2d444d2005-11-08 21:39:31 -08006622 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006623 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624 bdevname(rdev->bdev,b), mdname(mddev));
6625 err = -EINVAL;
6626 goto abort_export;
6627 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006628
NeilBrownb2d444d2005-11-08 21:39:31 -08006629 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006631 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006632 err = bind_rdev_to_array(rdev, mddev);
6633 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006634 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635
6636 /*
6637 * The rest should better be atomic, we can have disk failures
6638 * noticed in interrupt contexts ...
6639 */
6640
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 rdev->raid_disk = -1;
6642
Shaohua Li29530792016-12-08 15:48:19 -08006643 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006644 if (!mddev->thread)
6645 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 /*
6647 * Kick recovery, maybe this spare has to be added to the
6648 * array immediately.
6649 */
6650 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6651 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006652 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 return 0;
6654
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655abort_export:
6656 export_rdev(rdev);
6657 return err;
6658}
6659
NeilBrownfd01b882011-10-11 16:47:53 +11006660static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006661{
NeilBrown035328c2014-04-09 12:25:40 +10006662 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006663
NeilBrown36fa3062005-09-09 16:23:45 -07006664 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006665 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006666 return -EBUSY;
6667 if (mddev->recovery || mddev->sync_thread)
6668 return -EBUSY;
6669 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006670 }
6671
NeilBrown36fa3062005-09-09 16:23:45 -07006672 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006673 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006674 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006675
NeilBrown1e594bb2014-12-15 12:57:00 +11006676 if (mddev->bitmap || mddev->bitmap_info.file)
6677 return -EEXIST; /* cannot add when bitmap is present */
6678 f = fget(fd);
6679
6680 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006681 pr_warn("%s: error: failed to get bitmap file\n",
6682 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006683 return -EBADF;
6684 }
6685
NeilBrown1e594bb2014-12-15 12:57:00 +11006686 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006687 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006688 pr_warn("%s: error: bitmap file must be a regular file\n",
6689 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006690 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006691 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006692 pr_warn("%s: error: bitmap file must open for write\n",
6693 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006694 err = -EBADF;
6695 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006696 pr_warn("%s: error: bitmap file is already in use\n",
6697 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006698 err = -EBUSY;
6699 }
6700 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006701 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006702 return err;
6703 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006704 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006705 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006706 } else if (mddev->bitmap == NULL)
6707 return -ENOENT; /* cannot remove what isn't there */
6708 err = 0;
6709 if (mddev->pers) {
NeilBrown69e51b42010-06-01 19:37:35 +10006710 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006711 struct bitmap *bitmap;
6712
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006713 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006714 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006715 if (!IS_ERR(bitmap)) {
6716 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006717 err = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006718 } else
6719 err = PTR_ERR(bitmap);
NeilBrown52a0d492017-10-17 13:46:43 +11006720 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006721 md_bitmap_destroy(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11006722 fd = -1;
6723 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006724 mddev_resume(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11006725 } else if (fd < 0) {
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006726 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006727 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006728 mddev_resume(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006729 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006730 }
6731 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006732 struct file *f = mddev->bitmap_info.file;
6733 if (f) {
6734 spin_lock(&mddev->lock);
6735 mddev->bitmap_info.file = NULL;
6736 spin_unlock(&mddev->lock);
6737 fput(f);
6738 }
NeilBrown36fa3062005-09-09 16:23:45 -07006739 }
6740
NeilBrown32a76272005-06-21 17:17:14 -07006741 return err;
6742}
6743
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744/*
6745 * set_array_info is used two different ways
6746 * The original usage is when creating a new array.
6747 * In this usage, raid_disks is > 0 and it together with
6748 * level, size, not_persistent,layout,chunksize determine the
6749 * shape of the array.
6750 * This will always create an array with a type-0.90.0 superblock.
6751 * The newer usage is when assembling an array.
6752 * In this case raid_disks will be 0, and the major_version field is
6753 * use to determine which style super-blocks are to be found on the devices.
6754 * The minor and patch _version numbers are also kept incase the
6755 * super_block handler wishes to interpret them.
6756 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006757static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758{
6759
6760 if (info->raid_disks == 0) {
6761 /* just setting version number for superblock loading */
6762 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006763 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 super_types[info->major_version].name == NULL) {
6765 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11006766 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 info->major_version);
6768 return -EINVAL;
6769 }
6770 mddev->major_version = info->major_version;
6771 mddev->minor_version = info->minor_version;
6772 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006773 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006774 /* ensure mddev_put doesn't delete this now that there
6775 * is some minimal configuration.
6776 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006777 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 return 0;
6779 }
6780 mddev->major_version = MD_MAJOR_VERSION;
6781 mddev->minor_version = MD_MINOR_VERSION;
6782 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006783 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784
6785 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006786 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006787 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 mddev->raid_disks = info->raid_disks;
6789 /* don't set md_minor, it is determined by which /dev/md* was
6790 * openned
6791 */
6792 if (info->state & (1<<MD_SB_CLEAN))
6793 mddev->recovery_cp = MaxSector;
6794 else
6795 mddev->recovery_cp = 0;
6796 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006797 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798
6799 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006800 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801
Shaohua Li29530792016-12-08 15:48:19 -08006802 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11006803 mddev->max_disks = MD_SB_DISKS;
6804 mddev->flags = 0;
6805 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006806 }
6807 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808
NeilBrownc3d97142009-12-14 12:49:52 +11006809 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006810 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006811 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006812
NeilBrownf6705572006-03-27 01:18:11 -08006813 mddev->reshape_position = MaxSector;
6814
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815 /*
6816 * Generate a 128 bit UUID
6817 */
6818 get_random_bytes(mddev->uuid, 16);
6819
NeilBrownf6705572006-03-27 01:18:11 -08006820 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006821 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006822 mddev->new_layout = mddev->layout;
6823 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006824 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006825
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 return 0;
6827}
6828
NeilBrownfd01b882011-10-11 16:47:53 +11006829void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006830{
Shaohua Liefa4b772017-10-18 22:08:13 -07006831 lockdep_assert_held(&mddev->reconfig_mutex);
Dan Williamsb522adc2009-03-31 15:00:31 +11006832
6833 if (mddev->external_size)
6834 return;
6835
Dan Williams1f403622009-03-31 14:59:03 +11006836 mddev->array_sectors = array_sectors;
6837}
6838EXPORT_SYMBOL(md_set_array_sectors);
6839
NeilBrownfd01b882011-10-11 16:47:53 +11006840static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006841{
NeilBrown3cb03002011-10-11 16:45:26 +11006842 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006843 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006844 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08006845 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006846
NeilBrowna35b0d62006-01-06 00:20:49 -08006847 if (mddev->pers->resize == NULL)
6848 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006849 /* The "num_sectors" is the number of sectors of each device that
6850 * is used. This can only make sense for arrays with redundancy.
6851 * linear and raid0 always use whatever space is available. We can only
6852 * consider changing this number if no resync or reconstruction is
6853 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006854 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006855 * of each device. If num_sectors is zero, we find the largest size
6856 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006857 */
NeilBrownf851b602014-12-11 10:02:10 +11006858 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6859 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006860 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006861 if (mddev->ro)
6862 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006863
NeilBrowndafb20f2012-03-19 12:46:39 +11006864 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006865 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006866
Andre Nolld71f9f82008-07-11 22:02:22 +10006867 if (fit && (num_sectors == 0 || num_sectors > avail))
6868 num_sectors = avail;
6869 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006870 return -ENOSPC;
6871 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006872 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08006873 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08006874 if (mddev_is_clustered(mddev))
6875 md_cluster_ops->update_size(mddev, old_dev_sectors);
6876 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08006877 set_capacity(mddev->gendisk, mddev->array_sectors);
6878 revalidate_disk(mddev->gendisk);
6879 }
6880 }
NeilBrowna35b0d62006-01-06 00:20:49 -08006881 return rv;
6882}
6883
NeilBrownfd01b882011-10-11 16:47:53 +11006884static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006885{
6886 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006887 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006888 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006889 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006890 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006891 if (mddev->ro)
6892 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006893 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006894 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006895 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006896 if (mddev->sync_thread ||
6897 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6898 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006899 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006900
6901 rdev_for_each(rdev, mddev) {
6902 if (mddev->raid_disks < raid_disks &&
6903 rdev->data_offset < rdev->new_data_offset)
6904 return -EINVAL;
6905 if (mddev->raid_disks > raid_disks &&
6906 rdev->data_offset > rdev->new_data_offset)
6907 return -EINVAL;
6908 }
6909
NeilBrown63c70c42006-03-27 01:18:13 -08006910 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006911 if (mddev->delta_disks < 0)
6912 mddev->reshape_backwards = 1;
6913 else if (mddev->delta_disks > 0)
6914 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006915
6916 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006917 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006918 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006919 mddev->reshape_backwards = 0;
6920 }
NeilBrownda943b992006-01-06 00:20:54 -08006921 return rv;
6922}
6923
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924/*
6925 * update_array_info is used to change the configuration of an
6926 * on-line array.
6927 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6928 * fields in the info are checked against the array.
6929 * Any differences that cannot be handled will cause an error.
6930 * Normally, only one change can be managed at a time.
6931 */
NeilBrownfd01b882011-10-11 16:47:53 +11006932static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933{
6934 int rv = 0;
6935 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006936 int state = 0;
6937
6938 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006939 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006940 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941
6942 if (mddev->major_version != info->major_version ||
6943 mddev->minor_version != info->minor_version ||
6944/* mddev->patch_version != info->patch_version || */
6945 mddev->ctime != info->ctime ||
6946 mddev->level != info->level ||
6947/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006948 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006949 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006950 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6951 ((state^info->state) & 0xfffffe00)
6952 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 return -EINVAL;
6954 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006955 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6956 cnt++;
6957 if (mddev->raid_disks != info->raid_disks)
6958 cnt++;
6959 if (mddev->layout != info->layout)
6960 cnt++;
6961 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6962 cnt++;
6963 if (cnt == 0)
6964 return 0;
6965 if (cnt > 1)
6966 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967
6968 if (mddev->layout != info->layout) {
6969 /* Change layout
6970 * we don't need to do anything at the md level, the
6971 * personality will take care of it all.
6972 */
NeilBrown50ac1682009-06-18 08:47:55 +10006973 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006975 else {
6976 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006977 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006978 if (rv)
6979 mddev->new_layout = mddev->layout;
6980 return rv;
6981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006983 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006984 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985
NeilBrownda943b992006-01-06 00:20:54 -08006986 if (mddev->raid_disks != info->raid_disks)
6987 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988
NeilBrown36fa3062005-09-09 16:23:45 -07006989 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006990 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6991 rv = -EINVAL;
6992 goto err;
6993 }
6994 if (mddev->recovery || mddev->sync_thread) {
6995 rv = -EBUSY;
6996 goto err;
6997 }
NeilBrown36fa3062005-09-09 16:23:45 -07006998 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006999 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07007000 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007001 if (mddev->bitmap) {
7002 rv = -EEXIST;
7003 goto err;
7004 }
7005 if (mddev->bitmap_info.default_offset == 0) {
7006 rv = -EINVAL;
7007 goto err;
7008 }
NeilBrownc3d97142009-12-14 12:49:52 +11007009 mddev->bitmap_info.offset =
7010 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10007011 mddev->bitmap_info.space =
7012 mddev->bitmap_info.default_space;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007013 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007014 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007015 if (!IS_ERR(bitmap)) {
7016 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007017 rv = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007018 } else
7019 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07007020 if (rv)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007021 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007022 mddev_resume(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07007023 } else {
7024 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007025 if (!mddev->bitmap) {
7026 rv = -ENOENT;
7027 goto err;
7028 }
7029 if (mddev->bitmap->storage.file) {
7030 rv = -EINVAL;
7031 goto err;
7032 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11007033 if (mddev->bitmap_info.nodes) {
7034 /* hold PW on all the bitmap lock */
7035 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11007036 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 +11007037 rv = -EPERM;
7038 md_cluster_ops->unlock_all_bitmaps(mddev);
7039 goto err;
7040 }
7041
7042 mddev->bitmap_info.nodes = 0;
7043 md_cluster_ops->leave(mddev);
7044 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007045 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007046 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007047 mddev_resume(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11007048 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007049 }
7050 }
NeilBrown850b2b422006-10-03 01:15:46 -07007051 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007052 return rv;
7053err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 return rv;
7055}
7056
NeilBrownfd01b882011-10-11 16:47:53 +11007057static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058{
NeilBrown3cb03002011-10-11 16:45:26 +11007059 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11007060 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061
7062 if (mddev->pers == NULL)
7063 return -ENODEV;
7064
NeilBrown1ca69c42012-10-11 13:37:33 +11007065 rcu_read_lock();
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01007066 rdev = md_find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11007068 err = -ENODEV;
7069 else {
7070 md_error(mddev, rdev);
7071 if (!test_bit(Faulty, &rdev->flags))
7072 err = -EBUSY;
7073 }
7074 rcu_read_unlock();
7075 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076}
7077
Andre Noll2f9618c2008-04-25 18:57:58 +02007078/*
7079 * We have a problem here : there is no easy way to give a CHS
7080 * virtual geometry. We currently pretend that we have a 2 heads
7081 * 4 sectors (with a BIG number of cylinders...). This drives
7082 * dosfs just mad... ;-)
7083 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007084static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7085{
NeilBrownfd01b882011-10-11 16:47:53 +11007086 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007087
7088 geo->heads = 2;
7089 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11007090 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007091 return 0;
7092}
7093
Nicolas Schichancb335f82014-01-15 16:58:52 +01007094static inline bool md_ioctl_valid(unsigned int cmd)
7095{
7096 switch (cmd) {
7097 case ADD_NEW_DISK:
7098 case BLKROSET:
7099 case GET_ARRAY_INFO:
7100 case GET_BITMAP_FILE:
7101 case GET_DISK_INFO:
7102 case HOT_ADD_DISK:
7103 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007104 case RAID_AUTORUN:
7105 case RAID_VERSION:
7106 case RESTART_ARRAY_RW:
7107 case RUN_ARRAY:
7108 case SET_ARRAY_INFO:
7109 case SET_BITMAP_FILE:
7110 case SET_DISK_FAULTY:
7111 case STOP_ARRAY:
7112 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007113 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007114 return true;
7115 default:
7116 return false;
7117 }
7118}
7119
Al Viroa39907f2008-03-02 10:31:15 -05007120static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 unsigned int cmd, unsigned long arg)
7122{
7123 int err = 0;
7124 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11007125 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10007126 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08007127 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128
Nicolas Schichancb335f82014-01-15 16:58:52 +01007129 if (!md_ioctl_valid(cmd))
7130 return -ENOTTY;
7131
NeilBrown506c9e42011-12-23 10:17:26 +11007132 switch (cmd) {
7133 case RAID_VERSION:
7134 case GET_ARRAY_INFO:
7135 case GET_DISK_INFO:
7136 break;
7137 default:
7138 if (!capable(CAP_SYS_ADMIN))
7139 return -EACCES;
7140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141
7142 /*
7143 * Commands dealing with the RAID driver but not any
7144 * particular array:
7145 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007146 switch (cmd) {
7147 case RAID_VERSION:
7148 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007149 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11007152 case RAID_AUTORUN:
7153 err = 0;
7154 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007155 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11007157 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 }
7159
7160 /*
7161 * Commands creating/starting a new array:
7162 */
7163
Al Viroa39907f2008-03-02 10:31:15 -05007164 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165
7166 if (!mddev) {
7167 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007168 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169 }
7170
NeilBrown1ca69c42012-10-11 13:37:33 +11007171 /* Some actions do not requires the mutex */
7172 switch (cmd) {
7173 case GET_ARRAY_INFO:
7174 if (!mddev->raid_disks && !mddev->external)
7175 err = -ENODEV;
7176 else
7177 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007178 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007179
7180 case GET_DISK_INFO:
7181 if (!mddev->raid_disks && !mddev->external)
7182 err = -ENODEV;
7183 else
7184 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007185 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007186
7187 case SET_DISK_FAULTY:
7188 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007189 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007190
7191 case GET_BITMAP_FILE:
7192 err = get_bitmap_file(mddev, argp);
7193 goto out;
7194
NeilBrown1ca69c42012-10-11 13:37:33 +11007195 }
7196
NeilBrowna7a3f082012-12-11 13:35:54 +11007197 if (cmd == ADD_NEW_DISK)
7198 /* need to ensure md_delayed_delete() has completed */
7199 flush_workqueue(md_misc_wq);
7200
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007201 if (cmd == HOT_REMOVE_DISK)
7202 /* need to ensure recovery thread has run */
7203 wait_event_interruptible_timeout(mddev->sb_wait,
7204 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007205 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007206 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007207 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7208 /* Need to flush page cache, and ensure no-one else opens
7209 * and writes
7210 */
7211 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007212 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007213 mutex_unlock(&mddev->open_mutex);
7214 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007215 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007216 }
NeilBrown065e5192017-04-06 11:16:33 +08007217 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007218 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007219 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007220 mutex_unlock(&mddev->open_mutex);
7221 sync_blockdev(bdev);
7222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223 err = mddev_lock(mddev);
7224 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007225 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7226 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007227 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 }
7229
NeilBrownc02c0ae2012-12-11 13:39:21 +11007230 if (cmd == SET_ARRAY_INFO) {
7231 mdu_array_info_t info;
7232 if (!arg)
7233 memset(&info, 0, sizeof(info));
7234 else if (copy_from_user(&info, argp, sizeof(info))) {
7235 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007236 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007237 }
7238 if (mddev->pers) {
7239 err = update_array_info(mddev, &info);
7240 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007241 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007242 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007244 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007245 }
7246 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007247 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007248 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007249 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007250 }
7251 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007252 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007253 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007254 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007255 }
7256 err = set_array_info(mddev, &info);
7257 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007258 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007259 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007260 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007261 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262 }
7263
7264 /*
7265 * Commands querying/configuring an existing array:
7266 */
NeilBrown32a76272005-06-21 17:17:14 -07007267 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007268 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007269 if ((!mddev->raid_disks && !mddev->external)
7270 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7271 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7272 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007274 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275 }
7276
7277 /*
7278 * Commands even a read-only array can execute:
7279 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007280 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007281 case RESTART_ARRAY_RW:
7282 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007283 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007284
7285 case STOP_ARRAY:
7286 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007287 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007288
7289 case STOP_ARRAY_RO:
7290 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007291 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007292
NeilBrown3ea8929d2013-04-24 11:42:41 +10007293 case HOT_REMOVE_DISK:
7294 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007295 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007296
NeilBrown7ceb17e2013-04-24 11:42:42 +10007297 case ADD_NEW_DISK:
7298 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007299 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007300 * So require mddev->pers and MD_DISK_SYNC.
7301 */
7302 if (mddev->pers) {
7303 mdu_disk_info_t info;
7304 if (copy_from_user(&info, argp, sizeof(info)))
7305 err = -EFAULT;
7306 else if (!(info.state & (1<<MD_DISK_SYNC)))
7307 /* Need to clear read-only for this */
7308 break;
7309 else
7310 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007311 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007312 }
7313 break;
7314
NeilBrownc02c0ae2012-12-11 13:39:21 +11007315 case BLKROSET:
7316 if (get_user(ro, (int __user *)(arg))) {
7317 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007318 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007319 }
7320 err = -EINVAL;
7321
7322 /* if the bdev is going readonly the value of mddev->ro
7323 * does not matter, no writes are coming
7324 */
7325 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007326 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007327
NeilBrownc02c0ae2012-12-11 13:39:21 +11007328 /* are we are already prepared for writes? */
7329 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007330 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007331
7332 /* transitioning to readauto need only happen for
7333 * arrays that call md_write_start
7334 */
7335 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007337 if (err == 0) {
7338 mddev->ro = 2;
7339 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007340 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007341 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007342 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343 }
7344
7345 /*
7346 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007347 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 */
NeilBrown326eb172014-09-30 15:36:28 +10007349 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007350 if (mddev->ro == 2) {
7351 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007352 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007353 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007354 /* mddev_unlock will wake thread */
7355 /* If a device failed while we were read-only, we
7356 * need to make sure the metadata is updated now.
7357 */
Shaohua Li29530792016-12-08 15:48:19 -08007358 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007359 mddev_unlock(mddev);
7360 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007361 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7362 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007363 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007364 }
NeilBrownf91de922005-11-08 21:39:36 -08007365 } else {
7366 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007367 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 }
7370
NeilBrownc02c0ae2012-12-11 13:39:21 +11007371 switch (cmd) {
7372 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007374 mdu_disk_info_t info;
7375 if (copy_from_user(&info, argp, sizeof(info)))
7376 err = -EFAULT;
7377 else
7378 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007379 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007382 case CLUSTERED_DISK_NACK:
7383 if (mddev_is_clustered(mddev))
7384 md_cluster_ops->new_disk_ack(mddev, false);
7385 else
7386 err = -EINVAL;
7387 goto unlock;
7388
NeilBrownc02c0ae2012-12-11 13:39:21 +11007389 case HOT_ADD_DISK:
7390 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007391 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392
NeilBrownc02c0ae2012-12-11 13:39:21 +11007393 case RUN_ARRAY:
7394 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007395 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396
NeilBrownc02c0ae2012-12-11 13:39:21 +11007397 case SET_BITMAP_FILE:
7398 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007399 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007400
NeilBrownc02c0ae2012-12-11 13:39:21 +11007401 default:
7402 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007403 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 }
7405
NeilBrown3adc28d2014-09-30 15:46:41 +10007406unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007407 if (mddev->hold_active == UNTIL_IOCTL &&
7408 err != -EINVAL)
7409 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007411out:
NeilBrown065e5192017-04-06 11:16:33 +08007412 if(did_set_md_closing)
7413 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414 return err;
7415}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007416#ifdef CONFIG_COMPAT
7417static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7418 unsigned int cmd, unsigned long arg)
7419{
7420 switch (cmd) {
7421 case HOT_REMOVE_DISK:
7422 case HOT_ADD_DISK:
7423 case SET_DISK_FAULTY:
7424 case SET_BITMAP_FILE:
7425 /* These take in integer arg, do not convert */
7426 break;
7427 default:
7428 arg = (unsigned long)compat_ptr(arg);
7429 break;
7430 }
7431
7432 return md_ioctl(bdev, mode, cmd, arg);
7433}
7434#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435
Al Viroa39907f2008-03-02 10:31:15 -05007436static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437{
7438 /*
7439 * Succeed if we can lock the mddev, which confirms that
7440 * it isn't being stopped right now.
7441 */
NeilBrownfd01b882011-10-11 16:47:53 +11007442 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443 int err;
7444
Yuanhan Liu0c098222012-05-22 13:55:32 +10007445 if (!mddev)
7446 return -ENODEV;
7447
NeilBrownd3374822009-01-09 08:31:10 +11007448 if (mddev->gendisk != bdev->bd_disk) {
7449 /* we are racing with mddev_put which is discarding this
7450 * bd_disk.
7451 */
7452 mddev_put(mddev);
7453 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007454 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007455 /* Then retry the open from the top */
7456 return -ERESTARTSYS;
7457 }
7458 BUG_ON(mddev != bdev->bd_disk->private_data);
7459
NeilBrownc8c00a62009-08-10 12:50:52 +10007460 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461 goto out;
7462
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007463 if (test_bit(MD_CLOSING, &mddev->flags)) {
7464 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007465 err = -ENODEV;
7466 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007467 }
7468
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007470 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007471 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007473 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007475 if (err)
7476 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477 return err;
7478}
7479
Al Virodb2a1442013-05-05 21:52:57 -04007480static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007482 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007484 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007485 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007486 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007488
7489static int md_media_changed(struct gendisk *disk)
7490{
NeilBrownfd01b882011-10-11 16:47:53 +11007491 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007492
7493 return mddev->changed;
7494}
7495
7496static int md_revalidate(struct gendisk *disk)
7497{
NeilBrownfd01b882011-10-11 16:47:53 +11007498 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007499
7500 mddev->changed = 0;
7501 return 0;
7502}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007503static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504{
7505 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007506 .open = md_open,
7507 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007508 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007509#ifdef CONFIG_COMPAT
7510 .compat_ioctl = md_compat_ioctl,
7511#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007512 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007513 .media_changed = md_media_changed,
7514 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515};
7516
NeilBrownf72ffdd2014-09-30 14:23:59 +10007517static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518{
NeilBrown2b8bf342011-10-11 16:48:23 +11007519 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521 /*
7522 * md_thread is a 'system-thread', it's priority should be very
7523 * high. We avoid resource deadlocks individually in each
7524 * raid personality. (RAID5 does preallocation) We also use RR and
7525 * the very same RT priority as kswapd, thus we will never get
7526 * into a priority inversion deadlock.
7527 *
7528 * we definitely have to have equal or higher priority than
7529 * bdflush, otherwise bdflush will deadlock if there are too
7530 * many dirty RAID5 blocks.
7531 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532
NeilBrown6985c432005-10-19 21:23:47 -07007533 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007534 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535
NeilBrown93588e22005-11-15 00:09:12 -08007536 /* We need to wait INTERRUPTIBLE so that
7537 * we don't add to the load-average.
7538 * That means we need to be sure no signals are
7539 * pending
7540 */
7541 if (signal_pending(current))
7542 flush_signals(current);
7543
7544 wait_event_interruptible_timeout
7545 (thread->wqueue,
7546 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007547 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007548 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549
NeilBrown6c987912011-01-14 09:13:53 +11007550 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007551 if (kthread_should_park())
7552 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007553 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007554 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007556
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557 return 0;
7558}
7559
NeilBrown2b8bf342011-10-11 16:48:23 +11007560void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561{
7562 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007563 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Guoqing Jiangd1d90142017-10-09 10:32:48 +08007564 set_bit(THREAD_WAKEUP, &thread->flags);
7565 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566 }
7567}
NeilBrown6c144d32014-09-30 16:15:38 +10007568EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569
Shaohua Li4ed87312012-10-11 13:34:00 +11007570struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7571 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572{
NeilBrown2b8bf342011-10-11 16:48:23 +11007573 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574
NeilBrown2b8bf342011-10-11 16:48:23 +11007575 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576 if (!thread)
7577 return NULL;
7578
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579 init_waitqueue_head(&thread->wqueue);
7580
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 thread->run = run;
7582 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007583 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007584 thread->tsk = kthread_run(md_thread, thread,
7585 "%s_%s",
7586 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007587 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007588 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589 kfree(thread);
7590 return NULL;
7591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592 return thread;
7593}
NeilBrown6c144d32014-09-30 16:15:38 +10007594EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595
NeilBrown2b8bf342011-10-11 16:48:23 +11007596void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007597{
NeilBrown2b8bf342011-10-11 16:48:23 +11007598 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007599 if (!thread)
7600 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007601 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007602 /* Locking ensures that mddev_unlock does not wake_up a
7603 * non-existent thread
7604 */
7605 spin_lock(&pers_lock);
7606 *threadp = NULL;
7607 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007608
7609 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 kfree(thread);
7611}
NeilBrown6c144d32014-09-30 16:15:38 +10007612EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613
NeilBrownfd01b882011-10-11 16:47:53 +11007614void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615{
NeilBrownb2d444d2005-11-08 21:39:31 -08007616 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007618
NeilBrownde393cd2011-07-28 11:31:48 +10007619 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620 return;
7621 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007622 if (mddev->degraded)
7623 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007624 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7626 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7627 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007628 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007629 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007630 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631}
NeilBrown6c144d32014-09-30 16:15:38 +10007632EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633
7634/* seq_file implementation /proc/mdstat */
7635
7636static void status_unused(struct seq_file *seq)
7637{
7638 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007639 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640
7641 seq_printf(seq, "unused devices: ");
7642
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007643 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644 char b[BDEVNAME_SIZE];
7645 i++;
7646 seq_printf(seq, "%s ",
7647 bdevname(rdev->bdev,b));
7648 }
7649 if (!i)
7650 seq_printf(seq, "<none>");
7651
7652 seq_printf(seq, "\n");
7653}
7654
NeilBrownf7851be2015-07-02 17:12:58 +10007655static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656{
NeilBrowndd71cf62009-05-07 12:49:35 +10007657 sector_t max_sectors, resync, res;
7658 unsigned long dt, db;
7659 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007660 int scale;
7661 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007662
NeilBrownc804cde2012-05-21 09:28:33 +10007663 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7664 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007665 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007667 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668
NeilBrownf7851be2015-07-02 17:12:58 +10007669 resync = mddev->curr_resync;
7670 if (resync <= 3) {
7671 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7672 /* Still cleaning up */
7673 resync = max_sectors;
Nate Daileyd2e2ec82017-11-30 11:33:30 -05007674 } else if (resync > max_sectors)
7675 resync = max_sectors;
7676 else
NeilBrownf7851be2015-07-02 17:12:58 +10007677 resync -= atomic_read(&mddev->recovery_active);
7678
7679 if (resync == 0) {
Guoqing Jiang0357ba22018-07-02 16:26:25 +08007680 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
7681 struct md_rdev *rdev;
7682
7683 rdev_for_each(rdev, mddev)
7684 if (rdev->raid_disk >= 0 &&
7685 !test_bit(Faulty, &rdev->flags) &&
7686 rdev->recovery_offset != MaxSector &&
7687 rdev->recovery_offset) {
7688 seq_printf(seq, "\trecover=REMOTE");
7689 return 1;
7690 }
7691 if (mddev->reshape_position != MaxSector)
7692 seq_printf(seq, "\treshape=REMOTE");
7693 else
7694 seq_printf(seq, "\tresync=REMOTE");
7695 return 1;
7696 }
NeilBrownf7851be2015-07-02 17:12:58 +10007697 if (mddev->recovery_cp < MaxSector) {
7698 seq_printf(seq, "\tresync=PENDING");
7699 return 1;
7700 }
7701 return 0;
7702 }
7703 if (resync < 3) {
7704 seq_printf(seq, "\tresync=DELAYED");
7705 return 1;
7706 }
7707
NeilBrown403df472014-09-30 15:52:29 +10007708 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007709 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007710 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007711 * u32, as those are the requirements for sector_div.
7712 * Thus 'scale' must be at least 10
7713 */
7714 scale = 10;
7715 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007716 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007717 scale++;
7718 }
7719 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007720 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007721
7722 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723 {
NeilBrown4588b422006-03-27 01:18:04 -08007724 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725 seq_printf(seq, "[");
7726 for (i = 0; i < x; i++)
7727 seq_printf(seq, "=");
7728 seq_printf(seq, ">");
7729 for (i = 0; i < y; i++)
7730 seq_printf(seq, ".");
7731 seq_printf(seq, "] ");
7732 }
NeilBrown4588b422006-03-27 01:18:04 -08007733 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007734 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7735 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007736 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7737 "check" :
7738 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7739 "resync" : "recovery"))),
7740 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007741 (unsigned long long) resync/2,
7742 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743
7744 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745 * dt: time from mark until now
7746 * db: blocks written from mark until now
7747 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007748 *
7749 * rt is a sector_t, so could be 32bit or 64bit.
7750 * So we divide before multiply in case it is 32bit and close
7751 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007752 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007753 * near the end of resync when the number of remaining sectors
7754 * is close to 'db'.
7755 * We then divide rt by 32 after multiplying by db to compensate.
7756 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757 */
7758 dt = ((jiffies - mddev->resync_mark) / HZ);
7759 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007760 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7761 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007762
NeilBrowndd71cf62009-05-07 12:49:35 +10007763 rt = max_sectors - resync; /* number of remaining sectors */
7764 sector_div(rt, db/32+1);
7765 rt *= dt;
7766 rt >>= 5;
7767
7768 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7769 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770
NeilBrownff4e8d92006-07-10 04:44:16 -07007771 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007772 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007773}
7774
7775static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7776{
7777 struct list_head *tmp;
7778 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007779 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780
7781 if (l >= 0x10000)
7782 return NULL;
7783 if (!l--)
7784 /* header */
7785 return (void*)1;
7786
7787 spin_lock(&all_mddevs_lock);
7788 list_for_each(tmp,&all_mddevs)
7789 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007790 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791 mddev_get(mddev);
7792 spin_unlock(&all_mddevs_lock);
7793 return mddev;
7794 }
7795 spin_unlock(&all_mddevs_lock);
7796 if (!l--)
7797 return (void*)2;/* tail */
7798 return NULL;
7799}
7800
7801static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7802{
7803 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007804 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007805
Linus Torvalds1da177e2005-04-16 15:20:36 -07007806 ++*pos;
7807 if (v == (void*)2)
7808 return NULL;
7809
7810 spin_lock(&all_mddevs_lock);
7811 if (v == (void*)1)
7812 tmp = all_mddevs.next;
7813 else
7814 tmp = mddev->all_mddevs.next;
7815 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007816 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007817 else {
7818 next_mddev = (void*)2;
7819 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821 spin_unlock(&all_mddevs_lock);
7822
7823 if (v != (void*)1)
7824 mddev_put(mddev);
7825 return next_mddev;
7826
7827}
7828
7829static void md_seq_stop(struct seq_file *seq, void *v)
7830{
NeilBrownfd01b882011-10-11 16:47:53 +11007831 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832
7833 if (mddev && v != (void*)1 && v != (void*)2)
7834 mddev_put(mddev);
7835}
7836
7837static int md_seq_show(struct seq_file *seq, void *v)
7838{
NeilBrownfd01b882011-10-11 16:47:53 +11007839 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007840 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007841 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007842
7843 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007844 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845 seq_printf(seq, "Personalities : ");
7846 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007847 list_for_each_entry(pers, &pers_list, list)
7848 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007849
7850 spin_unlock(&pers_lock);
7851 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007852 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853 return 0;
7854 }
7855 if (v == (void*)2) {
7856 status_unused(seq);
7857 return 0;
7858 }
7859
NeilBrown36d091f2014-12-15 12:56:58 +11007860 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7862 seq_printf(seq, "%s : %sactive", mdname(mddev),
7863 mddev->pers ? "" : "in");
7864 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007865 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007866 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007867 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007868 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869 seq_printf(seq, " %s", mddev->pers->name);
7870 }
7871
Andre Nolldd8ac332009-03-31 14:33:13 +11007872 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007873 rcu_read_lock();
7874 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875 char b[BDEVNAME_SIZE];
7876 seq_printf(seq, " %s[%d]",
7877 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007878 if (test_bit(WriteMostly, &rdev->flags))
7879 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007880 if (test_bit(Journal, &rdev->flags))
7881 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007882 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883 seq_printf(seq, "(F)");
7884 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007885 }
7886 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007887 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007888 if (test_bit(Replacement, &rdev->flags))
7889 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007890 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007892 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893
7894 if (!list_empty(&mddev->disks)) {
7895 if (mddev->pers)
7896 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007897 (unsigned long long)
7898 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899 else
7900 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007901 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007903 if (mddev->persistent) {
7904 if (mddev->major_version != 0 ||
7905 mddev->minor_version != 90) {
7906 seq_printf(seq," super %d.%d",
7907 mddev->major_version,
7908 mddev->minor_version);
7909 }
NeilBrowne6910632008-02-06 01:39:51 -08007910 } else if (mddev->external)
7911 seq_printf(seq, " super external:%s",
7912 mddev->metadata_type);
7913 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007914 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915
7916 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007917 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007918 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007919 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007920 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007921 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007922 }
NeilBrown32a76272005-06-21 17:17:14 -07007923 } else
7924 seq_printf(seq, "\n ");
7925
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007926 md_bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927
7928 seq_printf(seq, "\n");
7929 }
NeilBrown36d091f2014-12-15 12:56:58 +11007930 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007931
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932 return 0;
7933}
7934
Jan Engelhardt110518b2009-05-07 12:49:37 +10007935static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936 .start = md_seq_start,
7937 .next = md_seq_next,
7938 .stop = md_seq_stop,
7939 .show = md_seq_show,
7940};
7941
7942static int md_seq_open(struct inode *inode, struct file *file)
7943{
Kay Sieversf1514632011-07-12 20:48:39 +02007944 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945 int error;
7946
7947 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007948 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007949 return error;
7950
7951 seq = file->private_data;
7952 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953 return error;
7954}
7955
NeilBrowne2f23b62014-04-09 14:33:51 +10007956static int md_unloading;
Al Viroafc9a422017-07-03 06:39:46 -04007957static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
NeilBrownd7603b72006-01-06 00:20:30 -08007958{
Kay Sieversf1514632011-07-12 20:48:39 +02007959 struct seq_file *seq = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -04007960 __poll_t mask;
NeilBrownd7603b72006-01-06 00:20:30 -08007961
NeilBrowne2f23b62014-04-09 14:33:51 +10007962 if (md_unloading)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007963 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007964 poll_wait(filp, &md_event_waiters, wait);
7965
7966 /* always allow read */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007967 mask = EPOLLIN | EPOLLRDNORM;
NeilBrownd7603b72006-01-06 00:20:30 -08007968
Kay Sieversf1514632011-07-12 20:48:39 +02007969 if (seq->poll_event != atomic_read(&md_event_count))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007970 mask |= EPOLLERR | EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007971 return mask;
7972}
7973
Arjan van de Venfa027c22007-02-12 00:55:33 -08007974static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007975 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976 .open = md_seq_open,
7977 .read = seq_read,
7978 .llseek = seq_lseek,
Cihangir Akturk26e13042017-07-29 19:52:45 +03007979 .release = seq_release,
NeilBrownd7603b72006-01-06 00:20:30 -08007980 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007981};
7982
NeilBrown84fc4b52011-10-11 16:49:58 +11007983int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007984{
NeilBrown9d487392016-11-02 14:16:49 +11007985 pr_debug("md: %s personality registered for level %d\n",
7986 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007987 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007988 list_add_tail(&p->list, &pers_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(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993
NeilBrown84fc4b52011-10-11 16:49:58 +11007994int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007995{
NeilBrown9d487392016-11-02 14:16:49 +11007996 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007998 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007999 spin_unlock(&pers_lock);
8000 return 0;
8001}
NeilBrown6c144d32014-09-30 16:15:38 +10008002EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003
NeilBrown6022e752015-08-13 12:32:55 +10008004int register_md_cluster_operations(struct md_cluster_operations *ops,
8005 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008006{
NeilBrown6022e752015-08-13 12:32:55 +10008007 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008008 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008009 if (md_cluster_ops != NULL)
8010 ret = -EALREADY;
8011 else {
8012 md_cluster_ops = ops;
8013 md_cluster_mod = module;
8014 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008015 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008016 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008017}
8018EXPORT_SYMBOL(register_md_cluster_operations);
8019
8020int unregister_md_cluster_operations(void)
8021{
8022 spin_lock(&pers_lock);
8023 md_cluster_ops = NULL;
8024 spin_unlock(&pers_lock);
8025 return 0;
8026}
8027EXPORT_SYMBOL(unregister_md_cluster_operations);
8028
8029int md_setup_cluster(struct mddev *mddev, int nodes)
8030{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008031 if (!md_cluster_ops)
8032 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008033 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008034 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008035 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11008036 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008037 spin_unlock(&pers_lock);
8038 return -ENOENT;
8039 }
8040 spin_unlock(&pers_lock);
8041
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05008042 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008043}
8044
8045void md_cluster_stop(struct mddev *mddev)
8046{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05008047 if (!md_cluster_ops)
8048 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008049 md_cluster_ops->leave(mddev);
8050 module_put(md_cluster_mod);
8051}
8052
NeilBrownfd01b882011-10-11 16:47:53 +11008053static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008054{
NeilBrownf72ffdd2014-09-30 14:23:59 +10008055 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11008057 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008058
8059 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10008060 rcu_read_lock();
8061 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008062 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Michael Callahan59767fb2018-07-18 04:47:37 -07008063 curr_events = (int)part_stat_read_accum(&disk->part0, sectors) -
NeilBrowneea1bf32009-03-31 14:27:02 +11008064 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07008065 /* sync IO will cause sync_io to increase before the disk_stats
8066 * as sync_io is counted when a request starts, and
8067 * disk_stats is counted when it completes.
8068 * So resync activity will cause curr_events to be smaller than
8069 * when there was no such activity.
8070 * non-sync IO will cause disk_stat to increase without
8071 * increasing sync_io so curr_events will (eventually)
8072 * be larger than it was before. Once it becomes
8073 * substantially larger, the test below will cause
8074 * the array to appear non-idle, and resync will slow
8075 * down.
8076 * If there is a lot of outstanding resync activity when
8077 * we set last_event to curr_events, then all that activity
8078 * completing might cause the array to appear non-idle
8079 * and resync will be slowed down even though there might
8080 * not have been non-resync activity. This will only
8081 * happen once though. 'last_events' will soon reflect
8082 * the state where there is little or no outstanding
8083 * resync requests, and further resync activity will
8084 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08008085 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008086 */
NeilBrowneea1bf32009-03-31 14:27:02 +11008087 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008088 rdev->last_events = curr_events;
8089 idle = 0;
8090 }
8091 }
NeilBrown4b809912008-07-21 17:05:25 +10008092 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008093 return idle;
8094}
8095
NeilBrownfd01b882011-10-11 16:47:53 +11008096void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008097{
8098 /* another "blocks" (512byte) blocks have been synced */
8099 atomic_sub(blocks, &mddev->recovery_active);
8100 wake_up(&mddev->recovery_wait);
8101 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07008102 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08008103 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008104 md_wakeup_thread(mddev->thread);
8105 // stop recovery, signal do_sync ....
8106 }
8107}
NeilBrown6c144d32014-09-30 16:15:38 +10008108EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008109
NeilBrown06d91a52005-06-21 17:17:12 -07008110/* md_write_start(mddev, bi)
8111 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07008112 * in superblock) before writing, schedule a superblock update
8113 * and wait for it to complete.
NeilBrowncc27b0c2017-06-05 16:49:39 +10008114 * A return value of 'false' means that the write wasn't recorded
8115 * and cannot proceed as the array is being suspend.
NeilBrown06d91a52005-06-21 17:17:12 -07008116 */
NeilBrowncc27b0c2017-06-05 16:49:39 +10008117bool md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118{
Neil Brown0fd62b82008-06-28 08:31:36 +10008119 int did_change = 0;
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008120
NeilBrown06d91a52005-06-21 17:17:12 -07008121 if (bio_data_dir(bi) != WRITE)
NeilBrowncc27b0c2017-06-05 16:49:39 +10008122 return true;
NeilBrown06d91a52005-06-21 17:17:12 -07008123
NeilBrownf91de922005-11-08 21:39:36 -08008124 BUG_ON(mddev->ro == 1);
8125 if (mddev->ro == 2) {
8126 /* need to switch to read/write */
8127 mddev->ro = 0;
8128 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8129 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08008130 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008131 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08008132 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008133 rcu_read_lock();
8134 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11008135 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07008136 if (mddev->safemode == 1)
8137 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11008138 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
NeilBrown81fe48e2017-08-08 16:56:36 +10008139 if (mddev->in_sync || mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11008140 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07008141 if (mddev->in_sync) {
8142 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008143 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8144 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07008145 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008146 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07008147 }
NeilBrown85572d72014-12-15 12:56:56 +11008148 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07008149 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008150 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10008151 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008152 sysfs_notify_dirent_safe(mddev->sysfs_state);
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008153 if (!mddev->has_superblocks)
8154 return true;
NeilBrown09a44cc2008-05-23 13:04:36 -07008155 wait_event(mddev->sb_wait,
NeilBrownd47c8ad2017-10-05 16:23:16 +11008156 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8157 mddev->suspended);
NeilBrowncc27b0c2017-06-05 16:49:39 +10008158 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8159 percpu_ref_put(&mddev->writes_pending);
8160 return false;
8161 }
8162 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008163}
NeilBrown6c144d32014-09-30 16:15:38 +10008164EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165
NeilBrown49728052017-03-15 14:05:12 +11008166/* md_write_inc can only be called when md_write_start() has
8167 * already been called at least once of the current request.
8168 * It increments the counter and is useful when a single request
8169 * is split into several parts. Each part causes an increment and
8170 * so needs a matching md_write_end().
8171 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8172 * a spinlocked region.
8173 */
8174void md_write_inc(struct mddev *mddev, struct bio *bi)
8175{
8176 if (bio_data_dir(bi) != WRITE)
8177 return;
8178 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008179 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008180}
8181EXPORT_SYMBOL(md_write_inc);
8182
NeilBrownfd01b882011-10-11 16:47:53 +11008183void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008184{
NeilBrown4ad23a972017-03-15 14:05:14 +11008185 percpu_ref_put(&mddev->writes_pending);
8186
8187 if (mddev->safemode == 2)
8188 md_wakeup_thread(mddev->thread);
8189 else if (mddev->safemode_delay)
8190 /* The roundup() ensures this only performs locking once
8191 * every ->safemode_delay jiffies
8192 */
8193 mod_timer(&mddev->safemode_timer,
8194 roundup(jiffies, mddev->safemode_delay) +
8195 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196}
NeilBrown4ad23a972017-03-15 14:05:14 +11008197
NeilBrown6c144d32014-09-30 16:15:38 +10008198EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008199
NeilBrown2a2275d2007-01-26 00:57:11 -08008200/* md_allow_write(mddev)
8201 * Calling this ensures that the array is marked 'active' so that writes
8202 * may proceed without blocking. It is important to call this before
8203 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8204 * Must be called with mddev_lock held.
8205 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008206void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008207{
8208 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008209 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008210 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008211 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008212 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008213 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008214
NeilBrown85572d72014-12-15 12:56:56 +11008215 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008216 if (mddev->in_sync) {
8217 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008218 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8219 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008220 if (mddev->safemode_delay &&
8221 mddev->safemode == 0)
8222 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008223 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008224 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008225 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008226 /* wait for the dirty state to be recorded in the metadata */
8227 wait_event(mddev->sb_wait,
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008228 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008229 } else
NeilBrown85572d72014-12-15 12:56:56 +11008230 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008231}
8232EXPORT_SYMBOL_GPL(md_allow_write);
8233
Linus Torvalds1da177e2005-04-16 15:20:36 -07008234#define SYNC_MARKS 10
8235#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008236#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008237void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238{
Shaohua Li4ed87312012-10-11 13:34:00 +11008239 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008240 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008241 unsigned int currspeed = 0,
8242 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008243 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008245 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008246 sector_t mark_cnt[SYNC_MARKS];
8247 int last_mark,m;
8248 struct list_head *tmp;
8249 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008250 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008251 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008252 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008253 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008254 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008255
8256 /* just incase thread restarts... */
Song Liud5d885f2017-11-19 22:17:01 -08008257 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8258 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259 return;
NeilBrown3991b312014-05-28 13:39:23 +10008260 if (mddev->ro) {/* never try to sync a read-only array */
8261 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008262 return;
NeilBrown3991b312014-05-28 13:39:23 +10008263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008265 if (mddev_is_clustered(mddev)) {
8266 ret = md_cluster_ops->resync_start(mddev);
8267 if (ret)
8268 goto skip;
8269
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008270 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008271 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8272 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8273 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8274 && ((unsigned long long)mddev->curr_resync_completed
8275 < (unsigned long long)mddev->resync_max_sectors))
8276 goto skip;
8277 }
8278
NeilBrown61df9d92006-10-03 01:15:57 -07008279 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008280 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008281 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008282 action = "check";
8283 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008284 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008285 action = "repair";
8286 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008287 desc = "resync";
8288 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8289 desc = "reshape";
8290 else
8291 desc = "recovery";
8292
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008293 mddev->last_sync_action = action ?: desc;
8294
Linus Torvalds1da177e2005-04-16 15:20:36 -07008295 /* we overload curr_resync somewhat here.
8296 * 0 == not engaged in resync at all
8297 * 2 == checking that there is no conflict with another sync
8298 * 1 == like 2, but have yielded to allow conflicting resync to
8299 * commense
8300 * other == active in resync - this many blocks
8301 *
8302 * Before starting a resync we must have set curr_resync to
8303 * 2, and then checked that every "conflicting" array has curr_resync
8304 * less than ours. When we find one that is the same or higher
8305 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8306 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8307 * This will mean we have to start checking from the beginning again.
8308 *
8309 */
8310
8311 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008312 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313 mddev->curr_resync = 2;
8314
8315 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008316 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008318 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008319 if (mddev2 == mddev)
8320 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008321 if (!mddev->parallel_resync
8322 && mddev2->curr_resync
8323 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324 DEFINE_WAIT(wq);
8325 if (mddev < mddev2 && mddev->curr_resync == 2) {
8326 /* arbitrarily yield */
8327 mddev->curr_resync = 1;
8328 wake_up(&resync_wait);
8329 }
8330 if (mddev > mddev2 && mddev->curr_resync == 1)
8331 /* no need to wait here, we can wait the next
8332 * time 'round when curr_resync == 2
8333 */
8334 continue;
NeilBrown97441972008-09-19 11:49:54 +10008335 /* We need to wait 'interruptible' so as not to
8336 * contribute to the load average, and not to
8337 * be caught by 'softlockup'
8338 */
8339 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008340 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008341 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008342 if (mddev2_minor != mddev2->md_minor) {
8343 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008344 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8345 desc, mdname(mddev),
8346 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008348 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008349 if (signal_pending(current))
8350 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008351 schedule();
8352 finish_wait(&resync_wait, &wq);
8353 goto try_again;
8354 }
8355 finish_wait(&resync_wait, &wq);
8356 }
8357 }
8358 } while (mddev->curr_resync < 2);
8359
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008360 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008361 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008363 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008364 */
8365 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008366 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008367 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008368 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8369 j = mddev->resync_min;
8370 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008371 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008372
NeilBrownccfcc3c2006-03-27 01:18:09 -08008373 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10008374 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008375 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008376 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008377 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008378 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008379 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008380 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008381 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008382 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008383 !test_bit(Faulty, &rdev->flags) &&
8384 !test_bit(In_sync, &rdev->flags) &&
8385 rdev->recovery_offset < j)
8386 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008387 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008388
8389 /* If there is a bitmap, we need to make sure all
8390 * writes that started before we added a spare
8391 * complete before we start doing a recovery.
8392 * Otherwise the write might complete and (via
8393 * bitmap_endwrite) set a bit in the bitmap after the
8394 * recovery has checked that bit and skipped that
8395 * region.
8396 */
8397 if (mddev->bitmap) {
8398 mddev->pers->quiesce(mddev, 1);
8399 mddev->pers->quiesce(mddev, 0);
8400 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402
NeilBrown9d487392016-11-02 14:16:49 +11008403 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8404 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8405 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8406 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407
NeilBrowneea1bf32009-03-31 14:27:02 +11008408 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008409
NeilBrown57afd892005-06-21 17:17:13 -07008410 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008411 for (m = 0; m < SYNC_MARKS; m++) {
8412 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008413 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414 }
8415 last_mark = 0;
8416 mddev->resync_mark = mark[last_mark];
8417 mddev->resync_mark_cnt = mark_cnt[last_mark];
8418
8419 /*
8420 * Tune reconstruction:
8421 */
8422 window = 32*(PAGE_SIZE/512);
NeilBrown9d487392016-11-02 14:16:49 +11008423 pr_debug("md: using %dk window, over a total of %lluk.\n",
8424 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008425
8426 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008427 last_check = 0;
8428
8429 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008430 pr_debug("md: resuming %s of %s from checkpoint.\n",
8431 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008432 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008433 } else
8434 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008435 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008436 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8437 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008438 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439
majianpeng7c2c57c2012-07-03 12:12:26 +10008440 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008441 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008442 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443
NeilBrown57afd892005-06-21 17:17:13 -07008444 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008445
NeilBrown7a91ee12009-05-26 12:57:21 +10008446 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8447 ((mddev->curr_resync > mddev->curr_resync_completed &&
8448 (mddev->curr_resync - mddev->curr_resync_completed)
8449 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008450 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008451 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008452 >= mddev->resync_max - mddev->curr_resync_completed ||
8453 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008454 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008455 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008456 wait_event(mddev->recovery_wait,
8457 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008458 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008459 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8460 j > mddev->recovery_cp)
8461 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008462 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008463 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008464 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008465 }
NeilBrownacb180b2009-04-14 16:28:34 +10008466
NeilBrownc91abf52013-11-19 12:02:01 +11008467 while (j >= mddev->resync_max &&
8468 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008469 /* As this condition is controlled by user-space,
8470 * we can block indefinitely, so use '_interruptible'
8471 * to avoid triggering warnings.
8472 */
8473 flush_signals(current); /* just in case */
8474 wait_event_interruptible(mddev->recovery_wait,
8475 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008476 || test_bit(MD_RECOVERY_INTR,
8477 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008478 }
NeilBrownacb180b2009-04-14 16:28:34 +10008479
NeilBrownc91abf52013-11-19 12:02:01 +11008480 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8481 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008482
NeilBrown09314792015-02-19 16:04:40 +11008483 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008484 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008485 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008486 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008487 }
NeilBrown57afd892005-06-21 17:17:13 -07008488
8489 if (!skipped) { /* actual IO requested */
8490 io_sectors += sectors;
8491 atomic_add(sectors, &mddev->recovery_active);
8492 }
8493
NeilBrowne875ece2011-07-28 11:39:24 +10008494 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8495 break;
8496
Linus Torvalds1da177e2005-04-16 15:20:36 -07008497 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008498 if (j > max_sectors)
8499 /* when skipping, extra large numbers can be returned. */
8500 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008501 if (j > 2)
8502 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008503 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008504 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008505 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008506 * visible in /proc/mdstat
8507 */
8508 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008509
8510 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008511 continue;
8512
NeilBrown57afd892005-06-21 17:17:13 -07008513 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514 repeat:
8515 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8516 /* step marks */
8517 int next = (last_mark+1) % SYNC_MARKS;
8518
8519 mddev->resync_mark = mark[next];
8520 mddev->resync_mark_cnt = mark_cnt[next];
8521 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008522 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523 last_mark = next;
8524 }
8525
NeilBrownc91abf52013-11-19 12:02:01 +11008526 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8527 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008528
8529 /*
8530 * this loop exits only if either when we are slower than
8531 * the 'hard' speed limit, or the system was IO-idle for
8532 * a jiffy.
8533 * the system might be non-idle CPU-wise, but we only care
8534 * about not overloading the IO subsystem. (things like an
8535 * e2fsck being done on the RAID array should execute fast)
8536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008537 cond_resched();
8538
Xiao Niac7e50a2014-08-07 09:37:41 -04008539 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8540 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008541 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008542
NeilBrown88202a02006-01-06 00:21:36 -08008543 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008544 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008545 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546 goto repeat;
8547 }
NeilBrownac8fa412015-02-19 16:55:00 +11008548 if (!is_mddev_idle(mddev, 0)) {
8549 /*
8550 * Give other IO more of a chance.
8551 * The faster the devices, the less we wait.
8552 */
8553 wait_event(mddev->recovery_wait,
8554 !atomic_read(&mddev->recovery_active));
8555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556 }
8557 }
NeilBrown9d487392016-11-02 14:16:49 +11008558 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8559 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8560 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008561 /*
8562 * this also signals 'finished resyncing' to md_stop
8563 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008564 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8566
NeilBrown5ed1df22015-07-24 13:27:08 +10008567 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8568 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008569 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008570 mddev->curr_resync_completed = mddev->curr_resync;
8571 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8572 }
NeilBrown09314792015-02-19 16:04:40 +11008573 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008574
NeilBrowndfc70642008-05-23 13:04:39 -07008575 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008576 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008577 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8578 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8579 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008580 pr_debug("md: checkpointing %s of %s.\n",
8581 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008582 if (test_bit(MD_RECOVERY_ERROR,
8583 &mddev->recovery))
8584 mddev->recovery_cp =
8585 mddev->curr_resync_completed;
8586 else
8587 mddev->recovery_cp =
8588 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008589 }
8590 } else
8591 mddev->recovery_cp = MaxSector;
8592 } else {
8593 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8594 mddev->curr_resync = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11008595 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8596 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
8597 rcu_read_lock();
8598 rdev_for_each_rcu(rdev, mddev)
8599 if (rdev->raid_disk >= 0 &&
8600 mddev->delta_disks >= 0 &&
8601 !test_bit(Journal, &rdev->flags) &&
8602 !test_bit(Faulty, &rdev->flags) &&
8603 !test_bit(In_sync, &rdev->flags) &&
8604 rdev->recovery_offset < mddev->curr_resync)
8605 rdev->recovery_offset = mddev->curr_resync;
8606 rcu_read_unlock();
8607 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008609 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008610 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008611 /* set CHANGE_PENDING here since maybe another update is needed,
8612 * so other nodes are informed. It should be harmless for normal
8613 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008614 set_mask_bits(&mddev->sb_flags, 0,
8615 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008616
BingJing Chang88763912018-02-22 13:34:46 +08008617 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8618 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8619 mddev->delta_disks > 0 &&
8620 mddev->pers->finish_reshape &&
8621 mddev->pers->size &&
8622 mddev->queue) {
8623 mddev_lock_nointr(mddev);
8624 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
8625 mddev_unlock(mddev);
8626 set_capacity(mddev->gendisk, mddev->array_sectors);
8627 revalidate_disk(mddev->gendisk);
8628 }
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;
8971
8972 /* resync has finished, collect result */
8973 md_unregister_thread(&mddev->sync_thread);
8974 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8975 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8976 /* success...*/
8977 /* activate any spares */
8978 if (mddev->pers->spare_active(mddev)) {
8979 sysfs_notify(&mddev->kobj, NULL,
8980 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08008981 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008982 }
8983 }
8984 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8985 mddev->pers->finish_reshape)
8986 mddev->pers->finish_reshape(mddev);
8987
8988 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008989 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008990 */
NeilBrownf4667222013-12-09 12:04:56 +11008991 if (!mddev->degraded)
8992 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008993 rdev->saved_raid_disk = -1;
8994
8995 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08008996 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008997 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8998 * clustered raid */
8999 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9000 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009001 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10009002 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009003 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9004 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9005 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9006 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009007 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009008 /* flag recovery needed just to double check */
9009 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9010 sysfs_notify_dirent_safe(mddev->sysfs_action);
9011 md_new_event(mddev);
9012 if (mddev->event_work.func)
9013 queue_work(md_misc_wq, &mddev->event_work);
9014}
NeilBrown6c144d32014-09-30 16:15:38 +10009015EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009016
NeilBrownfd01b882011-10-11 16:47:53 +11009017void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07009018{
NeilBrown00bcb4a2010-06-01 19:37:23 +10009019 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07009020 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10009021 !test_bit(Blocked, &rdev->flags) &&
9022 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07009023 msecs_to_jiffies(5000));
9024 rdev_dec_pending(rdev, mddev);
9025}
9026EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9027
NeilBrownc6563a82012-05-21 09:27:00 +10009028void md_finish_reshape(struct mddev *mddev)
9029{
9030 /* called be personality module when reshape completes. */
9031 struct md_rdev *rdev;
9032
9033 rdev_for_each(rdev, mddev) {
9034 if (rdev->data_offset > rdev->new_data_offset)
9035 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9036 else
9037 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9038 rdev->data_offset = rdev->new_data_offset;
9039 }
9040}
9041EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10009042
Vishal Vermafc974ee2015-12-24 19:20:34 -07009043/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10009044
Vishal Vermafc974ee2015-12-24 19:20:34 -07009045/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11009046int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10009047 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009048{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04009049 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10009050 int rv;
9051 if (is_new)
9052 s += rdev->new_data_offset;
9053 else
9054 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07009055 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9056 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10009057 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009058 if (test_bit(ExternalBbl, &rdev->flags))
9059 sysfs_notify(&rdev->kobj, NULL,
9060 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11009061 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08009062 set_mask_bits(&mddev->sb_flags, 0,
9063 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10009064 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07009065 return 1;
9066 } else
9067 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10009068}
9069EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9070
NeilBrownc6563a82012-05-21 09:27:00 +10009071int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9072 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009073{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009074 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10009075 if (is_new)
9076 s += rdev->new_data_offset;
9077 else
9078 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009079 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9080 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
9081 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
9082 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10009083}
9084EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9085
Adrian Bunk75c96f82005-05-05 16:16:09 -07009086static int md_notify_reboot(struct notifier_block *this,
9087 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009088{
9089 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009090 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009091 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009092
NeilBrownc744a652012-03-19 12:46:37 +11009093 for_each_mddev(mddev, tmp) {
9094 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009095 if (mddev->pers)
9096 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009097 if (mddev->persistent)
9098 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009099 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009100 }
NeilBrownc744a652012-03-19 12:46:37 +11009101 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009102 }
NeilBrownc744a652012-03-19 12:46:37 +11009103 /*
9104 * certain more exotic SCSI devices are known to be
9105 * volatile wrt too early system reboots. While the
9106 * right place to handle this issue is the given
9107 * driver, we do want to have a safe RAID driver ...
9108 */
9109 if (need_delay)
9110 mdelay(1000*1);
9111
Linus Torvalds1da177e2005-04-16 15:20:36 -07009112 return NOTIFY_DONE;
9113}
9114
Adrian Bunk75c96f82005-05-05 16:16:09 -07009115static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009116 .notifier_call = md_notify_reboot,
9117 .next = NULL,
9118 .priority = INT_MAX, /* before any real devices */
9119};
9120
9121static void md_geninit(void)
9122{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009123 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009124
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07009125 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009126}
9127
Adrian Bunk75c96f82005-05-05 16:16:09 -07009128static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009129{
Tejun Heoe804ac72010-10-15 15:36:08 +02009130 int ret = -ENOMEM;
9131
Tejun Heoada609e2011-01-25 14:35:54 +01009132 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009133 if (!md_wq)
9134 goto err_wq;
9135
9136 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9137 if (!md_misc_wq)
9138 goto err_misc_wq;
9139
9140 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9141 goto err_md;
9142
9143 if ((ret = register_blkdev(0, "mdp")) < 0)
9144 goto err_mdp;
9145 mdp_major = ret;
9146
NeilBrownaf5628f2014-07-31 13:54:54 +10009147 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009148 md_probe, NULL, NULL);
9149 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009150 md_probe, NULL, NULL);
9151
Linus Torvalds1da177e2005-04-16 15:20:36 -07009152 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009153 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009154
9155 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009156 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009157
Tejun Heoe804ac72010-10-15 15:36:08 +02009158err_mdp:
9159 unregister_blkdev(MD_MAJOR, "md");
9160err_md:
9161 destroy_workqueue(md_misc_wq);
9162err_misc_wq:
9163 destroy_workqueue(md_wq);
9164err_wq:
9165 return ret;
9166}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009167
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009168static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009169{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009170 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9171 struct md_rdev *rdev2;
9172 int role, ret;
9173 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009174
Guoqing Jiang818da592017-03-01 16:42:40 +08009175 /*
9176 * If size is changed in another node then we need to
9177 * do resize as well.
9178 */
9179 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9180 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9181 if (ret)
9182 pr_info("md-cluster: resize failed\n");
9183 else
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009184 md_bitmap_update_sb(mddev->bitmap);
Guoqing Jiang818da592017-03-01 16:42:40 +08009185 }
9186
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009187 /* Check for change of roles in the active devices */
9188 rdev_for_each(rdev2, mddev) {
9189 if (test_bit(Faulty, &rdev2->flags))
9190 continue;
9191
9192 /* Check if the roles changed */
9193 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009194
9195 if (test_bit(Candidate, &rdev2->flags)) {
9196 if (role == 0xfffe) {
9197 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9198 md_kick_rdev_from_array(rdev2);
9199 continue;
9200 }
9201 else
9202 clear_bit(Candidate, &rdev2->flags);
9203 }
9204
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009205 if (role != rdev2->raid_disk) {
9206 /* got activated */
9207 if (rdev2->raid_disk == -1 && role != 0xffff) {
9208 rdev2->saved_raid_disk = role;
9209 ret = remove_and_add_spares(mddev, rdev2);
9210 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009211 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009212 /* wakeup mddev->thread here, so array could
9213 * perform resync with the new activated disk */
9214 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9215 md_wakeup_thread(mddev->thread);
9216
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009217 }
9218 /* device faulty
9219 * We just want to do the minimum to mark the disk
9220 * as faulty. The recovery is performed by the
9221 * one who initiated the error.
9222 */
9223 if ((role == 0xfffe) || (role == 0xfffd)) {
9224 md_error(mddev, rdev2);
9225 clear_bit(Blocked, &rdev2->flags);
9226 }
9227 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009228 }
9229
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009230 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9231 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009232
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009233 /*
9234 * Since mddev->delta_disks has already updated in update_raid_disks,
9235 * so it is time to check reshape.
9236 */
9237 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9238 (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9239 /*
9240 * reshape is happening in the remote node, we need to
9241 * update reshape_position and call start_reshape.
9242 */
9243 mddev->reshape_position = sb->reshape_position;
9244 if (mddev->pers->update_reshape_pos)
9245 mddev->pers->update_reshape_pos(mddev);
9246 if (mddev->pers->start_reshape)
9247 mddev->pers->start_reshape(mddev);
9248 } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9249 mddev->reshape_position != MaxSector &&
9250 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9251 /* reshape is just done in another node. */
9252 mddev->reshape_position = MaxSector;
9253 if (mddev->pers->update_reshape_pos)
9254 mddev->pers->update_reshape_pos(mddev);
9255 }
9256
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009257 /* Finally set the event to be up to date */
9258 mddev->events = le64_to_cpu(sb->events);
9259}
9260
9261static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9262{
9263 int err;
9264 struct page *swapout = rdev->sb_page;
9265 struct mdp_superblock_1 *sb;
9266
9267 /* Store the sb page of the rdev in the swapout temporary
9268 * variable in case we err in the future
9269 */
9270 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009271 err = alloc_disk_sb(rdev);
9272 if (err == 0) {
9273 ClearPageUptodate(rdev->sb_page);
9274 rdev->sb_loaded = 0;
9275 err = super_types[mddev->major_version].
9276 load_super(rdev, NULL, mddev->minor_version);
9277 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009278 if (err < 0) {
9279 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9280 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009281 if (rdev->sb_page)
9282 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009283 rdev->sb_page = swapout;
9284 rdev->sb_loaded = 1;
9285 return err;
9286 }
9287
9288 sb = page_address(rdev->sb_page);
9289 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9290 * is not set
9291 */
9292
9293 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9294 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9295
9296 /* The other node finished recovery, call spare_active to set
9297 * device In_sync and mddev->degraded
9298 */
9299 if (rdev->recovery_offset == MaxSector &&
9300 !test_bit(In_sync, &rdev->flags) &&
9301 mddev->pers->spare_active(mddev))
9302 sysfs_notify(&mddev->kobj, NULL, "degraded");
9303
9304 put_page(swapout);
9305 return 0;
9306}
9307
9308void md_reload_sb(struct mddev *mddev, int nr)
9309{
9310 struct md_rdev *rdev;
9311 int err;
9312
9313 /* Find the rdev */
9314 rdev_for_each_rcu(rdev, mddev) {
9315 if (rdev->desc_nr == nr)
9316 break;
9317 }
9318
9319 if (!rdev || rdev->desc_nr != nr) {
9320 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9321 return;
9322 }
9323
9324 err = read_rdev(mddev, rdev);
9325 if (err < 0)
9326 return;
9327
9328 check_sb_changes(mddev, rdev);
9329
9330 /* Read all rdev's to update recovery_offset */
Guoqing Jiang0ea99242018-04-09 17:01:21 +08009331 rdev_for_each_rcu(rdev, mddev) {
9332 if (!test_bit(Faulty, &rdev->flags))
9333 read_rdev(mddev, rdev);
9334 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009335}
9336EXPORT_SYMBOL(md_reload_sb);
9337
Linus Torvalds1da177e2005-04-16 15:20:36 -07009338#ifndef MODULE
9339
9340/*
9341 * Searches all registered partitions for autorun RAID arrays
9342 * at boot time.
9343 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009344
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009345static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009346static LIST_HEAD(all_detected_devices);
9347struct detected_devices_node {
9348 struct list_head list;
9349 dev_t dev;
9350};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351
9352void md_autodetect_dev(dev_t dev)
9353{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009354 struct detected_devices_node *node_detected_dev;
9355
9356 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9357 if (node_detected_dev) {
9358 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009359 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009360 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009361 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009363}
9364
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365static void autostart_arrays(int part)
9366{
NeilBrown3cb03002011-10-11 16:45:26 +11009367 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009368 struct detected_devices_node *node_detected_dev;
9369 dev_t dev;
9370 int i_scanned, i_passed;
9371
9372 i_scanned = 0;
9373 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009374
NeilBrown9d487392016-11-02 14:16:49 +11009375 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009376
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009377 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009378 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9379 i_scanned++;
9380 node_detected_dev = list_entry(all_detected_devices.next,
9381 struct detected_devices_node, list);
9382 list_del(&node_detected_dev->list);
9383 dev = node_detected_dev->dev;
9384 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009385 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009386 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009387 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009388 if (IS_ERR(rdev))
9389 continue;
9390
NeilBrown403df472014-09-30 15:52:29 +10009391 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009392 continue;
NeilBrown403df472014-09-30 15:52:29 +10009393
NeilBrownd0fae182008-03-04 14:29:31 -08009394 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009395 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009396 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009397 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009398 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009399
NeilBrown9d487392016-11-02 14:16:49 +11009400 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009401
9402 autorun_devices(part);
9403}
9404
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009405#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009406
9407static __exit void md_exit(void)
9408{
NeilBrownfd01b882011-10-11 16:47:53 +11009409 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009410 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009411 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009412
NeilBrownaf5628f2014-07-31 13:54:54 +10009413 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009414 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009415
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009416 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009417 unregister_blkdev(mdp_major, "mdp");
9418 unregister_reboot_notifier(&md_notifier);
9419 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009420
9421 /* We cannot unload the modules while some process is
9422 * waiting for us in select() or poll() - wake them up
9423 */
9424 md_unloading = 1;
9425 while (waitqueue_active(&md_event_waiters)) {
9426 /* not safe to leave yet */
9427 wake_up(&md_event_waiters);
9428 msleep(delay);
9429 delay += delay;
9430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009431 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009432
NeilBrown29ac4aa2008-02-06 01:39:58 -08009433 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009434 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009435 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009436 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009437 /*
9438 * for_each_mddev() will call mddev_put() at the end of each
9439 * iteration. As the mddev is now fully clear, this will
9440 * schedule the mddev for destruction by a workqueue, and the
9441 * destroy_workqueue() below will wait for that to complete.
9442 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009443 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009444 destroy_workqueue(md_misc_wq);
9445 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009446}
9447
Dan Williams685784a2007-07-09 11:56:42 -07009448subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009449module_exit(md_exit)
9450
Kees Cooke4dca7b2017-10-17 19:04:42 -07009451static int get_ro(char *buffer, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009452{
9453 return sprintf(buffer, "%d", start_readonly);
9454}
Kees Cooke4dca7b2017-10-17 19:04:42 -07009455static int set_ro(const char *val, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009456{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009457 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009458}
9459
NeilBrown80ca3a42006-07-10 04:44:18 -07009460module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9461module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009462module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009463module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009464
Linus Torvalds1da177e2005-04-16 15:20:36 -07009465MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009466MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009467MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009468MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);