blob: 62bf96daa15758814b1c9def6446bad26f458ae1 [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;
Shaohua Lid5955672018-10-01 18:36:36 -07001777 if (role >= mddev->raid_disks) {
1778 rdev->saved_raid_disk = -1;
1779 rdev->raid_disk = -1;
1780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 break;
1782 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001783 if (sb->devflags & WriteMostly1)
1784 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001785 if (sb->devflags & FailFast1)
1786 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001787 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1788 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001789 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001790 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return 0;
1793}
1794
NeilBrownfd01b882011-10-11 16:47:53 +11001795static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001798 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 int max_dev, i;
1800 /* make rdev->sb match mddev and rdev data. */
1801
Namhyung Kim65a06f062011-07-27 11:00:36 +10001802 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 sb->feature_map = 0;
1805 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001806 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 memset(sb->pad3, 0, sizeof(sb->pad3));
1808
1809 sb->utime = cpu_to_le64((__u64)mddev->utime);
1810 sb->events = cpu_to_le64(mddev->events);
1811 if (mddev->in_sync)
1812 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001813 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1814 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 else
1816 sb->resync_offset = cpu_to_le64(0);
1817
NeilBrown1c05b4b2006-10-21 10:24:08 -07001818 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001819
NeilBrownf0ca3402006-02-02 14:28:04 -08001820 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001821 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001822 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001823 sb->level = cpu_to_le32(mddev->level);
1824 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11001825 if (test_bit(FailFast, &rdev->flags))
1826 sb->devflags |= FailFast1;
1827 else
1828 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08001829
NeilBrownaeb9b2112011-08-25 14:43:08 +10001830 if (test_bit(WriteMostly, &rdev->flags))
1831 sb->devflags |= WriteMostly1;
1832 else
1833 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001834 sb->data_offset = cpu_to_le64(rdev->data_offset);
1835 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001836
NeilBrownc3d97142009-12-14 12:49:52 +11001837 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1838 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001839 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001840 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001841
Shaohua Lif2076e72015-10-08 21:54:12 -07001842 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001843 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001844 sb->feature_map |=
1845 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1846 sb->recovery_offset =
1847 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001848 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1849 sb->feature_map |=
1850 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001851 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001852 /* Note: recovery_offset and journal_tail share space */
1853 if (test_bit(Journal, &rdev->flags))
1854 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001855 if (test_bit(Replacement, &rdev->flags))
1856 sb->feature_map |=
1857 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001858
NeilBrownf6705572006-03-27 01:18:11 -08001859 if (mddev->reshape_position != MaxSector) {
1860 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1861 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1862 sb->new_layout = cpu_to_le32(mddev->new_layout);
1863 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1864 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001865 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001866 if (mddev->delta_disks == 0 &&
1867 mddev->reshape_backwards)
1868 sb->feature_map
1869 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001870 if (rdev->new_data_offset != rdev->data_offset) {
1871 sb->feature_map
1872 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1873 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1874 - rdev->data_offset));
1875 }
NeilBrownf6705572006-03-27 01:18:11 -08001876 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001877
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001878 if (mddev_is_clustered(mddev))
1879 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1880
NeilBrown2699b672011-07-28 11:31:47 +10001881 if (rdev->badblocks.count == 0)
1882 /* Nothing to do for bad blocks*/ ;
1883 else if (sb->bblog_offset == 0)
1884 /* Cannot record bad blocks on this device */
1885 md_error(mddev, rdev);
1886 else {
1887 struct badblocks *bb = &rdev->badblocks;
1888 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1889 u64 *p = bb->page;
1890 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1891 if (bb->changed) {
1892 unsigned seq;
1893
1894retry:
1895 seq = read_seqbegin(&bb->lock);
1896
1897 memset(bbp, 0xff, PAGE_SIZE);
1898
1899 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001900 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001901 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1902 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001903 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001904 }
NeilBrownd0962932012-03-19 12:46:41 +11001905 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001906 if (read_seqretry(&bb->lock, seq))
1907 goto retry;
1908
1909 bb->sector = (rdev->sb_start +
1910 (int)le32_to_cpu(sb->bblog_offset));
1911 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001912 }
1913 }
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001916 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (rdev2->desc_nr+1 > max_dev)
1918 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001919
NeilBrown70471da2009-08-03 10:59:57 +10001920 if (max_dev > le32_to_cpu(sb->max_dev)) {
1921 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001922 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001923 rdev->sb_size = max_dev * 2 + 256;
1924 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1925 if (rdev->sb_size & bmask)
1926 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001927 } else
1928 max_dev = le32_to_cpu(sb->max_dev);
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 for (i=0; i<max_dev;i++)
Lidong Zhong8df72022017-06-12 10:45:55 +08001931 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001932
Song Liua97b7892015-10-08 21:54:09 -07001933 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1934 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001936 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001937 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
1938 sb->feature_map |=
1939 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
1940 else
1941 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001942 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
1943 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
1944 }
1945
NeilBrowndafb20f2012-03-19 12:46:39 +11001946 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001948 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001949 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001950 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001952 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001953 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001954 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001955 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 else
Song Liuc4d4c912015-08-13 14:31:54 -07001957 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 sb->sb_csum = calc_sb_1_csum(sb);
1961}
1962
Chris Webb0cd17fe2008-06-28 08:31:46 +10001963static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001964super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001965{
1966 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001967 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001968 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001969 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001970 if (rdev->data_offset != rdev->new_data_offset)
1971 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001972 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001973 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001974 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001975 max_sectors -= rdev->data_offset;
1976 if (!num_sectors || num_sectors > max_sectors)
1977 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001978 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001979 /* minor version 0 with bitmap we can't move */
1980 return 0;
1981 } else {
1982 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001983 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001984 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001985 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001986 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001987 if (!num_sectors || num_sectors > max_sectors)
1988 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001989 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001990 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001991 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001992 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08001993 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10001994 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11001995 do {
1996 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1997 rdev->sb_page);
1998 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001999 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10002000
2001}
2002
2003static int
2004super_1_allow_new_offset(struct md_rdev *rdev,
2005 unsigned long long new_offset)
2006{
2007 /* All necessary checks on new >= old have been done */
2008 struct bitmap *bitmap;
2009 if (new_offset >= rdev->data_offset)
2010 return 1;
2011
2012 /* with 1.0 metadata, there is no metadata to tread on
2013 * so we can always move back */
2014 if (rdev->mddev->minor_version == 0)
2015 return 1;
2016
2017 /* otherwise we must be sure not to step on
2018 * any metadata, so stay:
2019 * 36K beyond start of superblock
2020 * beyond end of badblocks
2021 * beyond write-intent bitmap
2022 */
2023 if (rdev->sb_start + (32+4)*2 > new_offset)
2024 return 0;
2025 bitmap = rdev->mddev->bitmap;
2026 if (bitmap && !rdev->mddev->bitmap_info.file &&
2027 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10002028 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10002029 return 0;
2030 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2031 return 0;
2032
2033 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002034}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Adrian Bunk75c96f82005-05-05 16:16:09 -07002036static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 [0] = {
2038 .name = "0.90.0",
2039 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002040 .load_super = super_90_load,
2041 .validate_super = super_90_validate,
2042 .sync_super = super_90_sync,
2043 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002044 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 },
2046 [1] = {
2047 .name = "md-1",
2048 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002049 .load_super = super_1_load,
2050 .validate_super = super_1_validate,
2051 .sync_super = super_1_sync,
2052 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002053 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 },
2055};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
NeilBrownfd01b882011-10-11 16:47:53 +11002057static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05002058{
2059 if (mddev->sync_super) {
2060 mddev->sync_super(mddev, rdev);
2061 return;
2062 }
2063
2064 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2065
2066 super_types[mddev->major_version].sync_super(mddev, rdev);
2067}
2068
NeilBrownfd01b882011-10-11 16:47:53 +11002069static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
NeilBrown3cb03002011-10-11 16:45:26 +11002071 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
NeilBrown4b809912008-07-21 17:05:25 +10002073 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07002074 rdev_for_each_rcu(rdev, mddev1) {
2075 if (test_bit(Faulty, &rdev->flags) ||
2076 test_bit(Journal, &rdev->flags) ||
2077 rdev->raid_disk == -1)
2078 continue;
2079 rdev_for_each_rcu(rdev2, mddev2) {
2080 if (test_bit(Faulty, &rdev2->flags) ||
2081 test_bit(Journal, &rdev2->flags) ||
2082 rdev2->raid_disk == -1)
2083 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002084 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002085 rdev2->bdev->bd_contains) {
2086 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002087 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002088 }
Song Liu0b020e82015-09-03 23:00:35 -07002089 }
2090 }
NeilBrown4b809912008-07-21 17:05:25 +10002091 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return 0;
2093}
2094
2095static LIST_HEAD(pending_raid_disks);
2096
Andre Nollac5e7112009-08-03 10:59:47 +10002097/*
2098 * Try to register data integrity profile for an mddev
2099 *
2100 * This is called when an array is started and after a disk has been kicked
2101 * from the array. It only succeeds if all working and active component devices
2102 * are integrity capable with matching profiles.
2103 */
NeilBrownfd01b882011-10-11 16:47:53 +11002104int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002105{
NeilBrown3cb03002011-10-11 16:45:26 +11002106 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002107
Andre Nollac5e7112009-08-03 10:59:47 +10002108 if (list_empty(&mddev->disks))
2109 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002110 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2111 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002112 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002113 /* skip spares and non-functional disks */
2114 if (test_bit(Faulty, &rdev->flags))
2115 continue;
2116 if (rdev->raid_disk < 0)
2117 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002118 if (!reference) {
2119 /* Use the first rdev as the reference */
2120 reference = rdev;
2121 continue;
2122 }
2123 /* does this rdev's profile match the reference profile? */
2124 if (blk_integrity_compare(reference->bdev->bd_disk,
2125 rdev->bdev->bd_disk) < 0)
2126 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002127 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002128 if (!reference || !bdev_get_integrity(reference->bdev))
2129 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002130 /*
2131 * All component devices are integrity capable and have matching
2132 * profiles, register the common profile for the md device.
2133 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002134 blk_integrity_register(mddev->gendisk,
2135 bdev_get_integrity(reference->bdev));
2136
NeilBrown9d487392016-11-02 14:16:49 +11002137 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Kent Overstreetafeee512018-05-20 18:25:52 -04002138 if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002139 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002140 mdname(mddev));
2141 return -EINVAL;
2142 }
Andre Nollac5e7112009-08-03 10:59:47 +10002143 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002144}
Andre Nollac5e7112009-08-03 10:59:47 +10002145EXPORT_SYMBOL(md_integrity_register);
2146
Dan Williams1501efa2016-01-13 16:00:07 -08002147/*
2148 * Attempt to add an rdev, but only if it is consistent with the current
2149 * integrity profile
2150 */
2151int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002152{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002153 struct blk_integrity *bi_rdev;
2154 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002155 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002156
2157 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002158 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002159
2160 bi_rdev = bdev_get_integrity(rdev->bdev);
2161 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002162
2163 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002164 return 0;
2165
2166 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002167 pr_err("%s: incompatible integrity profile for %s\n",
2168 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002169 return -ENXIO;
2170 }
2171
2172 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002173}
2174EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002175
NeilBrownf72ffdd2014-09-30 14:23:59 +10002176static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002178 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002179 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002180 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Dan Williams11e2ede2008-04-30 00:52:32 -07002182 /* prevent duplicates */
2183 if (find_rdev(mddev, rdev->bdev->bd_dev))
2184 return -EEXIST;
2185
NeilBrown97b20ef2017-04-13 08:53:48 +10002186 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2187 mddev->pers)
2188 return -EROFS;
2189
Andre Nolldd8ac332009-03-31 14:33:13 +11002190 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002191 if (!test_bit(Journal, &rdev->flags) &&
2192 rdev->sectors &&
2193 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002194 if (mddev->pers) {
2195 /* Cannot change size, so fail
2196 * If mddev->level <= 0, then we don't care
2197 * about aligning sizes (e.g. linear)
2198 */
2199 if (mddev->level > 0)
2200 return -ENOSPC;
2201 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002202 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 /* Verify rdev->desc_nr is unique.
2206 * If it is -1, assign a free number, else
2207 * check number is not in use
2208 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002209 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (rdev->desc_nr < 0) {
2211 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002212 if (mddev->pers)
2213 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002214 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 choice++;
2216 rdev->desc_nr = choice;
2217 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002218 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002219 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002223 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002224 if (!test_bit(Journal, &rdev->flags) &&
2225 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002226 pr_warn("md: %s: array is limited to %d devices\n",
2227 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002228 return -EBUSY;
2229 }
NeilBrown19133a42005-11-08 21:39:35 -08002230 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002231 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002234 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002235
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002236 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002237 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002238
Tejun Heo0762b8b2008-08-25 19:56:12 +09002239 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002240 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2241 /* failure here is OK */;
2242 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002243
NeilBrown4b809912008-07-21 17:05:25 +10002244 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002245 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002246
2247 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002248 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002251
2252 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002253 pr_warn("md: failed to register dev-%s for %s\n",
2254 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002255 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256}
2257
NeilBrown177a99b2008-02-06 01:39:56 -08002258static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002259{
NeilBrown3cb03002011-10-11 16:45:26 +11002260 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002261 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002262 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002263}
2264
NeilBrownf72ffdd2014-09-30 14:23:59 +10002265static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
2267 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002268
Tejun Heo49731ba2011-01-14 18:43:57 +01002269 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002270 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002271 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002273 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002274 sysfs_put(rdev->sysfs_state);
2275 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002276 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002277 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002278 * writing to 'remove' to "dev/state". We also need
2279 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002280 */
NeilBrown4b809912008-07-21 17:05:25 +10002281 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002282 INIT_WORK(&rdev->del_work, md_delayed_delete);
2283 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002284 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
2287/*
2288 * prevent the device from being mounted, repartitioned or
2289 * otherwise reused by a RAID array (or any other kernel
2290 * subsystem), by bd_claiming the device.
2291 */
NeilBrown3cb03002011-10-11 16:45:26 +11002292static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
2294 int err = 0;
2295 struct block_device *bdev;
2296 char b[BDEVNAME_SIZE];
2297
Tejun Heod4d77622010-11-13 11:55:18 +01002298 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002299 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002301 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 return PTR_ERR(bdev);
2303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 rdev->bdev = bdev;
2305 return err;
2306}
2307
NeilBrown3cb03002011-10-11 16:45:26 +11002308static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
2310 struct block_device *bdev = rdev->bdev;
2311 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002312 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
2315void md_autodetect_dev(dev_t dev);
2316
NeilBrownf72ffdd2014-09-30 14:23:59 +10002317static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
2319 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002320
NeilBrown9d487392016-11-02 14:16:49 +11002321 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002322 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002324 if (test_bit(AutoDetected, &rdev->flags))
2325 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326#endif
2327 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002328 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002331void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
2333 unbind_rdev_from_array(rdev);
2334 export_rdev(rdev);
2335}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002336EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
NeilBrownfd01b882011-10-11 16:47:53 +11002338static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
NeilBrown0638bb02014-09-25 17:43:47 +10002340 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
NeilBrown0638bb02014-09-25 17:43:47 +10002342 while (!list_empty(&mddev->disks)) {
2343 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2344 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002345 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 mddev->raid_disks = 0;
2348 mddev->major_version = 0;
2349}
2350
NeilBrown6497709b2017-03-15 14:05:14 +11002351static bool set_in_sync(struct mddev *mddev)
2352{
Shaohua Liefa4b772017-10-18 22:08:13 -07002353 lockdep_assert_held(&mddev->lock);
NeilBrown4ad23a972017-03-15 14:05:14 +11002354 if (!mddev->in_sync) {
2355 mddev->sync_checkers++;
2356 spin_unlock(&mddev->lock);
2357 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2358 spin_lock(&mddev->lock);
2359 if (!mddev->in_sync &&
2360 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002361 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002362 /*
2363 * Ensure ->in_sync is visible before we clear
2364 * ->sync_checkers.
2365 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002366 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002367 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2368 sysfs_notify_dirent_safe(mddev->sysfs_state);
2369 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002370 if (--mddev->sync_checkers == 0)
2371 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002372 }
2373 if (mddev->safemode == 1)
2374 mddev->safemode = 0;
2375 return mddev->in_sync;
2376}
2377
NeilBrownf72ffdd2014-09-30 14:23:59 +10002378static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
NeilBrown42543762006-06-26 00:27:57 -07002380 /* Update each superblock (in-memory image), but
2381 * if we are allowed to, skip spares which already
2382 * have the right event counter, or have one earlier
2383 * (which would mean they aren't being marked as dirty
2384 * with the rest of the array)
2385 */
NeilBrown3cb03002011-10-11 16:45:26 +11002386 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002387 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002388 if (rdev->sb_events == mddev->events ||
2389 (nospares &&
2390 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002391 rdev->sb_events+1 == mddev->events)) {
2392 /* Don't update this superblock */
2393 rdev->sb_loaded = 2;
2394 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002395 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002396 rdev->sb_loaded = 1;
2397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
2399}
2400
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002401static bool does_sb_need_changing(struct mddev *mddev)
2402{
2403 struct md_rdev *rdev;
2404 struct mdp_superblock_1 *sb;
2405 int role;
2406
2407 /* Find a good rdev */
2408 rdev_for_each(rdev, mddev)
2409 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2410 break;
2411
2412 /* No good device found. */
2413 if (!rdev)
2414 return false;
2415
2416 sb = page_address(rdev->sb_page);
2417 /* Check if a device has become faulty or a spare become active */
2418 rdev_for_each(rdev, mddev) {
2419 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2420 /* Device activated? */
2421 if (role == 0xffff && rdev->raid_disk >=0 &&
2422 !test_bit(Faulty, &rdev->flags))
2423 return true;
2424 /* Device turned faulty? */
2425 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2426 return true;
2427 }
2428
2429 /* Check if any mddev parameters have changed */
2430 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2431 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002432 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002433 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2434 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2435 return true;
2436
2437 return false;
2438}
2439
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002440void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
NeilBrown3cb03002011-10-11 16:45:26 +11002442 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002443 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002444 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002445 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002446 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
NeilBrownd87f0642013-04-24 11:42:40 +10002448 if (mddev->ro) {
2449 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002450 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002451 return;
2452 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002453
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002454repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002455 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002456 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002457 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002458 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002459 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002460 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002461 /* Has someone else has updated the sb */
2462 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002463 if (ret == 0)
2464 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002465 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2466 BIT(MD_SB_CHANGE_DEVS) |
2467 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002468 return;
2469 }
2470 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002471
NeilBrowndb0505d2017-10-17 16:18:36 +11002472 /*
2473 * First make sure individual recovery_offsets are correct
2474 * curr_resync_completed can only be used during recovery.
2475 * During reshape/resync it might use array-addresses rather
2476 * that device addresses.
2477 */
NeilBrowndafb20f2012-03-19 12:46:39 +11002478 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002479 if (rdev->raid_disk >= 0 &&
2480 mddev->delta_disks >= 0 &&
NeilBrowndb0505d2017-10-17 16:18:36 +11002481 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2482 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2483 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002484 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002485 !test_bit(In_sync, &rdev->flags) &&
2486 mddev->curr_resync_completed > rdev->recovery_offset)
2487 rdev->recovery_offset = mddev->curr_resync_completed;
2488
NeilBrownf72ffdd2014-09-30 14:23:59 +10002489 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002490 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002491 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2492 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002493 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002494 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002495 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002496 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002497 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002498 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002499 md_error(mddev, rdev);
2500 }
2501 clear_bit(Blocked, &rdev->flags);
2502 clear_bit(BlockedBadBlocks, &rdev->flags);
2503 wake_up(&rdev->blocked_wait);
2504 }
2505 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002506 wake_up(&mddev->sb_wait);
2507 return;
2508 }
2509
NeilBrown85572d72014-12-15 12:56:56 +11002510 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002511
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002512 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002513
Shaohua Li29530792016-12-08 15:48:19 -08002514 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002515 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002516 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002517 /* just a clean<-> dirty transition, possibly leave spares alone,
2518 * though if events isn't the right even/odd, we will have to do
2519 * spares after all
2520 */
2521 nospares = 1;
2522 if (force_change)
2523 nospares = 0;
2524 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002525 /* If the array is degraded, then skipping spares is both
2526 * dangerous and fairly pointless.
2527 * Dangerous because a device that was removed from the array
2528 * might have a event_count that still looks up-to-date,
2529 * so it can be re-added without a resync.
2530 * Pointless because if there are any spares to skip,
2531 * then a recovery will happen and soon that array won't
2532 * be degraded any more and the spare can go back to sleep then.
2533 */
NeilBrown850b2b422006-10-03 01:15:46 -07002534 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002535
NeilBrown06d91a52005-06-21 17:17:12 -07002536 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002537
2538 /* If this is just a dirty<->clean transition, and the array is clean
2539 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002540 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002541 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002542 && mddev->can_decrease_events
2543 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002544 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002545 mddev->can_decrease_events = 0;
2546 } else {
NeilBrown42543762006-06-26 00:27:57 -07002547 /* otherwise we have to go forward and ... */
2548 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002549 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
NeilBrown403df472014-09-30 15:52:29 +10002552 /*
2553 * This 64-bit counter should never wrap.
2554 * Either we are in around ~1 trillion A.C., assuming
2555 * 1 reboot per second, or we have a bug...
2556 */
2557 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002558
NeilBrowndafb20f2012-03-19 12:46:39 +11002559 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002560 if (rdev->badblocks.changed)
2561 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002562 if (test_bit(Faulty, &rdev->flags))
2563 set_bit(FaultRecorded, &rdev->flags);
2564 }
NeilBrown2699b672011-07-28 11:31:47 +10002565
NeilBrowne6910632008-02-06 01:39:51 -08002566 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002567 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
NeilBrown36a4e1f2011-10-07 14:23:17 +11002569 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2570 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Shaohua Li504634f2016-11-18 09:44:08 -08002572 if (mddev->queue)
2573 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002574rewrite:
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002575 md_bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002576 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002578
NeilBrown42543762006-06-26 00:27:57 -07002579 if (rdev->sb_loaded != 1)
2580 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
NeilBrownf4667222013-12-09 12:04:56 +11002582 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002583 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002584 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002585 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002586 pr_debug("md: (write) %s's sb offset: %llu\n",
2587 bdevname(rdev->bdev, b),
2588 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002589 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002590 if (rdev->badblocks.size) {
2591 md_super_write(mddev, rdev,
2592 rdev->badblocks.sector,
2593 rdev->badblocks.size << 9,
2594 rdev->bb_page);
2595 rdev->badblocks.size = 0;
2596 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002597
NeilBrownf4667222013-12-09 12:04:56 +11002598 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002599 pr_debug("md: %s (skipping faulty)\n",
2600 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002601
NeilBrown7bfa19f2005-06-21 17:17:28 -07002602 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 /* only need to write one superblock... */
2604 break;
2605 }
NeilBrown46533ff2016-11-18 16:16:11 +11002606 if (md_super_wait(mddev) < 0)
2607 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002608 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002609
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002610 if (mddev_is_clustered(mddev) && ret == 0)
2611 md_cluster_ops->metadata_update_finish(mddev);
2612
NeilBrown850b2b422006-10-03 01:15:46 -07002613 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002614 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2615 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002616 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002617 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002618 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002619 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2620 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002621
NeilBrowndafb20f2012-03-19 12:46:39 +11002622 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002623 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2624 clear_bit(Blocked, &rdev->flags);
2625
2626 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002627 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002628 clear_bit(BlockedBadBlocks, &rdev->flags);
2629 wake_up(&rdev->blocked_wait);
2630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002632EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002634static int add_bound_rdev(struct md_rdev *rdev)
2635{
2636 struct mddev *mddev = rdev->mddev;
2637 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002638 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002639
Shaohua Li87d4d912016-01-06 14:37:14 -08002640 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002641 /* If there is hot_add_disk but no hot_remove_disk
2642 * then added disks for geometry changes,
2643 * and should be added immediately.
2644 */
2645 super_types[mddev->major_version].
2646 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002647 if (add_journal)
2648 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002649 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002650 if (add_journal)
2651 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002652 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002653 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002654 return err;
2655 }
2656 }
2657 sysfs_notify_dirent_safe(rdev->sysfs_state);
2658
Shaohua Li29530792016-12-08 15:48:19 -08002659 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002660 if (mddev->degraded)
2661 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2662 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2663 md_new_event(mddev);
2664 md_wakeup_thread(mddev->thread);
2665 return 0;
2666}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Andre Noll7f6ce762008-03-23 18:34:54 +01002668/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002669 * We want to accept with case. For this we use cmd_match.
2670 */
2671static int cmd_match(const char *cmd, const char *str)
2672{
2673 /* See if cmd, written into a sysfs file, matches
2674 * str. They must either be the same, or cmd can
2675 * have a trailing newline
2676 */
2677 while (*cmd && *str && *cmd == *str) {
2678 cmd++;
2679 str++;
2680 }
2681 if (*cmd == '\n')
2682 cmd++;
2683 if (*str || *cmd)
2684 return 0;
2685 return 1;
2686}
2687
NeilBrown86e6ffd2005-11-08 21:39:24 -08002688struct rdev_sysfs_entry {
2689 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002690 ssize_t (*show)(struct md_rdev *, char *);
2691 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002692};
2693
2694static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002695state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002696{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002697 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002698 size_t len = 0;
Mark Rutland6aa7de02017-10-23 14:07:29 -07002699 unsigned long flags = READ_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002700
NeilBrown758bfc82014-12-15 12:56:59 +11002701 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002702 (!test_bit(ExternalBbl, &flags) &&
2703 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002704 len += sprintf(page+len, "faulty%s", sep);
2705 if (test_bit(In_sync, &flags))
2706 len += sprintf(page+len, "in_sync%s", sep);
2707 if (test_bit(Journal, &flags))
2708 len += sprintf(page+len, "journal%s", sep);
2709 if (test_bit(WriteMostly, &flags))
2710 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002711 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002712 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002713 && !test_bit(Faulty, &flags)))
2714 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002715 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002716 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002717 !test_bit(In_sync, &flags))
2718 len += sprintf(page+len, "spare%s", sep);
2719 if (test_bit(WriteErrorSeen, &flags))
2720 len += sprintf(page+len, "write_error%s", sep);
2721 if (test_bit(WantReplacement, &flags))
2722 len += sprintf(page+len, "want_replacement%s", sep);
2723 if (test_bit(Replacement, &flags))
2724 len += sprintf(page+len, "replacement%s", sep);
2725 if (test_bit(ExternalBbl, &flags))
2726 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002727 if (test_bit(FailFast, &flags))
2728 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002729
2730 if (len)
2731 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002732
NeilBrown86e6ffd2005-11-08 21:39:24 -08002733 return len+sprintf(page+len, "\n");
2734}
2735
NeilBrown45dc2de2006-06-26 00:27:58 -07002736static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002737state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002738{
2739 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002740 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002741 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002742 * writemostly - sets write_mostly
2743 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002744 * blocked - sets the Blocked flags
2745 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002746 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002747 * -insync - clear Insync for a device with a slot assigned,
2748 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002749 * write_error - sets WriteErrorSeen
2750 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002751 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002752 */
2753 int err = -EINVAL;
2754 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2755 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002756 if (test_bit(Faulty, &rdev->flags))
2757 err = 0;
2758 else
2759 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002760 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002761 if (rdev->mddev->pers) {
2762 clear_bit(Blocked, &rdev->flags);
2763 remove_and_add_spares(rdev->mddev, rdev);
2764 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002765 if (rdev->raid_disk >= 0)
2766 err = -EBUSY;
2767 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002768 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002769 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002770 if (mddev_is_clustered(mddev))
2771 err = md_cluster_ops->remove_disk(mddev, rdev);
2772
2773 if (err == 0) {
2774 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002775 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002776 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002777 md_wakeup_thread(mddev->thread);
2778 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002779 md_new_event(mddev);
2780 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002781 }
NeilBrownf6556752006-06-26 00:28:01 -07002782 } else if (cmd_match(buf, "writemostly")) {
2783 set_bit(WriteMostly, &rdev->flags);
2784 err = 0;
2785 } else if (cmd_match(buf, "-writemostly")) {
2786 clear_bit(WriteMostly, &rdev->flags);
2787 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002788 } else if (cmd_match(buf, "blocked")) {
2789 set_bit(Blocked, &rdev->flags);
2790 err = 0;
2791 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002792 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002793 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002794 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002795 /* metadata handler doesn't understand badblocks,
2796 * so we need to fail the device
2797 */
2798 md_error(rdev->mddev, rdev);
2799 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002800 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002801 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002802 wake_up(&rdev->blocked_wait);
2803 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2804 md_wakeup_thread(rdev->mddev->thread);
2805
2806 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002807 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2808 set_bit(In_sync, &rdev->flags);
2809 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11002810 } else if (cmd_match(buf, "failfast")) {
2811 set_bit(FailFast, &rdev->flags);
2812 err = 0;
2813 } else if (cmd_match(buf, "-failfast")) {
2814 clear_bit(FailFast, &rdev->flags);
2815 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002816 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2817 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002818 if (rdev->mddev->pers == NULL) {
2819 clear_bit(In_sync, &rdev->flags);
2820 rdev->saved_raid_disk = rdev->raid_disk;
2821 rdev->raid_disk = -1;
2822 err = 0;
2823 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002824 } else if (cmd_match(buf, "write_error")) {
2825 set_bit(WriteErrorSeen, &rdev->flags);
2826 err = 0;
2827 } else if (cmd_match(buf, "-write_error")) {
2828 clear_bit(WriteErrorSeen, &rdev->flags);
2829 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002830 } else if (cmd_match(buf, "want_replacement")) {
2831 /* Any non-spare device that is not a replacement can
2832 * become want_replacement at any time, but we then need to
2833 * check if recovery is needed.
2834 */
2835 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002836 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002837 !test_bit(Replacement, &rdev->flags))
2838 set_bit(WantReplacement, &rdev->flags);
2839 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2840 md_wakeup_thread(rdev->mddev->thread);
2841 err = 0;
2842 } else if (cmd_match(buf, "-want_replacement")) {
2843 /* Clearing 'want_replacement' is always allowed.
2844 * Once replacements starts it is too late though.
2845 */
2846 err = 0;
2847 clear_bit(WantReplacement, &rdev->flags);
2848 } else if (cmd_match(buf, "replacement")) {
2849 /* Can only set a device as a replacement when array has not
2850 * yet been started. Once running, replacement is automatic
2851 * from spares, or by assigning 'slot'.
2852 */
2853 if (rdev->mddev->pers)
2854 err = -EBUSY;
2855 else {
2856 set_bit(Replacement, &rdev->flags);
2857 err = 0;
2858 }
2859 } else if (cmd_match(buf, "-replacement")) {
2860 /* Similarly, can only clear Replacement before start */
2861 if (rdev->mddev->pers)
2862 err = -EBUSY;
2863 else {
2864 clear_bit(Replacement, &rdev->flags);
2865 err = 0;
2866 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002867 } else if (cmd_match(buf, "re-add")) {
NeilBrown011abdc2018-04-26 14:46:29 +10002868 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
2869 rdev->saved_raid_disk >= 0) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002870 /* clear_bit is performed _after_ all the devices
2871 * have their local Faulty bit cleared. If any writes
2872 * happen in the meantime in the local node, they
2873 * will land in the local bitmap, which will be synced
2874 * by this node eventually
2875 */
2876 if (!mddev_is_clustered(rdev->mddev) ||
2877 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2878 clear_bit(Faulty, &rdev->flags);
2879 err = add_bound_rdev(rdev);
2880 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002881 } else
2882 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002883 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2884 set_bit(ExternalBbl, &rdev->flags);
2885 rdev->badblocks.shift = 0;
2886 err = 0;
2887 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2888 clear_bit(ExternalBbl, &rdev->flags);
2889 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002890 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002891 if (!err)
2892 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002893 return err ? err : len;
2894}
NeilBrown80ca3a42006-07-10 04:44:18 -07002895static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002896__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002897
2898static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002899errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002900{
2901 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2902}
2903
2904static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002905errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002906{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002907 unsigned int n;
2908 int rv;
2909
2910 rv = kstrtouint(buf, 10, &n);
2911 if (rv < 0)
2912 return rv;
2913 atomic_set(&rdev->corrected_errors, n);
2914 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002915}
2916static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002917__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002918
NeilBrown014236d2006-01-06 00:20:55 -08002919static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002920slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002921{
Shaohua Lif2076e72015-10-08 21:54:12 -07002922 if (test_bit(Journal, &rdev->flags))
2923 return sprintf(page, "journal\n");
2924 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002925 return sprintf(page, "none\n");
2926 else
2927 return sprintf(page, "%d\n", rdev->raid_disk);
2928}
2929
2930static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002931slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002932{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002933 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002934 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002935
Shaohua Lif2076e72015-10-08 21:54:12 -07002936 if (test_bit(Journal, &rdev->flags))
2937 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002938 if (strncmp(buf, "none", 4)==0)
2939 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002940 else {
2941 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2942 if (err < 0)
2943 return err;
2944 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002945 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002946 /* Setting 'slot' on an active array requires also
2947 * updating the 'rd%d' link, and communicating
2948 * with the personality with ->hot_*_disk.
2949 * For now we only support removing
2950 * failed/spare devices. This normally happens automatically,
2951 * but not when the metadata is externally managed.
2952 */
NeilBrownc303da62008-02-06 01:39:51 -08002953 if (rdev->raid_disk == -1)
2954 return -EEXIST;
2955 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002956 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002957 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002958 clear_bit(Blocked, &rdev->flags);
2959 remove_and_add_spares(rdev->mddev, rdev);
2960 if (rdev->raid_disk >= 0)
2961 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002962 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2963 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002964 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002965 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002966 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002967 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002968 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002969
2970 if (rdev->raid_disk != -1)
2971 return -EBUSY;
2972
NeilBrownc6751b22011-02-02 11:57:13 +11002973 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2974 return -EBUSY;
2975
Neil Brown6c2fce22008-06-28 08:31:31 +10002976 if (rdev->mddev->pers->hot_add_disk == NULL)
2977 return -EINVAL;
2978
NeilBrownba1b41b2011-01-14 09:14:34 +11002979 if (slot >= rdev->mddev->raid_disks &&
2980 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2981 return -ENOSPC;
2982
Neil Brown6c2fce22008-06-28 08:31:31 +10002983 rdev->raid_disk = slot;
2984 if (test_bit(In_sync, &rdev->flags))
2985 rdev->saved_raid_disk = slot;
2986 else
2987 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002988 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002989 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002990 err = rdev->mddev->pers->
2991 hot_add_disk(rdev->mddev, rdev);
2992 if (err) {
2993 rdev->raid_disk = -1;
2994 return err;
2995 } else
2996 sysfs_notify_dirent_safe(rdev->sysfs_state);
2997 if (sysfs_link_rdev(rdev->mddev, rdev))
2998 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002999 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08003000 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11003001 if (slot >= rdev->mddev->raid_disks &&
3002 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08003003 return -ENOSPC;
3004 rdev->raid_disk = slot;
3005 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08003006 clear_bit(Faulty, &rdev->flags);
3007 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08003008 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003009 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08003010 }
NeilBrown014236d2006-01-06 00:20:55 -08003011 return len;
3012}
3013
NeilBrown014236d2006-01-06 00:20:55 -08003014static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07003015__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08003016
NeilBrown93c8cad2006-01-06 00:20:56 -08003017static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003018offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08003019{
Andrew Morton6961ece2006-01-06 00:20:59 -08003020 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08003021}
3022
3023static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003024offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08003025{
NeilBrownc6563a82012-05-21 09:27:00 +10003026 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003027 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003028 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10003029 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003030 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003031 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08003032 /* Must set offset before size, so overlap checks
3033 * can be sane */
3034 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08003035 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10003036 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08003037 return len;
3038}
3039
3040static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07003041__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08003042
NeilBrownc6563a82012-05-21 09:27:00 +10003043static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3044{
3045 return sprintf(page, "%llu\n",
3046 (unsigned long long)rdev->new_data_offset);
3047}
3048
3049static ssize_t new_offset_store(struct md_rdev *rdev,
3050 const char *buf, size_t len)
3051{
3052 unsigned long long new_offset;
3053 struct mddev *mddev = rdev->mddev;
3054
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003055 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10003056 return -EINVAL;
3057
NeilBrownf851b602014-12-11 10:02:10 +11003058 if (mddev->sync_thread ||
3059 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10003060 return -EBUSY;
3061 if (new_offset == rdev->data_offset)
3062 /* reset is always permitted */
3063 ;
3064 else if (new_offset > rdev->data_offset) {
3065 /* must not push array size beyond rdev_sectors */
3066 if (new_offset - rdev->data_offset
3067 + mddev->dev_sectors > rdev->sectors)
3068 return -E2BIG;
3069 }
3070 /* Metadata worries about other space details. */
3071
3072 /* decreasing the offset is inconsistent with a backwards
3073 * reshape.
3074 */
3075 if (new_offset < rdev->data_offset &&
3076 mddev->reshape_backwards)
3077 return -EINVAL;
3078 /* Increasing offset is inconsistent with forwards
3079 * reshape. reshape_direction should be set to
3080 * 'backwards' first.
3081 */
3082 if (new_offset > rdev->data_offset &&
3083 !mddev->reshape_backwards)
3084 return -EINVAL;
3085
3086 if (mddev->pers && mddev->persistent &&
3087 !super_types[mddev->major_version]
3088 .allow_new_offset(rdev, new_offset))
3089 return -E2BIG;
3090 rdev->new_data_offset = new_offset;
3091 if (new_offset > rdev->data_offset)
3092 mddev->reshape_backwards = 1;
3093 else if (new_offset < rdev->data_offset)
3094 mddev->reshape_backwards = 0;
3095
3096 return len;
3097}
3098static struct rdev_sysfs_entry rdev_new_offset =
3099__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3100
NeilBrown83303b62006-01-06 00:21:06 -08003101static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003102rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003103{
Andre Nolldd8ac332009-03-31 14:33:13 +11003104 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003105}
3106
NeilBrownc5d79ad2008-02-06 01:39:54 -08003107static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3108{
3109 /* check if two start/length pairs overlap */
3110 if (s1+l1 <= s2)
3111 return 0;
3112 if (s2+l2 <= s1)
3113 return 0;
3114 return 1;
3115}
3116
Dan Williamsb522adc2009-03-31 15:00:31 +11003117static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3118{
3119 unsigned long long blocks;
3120 sector_t new;
3121
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003122 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003123 return -EINVAL;
3124
3125 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3126 return -EINVAL; /* sector conversion overflow */
3127
3128 new = blocks * 2;
3129 if (new != blocks * 2)
3130 return -EINVAL; /* unsigned long long to sector_t overflow */
3131
3132 *sectors = new;
3133 return 0;
3134}
3135
NeilBrown83303b62006-01-06 00:21:06 -08003136static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003137rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003138{
NeilBrownfd01b882011-10-11 16:47:53 +11003139 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003140 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003141 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003142
Shaohua Lif2076e72015-10-08 21:54:12 -07003143 if (test_bit(Journal, &rdev->flags))
3144 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003145 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003146 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003147 if (rdev->data_offset != rdev->new_data_offset)
3148 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003149 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003150 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003151 sectors = super_types[my_mddev->major_version].
3152 rdev_size_change(rdev, sectors);
3153 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003154 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003155 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003156 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003157 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003158 if (!my_mddev->pers->resize)
3159 /* Cannot change size for RAID0 or Linear etc */
3160 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003161 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003162 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003163 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003164
Andre Nolldd8ac332009-03-31 14:33:13 +11003165 rdev->sectors = sectors;
3166 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003167 /* Need to check that all other rdevs with the same
3168 * ->bdev do not overlap. 'rcu' is sufficient to walk
3169 * the rdev lists safely.
3170 * This check does not provide a hard guarantee, it
3171 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003172 */
NeilBrownfd01b882011-10-11 16:47:53 +11003173 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003174 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003175 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003176
NeilBrown8b1afc32014-09-29 15:33:20 +10003177 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003178 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003179 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003180
NeilBrowndafb20f2012-03-19 12:46:39 +11003181 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003182 if (rdev->bdev == rdev2->bdev &&
3183 rdev != rdev2 &&
3184 overlaps(rdev->data_offset, rdev->sectors,
3185 rdev2->data_offset,
3186 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003187 overlap = 1;
3188 break;
3189 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003190 if (overlap) {
3191 mddev_put(mddev);
3192 break;
3193 }
3194 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003195 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003196 if (overlap) {
3197 /* Someone else could have slipped in a size
3198 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003199 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003200 * safe, and trust userspace not to race with
3201 * itself
3202 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003203 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003204 return -EBUSY;
3205 }
3206 }
NeilBrown83303b62006-01-06 00:21:06 -08003207 return len;
3208}
3209
3210static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003211__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003212
NeilBrown3cb03002011-10-11 16:45:26 +11003213static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003214{
3215 unsigned long long recovery_start = rdev->recovery_offset;
3216
3217 if (test_bit(In_sync, &rdev->flags) ||
3218 recovery_start == MaxSector)
3219 return sprintf(page, "none\n");
3220
3221 return sprintf(page, "%llu\n", recovery_start);
3222}
3223
NeilBrown3cb03002011-10-11 16:45:26 +11003224static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003225{
3226 unsigned long long recovery_start;
3227
3228 if (cmd_match(buf, "none"))
3229 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003230 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003231 return -EINVAL;
3232
3233 if (rdev->mddev->pers &&
3234 rdev->raid_disk >= 0)
3235 return -EBUSY;
3236
3237 rdev->recovery_offset = recovery_start;
3238 if (recovery_start == MaxSector)
3239 set_bit(In_sync, &rdev->flags);
3240 else
3241 clear_bit(In_sync, &rdev->flags);
3242 return len;
3243}
3244
3245static struct rdev_sysfs_entry rdev_recovery_start =
3246__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3247
Vishal Vermafc974ee2015-12-24 19:20:34 -07003248/* sysfs access to bad-blocks list.
3249 * We present two files.
3250 * 'bad-blocks' lists sector numbers and lengths of ranges that
3251 * are recorded as bad. The list is truncated to fit within
3252 * the one-page limit of sysfs.
3253 * Writing "sector length" to this file adds an acknowledged
3254 * bad block list.
3255 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3256 * been acknowledged. Writing to this file adds bad blocks
3257 * without acknowledging them. This is largely for testing.
3258 */
NeilBrown3cb03002011-10-11 16:45:26 +11003259static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003260{
3261 return badblocks_show(&rdev->badblocks, page, 0);
3262}
NeilBrown3cb03002011-10-11 16:45:26 +11003263static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003264{
NeilBrownde393cd2011-07-28 11:31:48 +10003265 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3266 /* Maybe that ack was all we needed */
3267 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3268 wake_up(&rdev->blocked_wait);
3269 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003270}
3271static struct rdev_sysfs_entry rdev_bad_blocks =
3272__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3273
NeilBrown3cb03002011-10-11 16:45:26 +11003274static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003275{
3276 return badblocks_show(&rdev->badblocks, page, 1);
3277}
NeilBrown3cb03002011-10-11 16:45:26 +11003278static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003279{
3280 return badblocks_store(&rdev->badblocks, page, len, 1);
3281}
3282static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3283__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3284
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003285static ssize_t
3286ppl_sector_show(struct md_rdev *rdev, char *page)
3287{
3288 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3289}
3290
3291static ssize_t
3292ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3293{
3294 unsigned long long sector;
3295
3296 if (kstrtoull(buf, 10, &sector) < 0)
3297 return -EINVAL;
3298 if (sector != (sector_t)sector)
3299 return -EINVAL;
3300
3301 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3302 rdev->raid_disk >= 0)
3303 return -EBUSY;
3304
3305 if (rdev->mddev->persistent) {
3306 if (rdev->mddev->major_version == 0)
3307 return -EINVAL;
3308 if ((sector > rdev->sb_start &&
3309 sector - rdev->sb_start > S16_MAX) ||
3310 (sector < rdev->sb_start &&
3311 rdev->sb_start - sector > -S16_MIN))
3312 return -EINVAL;
3313 rdev->ppl.offset = sector - rdev->sb_start;
3314 } else if (!rdev->mddev->external) {
3315 return -EBUSY;
3316 }
3317 rdev->ppl.sector = sector;
3318 return len;
3319}
3320
3321static struct rdev_sysfs_entry rdev_ppl_sector =
3322__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3323
3324static ssize_t
3325ppl_size_show(struct md_rdev *rdev, char *page)
3326{
3327 return sprintf(page, "%u\n", rdev->ppl.size);
3328}
3329
3330static ssize_t
3331ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3332{
3333 unsigned int size;
3334
3335 if (kstrtouint(buf, 10, &size) < 0)
3336 return -EINVAL;
3337
3338 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3339 rdev->raid_disk >= 0)
3340 return -EBUSY;
3341
3342 if (rdev->mddev->persistent) {
3343 if (rdev->mddev->major_version == 0)
3344 return -EINVAL;
3345 if (size > U16_MAX)
3346 return -EINVAL;
3347 } else if (!rdev->mddev->external) {
3348 return -EBUSY;
3349 }
3350 rdev->ppl.size = size;
3351 return len;
3352}
3353
3354static struct rdev_sysfs_entry rdev_ppl_size =
3355__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3356
NeilBrown86e6ffd2005-11-08 21:39:24 -08003357static struct attribute *rdev_default_attrs[] = {
3358 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003359 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003360 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003361 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003362 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003363 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003364 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003365 &rdev_bad_blocks.attr,
3366 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003367 &rdev_ppl_sector.attr,
3368 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003369 NULL,
3370};
3371static ssize_t
3372rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3373{
3374 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003375 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003376
3377 if (!entry->show)
3378 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003379 if (!rdev->mddev)
3380 return -EBUSY;
3381 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003382}
3383
3384static ssize_t
3385rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3386 const char *page, size_t length)
3387{
3388 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003389 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003390 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003391 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003392
3393 if (!entry->store)
3394 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003395 if (!capable(CAP_SYS_ADMIN))
3396 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003397 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003398 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003399 if (rdev->mddev == NULL)
3400 rv = -EBUSY;
3401 else
3402 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003403 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003404 }
3405 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003406}
3407
3408static void rdev_free(struct kobject *ko)
3409{
NeilBrown3cb03002011-10-11 16:45:26 +11003410 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003411 kfree(rdev);
3412}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003413static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003414 .show = rdev_attr_show,
3415 .store = rdev_attr_store,
3416};
3417static struct kobj_type rdev_ktype = {
3418 .release = rdev_free,
3419 .sysfs_ops = &rdev_sysfs_ops,
3420 .default_attrs = rdev_default_attrs,
3421};
3422
NeilBrown3cb03002011-10-11 16:45:26 +11003423int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003424{
3425 rdev->desc_nr = -1;
3426 rdev->saved_raid_disk = -1;
3427 rdev->raid_disk = -1;
3428 rdev->flags = 0;
3429 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003430 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003431 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003432 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003433 rdev->sb_loaded = 0;
3434 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003435 atomic_set(&rdev->nr_pending, 0);
3436 atomic_set(&rdev->read_errors, 0);
3437 atomic_set(&rdev->corrected_errors, 0);
3438
3439 INIT_LIST_HEAD(&rdev->same_set);
3440 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003441
3442 /* Add space to store bad block list.
3443 * This reserves the space even on arrays where it cannot
3444 * be used - I wonder if that matters
3445 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003446 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003447}
3448EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449/*
3450 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3451 *
3452 * mark the device faulty if:
3453 *
3454 * - the device is nonexistent (zero size)
3455 * - the device has no valid superblock
3456 *
3457 * a faulty rdev _never_ has rdev->sb set.
3458 */
NeilBrown3cb03002011-10-11 16:45:26 +11003459static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460{
3461 char b[BDEVNAME_SIZE];
3462 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003463 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 sector_t size;
3465
NeilBrown9ffae0c2006-01-06 00:20:32 -08003466 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003467 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
NeilBrown2230dfe2011-07-28 11:31:46 +10003470 err = md_rdev_init(rdev);
3471 if (err)
3472 goto abort_free;
3473 err = alloc_disk_sb(rdev);
3474 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 goto abort_free;
3476
NeilBrownc5d79ad2008-02-06 01:39:54 -08003477 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 if (err)
3479 goto abort_free;
3480
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003481 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003482
Mike Snitzer77304d22010-11-08 14:39:12 +01003483 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003485 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 bdevname(rdev->bdev,b));
3487 err = -EINVAL;
3488 goto abort_free;
3489 }
3490
3491 if (super_format >= 0) {
3492 err = super_types[super_format].
3493 load_super(rdev, NULL, super_minor);
3494 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003495 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003496 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003497 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 goto abort_free;
3499 }
3500 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003501 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 bdevname(rdev->bdev,b));
3503 goto abort_free;
3504 }
3505 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 return rdev;
3508
3509abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003510 if (rdev->bdev)
3511 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003512 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 kfree(rdev);
3514 return ERR_PTR(err);
3515}
3516
3517/*
3518 * Check a full RAID array for plausibility
3519 */
3520
NeilBrownf72ffdd2014-09-30 14:23:59 +10003521static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522{
3523 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003524 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 char b[BDEVNAME_SIZE];
3526
3527 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003528 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 switch (super_types[mddev->major_version].
3530 load_super(rdev, freshest, mddev->minor_version)) {
3531 case 1:
3532 freshest = rdev;
3533 break;
3534 case 0:
3535 break;
3536 default:
NeilBrown9d487392016-11-02 14:16:49 +11003537 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003539 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 }
3541
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 super_types[mddev->major_version].
3543 validate_super(mddev, freshest);
3544
3545 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003546 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003547 if (mddev->max_disks &&
3548 (rdev->desc_nr >= mddev->max_disks ||
3549 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003550 pr_warn("md: %s: %s: only %d devices permitted\n",
3551 mdname(mddev), bdevname(rdev->bdev, b),
3552 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003553 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003554 continue;
3555 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003556 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 if (super_types[mddev->major_version].
3558 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003559 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003561 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 continue;
3563 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 if (mddev->level == LEVEL_MULTIPATH) {
3566 rdev->desc_nr = i++;
3567 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003568 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003569 } else if (rdev->raid_disk >=
3570 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3571 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003572 rdev->raid_disk = -1;
3573 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 }
3575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576}
3577
NeilBrown72e02072009-12-14 12:49:55 +11003578/* Read a fixed-point number.
3579 * Numbers in sysfs attributes should be in "standard" units where
3580 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003581 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003582 * milliseconds or jiffies.
3583 * This function takes a decimal number with a possible fractional
3584 * component, and produces an integer which is the result of
3585 * multiplying that number by 10^'scale'.
3586 * all without any floating-point arithmetic.
3587 */
3588int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3589{
3590 unsigned long result = 0;
3591 long decimals = -1;
3592 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3593 if (*cp == '.')
3594 decimals = 0;
3595 else if (decimals < scale) {
3596 unsigned int value;
3597 value = *cp - '0';
3598 result = result * 10 + value;
3599 if (decimals >= 0)
3600 decimals++;
3601 }
3602 cp++;
3603 }
3604 if (*cp == '\n')
3605 cp++;
3606 if (*cp)
3607 return -EINVAL;
3608 if (decimals < 0)
3609 decimals = 0;
3610 while (decimals < scale) {
3611 result *= 10;
3612 decimals ++;
3613 }
3614 *res = result;
3615 return 0;
3616}
3617
NeilBrowneae17012005-11-08 21:39:23 -08003618static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003619safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003620{
3621 int msec = (mddev->safemode_delay*1000)/HZ;
3622 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3623}
3624static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003625safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003626{
NeilBrown16f17b32006-06-26 00:27:37 -07003627 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003628
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003629 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003630 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003631 return -EINVAL;
3632 }
3633
NeilBrown72e02072009-12-14 12:49:55 +11003634 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003635 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003636 if (msec == 0)
3637 mddev->safemode_delay = 0;
3638 else {
NeilBrown19052c02008-08-05 15:54:13 +10003639 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003640 unsigned long new_delay = (msec*HZ)/1000;
3641
3642 if (new_delay == 0)
3643 new_delay = 1;
3644 mddev->safemode_delay = new_delay;
3645 if (new_delay < old_delay || old_delay == 0)
3646 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003647 }
3648 return len;
3649}
3650static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003651__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003652
3653static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003654level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003655{
NeilBrown36d091f2014-12-15 12:56:58 +11003656 struct md_personality *p;
3657 int ret;
3658 spin_lock(&mddev->lock);
3659 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003660 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003661 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003662 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003663 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003664 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003665 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003666 else
NeilBrown36d091f2014-12-15 12:56:58 +11003667 ret = 0;
3668 spin_unlock(&mddev->lock);
3669 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003670}
3671
NeilBrownd9d166c2006-01-06 00:20:51 -08003672static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003673level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003674{
Dan Williamsf2859af2010-05-02 10:04:16 -07003675 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003676 ssize_t rv;
3677 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003678 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003679 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003680 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003681 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003682
NeilBrown67918752014-12-15 12:57:01 +11003683 if (slen == 0 || slen >= sizeof(clevel))
3684 return -EINVAL;
3685
3686 rv = mddev_lock(mddev);
3687 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003688 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003689
3690 if (mddev->pers == NULL) {
3691 strncpy(mddev->clevel, buf, slen);
3692 if (mddev->clevel[slen-1] == '\n')
3693 slen--;
3694 mddev->clevel[slen] = 0;
3695 mddev->level = LEVEL_NONE;
3696 rv = len;
3697 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003698 }
NeilBrown67918752014-12-15 12:57:01 +11003699 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003700 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003701 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003702
3703 /* request to change the personality. Need to ensure:
3704 * - array is not engaged in resync/recovery/reshape
3705 * - old personality can be suspended
3706 * - new personality will access other array.
3707 */
3708
NeilBrown67918752014-12-15 12:57:01 +11003709 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003710 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003711 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003712 mddev->reshape_position != MaxSector ||
3713 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003714 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003715
NeilBrown67918752014-12-15 12:57:01 +11003716 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003717 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003718 pr_warn("md: %s: %s does not support online personality change\n",
3719 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003720 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003721 }
3722
3723 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003724 strncpy(clevel, buf, slen);
3725 if (clevel[slen-1] == '\n')
3726 slen--;
3727 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003728 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003729 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003730
Dan Williamsf2859af2010-05-02 10:04:16 -07003731 if (request_module("md-%s", clevel) != 0)
3732 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003733 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003734 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003735 if (!pers || !try_module_get(pers->owner)) {
3736 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003737 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003738 rv = -EINVAL;
3739 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003740 }
3741 spin_unlock(&pers_lock);
3742
3743 if (pers == mddev->pers) {
3744 /* Nothing to do! */
3745 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003746 rv = len;
3747 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003748 }
3749 if (!pers->takeover) {
3750 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003751 pr_warn("md: %s: %s does not support personality takeover\n",
3752 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003753 rv = -EINVAL;
3754 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003755 }
3756
NeilBrowndafb20f2012-03-19 12:46:39 +11003757 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003758 rdev->new_raid_disk = rdev->raid_disk;
3759
NeilBrown245f46c2009-03-31 14:39:39 +11003760 /* ->takeover must set new_* and/or delta_disks
3761 * if it succeeds, and may set them when it fails.
3762 */
3763 priv = pers->takeover(mddev);
3764 if (IS_ERR(priv)) {
3765 mddev->new_level = mddev->level;
3766 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003767 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003768 mddev->raid_disks -= mddev->delta_disks;
3769 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003770 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003771 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003772 pr_warn("md: %s: %s would not accept array\n",
3773 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003774 rv = PTR_ERR(priv);
3775 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003776 }
3777
3778 /* Looks like we have a winner */
3779 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003780 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003781
3782 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003783 oldpers = mddev->pers;
3784 oldpriv = mddev->private;
3785 mddev->pers = pers;
3786 mddev->private = priv;
3787 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3788 mddev->level = mddev->new_level;
3789 mddev->layout = mddev->new_layout;
3790 mddev->chunk_sectors = mddev->new_chunk_sectors;
3791 mddev->delta_disks = 0;
3792 mddev->reshape_backwards = 0;
3793 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003794 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003795
NeilBrowndb721d32014-12-15 12:56:58 +11003796 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003797 mddev->external) {
3798 /* We are converting from a no-redundancy array
3799 * to a redundancy array and metadata is managed
3800 * externally so we need to be sure that writes
3801 * won't block due to a need to transition
3802 * clean->dirty
3803 * until external management is started.
3804 */
3805 mddev->in_sync = 0;
3806 mddev->safemode_delay = 0;
3807 mddev->safemode = 0;
3808 }
3809
NeilBrowndb721d32014-12-15 12:56:58 +11003810 oldpers->free(mddev, oldpriv);
3811
3812 if (oldpers->sync_request == NULL &&
3813 pers->sync_request != NULL) {
3814 /* need to add the md_redundancy_group */
3815 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11003816 pr_warn("md: cannot register extra attributes for %s\n",
3817 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11003818 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3819 }
3820 if (oldpers->sync_request != NULL &&
3821 pers->sync_request == NULL) {
3822 /* need to remove the md_redundancy_group */
3823 if (mddev->to_remove == NULL)
3824 mddev->to_remove = &md_redundancy_group;
3825 }
3826
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003827 module_put(oldpers->owner);
3828
NeilBrowndafb20f2012-03-19 12:46:39 +11003829 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003830 if (rdev->raid_disk < 0)
3831 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003832 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003833 rdev->new_raid_disk = -1;
3834 if (rdev->new_raid_disk == rdev->raid_disk)
3835 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003836 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003837 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003838 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003839 if (rdev->raid_disk < 0)
3840 continue;
3841 if (rdev->new_raid_disk == rdev->raid_disk)
3842 continue;
3843 rdev->raid_disk = rdev->new_raid_disk;
3844 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003845 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003846 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003847 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11003848 pr_warn("md: cannot register rd%d for %s after level change\n",
3849 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003850 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003851 }
3852
NeilBrowndb721d32014-12-15 12:56:58 +11003853 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003854 /* this is now an array without redundancy, so
3855 * it must always be in_sync
3856 */
3857 mddev->in_sync = 1;
3858 del_timer_sync(&mddev->safemode_timer);
3859 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003860 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003861 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08003862 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003863 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003864 if (!mddev->thread)
3865 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003866 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003867 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003868 rv = len;
3869out_unlock:
3870 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003871 return rv;
3872}
3873
3874static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003875__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003876
NeilBrownd4dbd022006-06-26 00:27:59 -07003877static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003878layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003879{
3880 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003881 if (mddev->reshape_position != MaxSector &&
3882 mddev->layout != mddev->new_layout)
3883 return sprintf(page, "%d (%d)\n",
3884 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003885 return sprintf(page, "%d\n", mddev->layout);
3886}
3887
3888static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003889layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003890{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003891 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003892 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003893
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003894 err = kstrtouint(buf, 10, &n);
3895 if (err < 0)
3896 return err;
NeilBrown67918752014-12-15 12:57:01 +11003897 err = mddev_lock(mddev);
3898 if (err)
3899 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003900
NeilBrownb3546032009-03-31 14:56:41 +11003901 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003902 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003903 err = -EBUSY;
3904 else if (mddev->ro)
3905 err = -EROFS;
3906 else {
3907 mddev->new_layout = n;
3908 err = mddev->pers->check_reshape(mddev);
3909 if (err)
3910 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003911 }
NeilBrownb3546032009-03-31 14:56:41 +11003912 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003913 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003914 if (mddev->reshape_position == MaxSector)
3915 mddev->layout = n;
3916 }
NeilBrown67918752014-12-15 12:57:01 +11003917 mddev_unlock(mddev);
3918 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003919}
3920static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003921__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003922
NeilBrowneae17012005-11-08 21:39:23 -08003923static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003924raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003925{
NeilBrownbb636542005-11-08 21:39:45 -08003926 if (mddev->raid_disks == 0)
3927 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003928 if (mddev->reshape_position != MaxSector &&
3929 mddev->delta_disks != 0)
3930 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3931 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003932 return sprintf(page, "%d\n", mddev->raid_disks);
3933}
3934
NeilBrownfd01b882011-10-11 16:47:53 +11003935static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003936
3937static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003938raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003939{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003940 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003941 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003942
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003943 err = kstrtouint(buf, 10, &n);
3944 if (err < 0)
3945 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003946
NeilBrown67918752014-12-15 12:57:01 +11003947 err = mddev_lock(mddev);
3948 if (err)
3949 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003950 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003951 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003952 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003953 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003954 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003955
NeilBrown67918752014-12-15 12:57:01 +11003956 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003957 rdev_for_each(rdev, mddev) {
3958 if (olddisks < n &&
3959 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003960 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003961 if (olddisks > n &&
3962 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003963 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003964 }
NeilBrown67918752014-12-15 12:57:01 +11003965 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003966 mddev->delta_disks = n - olddisks;
3967 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003968 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003969 } else
NeilBrownda943b992006-01-06 00:20:54 -08003970 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003971out_unlock:
3972 mddev_unlock(mddev);
3973 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003974}
3975static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003976__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003977
NeilBrown24dd4692005-11-08 21:39:26 -08003978static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003979chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003980{
NeilBrown08a02ec2007-05-09 02:35:38 -07003981 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003982 mddev->chunk_sectors != mddev->new_chunk_sectors)
3983 return sprintf(page, "%d (%d)\n",
3984 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003985 mddev->chunk_sectors << 9);
3986 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003987}
3988
3989static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003990chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003991{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003992 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003993 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003994
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003995 err = kstrtoul(buf, 10, &n);
3996 if (err < 0)
3997 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003998
NeilBrown67918752014-12-15 12:57:01 +11003999 err = mddev_lock(mddev);
4000 if (err)
4001 return err;
NeilBrownb3546032009-03-31 14:56:41 +11004002 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004003 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004004 err = -EBUSY;
4005 else if (mddev->ro)
4006 err = -EROFS;
4007 else {
4008 mddev->new_chunk_sectors = n >> 9;
4009 err = mddev->pers->check_reshape(mddev);
4010 if (err)
4011 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10004012 }
NeilBrownb3546032009-03-31 14:56:41 +11004013 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10004014 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004015 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10004016 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004017 }
NeilBrown67918752014-12-15 12:57:01 +11004018 mddev_unlock(mddev);
4019 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08004020}
4021static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004022__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08004023
NeilBrowna94213b2006-06-26 00:28:00 -07004024static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004025resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07004026{
NeilBrownd1a7c502009-03-31 15:24:32 +11004027 if (mddev->recovery_cp == MaxSector)
4028 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07004029 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4030}
4031
4032static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004033resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07004034{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004035 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11004036 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004037
4038 if (cmd_match(buf, "none"))
4039 n = MaxSector;
4040 else {
4041 err = kstrtoull(buf, 10, &n);
4042 if (err < 0)
4043 return err;
4044 if (n != (sector_t)n)
4045 return -EINVAL;
4046 }
NeilBrowna94213b2006-06-26 00:28:00 -07004047
NeilBrown67918752014-12-15 12:57:01 +11004048 err = mddev_lock(mddev);
4049 if (err)
4050 return err;
NeilBrownb0986362011-05-11 15:52:21 +10004051 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11004052 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07004053
NeilBrown67918752014-12-15 12:57:01 +11004054 if (!err) {
4055 mddev->recovery_cp = n;
4056 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08004057 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004058 }
4059 mddev_unlock(mddev);
4060 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07004061}
4062static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10004063__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4064 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07004065
NeilBrown9e653b62006-06-26 00:27:58 -07004066/*
4067 * The array state can be:
4068 *
4069 * clear
4070 * No devices, no size, no level
4071 * Equivalent to STOP_ARRAY ioctl
4072 * inactive
4073 * May have some settings, but array is not active
4074 * all IO results in error
4075 * When written, doesn't tear down array, but just stops it
4076 * suspended (not supported yet)
4077 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01004078 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07004079 * readonly
4080 * no resync can happen. no superblocks get written.
4081 * write requests fail
4082 * read-auto
4083 * like readonly, but behaves like 'clean' on a write request.
4084 *
4085 * clean - no pending writes, but otherwise active.
4086 * When written to inactive array, starts without resync
4087 * If a write request arrives then
4088 * if metadata is known, mark 'dirty' and switch to 'active'.
4089 * if not known, block and switch to write-pending
4090 * If written to an active array that has pending writes, then fails.
4091 * active
4092 * fully active: IO and resync can be happening.
4093 * When written to inactive array, starts with resync
4094 *
4095 * write-pending
4096 * clean, but writes are blocked waiting for 'active' to be written.
4097 *
4098 * active-idle
4099 * like active, but no writes have been seen for a while (100msec).
4100 *
4101 */
4102enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4103 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004104static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004105 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4106 "write-pending", "active-idle", NULL };
4107
4108static int match_word(const char *word, char **list)
4109{
4110 int n;
4111 for (n=0; list[n]; n++)
4112 if (cmd_match(word, list[n]))
4113 break;
4114 return n;
4115}
4116
4117static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004118array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004119{
4120 enum array_state st = inactive;
4121
4122 if (mddev->pers)
4123 switch(mddev->ro) {
4124 case 1:
4125 st = readonly;
4126 break;
4127 case 2:
4128 st = read_auto;
4129 break;
4130 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004131 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004132 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004133 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004134 else if (mddev->in_sync)
4135 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004136 else if (mddev->safemode)
4137 st = active_idle;
4138 else
4139 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004140 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004141 }
4142 else {
4143 if (list_empty(&mddev->disks) &&
4144 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004145 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004146 st = clear;
4147 else
4148 st = inactive;
4149 }
4150 return sprintf(page, "%s\n", array_states[st]);
4151}
4152
NeilBrownf72ffdd2014-09-30 14:23:59 +10004153static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4154static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4155static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004156static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004157
4158static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004159array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004160{
NeilBrown6497709b2017-03-15 14:05:14 +11004161 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004162 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004163
4164 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4165 /* don't take reconfig_mutex when toggling between
4166 * clean and active
4167 */
4168 spin_lock(&mddev->lock);
4169 if (st == active) {
4170 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004171 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004172 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004173 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004174 } else /* st == clean */ {
4175 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004176 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004177 err = -EBUSY;
4178 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004179 if (!err)
4180 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004181 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004182 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004183 }
4184 err = mddev_lock(mddev);
4185 if (err)
4186 return err;
4187 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004188 switch(st) {
4189 case bad_word:
4190 break;
4191 case clear:
4192 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004193 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004194 break;
4195 case inactive:
4196 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004197 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004198 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004199 else
NeilBrowne6910632008-02-06 01:39:51 -08004200 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004201 break;
4202 case suspended:
4203 break; /* not supported yet */
4204 case readonly:
4205 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004206 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004207 else {
4208 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004209 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004210 err = do_md_run(mddev);
4211 }
4212 break;
4213 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004214 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004215 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004216 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004217 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004218 err = restart_array(mddev);
4219 if (err == 0) {
4220 mddev->ro = 2;
4221 set_disk_ro(mddev->gendisk, 0);
4222 }
NeilBrown9e653b62006-06-26 00:27:58 -07004223 } else {
4224 mddev->ro = 2;
4225 err = do_md_run(mddev);
4226 }
4227 break;
4228 case clean:
4229 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004230 err = restart_array(mddev);
4231 if (err)
4232 break;
NeilBrown85572d72014-12-15 12:56:56 +11004233 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004234 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004235 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004236 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004237 } else
4238 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004239 break;
4240 case active:
4241 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004242 err = restart_array(mddev);
4243 if (err)
4244 break;
Shaohua Li29530792016-12-08 15:48:19 -08004245 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004246 wake_up(&mddev->sb_wait);
4247 err = 0;
4248 } else {
4249 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004250 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004251 err = do_md_run(mddev);
4252 }
4253 break;
4254 case write_pending:
4255 case active_idle:
4256 /* these cannot be set */
4257 break;
4258 }
NeilBrown67918752014-12-15 12:57:01 +11004259
4260 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004261 if (mddev->hold_active == UNTIL_IOCTL)
4262 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004263 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004264 }
NeilBrown67918752014-12-15 12:57:01 +11004265 mddev_unlock(mddev);
4266 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004267}
NeilBrown80ca3a42006-07-10 04:44:18 -07004268static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004269__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004270
NeilBrown6d7ff7382006-01-06 00:21:16 -08004271static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004272max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004273 return sprintf(page, "%d\n",
4274 atomic_read(&mddev->max_corr_read_errors));
4275}
4276
4277static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004278max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004279{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004280 unsigned int n;
4281 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004282
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004283 rv = kstrtouint(buf, 10, &n);
4284 if (rv < 0)
4285 return rv;
4286 atomic_set(&mddev->max_corr_read_errors, n);
4287 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004288}
4289
4290static struct md_sysfs_entry max_corr_read_errors =
4291__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4292 max_corrected_read_errors_store);
4293
4294static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004295null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004296{
4297 return -EINVAL;
4298}
4299
4300static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004301new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004302{
4303 /* buf must be %d:%d\n? giving major and minor numbers */
4304 /* The new device is added to the array.
4305 * If the array has a persistent superblock, we read the
4306 * superblock to initialise info and check validity.
4307 * Otherwise, only checking done is that in bind_rdev_to_array,
4308 * which mainly checks size.
4309 */
4310 char *e;
4311 int major = simple_strtoul(buf, &e, 10);
4312 int minor;
4313 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004314 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004315 int err;
4316
4317 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4318 return -EINVAL;
4319 minor = simple_strtoul(e+1, &e, 10);
4320 if (*e && *e != '\n')
4321 return -EINVAL;
4322 dev = MKDEV(major, minor);
4323 if (major != MAJOR(dev) ||
4324 minor != MINOR(dev))
4325 return -EOVERFLOW;
4326
NeilBrown67918752014-12-15 12:57:01 +11004327 flush_workqueue(md_misc_wq);
4328
4329 err = mddev_lock(mddev);
4330 if (err)
4331 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004332 if (mddev->persistent) {
4333 rdev = md_import_device(dev, mddev->major_version,
4334 mddev->minor_version);
4335 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004336 struct md_rdev *rdev0
4337 = list_entry(mddev->disks.next,
4338 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004339 err = super_types[mddev->major_version]
4340 .load_super(rdev, rdev0, mddev->minor_version);
4341 if (err < 0)
4342 goto out;
4343 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004344 } else if (mddev->external)
4345 rdev = md_import_device(dev, -2, -1);
4346 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004347 rdev = md_import_device(dev, -1, -1);
4348
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004349 if (IS_ERR(rdev)) {
4350 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004351 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004352 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004353 err = bind_rdev_to_array(rdev, mddev);
4354 out:
4355 if (err)
4356 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004357 mddev_unlock(mddev);
Alexey Obitotskiy5492c462017-07-28 15:49:25 +02004358 if (!err)
4359 md_new_event(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004360 return err ? err : len;
4361}
4362
4363static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004364__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004365
4366static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004367bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004368{
4369 char *end;
4370 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004371 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004372
NeilBrown67918752014-12-15 12:57:01 +11004373 err = mddev_lock(mddev);
4374 if (err)
4375 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004376 if (!mddev->bitmap)
4377 goto out;
4378 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4379 while (*buf) {
4380 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4381 if (buf == end) break;
4382 if (*end == '-') { /* range */
4383 buf = end + 1;
4384 end_chunk = simple_strtoul(buf, &end, 0);
4385 if (buf == end) break;
4386 }
4387 if (*end && !isspace(*end)) break;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004388 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004389 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004390 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004391 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
Paul Clements9b1d1da2006-10-03 01:15:49 -07004392out:
NeilBrown67918752014-12-15 12:57:01 +11004393 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004394 return len;
4395}
4396
4397static struct md_sysfs_entry md_bitmap =
4398__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4399
4400static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004401size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004402{
Andre Noll58c0fed2009-03-31 14:33:13 +11004403 return sprintf(page, "%llu\n",
4404 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004405}
4406
NeilBrownfd01b882011-10-11 16:47:53 +11004407static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004408
4409static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004410size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004411{
4412 /* If array is inactive, we can reduce the component size, but
4413 * not increase it (except from 0).
4414 * If array is active, we can try an on-line resize
4415 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004416 sector_t sectors;
4417 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004418
Andre Noll58c0fed2009-03-31 14:33:13 +11004419 if (err < 0)
4420 return err;
NeilBrown67918752014-12-15 12:57:01 +11004421 err = mddev_lock(mddev);
4422 if (err)
4423 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004424 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004425 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004426 if (err == 0)
4427 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004428 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004429 if (mddev->dev_sectors == 0 ||
4430 mddev->dev_sectors > sectors)
4431 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004432 else
4433 err = -ENOSPC;
4434 }
NeilBrown67918752014-12-15 12:57:01 +11004435 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004436 return err ? err : len;
4437}
4438
4439static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004440__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004441
Masanari Iida83f0d772012-10-30 00:18:08 +09004442/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004443 * This is one of
4444 * 'none' for arrays with no metadata (good luck...)
4445 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004446 * or N.M for internally known formats
4447 */
4448static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004449metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004450{
4451 if (mddev->persistent)
4452 return sprintf(page, "%d.%d\n",
4453 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004454 else if (mddev->external)
4455 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004456 else
4457 return sprintf(page, "none\n");
4458}
4459
4460static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004461metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004462{
4463 int major, minor;
4464 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004465 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004466 /* Changing the details of 'external' metadata is
4467 * always permitted. Otherwise there must be
4468 * no devices attached to the array.
4469 */
NeilBrown67918752014-12-15 12:57:01 +11004470
4471 err = mddev_lock(mddev);
4472 if (err)
4473 return err;
4474 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004475 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4476 ;
4477 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004478 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004479
NeilBrown67918752014-12-15 12:57:01 +11004480 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004481 if (cmd_match(buf, "none")) {
4482 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004483 mddev->external = 0;
4484 mddev->major_version = 0;
4485 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004486 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004487 }
4488 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004489 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004490 if (namelen >= sizeof(mddev->metadata_type))
4491 namelen = sizeof(mddev->metadata_type)-1;
4492 strncpy(mddev->metadata_type, buf+9, namelen);
4493 mddev->metadata_type[namelen] = 0;
4494 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4495 mddev->metadata_type[--namelen] = 0;
4496 mddev->persistent = 0;
4497 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004498 mddev->major_version = 0;
4499 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004500 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004501 }
4502 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004503 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004504 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004505 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004506 buf = e+1;
4507 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004508 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004509 goto out_unlock;
4510 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004511 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004512 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004513 mddev->major_version = major;
4514 mddev->minor_version = minor;
4515 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004516 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004517 err = 0;
4518out_unlock:
4519 mddev_unlock(mddev);
4520 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004521}
4522
4523static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004524__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004525
NeilBrowna35b0d62006-01-06 00:20:49 -08004526static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004527action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004528{
NeilBrown7eec3142005-11-08 21:39:44 -08004529 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004530 unsigned long recovery = mddev->recovery;
4531 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004532 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004533 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4534 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4535 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004536 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004537 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4538 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004539 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004540 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004541 type = "check";
4542 else
4543 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004544 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004545 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004546 else if (mddev->reshape_position != MaxSector)
4547 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004548 }
4549 return sprintf(page, "%s\n", type);
4550}
4551
4552static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004553action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004554{
NeilBrown7eec3142005-11-08 21:39:44 -08004555 if (!mddev->pers || !mddev->pers->sync_request)
4556 return -EINVAL;
4557
NeilBrownb6a9ce62009-05-26 09:41:17 +10004558
4559 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004560 if (cmd_match(page, "frozen"))
4561 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4562 else
4563 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004564 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4565 mddev_lock(mddev) == 0) {
4566 flush_workqueue(md_misc_wq);
4567 if (mddev->sync_thread) {
4568 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004569 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004570 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004571 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004572 }
NeilBrown312045e2015-12-21 11:01:21 +11004573 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004574 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004575 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004576 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004577 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004578 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004579 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004580 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004581 int err;
4582 if (mddev->pers->start_reshape == NULL)
4583 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004584 err = mddev_lock(mddev);
4585 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004586 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4587 err = -EBUSY;
4588 else {
4589 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4590 err = mddev->pers->start_reshape(mddev);
4591 }
NeilBrown67918752014-12-15 12:57:01 +11004592 mddev_unlock(mddev);
4593 }
NeilBrown16484bf2006-03-27 01:18:13 -08004594 if (err)
4595 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004596 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004597 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004598 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004599 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004600 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004601 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004602 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004603 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4604 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004605 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004606 if (mddev->ro == 2) {
4607 /* A write to sync_action is enough to justify
4608 * canceling read-auto mode
4609 */
4610 mddev->ro = 0;
4611 md_wakeup_thread(mddev->sync_thread);
4612 }
NeilBrown03c902e2006-01-06 00:20:46 -08004613 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004614 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004615 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004616 return len;
4617}
4618
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004619static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004620__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004621
4622static ssize_t
4623last_sync_action_show(struct mddev *mddev, char *page)
4624{
4625 return sprintf(page, "%s\n", mddev->last_sync_action);
4626}
4627
4628static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4629
NeilBrown9d888832005-11-08 21:39:26 -08004630static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004631mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004632{
4633 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004634 (unsigned long long)
4635 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004636}
4637
NeilBrown80ca3a42006-07-10 04:44:18 -07004638static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004639
NeilBrown88202a02006-01-06 00:21:36 -08004640static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004641sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004642{
4643 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4644 mddev->sync_speed_min ? "local": "system");
4645}
4646
4647static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004648sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004649{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004650 unsigned int min;
4651 int rv;
4652
NeilBrown88202a02006-01-06 00:21:36 -08004653 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004654 min = 0;
4655 } else {
4656 rv = kstrtouint(buf, 10, &min);
4657 if (rv < 0)
4658 return rv;
4659 if (min == 0)
4660 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004661 }
NeilBrown88202a02006-01-06 00:21:36 -08004662 mddev->sync_speed_min = min;
4663 return len;
4664}
4665
4666static struct md_sysfs_entry md_sync_min =
4667__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4668
4669static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004670sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004671{
4672 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4673 mddev->sync_speed_max ? "local": "system");
4674}
4675
4676static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004677sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004678{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004679 unsigned int max;
4680 int rv;
4681
NeilBrown88202a02006-01-06 00:21:36 -08004682 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004683 max = 0;
4684 } else {
4685 rv = kstrtouint(buf, 10, &max);
4686 if (rv < 0)
4687 return rv;
4688 if (max == 0)
4689 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004690 }
NeilBrown88202a02006-01-06 00:21:36 -08004691 mddev->sync_speed_max = max;
4692 return len;
4693}
4694
4695static struct md_sysfs_entry md_sync_max =
4696__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4697
Iustin Popd7f3d292007-10-16 23:30:54 -07004698static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004699degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004700{
4701 return sprintf(page, "%d\n", mddev->degraded);
4702}
4703static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004704
4705static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004706sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004707{
4708 return sprintf(page, "%d\n", mddev->parallel_resync);
4709}
4710
4711static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004712sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004713{
4714 long n;
4715
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004716 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004717 return -EINVAL;
4718
4719 if (n != 0 && n != 1)
4720 return -EINVAL;
4721
4722 mddev->parallel_resync = n;
4723
4724 if (mddev->sync_thread)
4725 wake_up(&resync_wait);
4726
4727 return len;
4728}
4729
4730/* force parallel resync, even with shared block devices */
4731static struct md_sysfs_entry md_sync_force_parallel =
4732__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4733 sync_force_parallel_show, sync_force_parallel_store);
4734
4735static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004736sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004737{
4738 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004739 if (mddev->curr_resync == 0)
4740 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004741 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4742 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004743 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004744 db = resync - mddev->resync_mark_cnt;
4745 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004746}
4747
NeilBrown80ca3a42006-07-10 04:44:18 -07004748static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004749
4750static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004751sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004752{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004753 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004754
NeilBrownacb180b2009-04-14 16:28:34 +10004755 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4756 return sprintf(page, "none\n");
4757
NeilBrown72f36d52012-10-11 14:25:57 +11004758 if (mddev->curr_resync == 1 ||
4759 mddev->curr_resync == 2)
4760 return sprintf(page, "delayed\n");
4761
NeilBrownc804cde2012-05-21 09:28:33 +10004762 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4763 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004764 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004765 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004766 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004767
NeilBrownacb180b2009-04-14 16:28:34 +10004768 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004769 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004770}
4771
NeilBrown750f1992014-09-30 08:53:05 +10004772static struct md_sysfs_entry md_sync_completed =
4773 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004774
NeilBrowne464eaf2006-03-27 01:18:14 -08004775static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004776min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004777{
4778 return sprintf(page, "%llu\n",
4779 (unsigned long long)mddev->resync_min);
4780}
4781static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004782min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004783{
4784 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004785 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004786
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004787 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004788 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004789
4790 spin_lock(&mddev->lock);
4791 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004792 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004793 goto out_unlock;
4794
4795 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004796 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004797 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004798
NeilBrown50c37b12015-03-23 17:36:38 +11004799 /* Round down to multiple of 4K for safety */
4800 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004801 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004802
NeilBrown23da4222014-12-15 12:57:01 +11004803out_unlock:
4804 spin_unlock(&mddev->lock);
4805 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004806}
4807
4808static struct md_sysfs_entry md_min_sync =
4809__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4810
4811static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004812max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004813{
4814 if (mddev->resync_max == MaxSector)
4815 return sprintf(page, "max\n");
4816 else
4817 return sprintf(page, "%llu\n",
4818 (unsigned long long)mddev->resync_max);
4819}
4820static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004821max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004822{
NeilBrown23da4222014-12-15 12:57:01 +11004823 int err;
4824 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004825 if (strncmp(buf, "max", 3) == 0)
4826 mddev->resync_max = MaxSector;
4827 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004828 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004829 int chunk;
4830
4831 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004832 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004833 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004834 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004835 goto out_unlock;
4836
4837 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004838 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004839 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004840 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004841 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004842
4843 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004844 chunk = mddev->chunk_sectors;
4845 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004846 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004847
4848 err = -EINVAL;
4849 if (sector_div(temp, chunk))
4850 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004851 }
4852 mddev->resync_max = max;
4853 }
4854 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004855 err = 0;
4856out_unlock:
4857 spin_unlock(&mddev->lock);
4858 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004859}
4860
4861static struct md_sysfs_entry md_max_sync =
4862__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4863
4864static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004865suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004866{
4867 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4868}
4869
4870static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004871suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004872{
NeilBrownb03e0cc2017-10-19 12:49:15 +11004873 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004874 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004875
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004876 err = kstrtoull(buf, 10, &new);
4877 if (err < 0)
4878 return err;
4879 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004880 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004881
NeilBrown67918752014-12-15 12:57:01 +11004882 err = mddev_lock(mddev);
4883 if (err)
4884 return err;
4885 err = -EINVAL;
4886 if (mddev->pers == NULL ||
4887 mddev->pers->quiesce == NULL)
4888 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004889 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11004890 mddev->suspend_lo = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004891 mddev_resume(mddev);
4892
NeilBrown67918752014-12-15 12:57:01 +11004893 err = 0;
4894unlock:
4895 mddev_unlock(mddev);
4896 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004897}
4898static struct md_sysfs_entry md_suspend_lo =
4899__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4900
NeilBrowne464eaf2006-03-27 01:18:14 -08004901static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004902suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004903{
4904 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4905}
4906
4907static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004908suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004909{
NeilBrownb03e0cc2017-10-19 12:49:15 +11004910 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004911 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004912
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004913 err = kstrtoull(buf, 10, &new);
4914 if (err < 0)
4915 return err;
4916 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004917 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004918
NeilBrown67918752014-12-15 12:57:01 +11004919 err = mddev_lock(mddev);
4920 if (err)
4921 return err;
4922 err = -EINVAL;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004923 if (mddev->pers == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004924 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004925
4926 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11004927 mddev->suspend_hi = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004928 mddev_resume(mddev);
4929
NeilBrown67918752014-12-15 12:57:01 +11004930 err = 0;
4931unlock:
4932 mddev_unlock(mddev);
4933 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004934}
4935static struct md_sysfs_entry md_suspend_hi =
4936__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4937
NeilBrown08a02ec2007-05-09 02:35:38 -07004938static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004939reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004940{
4941 if (mddev->reshape_position != MaxSector)
4942 return sprintf(page, "%llu\n",
4943 (unsigned long long)mddev->reshape_position);
4944 strcpy(page, "none\n");
4945 return 5;
4946}
4947
4948static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004949reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004950{
NeilBrownc6563a82012-05-21 09:27:00 +10004951 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004952 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004953 int err;
NeilBrown67918752014-12-15 12:57:01 +11004954
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004955 err = kstrtoull(buf, 10, &new);
4956 if (err < 0)
4957 return err;
4958 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004959 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004960 err = mddev_lock(mddev);
4961 if (err)
4962 return err;
4963 err = -EBUSY;
4964 if (mddev->pers)
4965 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004966 mddev->reshape_position = new;
4967 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004968 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004969 mddev->new_level = mddev->level;
4970 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004971 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004972 rdev_for_each(rdev, mddev)
4973 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004974 err = 0;
4975unlock:
4976 mddev_unlock(mddev);
4977 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004978}
4979
4980static struct md_sysfs_entry md_reshape_position =
4981__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4982 reshape_position_store);
4983
Dan Williamsb522adc2009-03-31 15:00:31 +11004984static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004985reshape_direction_show(struct mddev *mddev, char *page)
4986{
4987 return sprintf(page, "%s\n",
4988 mddev->reshape_backwards ? "backwards" : "forwards");
4989}
4990
4991static ssize_t
4992reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4993{
4994 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004995 int err;
4996
NeilBrown2c810cd2012-05-21 09:27:00 +10004997 if (cmd_match(buf, "forwards"))
4998 backwards = 0;
4999 else if (cmd_match(buf, "backwards"))
5000 backwards = 1;
5001 else
5002 return -EINVAL;
5003 if (mddev->reshape_backwards == backwards)
5004 return len;
5005
NeilBrown67918752014-12-15 12:57:01 +11005006 err = mddev_lock(mddev);
5007 if (err)
5008 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10005009 /* check if we are allowed to change */
5010 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11005011 err = -EBUSY;
5012 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10005013 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11005014 err = -EINVAL;
5015 else
5016 mddev->reshape_backwards = backwards;
5017 mddev_unlock(mddev);
5018 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10005019}
5020
5021static struct md_sysfs_entry md_reshape_direction =
5022__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5023 reshape_direction_store);
5024
5025static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005026array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11005027{
5028 if (mddev->external_size)
5029 return sprintf(page, "%llu\n",
5030 (unsigned long long)mddev->array_sectors/2);
5031 else
5032 return sprintf(page, "default\n");
5033}
5034
5035static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005036array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11005037{
5038 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11005039 int err;
5040
5041 err = mddev_lock(mddev);
5042 if (err)
5043 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11005044
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005045 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08005046 if (mddev_is_clustered(mddev)) {
5047 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005048 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08005049 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005050
Dan Williamsb522adc2009-03-31 15:00:31 +11005051 if (strncmp(buf, "default", 7) == 0) {
5052 if (mddev->pers)
5053 sectors = mddev->pers->size(mddev, 0, 0);
5054 else
5055 sectors = mddev->array_sectors;
5056
5057 mddev->external_size = 0;
5058 } else {
5059 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11005060 err = -EINVAL;
5061 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5062 err = -E2BIG;
5063 else
5064 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11005065 }
5066
NeilBrown67918752014-12-15 12:57:01 +11005067 if (!err) {
5068 mddev->array_sectors = sectors;
5069 if (mddev->pers) {
5070 set_capacity(mddev->gendisk, mddev->array_sectors);
5071 revalidate_disk(mddev->gendisk);
5072 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11005073 }
NeilBrown67918752014-12-15 12:57:01 +11005074 mddev_unlock(mddev);
5075 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11005076}
5077
5078static struct md_sysfs_entry md_array_size =
5079__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5080 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005081
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005082static ssize_t
5083consistency_policy_show(struct mddev *mddev, char *page)
5084{
5085 int ret;
5086
5087 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5088 ret = sprintf(page, "journal\n");
5089 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5090 ret = sprintf(page, "ppl\n");
5091 } else if (mddev->bitmap) {
5092 ret = sprintf(page, "bitmap\n");
5093 } else if (mddev->pers) {
5094 if (mddev->pers->sync_request)
5095 ret = sprintf(page, "resync\n");
5096 else
5097 ret = sprintf(page, "none\n");
5098 } else {
5099 ret = sprintf(page, "unknown\n");
5100 }
5101
5102 return ret;
5103}
5104
5105static ssize_t
5106consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5107{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005108 int err = 0;
5109
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005110 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005111 if (mddev->pers->change_consistency_policy)
5112 err = mddev->pers->change_consistency_policy(mddev, buf);
5113 else
5114 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005115 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5116 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005117 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005118 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005119 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005120
5121 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005122}
5123
5124static struct md_sysfs_entry md_consistency_policy =
5125__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5126 consistency_policy_store);
5127
NeilBrowneae17012005-11-08 21:39:23 -08005128static struct attribute *md_default_attrs[] = {
5129 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005130 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005131 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005132 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005133 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005134 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005135 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005136 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005137 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005138 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005139 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005140 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005141 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005142 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005143 &md_consistency_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005144 NULL,
5145};
5146
5147static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005148 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005149 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005150 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005151 &md_sync_min.attr,
5152 &md_sync_max.attr,
5153 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005154 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005155 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005156 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005157 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005158 &md_suspend_lo.attr,
5159 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005160 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005161 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005162 NULL,
5163};
NeilBrown411036f2005-11-08 21:39:40 -08005164static struct attribute_group md_redundancy_group = {
5165 .name = NULL,
5166 .attrs = md_redundancy_attrs,
5167};
5168
NeilBrowneae17012005-11-08 21:39:23 -08005169static ssize_t
5170md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5171{
5172 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005173 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005174 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005175
5176 if (!entry->show)
5177 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005178 spin_lock(&all_mddevs_lock);
5179 if (list_empty(&mddev->all_mddevs)) {
5180 spin_unlock(&all_mddevs_lock);
5181 return -EBUSY;
5182 }
5183 mddev_get(mddev);
5184 spin_unlock(&all_mddevs_lock);
5185
NeilBrownb7b17c92014-12-15 12:56:59 +11005186 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005187 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005188 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005189}
5190
5191static ssize_t
5192md_attr_store(struct kobject *kobj, struct attribute *attr,
5193 const char *page, size_t length)
5194{
5195 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005196 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005197 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005198
5199 if (!entry->store)
5200 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005201 if (!capable(CAP_SYS_ADMIN))
5202 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005203 spin_lock(&all_mddevs_lock);
5204 if (list_empty(&mddev->all_mddevs)) {
5205 spin_unlock(&all_mddevs_lock);
5206 return -EBUSY;
5207 }
5208 mddev_get(mddev);
5209 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005210 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005211 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005212 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005213}
5214
5215static void md_free(struct kobject *ko)
5216{
NeilBrownfd01b882011-10-11 16:47:53 +11005217 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005218
5219 if (mddev->sysfs_state)
5220 sysfs_put(mddev->sysfs_state);
5221
Bart Van Assched8115c352018-02-28 10:15:29 -08005222 if (mddev->gendisk)
5223 del_gendisk(mddev->gendisk);
NeilBrown6cd18e72015-04-27 14:12:22 +10005224 if (mddev->queue)
5225 blk_cleanup_queue(mddev->queue);
Bart Van Assched8115c352018-02-28 10:15:29 -08005226 if (mddev->gendisk)
NeilBrowna21d1502009-01-09 08:31:09 +11005227 put_disk(mddev->gendisk);
NeilBrown4ad23a972017-03-15 14:05:14 +11005228 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005229
Kent Overstreet28dec872018-06-07 20:52:54 -04005230 bioset_exit(&mddev->bio_set);
5231 bioset_exit(&mddev->sync_set);
NeilBrowneae17012005-11-08 21:39:23 -08005232 kfree(mddev);
5233}
5234
Emese Revfy52cf25d2010-01-19 02:58:23 +01005235static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005236 .show = md_attr_show,
5237 .store = md_attr_store,
5238};
5239static struct kobj_type md_ktype = {
5240 .release = md_free,
5241 .sysfs_ops = &md_sysfs_ops,
5242 .default_attrs = md_default_attrs,
5243};
5244
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245int mdp_major = 0;
5246
Dan Williams5fd3a172009-03-04 00:57:25 -07005247static void mddev_delayed_delete(struct work_struct *ws)
5248{
NeilBrownfd01b882011-10-11 16:47:53 +11005249 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005250
NeilBrown43a70502009-12-14 12:49:55 +11005251 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005252 kobject_del(&mddev->kobj);
5253 kobject_put(&mddev->kobj);
5254}
5255
NeilBrown4ad23a972017-03-15 14:05:14 +11005256static void no_op(struct percpu_ref *r) {}
5257
NeilBrowna415c0f2017-06-05 16:05:13 +10005258int mddev_init_writes_pending(struct mddev *mddev)
5259{
5260 if (mddev->writes_pending.percpu_count_ptr)
5261 return 0;
5262 if (percpu_ref_init(&mddev->writes_pending, no_op, 0, GFP_KERNEL) < 0)
5263 return -ENOMEM;
5264 /* We want to start with the refcount at zero */
5265 percpu_ref_put(&mddev->writes_pending);
5266 return 0;
5267}
5268EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5269
NeilBrownefeb53c2009-01-09 08:31:10 +11005270static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271{
NeilBrown039b7222017-04-12 16:26:13 +10005272 /*
5273 * If dev is zero, name is the name of a device to allocate with
5274 * an arbitrary minor number. It will be "md_???"
5275 * If dev is non-zero it must be a device number with a MAJOR of
5276 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5277 * the device is being created by opening a node in /dev.
5278 * If "name" is not NULL, the device is being created by
5279 * writing to /sys/module/md_mod/parameters/new_array.
5280 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005281 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005282 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005284 int partitioned;
5285 int shift;
5286 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005287 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288
5289 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005290 return -ENODEV;
5291
5292 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5293 shift = partitioned ? MdpMinorShift : 0;
5294 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295
Tejun Heoe804ac72010-10-15 15:36:08 +02005296 /* wait for any previous instance of this device to be
5297 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005298 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005299 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005300
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005301 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005302 error = -EEXIST;
5303 if (mddev->gendisk)
5304 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005305
NeilBrown039b7222017-04-12 16:26:13 +10005306 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005307 /* Need to ensure that 'name' is not a duplicate.
5308 */
NeilBrownfd01b882011-10-11 16:47:53 +11005309 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005310 spin_lock(&all_mddevs_lock);
5311
5312 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5313 if (mddev2->gendisk &&
5314 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5315 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005316 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005317 }
5318 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 }
NeilBrown039b7222017-04-12 16:26:13 +10005320 if (name && dev)
5321 /*
5322 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5323 */
5324 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005325
NeilBrown0909dc42009-07-01 12:27:21 +10005326 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005327 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005328 if (!mddev->queue)
5329 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005330 mddev->queue->queuedata = mddev;
5331
NeilBrown409c57f2009-03-31 14:39:39 +11005332 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005333 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005334
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 disk = alloc_disk(1 << shift);
5336 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005337 blk_cleanup_queue(mddev->queue);
5338 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005339 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005341 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005343 if (name)
5344 strcpy(disk->disk_name, name);
5345 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005347 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 disk->fops = &md_fops;
5350 disk->private_data = mddev;
5351 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005352 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005353 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005354 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005355 * remove it now.
5356 */
5357 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005359 /* As soon as we call add_disk(), another thread could get
5360 * through to md_open, so make sure it doesn't get too far
5361 */
5362 mutex_lock(&mddev->open_mutex);
5363 add_disk(disk);
5364
Kent Overstreet28dec872018-06-07 20:52:54 -04005365 error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005366 if (error) {
5367 /* This isn't possible, but as kobject_init_and_add is marked
5368 * __must_check, we must do something with the result
5369 */
NeilBrown9d487392016-11-02 14:16:49 +11005370 pr_debug("md: cannot register %s/md - name in use\n",
5371 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005372 error = 0;
5373 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005374 if (mddev->kobj.sd &&
5375 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005376 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005377 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005378 abort:
5379 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005380 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005381 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005382 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005383 }
NeilBrownd3374822009-01-09 08:31:10 +11005384 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005385 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005386}
5387
5388static struct kobject *md_probe(dev_t dev, int *part, void *data)
5389{
NeilBrown78b63502017-04-12 16:26:13 +10005390 if (create_on_open)
5391 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 return NULL;
5393}
5394
Kees Cooke4dca7b2017-10-17 19:04:42 -07005395static int add_named_array(const char *val, const struct kernel_param *kp)
NeilBrownefeb53c2009-01-09 08:31:10 +11005396{
NeilBrown039b7222017-04-12 16:26:13 +10005397 /*
5398 * val must be "md_*" or "mdNNN".
5399 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005400 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005401 * For "mdNNN" we allocate an array with the minor number NNN
5402 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005403 */
5404 int len = strlen(val);
5405 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005406 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005407
5408 while (len && val[len-1] == '\n')
5409 len--;
5410 if (len >= DISK_NAME_LEN)
5411 return -E2BIG;
5412 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005413 if (strncmp(buf, "md_", 3) == 0)
5414 return md_alloc(0, buf);
5415 if (strncmp(buf, "md", 2) == 0 &&
5416 isdigit(buf[2]) &&
5417 kstrtoul(buf+2, 10, &devnum) == 0 &&
5418 devnum <= MINORMASK)
5419 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5420
5421 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005422}
5423
Kees Cook8376d3c2017-10-16 17:01:48 -07005424static void md_safemode_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425{
Kees Cook8376d3c2017-10-16 17:01:48 -07005426 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427
NeilBrown4ad23a972017-03-15 14:05:14 +11005428 mddev->safemode = 1;
5429 if (mddev->external)
5430 sysfs_notify_dirent_safe(mddev->sysfs_state);
5431
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 md_wakeup_thread(mddev->thread);
5433}
5434
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005435static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
NeilBrownfd01b882011-10-11 16:47:53 +11005437int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438{
NeilBrown2604b702006-01-06 00:20:36 -08005439 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005440 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005441 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
NeilBrowna757e642005-04-16 15:26:42 -07005443 if (list_empty(&mddev->disks))
5444 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
5447 if (mddev->pers)
5448 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005449 /* Cannot run until previous stop completes properly */
5450 if (mddev->sysfs_active)
5451 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005452
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 /*
5454 * Analyze all RAID superblock(s)
5455 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005456 if (!mddev->raid_disks) {
5457 if (!mddev->persistent)
5458 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005459 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
NeilBrownd9d166c2006-01-06 00:20:51 -08005462 if (mddev->level != LEVEL_NONE)
5463 request_module("md-level-%d", mddev->level);
5464 else if (mddev->clevel[0])
5465 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
5467 /*
5468 * Drop all container device buffers, from now on
5469 * the only valid external interface is through the md
5470 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 */
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005472 mddev->has_superblocks = false;
NeilBrowndafb20f2012-03-19 12:46:39 +11005473 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005474 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 continue;
5476 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005477 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005478 if (mddev->ro != 1 &&
5479 (bdev_read_only(rdev->bdev) ||
5480 bdev_read_only(rdev->meta_bdev))) {
5481 mddev->ro = 1;
5482 if (mddev->gendisk)
5483 set_disk_ro(mddev->gendisk, 1);
5484 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005485
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005486 if (rdev->sb_page)
5487 mddev->has_superblocks = true;
5488
NeilBrownf0d76d72007-07-17 04:06:12 -07005489 /* perform some consistency tests on the device.
5490 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005491 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005492 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005493 if (rdev->meta_bdev) {
5494 /* Nothing to check */;
5495 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005496 if (mddev->dev_sectors &&
5497 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005498 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005499 pr_warn("md: %s: data overlaps metadata\n",
5500 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005501 return -EINVAL;
5502 }
5503 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005504 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005505 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005506 pr_warn("md: %s: metadata overlaps data\n",
5507 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005508 return -EINVAL;
5509 }
5510 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005511 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 }
5513
Kent Overstreetafeee512018-05-20 18:25:52 -04005514 if (!bioset_initialized(&mddev->bio_set)) {
5515 err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5516 if (err)
5517 return err;
Ming Lei10273172017-02-14 23:29:00 +08005518 }
Kent Overstreetafeee512018-05-20 18:25:52 -04005519 if (!bioset_initialized(&mddev->sync_set)) {
5520 err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5521 if (err)
Kent Overstreet28dec872018-06-07 20:52:54 -04005522 return err;
NeilBrown5a850712017-06-21 09:12:21 +10005523 }
Xiao Ni5a409b42018-05-21 11:49:54 +08005524 if (mddev->flush_pool == NULL) {
5525 mddev->flush_pool = mempool_create(NR_FLUSH_INFOS, flush_info_alloc,
5526 flush_info_free, mddev);
5527 if (!mddev->flush_pool) {
5528 err = -ENOMEM;
5529 goto abort;
5530 }
5531 }
5532 if (mddev->flush_bio_pool == NULL) {
5533 mddev->flush_bio_pool = mempool_create(NR_FLUSH_BIOS, flush_bio_alloc,
5534 flush_bio_free, mddev);
5535 if (!mddev->flush_bio_pool) {
5536 err = -ENOMEM;
5537 goto abort;
5538 }
5539 }
NeilBrowna167f662010-10-26 18:31:13 +11005540
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005542 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005543 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005545 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005546 pr_warn("md: personality for level %d is not loaded!\n",
5547 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005548 else
NeilBrown9d487392016-11-02 14:16:49 +11005549 pr_warn("md: personality for level %s is not loaded!\n",
5550 mddev->clevel);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005551 err = -EINVAL;
5552 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005555 if (mddev->level != pers->level) {
5556 mddev->level = pers->level;
5557 mddev->new_level = pers->level;
5558 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005559 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560
NeilBrownf6705572006-03-27 01:18:11 -08005561 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005562 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005563 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005564 module_put(pers->owner);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005565 err = -EINVAL;
5566 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08005567 }
5568
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005569 if (pers->sync_request) {
5570 /* Warn if this is a potentially silly
5571 * configuration.
5572 */
5573 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005574 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005575 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005576
NeilBrowndafb20f2012-03-19 12:46:39 +11005577 rdev_for_each(rdev, mddev)
5578 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005579 if (rdev < rdev2 &&
5580 rdev->bdev->bd_contains ==
5581 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005582 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5583 mdname(mddev),
5584 bdevname(rdev->bdev,b),
5585 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005586 warned = 1;
5587 }
5588 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005589
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005590 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005591 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005592 }
5593
NeilBrown657390d2005-08-26 18:34:16 -07005594 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005595 /* may be over-ridden by personality */
5596 mddev->resync_max_sectors = mddev->dev_sectors;
5597
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005598 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599
NeilBrown0f9552b52009-12-30 12:08:50 +11005600 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005601 mddev->ro = 2; /* read-only, but switch on first write */
5602
NeilBrown36d091f2014-12-15 12:56:58 +11005603 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005604 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005605 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005606 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005607 WARN_ONCE(!mddev->external_size,
5608 "%s: default size too small, but 'external_size' not in effect?\n",
5609 __func__);
5610 pr_warn("md: invalid array_size %llu > default size %llu\n",
5611 (unsigned long long)mddev->array_sectors / 2,
5612 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005613 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005614 }
NeilBrown36d091f2014-12-15 12:56:58 +11005615 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005616 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005617 struct bitmap *bitmap;
5618
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005619 bitmap = md_bitmap_create(mddev, -1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005620 if (IS_ERR(bitmap)) {
5621 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005622 pr_warn("%s: failed to create bitmap (%d)\n",
5623 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005624 } else
5625 mddev->bitmap = bitmap;
5626
NeilBrownb15c2e52006-01-06 00:20:16 -08005627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005629 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005630 if (mddev->private)
5631 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005632 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005633 module_put(pers->owner);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005634 md_bitmap_destroy(mddev);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005635 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 }
NeilBrown5c675f82014-12-15 12:56:56 +11005637 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005638 bool nonrot = true;
5639
5640 rdev_for_each(rdev, mddev) {
5641 if (rdev->raid_disk >= 0 &&
5642 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5643 nonrot = false;
5644 break;
5645 }
5646 }
5647 if (mddev->degraded)
5648 nonrot = false;
5649 if (nonrot)
Bart Van Assche8b904b52018-03-07 17:10:10 -08005650 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
Shaohua Libb086a82016-09-30 09:45:40 -07005651 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08005652 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005653 mddev->queue->backing_dev_info->congested_data = mddev;
5654 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005655 }
NeilBrown36d091f2014-12-15 12:56:58 +11005656 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005657 if (mddev->kobj.sd &&
5658 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005659 pr_warn("md: cannot register extra attributes for %s\n",
5660 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005661 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005662 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005663 mddev->ro = 0;
5664
Robert Becker1e509152009-12-14 12:49:58 +11005665 atomic_set(&mddev->max_corr_read_errors,
5666 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005668 if (mddev_is_clustered(mddev))
5669 mddev->safemode_delay = 0;
5670 else
5671 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005673 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005674 spin_lock(&mddev->lock);
5675 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005676 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005677 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005678 if (rdev->raid_disk >= 0)
5679 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005680 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005681
NeilBrowna4a3d262015-07-17 11:57:30 +10005682 if (mddev->degraded && !mddev->ro)
5683 /* This ensures that recovering status is reported immediately
5684 * via sysfs - until a lack of spares is confirmed.
5685 */
5686 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005688
Shaohua Li29530792016-12-08 15:48:19 -08005689 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07005690 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691
NeilBrownd7603b72006-01-06 00:20:30 -08005692 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005693 sysfs_notify_dirent_safe(mddev->sysfs_state);
5694 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005695 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 return 0;
Xiao Nib1261942018-01-24 12:17:38 +08005697
5698abort:
Xiao Ni5a409b42018-05-21 11:49:54 +08005699 if (mddev->flush_bio_pool) {
5700 mempool_destroy(mddev->flush_bio_pool);
5701 mddev->flush_bio_pool = NULL;
5702 }
5703 if (mddev->flush_pool){
5704 mempool_destroy(mddev->flush_pool);
5705 mddev->flush_pool = NULL;
5706 }
Xiao Nib1261942018-01-24 12:17:38 +08005707
5708 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709}
NeilBrown390ee602010-06-01 19:37:27 +10005710EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711
NeilBrownfd01b882011-10-11 16:47:53 +11005712static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005713{
5714 int err;
5715
5716 err = md_run(mddev);
5717 if (err)
5718 goto out;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005719 err = md_bitmap_load(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005720 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005721 md_bitmap_destroy(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005722 goto out;
5723 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005724
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005725 if (mddev_is_clustered(mddev))
5726 md_allow_write(mddev);
5727
Song Liud5d885f2017-11-19 22:17:01 -08005728 /* run start up tasks that require md_thread */
5729 md_start(mddev);
5730
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005731 md_wakeup_thread(mddev->thread);
5732 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5733
NeilBrownfe60b012010-03-29 11:10:42 +11005734 set_capacity(mddev->gendisk, mddev->array_sectors);
5735 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005736 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005737 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5738out:
5739 return err;
5740}
5741
Song Liud5d885f2017-11-19 22:17:01 -08005742int md_start(struct mddev *mddev)
5743{
5744 int ret = 0;
5745
5746 if (mddev->pers->start) {
5747 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
5748 md_wakeup_thread(mddev->thread);
5749 ret = mddev->pers->start(mddev);
5750 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
5751 md_wakeup_thread(mddev->sync_thread);
5752 }
5753 return ret;
5754}
5755EXPORT_SYMBOL_GPL(md_start);
5756
NeilBrownfd01b882011-10-11 16:47:53 +11005757static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758{
5759 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10005760 struct md_rdev *rdev;
5761 bool has_journal = false;
5762 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763
Andre Noll80fab1d2008-07-11 22:02:21 +10005764 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005766 return -ENXIO;
5767 if (!mddev->pers)
5768 return -EINVAL;
5769 if (!mddev->ro)
5770 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005771
NeilBrown97b20ef2017-04-13 08:53:48 +10005772 rcu_read_lock();
5773 rdev_for_each_rcu(rdev, mddev) {
5774 if (test_bit(Journal, &rdev->flags) &&
5775 !test_bit(Faulty, &rdev->flags))
5776 has_journal = true;
5777 if (bdev_read_only(rdev->bdev))
5778 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07005779 }
NeilBrown97b20ef2017-04-13 08:53:48 +10005780 rcu_read_unlock();
5781 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
5782 /* Don't restart rw with journal missing/faulty */
5783 return -EINVAL;
5784 if (has_readonly)
5785 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07005786
Andre Noll80fab1d2008-07-11 22:02:21 +10005787 mddev->safemode = 0;
5788 mddev->ro = 0;
5789 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11005790 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10005791 /* Kick recovery or resync if necessary */
5792 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5793 md_wakeup_thread(mddev->thread);
5794 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005795 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005796 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797}
5798
NeilBrownfd01b882011-10-11 16:47:53 +11005799static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005800{
5801 mddev->array_sectors = 0;
5802 mddev->external_size = 0;
5803 mddev->dev_sectors = 0;
5804 mddev->raid_disks = 0;
5805 mddev->recovery_cp = 0;
5806 mddev->resync_min = 0;
5807 mddev->resync_max = MaxSector;
5808 mddev->reshape_position = MaxSector;
5809 mddev->external = 0;
5810 mddev->persistent = 0;
5811 mddev->level = LEVEL_NONE;
5812 mddev->clevel[0] = 0;
5813 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08005814 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005815 mddev->ro = 0;
5816 mddev->metadata_type[0] = 0;
5817 mddev->chunk_sectors = 0;
5818 mddev->ctime = mddev->utime = 0;
5819 mddev->layout = 0;
5820 mddev->max_disks = 0;
5821 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005822 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005823 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005824 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005825 mddev->new_level = LEVEL_NONE;
5826 mddev->new_layout = 0;
5827 mddev->new_chunk_sectors = 0;
5828 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005829 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005830 mddev->suspend_lo = mddev->suspend_hi = 0;
5831 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5832 mddev->recovery = 0;
5833 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005834 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005835 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005836 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005837 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005838 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005839 mddev->bitmap_info.offset = 0;
5840 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005841 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005842 mddev->bitmap_info.chunksize = 0;
5843 mddev->bitmap_info.daemon_sleep = 0;
5844 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005845 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005846}
5847
NeilBrownfd01b882011-10-11 16:47:53 +11005848static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005849{
NeilBrown6b6204e2013-05-09 09:48:30 +10005850 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005851 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005852 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005853 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005854 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005855 }
5856
5857 del_timer_sync(&mddev->safemode_timer);
5858
Shaohua Li034e33f2016-11-21 10:29:19 -08005859 if (mddev->pers && mddev->pers->quiesce) {
5860 mddev->pers->quiesce(mddev, 1);
5861 mddev->pers->quiesce(mddev, 0);
5862 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005863 md_bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005864
NeilBrownb6d428c2013-04-24 11:42:42 +10005865 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005866 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08005867 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11005868 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005869 if (!mddev_is_clustered(mddev))
5870 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005871 md_update_sb(mddev, 1);
5872 }
5873}
NeilBrowndefad612011-01-14 09:14:33 +11005874
NeilBrownfd01b882011-10-11 16:47:53 +11005875void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005876{
NeilBrown29f097c2013-11-14 17:54:51 +11005877 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005878 __md_stop_writes(mddev);
5879 mddev_unlock(mddev);
5880}
NeilBrown390ee602010-06-01 19:37:27 +10005881EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005882
NeilBrown5aa61f42014-12-15 12:56:57 +11005883static void mddev_detach(struct mddev *mddev)
5884{
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005885 md_bitmap_wait_behind_writes(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005886 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005887 mddev->pers->quiesce(mddev, 1);
5888 mddev->pers->quiesce(mddev, 0);
5889 }
5890 md_unregister_thread(&mddev->thread);
5891 if (mddev->queue)
5892 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5893}
5894
NeilBrown5eff3c42012-11-19 10:47:48 +11005895static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005896{
NeilBrown36d091f2014-12-15 12:56:58 +11005897 struct md_personality *pers = mddev->pers;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005898 md_bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11005899 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005900 /* Ensure ->event_work is done */
5901 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005902 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005903 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005904 spin_unlock(&mddev->lock);
5905 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005906 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005907 if (pers->sync_request && mddev->to_remove == NULL)
5908 mddev->to_remove = &md_redundancy_group;
5909 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005910 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005911}
NeilBrown5eff3c42012-11-19 10:47:48 +11005912
5913void md_stop(struct mddev *mddev)
5914{
5915 /* stop the array and free an attached data structures.
5916 * This is called from dm-raid
5917 */
5918 __md_stop(mddev);
Xiao Ni5a409b42018-05-21 11:49:54 +08005919 if (mddev->flush_bio_pool) {
5920 mempool_destroy(mddev->flush_bio_pool);
5921 mddev->flush_bio_pool = NULL;
5922 }
5923 if (mddev->flush_pool) {
5924 mempool_destroy(mddev->flush_pool);
5925 mddev->flush_pool = NULL;
5926 }
Kent Overstreetafeee512018-05-20 18:25:52 -04005927 bioset_exit(&mddev->bio_set);
5928 bioset_exit(&mddev->sync_set);
NeilBrown5eff3c42012-11-19 10:47:48 +11005929}
5930
NeilBrown390ee602010-06-01 19:37:27 +10005931EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005932
NeilBrowna05b7ea2012-07-19 15:59:18 +10005933static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005934{
5935 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005936 int did_freeze = 0;
5937
5938 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5939 did_freeze = 1;
5940 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5941 md_wakeup_thread(mddev->thread);
5942 }
NeilBrownf851b602014-12-11 10:02:10 +11005943 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005944 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005945 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005946 /* Thread might be blocked waiting for metadata update
5947 * which will now never happen */
5948 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005949
Shaohua Li29530792016-12-08 15:48:19 -08005950 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10005951 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005952 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005953 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5954 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005955 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005956 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005957 mddev_lock_nointr(mddev);
5958
NeilBrowna4bd82d2010-03-29 13:23:10 +11005959 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005960 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005961 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005962 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005963 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005964 if (did_freeze) {
5965 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005966 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005967 md_wakeup_thread(mddev->thread);
5968 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005969 err = -EBUSY;
5970 goto out;
5971 }
5972 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005973 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005974
5975 err = -ENXIO;
5976 if (mddev->ro==1)
5977 goto out;
5978 mddev->ro = 1;
5979 set_disk_ro(mddev->gendisk, 1);
5980 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005981 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5982 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005983 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005984 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005985 }
5986out:
5987 mutex_unlock(&mddev->open_mutex);
5988 return err;
5989}
5990
NeilBrown9e653b62006-06-26 00:27:58 -07005991/* mode:
5992 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005993 * 2 - stop but do not disassemble array
5994 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005995static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005996 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005999 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11006000 int did_freeze = 0;
6001
6002 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6003 did_freeze = 1;
6004 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6005 md_wakeup_thread(mddev->thread);
6006 }
NeilBrownf851b602014-12-11 10:02:10 +11006007 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006008 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006009 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006010 /* Thread might be blocked waiting for metadata update
6011 * which will now never happen */
6012 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006013
NeilBrown30b8feb2013-11-14 15:16:17 +11006014 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006015 wait_event(resync_wait, (mddev->sync_thread == NULL &&
6016 !test_bit(MD_RECOVERY_RUNNING,
6017 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11006018 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
NeilBrownc8c00a62009-08-10 12:50:52 +10006020 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006021 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006022 mddev->sysfs_active ||
6023 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006024 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006025 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10006026 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11006027 if (did_freeze) {
6028 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006029 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006030 md_wakeup_thread(mddev->thread);
6031 }
NeilBrown260fa032013-08-27 16:44:13 +10006032 return -EBUSY;
6033 }
NeilBrown6e17b022010-08-07 21:41:19 +10006034 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11006035 if (mddev->ro)
6036 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11006037
NeilBrowndefad612011-01-14 09:14:33 +11006038 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11006039 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01006040 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006041
NeilBrowna4bd82d2010-03-29 13:23:10 +11006042 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10006043 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08006044
NeilBrowndafb20f2012-03-19 12:46:39 +11006045 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10006046 if (rdev->raid_disk >= 0)
6047 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10006048
NeilBrowna4bd82d2010-03-29 13:23:10 +11006049 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10006050 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006051 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006052 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08006053
NeilBrowna4bd82d2010-03-29 13:23:10 +11006054 if (mddev->ro)
6055 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10006056 } else
6057 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 /*
6059 * Free resources if final stop
6060 */
NeilBrown9e653b62006-06-26 00:27:58 -07006061 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006062 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063
NeilBrownc3d97142009-12-14 12:49:52 +11006064 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11006065 struct file *f = mddev->bitmap_info.file;
6066 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11006067 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11006068 spin_unlock(&mddev->lock);
6069 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08006070 }
NeilBrownc3d97142009-12-14 12:49:52 +11006071 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08006072
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 export_array(mddev);
6074
NeilBrown6177b472010-03-29 11:37:13 +11006075 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11006076 if (mddev->hold_active == UNTIL_STOP)
6077 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006078 }
NeilBrownd7603b72006-01-06 00:20:30 -08006079 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006080 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10006081 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082}
6083
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006084#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11006085static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086{
NeilBrown3cb03002011-10-11 16:45:26 +11006087 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 int err;
6089
NeilBrowna757e642005-04-16 15:26:42 -07006090 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092
NeilBrown9d487392016-11-02 14:16:49 +11006093 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094
NeilBrowndafb20f2012-03-19 12:46:39 +11006095 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11006097 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 }
NeilBrown9d487392016-11-02 14:16:49 +11006099 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100
NeilBrownd710e132008-10-13 11:55:12 +11006101 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006103 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10006104 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 }
6106}
6107
6108/*
6109 * lets try to run arrays based on all disks that have arrived
6110 * until now. (those are in pending_raid_disks)
6111 *
6112 * the method: pick the first pending disk, collect all disks with
6113 * the same UUID, remove all from the pending list and put them into
6114 * the 'same_array' list. Then order this list based on superblock
6115 * update time (freshest comes first), kick out 'old' disks and
6116 * compare superblocks. If everything's fine then run it.
6117 *
6118 * If "unit" is allocated, then bump its reference count
6119 */
6120static void autorun_devices(int part)
6121{
NeilBrown3cb03002011-10-11 16:45:26 +11006122 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006123 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 char b[BDEVNAME_SIZE];
6125
NeilBrown9d487392016-11-02 14:16:49 +11006126 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07006128 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08006130 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11006132 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133
NeilBrown9d487392016-11-02 14:16:49 +11006134 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006136 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006138 pr_debug("md: adding %s ...\n",
6139 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 list_move(&rdev->same_set, &candidates);
6141 }
6142 /*
6143 * now we have a set of devices, with all of them having
6144 * mostly sane superblocks. It's time to allocate the
6145 * mddev.
6146 */
NeilBrowne8703fe2006-10-03 01:15:59 -07006147 if (part) {
6148 dev = MKDEV(mdp_major,
6149 rdev0->preferred_minor << MdpMinorShift);
6150 unit = MINOR(dev) >> MdpMinorShift;
6151 } else {
6152 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6153 unit = MINOR(dev);
6154 }
6155 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006156 pr_warn("md: unit number in %s is bad: %d\n",
6157 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 break;
6159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
6161 md_probe(dev, NULL, NULL);
6162 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006163 if (!mddev || !mddev->gendisk) {
6164 if (mddev)
6165 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 break;
6167 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006168 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006169 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 else if (mddev->raid_disks || mddev->major_version
6171 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006172 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 mdname(mddev), bdevname(rdev0->bdev,b));
6174 mddev_unlock(mddev);
6175 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006176 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006177 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006178 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 list_del_init(&rdev->same_set);
6180 if (bind_rdev_to_array(rdev, mddev))
6181 export_rdev(rdev);
6182 }
6183 autorun_array(mddev);
6184 mddev_unlock(mddev);
6185 }
6186 /* on success, candidates will be empty, on error
6187 * it won't...
6188 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006189 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006190 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 mddev_put(mddev);
6194 }
NeilBrown9d487392016-11-02 14:16:49 +11006195 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006197#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198
NeilBrownf72ffdd2014-09-30 14:23:59 +10006199static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200{
6201 mdu_version_t ver;
6202
6203 ver.major = MD_MAJOR_VERSION;
6204 ver.minor = MD_MINOR_VERSION;
6205 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6206
6207 if (copy_to_user(arg, &ver, sizeof(ver)))
6208 return -EFAULT;
6209
6210 return 0;
6211}
6212
NeilBrownf72ffdd2014-09-30 14:23:59 +10006213static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214{
6215 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006216 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006217 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
NeilBrown1ca69c42012-10-11 13:37:33 +11006219 nr = working = insync = failed = spare = 0;
6220 rcu_read_lock();
6221 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006223 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 failed++;
6225 else {
6226 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006227 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006228 insync++;
Song Liub347af82016-08-11 17:14:45 -07006229 else if (test_bit(Journal, &rdev->flags))
6230 /* TODO: add journal count to md_u.h */
6231 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 else
6233 spare++;
6234 }
6235 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006236 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237
6238 info.major_version = mddev->major_version;
6239 info.minor_version = mddev->minor_version;
6240 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006241 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006243 info.size = mddev->dev_sectors / 2;
6244 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006245 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 info.nr_disks = nr;
6247 info.raid_disks = mddev->raid_disks;
6248 info.md_minor = mddev->md_minor;
6249 info.not_persistent= !mddev->persistent;
6250
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006251 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 info.state = 0;
6253 if (mddev->in_sync)
6254 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006255 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006256 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006257 if (mddev_is_clustered(mddev))
6258 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006259 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 info.working_disks = working;
6261 info.failed_disks = failed;
6262 info.spare_disks = spare;
6263
6264 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006265 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266
6267 if (copy_to_user(arg, &info, sizeof(info)))
6268 return -EFAULT;
6269
6270 return 0;
6271}
6272
NeilBrownf72ffdd2014-09-30 14:23:59 +10006273static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006274{
6275 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006276 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006277 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006278
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006279 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006280 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006281 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006282
NeilBrown32a76272005-06-21 17:17:14 -07006283 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006284 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006285 /* bitmap enabled */
6286 if (mddev->bitmap_info.file) {
6287 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6288 sizeof(file->pathname));
6289 if (IS_ERR(ptr))
6290 err = PTR_ERR(ptr);
6291 else
6292 memmove(file->pathname, ptr,
6293 sizeof(file->pathname)-(ptr-file->pathname));
6294 }
NeilBrown4af1a042014-12-15 12:57:00 +11006295 spin_unlock(&mddev->lock);
6296
6297 if (err == 0 &&
6298 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006299 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006300
NeilBrown32a76272005-06-21 17:17:14 -07006301 kfree(file);
6302 return err;
6303}
6304
NeilBrownf72ffdd2014-09-30 14:23:59 +10006305static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306{
6307 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006308 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309
6310 if (copy_from_user(&info, arg, sizeof(info)))
6311 return -EFAULT;
6312
NeilBrown1ca69c42012-10-11 13:37:33 +11006313 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006314 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315 if (rdev) {
6316 info.major = MAJOR(rdev->bdev->bd_dev);
6317 info.minor = MINOR(rdev->bdev->bd_dev);
6318 info.raid_disk = rdev->raid_disk;
6319 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006320 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006322 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 info.state |= (1<<MD_DISK_ACTIVE);
6324 info.state |= (1<<MD_DISK_SYNC);
6325 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006326 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006327 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006328 if (test_bit(WriteMostly, &rdev->flags))
6329 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006330 if (test_bit(FailFast, &rdev->flags))
6331 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 } else {
6333 info.major = info.minor = 0;
6334 info.raid_disk = -1;
6335 info.state = (1<<MD_DISK_REMOVED);
6336 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006337 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
6339 if (copy_to_user(arg, &info, sizeof(info)))
6340 return -EFAULT;
6341
6342 return 0;
6343}
6344
NeilBrownf72ffdd2014-09-30 14:23:59 +10006345static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346{
6347 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006348 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 dev_t dev = MKDEV(info->major,info->minor);
6350
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006351 if (mddev_is_clustered(mddev) &&
6352 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006353 pr_warn("%s: Cannot add to clustered mddev.\n",
6354 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006355 return -EINVAL;
6356 }
6357
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6359 return -EOVERFLOW;
6360
6361 if (!mddev->raid_disks) {
6362 int err;
6363 /* expecting a device which has a superblock */
6364 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6365 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006366 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 PTR_ERR(rdev));
6368 return PTR_ERR(rdev);
6369 }
6370 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006371 struct md_rdev *rdev0
6372 = list_entry(mddev->disks.next,
6373 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006374 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 .load_super(rdev, rdev0, mddev->minor_version);
6376 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006377 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006378 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 bdevname(rdev0->bdev,b2));
6380 export_rdev(rdev);
6381 return -EINVAL;
6382 }
6383 }
6384 err = bind_rdev_to_array(rdev, mddev);
6385 if (err)
6386 export_rdev(rdev);
6387 return err;
6388 }
6389
6390 /*
6391 * add_new_disk can be used once the array is assembled
6392 * to add "hot spares". They must already have a superblock
6393 * written
6394 */
6395 if (mddev->pers) {
6396 int err;
6397 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006398 pr_warn("%s: personality does not support diskops!\n",
6399 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 return -EINVAL;
6401 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006402 if (mddev->persistent)
6403 rdev = md_import_device(dev, mddev->major_version,
6404 mddev->minor_version);
6405 else
6406 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006408 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 PTR_ERR(rdev));
6410 return PTR_ERR(rdev);
6411 }
NeilBrown1a855a02010-12-09 16:36:28 +11006412 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006413 if (!mddev->persistent) {
6414 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006415 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006416 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006417 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006418 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006419 } else
NeilBrown41158c72005-06-21 17:17:25 -07006420 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006421 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006422 } else
6423 super_types[mddev->major_version].
6424 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006425 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006426 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006427 /* This was a hot-add request, but events doesn't
6428 * match, so reject it.
6429 */
6430 export_rdev(rdev);
6431 return -EINVAL;
6432 }
6433
NeilBrownb2d444d2005-11-08 21:39:31 -08006434 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006435 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6436 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006437 else
6438 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006439 if (info->state & (1<<MD_DISK_FAILFAST))
6440 set_bit(FailFast, &rdev->flags);
6441 else
6442 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006443
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006444 if (info->state & (1<<MD_DISK_JOURNAL)) {
6445 struct md_rdev *rdev2;
6446 bool has_journal = false;
6447
6448 /* make sure no existing journal disk */
6449 rdev_for_each(rdev2, mddev) {
6450 if (test_bit(Journal, &rdev2->flags)) {
6451 has_journal = true;
6452 break;
6453 }
6454 }
NeilBrown230b55f2017-10-17 14:24:09 +11006455 if (has_journal || mddev->bitmap) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006456 export_rdev(rdev);
6457 return -EBUSY;
6458 }
Song Liubac624f2015-08-13 14:31:55 -07006459 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006460 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006461 /*
6462 * check whether the device shows up in other nodes
6463 */
6464 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006465 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006466 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006467 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006468 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006469 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006470 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006471 export_rdev(rdev);
6472 return err;
6473 }
6474 }
6475 }
6476
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477 rdev->raid_disk = -1;
6478 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006479
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 if (err)
6481 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006482
6483 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006484 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6485 if (!err) {
6486 err = md_cluster_ops->new_disk_ack(mddev,
6487 err == 0);
6488 if (err)
6489 md_kick_rdev_from_array(rdev);
6490 }
6491 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006492 if (err)
6493 md_cluster_ops->add_new_disk_cancel(mddev);
6494 else
6495 err = add_bound_rdev(rdev);
6496 }
6497
6498 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006499 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006500
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501 return err;
6502 }
6503
6504 /* otherwise, add_new_disk is only allowed
6505 * for major_version==0 superblocks
6506 */
6507 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006508 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509 return -EINVAL;
6510 }
6511
6512 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6513 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006514 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006516 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 PTR_ERR(rdev));
6518 return PTR_ERR(rdev);
6519 }
6520 rdev->desc_nr = info->number;
6521 if (info->raid_disk < mddev->raid_disks)
6522 rdev->raid_disk = info->raid_disk;
6523 else
6524 rdev->raid_disk = -1;
6525
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006527 if (info->state & (1<<MD_DISK_SYNC))
6528 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006530 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6531 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006532 if (info->state & (1<<MD_DISK_FAILFAST))
6533 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006534
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006536 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006537 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6538 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006539 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006540 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541
NeilBrown2bf071b2006-01-06 00:20:55 -08006542 err = bind_rdev_to_array(rdev, mddev);
6543 if (err) {
6544 export_rdev(rdev);
6545 return err;
6546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 }
6548
6549 return 0;
6550}
6551
NeilBrownf72ffdd2014-09-30 14:23:59 +10006552static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553{
6554 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006555 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556
Yufen Yuc42a0e22018-05-04 18:08:10 +08006557 if (!mddev->pers)
6558 return -ENODEV;
6559
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 rdev = find_rdev(mddev, dev);
6561 if (!rdev)
6562 return -ENXIO;
6563
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006564 if (rdev->raid_disk < 0)
6565 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006566
NeilBrown3ea8929d2013-04-24 11:42:41 +10006567 clear_bit(Blocked, &rdev->flags);
6568 remove_and_add_spares(mddev, rdev);
6569
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 if (rdev->raid_disk >= 0)
6571 goto busy;
6572
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006573kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006574 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006575 md_cluster_ops->remove_disk(mddev, rdev);
6576
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006577 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006578 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006579 if (mddev->thread)
6580 md_wakeup_thread(mddev->thread);
6581 else
6582 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006583 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584
6585 return 0;
6586busy:
NeilBrown9d487392016-11-02 14:16:49 +11006587 pr_debug("md: cannot remove active disk %s from %s ...\n",
6588 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 return -EBUSY;
6590}
6591
NeilBrownf72ffdd2014-09-30 14:23:59 +10006592static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593{
6594 char b[BDEVNAME_SIZE];
6595 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006596 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597
6598 if (!mddev->pers)
6599 return -ENODEV;
6600
6601 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006602 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603 mdname(mddev));
6604 return -EINVAL;
6605 }
6606 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006607 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 mdname(mddev));
6609 return -EINVAL;
6610 }
6611
NeilBrownd710e132008-10-13 11:55:12 +11006612 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006614 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 PTR_ERR(rdev));
6616 return -EINVAL;
6617 }
6618
6619 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006620 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006622 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623
NeilBrown8190e752009-06-18 08:48:58 +10006624 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625
NeilBrownb2d444d2005-11-08 21:39:31 -08006626 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006627 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 bdevname(rdev->bdev,b), mdname(mddev));
6629 err = -EINVAL;
6630 goto abort_export;
6631 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006632
NeilBrownb2d444d2005-11-08 21:39:31 -08006633 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006635 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006636 err = bind_rdev_to_array(rdev, mddev);
6637 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006638 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639
6640 /*
6641 * The rest should better be atomic, we can have disk failures
6642 * noticed in interrupt contexts ...
6643 */
6644
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 rdev->raid_disk = -1;
6646
Shaohua Li29530792016-12-08 15:48:19 -08006647 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006648 if (!mddev->thread)
6649 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 /*
6651 * Kick recovery, maybe this spare has to be added to the
6652 * array immediately.
6653 */
6654 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6655 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006656 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 return 0;
6658
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659abort_export:
6660 export_rdev(rdev);
6661 return err;
6662}
6663
NeilBrownfd01b882011-10-11 16:47:53 +11006664static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006665{
NeilBrown035328c2014-04-09 12:25:40 +10006666 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006667
NeilBrown36fa3062005-09-09 16:23:45 -07006668 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006669 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006670 return -EBUSY;
6671 if (mddev->recovery || mddev->sync_thread)
6672 return -EBUSY;
6673 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006674 }
6675
NeilBrown36fa3062005-09-09 16:23:45 -07006676 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006677 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006678 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006679
NeilBrown1e594bb2014-12-15 12:57:00 +11006680 if (mddev->bitmap || mddev->bitmap_info.file)
6681 return -EEXIST; /* cannot add when bitmap is present */
6682 f = fget(fd);
6683
6684 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006685 pr_warn("%s: error: failed to get bitmap file\n",
6686 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006687 return -EBADF;
6688 }
6689
NeilBrown1e594bb2014-12-15 12:57:00 +11006690 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006691 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006692 pr_warn("%s: error: bitmap file must be a regular file\n",
6693 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006694 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006695 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006696 pr_warn("%s: error: bitmap file must open for write\n",
6697 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006698 err = -EBADF;
6699 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006700 pr_warn("%s: error: bitmap file is already in use\n",
6701 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006702 err = -EBUSY;
6703 }
6704 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006705 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006706 return err;
6707 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006708 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006709 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006710 } else if (mddev->bitmap == NULL)
6711 return -ENOENT; /* cannot remove what isn't there */
6712 err = 0;
6713 if (mddev->pers) {
NeilBrown69e51b42010-06-01 19:37:35 +10006714 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006715 struct bitmap *bitmap;
6716
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006717 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006718 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006719 if (!IS_ERR(bitmap)) {
6720 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006721 err = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006722 } else
6723 err = PTR_ERR(bitmap);
NeilBrown52a0d492017-10-17 13:46:43 +11006724 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006725 md_bitmap_destroy(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11006726 fd = -1;
6727 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006728 mddev_resume(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11006729 } else if (fd < 0) {
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006730 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006731 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006732 mddev_resume(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006733 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006734 }
6735 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006736 struct file *f = mddev->bitmap_info.file;
6737 if (f) {
6738 spin_lock(&mddev->lock);
6739 mddev->bitmap_info.file = NULL;
6740 spin_unlock(&mddev->lock);
6741 fput(f);
6742 }
NeilBrown36fa3062005-09-09 16:23:45 -07006743 }
6744
NeilBrown32a76272005-06-21 17:17:14 -07006745 return err;
6746}
6747
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748/*
6749 * set_array_info is used two different ways
6750 * The original usage is when creating a new array.
6751 * In this usage, raid_disks is > 0 and it together with
6752 * level, size, not_persistent,layout,chunksize determine the
6753 * shape of the array.
6754 * This will always create an array with a type-0.90.0 superblock.
6755 * The newer usage is when assembling an array.
6756 * In this case raid_disks will be 0, and the major_version field is
6757 * use to determine which style super-blocks are to be found on the devices.
6758 * The minor and patch _version numbers are also kept incase the
6759 * super_block handler wishes to interpret them.
6760 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006761static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762{
6763
6764 if (info->raid_disks == 0) {
6765 /* just setting version number for superblock loading */
6766 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006767 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 super_types[info->major_version].name == NULL) {
6769 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11006770 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 info->major_version);
6772 return -EINVAL;
6773 }
6774 mddev->major_version = info->major_version;
6775 mddev->minor_version = info->minor_version;
6776 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006777 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006778 /* ensure mddev_put doesn't delete this now that there
6779 * is some minimal configuration.
6780 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006781 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 return 0;
6783 }
6784 mddev->major_version = MD_MAJOR_VERSION;
6785 mddev->minor_version = MD_MINOR_VERSION;
6786 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006787 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788
6789 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006790 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006791 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 mddev->raid_disks = info->raid_disks;
6793 /* don't set md_minor, it is determined by which /dev/md* was
6794 * openned
6795 */
6796 if (info->state & (1<<MD_SB_CLEAN))
6797 mddev->recovery_cp = MaxSector;
6798 else
6799 mddev->recovery_cp = 0;
6800 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006801 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802
6803 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006804 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805
Shaohua Li29530792016-12-08 15:48:19 -08006806 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11006807 mddev->max_disks = MD_SB_DISKS;
6808 mddev->flags = 0;
6809 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006810 }
6811 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812
NeilBrownc3d97142009-12-14 12:49:52 +11006813 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006814 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006815 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006816
NeilBrownf6705572006-03-27 01:18:11 -08006817 mddev->reshape_position = MaxSector;
6818
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 /*
6820 * Generate a 128 bit UUID
6821 */
6822 get_random_bytes(mddev->uuid, 16);
6823
NeilBrownf6705572006-03-27 01:18:11 -08006824 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006825 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006826 mddev->new_layout = mddev->layout;
6827 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006828 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006829
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830 return 0;
6831}
6832
NeilBrownfd01b882011-10-11 16:47:53 +11006833void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006834{
Shaohua Liefa4b772017-10-18 22:08:13 -07006835 lockdep_assert_held(&mddev->reconfig_mutex);
Dan Williamsb522adc2009-03-31 15:00:31 +11006836
6837 if (mddev->external_size)
6838 return;
6839
Dan Williams1f403622009-03-31 14:59:03 +11006840 mddev->array_sectors = array_sectors;
6841}
6842EXPORT_SYMBOL(md_set_array_sectors);
6843
NeilBrownfd01b882011-10-11 16:47:53 +11006844static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006845{
NeilBrown3cb03002011-10-11 16:45:26 +11006846 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006847 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006848 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08006849 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006850
NeilBrowna35b0d62006-01-06 00:20:49 -08006851 if (mddev->pers->resize == NULL)
6852 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006853 /* The "num_sectors" is the number of sectors of each device that
6854 * is used. This can only make sense for arrays with redundancy.
6855 * linear and raid0 always use whatever space is available. We can only
6856 * consider changing this number if no resync or reconstruction is
6857 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006858 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006859 * of each device. If num_sectors is zero, we find the largest size
6860 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006861 */
NeilBrownf851b602014-12-11 10:02:10 +11006862 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6863 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006864 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006865 if (mddev->ro)
6866 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006867
NeilBrowndafb20f2012-03-19 12:46:39 +11006868 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006869 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006870
Andre Nolld71f9f82008-07-11 22:02:22 +10006871 if (fit && (num_sectors == 0 || num_sectors > avail))
6872 num_sectors = avail;
6873 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006874 return -ENOSPC;
6875 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006876 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08006877 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08006878 if (mddev_is_clustered(mddev))
6879 md_cluster_ops->update_size(mddev, old_dev_sectors);
6880 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08006881 set_capacity(mddev->gendisk, mddev->array_sectors);
6882 revalidate_disk(mddev->gendisk);
6883 }
6884 }
NeilBrowna35b0d62006-01-06 00:20:49 -08006885 return rv;
6886}
6887
NeilBrownfd01b882011-10-11 16:47:53 +11006888static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006889{
6890 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006891 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006892 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006893 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006894 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006895 if (mddev->ro)
6896 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006897 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006898 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006899 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006900 if (mddev->sync_thread ||
6901 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6902 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006903 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006904
6905 rdev_for_each(rdev, mddev) {
6906 if (mddev->raid_disks < raid_disks &&
6907 rdev->data_offset < rdev->new_data_offset)
6908 return -EINVAL;
6909 if (mddev->raid_disks > raid_disks &&
6910 rdev->data_offset > rdev->new_data_offset)
6911 return -EINVAL;
6912 }
6913
NeilBrown63c70c42006-03-27 01:18:13 -08006914 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006915 if (mddev->delta_disks < 0)
6916 mddev->reshape_backwards = 1;
6917 else if (mddev->delta_disks > 0)
6918 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006919
6920 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006921 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006922 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006923 mddev->reshape_backwards = 0;
6924 }
NeilBrownda943b992006-01-06 00:20:54 -08006925 return rv;
6926}
6927
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928/*
6929 * update_array_info is used to change the configuration of an
6930 * on-line array.
6931 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6932 * fields in the info are checked against the array.
6933 * Any differences that cannot be handled will cause an error.
6934 * Normally, only one change can be managed at a time.
6935 */
NeilBrownfd01b882011-10-11 16:47:53 +11006936static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937{
6938 int rv = 0;
6939 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006940 int state = 0;
6941
6942 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006943 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006944 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945
6946 if (mddev->major_version != info->major_version ||
6947 mddev->minor_version != info->minor_version ||
6948/* mddev->patch_version != info->patch_version || */
6949 mddev->ctime != info->ctime ||
6950 mddev->level != info->level ||
6951/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006952 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006953 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006954 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6955 ((state^info->state) & 0xfffffe00)
6956 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 return -EINVAL;
6958 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006959 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6960 cnt++;
6961 if (mddev->raid_disks != info->raid_disks)
6962 cnt++;
6963 if (mddev->layout != info->layout)
6964 cnt++;
6965 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6966 cnt++;
6967 if (cnt == 0)
6968 return 0;
6969 if (cnt > 1)
6970 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971
6972 if (mddev->layout != info->layout) {
6973 /* Change layout
6974 * we don't need to do anything at the md level, the
6975 * personality will take care of it all.
6976 */
NeilBrown50ac1682009-06-18 08:47:55 +10006977 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006979 else {
6980 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006981 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006982 if (rv)
6983 mddev->new_layout = mddev->layout;
6984 return rv;
6985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006987 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006988 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989
NeilBrownda943b992006-01-06 00:20:54 -08006990 if (mddev->raid_disks != info->raid_disks)
6991 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992
NeilBrown36fa3062005-09-09 16:23:45 -07006993 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006994 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6995 rv = -EINVAL;
6996 goto err;
6997 }
6998 if (mddev->recovery || mddev->sync_thread) {
6999 rv = -EBUSY;
7000 goto err;
7001 }
NeilBrown36fa3062005-09-09 16:23:45 -07007002 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007003 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07007004 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007005 if (mddev->bitmap) {
7006 rv = -EEXIST;
7007 goto err;
7008 }
7009 if (mddev->bitmap_info.default_offset == 0) {
7010 rv = -EINVAL;
7011 goto err;
7012 }
NeilBrownc3d97142009-12-14 12:49:52 +11007013 mddev->bitmap_info.offset =
7014 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10007015 mddev->bitmap_info.space =
7016 mddev->bitmap_info.default_space;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007017 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007018 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007019 if (!IS_ERR(bitmap)) {
7020 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007021 rv = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007022 } else
7023 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07007024 if (rv)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007025 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007026 mddev_resume(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07007027 } else {
7028 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007029 if (!mddev->bitmap) {
7030 rv = -ENOENT;
7031 goto err;
7032 }
7033 if (mddev->bitmap->storage.file) {
7034 rv = -EINVAL;
7035 goto err;
7036 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11007037 if (mddev->bitmap_info.nodes) {
7038 /* hold PW on all the bitmap lock */
7039 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11007040 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 +11007041 rv = -EPERM;
7042 md_cluster_ops->unlock_all_bitmaps(mddev);
7043 goto err;
7044 }
7045
7046 mddev->bitmap_info.nodes = 0;
7047 md_cluster_ops->leave(mddev);
7048 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007049 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007050 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007051 mddev_resume(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11007052 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007053 }
7054 }
NeilBrown850b2b422006-10-03 01:15:46 -07007055 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007056 return rv;
7057err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 return rv;
7059}
7060
NeilBrownfd01b882011-10-11 16:47:53 +11007061static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062{
NeilBrown3cb03002011-10-11 16:45:26 +11007063 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11007064 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065
7066 if (mddev->pers == NULL)
7067 return -ENODEV;
7068
NeilBrown1ca69c42012-10-11 13:37:33 +11007069 rcu_read_lock();
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01007070 rdev = md_find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11007072 err = -ENODEV;
7073 else {
7074 md_error(mddev, rdev);
7075 if (!test_bit(Faulty, &rdev->flags))
7076 err = -EBUSY;
7077 }
7078 rcu_read_unlock();
7079 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080}
7081
Andre Noll2f9618c2008-04-25 18:57:58 +02007082/*
7083 * We have a problem here : there is no easy way to give a CHS
7084 * virtual geometry. We currently pretend that we have a 2 heads
7085 * 4 sectors (with a BIG number of cylinders...). This drives
7086 * dosfs just mad... ;-)
7087 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007088static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7089{
NeilBrownfd01b882011-10-11 16:47:53 +11007090 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007091
7092 geo->heads = 2;
7093 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11007094 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007095 return 0;
7096}
7097
Nicolas Schichancb335f82014-01-15 16:58:52 +01007098static inline bool md_ioctl_valid(unsigned int cmd)
7099{
7100 switch (cmd) {
7101 case ADD_NEW_DISK:
7102 case BLKROSET:
7103 case GET_ARRAY_INFO:
7104 case GET_BITMAP_FILE:
7105 case GET_DISK_INFO:
7106 case HOT_ADD_DISK:
7107 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007108 case RAID_AUTORUN:
7109 case RAID_VERSION:
7110 case RESTART_ARRAY_RW:
7111 case RUN_ARRAY:
7112 case SET_ARRAY_INFO:
7113 case SET_BITMAP_FILE:
7114 case SET_DISK_FAULTY:
7115 case STOP_ARRAY:
7116 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007117 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007118 return true;
7119 default:
7120 return false;
7121 }
7122}
7123
Al Viroa39907f2008-03-02 10:31:15 -05007124static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125 unsigned int cmd, unsigned long arg)
7126{
7127 int err = 0;
7128 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11007129 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10007130 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08007131 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132
Nicolas Schichancb335f82014-01-15 16:58:52 +01007133 if (!md_ioctl_valid(cmd))
7134 return -ENOTTY;
7135
NeilBrown506c9e42011-12-23 10:17:26 +11007136 switch (cmd) {
7137 case RAID_VERSION:
7138 case GET_ARRAY_INFO:
7139 case GET_DISK_INFO:
7140 break;
7141 default:
7142 if (!capable(CAP_SYS_ADMIN))
7143 return -EACCES;
7144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145
7146 /*
7147 * Commands dealing with the RAID driver but not any
7148 * particular array:
7149 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007150 switch (cmd) {
7151 case RAID_VERSION:
7152 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007153 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11007156 case RAID_AUTORUN:
7157 err = 0;
7158 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007159 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11007161 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 }
7163
7164 /*
7165 * Commands creating/starting a new array:
7166 */
7167
Al Viroa39907f2008-03-02 10:31:15 -05007168 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169
7170 if (!mddev) {
7171 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007172 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173 }
7174
NeilBrown1ca69c42012-10-11 13:37:33 +11007175 /* Some actions do not requires the mutex */
7176 switch (cmd) {
7177 case GET_ARRAY_INFO:
7178 if (!mddev->raid_disks && !mddev->external)
7179 err = -ENODEV;
7180 else
7181 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007182 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007183
7184 case GET_DISK_INFO:
7185 if (!mddev->raid_disks && !mddev->external)
7186 err = -ENODEV;
7187 else
7188 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007189 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007190
7191 case SET_DISK_FAULTY:
7192 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007193 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007194
7195 case GET_BITMAP_FILE:
7196 err = get_bitmap_file(mddev, argp);
7197 goto out;
7198
NeilBrown1ca69c42012-10-11 13:37:33 +11007199 }
7200
NeilBrowna7a3f082012-12-11 13:35:54 +11007201 if (cmd == ADD_NEW_DISK)
7202 /* need to ensure md_delayed_delete() has completed */
7203 flush_workqueue(md_misc_wq);
7204
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007205 if (cmd == HOT_REMOVE_DISK)
7206 /* need to ensure recovery thread has run */
7207 wait_event_interruptible_timeout(mddev->sb_wait,
7208 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007209 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007210 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007211 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7212 /* Need to flush page cache, and ensure no-one else opens
7213 * and writes
7214 */
7215 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007216 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007217 mutex_unlock(&mddev->open_mutex);
7218 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007219 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007220 }
NeilBrown065e5192017-04-06 11:16:33 +08007221 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007222 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007223 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007224 mutex_unlock(&mddev->open_mutex);
7225 sync_blockdev(bdev);
7226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 err = mddev_lock(mddev);
7228 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007229 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7230 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007231 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232 }
7233
NeilBrownc02c0ae2012-12-11 13:39:21 +11007234 if (cmd == SET_ARRAY_INFO) {
7235 mdu_array_info_t info;
7236 if (!arg)
7237 memset(&info, 0, sizeof(info));
7238 else if (copy_from_user(&info, argp, sizeof(info))) {
7239 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007240 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007241 }
7242 if (mddev->pers) {
7243 err = update_array_info(mddev, &info);
7244 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007245 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007246 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007248 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007249 }
7250 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007251 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007252 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007253 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007254 }
7255 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007256 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007257 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007258 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007259 }
7260 err = set_array_info(mddev, &info);
7261 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007262 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007263 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007264 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007265 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266 }
7267
7268 /*
7269 * Commands querying/configuring an existing array:
7270 */
NeilBrown32a76272005-06-21 17:17:14 -07007271 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007272 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007273 if ((!mddev->raid_disks && !mddev->external)
7274 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7275 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7276 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007278 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279 }
7280
7281 /*
7282 * Commands even a read-only array can execute:
7283 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007284 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007285 case RESTART_ARRAY_RW:
7286 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007287 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007288
7289 case STOP_ARRAY:
7290 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007291 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007292
7293 case STOP_ARRAY_RO:
7294 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007295 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007296
NeilBrown3ea8929d2013-04-24 11:42:41 +10007297 case HOT_REMOVE_DISK:
7298 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007299 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007300
NeilBrown7ceb17e2013-04-24 11:42:42 +10007301 case ADD_NEW_DISK:
7302 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007303 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007304 * So require mddev->pers and MD_DISK_SYNC.
7305 */
7306 if (mddev->pers) {
7307 mdu_disk_info_t info;
7308 if (copy_from_user(&info, argp, sizeof(info)))
7309 err = -EFAULT;
7310 else if (!(info.state & (1<<MD_DISK_SYNC)))
7311 /* Need to clear read-only for this */
7312 break;
7313 else
7314 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007315 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007316 }
7317 break;
7318
NeilBrownc02c0ae2012-12-11 13:39:21 +11007319 case BLKROSET:
7320 if (get_user(ro, (int __user *)(arg))) {
7321 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007322 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007323 }
7324 err = -EINVAL;
7325
7326 /* if the bdev is going readonly the value of mddev->ro
7327 * does not matter, no writes are coming
7328 */
7329 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007330 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007331
NeilBrownc02c0ae2012-12-11 13:39:21 +11007332 /* are we are already prepared for writes? */
7333 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007334 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007335
7336 /* transitioning to readauto need only happen for
7337 * arrays that call md_write_start
7338 */
7339 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007341 if (err == 0) {
7342 mddev->ro = 2;
7343 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007344 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007345 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007346 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347 }
7348
7349 /*
7350 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007351 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 */
NeilBrown326eb172014-09-30 15:36:28 +10007353 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007354 if (mddev->ro == 2) {
7355 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007356 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007357 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007358 /* mddev_unlock will wake thread */
7359 /* If a device failed while we were read-only, we
7360 * need to make sure the metadata is updated now.
7361 */
Shaohua Li29530792016-12-08 15:48:19 -08007362 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007363 mddev_unlock(mddev);
7364 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007365 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7366 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007367 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007368 }
NeilBrownf91de922005-11-08 21:39:36 -08007369 } else {
7370 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007371 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 }
7374
NeilBrownc02c0ae2012-12-11 13:39:21 +11007375 switch (cmd) {
7376 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007378 mdu_disk_info_t info;
7379 if (copy_from_user(&info, argp, sizeof(info)))
7380 err = -EFAULT;
7381 else
7382 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007383 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007386 case CLUSTERED_DISK_NACK:
7387 if (mddev_is_clustered(mddev))
7388 md_cluster_ops->new_disk_ack(mddev, false);
7389 else
7390 err = -EINVAL;
7391 goto unlock;
7392
NeilBrownc02c0ae2012-12-11 13:39:21 +11007393 case HOT_ADD_DISK:
7394 err = hot_add_disk(mddev, new_decode_dev(arg));
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 RUN_ARRAY:
7398 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007399 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400
NeilBrownc02c0ae2012-12-11 13:39:21 +11007401 case SET_BITMAP_FILE:
7402 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007403 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007404
NeilBrownc02c0ae2012-12-11 13:39:21 +11007405 default:
7406 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007407 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 }
7409
NeilBrown3adc28d2014-09-30 15:46:41 +10007410unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007411 if (mddev->hold_active == UNTIL_IOCTL &&
7412 err != -EINVAL)
7413 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007415out:
NeilBrown065e5192017-04-06 11:16:33 +08007416 if(did_set_md_closing)
7417 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418 return err;
7419}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007420#ifdef CONFIG_COMPAT
7421static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7422 unsigned int cmd, unsigned long arg)
7423{
7424 switch (cmd) {
7425 case HOT_REMOVE_DISK:
7426 case HOT_ADD_DISK:
7427 case SET_DISK_FAULTY:
7428 case SET_BITMAP_FILE:
7429 /* These take in integer arg, do not convert */
7430 break;
7431 default:
7432 arg = (unsigned long)compat_ptr(arg);
7433 break;
7434 }
7435
7436 return md_ioctl(bdev, mode, cmd, arg);
7437}
7438#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439
Al Viroa39907f2008-03-02 10:31:15 -05007440static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441{
7442 /*
7443 * Succeed if we can lock the mddev, which confirms that
7444 * it isn't being stopped right now.
7445 */
NeilBrownfd01b882011-10-11 16:47:53 +11007446 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447 int err;
7448
Yuanhan Liu0c098222012-05-22 13:55:32 +10007449 if (!mddev)
7450 return -ENODEV;
7451
NeilBrownd3374822009-01-09 08:31:10 +11007452 if (mddev->gendisk != bdev->bd_disk) {
7453 /* we are racing with mddev_put which is discarding this
7454 * bd_disk.
7455 */
7456 mddev_put(mddev);
7457 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007458 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007459 /* Then retry the open from the top */
7460 return -ERESTARTSYS;
7461 }
7462 BUG_ON(mddev != bdev->bd_disk->private_data);
7463
NeilBrownc8c00a62009-08-10 12:50:52 +10007464 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465 goto out;
7466
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007467 if (test_bit(MD_CLOSING, &mddev->flags)) {
7468 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007469 err = -ENODEV;
7470 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007471 }
7472
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007474 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007475 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007477 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007479 if (err)
7480 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481 return err;
7482}
7483
Al Virodb2a1442013-05-05 21:52:57 -04007484static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007486 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007488 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007489 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007492
7493static int md_media_changed(struct gendisk *disk)
7494{
NeilBrownfd01b882011-10-11 16:47:53 +11007495 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007496
7497 return mddev->changed;
7498}
7499
7500static int md_revalidate(struct gendisk *disk)
7501{
NeilBrownfd01b882011-10-11 16:47:53 +11007502 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007503
7504 mddev->changed = 0;
7505 return 0;
7506}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007507static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508{
7509 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007510 .open = md_open,
7511 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007512 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007513#ifdef CONFIG_COMPAT
7514 .compat_ioctl = md_compat_ioctl,
7515#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007516 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007517 .media_changed = md_media_changed,
7518 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519};
7520
NeilBrownf72ffdd2014-09-30 14:23:59 +10007521static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522{
NeilBrown2b8bf342011-10-11 16:48:23 +11007523 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 /*
7526 * md_thread is a 'system-thread', it's priority should be very
7527 * high. We avoid resource deadlocks individually in each
7528 * raid personality. (RAID5 does preallocation) We also use RR and
7529 * the very same RT priority as kswapd, thus we will never get
7530 * into a priority inversion deadlock.
7531 *
7532 * we definitely have to have equal or higher priority than
7533 * bdflush, otherwise bdflush will deadlock if there are too
7534 * many dirty RAID5 blocks.
7535 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536
NeilBrown6985c432005-10-19 21:23:47 -07007537 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007538 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539
NeilBrown93588e22005-11-15 00:09:12 -08007540 /* We need to wait INTERRUPTIBLE so that
7541 * we don't add to the load-average.
7542 * That means we need to be sure no signals are
7543 * pending
7544 */
7545 if (signal_pending(current))
7546 flush_signals(current);
7547
7548 wait_event_interruptible_timeout
7549 (thread->wqueue,
7550 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007551 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007552 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553
NeilBrown6c987912011-01-14 09:13:53 +11007554 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007555 if (kthread_should_park())
7556 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007557 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007558 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007560
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561 return 0;
7562}
7563
NeilBrown2b8bf342011-10-11 16:48:23 +11007564void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565{
7566 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007567 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Guoqing Jiangd1d90142017-10-09 10:32:48 +08007568 set_bit(THREAD_WAKEUP, &thread->flags);
7569 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 }
7571}
NeilBrown6c144d32014-09-30 16:15:38 +10007572EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007573
Shaohua Li4ed87312012-10-11 13:34:00 +11007574struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7575 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576{
NeilBrown2b8bf342011-10-11 16:48:23 +11007577 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578
NeilBrown2b8bf342011-10-11 16:48:23 +11007579 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 if (!thread)
7581 return NULL;
7582
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 init_waitqueue_head(&thread->wqueue);
7584
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585 thread->run = run;
7586 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007587 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007588 thread->tsk = kthread_run(md_thread, thread,
7589 "%s_%s",
7590 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007591 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007592 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593 kfree(thread);
7594 return NULL;
7595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596 return thread;
7597}
NeilBrown6c144d32014-09-30 16:15:38 +10007598EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599
NeilBrown2b8bf342011-10-11 16:48:23 +11007600void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007601{
NeilBrown2b8bf342011-10-11 16:48:23 +11007602 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007603 if (!thread)
7604 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007605 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007606 /* Locking ensures that mddev_unlock does not wake_up a
7607 * non-existent thread
7608 */
7609 spin_lock(&pers_lock);
7610 *threadp = NULL;
7611 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007612
7613 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614 kfree(thread);
7615}
NeilBrown6c144d32014-09-30 16:15:38 +10007616EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617
NeilBrownfd01b882011-10-11 16:47:53 +11007618void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619{
NeilBrownb2d444d2005-11-08 21:39:31 -08007620 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007622
NeilBrownde393cd2011-07-28 11:31:48 +10007623 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007624 return;
7625 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007626 if (mddev->degraded)
7627 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007628 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7630 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7631 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007632 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007633 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007634 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635}
NeilBrown6c144d32014-09-30 16:15:38 +10007636EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637
7638/* seq_file implementation /proc/mdstat */
7639
7640static void status_unused(struct seq_file *seq)
7641{
7642 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007643 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644
7645 seq_printf(seq, "unused devices: ");
7646
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007647 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648 char b[BDEVNAME_SIZE];
7649 i++;
7650 seq_printf(seq, "%s ",
7651 bdevname(rdev->bdev,b));
7652 }
7653 if (!i)
7654 seq_printf(seq, "<none>");
7655
7656 seq_printf(seq, "\n");
7657}
7658
NeilBrownf7851be2015-07-02 17:12:58 +10007659static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660{
NeilBrowndd71cf62009-05-07 12:49:35 +10007661 sector_t max_sectors, resync, res;
7662 unsigned long dt, db;
7663 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007664 int scale;
7665 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666
NeilBrownc804cde2012-05-21 09:28:33 +10007667 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7668 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007669 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007671 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672
NeilBrownf7851be2015-07-02 17:12:58 +10007673 resync = mddev->curr_resync;
7674 if (resync <= 3) {
7675 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7676 /* Still cleaning up */
7677 resync = max_sectors;
Nate Daileyd2e2ec82017-11-30 11:33:30 -05007678 } else if (resync > max_sectors)
7679 resync = max_sectors;
7680 else
NeilBrownf7851be2015-07-02 17:12:58 +10007681 resync -= atomic_read(&mddev->recovery_active);
7682
7683 if (resync == 0) {
Guoqing Jiang0357ba22018-07-02 16:26:25 +08007684 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
7685 struct md_rdev *rdev;
7686
7687 rdev_for_each(rdev, mddev)
7688 if (rdev->raid_disk >= 0 &&
7689 !test_bit(Faulty, &rdev->flags) &&
7690 rdev->recovery_offset != MaxSector &&
7691 rdev->recovery_offset) {
7692 seq_printf(seq, "\trecover=REMOTE");
7693 return 1;
7694 }
7695 if (mddev->reshape_position != MaxSector)
7696 seq_printf(seq, "\treshape=REMOTE");
7697 else
7698 seq_printf(seq, "\tresync=REMOTE");
7699 return 1;
7700 }
NeilBrownf7851be2015-07-02 17:12:58 +10007701 if (mddev->recovery_cp < MaxSector) {
7702 seq_printf(seq, "\tresync=PENDING");
7703 return 1;
7704 }
7705 return 0;
7706 }
7707 if (resync < 3) {
7708 seq_printf(seq, "\tresync=DELAYED");
7709 return 1;
7710 }
7711
NeilBrown403df472014-09-30 15:52:29 +10007712 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007713 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007714 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007715 * u32, as those are the requirements for sector_div.
7716 * Thus 'scale' must be at least 10
7717 */
7718 scale = 10;
7719 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007720 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007721 scale++;
7722 }
7723 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007724 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007725
7726 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727 {
NeilBrown4588b422006-03-27 01:18:04 -08007728 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729 seq_printf(seq, "[");
7730 for (i = 0; i < x; i++)
7731 seq_printf(seq, "=");
7732 seq_printf(seq, ">");
7733 for (i = 0; i < y; i++)
7734 seq_printf(seq, ".");
7735 seq_printf(seq, "] ");
7736 }
NeilBrown4588b422006-03-27 01:18:04 -08007737 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007738 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7739 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007740 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7741 "check" :
7742 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7743 "resync" : "recovery"))),
7744 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007745 (unsigned long long) resync/2,
7746 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007747
7748 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007749 * dt: time from mark until now
7750 * db: blocks written from mark until now
7751 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007752 *
7753 * rt is a sector_t, so could be 32bit or 64bit.
7754 * So we divide before multiply in case it is 32bit and close
7755 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007756 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007757 * near the end of resync when the number of remaining sectors
7758 * is close to 'db'.
7759 * We then divide rt by 32 after multiplying by db to compensate.
7760 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007761 */
7762 dt = ((jiffies - mddev->resync_mark) / HZ);
7763 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007764 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7765 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766
NeilBrowndd71cf62009-05-07 12:49:35 +10007767 rt = max_sectors - resync; /* number of remaining sectors */
7768 sector_div(rt, db/32+1);
7769 rt *= dt;
7770 rt >>= 5;
7771
7772 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7773 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774
NeilBrownff4e8d92006-07-10 04:44:16 -07007775 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007776 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007777}
7778
7779static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7780{
7781 struct list_head *tmp;
7782 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007783 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007784
7785 if (l >= 0x10000)
7786 return NULL;
7787 if (!l--)
7788 /* header */
7789 return (void*)1;
7790
7791 spin_lock(&all_mddevs_lock);
7792 list_for_each(tmp,&all_mddevs)
7793 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007794 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 mddev_get(mddev);
7796 spin_unlock(&all_mddevs_lock);
7797 return mddev;
7798 }
7799 spin_unlock(&all_mddevs_lock);
7800 if (!l--)
7801 return (void*)2;/* tail */
7802 return NULL;
7803}
7804
7805static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7806{
7807 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007808 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007809
Linus Torvalds1da177e2005-04-16 15:20:36 -07007810 ++*pos;
7811 if (v == (void*)2)
7812 return NULL;
7813
7814 spin_lock(&all_mddevs_lock);
7815 if (v == (void*)1)
7816 tmp = all_mddevs.next;
7817 else
7818 tmp = mddev->all_mddevs.next;
7819 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007820 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821 else {
7822 next_mddev = (void*)2;
7823 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825 spin_unlock(&all_mddevs_lock);
7826
7827 if (v != (void*)1)
7828 mddev_put(mddev);
7829 return next_mddev;
7830
7831}
7832
7833static void md_seq_stop(struct seq_file *seq, void *v)
7834{
NeilBrownfd01b882011-10-11 16:47:53 +11007835 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836
7837 if (mddev && v != (void*)1 && v != (void*)2)
7838 mddev_put(mddev);
7839}
7840
7841static int md_seq_show(struct seq_file *seq, void *v)
7842{
NeilBrownfd01b882011-10-11 16:47:53 +11007843 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007844 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007845 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007846
7847 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007848 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007849 seq_printf(seq, "Personalities : ");
7850 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007851 list_for_each_entry(pers, &pers_list, list)
7852 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853
7854 spin_unlock(&pers_lock);
7855 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007856 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857 return 0;
7858 }
7859 if (v == (void*)2) {
7860 status_unused(seq);
7861 return 0;
7862 }
7863
NeilBrown36d091f2014-12-15 12:56:58 +11007864 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7866 seq_printf(seq, "%s : %sactive", mdname(mddev),
7867 mddev->pers ? "" : "in");
7868 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007869 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007871 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007872 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873 seq_printf(seq, " %s", mddev->pers->name);
7874 }
7875
Andre Nolldd8ac332009-03-31 14:33:13 +11007876 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007877 rcu_read_lock();
7878 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879 char b[BDEVNAME_SIZE];
7880 seq_printf(seq, " %s[%d]",
7881 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007882 if (test_bit(WriteMostly, &rdev->flags))
7883 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007884 if (test_bit(Journal, &rdev->flags))
7885 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007886 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887 seq_printf(seq, "(F)");
7888 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007889 }
7890 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007891 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007892 if (test_bit(Replacement, &rdev->flags))
7893 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007894 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007895 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007896 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897
7898 if (!list_empty(&mddev->disks)) {
7899 if (mddev->pers)
7900 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007901 (unsigned long long)
7902 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903 else
7904 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007905 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007906 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007907 if (mddev->persistent) {
7908 if (mddev->major_version != 0 ||
7909 mddev->minor_version != 90) {
7910 seq_printf(seq," super %d.%d",
7911 mddev->major_version,
7912 mddev->minor_version);
7913 }
NeilBrowne6910632008-02-06 01:39:51 -08007914 } else if (mddev->external)
7915 seq_printf(seq, " super external:%s",
7916 mddev->metadata_type);
7917 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007918 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919
7920 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007921 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007922 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007923 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007924 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007925 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007926 }
NeilBrown32a76272005-06-21 17:17:14 -07007927 } else
7928 seq_printf(seq, "\n ");
7929
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007930 md_bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931
7932 seq_printf(seq, "\n");
7933 }
NeilBrown36d091f2014-12-15 12:56:58 +11007934 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007935
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936 return 0;
7937}
7938
Jan Engelhardt110518b2009-05-07 12:49:37 +10007939static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940 .start = md_seq_start,
7941 .next = md_seq_next,
7942 .stop = md_seq_stop,
7943 .show = md_seq_show,
7944};
7945
7946static int md_seq_open(struct inode *inode, struct file *file)
7947{
Kay Sieversf1514632011-07-12 20:48:39 +02007948 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949 int error;
7950
7951 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007952 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007953 return error;
7954
7955 seq = file->private_data;
7956 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957 return error;
7958}
7959
NeilBrowne2f23b62014-04-09 14:33:51 +10007960static int md_unloading;
Al Viroafc9a422017-07-03 06:39:46 -04007961static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
NeilBrownd7603b72006-01-06 00:20:30 -08007962{
Kay Sieversf1514632011-07-12 20:48:39 +02007963 struct seq_file *seq = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -04007964 __poll_t mask;
NeilBrownd7603b72006-01-06 00:20:30 -08007965
NeilBrowne2f23b62014-04-09 14:33:51 +10007966 if (md_unloading)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007967 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007968 poll_wait(filp, &md_event_waiters, wait);
7969
7970 /* always allow read */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007971 mask = EPOLLIN | EPOLLRDNORM;
NeilBrownd7603b72006-01-06 00:20:30 -08007972
Kay Sieversf1514632011-07-12 20:48:39 +02007973 if (seq->poll_event != atomic_read(&md_event_count))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007974 mask |= EPOLLERR | EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007975 return mask;
7976}
7977
Arjan van de Venfa027c22007-02-12 00:55:33 -08007978static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007979 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007980 .open = md_seq_open,
7981 .read = seq_read,
7982 .llseek = seq_lseek,
Cihangir Akturk26e13042017-07-29 19:52:45 +03007983 .release = seq_release,
NeilBrownd7603b72006-01-06 00:20:30 -08007984 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985};
7986
NeilBrown84fc4b52011-10-11 16:49:58 +11007987int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007988{
NeilBrown9d487392016-11-02 14:16:49 +11007989 pr_debug("md: %s personality registered for level %d\n",
7990 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007992 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993 spin_unlock(&pers_lock);
7994 return 0;
7995}
NeilBrown6c144d32014-09-30 16:15:38 +10007996EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997
NeilBrown84fc4b52011-10-11 16:49:58 +11007998int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007999{
NeilBrown9d487392016-11-02 14:16:49 +11008000 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008001 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008002 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003 spin_unlock(&pers_lock);
8004 return 0;
8005}
NeilBrown6c144d32014-09-30 16:15:38 +10008006EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007
NeilBrown6022e752015-08-13 12:32:55 +10008008int register_md_cluster_operations(struct md_cluster_operations *ops,
8009 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008010{
NeilBrown6022e752015-08-13 12:32:55 +10008011 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008012 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008013 if (md_cluster_ops != NULL)
8014 ret = -EALREADY;
8015 else {
8016 md_cluster_ops = ops;
8017 md_cluster_mod = module;
8018 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008019 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008020 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008021}
8022EXPORT_SYMBOL(register_md_cluster_operations);
8023
8024int unregister_md_cluster_operations(void)
8025{
8026 spin_lock(&pers_lock);
8027 md_cluster_ops = NULL;
8028 spin_unlock(&pers_lock);
8029 return 0;
8030}
8031EXPORT_SYMBOL(unregister_md_cluster_operations);
8032
8033int md_setup_cluster(struct mddev *mddev, int nodes)
8034{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008035 if (!md_cluster_ops)
8036 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008037 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008038 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008039 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11008040 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008041 spin_unlock(&pers_lock);
8042 return -ENOENT;
8043 }
8044 spin_unlock(&pers_lock);
8045
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05008046 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008047}
8048
8049void md_cluster_stop(struct mddev *mddev)
8050{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05008051 if (!md_cluster_ops)
8052 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008053 md_cluster_ops->leave(mddev);
8054 module_put(md_cluster_mod);
8055}
8056
NeilBrownfd01b882011-10-11 16:47:53 +11008057static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008058{
NeilBrownf72ffdd2014-09-30 14:23:59 +10008059 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008060 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11008061 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008062
8063 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10008064 rcu_read_lock();
8065 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008066 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Michael Callahan59767fb2018-07-18 04:47:37 -07008067 curr_events = (int)part_stat_read_accum(&disk->part0, sectors) -
NeilBrowneea1bf32009-03-31 14:27:02 +11008068 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07008069 /* sync IO will cause sync_io to increase before the disk_stats
8070 * as sync_io is counted when a request starts, and
8071 * disk_stats is counted when it completes.
8072 * So resync activity will cause curr_events to be smaller than
8073 * when there was no such activity.
8074 * non-sync IO will cause disk_stat to increase without
8075 * increasing sync_io so curr_events will (eventually)
8076 * be larger than it was before. Once it becomes
8077 * substantially larger, the test below will cause
8078 * the array to appear non-idle, and resync will slow
8079 * down.
8080 * If there is a lot of outstanding resync activity when
8081 * we set last_event to curr_events, then all that activity
8082 * completing might cause the array to appear non-idle
8083 * and resync will be slowed down even though there might
8084 * not have been non-resync activity. This will only
8085 * happen once though. 'last_events' will soon reflect
8086 * the state where there is little or no outstanding
8087 * resync requests, and further resync activity will
8088 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08008089 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090 */
NeilBrowneea1bf32009-03-31 14:27:02 +11008091 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092 rdev->last_events = curr_events;
8093 idle = 0;
8094 }
8095 }
NeilBrown4b809912008-07-21 17:05:25 +10008096 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008097 return idle;
8098}
8099
NeilBrownfd01b882011-10-11 16:47:53 +11008100void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008101{
8102 /* another "blocks" (512byte) blocks have been synced */
8103 atomic_sub(blocks, &mddev->recovery_active);
8104 wake_up(&mddev->recovery_wait);
8105 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07008106 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08008107 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008108 md_wakeup_thread(mddev->thread);
8109 // stop recovery, signal do_sync ....
8110 }
8111}
NeilBrown6c144d32014-09-30 16:15:38 +10008112EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008113
NeilBrown06d91a52005-06-21 17:17:12 -07008114/* md_write_start(mddev, bi)
8115 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07008116 * in superblock) before writing, schedule a superblock update
8117 * and wait for it to complete.
NeilBrowncc27b0c2017-06-05 16:49:39 +10008118 * A return value of 'false' means that the write wasn't recorded
8119 * and cannot proceed as the array is being suspend.
NeilBrown06d91a52005-06-21 17:17:12 -07008120 */
NeilBrowncc27b0c2017-06-05 16:49:39 +10008121bool md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008122{
Neil Brown0fd62b82008-06-28 08:31:36 +10008123 int did_change = 0;
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008124
NeilBrown06d91a52005-06-21 17:17:12 -07008125 if (bio_data_dir(bi) != WRITE)
NeilBrowncc27b0c2017-06-05 16:49:39 +10008126 return true;
NeilBrown06d91a52005-06-21 17:17:12 -07008127
NeilBrownf91de922005-11-08 21:39:36 -08008128 BUG_ON(mddev->ro == 1);
8129 if (mddev->ro == 2) {
8130 /* need to switch to read/write */
8131 mddev->ro = 0;
8132 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8133 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08008134 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008135 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08008136 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008137 rcu_read_lock();
8138 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11008139 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07008140 if (mddev->safemode == 1)
8141 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11008142 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
NeilBrown81fe48e2017-08-08 16:56:36 +10008143 if (mddev->in_sync || mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11008144 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07008145 if (mddev->in_sync) {
8146 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008147 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8148 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07008149 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008150 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07008151 }
NeilBrown85572d72014-12-15 12:56:56 +11008152 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07008153 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008154 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10008155 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008156 sysfs_notify_dirent_safe(mddev->sysfs_state);
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008157 if (!mddev->has_superblocks)
8158 return true;
NeilBrown09a44cc2008-05-23 13:04:36 -07008159 wait_event(mddev->sb_wait,
NeilBrownd47c8ad2017-10-05 16:23:16 +11008160 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8161 mddev->suspended);
NeilBrowncc27b0c2017-06-05 16:49:39 +10008162 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8163 percpu_ref_put(&mddev->writes_pending);
8164 return false;
8165 }
8166 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008167}
NeilBrown6c144d32014-09-30 16:15:38 +10008168EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169
NeilBrown49728052017-03-15 14:05:12 +11008170/* md_write_inc can only be called when md_write_start() has
8171 * already been called at least once of the current request.
8172 * It increments the counter and is useful when a single request
8173 * is split into several parts. Each part causes an increment and
8174 * so needs a matching md_write_end().
8175 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8176 * a spinlocked region.
8177 */
8178void md_write_inc(struct mddev *mddev, struct bio *bi)
8179{
8180 if (bio_data_dir(bi) != WRITE)
8181 return;
8182 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008183 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008184}
8185EXPORT_SYMBOL(md_write_inc);
8186
NeilBrownfd01b882011-10-11 16:47:53 +11008187void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188{
NeilBrown4ad23a972017-03-15 14:05:14 +11008189 percpu_ref_put(&mddev->writes_pending);
8190
8191 if (mddev->safemode == 2)
8192 md_wakeup_thread(mddev->thread);
8193 else if (mddev->safemode_delay)
8194 /* The roundup() ensures this only performs locking once
8195 * every ->safemode_delay jiffies
8196 */
8197 mod_timer(&mddev->safemode_timer,
8198 roundup(jiffies, mddev->safemode_delay) +
8199 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008200}
NeilBrown4ad23a972017-03-15 14:05:14 +11008201
NeilBrown6c144d32014-09-30 16:15:38 +10008202EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008203
NeilBrown2a2275d2007-01-26 00:57:11 -08008204/* md_allow_write(mddev)
8205 * Calling this ensures that the array is marked 'active' so that writes
8206 * may proceed without blocking. It is important to call this before
8207 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8208 * Must be called with mddev_lock held.
8209 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008210void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008211{
8212 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008213 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008214 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008215 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008216 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008217 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008218
NeilBrown85572d72014-12-15 12:56:56 +11008219 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008220 if (mddev->in_sync) {
8221 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008222 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8223 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008224 if (mddev->safemode_delay &&
8225 mddev->safemode == 0)
8226 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008227 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008228 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008229 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008230 /* wait for the dirty state to be recorded in the metadata */
8231 wait_event(mddev->sb_wait,
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008232 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008233 } else
NeilBrown85572d72014-12-15 12:56:56 +11008234 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008235}
8236EXPORT_SYMBOL_GPL(md_allow_write);
8237
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238#define SYNC_MARKS 10
8239#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008240#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008241void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242{
Shaohua Li4ed87312012-10-11 13:34:00 +11008243 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008244 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008245 unsigned int currspeed = 0,
8246 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008247 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008249 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008250 sector_t mark_cnt[SYNC_MARKS];
8251 int last_mark,m;
8252 struct list_head *tmp;
8253 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008254 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008255 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008256 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008257 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008258 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259
8260 /* just incase thread restarts... */
Song Liud5d885f2017-11-19 22:17:01 -08008261 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8262 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263 return;
NeilBrown3991b312014-05-28 13:39:23 +10008264 if (mddev->ro) {/* never try to sync a read-only array */
8265 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008266 return;
NeilBrown3991b312014-05-28 13:39:23 +10008267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008268
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008269 if (mddev_is_clustered(mddev)) {
8270 ret = md_cluster_ops->resync_start(mddev);
8271 if (ret)
8272 goto skip;
8273
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008274 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008275 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8276 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8277 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8278 && ((unsigned long long)mddev->curr_resync_completed
8279 < (unsigned long long)mddev->resync_max_sectors))
8280 goto skip;
8281 }
8282
NeilBrown61df9d92006-10-03 01:15:57 -07008283 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008284 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008285 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008286 action = "check";
8287 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008288 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008289 action = "repair";
8290 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008291 desc = "resync";
8292 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8293 desc = "reshape";
8294 else
8295 desc = "recovery";
8296
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008297 mddev->last_sync_action = action ?: desc;
8298
Linus Torvalds1da177e2005-04-16 15:20:36 -07008299 /* we overload curr_resync somewhat here.
8300 * 0 == not engaged in resync at all
8301 * 2 == checking that there is no conflict with another sync
8302 * 1 == like 2, but have yielded to allow conflicting resync to
8303 * commense
8304 * other == active in resync - this many blocks
8305 *
8306 * Before starting a resync we must have set curr_resync to
8307 * 2, and then checked that every "conflicting" array has curr_resync
8308 * less than ours. When we find one that is the same or higher
8309 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8310 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8311 * This will mean we have to start checking from the beginning again.
8312 *
8313 */
8314
8315 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008316 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317 mddev->curr_resync = 2;
8318
8319 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008320 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008321 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008322 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008323 if (mddev2 == mddev)
8324 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008325 if (!mddev->parallel_resync
8326 && mddev2->curr_resync
8327 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008328 DEFINE_WAIT(wq);
8329 if (mddev < mddev2 && mddev->curr_resync == 2) {
8330 /* arbitrarily yield */
8331 mddev->curr_resync = 1;
8332 wake_up(&resync_wait);
8333 }
8334 if (mddev > mddev2 && mddev->curr_resync == 1)
8335 /* no need to wait here, we can wait the next
8336 * time 'round when curr_resync == 2
8337 */
8338 continue;
NeilBrown97441972008-09-19 11:49:54 +10008339 /* We need to wait 'interruptible' so as not to
8340 * contribute to the load average, and not to
8341 * be caught by 'softlockup'
8342 */
8343 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008344 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008345 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008346 if (mddev2_minor != mddev2->md_minor) {
8347 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008348 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8349 desc, mdname(mddev),
8350 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008352 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008353 if (signal_pending(current))
8354 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355 schedule();
8356 finish_wait(&resync_wait, &wq);
8357 goto try_again;
8358 }
8359 finish_wait(&resync_wait, &wq);
8360 }
8361 }
8362 } while (mddev->curr_resync < 2);
8363
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008364 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008365 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008366 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008367 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008368 */
8369 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008370 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008371 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008372 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8373 j = mddev->resync_min;
8374 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008375 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008376
NeilBrownccfcc3c2006-03-27 01:18:09 -08008377 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10008378 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008379 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008381 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008382 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008383 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008384 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008385 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008386 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008387 !test_bit(Faulty, &rdev->flags) &&
8388 !test_bit(In_sync, &rdev->flags) &&
8389 rdev->recovery_offset < j)
8390 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008391 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008392
8393 /* If there is a bitmap, we need to make sure all
8394 * writes that started before we added a spare
8395 * complete before we start doing a recovery.
8396 * Otherwise the write might complete and (via
8397 * bitmap_endwrite) set a bit in the bitmap after the
8398 * recovery has checked that bit and skipped that
8399 * region.
8400 */
8401 if (mddev->bitmap) {
8402 mddev->pers->quiesce(mddev, 1);
8403 mddev->pers->quiesce(mddev, 0);
8404 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008406
NeilBrown9d487392016-11-02 14:16:49 +11008407 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8408 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8409 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8410 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008411
NeilBrowneea1bf32009-03-31 14:27:02 +11008412 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008413
NeilBrown57afd892005-06-21 17:17:13 -07008414 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415 for (m = 0; m < SYNC_MARKS; m++) {
8416 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008417 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008418 }
8419 last_mark = 0;
8420 mddev->resync_mark = mark[last_mark];
8421 mddev->resync_mark_cnt = mark_cnt[last_mark];
8422
8423 /*
8424 * Tune reconstruction:
8425 */
8426 window = 32*(PAGE_SIZE/512);
NeilBrown9d487392016-11-02 14:16:49 +11008427 pr_debug("md: using %dk window, over a total of %lluk.\n",
8428 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008429
8430 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008431 last_check = 0;
8432
8433 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008434 pr_debug("md: resuming %s of %s from checkpoint.\n",
8435 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008436 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008437 } else
8438 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008439 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008440 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8441 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008442 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443
majianpeng7c2c57c2012-07-03 12:12:26 +10008444 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008446 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008447
NeilBrown57afd892005-06-21 17:17:13 -07008448 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008449
NeilBrown7a91ee12009-05-26 12:57:21 +10008450 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8451 ((mddev->curr_resync > mddev->curr_resync_completed &&
8452 (mddev->curr_resync - mddev->curr_resync_completed)
8453 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008454 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008455 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008456 >= mddev->resync_max - mddev->curr_resync_completed ||
8457 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008458 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008459 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008460 wait_event(mddev->recovery_wait,
8461 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008462 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008463 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8464 j > mddev->recovery_cp)
8465 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008466 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008467 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008468 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008469 }
NeilBrownacb180b2009-04-14 16:28:34 +10008470
NeilBrownc91abf52013-11-19 12:02:01 +11008471 while (j >= mddev->resync_max &&
8472 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008473 /* As this condition is controlled by user-space,
8474 * we can block indefinitely, so use '_interruptible'
8475 * to avoid triggering warnings.
8476 */
8477 flush_signals(current); /* just in case */
8478 wait_event_interruptible(mddev->recovery_wait,
8479 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008480 || test_bit(MD_RECOVERY_INTR,
8481 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008482 }
NeilBrownacb180b2009-04-14 16:28:34 +10008483
NeilBrownc91abf52013-11-19 12:02:01 +11008484 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8485 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008486
NeilBrown09314792015-02-19 16:04:40 +11008487 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008488 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008489 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008490 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008491 }
NeilBrown57afd892005-06-21 17:17:13 -07008492
8493 if (!skipped) { /* actual IO requested */
8494 io_sectors += sectors;
8495 atomic_add(sectors, &mddev->recovery_active);
8496 }
8497
NeilBrowne875ece2011-07-28 11:39:24 +10008498 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8499 break;
8500
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008502 if (j > max_sectors)
8503 /* when skipping, extra large numbers can be returned. */
8504 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008505 if (j > 2)
8506 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008507 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008508 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008509 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008510 * visible in /proc/mdstat
8511 */
8512 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008513
8514 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008515 continue;
8516
NeilBrown57afd892005-06-21 17:17:13 -07008517 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008518 repeat:
8519 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8520 /* step marks */
8521 int next = (last_mark+1) % SYNC_MARKS;
8522
8523 mddev->resync_mark = mark[next];
8524 mddev->resync_mark_cnt = mark_cnt[next];
8525 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008526 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527 last_mark = next;
8528 }
8529
NeilBrownc91abf52013-11-19 12:02:01 +11008530 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8531 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008532
8533 /*
8534 * this loop exits only if either when we are slower than
8535 * the 'hard' speed limit, or the system was IO-idle for
8536 * a jiffy.
8537 * the system might be non-idle CPU-wise, but we only care
8538 * about not overloading the IO subsystem. (things like an
8539 * e2fsck being done on the RAID array should execute fast)
8540 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008541 cond_resched();
8542
Xiao Niac7e50a2014-08-07 09:37:41 -04008543 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8544 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008545 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546
NeilBrown88202a02006-01-06 00:21:36 -08008547 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008548 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008549 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008550 goto repeat;
8551 }
NeilBrownac8fa412015-02-19 16:55:00 +11008552 if (!is_mddev_idle(mddev, 0)) {
8553 /*
8554 * Give other IO more of a chance.
8555 * The faster the devices, the less we wait.
8556 */
8557 wait_event(mddev->recovery_wait,
8558 !atomic_read(&mddev->recovery_active));
8559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008560 }
8561 }
NeilBrown9d487392016-11-02 14:16:49 +11008562 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8563 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8564 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565 /*
8566 * this also signals 'finished resyncing' to md_stop
8567 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008568 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008569 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8570
NeilBrown5ed1df22015-07-24 13:27:08 +10008571 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8572 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008573 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008574 mddev->curr_resync_completed = mddev->curr_resync;
8575 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8576 }
NeilBrown09314792015-02-19 16:04:40 +11008577 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008578
NeilBrowndfc70642008-05-23 13:04:39 -07008579 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008580 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008581 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8582 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8583 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008584 pr_debug("md: checkpointing %s of %s.\n",
8585 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008586 if (test_bit(MD_RECOVERY_ERROR,
8587 &mddev->recovery))
8588 mddev->recovery_cp =
8589 mddev->curr_resync_completed;
8590 else
8591 mddev->recovery_cp =
8592 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008593 }
8594 } else
8595 mddev->recovery_cp = MaxSector;
8596 } else {
8597 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8598 mddev->curr_resync = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11008599 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8600 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
8601 rcu_read_lock();
8602 rdev_for_each_rcu(rdev, mddev)
8603 if (rdev->raid_disk >= 0 &&
8604 mddev->delta_disks >= 0 &&
8605 !test_bit(Journal, &rdev->flags) &&
8606 !test_bit(Faulty, &rdev->flags) &&
8607 !test_bit(In_sync, &rdev->flags) &&
8608 rdev->recovery_offset < mddev->curr_resync)
8609 rdev->recovery_offset = mddev->curr_resync;
8610 rcu_read_unlock();
8611 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008614 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008615 /* set CHANGE_PENDING here since maybe another update is needed,
8616 * so other nodes are informed. It should be harmless for normal
8617 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008618 set_mask_bits(&mddev->sb_flags, 0,
8619 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008620
BingJing Chang88763912018-02-22 13:34:46 +08008621 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8622 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8623 mddev->delta_disks > 0 &&
8624 mddev->pers->finish_reshape &&
8625 mddev->pers->size &&
8626 mddev->queue) {
8627 mddev_lock_nointr(mddev);
8628 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
8629 mddev_unlock(mddev);
8630 set_capacity(mddev->gendisk, mddev->array_sectors);
8631 revalidate_disk(mddev->gendisk);
8632 }
8633
NeilBrown23da4222014-12-15 12:57:01 +11008634 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008635 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8636 /* We completed so min/max setting can be forgotten if used. */
8637 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8638 mddev->resync_min = 0;
8639 mddev->resync_max = MaxSector;
8640 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8641 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008642 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008643 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008644 spin_unlock(&mddev->lock);
8645
Linus Torvalds1da177e2005-04-16 15:20:36 -07008646 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008648 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649}
NeilBrown29269552006-03-27 01:18:10 -08008650EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008651
NeilBrown746d3202013-04-24 11:42:41 +10008652static int remove_and_add_spares(struct mddev *mddev,
8653 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008654{
NeilBrown3cb03002011-10-11 16:45:26 +11008655 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008656 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008657 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008658 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008659
NeilBrown39772f02018-02-03 09:19:30 +11008660 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
8661 /* Mustn't remove devices when resync thread is running */
8662 return 0;
8663
NeilBrownd787be42016-06-02 16:19:53 +10008664 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008665 if ((this == NULL || rdev == this) &&
8666 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008667 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008668 test_bit(Faulty, &rdev->flags) &&
8669 atomic_read(&rdev->nr_pending)==0) {
8670 /* Faulty non-Blocked devices with nr_pending == 0
8671 * never get nr_pending incremented,
8672 * never get Faulty cleared, and never get Blocked set.
8673 * So we can synchronize_rcu now rather than once per device
8674 */
8675 remove_some = true;
8676 set_bit(RemoveSynchronized, &rdev->flags);
8677 }
8678 }
8679
8680 if (remove_some)
8681 synchronize_rcu();
8682 rdev_for_each(rdev, mddev) {
8683 if ((this == NULL || rdev == this) &&
8684 rdev->raid_disk >= 0 &&
8685 !test_bit(Blocked, &rdev->flags) &&
8686 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008687 (!test_bit(In_sync, &rdev->flags) &&
8688 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008689 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008690 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008691 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008692 sysfs_unlink_rdev(mddev, rdev);
NeilBrown011abdc2018-04-26 14:46:29 +10008693 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008694 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008695 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008696 }
8697 }
NeilBrownd787be42016-06-02 16:19:53 +10008698 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8699 clear_bit(RemoveSynchronized, &rdev->flags);
8700 }
8701
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008702 if (removed && mddev->kobj.sd)
8703 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008704
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008705 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008706 goto no_add;
8707
NeilBrowndafb20f2012-03-19 12:46:39 +11008708 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008709 if (this && this != rdev)
8710 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008711 if (test_bit(Candidate, &rdev->flags))
8712 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008713 if (rdev->raid_disk >= 0 &&
8714 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008715 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008716 !test_bit(Faulty, &rdev->flags))
8717 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008718 if (rdev->raid_disk >= 0)
8719 continue;
8720 if (test_bit(Faulty, &rdev->flags))
8721 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008722 if (!test_bit(Journal, &rdev->flags)) {
8723 if (mddev->ro &&
8724 ! (rdev->saved_raid_disk >= 0 &&
8725 !test_bit(Bitmap_sync, &rdev->flags)))
8726 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008727
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008728 rdev->recovery_offset = 0;
8729 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008730 if (mddev->pers->
8731 hot_add_disk(mddev, rdev) == 0) {
8732 if (sysfs_link_rdev(mddev, rdev))
8733 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008734 if (!test_bit(Journal, &rdev->flags))
8735 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008736 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08008737 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008738 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008739 }
NeilBrown746d3202013-04-24 11:42:41 +10008740no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008741 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08008742 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008743 return spares;
8744}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008745
NeilBrownac05f252014-09-30 08:10:42 +10008746static void md_start_sync(struct work_struct *ws)
8747{
8748 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008749
NeilBrownac05f252014-09-30 08:10:42 +10008750 mddev->sync_thread = md_register_thread(md_do_sync,
8751 mddev,
8752 "resync");
8753 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11008754 pr_warn("%s: could not start resync thread...\n",
8755 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008756 /* leave the spares where they are, it shouldn't hurt */
8757 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8758 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8759 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8760 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8761 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008762 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008763 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8764 &mddev->recovery))
8765 if (mddev->sysfs_action)
8766 sysfs_notify_dirent_safe(mddev->sysfs_action);
8767 } else
8768 md_wakeup_thread(mddev->sync_thread);
8769 sysfs_notify_dirent_safe(mddev->sysfs_action);
8770 md_new_event(mddev);
8771}
8772
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773/*
8774 * This routine is regularly called by all per-raid-array threads to
8775 * deal with generic issues like resync and super-block update.
8776 * Raid personalities that don't have a thread (linear/raid0) do not
8777 * need this as they never do any recovery or update the superblock.
8778 *
8779 * It does not do any resync itself, but rather "forks" off other threads
8780 * to do that as needed.
8781 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8782 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008783 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008784 * and wakeups up this thread which will reap the thread and finish up.
8785 * This thread also removes any faulty devices (with nr_pending == 0).
8786 *
8787 * The overall approach is:
8788 * 1/ if the superblock needs updating, update it.
8789 * 2/ If a recovery thread is running, don't do anything else.
8790 * 3/ If recovery has finished, clean up, possibly marking spares active.
8791 * 4/ If there are any faulty devices, remove them.
8792 * 5/ If array is degraded, try to add spares devices
8793 * 6/ If array has spares or is not in-sync, start a resync thread.
8794 */
NeilBrownfd01b882011-10-11 16:47:53 +11008795void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008796{
NeilBrown059421e2018-10-03 15:04:41 +10008797 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
8798 /* Write superblock - thread that called mddev_suspend()
8799 * holds reconfig_mutex for us.
8800 */
8801 set_bit(MD_UPDATING_SB, &mddev->flags);
8802 smp_mb__after_atomic();
8803 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
8804 md_update_sb(mddev, 0);
8805 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
8806 wake_up(&mddev->sb_wait);
8807 }
8808
Jonathan Brassow68866e42011-06-08 15:10:08 +10008809 if (mddev->suspended)
8810 return;
8811
NeilBrown5f404022005-06-21 17:17:16 -07008812 if (mddev->bitmap)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008813 md_bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008814
NeilBrownfca4d842005-06-21 17:17:11 -07008815 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008816 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11008817 pr_debug("md: %s in immediate safe mode\n",
8818 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07008819 mddev->safemode = 2;
8820 }
8821 flush_signals(current);
8822 }
8823
NeilBrownc89a8ee2008-08-05 15:54:13 +10008824 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8825 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008826 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08008827 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008828 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008829 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008830 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11008831 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07008832 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008833 ))
8834 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008835
NeilBrowndf5b89b2006-03-27 01:18:20 -08008836 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008837 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008838
Shaohua Liafc1f552017-08-11 20:34:45 -07008839 if (!mddev->external && mddev->safemode == 1)
NeilBrown33182d12017-08-08 16:56:36 +10008840 mddev->safemode = 0;
8841
NeilBrownc89a8ee2008-08-05 15:54:13 +10008842 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008843 struct md_rdev *rdev;
8844 if (!mddev->external && mddev->in_sync)
8845 /* 'Blocked' flag not needed as failed devices
8846 * will be recorded if array switched to read/write.
8847 * Leaving it set will prevent the device
8848 * from being removed.
8849 */
8850 rdev_for_each(rdev, mddev)
8851 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008852 /* On a read-only array we can:
8853 * - remove failed devices
8854 * - add already-in_sync devices if the array itself
8855 * is in-sync.
8856 * As we only add devices that are already in-sync,
8857 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008858 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008859 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008860 /* There is no thread, but we need to call
8861 * ->spare_active and clear saved_raid_disk
8862 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008863 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008864 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008865 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008866 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08008867 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008868 goto unlock;
8869 }
8870
Guoqing Jiang659b2542015-12-21 10:50:59 +11008871 if (mddev_is_clustered(mddev)) {
8872 struct md_rdev *rdev;
8873 /* kick the device if another node issued a
8874 * remove disk.
8875 */
8876 rdev_for_each(rdev, mddev) {
8877 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8878 rdev->raid_disk < 0)
8879 md_kick_rdev_from_array(rdev);
8880 }
8881 }
8882
NeilBrown6497709b2017-03-15 14:05:14 +11008883 if (!mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11008884 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11008885 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11008886 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07008887 }
NeilBrownfca4d842005-06-21 17:17:11 -07008888
Shaohua Li29530792016-12-08 15:48:19 -08008889 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07008890 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008891
Linus Torvalds1da177e2005-04-16 15:20:36 -07008892 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8893 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8894 /* resync/recovery still happening */
8895 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8896 goto unlock;
8897 }
8898 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008899 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900 goto unlock;
8901 }
Neil Brown72a23c22008-06-28 08:31:41 +10008902 /* Set RUNNING before clearing NEEDED to avoid
8903 * any transients in the value of "sync_action".
8904 */
NeilBrown72f36d52012-10-11 14:25:57 +11008905 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008906 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008907 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008908 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008909 /* Clear some bits that don't mean anything, but
8910 * might be left set
8911 */
NeilBrown24dd4692005-11-08 21:39:26 -08008912 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8913 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008914
NeilBrowned209582012-04-24 10:23:14 +10008915 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8916 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008917 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008918 /* no recovery is running.
8919 * remove any failed drives, then
8920 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008921 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008922 * the personality to fail the re-add.
8923 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008924
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008925 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008926 if (mddev->pers->check_reshape == NULL ||
8927 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008928 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008929 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008930 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008931 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008932 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008933 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8934 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008935 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008936 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008937 } else if (mddev->recovery_cp < MaxSector) {
8938 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008939 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008940 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8941 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008942 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008943
Linus Torvalds1da177e2005-04-16 15:20:36 -07008944 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008945 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008946 /* We are adding a device or devices to an array
8947 * which has the bitmap stored on all devices.
8948 * So make sure all bitmap pages get written
8949 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008950 md_bitmap_write_all(mddev->bitmap);
NeilBrowna654b9d82005-06-21 17:17:27 -07008951 }
NeilBrownac05f252014-09-30 08:10:42 +10008952 INIT_WORK(&mddev->del_work, md_start_sync);
8953 queue_work(md_misc_wq, &mddev->del_work);
8954 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955 }
NeilBrownac05f252014-09-30 08:10:42 +10008956 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008957 if (!mddev->sync_thread) {
8958 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008959 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008960 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8961 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008962 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008963 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008964 }
NeilBrownac05f252014-09-30 08:10:42 +10008965 unlock:
8966 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008967 mddev_unlock(mddev);
8968 }
8969}
NeilBrown6c144d32014-09-30 16:15:38 +10008970EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008971
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008972void md_reap_sync_thread(struct mddev *mddev)
8973{
8974 struct md_rdev *rdev;
8975
8976 /* resync has finished, collect result */
8977 md_unregister_thread(&mddev->sync_thread);
8978 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8979 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8980 /* success...*/
8981 /* activate any spares */
8982 if (mddev->pers->spare_active(mddev)) {
8983 sysfs_notify(&mddev->kobj, NULL,
8984 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08008985 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008986 }
8987 }
8988 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8989 mddev->pers->finish_reshape)
8990 mddev->pers->finish_reshape(mddev);
8991
8992 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008993 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008994 */
NeilBrownf4667222013-12-09 12:04:56 +11008995 if (!mddev->degraded)
8996 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008997 rdev->saved_raid_disk = -1;
8998
8999 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08009000 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04009001 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9002 * clustered raid */
9003 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9004 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009005 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10009006 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009007 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9008 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9009 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9010 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009011 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009012 /* flag recovery needed just to double check */
9013 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9014 sysfs_notify_dirent_safe(mddev->sysfs_action);
9015 md_new_event(mddev);
9016 if (mddev->event_work.func)
9017 queue_work(md_misc_wq, &mddev->event_work);
9018}
NeilBrown6c144d32014-09-30 16:15:38 +10009019EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009020
NeilBrownfd01b882011-10-11 16:47:53 +11009021void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07009022{
NeilBrown00bcb4a2010-06-01 19:37:23 +10009023 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07009024 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10009025 !test_bit(Blocked, &rdev->flags) &&
9026 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07009027 msecs_to_jiffies(5000));
9028 rdev_dec_pending(rdev, mddev);
9029}
9030EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9031
NeilBrownc6563a82012-05-21 09:27:00 +10009032void md_finish_reshape(struct mddev *mddev)
9033{
9034 /* called be personality module when reshape completes. */
9035 struct md_rdev *rdev;
9036
9037 rdev_for_each(rdev, mddev) {
9038 if (rdev->data_offset > rdev->new_data_offset)
9039 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9040 else
9041 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9042 rdev->data_offset = rdev->new_data_offset;
9043 }
9044}
9045EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10009046
Vishal Vermafc974ee2015-12-24 19:20:34 -07009047/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10009048
Vishal Vermafc974ee2015-12-24 19:20:34 -07009049/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11009050int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10009051 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009052{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04009053 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10009054 int rv;
9055 if (is_new)
9056 s += rdev->new_data_offset;
9057 else
9058 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07009059 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9060 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10009061 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009062 if (test_bit(ExternalBbl, &rdev->flags))
9063 sysfs_notify(&rdev->kobj, NULL,
9064 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11009065 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08009066 set_mask_bits(&mddev->sb_flags, 0,
9067 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10009068 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07009069 return 1;
9070 } else
9071 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10009072}
9073EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9074
NeilBrownc6563a82012-05-21 09:27:00 +10009075int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9076 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009077{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009078 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10009079 if (is_new)
9080 s += rdev->new_data_offset;
9081 else
9082 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009083 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9084 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
9085 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
9086 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10009087}
9088EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9089
Adrian Bunk75c96f82005-05-05 16:16:09 -07009090static int md_notify_reboot(struct notifier_block *this,
9091 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009092{
9093 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009094 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009095 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009096
NeilBrownc744a652012-03-19 12:46:37 +11009097 for_each_mddev(mddev, tmp) {
9098 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009099 if (mddev->pers)
9100 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009101 if (mddev->persistent)
9102 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009103 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009104 }
NeilBrownc744a652012-03-19 12:46:37 +11009105 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009106 }
NeilBrownc744a652012-03-19 12:46:37 +11009107 /*
9108 * certain more exotic SCSI devices are known to be
9109 * volatile wrt too early system reboots. While the
9110 * right place to handle this issue is the given
9111 * driver, we do want to have a safe RAID driver ...
9112 */
9113 if (need_delay)
9114 mdelay(1000*1);
9115
Linus Torvalds1da177e2005-04-16 15:20:36 -07009116 return NOTIFY_DONE;
9117}
9118
Adrian Bunk75c96f82005-05-05 16:16:09 -07009119static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009120 .notifier_call = md_notify_reboot,
9121 .next = NULL,
9122 .priority = INT_MAX, /* before any real devices */
9123};
9124
9125static void md_geninit(void)
9126{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009127 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009128
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07009129 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009130}
9131
Adrian Bunk75c96f82005-05-05 16:16:09 -07009132static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009133{
Tejun Heoe804ac72010-10-15 15:36:08 +02009134 int ret = -ENOMEM;
9135
Tejun Heoada609e2011-01-25 14:35:54 +01009136 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009137 if (!md_wq)
9138 goto err_wq;
9139
9140 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9141 if (!md_misc_wq)
9142 goto err_misc_wq;
9143
9144 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9145 goto err_md;
9146
9147 if ((ret = register_blkdev(0, "mdp")) < 0)
9148 goto err_mdp;
9149 mdp_major = ret;
9150
NeilBrownaf5628f2014-07-31 13:54:54 +10009151 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009152 md_probe, NULL, NULL);
9153 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009154 md_probe, NULL, NULL);
9155
Linus Torvalds1da177e2005-04-16 15:20:36 -07009156 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009157 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009158
9159 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009161
Tejun Heoe804ac72010-10-15 15:36:08 +02009162err_mdp:
9163 unregister_blkdev(MD_MAJOR, "md");
9164err_md:
9165 destroy_workqueue(md_misc_wq);
9166err_misc_wq:
9167 destroy_workqueue(md_wq);
9168err_wq:
9169 return ret;
9170}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009171
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009172static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009173{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009174 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9175 struct md_rdev *rdev2;
9176 int role, ret;
9177 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009178
Guoqing Jiang818da592017-03-01 16:42:40 +08009179 /*
9180 * If size is changed in another node then we need to
9181 * do resize as well.
9182 */
9183 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9184 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9185 if (ret)
9186 pr_info("md-cluster: resize failed\n");
9187 else
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009188 md_bitmap_update_sb(mddev->bitmap);
Guoqing Jiang818da592017-03-01 16:42:40 +08009189 }
9190
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009191 /* Check for change of roles in the active devices */
9192 rdev_for_each(rdev2, mddev) {
9193 if (test_bit(Faulty, &rdev2->flags))
9194 continue;
9195
9196 /* Check if the roles changed */
9197 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009198
9199 if (test_bit(Candidate, &rdev2->flags)) {
9200 if (role == 0xfffe) {
9201 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9202 md_kick_rdev_from_array(rdev2);
9203 continue;
9204 }
9205 else
9206 clear_bit(Candidate, &rdev2->flags);
9207 }
9208
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009209 if (role != rdev2->raid_disk) {
9210 /* got activated */
9211 if (rdev2->raid_disk == -1 && role != 0xffff) {
9212 rdev2->saved_raid_disk = role;
9213 ret = remove_and_add_spares(mddev, rdev2);
9214 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009215 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009216 /* wakeup mddev->thread here, so array could
9217 * perform resync with the new activated disk */
9218 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9219 md_wakeup_thread(mddev->thread);
9220
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009221 }
9222 /* device faulty
9223 * We just want to do the minimum to mark the disk
9224 * as faulty. The recovery is performed by the
9225 * one who initiated the error.
9226 */
9227 if ((role == 0xfffe) || (role == 0xfffd)) {
9228 md_error(mddev, rdev2);
9229 clear_bit(Blocked, &rdev2->flags);
9230 }
9231 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009232 }
9233
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009234 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9235 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009236
9237 /* Finally set the event to be up to date */
9238 mddev->events = le64_to_cpu(sb->events);
9239}
9240
9241static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9242{
9243 int err;
9244 struct page *swapout = rdev->sb_page;
9245 struct mdp_superblock_1 *sb;
9246
9247 /* Store the sb page of the rdev in the swapout temporary
9248 * variable in case we err in the future
9249 */
9250 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009251 err = alloc_disk_sb(rdev);
9252 if (err == 0) {
9253 ClearPageUptodate(rdev->sb_page);
9254 rdev->sb_loaded = 0;
9255 err = super_types[mddev->major_version].
9256 load_super(rdev, NULL, mddev->minor_version);
9257 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009258 if (err < 0) {
9259 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9260 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009261 if (rdev->sb_page)
9262 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009263 rdev->sb_page = swapout;
9264 rdev->sb_loaded = 1;
9265 return err;
9266 }
9267
9268 sb = page_address(rdev->sb_page);
9269 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9270 * is not set
9271 */
9272
9273 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9274 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9275
9276 /* The other node finished recovery, call spare_active to set
9277 * device In_sync and mddev->degraded
9278 */
9279 if (rdev->recovery_offset == MaxSector &&
9280 !test_bit(In_sync, &rdev->flags) &&
9281 mddev->pers->spare_active(mddev))
9282 sysfs_notify(&mddev->kobj, NULL, "degraded");
9283
9284 put_page(swapout);
9285 return 0;
9286}
9287
9288void md_reload_sb(struct mddev *mddev, int nr)
9289{
9290 struct md_rdev *rdev;
9291 int err;
9292
9293 /* Find the rdev */
9294 rdev_for_each_rcu(rdev, mddev) {
9295 if (rdev->desc_nr == nr)
9296 break;
9297 }
9298
9299 if (!rdev || rdev->desc_nr != nr) {
9300 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9301 return;
9302 }
9303
9304 err = read_rdev(mddev, rdev);
9305 if (err < 0)
9306 return;
9307
9308 check_sb_changes(mddev, rdev);
9309
9310 /* Read all rdev's to update recovery_offset */
Guoqing Jiang0ea99242018-04-09 17:01:21 +08009311 rdev_for_each_rcu(rdev, mddev) {
9312 if (!test_bit(Faulty, &rdev->flags))
9313 read_rdev(mddev, rdev);
9314 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009315}
9316EXPORT_SYMBOL(md_reload_sb);
9317
Linus Torvalds1da177e2005-04-16 15:20:36 -07009318#ifndef MODULE
9319
9320/*
9321 * Searches all registered partitions for autorun RAID arrays
9322 * at boot time.
9323 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009324
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009325static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009326static LIST_HEAD(all_detected_devices);
9327struct detected_devices_node {
9328 struct list_head list;
9329 dev_t dev;
9330};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009331
9332void md_autodetect_dev(dev_t dev)
9333{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009334 struct detected_devices_node *node_detected_dev;
9335
9336 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9337 if (node_detected_dev) {
9338 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009339 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009340 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009341 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009343}
9344
Linus Torvalds1da177e2005-04-16 15:20:36 -07009345static void autostart_arrays(int part)
9346{
NeilBrown3cb03002011-10-11 16:45:26 +11009347 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009348 struct detected_devices_node *node_detected_dev;
9349 dev_t dev;
9350 int i_scanned, i_passed;
9351
9352 i_scanned = 0;
9353 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009354
NeilBrown9d487392016-11-02 14:16:49 +11009355 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009356
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009357 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009358 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9359 i_scanned++;
9360 node_detected_dev = list_entry(all_detected_devices.next,
9361 struct detected_devices_node, list);
9362 list_del(&node_detected_dev->list);
9363 dev = node_detected_dev->dev;
9364 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009365 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009366 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009367 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009368 if (IS_ERR(rdev))
9369 continue;
9370
NeilBrown403df472014-09-30 15:52:29 +10009371 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009372 continue;
NeilBrown403df472014-09-30 15:52:29 +10009373
NeilBrownd0fae182008-03-04 14:29:31 -08009374 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009375 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009376 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009378 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009379
NeilBrown9d487392016-11-02 14:16:49 +11009380 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009381
9382 autorun_devices(part);
9383}
9384
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009385#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009386
9387static __exit void md_exit(void)
9388{
NeilBrownfd01b882011-10-11 16:47:53 +11009389 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009390 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009391 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009392
NeilBrownaf5628f2014-07-31 13:54:54 +10009393 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009394 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009395
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009396 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009397 unregister_blkdev(mdp_major, "mdp");
9398 unregister_reboot_notifier(&md_notifier);
9399 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009400
9401 /* We cannot unload the modules while some process is
9402 * waiting for us in select() or poll() - wake them up
9403 */
9404 md_unloading = 1;
9405 while (waitqueue_active(&md_event_waiters)) {
9406 /* not safe to leave yet */
9407 wake_up(&md_event_waiters);
9408 msleep(delay);
9409 delay += delay;
9410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009411 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009412
NeilBrown29ac4aa2008-02-06 01:39:58 -08009413 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009414 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009415 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009416 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009417 /*
9418 * for_each_mddev() will call mddev_put() at the end of each
9419 * iteration. As the mddev is now fully clear, this will
9420 * schedule the mddev for destruction by a workqueue, and the
9421 * destroy_workqueue() below will wait for that to complete.
9422 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009423 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009424 destroy_workqueue(md_misc_wq);
9425 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009426}
9427
Dan Williams685784a2007-07-09 11:56:42 -07009428subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009429module_exit(md_exit)
9430
Kees Cooke4dca7b2017-10-17 19:04:42 -07009431static int get_ro(char *buffer, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009432{
9433 return sprintf(buffer, "%d", start_readonly);
9434}
Kees Cooke4dca7b2017-10-17 19:04:42 -07009435static int set_ro(const char *val, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009436{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009437 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009438}
9439
NeilBrown80ca3a42006-07-10 04:44:18 -07009440module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9441module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009442module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009443module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009444
Linus Torvalds1da177e2005-04-16 15:20:36 -07009445MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009446MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009447MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009448MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);