blob: fc488cb30a94780d8a4fee0674d718aa901b047f [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)) {
Xiao Niaf9b9262018-10-20 08:09:25 +0800455 if (bio->bi_iter.bi_size == 0) {
Xiao Ni5a409b42018-05-21 11:49:54 +0800456 /* an empty barrier - all done */
457 bio_endio(bio);
Xiao Niaf9b9262018-10-20 08:09:25 +0800458 mempool_free(fi, mddev->flush_pool);
459 } else {
Xiao Ni5a409b42018-05-21 11:49:54 +0800460 INIT_WORK(&fi->flush_work, submit_flushes);
461 queue_work(md_wq, &fi->flush_work);
462 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100463 }
Xiao Ni5a409b42018-05-21 11:49:54 +0800464
465 mempool_free(fb, mddev->flush_bio_pool);
466 bio_put(fbio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100467}
468
Xiao Ni5a409b42018-05-21 11:49:54 +0800469void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100470{
NeilBrown3cb03002011-10-11 16:45:26 +1100471 struct md_rdev *rdev;
Xiao Ni5a409b42018-05-21 11:49:54 +0800472 struct flush_info *fi;
NeilBrowna2826aa2009-12-14 12:49:49 +1100473
Xiao Ni5a409b42018-05-21 11:49:54 +0800474 fi = mempool_alloc(mddev->flush_pool, GFP_NOIO);
475
476 fi->bio = bio;
477 fi->mddev = mddev;
478 atomic_set(&fi->flush_pending, 1);
479
NeilBrowna2826aa2009-12-14 12:49:49 +1100480 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100481 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100482 if (rdev->raid_disk >= 0 &&
483 !test_bit(Faulty, &rdev->flags)) {
484 /* Take two references, one is dropped
485 * when request finishes, one after
486 * we reclaim rcu_read_lock
487 */
488 struct bio *bi;
Xiao Ni5a409b42018-05-21 11:49:54 +0800489 struct flush_bio *fb;
NeilBrowna2826aa2009-12-14 12:49:49 +1100490 atomic_inc(&rdev->nr_pending);
491 atomic_inc(&rdev->nr_pending);
492 rcu_read_unlock();
Xiao Ni5a409b42018-05-21 11:49:54 +0800493
494 fb = mempool_alloc(mddev->flush_bio_pool, GFP_NOIO);
495 fb->fi = fi;
496 fb->rdev = rdev;
497
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000498 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Christoph Hellwig74d46992017-08-23 19:10:32 +0200499 bio_set_dev(bi, rdev->bdev);
Xiao Ni5a409b42018-05-21 11:49:54 +0800500 bi->bi_end_io = md_end_flush;
501 bi->bi_private = fb;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600502 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
Xiao Ni5a409b42018-05-21 11:49:54 +0800503
504 atomic_inc(&fi->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500505 submit_bio(bi);
Xiao Ni5a409b42018-05-21 11:49:54 +0800506
NeilBrowna2826aa2009-12-14 12:49:49 +1100507 rcu_read_lock();
508 rdev_dec_pending(rdev, mddev);
509 }
510 rcu_read_unlock();
NeilBrowna2826aa2009-12-14 12:49:49 +1100511
Xiao Ni5a409b42018-05-21 11:49:54 +0800512 if (atomic_dec_and_test(&fi->flush_pending)) {
Xiao Niaf9b9262018-10-20 08:09:25 +0800513 if (bio->bi_iter.bi_size == 0) {
Xiao Ni5a409b42018-05-21 11:49:54 +0800514 /* an empty barrier - all done */
515 bio_endio(bio);
Xiao Niaf9b9262018-10-20 08:09:25 +0800516 mempool_free(fi, mddev->flush_pool);
517 } else {
Xiao Ni5a409b42018-05-21 11:49:54 +0800518 INIT_WORK(&fi->flush_work, submit_flushes);
519 queue_work(md_wq, &fi->flush_work);
520 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100521 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100522}
Tejun Heoe9c74692010-09-03 11:56:18 +0200523EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100524
NeilBrownfd01b882011-10-11 16:47:53 +1100525static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 atomic_inc(&mddev->active);
528 return mddev;
529}
530
Dan Williams5fd3a172009-03-04 00:57:25 -0700531static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100532
NeilBrownfd01b882011-10-11 16:47:53 +1100533static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
536 return;
NeilBrownd3374822009-01-09 08:31:10 +1100537 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100538 mddev->ctime == 0 && !mddev->hold_active) {
539 /* Array is not configured at all, and not held active,
540 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100541 list_del_init(&mddev->all_mddevs);
Kent Overstreet28dec872018-06-07 20:52:54 -0400542
543 /*
544 * Call queue_work inside the spinlock so that
545 * flush_workqueue() after mddev_find will succeed in waiting
546 * for the work to be done.
547 */
548 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
549 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100550 }
551 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Kees Cook8376d3c2017-10-16 17:01:48 -0700554static void md_safemode_timeout(struct timer_list *t);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400555
NeilBrownfd01b882011-10-11 16:47:53 +1100556void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100557{
Kent Overstreet28dec872018-06-07 20:52:54 -0400558 kobject_init(&mddev->kobj, &md_ktype);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100559 mutex_init(&mddev->open_mutex);
560 mutex_init(&mddev->reconfig_mutex);
561 mutex_init(&mddev->bitmap_info.mutex);
562 INIT_LIST_HEAD(&mddev->disks);
563 INIT_LIST_HEAD(&mddev->all_mddevs);
Kees Cook8376d3c2017-10-16 17:01:48 -0700564 timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100565 atomic_set(&mddev->active, 1);
566 atomic_set(&mddev->openers, 0);
567 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100568 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100569 init_waitqueue_head(&mddev->sb_wait);
570 init_waitqueue_head(&mddev->recovery_wait);
571 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000572 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500573 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100574 mddev->resync_min = 0;
575 mddev->resync_max = MaxSector;
576 mddev->level = LEVEL_NONE;
577}
NeilBrown390ee602010-06-01 19:37:27 +1000578EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100579
NeilBrownf72ffdd2014-09-30 14:23:59 +1000580static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
NeilBrownfd01b882011-10-11 16:47:53 +1100582 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
NeilBrown8f5f02c2011-02-16 13:58:51 +1100584 if (unit && MAJOR(unit) != MD_MAJOR)
585 unit &= ~((1<<MdpMinorShift)-1);
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 retry:
588 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
NeilBrownefeb53c2009-01-09 08:31:10 +1100590 if (unit) {
591 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
592 if (mddev->unit == unit) {
593 mddev_get(mddev);
594 spin_unlock(&all_mddevs_lock);
595 kfree(new);
596 return mddev;
597 }
598
599 if (new) {
600 list_add(&new->all_mddevs, &all_mddevs);
601 spin_unlock(&all_mddevs_lock);
602 new->hold_active = UNTIL_IOCTL;
603 return new;
604 }
605 } else if (new) {
606 /* find an unused unit number */
607 static int next_minor = 512;
608 int start = next_minor;
609 int is_free = 0;
610 int dev = 0;
611 while (!is_free) {
612 dev = MKDEV(MD_MAJOR, next_minor);
613 next_minor++;
614 if (next_minor > MINORMASK)
615 next_minor = 0;
616 if (next_minor == start) {
617 /* Oh dear, all in use. */
618 spin_unlock(&all_mddevs_lock);
619 kfree(new);
620 return NULL;
621 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000622
NeilBrownefeb53c2009-01-09 08:31:10 +1100623 is_free = 1;
624 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
625 if (mddev->unit == dev) {
626 is_free = 0;
627 break;
628 }
629 }
630 new->unit = dev;
631 new->md_minor = MINOR(dev);
632 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 list_add(&new->all_mddevs, &all_mddevs);
634 spin_unlock(&all_mddevs_lock);
635 return new;
636 }
637 spin_unlock(&all_mddevs_lock);
638
NeilBrown9ffae0c2006-01-06 00:20:32 -0800639 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (!new)
641 return NULL;
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 new->unit = unit;
644 if (MAJOR(unit) == MD_MAJOR)
645 new->md_minor = MINOR(unit);
646 else
647 new->md_minor = MINOR(unit) >> MdpMinorShift;
648
NeilBrownfafd7fb2010-04-01 15:55:30 +1100649 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 goto retry;
652}
653
NeilBrownb6eb1272010-04-15 10:13:47 +1000654static struct attribute_group md_redundancy_group;
655
NeilBrown5c47daf2014-12-15 12:57:01 +1100656void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
NeilBrowna64c8762010-04-14 17:15:37 +1000658 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000659 /* These cannot be removed under reconfig_mutex as
660 * an access to the files will try to take reconfig_mutex
661 * while holding the file unremovable, which leads to
662 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000663 * So hold set sysfs_active while the remove in happeing,
664 * and anything else which might set ->to_remove or my
665 * otherwise change the sysfs namespace will fail with
666 * -EBUSY if sysfs_active is still set.
667 * We set sysfs_active under reconfig_mutex and elsewhere
668 * test it under the same mutex to ensure its correct value
669 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000670 */
NeilBrowna64c8762010-04-14 17:15:37 +1000671 struct attribute_group *to_remove = mddev->to_remove;
672 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000673 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000674 mutex_unlock(&mddev->reconfig_mutex);
675
NeilBrown00bcb4a2010-06-01 19:37:23 +1000676 if (mddev->kobj.sd) {
677 if (to_remove != &md_redundancy_group)
678 sysfs_remove_group(&mddev->kobj, to_remove);
679 if (mddev->pers == NULL ||
680 mddev->pers->sync_request == NULL) {
681 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
682 if (mddev->sysfs_action)
683 sysfs_put(mddev->sysfs_action);
684 mddev->sysfs_action = NULL;
685 }
NeilBrowna64c8762010-04-14 17:15:37 +1000686 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000687 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000688 } else
689 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Chris Dunlop751e67c2011-10-19 16:48:26 +1100691 /* As we've dropped the mutex we need a spinlock to
692 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000693 */
694 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700695 md_wakeup_thread(mddev->thread);
NeilBrown4d5324f2017-10-19 12:17:16 +1100696 wake_up(&mddev->sb_wait);
NeilBrown01f96c02011-09-21 15:30:20 +1000697 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
NeilBrown5c47daf2014-12-15 12:57:01 +1100699EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500701struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100702{
703 struct md_rdev *rdev;
704
705 rdev_for_each_rcu(rdev, mddev)
706 if (rdev->desc_nr == nr)
707 return rdev;
708
709 return NULL;
710}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500711EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100712
713static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
NeilBrown3cb03002011-10-11 16:45:26 +1100715 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
NeilBrowndafb20f2012-03-19 12:46:39 +1100717 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (rdev->bdev->bd_dev == dev)
719 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return NULL;
722}
723
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100724struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
NeilBrown1ca69c42012-10-11 13:37:33 +1100725{
726 struct md_rdev *rdev;
727
728 rdev_for_each_rcu(rdev, mddev)
729 if (rdev->bdev->bd_dev == dev)
730 return rdev;
731
732 return NULL;
733}
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100734EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100735
NeilBrown84fc4b52011-10-11 16:49:58 +1100736static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800737{
NeilBrown84fc4b52011-10-11 16:49:58 +1100738 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800739 list_for_each_entry(pers, &pers_list, list) {
740 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800741 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800742 if (strcmp(pers->name, clevel)==0)
743 return pers;
744 }
NeilBrown2604b702006-01-06 00:20:36 -0800745 return NULL;
746}
747
Andre Nollb73df2d2008-07-11 22:02:23 +1000748/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100749static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100751 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000752 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
NeilBrownf72ffdd2014-09-30 14:23:59 +1000755static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100758 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000759 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return 0;
761}
762
NeilBrown545c8792012-05-22 13:54:30 +1000763void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800766 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 rdev->sb_loaded = 0;
768 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000769 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100770 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
NeilBrown2699b672011-07-28 11:31:47 +1000772 if (rdev->bb_page) {
773 put_page(rdev->bb_page);
774 rdev->bb_page = NULL;
775 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800776 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
NeilBrown545c8792012-05-22 13:54:30 +1000778EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200780static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700781{
NeilBrown3cb03002011-10-11 16:45:26 +1100782 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100783 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700784
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200785 if (bio->bi_status) {
786 pr_err("md: super_written gets error=%d\n", bio->bi_status);
NeilBrowna9701a32005-11-08 21:39:34 -0800787 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100788 if (!test_bit(Faulty, &rdev->flags)
789 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800790 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100791 set_bit(LastDev, &rdev->flags);
792 }
793 } else
794 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700795
NeilBrowna9701a32005-11-08 21:39:34 -0800796 if (atomic_dec_and_test(&mddev->pending_writes))
797 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700798 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700799 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700800}
801
NeilBrownfd01b882011-10-11 16:47:53 +1100802void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700803 sector_t sector, int size, struct page *page)
804{
805 /* write first size bytes of page to sector of rdev
806 * Increment mddev->pending_writes before returning
807 * and decrement it on completion, waking up sb_wait
808 * if zero is reached.
809 * If an error occurred, call md_error
810 */
NeilBrown46533ff2016-11-18 16:16:11 +1100811 struct bio *bio;
812 int ff = 0;
813
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +0100814 if (!page)
815 return;
816
NeilBrown46533ff2016-11-18 16:16:11 +1100817 if (test_bit(Faulty, &rdev->flags))
818 return;
819
NeilBrown5a850712017-06-21 09:12:21 +1000820 bio = md_bio_alloc_sync(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700821
Shaohua Lied3b98c2016-03-29 14:00:19 -0700822 atomic_inc(&rdev->nr_pending);
823
Christoph Hellwig74d46992017-08-23 19:10:32 +0200824 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700825 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700826 bio_add_page(bio, page, size, 0);
827 bio->bi_private = rdev;
828 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100829
830 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
831 test_bit(FailFast, &rdev->flags) &&
832 !test_bit(LastDev, &rdev->flags))
833 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +0200834 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -0800835
NeilBrown7bfa19f2005-06-21 17:17:28 -0700836 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500837 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800838}
839
NeilBrown46533ff2016-11-18 16:16:11 +1100840int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800841{
Tejun Heoe9c74692010-09-03 11:56:18 +0200842 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000843 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -0800844 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +1100845 return -EAGAIN;
846 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700847}
848
NeilBrown3cb03002011-10-11 16:45:26 +1100849int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -0500850 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
NeilBrown5a850712017-06-21 09:12:21 +1000852 struct bio *bio = md_bio_alloc_sync(rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 int ret;
854
Christoph Hellwig74d46992017-08-23 19:10:32 +0200855 if (metadata_op && rdev->meta_bdev)
856 bio_set_dev(bio, rdev->meta_bdev);
857 else
858 bio_set_dev(bio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -0500859 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100860 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700861 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000862 else if (rdev->mddev->reshape_position != MaxSector &&
863 (rdev->mddev->reshape_backwards ==
864 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700865 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100866 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700867 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500869
870 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200872 ret = !bio->bi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 bio_put(bio);
874 return ret;
875}
NeilBrowna8745db2006-01-06 00:20:34 -0800876EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
NeilBrownf72ffdd2014-09-30 14:23:59 +1000878static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (rdev->sb_loaded)
883 return 0;
884
Mike Christie796a5cf2016-06-05 14:32:07 -0500885 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 goto fail;
887 rdev->sb_loaded = 1;
888 return 0;
889
890fail:
NeilBrown9d487392016-11-02 14:16:49 +1100891 pr_err("md: disabled device %s, could not read superblock.\n",
892 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return -EINVAL;
894}
895
Amir Goldsteine6fd2092017-05-04 16:26:20 +0300896static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000898 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000899 sb1->set_uuid1 == sb2->set_uuid1 &&
900 sb1->set_uuid2 == sb2->set_uuid2 &&
901 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Amir Goldsteine6fd2092017-05-04 16:26:20 +0300904static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 int ret;
907 mdp_super_t *tmp1, *tmp2;
908
909 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
910 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
911
912 if (!tmp1 || !tmp2) {
913 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 goto abort;
915 }
916
917 *tmp1 = *sb1;
918 *tmp2 = *sb2;
919
920 /*
921 * nr_disks is not constant
922 */
923 tmp1->nr_disks = 0;
924 tmp2->nr_disks = 0;
925
Andre Nollce0c8e02008-07-11 22:02:20 +1000926 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700928 kfree(tmp1);
929 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return ret;
931}
932
NeilBrown4d167f02007-05-09 02:35:37 -0700933static u32 md_csum_fold(u32 csum)
934{
935 csum = (csum & 0xffff) + (csum >> 16);
936 return (csum & 0xffff) + (csum >> 16);
937}
938
NeilBrownf72ffdd2014-09-30 14:23:59 +1000939static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
NeilBrown4d167f02007-05-09 02:35:37 -0700941 u64 newcsum = 0;
942 u32 *sb32 = (u32*)sb;
943 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 unsigned int disk_csum, csum;
945
946 disk_csum = sb->sb_csum;
947 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700948
949 for (i = 0; i < MD_SB_BYTES/4 ; i++)
950 newcsum += sb32[i];
951 csum = (newcsum & 0xffffffff) + (newcsum>>32);
952
NeilBrown4d167f02007-05-09 02:35:37 -0700953#ifdef CONFIG_ALPHA
954 /* This used to use csum_partial, which was wrong for several
955 * reasons including that different results are returned on
956 * different architectures. It isn't critical that we get exactly
957 * the same return value as before (we always csum_fold before
958 * testing, and that removes any differences). However as we
959 * know that csum_partial always returned a 16bit value on
960 * alphas, do a fold to maximise conformity to previous behaviour.
961 */
962 sb->sb_csum = md_csum_fold(disk_csum);
963#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700965#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return csum;
967}
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969/*
970 * Handle superblock details.
971 * We want to be able to handle multiple superblock formats
972 * so we have a common interface to them all, and an array of
973 * different handlers.
974 * We rely on user-space to write the initial superblock, and support
975 * reading and updating of superblocks.
976 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100977 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 * loads and validates a superblock on dev.
979 * if refdev != NULL, compare superblocks on both devices
980 * Return:
981 * 0 - dev has a superblock that is compatible with refdev
982 * 1 - dev has a superblock that is compatible and newer than refdev
983 * so dev should be used as the refdev in future
984 * -EINVAL superblock incompatible or invalid
985 * -othererror e.g. -EIO
986 *
NeilBrownfd01b882011-10-11 16:47:53 +1100987 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 * Verify that dev is acceptable into mddev.
989 * The first time, mddev->raid_disks will be 0, and data from
990 * dev should be merged in. Subsequent calls check that dev
991 * is new enough. Return 0 or -EINVAL
992 *
NeilBrownfd01b882011-10-11 16:47:53 +1100993 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 * Update the superblock for rdev with data in mddev
995 * This does not write to disc.
996 *
997 */
998
999struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001000 char *name;
1001 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +10001002 int (*load_super)(struct md_rdev *rdev,
1003 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001004 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +10001005 int (*validate_super)(struct mddev *mddev,
1006 struct md_rdev *rdev);
1007 void (*sync_super)(struct mddev *mddev,
1008 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001009 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001010 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +10001011 int (*allow_new_offset)(struct md_rdev *rdev,
1012 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013};
1014
1015/*
Andre Noll0894cc32009-06-18 08:49:23 +10001016 * Check that the given mddev has no bitmap.
1017 *
1018 * This function is called from the run method of all personalities that do not
1019 * support bitmaps. It prints an error message and returns non-zero if mddev
1020 * has a bitmap. Otherwise, it returns 0.
1021 *
1022 */
NeilBrownfd01b882011-10-11 16:47:53 +11001023int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001024{
NeilBrownc3d97142009-12-14 12:49:52 +11001025 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001026 return 0;
NeilBrown9d487392016-11-02 14:16:49 +11001027 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +10001028 mdname(mddev), mddev->pers->name);
1029 return 1;
1030}
1031EXPORT_SYMBOL(md_check_no_bitmap);
1032
1033/*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001034 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 */
NeilBrown3cb03002011-10-11 16:45:26 +11001036static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1039 mdp_super_t *sb;
1040 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 /*
Andre Noll0f420352008-07-11 22:02:23 +10001043 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 * it's at the end of the disk.
1045 *
1046 * It also happens to be a multiple of 4Kb.
1047 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001048 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
NeilBrown0002b272005-09-09 16:23:53 -07001050 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +11001051 if (ret)
1052 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 ret = -EINVAL;
1055
1056 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001057 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +11001060 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 goto abort;
1062 }
1063
1064 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001065 sb->minor_version < 90 ||
1066 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +11001067 pr_warn("Bad version number %d.%d on %s\n",
1068 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 goto abort;
1070 }
1071
1072 if (sb->raid_disks <= 0)
1073 goto abort;
1074
NeilBrown4d167f02007-05-09 02:35:37 -07001075 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001076 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 goto abort;
1078 }
1079
1080 rdev->preferred_minor = sb->md_minor;
1081 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001082 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001083 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001084 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 if (sb->level == LEVEL_MULTIPATH)
1087 rdev->desc_nr = -1;
1088 else
1089 rdev->desc_nr = sb->this_disk.number;
1090
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001091 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001093 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001095 mdp_super_t *refsb = page_address(refdev->sb_page);
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001096 if (!md_uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001097 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 b, bdevname(refdev->bdev,b2));
1099 goto abort;
1100 }
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001101 if (!md_sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001102 pr_warn("md: %s has same UUID but different superblock to %s\n",
1103 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 goto abort;
1105 }
1106 ev1 = md_event(sb);
1107 ev2 = md_event(refsb);
1108 if (ev1 > ev2)
1109 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001110 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 ret = 0;
1112 }
NeilBrown8190e752009-06-18 08:48:58 +10001113 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001114 /* Limit to 4TB as metadata cannot record more than that.
1115 * (not needed for Linear and RAID0 as metadata doesn't
1116 * record this size)
1117 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001118 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1119 sb->level >= 1)
1120 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
NeilBrown27a7b262011-09-10 17:21:28 +10001122 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001123 /* "this cannot possibly happen" ... */
1124 ret = -EINVAL;
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 abort:
1127 return ret;
1128}
1129
1130/*
1131 * validate_super for 0.90.0
1132 */
NeilBrownfd01b882011-10-11 16:47:53 +11001133static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001136 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001137 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
NeilBrown41158c72005-06-21 17:17:25 -07001139 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001140 clear_bit(Faulty, &rdev->flags);
1141 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001142 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001143 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (mddev->raid_disks == 0) {
1146 mddev->major_version = 0;
1147 mddev->minor_version = sb->minor_version;
1148 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001149 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001150 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 mddev->ctime = sb->ctime;
1152 mddev->utime = sb->utime;
1153 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001154 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 mddev->layout = sb->layout;
1156 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001157 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001158 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001159 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001160 mddev->bitmap_info.space = 0;
1161 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001162 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001163 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001164 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
NeilBrownf6705572006-03-27 01:18:11 -08001166 if (mddev->minor_version >= 91) {
1167 mddev->reshape_position = sb->reshape_position;
1168 mddev->delta_disks = sb->delta_disks;
1169 mddev->new_level = sb->new_level;
1170 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001171 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001172 if (mddev->delta_disks < 0)
1173 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001174 } else {
1175 mddev->reshape_position = MaxSector;
1176 mddev->delta_disks = 0;
1177 mddev->new_level = mddev->level;
1178 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001179 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001180 }
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (sb->state & (1<<MD_SB_CLEAN))
1183 mddev->recovery_cp = MaxSector;
1184 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001185 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 sb->events_lo == sb->cp_events_lo) {
1187 mddev->recovery_cp = sb->recovery_cp;
1188 } else
1189 mddev->recovery_cp = 0;
1190 }
1191
1192 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1193 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1194 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1195 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1196
1197 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001198
1199 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001200 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001201 mddev->bitmap_info.offset =
1202 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001203 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001204 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001205 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001206
NeilBrown41158c72005-06-21 17:17:25 -07001207 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001208 /* Insist on good event counter while assembling, except
1209 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001211 if (sb->disks[rdev->desc_nr].state & (
1212 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001213 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001214 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001215 } else if (mddev->bitmap) {
1216 /* if adding to array with a bitmap, then we can accept an
1217 * older device ... but not too old.
1218 */
NeilBrown41158c72005-06-21 17:17:25 -07001219 if (ev1 < mddev->bitmap->events_cleared)
1220 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001221 if (ev1 < mddev->events)
1222 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001223 } else {
1224 if (ev1 < mddev->events)
1225 /* just a hot-add of a new device, leave raid_disk at -1 */
1226 return 0;
1227 }
NeilBrown41158c72005-06-21 17:17:25 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 desc = sb->disks + rdev->desc_nr;
1231
1232 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001233 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001234 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1235 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001236 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001238 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001239 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1240 /* active but not in sync implies recovery up to
1241 * reshape position. We don't know exactly where
1242 * that is, so set to zero for now */
1243 if (mddev->minor_version >= 91) {
1244 rdev->recovery_offset = 0;
1245 rdev->raid_disk = desc->raid_disk;
1246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001248 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1249 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001250 if (desc->state & (1<<MD_DISK_FAILFAST))
1251 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001252 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001253 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return 0;
1255}
1256
1257/*
1258 * sync_super for 0.90.0
1259 */
NeilBrownfd01b882011-10-11 16:47:53 +11001260static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001263 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /* make rdev->sb match mddev data..
1267 *
1268 * 1/ zero out disks
1269 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1270 * 3/ any empty disks < next_spare become removed
1271 *
1272 * disks[0] gets initialised to REMOVED because
1273 * we cannot be sure from other fields if it has
1274 * been initialised or not.
1275 */
1276 int i;
1277 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1278
NeilBrown61181562005-09-09 16:24:02 -07001279 rdev->sb_size = MD_SB_BYTES;
1280
Namhyung Kim65a06f062011-07-27 11:00:36 +10001281 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 memset(sb, 0, sizeof(*sb));
1284
1285 sb->md_magic = MD_SB_MAGIC;
1286 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 sb->patch_version = mddev->patch_version;
1288 sb->gvalid_words = 0; /* ignored */
1289 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1290 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1291 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1292 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1293
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001294 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001296 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 sb->raid_disks = mddev->raid_disks;
1298 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001299 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001300 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 sb->state = 0;
1302 sb->events_hi = (mddev->events>>32);
1303 sb->events_lo = (u32)mddev->events;
1304
NeilBrownf6705572006-03-27 01:18:11 -08001305 if (mddev->reshape_position == MaxSector)
1306 sb->minor_version = 90;
1307 else {
1308 sb->minor_version = 91;
1309 sb->reshape_position = mddev->reshape_position;
1310 sb->new_level = mddev->new_level;
1311 sb->delta_disks = mddev->delta_disks;
1312 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001313 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001314 }
1315 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (mddev->in_sync)
1317 {
1318 sb->recovery_cp = mddev->recovery_cp;
1319 sb->cp_events_hi = (mddev->events>>32);
1320 sb->cp_events_lo = (u32)mddev->events;
1321 if (mddev->recovery_cp == MaxSector)
1322 sb->state = (1<< MD_SB_CLEAN);
1323 } else
1324 sb->recovery_cp = 0;
1325
1326 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001327 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
NeilBrownc3d97142009-12-14 12:49:52 +11001329 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001330 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001333 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001335 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001336 int is_active = test_bit(In_sync, &rdev2->flags);
1337
1338 if (rdev2->raid_disk >= 0 &&
1339 sb->minor_version >= 91)
1340 /* we have nowhere to store the recovery_offset,
1341 * but if it is not below the reshape_position,
1342 * we can piggy-back on that.
1343 */
1344 is_active = 1;
1345 if (rdev2->raid_disk < 0 ||
1346 test_bit(Faulty, &rdev2->flags))
1347 is_active = 0;
1348 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001349 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001351 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001352 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 d = &sb->disks[rdev2->desc_nr];
1354 nr_disks++;
1355 d->number = rdev2->desc_nr;
1356 d->major = MAJOR(rdev2->bdev->bd_dev);
1357 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001358 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 d->raid_disk = rdev2->raid_disk;
1360 else
1361 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001362 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001364 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001366 if (test_bit(In_sync, &rdev2->flags))
1367 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 active++;
1369 working++;
1370 } else {
1371 d->state = 0;
1372 spare++;
1373 working++;
1374 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001375 if (test_bit(WriteMostly, &rdev2->flags))
1376 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001377 if (test_bit(FailFast, &rdev2->flags))
1378 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 /* now set the "removed" and "faulty" bits on any missing devices */
1381 for (i=0 ; i < mddev->raid_disks ; i++) {
1382 mdp_disk_t *d = &sb->disks[i];
1383 if (d->state == 0 && d->number == 0) {
1384 d->number = i;
1385 d->raid_disk = i;
1386 d->state = (1<<MD_DISK_REMOVED);
1387 d->state |= (1<<MD_DISK_FAULTY);
1388 failed++;
1389 }
1390 }
1391 sb->nr_disks = nr_disks;
1392 sb->active_disks = active;
1393 sb->working_disks = working;
1394 sb->failed_disks = failed;
1395 sb->spare_disks = spare;
1396
1397 sb->this_disk = sb->disks[rdev->desc_nr];
1398 sb->sb_csum = calc_sb_csum(sb);
1399}
1400
1401/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001402 * rdev_size_change for 0.90.0
1403 */
1404static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001405super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001406{
Andre Noll58c0fed2009-03-31 14:33:13 +11001407 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001408 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001409 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001410 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001411 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001412 if (!num_sectors || num_sectors > rdev->sb_start)
1413 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001414 /* Limit to 4TB as metadata cannot record more than that.
1415 * 4TB == 2^32 KB, or 2*2^32 sectors.
1416 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001417 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1418 rdev->mddev->level >= 1)
1419 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001420 do {
1421 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001422 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001423 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001424 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001425}
1426
NeilBrownc6563a82012-05-21 09:27:00 +10001427static int
1428super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1429{
1430 /* non-zero offset changes not possible with v0.90 */
1431 return new_offset == 0;
1432}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001433
1434/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 * version 1 superblock
1436 */
1437
NeilBrownf72ffdd2014-09-30 14:23:59 +10001438static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001440 __le32 disk_csum;
1441 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 unsigned long long newcsum;
1443 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001444 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 disk_csum = sb->sb_csum;
1447 sb->sb_csum = 0;
1448 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001449 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 newcsum += le32_to_cpu(*isuper++);
1451
1452 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001453 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1456 sb->sb_csum = disk_csum;
1457 return cpu_to_le32(csum);
1458}
1459
NeilBrown3cb03002011-10-11 16:45:26 +11001460static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 struct mdp_superblock_1 *sb;
1463 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001464 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001465 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001467 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 /*
Andre Noll0f420352008-07-11 22:02:23 +10001470 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 * It is always aligned to a 4K boundary and
1472 * depeding on minor_version, it can be:
1473 * 0: At least 8K, but less than 12K, from end of device
1474 * 1: At start of device
1475 * 2: 4K from start of device.
1476 */
1477 switch(minor_version) {
1478 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001479 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001480 sb_start -= 8*2;
1481 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 break;
1483 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001484 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 break;
1486 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001487 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 break;
1489 default:
1490 return -EINVAL;
1491 }
Andre Noll0f420352008-07-11 22:02:23 +10001492 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
NeilBrown0002b272005-09-09 16:23:53 -07001494 /* superblock is rarely larger than 1K, but it can be larger,
1495 * and it is safe to read 4k, so we do that
1496 */
1497 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (ret) return ret;
1499
Namhyung Kim65a06f062011-07-27 11:00:36 +10001500 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1503 sb->major_version != cpu_to_le32(1) ||
1504 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001505 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001506 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return -EINVAL;
1508
1509 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001510 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 bdevname(rdev->bdev,b));
1512 return -EINVAL;
1513 }
1514 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001515 pr_warn("md: data_size too small on %s\n",
1516 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return -EINVAL;
1518 }
NeilBrownc6563a82012-05-21 09:27:00 +10001519 if (sb->pad0 ||
1520 sb->pad3[0] ||
1521 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1522 /* Some padding is non-zero, might be a new feature */
1523 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 rdev->preferred_minor = 0xffff;
1526 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001527 rdev->new_data_offset = rdev->data_offset;
1528 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1529 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1530 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001531 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
NeilBrown0002b272005-09-09 16:23:53 -07001533 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001534 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001535 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001536 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1537
1538 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001539 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001540 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001541 if (minor_version
1542 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1543 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001544
NeilBrown31b65a02006-07-10 04:44:14 -07001545 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1546 rdev->desc_nr = -1;
1547 else
1548 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1549
NeilBrown2699b672011-07-28 11:31:47 +10001550 if (!rdev->bb_page) {
1551 rdev->bb_page = alloc_page(GFP_KERNEL);
1552 if (!rdev->bb_page)
1553 return -ENOMEM;
1554 }
1555 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1556 rdev->badblocks.count == 0) {
1557 /* need to load the bad block list.
1558 * Currently we limit it to one page.
1559 */
1560 s32 offset;
1561 sector_t bb_sector;
1562 u64 *bbp;
1563 int i;
1564 int sectors = le16_to_cpu(sb->bblog_size);
1565 if (sectors > (PAGE_SIZE / 512))
1566 return -EINVAL;
1567 offset = le32_to_cpu(sb->bblog_offset);
1568 if (offset == 0)
1569 return -EINVAL;
1570 bb_sector = (long long)offset;
1571 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001572 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001573 return -EIO;
1574 bbp = (u64 *)page_address(rdev->bb_page);
1575 rdev->badblocks.shift = sb->bblog_shift;
1576 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1577 u64 bb = le64_to_cpu(*bbp);
1578 int count = bb & (0x3ff);
1579 u64 sector = bb >> 10;
1580 sector <<= sb->bblog_shift;
1581 count <<= sb->bblog_shift;
1582 if (bb + 1 == 0)
1583 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001584 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001585 return -EINVAL;
1586 }
NeilBrown486adf72013-04-24 11:42:44 +10001587 } else if (sb->bblog_offset != 0)
1588 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001589
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001590 if ((le32_to_cpu(sb->feature_map) &
1591 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001592 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1593 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1594 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1595 }
1596
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001597 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001598 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001599 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001601 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1604 sb->level != refsb->level ||
1605 sb->layout != refsb->layout ||
1606 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001607 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 bdevname(rdev->bdev,b),
1609 bdevname(refdev->bdev,b2));
1610 return -EINVAL;
1611 }
1612 ev1 = le64_to_cpu(sb->events);
1613 ev2 = le64_to_cpu(refsb->events);
1614
1615 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001616 ret = 1;
1617 else
1618 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
NeilBrownc6563a82012-05-21 09:27:00 +10001620 if (minor_version) {
1621 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1622 sectors -= rdev->data_offset;
1623 } else
1624 sectors = rdev->sb_start;
1625 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001627 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001628 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
NeilBrownfd01b882011-10-11 16:47:53 +11001631static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001633 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001634 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
NeilBrown41158c72005-06-21 17:17:25 -07001636 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001637 clear_bit(Faulty, &rdev->flags);
1638 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001639 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001640 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (mddev->raid_disks == 0) {
1643 mddev->major_version = 1;
1644 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001645 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001646 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001647 mddev->ctime = le64_to_cpu(sb->ctime);
1648 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001650 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 mddev->layout = le32_to_cpu(sb->layout);
1652 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001653 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001654 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001655 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001656 mddev->bitmap_info.space = 0;
1657 /* Default location for bitmap is 1K after superblock
1658 * using 3K - total of 4K
1659 */
NeilBrownc3d97142009-12-14 12:49:52 +11001660 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001661 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001662 mddev->reshape_backwards = 0;
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1665 memcpy(mddev->uuid, sb->set_uuid, 16);
1666
1667 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001668
NeilBrown71c08052005-09-09 16:23:51 -07001669 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001670 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001671 mddev->bitmap_info.offset =
1672 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001673 /* Metadata doesn't record how much space is available.
1674 * For 1.0, we assume we can use up to the superblock
1675 * if before, else to 4K beyond superblock.
1676 * For others, assume no change is possible.
1677 */
1678 if (mddev->minor_version > 0)
1679 mddev->bitmap_info.space = 0;
1680 else if (mddev->bitmap_info.offset > 0)
1681 mddev->bitmap_info.space =
1682 8 - mddev->bitmap_info.offset;
1683 else
1684 mddev->bitmap_info.space =
1685 -mddev->bitmap_info.offset;
1686 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001687
NeilBrownf6705572006-03-27 01:18:11 -08001688 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1689 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1690 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1691 mddev->new_level = le32_to_cpu(sb->new_level);
1692 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001693 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001694 if (mddev->delta_disks < 0 ||
1695 (mddev->delta_disks == 0 &&
1696 (le32_to_cpu(sb->feature_map)
1697 & MD_FEATURE_RESHAPE_BACKWARDS)))
1698 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001699 } else {
1700 mddev->reshape_position = MaxSector;
1701 mddev->delta_disks = 0;
1702 mddev->new_level = mddev->level;
1703 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001704 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001705 }
1706
Song Liu486b0f72016-08-19 15:34:01 -07001707 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001708 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001709
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001710 if (le32_to_cpu(sb->feature_map) &
1711 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001712 if (le32_to_cpu(sb->feature_map) &
1713 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1714 return -EINVAL;
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001715 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1716 (le32_to_cpu(sb->feature_map) &
1717 MD_FEATURE_MULTIPLE_PPLS))
1718 return -EINVAL;
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001719 set_bit(MD_HAS_PPL, &mddev->flags);
1720 }
NeilBrown41158c72005-06-21 17:17:25 -07001721 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001722 /* Insist of good event counter while assembling, except for
1723 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001725 if (rdev->desc_nr >= 0 &&
1726 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001727 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1728 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001729 if (ev1 < mddev->events)
1730 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001731 } else if (mddev->bitmap) {
1732 /* If adding to array with a bitmap, then we can accept an
1733 * older device, but not too old.
1734 */
NeilBrown41158c72005-06-21 17:17:25 -07001735 if (ev1 < mddev->bitmap->events_cleared)
1736 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001737 if (ev1 < mddev->events)
1738 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001739 } else {
1740 if (ev1 < mddev->events)
1741 /* just a hot-add of a new device, leave raid_disk at -1 */
1742 return 0;
1743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (mddev->level != LEVEL_MULTIPATH) {
1745 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001746 if (rdev->desc_nr < 0 ||
1747 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001748 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001749 rdev->desc_nr = -1;
1750 } else
1751 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001753 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001755 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001756 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 break;
Song Liubac624f2015-08-13 14:31:55 -07001758 case MD_DISK_ROLE_JOURNAL: /* journal device */
1759 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1760 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001761 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001762 return -EINVAL;
1763 }
1764 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001765 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001766 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001767 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 default:
NeilBrownf4667222013-12-09 12:04:56 +11001769 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001770 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001771 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001772 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001773 if (!(le32_to_cpu(sb->feature_map) &
1774 MD_FEATURE_RECOVERY_BITMAP))
1775 rdev->saved_raid_disk = -1;
1776 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001777 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 rdev->raid_disk = role;
1779 break;
1780 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001781 if (sb->devflags & WriteMostly1)
1782 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001783 if (sb->devflags & FailFast1)
1784 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001785 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1786 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001787 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001788 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return 0;
1791}
1792
NeilBrownfd01b882011-10-11 16:47:53 +11001793static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
1795 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001796 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 int max_dev, i;
1798 /* make rdev->sb match mddev and rdev data. */
1799
Namhyung Kim65a06f062011-07-27 11:00:36 +10001800 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 sb->feature_map = 0;
1803 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001804 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 memset(sb->pad3, 0, sizeof(sb->pad3));
1806
1807 sb->utime = cpu_to_le64((__u64)mddev->utime);
1808 sb->events = cpu_to_le64(mddev->events);
1809 if (mddev->in_sync)
1810 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001811 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1812 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 else
1814 sb->resync_offset = cpu_to_le64(0);
1815
NeilBrown1c05b4b2006-10-21 10:24:08 -07001816 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001817
NeilBrownf0ca3402006-02-02 14:28:04 -08001818 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001819 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001820 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001821 sb->level = cpu_to_le32(mddev->level);
1822 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11001823 if (test_bit(FailFast, &rdev->flags))
1824 sb->devflags |= FailFast1;
1825 else
1826 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08001827
NeilBrownaeb9b2112011-08-25 14:43:08 +10001828 if (test_bit(WriteMostly, &rdev->flags))
1829 sb->devflags |= WriteMostly1;
1830 else
1831 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001832 sb->data_offset = cpu_to_le64(rdev->data_offset);
1833 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001834
NeilBrownc3d97142009-12-14 12:49:52 +11001835 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1836 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001837 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001838 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001839
Shaohua Lif2076e72015-10-08 21:54:12 -07001840 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001841 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001842 sb->feature_map |=
1843 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1844 sb->recovery_offset =
1845 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001846 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1847 sb->feature_map |=
1848 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001849 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001850 /* Note: recovery_offset and journal_tail share space */
1851 if (test_bit(Journal, &rdev->flags))
1852 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001853 if (test_bit(Replacement, &rdev->flags))
1854 sb->feature_map |=
1855 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001856
NeilBrownf6705572006-03-27 01:18:11 -08001857 if (mddev->reshape_position != MaxSector) {
1858 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1859 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1860 sb->new_layout = cpu_to_le32(mddev->new_layout);
1861 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1862 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001863 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001864 if (mddev->delta_disks == 0 &&
1865 mddev->reshape_backwards)
1866 sb->feature_map
1867 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001868 if (rdev->new_data_offset != rdev->data_offset) {
1869 sb->feature_map
1870 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1871 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1872 - rdev->data_offset));
1873 }
NeilBrownf6705572006-03-27 01:18:11 -08001874 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001875
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001876 if (mddev_is_clustered(mddev))
1877 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1878
NeilBrown2699b672011-07-28 11:31:47 +10001879 if (rdev->badblocks.count == 0)
1880 /* Nothing to do for bad blocks*/ ;
1881 else if (sb->bblog_offset == 0)
1882 /* Cannot record bad blocks on this device */
1883 md_error(mddev, rdev);
1884 else {
1885 struct badblocks *bb = &rdev->badblocks;
1886 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1887 u64 *p = bb->page;
1888 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1889 if (bb->changed) {
1890 unsigned seq;
1891
1892retry:
1893 seq = read_seqbegin(&bb->lock);
1894
1895 memset(bbp, 0xff, PAGE_SIZE);
1896
1897 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001898 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001899 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1900 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001901 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001902 }
NeilBrownd0962932012-03-19 12:46:41 +11001903 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001904 if (read_seqretry(&bb->lock, seq))
1905 goto retry;
1906
1907 bb->sector = (rdev->sb_start +
1908 (int)le32_to_cpu(sb->bblog_offset));
1909 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001910 }
1911 }
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001914 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (rdev2->desc_nr+1 > max_dev)
1916 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001917
NeilBrown70471da2009-08-03 10:59:57 +10001918 if (max_dev > le32_to_cpu(sb->max_dev)) {
1919 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001920 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001921 rdev->sb_size = max_dev * 2 + 256;
1922 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1923 if (rdev->sb_size & bmask)
1924 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001925 } else
1926 max_dev = le32_to_cpu(sb->max_dev);
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 for (i=0; i<max_dev;i++)
Lidong Zhong8df72022017-06-12 10:45:55 +08001929 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001930
Song Liua97b7892015-10-08 21:54:09 -07001931 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1932 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001934 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001935 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
1936 sb->feature_map |=
1937 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
1938 else
1939 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001940 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
1941 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
1942 }
1943
NeilBrowndafb20f2012-03-19 12:46:39 +11001944 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001946 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001947 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001948 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001950 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001951 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001952 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001953 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 else
Song Liuc4d4c912015-08-13 14:31:54 -07001955 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 sb->sb_csum = calc_sb_1_csum(sb);
1959}
1960
Chris Webb0cd17fe2008-06-28 08:31:46 +10001961static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001962super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001963{
1964 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001965 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001966 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001967 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001968 if (rdev->data_offset != rdev->new_data_offset)
1969 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001970 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001971 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001972 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001973 max_sectors -= rdev->data_offset;
1974 if (!num_sectors || num_sectors > max_sectors)
1975 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001976 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001977 /* minor version 0 with bitmap we can't move */
1978 return 0;
1979 } else {
1980 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001981 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001982 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001983 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001984 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001985 if (!num_sectors || num_sectors > max_sectors)
1986 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001987 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001988 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001989 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001990 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08001991 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10001992 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11001993 do {
1994 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1995 rdev->sb_page);
1996 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001997 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001998
1999}
2000
2001static int
2002super_1_allow_new_offset(struct md_rdev *rdev,
2003 unsigned long long new_offset)
2004{
2005 /* All necessary checks on new >= old have been done */
2006 struct bitmap *bitmap;
2007 if (new_offset >= rdev->data_offset)
2008 return 1;
2009
2010 /* with 1.0 metadata, there is no metadata to tread on
2011 * so we can always move back */
2012 if (rdev->mddev->minor_version == 0)
2013 return 1;
2014
2015 /* otherwise we must be sure not to step on
2016 * any metadata, so stay:
2017 * 36K beyond start of superblock
2018 * beyond end of badblocks
2019 * beyond write-intent bitmap
2020 */
2021 if (rdev->sb_start + (32+4)*2 > new_offset)
2022 return 0;
2023 bitmap = rdev->mddev->bitmap;
2024 if (bitmap && !rdev->mddev->bitmap_info.file &&
2025 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10002026 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10002027 return 0;
2028 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2029 return 0;
2030
2031 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002032}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Adrian Bunk75c96f82005-05-05 16:16:09 -07002034static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 [0] = {
2036 .name = "0.90.0",
2037 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002038 .load_super = super_90_load,
2039 .validate_super = super_90_validate,
2040 .sync_super = super_90_sync,
2041 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002042 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 },
2044 [1] = {
2045 .name = "md-1",
2046 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002047 .load_super = super_1_load,
2048 .validate_super = super_1_validate,
2049 .sync_super = super_1_sync,
2050 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002051 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 },
2053};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
NeilBrownfd01b882011-10-11 16:47:53 +11002055static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05002056{
2057 if (mddev->sync_super) {
2058 mddev->sync_super(mddev, rdev);
2059 return;
2060 }
2061
2062 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2063
2064 super_types[mddev->major_version].sync_super(mddev, rdev);
2065}
2066
NeilBrownfd01b882011-10-11 16:47:53 +11002067static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
NeilBrown3cb03002011-10-11 16:45:26 +11002069 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
NeilBrown4b809912008-07-21 17:05:25 +10002071 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07002072 rdev_for_each_rcu(rdev, mddev1) {
2073 if (test_bit(Faulty, &rdev->flags) ||
2074 test_bit(Journal, &rdev->flags) ||
2075 rdev->raid_disk == -1)
2076 continue;
2077 rdev_for_each_rcu(rdev2, mddev2) {
2078 if (test_bit(Faulty, &rdev2->flags) ||
2079 test_bit(Journal, &rdev2->flags) ||
2080 rdev2->raid_disk == -1)
2081 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002082 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002083 rdev2->bdev->bd_contains) {
2084 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002085 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002086 }
Song Liu0b020e82015-09-03 23:00:35 -07002087 }
2088 }
NeilBrown4b809912008-07-21 17:05:25 +10002089 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return 0;
2091}
2092
2093static LIST_HEAD(pending_raid_disks);
2094
Andre Nollac5e7112009-08-03 10:59:47 +10002095/*
2096 * Try to register data integrity profile for an mddev
2097 *
2098 * This is called when an array is started and after a disk has been kicked
2099 * from the array. It only succeeds if all working and active component devices
2100 * are integrity capable with matching profiles.
2101 */
NeilBrownfd01b882011-10-11 16:47:53 +11002102int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002103{
NeilBrown3cb03002011-10-11 16:45:26 +11002104 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002105
Andre Nollac5e7112009-08-03 10:59:47 +10002106 if (list_empty(&mddev->disks))
2107 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002108 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2109 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002110 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002111 /* skip spares and non-functional disks */
2112 if (test_bit(Faulty, &rdev->flags))
2113 continue;
2114 if (rdev->raid_disk < 0)
2115 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002116 if (!reference) {
2117 /* Use the first rdev as the reference */
2118 reference = rdev;
2119 continue;
2120 }
2121 /* does this rdev's profile match the reference profile? */
2122 if (blk_integrity_compare(reference->bdev->bd_disk,
2123 rdev->bdev->bd_disk) < 0)
2124 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002125 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002126 if (!reference || !bdev_get_integrity(reference->bdev))
2127 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002128 /*
2129 * All component devices are integrity capable and have matching
2130 * profiles, register the common profile for the md device.
2131 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002132 blk_integrity_register(mddev->gendisk,
2133 bdev_get_integrity(reference->bdev));
2134
NeilBrown9d487392016-11-02 14:16:49 +11002135 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Kent Overstreetafeee512018-05-20 18:25:52 -04002136 if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002137 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002138 mdname(mddev));
2139 return -EINVAL;
2140 }
Andre Nollac5e7112009-08-03 10:59:47 +10002141 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002142}
Andre Nollac5e7112009-08-03 10:59:47 +10002143EXPORT_SYMBOL(md_integrity_register);
2144
Dan Williams1501efa2016-01-13 16:00:07 -08002145/*
2146 * Attempt to add an rdev, but only if it is consistent with the current
2147 * integrity profile
2148 */
2149int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002150{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002151 struct blk_integrity *bi_rdev;
2152 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002153 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002154
2155 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002156 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002157
2158 bi_rdev = bdev_get_integrity(rdev->bdev);
2159 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002160
2161 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002162 return 0;
2163
2164 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002165 pr_err("%s: incompatible integrity profile for %s\n",
2166 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002167 return -ENXIO;
2168 }
2169
2170 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002171}
2172EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002173
NeilBrownf72ffdd2014-09-30 14:23:59 +10002174static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002176 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002177 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002178 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Dan Williams11e2ede2008-04-30 00:52:32 -07002180 /* prevent duplicates */
2181 if (find_rdev(mddev, rdev->bdev->bd_dev))
2182 return -EEXIST;
2183
NeilBrown97b20ef2017-04-13 08:53:48 +10002184 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2185 mddev->pers)
2186 return -EROFS;
2187
Andre Nolldd8ac332009-03-31 14:33:13 +11002188 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002189 if (!test_bit(Journal, &rdev->flags) &&
2190 rdev->sectors &&
2191 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002192 if (mddev->pers) {
2193 /* Cannot change size, so fail
2194 * If mddev->level <= 0, then we don't care
2195 * about aligning sizes (e.g. linear)
2196 */
2197 if (mddev->level > 0)
2198 return -ENOSPC;
2199 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002200 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 /* Verify rdev->desc_nr is unique.
2204 * If it is -1, assign a free number, else
2205 * check number is not in use
2206 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002207 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (rdev->desc_nr < 0) {
2209 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002210 if (mddev->pers)
2211 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002212 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 choice++;
2214 rdev->desc_nr = choice;
2215 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002216 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002217 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002221 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002222 if (!test_bit(Journal, &rdev->flags) &&
2223 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002224 pr_warn("md: %s: array is limited to %d devices\n",
2225 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002226 return -EBUSY;
2227 }
NeilBrown19133a42005-11-08 21:39:35 -08002228 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002229 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002232 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002233
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002234 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002235 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002236
Tejun Heo0762b8b2008-08-25 19:56:12 +09002237 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002238 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2239 /* failure here is OK */;
2240 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002241
NeilBrown4b809912008-07-21 17:05:25 +10002242 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002243 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002244
2245 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002246 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002249
2250 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002251 pr_warn("md: failed to register dev-%s for %s\n",
2252 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002253 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
2255
NeilBrown177a99b2008-02-06 01:39:56 -08002256static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002257{
NeilBrown3cb03002011-10-11 16:45:26 +11002258 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002259 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002260 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002261}
2262
NeilBrownf72ffdd2014-09-30 14:23:59 +10002263static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
2265 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002266
Tejun Heo49731ba2011-01-14 18:43:57 +01002267 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002268 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002269 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002271 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002272 sysfs_put(rdev->sysfs_state);
2273 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002274 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002275 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002276 * writing to 'remove' to "dev/state". We also need
2277 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002278 */
NeilBrown4b809912008-07-21 17:05:25 +10002279 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002280 INIT_WORK(&rdev->del_work, md_delayed_delete);
2281 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002282 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
2284
2285/*
2286 * prevent the device from being mounted, repartitioned or
2287 * otherwise reused by a RAID array (or any other kernel
2288 * subsystem), by bd_claiming the device.
2289 */
NeilBrown3cb03002011-10-11 16:45:26 +11002290static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
2292 int err = 0;
2293 struct block_device *bdev;
2294 char b[BDEVNAME_SIZE];
2295
Tejun Heod4d77622010-11-13 11:55:18 +01002296 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002297 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002299 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return PTR_ERR(bdev);
2301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 rdev->bdev = bdev;
2303 return err;
2304}
2305
NeilBrown3cb03002011-10-11 16:45:26 +11002306static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
2308 struct block_device *bdev = rdev->bdev;
2309 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002310 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
2313void md_autodetect_dev(dev_t dev);
2314
NeilBrownf72ffdd2014-09-30 14:23:59 +10002315static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
2317 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002318
NeilBrown9d487392016-11-02 14:16:49 +11002319 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002320 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002322 if (test_bit(AutoDetected, &rdev->flags))
2323 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324#endif
2325 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002326 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002329void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
2331 unbind_rdev_from_array(rdev);
2332 export_rdev(rdev);
2333}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002334EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
NeilBrownfd01b882011-10-11 16:47:53 +11002336static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
NeilBrown0638bb02014-09-25 17:43:47 +10002338 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
NeilBrown0638bb02014-09-25 17:43:47 +10002340 while (!list_empty(&mddev->disks)) {
2341 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2342 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002343 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 mddev->raid_disks = 0;
2346 mddev->major_version = 0;
2347}
2348
NeilBrown6497709b2017-03-15 14:05:14 +11002349static bool set_in_sync(struct mddev *mddev)
2350{
Shaohua Liefa4b772017-10-18 22:08:13 -07002351 lockdep_assert_held(&mddev->lock);
NeilBrown4ad23a972017-03-15 14:05:14 +11002352 if (!mddev->in_sync) {
2353 mddev->sync_checkers++;
2354 spin_unlock(&mddev->lock);
2355 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2356 spin_lock(&mddev->lock);
2357 if (!mddev->in_sync &&
2358 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002359 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002360 /*
2361 * Ensure ->in_sync is visible before we clear
2362 * ->sync_checkers.
2363 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002364 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002365 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2366 sysfs_notify_dirent_safe(mddev->sysfs_state);
2367 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002368 if (--mddev->sync_checkers == 0)
2369 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002370 }
2371 if (mddev->safemode == 1)
2372 mddev->safemode = 0;
2373 return mddev->in_sync;
2374}
2375
NeilBrownf72ffdd2014-09-30 14:23:59 +10002376static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
NeilBrown42543762006-06-26 00:27:57 -07002378 /* Update each superblock (in-memory image), but
2379 * if we are allowed to, skip spares which already
2380 * have the right event counter, or have one earlier
2381 * (which would mean they aren't being marked as dirty
2382 * with the rest of the array)
2383 */
NeilBrown3cb03002011-10-11 16:45:26 +11002384 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002385 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002386 if (rdev->sb_events == mddev->events ||
2387 (nospares &&
2388 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002389 rdev->sb_events+1 == mddev->events)) {
2390 /* Don't update this superblock */
2391 rdev->sb_loaded = 2;
2392 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002393 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002394 rdev->sb_loaded = 1;
2395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
2397}
2398
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002399static bool does_sb_need_changing(struct mddev *mddev)
2400{
2401 struct md_rdev *rdev;
2402 struct mdp_superblock_1 *sb;
2403 int role;
2404
2405 /* Find a good rdev */
2406 rdev_for_each(rdev, mddev)
2407 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2408 break;
2409
2410 /* No good device found. */
2411 if (!rdev)
2412 return false;
2413
2414 sb = page_address(rdev->sb_page);
2415 /* Check if a device has become faulty or a spare become active */
2416 rdev_for_each(rdev, mddev) {
2417 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2418 /* Device activated? */
2419 if (role == 0xffff && rdev->raid_disk >=0 &&
2420 !test_bit(Faulty, &rdev->flags))
2421 return true;
2422 /* Device turned faulty? */
2423 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2424 return true;
2425 }
2426
2427 /* Check if any mddev parameters have changed */
2428 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2429 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002430 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002431 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2432 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2433 return true;
2434
2435 return false;
2436}
2437
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002438void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
NeilBrown3cb03002011-10-11 16:45:26 +11002440 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002441 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002442 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002443 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002444 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
NeilBrownd87f0642013-04-24 11:42:40 +10002446 if (mddev->ro) {
2447 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002448 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002449 return;
2450 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002451
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002452repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002453 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002454 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002455 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002456 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002457 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002458 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002459 /* Has someone else has updated the sb */
2460 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002461 if (ret == 0)
2462 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002463 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2464 BIT(MD_SB_CHANGE_DEVS) |
2465 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002466 return;
2467 }
2468 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002469
NeilBrowndb0505d2017-10-17 16:18:36 +11002470 /*
2471 * First make sure individual recovery_offsets are correct
2472 * curr_resync_completed can only be used during recovery.
2473 * During reshape/resync it might use array-addresses rather
2474 * that device addresses.
2475 */
NeilBrowndafb20f2012-03-19 12:46:39 +11002476 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002477 if (rdev->raid_disk >= 0 &&
2478 mddev->delta_disks >= 0 &&
NeilBrowndb0505d2017-10-17 16:18:36 +11002479 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2480 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2481 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002482 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002483 !test_bit(In_sync, &rdev->flags) &&
2484 mddev->curr_resync_completed > rdev->recovery_offset)
2485 rdev->recovery_offset = mddev->curr_resync_completed;
2486
NeilBrownf72ffdd2014-09-30 14:23:59 +10002487 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002488 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002489 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2490 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002491 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002492 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002493 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002494 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002495 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002496 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002497 md_error(mddev, rdev);
2498 }
2499 clear_bit(Blocked, &rdev->flags);
2500 clear_bit(BlockedBadBlocks, &rdev->flags);
2501 wake_up(&rdev->blocked_wait);
2502 }
2503 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002504 wake_up(&mddev->sb_wait);
2505 return;
2506 }
2507
NeilBrown85572d72014-12-15 12:56:56 +11002508 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002509
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002510 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002511
Shaohua Li29530792016-12-08 15:48:19 -08002512 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002513 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002514 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002515 /* just a clean<-> dirty transition, possibly leave spares alone,
2516 * though if events isn't the right even/odd, we will have to do
2517 * spares after all
2518 */
2519 nospares = 1;
2520 if (force_change)
2521 nospares = 0;
2522 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002523 /* If the array is degraded, then skipping spares is both
2524 * dangerous and fairly pointless.
2525 * Dangerous because a device that was removed from the array
2526 * might have a event_count that still looks up-to-date,
2527 * so it can be re-added without a resync.
2528 * Pointless because if there are any spares to skip,
2529 * then a recovery will happen and soon that array won't
2530 * be degraded any more and the spare can go back to sleep then.
2531 */
NeilBrown850b2b422006-10-03 01:15:46 -07002532 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002533
NeilBrown06d91a52005-06-21 17:17:12 -07002534 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002535
2536 /* If this is just a dirty<->clean transition, and the array is clean
2537 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002538 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002539 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002540 && mddev->can_decrease_events
2541 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002542 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002543 mddev->can_decrease_events = 0;
2544 } else {
NeilBrown42543762006-06-26 00:27:57 -07002545 /* otherwise we have to go forward and ... */
2546 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002547 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
NeilBrown403df472014-09-30 15:52:29 +10002550 /*
2551 * This 64-bit counter should never wrap.
2552 * Either we are in around ~1 trillion A.C., assuming
2553 * 1 reboot per second, or we have a bug...
2554 */
2555 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002556
NeilBrowndafb20f2012-03-19 12:46:39 +11002557 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002558 if (rdev->badblocks.changed)
2559 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002560 if (test_bit(Faulty, &rdev->flags))
2561 set_bit(FaultRecorded, &rdev->flags);
2562 }
NeilBrown2699b672011-07-28 11:31:47 +10002563
NeilBrowne6910632008-02-06 01:39:51 -08002564 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002565 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
NeilBrown36a4e1f2011-10-07 14:23:17 +11002567 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2568 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Shaohua Li504634f2016-11-18 09:44:08 -08002570 if (mddev->queue)
2571 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002572rewrite:
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002573 md_bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002574 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002576
NeilBrown42543762006-06-26 00:27:57 -07002577 if (rdev->sb_loaded != 1)
2578 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
NeilBrownf4667222013-12-09 12:04:56 +11002580 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002581 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002582 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002583 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002584 pr_debug("md: (write) %s's sb offset: %llu\n",
2585 bdevname(rdev->bdev, b),
2586 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002587 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002588 if (rdev->badblocks.size) {
2589 md_super_write(mddev, rdev,
2590 rdev->badblocks.sector,
2591 rdev->badblocks.size << 9,
2592 rdev->bb_page);
2593 rdev->badblocks.size = 0;
2594 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002595
NeilBrownf4667222013-12-09 12:04:56 +11002596 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002597 pr_debug("md: %s (skipping faulty)\n",
2598 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002599
NeilBrown7bfa19f2005-06-21 17:17:28 -07002600 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 /* only need to write one superblock... */
2602 break;
2603 }
NeilBrown46533ff2016-11-18 16:16:11 +11002604 if (md_super_wait(mddev) < 0)
2605 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002606 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002607
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002608 if (mddev_is_clustered(mddev) && ret == 0)
2609 md_cluster_ops->metadata_update_finish(mddev);
2610
NeilBrown850b2b422006-10-03 01:15:46 -07002611 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002612 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2613 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002614 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002615 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002616 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002617 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2618 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002619
NeilBrowndafb20f2012-03-19 12:46:39 +11002620 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002621 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2622 clear_bit(Blocked, &rdev->flags);
2623
2624 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002625 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002626 clear_bit(BlockedBadBlocks, &rdev->flags);
2627 wake_up(&rdev->blocked_wait);
2628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002630EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002632static int add_bound_rdev(struct md_rdev *rdev)
2633{
2634 struct mddev *mddev = rdev->mddev;
2635 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002636 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002637
Shaohua Li87d4d912016-01-06 14:37:14 -08002638 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002639 /* If there is hot_add_disk but no hot_remove_disk
2640 * then added disks for geometry changes,
2641 * and should be added immediately.
2642 */
2643 super_types[mddev->major_version].
2644 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002645 if (add_journal)
2646 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002647 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002648 if (add_journal)
2649 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002650 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002651 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002652 return err;
2653 }
2654 }
2655 sysfs_notify_dirent_safe(rdev->sysfs_state);
2656
Shaohua Li29530792016-12-08 15:48:19 -08002657 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002658 if (mddev->degraded)
2659 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2660 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2661 md_new_event(mddev);
2662 md_wakeup_thread(mddev->thread);
2663 return 0;
2664}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Andre Noll7f6ce762008-03-23 18:34:54 +01002666/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002667 * We want to accept with case. For this we use cmd_match.
2668 */
2669static int cmd_match(const char *cmd, const char *str)
2670{
2671 /* See if cmd, written into a sysfs file, matches
2672 * str. They must either be the same, or cmd can
2673 * have a trailing newline
2674 */
2675 while (*cmd && *str && *cmd == *str) {
2676 cmd++;
2677 str++;
2678 }
2679 if (*cmd == '\n')
2680 cmd++;
2681 if (*str || *cmd)
2682 return 0;
2683 return 1;
2684}
2685
NeilBrown86e6ffd2005-11-08 21:39:24 -08002686struct rdev_sysfs_entry {
2687 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002688 ssize_t (*show)(struct md_rdev *, char *);
2689 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002690};
2691
2692static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002693state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002694{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002695 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002696 size_t len = 0;
Mark Rutland6aa7de02017-10-23 14:07:29 -07002697 unsigned long flags = READ_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002698
NeilBrown758bfc82014-12-15 12:56:59 +11002699 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002700 (!test_bit(ExternalBbl, &flags) &&
2701 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002702 len += sprintf(page+len, "faulty%s", sep);
2703 if (test_bit(In_sync, &flags))
2704 len += sprintf(page+len, "in_sync%s", sep);
2705 if (test_bit(Journal, &flags))
2706 len += sprintf(page+len, "journal%s", sep);
2707 if (test_bit(WriteMostly, &flags))
2708 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002709 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002710 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002711 && !test_bit(Faulty, &flags)))
2712 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002713 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002714 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002715 !test_bit(In_sync, &flags))
2716 len += sprintf(page+len, "spare%s", sep);
2717 if (test_bit(WriteErrorSeen, &flags))
2718 len += sprintf(page+len, "write_error%s", sep);
2719 if (test_bit(WantReplacement, &flags))
2720 len += sprintf(page+len, "want_replacement%s", sep);
2721 if (test_bit(Replacement, &flags))
2722 len += sprintf(page+len, "replacement%s", sep);
2723 if (test_bit(ExternalBbl, &flags))
2724 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002725 if (test_bit(FailFast, &flags))
2726 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002727
2728 if (len)
2729 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002730
NeilBrown86e6ffd2005-11-08 21:39:24 -08002731 return len+sprintf(page+len, "\n");
2732}
2733
NeilBrown45dc2de2006-06-26 00:27:58 -07002734static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002735state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002736{
2737 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002738 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002739 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002740 * writemostly - sets write_mostly
2741 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002742 * blocked - sets the Blocked flags
2743 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002744 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002745 * -insync - clear Insync for a device with a slot assigned,
2746 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002747 * write_error - sets WriteErrorSeen
2748 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002749 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002750 */
2751 int err = -EINVAL;
2752 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2753 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002754 if (test_bit(Faulty, &rdev->flags))
2755 err = 0;
2756 else
2757 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002758 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002759 if (rdev->mddev->pers) {
2760 clear_bit(Blocked, &rdev->flags);
2761 remove_and_add_spares(rdev->mddev, rdev);
2762 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002763 if (rdev->raid_disk >= 0)
2764 err = -EBUSY;
2765 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002766 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002767 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002768 if (mddev_is_clustered(mddev))
2769 err = md_cluster_ops->remove_disk(mddev, rdev);
2770
2771 if (err == 0) {
2772 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002773 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002774 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002775 md_wakeup_thread(mddev->thread);
2776 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002777 md_new_event(mddev);
2778 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002779 }
NeilBrownf6556752006-06-26 00:28:01 -07002780 } else if (cmd_match(buf, "writemostly")) {
2781 set_bit(WriteMostly, &rdev->flags);
2782 err = 0;
2783 } else if (cmd_match(buf, "-writemostly")) {
2784 clear_bit(WriteMostly, &rdev->flags);
2785 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002786 } else if (cmd_match(buf, "blocked")) {
2787 set_bit(Blocked, &rdev->flags);
2788 err = 0;
2789 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002790 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002791 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002792 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002793 /* metadata handler doesn't understand badblocks,
2794 * so we need to fail the device
2795 */
2796 md_error(rdev->mddev, rdev);
2797 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002798 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002799 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002800 wake_up(&rdev->blocked_wait);
2801 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2802 md_wakeup_thread(rdev->mddev->thread);
2803
2804 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002805 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2806 set_bit(In_sync, &rdev->flags);
2807 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11002808 } else if (cmd_match(buf, "failfast")) {
2809 set_bit(FailFast, &rdev->flags);
2810 err = 0;
2811 } else if (cmd_match(buf, "-failfast")) {
2812 clear_bit(FailFast, &rdev->flags);
2813 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002814 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2815 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002816 if (rdev->mddev->pers == NULL) {
2817 clear_bit(In_sync, &rdev->flags);
2818 rdev->saved_raid_disk = rdev->raid_disk;
2819 rdev->raid_disk = -1;
2820 err = 0;
2821 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002822 } else if (cmd_match(buf, "write_error")) {
2823 set_bit(WriteErrorSeen, &rdev->flags);
2824 err = 0;
2825 } else if (cmd_match(buf, "-write_error")) {
2826 clear_bit(WriteErrorSeen, &rdev->flags);
2827 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002828 } else if (cmd_match(buf, "want_replacement")) {
2829 /* Any non-spare device that is not a replacement can
2830 * become want_replacement at any time, but we then need to
2831 * check if recovery is needed.
2832 */
2833 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002834 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002835 !test_bit(Replacement, &rdev->flags))
2836 set_bit(WantReplacement, &rdev->flags);
2837 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2838 md_wakeup_thread(rdev->mddev->thread);
2839 err = 0;
2840 } else if (cmd_match(buf, "-want_replacement")) {
2841 /* Clearing 'want_replacement' is always allowed.
2842 * Once replacements starts it is too late though.
2843 */
2844 err = 0;
2845 clear_bit(WantReplacement, &rdev->flags);
2846 } else if (cmd_match(buf, "replacement")) {
2847 /* Can only set a device as a replacement when array has not
2848 * yet been started. Once running, replacement is automatic
2849 * from spares, or by assigning 'slot'.
2850 */
2851 if (rdev->mddev->pers)
2852 err = -EBUSY;
2853 else {
2854 set_bit(Replacement, &rdev->flags);
2855 err = 0;
2856 }
2857 } else if (cmd_match(buf, "-replacement")) {
2858 /* Similarly, can only clear Replacement before start */
2859 if (rdev->mddev->pers)
2860 err = -EBUSY;
2861 else {
2862 clear_bit(Replacement, &rdev->flags);
2863 err = 0;
2864 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002865 } else if (cmd_match(buf, "re-add")) {
NeilBrown011abdc2018-04-26 14:46:29 +10002866 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
2867 rdev->saved_raid_disk >= 0) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002868 /* clear_bit is performed _after_ all the devices
2869 * have their local Faulty bit cleared. If any writes
2870 * happen in the meantime in the local node, they
2871 * will land in the local bitmap, which will be synced
2872 * by this node eventually
2873 */
2874 if (!mddev_is_clustered(rdev->mddev) ||
2875 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2876 clear_bit(Faulty, &rdev->flags);
2877 err = add_bound_rdev(rdev);
2878 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002879 } else
2880 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002881 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2882 set_bit(ExternalBbl, &rdev->flags);
2883 rdev->badblocks.shift = 0;
2884 err = 0;
2885 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2886 clear_bit(ExternalBbl, &rdev->flags);
2887 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002888 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002889 if (!err)
2890 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002891 return err ? err : len;
2892}
NeilBrown80ca3a42006-07-10 04:44:18 -07002893static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002894__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002895
2896static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002897errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002898{
2899 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2900}
2901
2902static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002903errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002904{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002905 unsigned int n;
2906 int rv;
2907
2908 rv = kstrtouint(buf, 10, &n);
2909 if (rv < 0)
2910 return rv;
2911 atomic_set(&rdev->corrected_errors, n);
2912 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002913}
2914static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002915__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002916
NeilBrown014236d2006-01-06 00:20:55 -08002917static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002918slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002919{
Shaohua Lif2076e72015-10-08 21:54:12 -07002920 if (test_bit(Journal, &rdev->flags))
2921 return sprintf(page, "journal\n");
2922 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002923 return sprintf(page, "none\n");
2924 else
2925 return sprintf(page, "%d\n", rdev->raid_disk);
2926}
2927
2928static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002929slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002930{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002931 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002932 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002933
Shaohua Lif2076e72015-10-08 21:54:12 -07002934 if (test_bit(Journal, &rdev->flags))
2935 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002936 if (strncmp(buf, "none", 4)==0)
2937 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002938 else {
2939 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2940 if (err < 0)
2941 return err;
2942 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002943 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002944 /* Setting 'slot' on an active array requires also
2945 * updating the 'rd%d' link, and communicating
2946 * with the personality with ->hot_*_disk.
2947 * For now we only support removing
2948 * failed/spare devices. This normally happens automatically,
2949 * but not when the metadata is externally managed.
2950 */
NeilBrownc303da62008-02-06 01:39:51 -08002951 if (rdev->raid_disk == -1)
2952 return -EEXIST;
2953 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002954 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002955 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002956 clear_bit(Blocked, &rdev->flags);
2957 remove_and_add_spares(rdev->mddev, rdev);
2958 if (rdev->raid_disk >= 0)
2959 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002960 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2961 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002962 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002963 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002964 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002965 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002966 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002967
2968 if (rdev->raid_disk != -1)
2969 return -EBUSY;
2970
NeilBrownc6751b22011-02-02 11:57:13 +11002971 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2972 return -EBUSY;
2973
Neil Brown6c2fce22008-06-28 08:31:31 +10002974 if (rdev->mddev->pers->hot_add_disk == NULL)
2975 return -EINVAL;
2976
NeilBrownba1b41b2011-01-14 09:14:34 +11002977 if (slot >= rdev->mddev->raid_disks &&
2978 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2979 return -ENOSPC;
2980
Neil Brown6c2fce22008-06-28 08:31:31 +10002981 rdev->raid_disk = slot;
2982 if (test_bit(In_sync, &rdev->flags))
2983 rdev->saved_raid_disk = slot;
2984 else
2985 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002986 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002987 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002988 err = rdev->mddev->pers->
2989 hot_add_disk(rdev->mddev, rdev);
2990 if (err) {
2991 rdev->raid_disk = -1;
2992 return err;
2993 } else
2994 sysfs_notify_dirent_safe(rdev->sysfs_state);
2995 if (sysfs_link_rdev(rdev->mddev, rdev))
2996 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002997 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002998 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002999 if (slot >= rdev->mddev->raid_disks &&
3000 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08003001 return -ENOSPC;
3002 rdev->raid_disk = slot;
3003 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08003004 clear_bit(Faulty, &rdev->flags);
3005 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08003006 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003007 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08003008 }
NeilBrown014236d2006-01-06 00:20:55 -08003009 return len;
3010}
3011
NeilBrown014236d2006-01-06 00:20:55 -08003012static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07003013__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08003014
NeilBrown93c8cad2006-01-06 00:20:56 -08003015static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003016offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08003017{
Andrew Morton6961ece2006-01-06 00:20:59 -08003018 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08003019}
3020
3021static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003022offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08003023{
NeilBrownc6563a82012-05-21 09:27:00 +10003024 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003025 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003026 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10003027 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003028 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003029 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08003030 /* Must set offset before size, so overlap checks
3031 * can be sane */
3032 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08003033 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10003034 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08003035 return len;
3036}
3037
3038static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07003039__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08003040
NeilBrownc6563a82012-05-21 09:27:00 +10003041static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3042{
3043 return sprintf(page, "%llu\n",
3044 (unsigned long long)rdev->new_data_offset);
3045}
3046
3047static ssize_t new_offset_store(struct md_rdev *rdev,
3048 const char *buf, size_t len)
3049{
3050 unsigned long long new_offset;
3051 struct mddev *mddev = rdev->mddev;
3052
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003053 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10003054 return -EINVAL;
3055
NeilBrownf851b602014-12-11 10:02:10 +11003056 if (mddev->sync_thread ||
3057 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10003058 return -EBUSY;
3059 if (new_offset == rdev->data_offset)
3060 /* reset is always permitted */
3061 ;
3062 else if (new_offset > rdev->data_offset) {
3063 /* must not push array size beyond rdev_sectors */
3064 if (new_offset - rdev->data_offset
3065 + mddev->dev_sectors > rdev->sectors)
3066 return -E2BIG;
3067 }
3068 /* Metadata worries about other space details. */
3069
3070 /* decreasing the offset is inconsistent with a backwards
3071 * reshape.
3072 */
3073 if (new_offset < rdev->data_offset &&
3074 mddev->reshape_backwards)
3075 return -EINVAL;
3076 /* Increasing offset is inconsistent with forwards
3077 * reshape. reshape_direction should be set to
3078 * 'backwards' first.
3079 */
3080 if (new_offset > rdev->data_offset &&
3081 !mddev->reshape_backwards)
3082 return -EINVAL;
3083
3084 if (mddev->pers && mddev->persistent &&
3085 !super_types[mddev->major_version]
3086 .allow_new_offset(rdev, new_offset))
3087 return -E2BIG;
3088 rdev->new_data_offset = new_offset;
3089 if (new_offset > rdev->data_offset)
3090 mddev->reshape_backwards = 1;
3091 else if (new_offset < rdev->data_offset)
3092 mddev->reshape_backwards = 0;
3093
3094 return len;
3095}
3096static struct rdev_sysfs_entry rdev_new_offset =
3097__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3098
NeilBrown83303b62006-01-06 00:21:06 -08003099static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003100rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003101{
Andre Nolldd8ac332009-03-31 14:33:13 +11003102 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003103}
3104
NeilBrownc5d79ad2008-02-06 01:39:54 -08003105static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3106{
3107 /* check if two start/length pairs overlap */
3108 if (s1+l1 <= s2)
3109 return 0;
3110 if (s2+l2 <= s1)
3111 return 0;
3112 return 1;
3113}
3114
Dan Williamsb522adc2009-03-31 15:00:31 +11003115static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3116{
3117 unsigned long long blocks;
3118 sector_t new;
3119
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003120 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003121 return -EINVAL;
3122
3123 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3124 return -EINVAL; /* sector conversion overflow */
3125
3126 new = blocks * 2;
3127 if (new != blocks * 2)
3128 return -EINVAL; /* unsigned long long to sector_t overflow */
3129
3130 *sectors = new;
3131 return 0;
3132}
3133
NeilBrown83303b62006-01-06 00:21:06 -08003134static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003135rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003136{
NeilBrownfd01b882011-10-11 16:47:53 +11003137 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003138 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003139 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003140
Shaohua Lif2076e72015-10-08 21:54:12 -07003141 if (test_bit(Journal, &rdev->flags))
3142 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003143 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003144 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003145 if (rdev->data_offset != rdev->new_data_offset)
3146 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003147 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003148 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003149 sectors = super_types[my_mddev->major_version].
3150 rdev_size_change(rdev, sectors);
3151 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003152 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003153 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003154 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003155 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003156 if (!my_mddev->pers->resize)
3157 /* Cannot change size for RAID0 or Linear etc */
3158 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003159 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003160 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003161 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003162
Andre Nolldd8ac332009-03-31 14:33:13 +11003163 rdev->sectors = sectors;
3164 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003165 /* Need to check that all other rdevs with the same
3166 * ->bdev do not overlap. 'rcu' is sufficient to walk
3167 * the rdev lists safely.
3168 * This check does not provide a hard guarantee, it
3169 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003170 */
NeilBrownfd01b882011-10-11 16:47:53 +11003171 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003172 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003173 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003174
NeilBrown8b1afc32014-09-29 15:33:20 +10003175 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003176 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003177 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003178
NeilBrowndafb20f2012-03-19 12:46:39 +11003179 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003180 if (rdev->bdev == rdev2->bdev &&
3181 rdev != rdev2 &&
3182 overlaps(rdev->data_offset, rdev->sectors,
3183 rdev2->data_offset,
3184 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003185 overlap = 1;
3186 break;
3187 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003188 if (overlap) {
3189 mddev_put(mddev);
3190 break;
3191 }
3192 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003193 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003194 if (overlap) {
3195 /* Someone else could have slipped in a size
3196 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003197 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003198 * safe, and trust userspace not to race with
3199 * itself
3200 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003201 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003202 return -EBUSY;
3203 }
3204 }
NeilBrown83303b62006-01-06 00:21:06 -08003205 return len;
3206}
3207
3208static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003209__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003210
NeilBrown3cb03002011-10-11 16:45:26 +11003211static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003212{
3213 unsigned long long recovery_start = rdev->recovery_offset;
3214
3215 if (test_bit(In_sync, &rdev->flags) ||
3216 recovery_start == MaxSector)
3217 return sprintf(page, "none\n");
3218
3219 return sprintf(page, "%llu\n", recovery_start);
3220}
3221
NeilBrown3cb03002011-10-11 16:45:26 +11003222static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003223{
3224 unsigned long long recovery_start;
3225
3226 if (cmd_match(buf, "none"))
3227 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003228 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003229 return -EINVAL;
3230
3231 if (rdev->mddev->pers &&
3232 rdev->raid_disk >= 0)
3233 return -EBUSY;
3234
3235 rdev->recovery_offset = recovery_start;
3236 if (recovery_start == MaxSector)
3237 set_bit(In_sync, &rdev->flags);
3238 else
3239 clear_bit(In_sync, &rdev->flags);
3240 return len;
3241}
3242
3243static struct rdev_sysfs_entry rdev_recovery_start =
3244__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3245
Vishal Vermafc974ee2015-12-24 19:20:34 -07003246/* sysfs access to bad-blocks list.
3247 * We present two files.
3248 * 'bad-blocks' lists sector numbers and lengths of ranges that
3249 * are recorded as bad. The list is truncated to fit within
3250 * the one-page limit of sysfs.
3251 * Writing "sector length" to this file adds an acknowledged
3252 * bad block list.
3253 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3254 * been acknowledged. Writing to this file adds bad blocks
3255 * without acknowledging them. This is largely for testing.
3256 */
NeilBrown3cb03002011-10-11 16:45:26 +11003257static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003258{
3259 return badblocks_show(&rdev->badblocks, page, 0);
3260}
NeilBrown3cb03002011-10-11 16:45:26 +11003261static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003262{
NeilBrownde393cd2011-07-28 11:31:48 +10003263 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3264 /* Maybe that ack was all we needed */
3265 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3266 wake_up(&rdev->blocked_wait);
3267 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003268}
3269static struct rdev_sysfs_entry rdev_bad_blocks =
3270__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3271
NeilBrown3cb03002011-10-11 16:45:26 +11003272static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003273{
3274 return badblocks_show(&rdev->badblocks, page, 1);
3275}
NeilBrown3cb03002011-10-11 16:45:26 +11003276static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003277{
3278 return badblocks_store(&rdev->badblocks, page, len, 1);
3279}
3280static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3281__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3282
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003283static ssize_t
3284ppl_sector_show(struct md_rdev *rdev, char *page)
3285{
3286 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3287}
3288
3289static ssize_t
3290ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3291{
3292 unsigned long long sector;
3293
3294 if (kstrtoull(buf, 10, &sector) < 0)
3295 return -EINVAL;
3296 if (sector != (sector_t)sector)
3297 return -EINVAL;
3298
3299 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3300 rdev->raid_disk >= 0)
3301 return -EBUSY;
3302
3303 if (rdev->mddev->persistent) {
3304 if (rdev->mddev->major_version == 0)
3305 return -EINVAL;
3306 if ((sector > rdev->sb_start &&
3307 sector - rdev->sb_start > S16_MAX) ||
3308 (sector < rdev->sb_start &&
3309 rdev->sb_start - sector > -S16_MIN))
3310 return -EINVAL;
3311 rdev->ppl.offset = sector - rdev->sb_start;
3312 } else if (!rdev->mddev->external) {
3313 return -EBUSY;
3314 }
3315 rdev->ppl.sector = sector;
3316 return len;
3317}
3318
3319static struct rdev_sysfs_entry rdev_ppl_sector =
3320__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3321
3322static ssize_t
3323ppl_size_show(struct md_rdev *rdev, char *page)
3324{
3325 return sprintf(page, "%u\n", rdev->ppl.size);
3326}
3327
3328static ssize_t
3329ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3330{
3331 unsigned int size;
3332
3333 if (kstrtouint(buf, 10, &size) < 0)
3334 return -EINVAL;
3335
3336 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3337 rdev->raid_disk >= 0)
3338 return -EBUSY;
3339
3340 if (rdev->mddev->persistent) {
3341 if (rdev->mddev->major_version == 0)
3342 return -EINVAL;
3343 if (size > U16_MAX)
3344 return -EINVAL;
3345 } else if (!rdev->mddev->external) {
3346 return -EBUSY;
3347 }
3348 rdev->ppl.size = size;
3349 return len;
3350}
3351
3352static struct rdev_sysfs_entry rdev_ppl_size =
3353__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3354
NeilBrown86e6ffd2005-11-08 21:39:24 -08003355static struct attribute *rdev_default_attrs[] = {
3356 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003357 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003358 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003359 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003360 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003361 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003362 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003363 &rdev_bad_blocks.attr,
3364 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003365 &rdev_ppl_sector.attr,
3366 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003367 NULL,
3368};
3369static ssize_t
3370rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3371{
3372 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003373 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003374
3375 if (!entry->show)
3376 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003377 if (!rdev->mddev)
3378 return -EBUSY;
3379 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003380}
3381
3382static ssize_t
3383rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3384 const char *page, size_t length)
3385{
3386 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003387 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003388 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003389 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003390
3391 if (!entry->store)
3392 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003393 if (!capable(CAP_SYS_ADMIN))
3394 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003395 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003396 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003397 if (rdev->mddev == NULL)
3398 rv = -EBUSY;
3399 else
3400 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003401 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003402 }
3403 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003404}
3405
3406static void rdev_free(struct kobject *ko)
3407{
NeilBrown3cb03002011-10-11 16:45:26 +11003408 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003409 kfree(rdev);
3410}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003411static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003412 .show = rdev_attr_show,
3413 .store = rdev_attr_store,
3414};
3415static struct kobj_type rdev_ktype = {
3416 .release = rdev_free,
3417 .sysfs_ops = &rdev_sysfs_ops,
3418 .default_attrs = rdev_default_attrs,
3419};
3420
NeilBrown3cb03002011-10-11 16:45:26 +11003421int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003422{
3423 rdev->desc_nr = -1;
3424 rdev->saved_raid_disk = -1;
3425 rdev->raid_disk = -1;
3426 rdev->flags = 0;
3427 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003428 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003429 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003430 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003431 rdev->sb_loaded = 0;
3432 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003433 atomic_set(&rdev->nr_pending, 0);
3434 atomic_set(&rdev->read_errors, 0);
3435 atomic_set(&rdev->corrected_errors, 0);
3436
3437 INIT_LIST_HEAD(&rdev->same_set);
3438 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003439
3440 /* Add space to store bad block list.
3441 * This reserves the space even on arrays where it cannot
3442 * be used - I wonder if that matters
3443 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003444 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003445}
3446EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447/*
3448 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3449 *
3450 * mark the device faulty if:
3451 *
3452 * - the device is nonexistent (zero size)
3453 * - the device has no valid superblock
3454 *
3455 * a faulty rdev _never_ has rdev->sb set.
3456 */
NeilBrown3cb03002011-10-11 16:45:26 +11003457static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
3459 char b[BDEVNAME_SIZE];
3460 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003461 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 sector_t size;
3463
NeilBrown9ffae0c2006-01-06 00:20:32 -08003464 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003465 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
NeilBrown2230dfe2011-07-28 11:31:46 +10003468 err = md_rdev_init(rdev);
3469 if (err)
3470 goto abort_free;
3471 err = alloc_disk_sb(rdev);
3472 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 goto abort_free;
3474
NeilBrownc5d79ad2008-02-06 01:39:54 -08003475 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 if (err)
3477 goto abort_free;
3478
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003479 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003480
Mike Snitzer77304d22010-11-08 14:39:12 +01003481 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003483 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 bdevname(rdev->bdev,b));
3485 err = -EINVAL;
3486 goto abort_free;
3487 }
3488
3489 if (super_format >= 0) {
3490 err = super_types[super_format].
3491 load_super(rdev, NULL, super_minor);
3492 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003493 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003494 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003495 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 goto abort_free;
3497 }
3498 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003499 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 bdevname(rdev->bdev,b));
3501 goto abort_free;
3502 }
3503 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003504
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 return rdev;
3506
3507abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003508 if (rdev->bdev)
3509 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003510 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 kfree(rdev);
3512 return ERR_PTR(err);
3513}
3514
3515/*
3516 * Check a full RAID array for plausibility
3517 */
3518
NeilBrownf72ffdd2014-09-30 14:23:59 +10003519static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
3521 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003522 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 char b[BDEVNAME_SIZE];
3524
3525 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003526 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 switch (super_types[mddev->major_version].
3528 load_super(rdev, freshest, mddev->minor_version)) {
3529 case 1:
3530 freshest = rdev;
3531 break;
3532 case 0:
3533 break;
3534 default:
NeilBrown9d487392016-11-02 14:16:49 +11003535 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003537 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 }
3539
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 super_types[mddev->major_version].
3541 validate_super(mddev, freshest);
3542
3543 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003544 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003545 if (mddev->max_disks &&
3546 (rdev->desc_nr >= mddev->max_disks ||
3547 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003548 pr_warn("md: %s: %s: only %d devices permitted\n",
3549 mdname(mddev), bdevname(rdev->bdev, b),
3550 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003551 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003552 continue;
3553 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003554 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 if (super_types[mddev->major_version].
3556 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003557 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003559 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 continue;
3561 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 if (mddev->level == LEVEL_MULTIPATH) {
3564 rdev->desc_nr = i++;
3565 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003566 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003567 } else if (rdev->raid_disk >=
3568 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3569 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003570 rdev->raid_disk = -1;
3571 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 }
3573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
NeilBrown72e02072009-12-14 12:49:55 +11003576/* Read a fixed-point number.
3577 * Numbers in sysfs attributes should be in "standard" units where
3578 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003579 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003580 * milliseconds or jiffies.
3581 * This function takes a decimal number with a possible fractional
3582 * component, and produces an integer which is the result of
3583 * multiplying that number by 10^'scale'.
3584 * all without any floating-point arithmetic.
3585 */
3586int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3587{
3588 unsigned long result = 0;
3589 long decimals = -1;
3590 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3591 if (*cp == '.')
3592 decimals = 0;
3593 else if (decimals < scale) {
3594 unsigned int value;
3595 value = *cp - '0';
3596 result = result * 10 + value;
3597 if (decimals >= 0)
3598 decimals++;
3599 }
3600 cp++;
3601 }
3602 if (*cp == '\n')
3603 cp++;
3604 if (*cp)
3605 return -EINVAL;
3606 if (decimals < 0)
3607 decimals = 0;
3608 while (decimals < scale) {
3609 result *= 10;
3610 decimals ++;
3611 }
3612 *res = result;
3613 return 0;
3614}
3615
NeilBrowneae17012005-11-08 21:39:23 -08003616static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003617safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003618{
3619 int msec = (mddev->safemode_delay*1000)/HZ;
3620 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3621}
3622static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003623safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003624{
NeilBrown16f17b32006-06-26 00:27:37 -07003625 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003626
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003627 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003628 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003629 return -EINVAL;
3630 }
3631
NeilBrown72e02072009-12-14 12:49:55 +11003632 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003633 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003634 if (msec == 0)
3635 mddev->safemode_delay = 0;
3636 else {
NeilBrown19052c02008-08-05 15:54:13 +10003637 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003638 unsigned long new_delay = (msec*HZ)/1000;
3639
3640 if (new_delay == 0)
3641 new_delay = 1;
3642 mddev->safemode_delay = new_delay;
3643 if (new_delay < old_delay || old_delay == 0)
3644 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003645 }
3646 return len;
3647}
3648static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003649__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003650
3651static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003652level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003653{
NeilBrown36d091f2014-12-15 12:56:58 +11003654 struct md_personality *p;
3655 int ret;
3656 spin_lock(&mddev->lock);
3657 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003658 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003659 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003660 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003661 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003662 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003663 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003664 else
NeilBrown36d091f2014-12-15 12:56:58 +11003665 ret = 0;
3666 spin_unlock(&mddev->lock);
3667 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003668}
3669
NeilBrownd9d166c2006-01-06 00:20:51 -08003670static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003671level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003672{
Dan Williamsf2859af2010-05-02 10:04:16 -07003673 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003674 ssize_t rv;
3675 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003676 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003677 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003678 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003679 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003680
NeilBrown67918752014-12-15 12:57:01 +11003681 if (slen == 0 || slen >= sizeof(clevel))
3682 return -EINVAL;
3683
3684 rv = mddev_lock(mddev);
3685 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003686 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003687
3688 if (mddev->pers == NULL) {
3689 strncpy(mddev->clevel, buf, slen);
3690 if (mddev->clevel[slen-1] == '\n')
3691 slen--;
3692 mddev->clevel[slen] = 0;
3693 mddev->level = LEVEL_NONE;
3694 rv = len;
3695 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003696 }
NeilBrown67918752014-12-15 12:57:01 +11003697 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003698 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003699 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003700
3701 /* request to change the personality. Need to ensure:
3702 * - array is not engaged in resync/recovery/reshape
3703 * - old personality can be suspended
3704 * - new personality will access other array.
3705 */
3706
NeilBrown67918752014-12-15 12:57:01 +11003707 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003708 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003709 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003710 mddev->reshape_position != MaxSector ||
3711 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003712 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003713
NeilBrown67918752014-12-15 12:57:01 +11003714 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003715 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003716 pr_warn("md: %s: %s does not support online personality change\n",
3717 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003718 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003719 }
3720
3721 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003722 strncpy(clevel, buf, slen);
3723 if (clevel[slen-1] == '\n')
3724 slen--;
3725 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003726 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003727 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003728
Dan Williamsf2859af2010-05-02 10:04:16 -07003729 if (request_module("md-%s", clevel) != 0)
3730 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003731 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003732 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003733 if (!pers || !try_module_get(pers->owner)) {
3734 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003735 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003736 rv = -EINVAL;
3737 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003738 }
3739 spin_unlock(&pers_lock);
3740
3741 if (pers == mddev->pers) {
3742 /* Nothing to do! */
3743 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003744 rv = len;
3745 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003746 }
3747 if (!pers->takeover) {
3748 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003749 pr_warn("md: %s: %s does not support personality takeover\n",
3750 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003751 rv = -EINVAL;
3752 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003753 }
3754
NeilBrowndafb20f2012-03-19 12:46:39 +11003755 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003756 rdev->new_raid_disk = rdev->raid_disk;
3757
NeilBrown245f46c2009-03-31 14:39:39 +11003758 /* ->takeover must set new_* and/or delta_disks
3759 * if it succeeds, and may set them when it fails.
3760 */
3761 priv = pers->takeover(mddev);
3762 if (IS_ERR(priv)) {
3763 mddev->new_level = mddev->level;
3764 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003765 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003766 mddev->raid_disks -= mddev->delta_disks;
3767 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003768 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003769 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003770 pr_warn("md: %s: %s would not accept array\n",
3771 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003772 rv = PTR_ERR(priv);
3773 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003774 }
3775
3776 /* Looks like we have a winner */
3777 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003778 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003779
3780 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003781 oldpers = mddev->pers;
3782 oldpriv = mddev->private;
3783 mddev->pers = pers;
3784 mddev->private = priv;
3785 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3786 mddev->level = mddev->new_level;
3787 mddev->layout = mddev->new_layout;
3788 mddev->chunk_sectors = mddev->new_chunk_sectors;
3789 mddev->delta_disks = 0;
3790 mddev->reshape_backwards = 0;
3791 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003792 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003793
NeilBrowndb721d32014-12-15 12:56:58 +11003794 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003795 mddev->external) {
3796 /* We are converting from a no-redundancy array
3797 * to a redundancy array and metadata is managed
3798 * externally so we need to be sure that writes
3799 * won't block due to a need to transition
3800 * clean->dirty
3801 * until external management is started.
3802 */
3803 mddev->in_sync = 0;
3804 mddev->safemode_delay = 0;
3805 mddev->safemode = 0;
3806 }
3807
NeilBrowndb721d32014-12-15 12:56:58 +11003808 oldpers->free(mddev, oldpriv);
3809
3810 if (oldpers->sync_request == NULL &&
3811 pers->sync_request != NULL) {
3812 /* need to add the md_redundancy_group */
3813 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11003814 pr_warn("md: cannot register extra attributes for %s\n",
3815 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11003816 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3817 }
3818 if (oldpers->sync_request != NULL &&
3819 pers->sync_request == NULL) {
3820 /* need to remove the md_redundancy_group */
3821 if (mddev->to_remove == NULL)
3822 mddev->to_remove = &md_redundancy_group;
3823 }
3824
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003825 module_put(oldpers->owner);
3826
NeilBrowndafb20f2012-03-19 12:46:39 +11003827 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003828 if (rdev->raid_disk < 0)
3829 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003830 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003831 rdev->new_raid_disk = -1;
3832 if (rdev->new_raid_disk == rdev->raid_disk)
3833 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003834 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003835 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003836 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003837 if (rdev->raid_disk < 0)
3838 continue;
3839 if (rdev->new_raid_disk == rdev->raid_disk)
3840 continue;
3841 rdev->raid_disk = rdev->new_raid_disk;
3842 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003843 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003844 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003845 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11003846 pr_warn("md: cannot register rd%d for %s after level change\n",
3847 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003848 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003849 }
3850
NeilBrowndb721d32014-12-15 12:56:58 +11003851 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003852 /* this is now an array without redundancy, so
3853 * it must always be in_sync
3854 */
3855 mddev->in_sync = 1;
3856 del_timer_sync(&mddev->safemode_timer);
3857 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003858 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003859 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08003860 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003861 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003862 if (!mddev->thread)
3863 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003864 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003865 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003866 rv = len;
3867out_unlock:
3868 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003869 return rv;
3870}
3871
3872static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003873__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003874
NeilBrownd4dbd022006-06-26 00:27:59 -07003875static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003876layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003877{
3878 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003879 if (mddev->reshape_position != MaxSector &&
3880 mddev->layout != mddev->new_layout)
3881 return sprintf(page, "%d (%d)\n",
3882 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003883 return sprintf(page, "%d\n", mddev->layout);
3884}
3885
3886static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003887layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003888{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003889 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003890 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003891
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003892 err = kstrtouint(buf, 10, &n);
3893 if (err < 0)
3894 return err;
NeilBrown67918752014-12-15 12:57:01 +11003895 err = mddev_lock(mddev);
3896 if (err)
3897 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003898
NeilBrownb3546032009-03-31 14:56:41 +11003899 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003900 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003901 err = -EBUSY;
3902 else if (mddev->ro)
3903 err = -EROFS;
3904 else {
3905 mddev->new_layout = n;
3906 err = mddev->pers->check_reshape(mddev);
3907 if (err)
3908 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003909 }
NeilBrownb3546032009-03-31 14:56:41 +11003910 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003911 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003912 if (mddev->reshape_position == MaxSector)
3913 mddev->layout = n;
3914 }
NeilBrown67918752014-12-15 12:57:01 +11003915 mddev_unlock(mddev);
3916 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003917}
3918static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003919__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003920
NeilBrowneae17012005-11-08 21:39:23 -08003921static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003922raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003923{
NeilBrownbb636542005-11-08 21:39:45 -08003924 if (mddev->raid_disks == 0)
3925 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003926 if (mddev->reshape_position != MaxSector &&
3927 mddev->delta_disks != 0)
3928 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3929 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003930 return sprintf(page, "%d\n", mddev->raid_disks);
3931}
3932
NeilBrownfd01b882011-10-11 16:47:53 +11003933static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003934
3935static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003936raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003937{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003938 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003939 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003940
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003941 err = kstrtouint(buf, 10, &n);
3942 if (err < 0)
3943 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003944
NeilBrown67918752014-12-15 12:57:01 +11003945 err = mddev_lock(mddev);
3946 if (err)
3947 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003948 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003949 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003950 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003951 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003952 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003953
NeilBrown67918752014-12-15 12:57:01 +11003954 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003955 rdev_for_each(rdev, mddev) {
3956 if (olddisks < n &&
3957 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003958 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003959 if (olddisks > n &&
3960 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003961 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003962 }
NeilBrown67918752014-12-15 12:57:01 +11003963 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003964 mddev->delta_disks = n - olddisks;
3965 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003966 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003967 } else
NeilBrownda943b992006-01-06 00:20:54 -08003968 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003969out_unlock:
3970 mddev_unlock(mddev);
3971 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003972}
3973static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003974__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003975
NeilBrown24dd4692005-11-08 21:39:26 -08003976static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003977chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003978{
NeilBrown08a02ec2007-05-09 02:35:38 -07003979 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003980 mddev->chunk_sectors != mddev->new_chunk_sectors)
3981 return sprintf(page, "%d (%d)\n",
3982 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003983 mddev->chunk_sectors << 9);
3984 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003985}
3986
3987static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003988chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003989{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003990 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003991 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003992
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003993 err = kstrtoul(buf, 10, &n);
3994 if (err < 0)
3995 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003996
NeilBrown67918752014-12-15 12:57:01 +11003997 err = mddev_lock(mddev);
3998 if (err)
3999 return err;
NeilBrownb3546032009-03-31 14:56:41 +11004000 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004001 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004002 err = -EBUSY;
4003 else if (mddev->ro)
4004 err = -EROFS;
4005 else {
4006 mddev->new_chunk_sectors = n >> 9;
4007 err = mddev->pers->check_reshape(mddev);
4008 if (err)
4009 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10004010 }
NeilBrownb3546032009-03-31 14:56:41 +11004011 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10004012 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004013 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10004014 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004015 }
NeilBrown67918752014-12-15 12:57:01 +11004016 mddev_unlock(mddev);
4017 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08004018}
4019static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004020__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08004021
NeilBrowna94213b2006-06-26 00:28:00 -07004022static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004023resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07004024{
NeilBrownd1a7c502009-03-31 15:24:32 +11004025 if (mddev->recovery_cp == MaxSector)
4026 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07004027 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4028}
4029
4030static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004031resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07004032{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004033 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11004034 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004035
4036 if (cmd_match(buf, "none"))
4037 n = MaxSector;
4038 else {
4039 err = kstrtoull(buf, 10, &n);
4040 if (err < 0)
4041 return err;
4042 if (n != (sector_t)n)
4043 return -EINVAL;
4044 }
NeilBrowna94213b2006-06-26 00:28:00 -07004045
NeilBrown67918752014-12-15 12:57:01 +11004046 err = mddev_lock(mddev);
4047 if (err)
4048 return err;
NeilBrownb0986362011-05-11 15:52:21 +10004049 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11004050 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07004051
NeilBrown67918752014-12-15 12:57:01 +11004052 if (!err) {
4053 mddev->recovery_cp = n;
4054 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08004055 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004056 }
4057 mddev_unlock(mddev);
4058 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07004059}
4060static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10004061__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4062 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07004063
NeilBrown9e653b62006-06-26 00:27:58 -07004064/*
4065 * The array state can be:
4066 *
4067 * clear
4068 * No devices, no size, no level
4069 * Equivalent to STOP_ARRAY ioctl
4070 * inactive
4071 * May have some settings, but array is not active
4072 * all IO results in error
4073 * When written, doesn't tear down array, but just stops it
4074 * suspended (not supported yet)
4075 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01004076 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07004077 * readonly
4078 * no resync can happen. no superblocks get written.
4079 * write requests fail
4080 * read-auto
4081 * like readonly, but behaves like 'clean' on a write request.
4082 *
4083 * clean - no pending writes, but otherwise active.
4084 * When written to inactive array, starts without resync
4085 * If a write request arrives then
4086 * if metadata is known, mark 'dirty' and switch to 'active'.
4087 * if not known, block and switch to write-pending
4088 * If written to an active array that has pending writes, then fails.
4089 * active
4090 * fully active: IO and resync can be happening.
4091 * When written to inactive array, starts with resync
4092 *
4093 * write-pending
4094 * clean, but writes are blocked waiting for 'active' to be written.
4095 *
4096 * active-idle
4097 * like active, but no writes have been seen for a while (100msec).
4098 *
4099 */
4100enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4101 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004102static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004103 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4104 "write-pending", "active-idle", NULL };
4105
4106static int match_word(const char *word, char **list)
4107{
4108 int n;
4109 for (n=0; list[n]; n++)
4110 if (cmd_match(word, list[n]))
4111 break;
4112 return n;
4113}
4114
4115static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004116array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004117{
4118 enum array_state st = inactive;
4119
4120 if (mddev->pers)
4121 switch(mddev->ro) {
4122 case 1:
4123 st = readonly;
4124 break;
4125 case 2:
4126 st = read_auto;
4127 break;
4128 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004129 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004130 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004131 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004132 else if (mddev->in_sync)
4133 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004134 else if (mddev->safemode)
4135 st = active_idle;
4136 else
4137 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004138 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004139 }
4140 else {
4141 if (list_empty(&mddev->disks) &&
4142 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004143 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004144 st = clear;
4145 else
4146 st = inactive;
4147 }
4148 return sprintf(page, "%s\n", array_states[st]);
4149}
4150
NeilBrownf72ffdd2014-09-30 14:23:59 +10004151static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4152static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4153static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004154static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004155
4156static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004157array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004158{
NeilBrown6497709b2017-03-15 14:05:14 +11004159 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004160 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004161
4162 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4163 /* don't take reconfig_mutex when toggling between
4164 * clean and active
4165 */
4166 spin_lock(&mddev->lock);
4167 if (st == active) {
4168 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004169 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004170 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004171 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004172 } else /* st == clean */ {
4173 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004174 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004175 err = -EBUSY;
4176 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004177 if (!err)
4178 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004179 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004180 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004181 }
4182 err = mddev_lock(mddev);
4183 if (err)
4184 return err;
4185 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004186 switch(st) {
4187 case bad_word:
4188 break;
4189 case clear:
4190 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004191 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004192 break;
4193 case inactive:
4194 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004195 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004196 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004197 else
NeilBrowne6910632008-02-06 01:39:51 -08004198 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004199 break;
4200 case suspended:
4201 break; /* not supported yet */
4202 case readonly:
4203 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004204 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004205 else {
4206 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004207 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004208 err = do_md_run(mddev);
4209 }
4210 break;
4211 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004212 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004213 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004214 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004215 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004216 err = restart_array(mddev);
4217 if (err == 0) {
4218 mddev->ro = 2;
4219 set_disk_ro(mddev->gendisk, 0);
4220 }
NeilBrown9e653b62006-06-26 00:27:58 -07004221 } else {
4222 mddev->ro = 2;
4223 err = do_md_run(mddev);
4224 }
4225 break;
4226 case clean:
4227 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004228 err = restart_array(mddev);
4229 if (err)
4230 break;
NeilBrown85572d72014-12-15 12:56:56 +11004231 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004232 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004233 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004234 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004235 } else
4236 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004237 break;
4238 case active:
4239 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004240 err = restart_array(mddev);
4241 if (err)
4242 break;
Shaohua Li29530792016-12-08 15:48:19 -08004243 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004244 wake_up(&mddev->sb_wait);
4245 err = 0;
4246 } else {
4247 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004248 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004249 err = do_md_run(mddev);
4250 }
4251 break;
4252 case write_pending:
4253 case active_idle:
4254 /* these cannot be set */
4255 break;
4256 }
NeilBrown67918752014-12-15 12:57:01 +11004257
4258 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004259 if (mddev->hold_active == UNTIL_IOCTL)
4260 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004261 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004262 }
NeilBrown67918752014-12-15 12:57:01 +11004263 mddev_unlock(mddev);
4264 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004265}
NeilBrown80ca3a42006-07-10 04:44:18 -07004266static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004267__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004268
NeilBrown6d7ff7382006-01-06 00:21:16 -08004269static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004270max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004271 return sprintf(page, "%d\n",
4272 atomic_read(&mddev->max_corr_read_errors));
4273}
4274
4275static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004276max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004277{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004278 unsigned int n;
4279 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004280
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004281 rv = kstrtouint(buf, 10, &n);
4282 if (rv < 0)
4283 return rv;
4284 atomic_set(&mddev->max_corr_read_errors, n);
4285 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004286}
4287
4288static struct md_sysfs_entry max_corr_read_errors =
4289__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4290 max_corrected_read_errors_store);
4291
4292static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004293null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004294{
4295 return -EINVAL;
4296}
4297
4298static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004299new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004300{
4301 /* buf must be %d:%d\n? giving major and minor numbers */
4302 /* The new device is added to the array.
4303 * If the array has a persistent superblock, we read the
4304 * superblock to initialise info and check validity.
4305 * Otherwise, only checking done is that in bind_rdev_to_array,
4306 * which mainly checks size.
4307 */
4308 char *e;
4309 int major = simple_strtoul(buf, &e, 10);
4310 int minor;
4311 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004312 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004313 int err;
4314
4315 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4316 return -EINVAL;
4317 minor = simple_strtoul(e+1, &e, 10);
4318 if (*e && *e != '\n')
4319 return -EINVAL;
4320 dev = MKDEV(major, minor);
4321 if (major != MAJOR(dev) ||
4322 minor != MINOR(dev))
4323 return -EOVERFLOW;
4324
NeilBrown67918752014-12-15 12:57:01 +11004325 flush_workqueue(md_misc_wq);
4326
4327 err = mddev_lock(mddev);
4328 if (err)
4329 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004330 if (mddev->persistent) {
4331 rdev = md_import_device(dev, mddev->major_version,
4332 mddev->minor_version);
4333 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004334 struct md_rdev *rdev0
4335 = list_entry(mddev->disks.next,
4336 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004337 err = super_types[mddev->major_version]
4338 .load_super(rdev, rdev0, mddev->minor_version);
4339 if (err < 0)
4340 goto out;
4341 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004342 } else if (mddev->external)
4343 rdev = md_import_device(dev, -2, -1);
4344 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004345 rdev = md_import_device(dev, -1, -1);
4346
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004347 if (IS_ERR(rdev)) {
4348 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004349 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004350 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004351 err = bind_rdev_to_array(rdev, mddev);
4352 out:
4353 if (err)
4354 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004355 mddev_unlock(mddev);
Alexey Obitotskiy5492c462017-07-28 15:49:25 +02004356 if (!err)
4357 md_new_event(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004358 return err ? err : len;
4359}
4360
4361static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004362__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004363
4364static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004365bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004366{
4367 char *end;
4368 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004369 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004370
NeilBrown67918752014-12-15 12:57:01 +11004371 err = mddev_lock(mddev);
4372 if (err)
4373 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004374 if (!mddev->bitmap)
4375 goto out;
4376 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4377 while (*buf) {
4378 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4379 if (buf == end) break;
4380 if (*end == '-') { /* range */
4381 buf = end + 1;
4382 end_chunk = simple_strtoul(buf, &end, 0);
4383 if (buf == end) break;
4384 }
4385 if (*end && !isspace(*end)) break;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004386 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004387 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004388 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004389 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
Paul Clements9b1d1da2006-10-03 01:15:49 -07004390out:
NeilBrown67918752014-12-15 12:57:01 +11004391 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004392 return len;
4393}
4394
4395static struct md_sysfs_entry md_bitmap =
4396__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4397
4398static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004399size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004400{
Andre Noll58c0fed2009-03-31 14:33:13 +11004401 return sprintf(page, "%llu\n",
4402 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004403}
4404
NeilBrownfd01b882011-10-11 16:47:53 +11004405static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004406
4407static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004408size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004409{
4410 /* If array is inactive, we can reduce the component size, but
4411 * not increase it (except from 0).
4412 * If array is active, we can try an on-line resize
4413 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004414 sector_t sectors;
4415 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004416
Andre Noll58c0fed2009-03-31 14:33:13 +11004417 if (err < 0)
4418 return err;
NeilBrown67918752014-12-15 12:57:01 +11004419 err = mddev_lock(mddev);
4420 if (err)
4421 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004422 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004423 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004424 if (err == 0)
4425 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004426 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004427 if (mddev->dev_sectors == 0 ||
4428 mddev->dev_sectors > sectors)
4429 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004430 else
4431 err = -ENOSPC;
4432 }
NeilBrown67918752014-12-15 12:57:01 +11004433 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004434 return err ? err : len;
4435}
4436
4437static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004438__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004439
Masanari Iida83f0d772012-10-30 00:18:08 +09004440/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004441 * This is one of
4442 * 'none' for arrays with no metadata (good luck...)
4443 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004444 * or N.M for internally known formats
4445 */
4446static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004447metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004448{
4449 if (mddev->persistent)
4450 return sprintf(page, "%d.%d\n",
4451 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004452 else if (mddev->external)
4453 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004454 else
4455 return sprintf(page, "none\n");
4456}
4457
4458static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004459metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004460{
4461 int major, minor;
4462 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004463 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004464 /* Changing the details of 'external' metadata is
4465 * always permitted. Otherwise there must be
4466 * no devices attached to the array.
4467 */
NeilBrown67918752014-12-15 12:57:01 +11004468
4469 err = mddev_lock(mddev);
4470 if (err)
4471 return err;
4472 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004473 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4474 ;
4475 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004476 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004477
NeilBrown67918752014-12-15 12:57:01 +11004478 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004479 if (cmd_match(buf, "none")) {
4480 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004481 mddev->external = 0;
4482 mddev->major_version = 0;
4483 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004484 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004485 }
4486 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004487 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004488 if (namelen >= sizeof(mddev->metadata_type))
4489 namelen = sizeof(mddev->metadata_type)-1;
4490 strncpy(mddev->metadata_type, buf+9, namelen);
4491 mddev->metadata_type[namelen] = 0;
4492 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4493 mddev->metadata_type[--namelen] = 0;
4494 mddev->persistent = 0;
4495 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004496 mddev->major_version = 0;
4497 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004498 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004499 }
4500 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004501 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004502 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004503 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004504 buf = e+1;
4505 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004506 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004507 goto out_unlock;
4508 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004509 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004510 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004511 mddev->major_version = major;
4512 mddev->minor_version = minor;
4513 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004514 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004515 err = 0;
4516out_unlock:
4517 mddev_unlock(mddev);
4518 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004519}
4520
4521static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004522__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004523
NeilBrowna35b0d62006-01-06 00:20:49 -08004524static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004525action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004526{
NeilBrown7eec3142005-11-08 21:39:44 -08004527 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004528 unsigned long recovery = mddev->recovery;
4529 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004530 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004531 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4532 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4533 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004534 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004535 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4536 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004537 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004538 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004539 type = "check";
4540 else
4541 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004542 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004543 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004544 else if (mddev->reshape_position != MaxSector)
4545 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004546 }
4547 return sprintf(page, "%s\n", type);
4548}
4549
4550static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004551action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004552{
NeilBrown7eec3142005-11-08 21:39:44 -08004553 if (!mddev->pers || !mddev->pers->sync_request)
4554 return -EINVAL;
4555
NeilBrownb6a9ce62009-05-26 09:41:17 +10004556
4557 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004558 if (cmd_match(page, "frozen"))
4559 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4560 else
4561 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004562 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4563 mddev_lock(mddev) == 0) {
4564 flush_workqueue(md_misc_wq);
4565 if (mddev->sync_thread) {
4566 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004567 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004568 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004569 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004570 }
NeilBrown312045e2015-12-21 11:01:21 +11004571 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004572 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004573 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004574 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004575 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004576 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004577 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004578 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004579 int err;
4580 if (mddev->pers->start_reshape == NULL)
4581 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004582 err = mddev_lock(mddev);
4583 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004584 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4585 err = -EBUSY;
4586 else {
4587 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4588 err = mddev->pers->start_reshape(mddev);
4589 }
NeilBrown67918752014-12-15 12:57:01 +11004590 mddev_unlock(mddev);
4591 }
NeilBrown16484bf2006-03-27 01:18:13 -08004592 if (err)
4593 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004594 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004595 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004596 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004597 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004598 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004599 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004600 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004601 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4602 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004603 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004604 if (mddev->ro == 2) {
4605 /* A write to sync_action is enough to justify
4606 * canceling read-auto mode
4607 */
4608 mddev->ro = 0;
4609 md_wakeup_thread(mddev->sync_thread);
4610 }
NeilBrown03c902e2006-01-06 00:20:46 -08004611 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004612 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004613 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004614 return len;
4615}
4616
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004617static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004618__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004619
4620static ssize_t
4621last_sync_action_show(struct mddev *mddev, char *page)
4622{
4623 return sprintf(page, "%s\n", mddev->last_sync_action);
4624}
4625
4626static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4627
NeilBrown9d888832005-11-08 21:39:26 -08004628static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004629mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004630{
4631 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004632 (unsigned long long)
4633 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004634}
4635
NeilBrown80ca3a42006-07-10 04:44:18 -07004636static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004637
NeilBrown88202a02006-01-06 00:21:36 -08004638static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004639sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004640{
4641 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4642 mddev->sync_speed_min ? "local": "system");
4643}
4644
4645static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004646sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004647{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004648 unsigned int min;
4649 int rv;
4650
NeilBrown88202a02006-01-06 00:21:36 -08004651 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004652 min = 0;
4653 } else {
4654 rv = kstrtouint(buf, 10, &min);
4655 if (rv < 0)
4656 return rv;
4657 if (min == 0)
4658 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004659 }
NeilBrown88202a02006-01-06 00:21:36 -08004660 mddev->sync_speed_min = min;
4661 return len;
4662}
4663
4664static struct md_sysfs_entry md_sync_min =
4665__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4666
4667static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004668sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004669{
4670 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4671 mddev->sync_speed_max ? "local": "system");
4672}
4673
4674static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004675sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004676{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004677 unsigned int max;
4678 int rv;
4679
NeilBrown88202a02006-01-06 00:21:36 -08004680 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004681 max = 0;
4682 } else {
4683 rv = kstrtouint(buf, 10, &max);
4684 if (rv < 0)
4685 return rv;
4686 if (max == 0)
4687 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004688 }
NeilBrown88202a02006-01-06 00:21:36 -08004689 mddev->sync_speed_max = max;
4690 return len;
4691}
4692
4693static struct md_sysfs_entry md_sync_max =
4694__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4695
Iustin Popd7f3d292007-10-16 23:30:54 -07004696static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004697degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004698{
4699 return sprintf(page, "%d\n", mddev->degraded);
4700}
4701static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004702
4703static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004704sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004705{
4706 return sprintf(page, "%d\n", mddev->parallel_resync);
4707}
4708
4709static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004710sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004711{
4712 long n;
4713
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004714 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004715 return -EINVAL;
4716
4717 if (n != 0 && n != 1)
4718 return -EINVAL;
4719
4720 mddev->parallel_resync = n;
4721
4722 if (mddev->sync_thread)
4723 wake_up(&resync_wait);
4724
4725 return len;
4726}
4727
4728/* force parallel resync, even with shared block devices */
4729static struct md_sysfs_entry md_sync_force_parallel =
4730__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4731 sync_force_parallel_show, sync_force_parallel_store);
4732
4733static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004734sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004735{
4736 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004737 if (mddev->curr_resync == 0)
4738 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004739 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4740 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004741 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004742 db = resync - mddev->resync_mark_cnt;
4743 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004744}
4745
NeilBrown80ca3a42006-07-10 04:44:18 -07004746static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004747
4748static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004749sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004750{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004751 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004752
NeilBrownacb180b2009-04-14 16:28:34 +10004753 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4754 return sprintf(page, "none\n");
4755
NeilBrown72f36d52012-10-11 14:25:57 +11004756 if (mddev->curr_resync == 1 ||
4757 mddev->curr_resync == 2)
4758 return sprintf(page, "delayed\n");
4759
NeilBrownc804cde2012-05-21 09:28:33 +10004760 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4761 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004762 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004763 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004764 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004765
NeilBrownacb180b2009-04-14 16:28:34 +10004766 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004767 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004768}
4769
NeilBrown750f1992014-09-30 08:53:05 +10004770static struct md_sysfs_entry md_sync_completed =
4771 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004772
NeilBrowne464eaf2006-03-27 01:18:14 -08004773static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004774min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004775{
4776 return sprintf(page, "%llu\n",
4777 (unsigned long long)mddev->resync_min);
4778}
4779static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004780min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004781{
4782 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004783 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004784
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004785 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004786 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004787
4788 spin_lock(&mddev->lock);
4789 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004790 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004791 goto out_unlock;
4792
4793 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004794 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004795 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004796
NeilBrown50c37b12015-03-23 17:36:38 +11004797 /* Round down to multiple of 4K for safety */
4798 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004799 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004800
NeilBrown23da4222014-12-15 12:57:01 +11004801out_unlock:
4802 spin_unlock(&mddev->lock);
4803 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004804}
4805
4806static struct md_sysfs_entry md_min_sync =
4807__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4808
4809static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004810max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004811{
4812 if (mddev->resync_max == MaxSector)
4813 return sprintf(page, "max\n");
4814 else
4815 return sprintf(page, "%llu\n",
4816 (unsigned long long)mddev->resync_max);
4817}
4818static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004819max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004820{
NeilBrown23da4222014-12-15 12:57:01 +11004821 int err;
4822 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004823 if (strncmp(buf, "max", 3) == 0)
4824 mddev->resync_max = MaxSector;
4825 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004826 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004827 int chunk;
4828
4829 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004830 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004831 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004832 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004833 goto out_unlock;
4834
4835 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004836 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004837 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004838 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004839 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004840
4841 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004842 chunk = mddev->chunk_sectors;
4843 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004844 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004845
4846 err = -EINVAL;
4847 if (sector_div(temp, chunk))
4848 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004849 }
4850 mddev->resync_max = max;
4851 }
4852 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004853 err = 0;
4854out_unlock:
4855 spin_unlock(&mddev->lock);
4856 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004857}
4858
4859static struct md_sysfs_entry md_max_sync =
4860__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4861
4862static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004863suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004864{
4865 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4866}
4867
4868static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004869suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004870{
NeilBrownb03e0cc2017-10-19 12:49:15 +11004871 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004872 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004873
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004874 err = kstrtoull(buf, 10, &new);
4875 if (err < 0)
4876 return err;
4877 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004878 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004879
NeilBrown67918752014-12-15 12:57:01 +11004880 err = mddev_lock(mddev);
4881 if (err)
4882 return err;
4883 err = -EINVAL;
4884 if (mddev->pers == NULL ||
4885 mddev->pers->quiesce == NULL)
4886 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004887 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11004888 mddev->suspend_lo = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004889 mddev_resume(mddev);
4890
NeilBrown67918752014-12-15 12:57:01 +11004891 err = 0;
4892unlock:
4893 mddev_unlock(mddev);
4894 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004895}
4896static struct md_sysfs_entry md_suspend_lo =
4897__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4898
NeilBrowne464eaf2006-03-27 01:18:14 -08004899static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004900suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004901{
4902 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4903}
4904
4905static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004906suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004907{
NeilBrownb03e0cc2017-10-19 12:49:15 +11004908 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004909 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004910
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004911 err = kstrtoull(buf, 10, &new);
4912 if (err < 0)
4913 return err;
4914 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004915 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004916
NeilBrown67918752014-12-15 12:57:01 +11004917 err = mddev_lock(mddev);
4918 if (err)
4919 return err;
4920 err = -EINVAL;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004921 if (mddev->pers == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004922 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004923
4924 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11004925 mddev->suspend_hi = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004926 mddev_resume(mddev);
4927
NeilBrown67918752014-12-15 12:57:01 +11004928 err = 0;
4929unlock:
4930 mddev_unlock(mddev);
4931 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004932}
4933static struct md_sysfs_entry md_suspend_hi =
4934__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4935
NeilBrown08a02ec2007-05-09 02:35:38 -07004936static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004937reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004938{
4939 if (mddev->reshape_position != MaxSector)
4940 return sprintf(page, "%llu\n",
4941 (unsigned long long)mddev->reshape_position);
4942 strcpy(page, "none\n");
4943 return 5;
4944}
4945
4946static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004947reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004948{
NeilBrownc6563a82012-05-21 09:27:00 +10004949 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004950 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004951 int err;
NeilBrown67918752014-12-15 12:57:01 +11004952
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004953 err = kstrtoull(buf, 10, &new);
4954 if (err < 0)
4955 return err;
4956 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004957 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004958 err = mddev_lock(mddev);
4959 if (err)
4960 return err;
4961 err = -EBUSY;
4962 if (mddev->pers)
4963 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004964 mddev->reshape_position = new;
4965 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004966 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004967 mddev->new_level = mddev->level;
4968 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004969 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004970 rdev_for_each(rdev, mddev)
4971 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004972 err = 0;
4973unlock:
4974 mddev_unlock(mddev);
4975 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004976}
4977
4978static struct md_sysfs_entry md_reshape_position =
4979__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4980 reshape_position_store);
4981
Dan Williamsb522adc2009-03-31 15:00:31 +11004982static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004983reshape_direction_show(struct mddev *mddev, char *page)
4984{
4985 return sprintf(page, "%s\n",
4986 mddev->reshape_backwards ? "backwards" : "forwards");
4987}
4988
4989static ssize_t
4990reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4991{
4992 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004993 int err;
4994
NeilBrown2c810cd2012-05-21 09:27:00 +10004995 if (cmd_match(buf, "forwards"))
4996 backwards = 0;
4997 else if (cmd_match(buf, "backwards"))
4998 backwards = 1;
4999 else
5000 return -EINVAL;
5001 if (mddev->reshape_backwards == backwards)
5002 return len;
5003
NeilBrown67918752014-12-15 12:57:01 +11005004 err = mddev_lock(mddev);
5005 if (err)
5006 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10005007 /* check if we are allowed to change */
5008 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11005009 err = -EBUSY;
5010 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10005011 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11005012 err = -EINVAL;
5013 else
5014 mddev->reshape_backwards = backwards;
5015 mddev_unlock(mddev);
5016 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10005017}
5018
5019static struct md_sysfs_entry md_reshape_direction =
5020__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5021 reshape_direction_store);
5022
5023static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005024array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11005025{
5026 if (mddev->external_size)
5027 return sprintf(page, "%llu\n",
5028 (unsigned long long)mddev->array_sectors/2);
5029 else
5030 return sprintf(page, "default\n");
5031}
5032
5033static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005034array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11005035{
5036 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11005037 int err;
5038
5039 err = mddev_lock(mddev);
5040 if (err)
5041 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11005042
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005043 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08005044 if (mddev_is_clustered(mddev)) {
5045 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005046 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08005047 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005048
Dan Williamsb522adc2009-03-31 15:00:31 +11005049 if (strncmp(buf, "default", 7) == 0) {
5050 if (mddev->pers)
5051 sectors = mddev->pers->size(mddev, 0, 0);
5052 else
5053 sectors = mddev->array_sectors;
5054
5055 mddev->external_size = 0;
5056 } else {
5057 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11005058 err = -EINVAL;
5059 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5060 err = -E2BIG;
5061 else
5062 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11005063 }
5064
NeilBrown67918752014-12-15 12:57:01 +11005065 if (!err) {
5066 mddev->array_sectors = sectors;
5067 if (mddev->pers) {
5068 set_capacity(mddev->gendisk, mddev->array_sectors);
5069 revalidate_disk(mddev->gendisk);
5070 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11005071 }
NeilBrown67918752014-12-15 12:57:01 +11005072 mddev_unlock(mddev);
5073 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11005074}
5075
5076static struct md_sysfs_entry md_array_size =
5077__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5078 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005079
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005080static ssize_t
5081consistency_policy_show(struct mddev *mddev, char *page)
5082{
5083 int ret;
5084
5085 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5086 ret = sprintf(page, "journal\n");
5087 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5088 ret = sprintf(page, "ppl\n");
5089 } else if (mddev->bitmap) {
5090 ret = sprintf(page, "bitmap\n");
5091 } else if (mddev->pers) {
5092 if (mddev->pers->sync_request)
5093 ret = sprintf(page, "resync\n");
5094 else
5095 ret = sprintf(page, "none\n");
5096 } else {
5097 ret = sprintf(page, "unknown\n");
5098 }
5099
5100 return ret;
5101}
5102
5103static ssize_t
5104consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5105{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005106 int err = 0;
5107
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005108 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005109 if (mddev->pers->change_consistency_policy)
5110 err = mddev->pers->change_consistency_policy(mddev, buf);
5111 else
5112 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005113 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5114 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005115 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005116 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005117 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005118
5119 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005120}
5121
5122static struct md_sysfs_entry md_consistency_policy =
5123__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5124 consistency_policy_store);
5125
NeilBrowneae17012005-11-08 21:39:23 -08005126static struct attribute *md_default_attrs[] = {
5127 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005128 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005129 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005130 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005131 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005132 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005133 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005134 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005135 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005136 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005137 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005138 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005139 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005140 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005141 &md_consistency_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005142 NULL,
5143};
5144
5145static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005146 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005147 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005148 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005149 &md_sync_min.attr,
5150 &md_sync_max.attr,
5151 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005152 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005153 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005154 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005155 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005156 &md_suspend_lo.attr,
5157 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005158 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005159 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005160 NULL,
5161};
NeilBrown411036f2005-11-08 21:39:40 -08005162static struct attribute_group md_redundancy_group = {
5163 .name = NULL,
5164 .attrs = md_redundancy_attrs,
5165};
5166
NeilBrowneae17012005-11-08 21:39:23 -08005167static ssize_t
5168md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5169{
5170 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005171 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005172 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005173
5174 if (!entry->show)
5175 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005176 spin_lock(&all_mddevs_lock);
5177 if (list_empty(&mddev->all_mddevs)) {
5178 spin_unlock(&all_mddevs_lock);
5179 return -EBUSY;
5180 }
5181 mddev_get(mddev);
5182 spin_unlock(&all_mddevs_lock);
5183
NeilBrownb7b17c92014-12-15 12:56:59 +11005184 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005185 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005186 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005187}
5188
5189static ssize_t
5190md_attr_store(struct kobject *kobj, struct attribute *attr,
5191 const char *page, size_t length)
5192{
5193 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005194 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005195 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005196
5197 if (!entry->store)
5198 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005199 if (!capable(CAP_SYS_ADMIN))
5200 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005201 spin_lock(&all_mddevs_lock);
5202 if (list_empty(&mddev->all_mddevs)) {
5203 spin_unlock(&all_mddevs_lock);
5204 return -EBUSY;
5205 }
5206 mddev_get(mddev);
5207 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005208 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005209 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005210 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005211}
5212
5213static void md_free(struct kobject *ko)
5214{
NeilBrownfd01b882011-10-11 16:47:53 +11005215 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005216
5217 if (mddev->sysfs_state)
5218 sysfs_put(mddev->sysfs_state);
5219
Bart Van Assched8115c352018-02-28 10:15:29 -08005220 if (mddev->gendisk)
5221 del_gendisk(mddev->gendisk);
NeilBrown6cd18e72015-04-27 14:12:22 +10005222 if (mddev->queue)
5223 blk_cleanup_queue(mddev->queue);
Bart Van Assched8115c352018-02-28 10:15:29 -08005224 if (mddev->gendisk)
NeilBrowna21d1502009-01-09 08:31:09 +11005225 put_disk(mddev->gendisk);
NeilBrown4ad23a972017-03-15 14:05:14 +11005226 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005227
Kent Overstreet28dec872018-06-07 20:52:54 -04005228 bioset_exit(&mddev->bio_set);
5229 bioset_exit(&mddev->sync_set);
NeilBrowneae17012005-11-08 21:39:23 -08005230 kfree(mddev);
5231}
5232
Emese Revfy52cf25d2010-01-19 02:58:23 +01005233static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005234 .show = md_attr_show,
5235 .store = md_attr_store,
5236};
5237static struct kobj_type md_ktype = {
5238 .release = md_free,
5239 .sysfs_ops = &md_sysfs_ops,
5240 .default_attrs = md_default_attrs,
5241};
5242
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243int mdp_major = 0;
5244
Dan Williams5fd3a172009-03-04 00:57:25 -07005245static void mddev_delayed_delete(struct work_struct *ws)
5246{
NeilBrownfd01b882011-10-11 16:47:53 +11005247 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005248
NeilBrown43a70502009-12-14 12:49:55 +11005249 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005250 kobject_del(&mddev->kobj);
5251 kobject_put(&mddev->kobj);
5252}
5253
NeilBrown4ad23a972017-03-15 14:05:14 +11005254static void no_op(struct percpu_ref *r) {}
5255
NeilBrowna415c0f2017-06-05 16:05:13 +10005256int mddev_init_writes_pending(struct mddev *mddev)
5257{
5258 if (mddev->writes_pending.percpu_count_ptr)
5259 return 0;
5260 if (percpu_ref_init(&mddev->writes_pending, no_op, 0, GFP_KERNEL) < 0)
5261 return -ENOMEM;
5262 /* We want to start with the refcount at zero */
5263 percpu_ref_put(&mddev->writes_pending);
5264 return 0;
5265}
5266EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5267
NeilBrownefeb53c2009-01-09 08:31:10 +11005268static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269{
NeilBrown039b7222017-04-12 16:26:13 +10005270 /*
5271 * If dev is zero, name is the name of a device to allocate with
5272 * an arbitrary minor number. It will be "md_???"
5273 * If dev is non-zero it must be a device number with a MAJOR of
5274 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5275 * the device is being created by opening a node in /dev.
5276 * If "name" is not NULL, the device is being created by
5277 * writing to /sys/module/md_mod/parameters/new_array.
5278 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005279 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005280 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005282 int partitioned;
5283 int shift;
5284 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005285 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286
5287 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005288 return -ENODEV;
5289
5290 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5291 shift = partitioned ? MdpMinorShift : 0;
5292 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293
Tejun Heoe804ac72010-10-15 15:36:08 +02005294 /* wait for any previous instance of this device to be
5295 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005296 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005297 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005298
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005299 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005300 error = -EEXIST;
5301 if (mddev->gendisk)
5302 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005303
NeilBrown039b7222017-04-12 16:26:13 +10005304 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005305 /* Need to ensure that 'name' is not a duplicate.
5306 */
NeilBrownfd01b882011-10-11 16:47:53 +11005307 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005308 spin_lock(&all_mddevs_lock);
5309
5310 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5311 if (mddev2->gendisk &&
5312 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5313 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005314 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005315 }
5316 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 }
NeilBrown039b7222017-04-12 16:26:13 +10005318 if (name && dev)
5319 /*
5320 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5321 */
5322 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005323
NeilBrown0909dc42009-07-01 12:27:21 +10005324 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005325 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005326 if (!mddev->queue)
5327 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005328 mddev->queue->queuedata = mddev;
5329
NeilBrown409c57f2009-03-31 14:39:39 +11005330 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005331 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005332
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 disk = alloc_disk(1 << shift);
5334 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005335 blk_cleanup_queue(mddev->queue);
5336 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005337 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005339 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005341 if (name)
5342 strcpy(disk->disk_name, name);
5343 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005345 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 disk->fops = &md_fops;
5348 disk->private_data = mddev;
5349 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005350 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005351 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005352 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005353 * remove it now.
5354 */
5355 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005357 /* As soon as we call add_disk(), another thread could get
5358 * through to md_open, so make sure it doesn't get too far
5359 */
5360 mutex_lock(&mddev->open_mutex);
5361 add_disk(disk);
5362
Kent Overstreet28dec872018-06-07 20:52:54 -04005363 error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005364 if (error) {
5365 /* This isn't possible, but as kobject_init_and_add is marked
5366 * __must_check, we must do something with the result
5367 */
NeilBrown9d487392016-11-02 14:16:49 +11005368 pr_debug("md: cannot register %s/md - name in use\n",
5369 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005370 error = 0;
5371 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005372 if (mddev->kobj.sd &&
5373 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005374 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005375 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005376 abort:
5377 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005378 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005379 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005380 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005381 }
NeilBrownd3374822009-01-09 08:31:10 +11005382 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005383 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005384}
5385
5386static struct kobject *md_probe(dev_t dev, int *part, void *data)
5387{
NeilBrown78b63502017-04-12 16:26:13 +10005388 if (create_on_open)
5389 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 return NULL;
5391}
5392
Kees Cooke4dca7b2017-10-17 19:04:42 -07005393static int add_named_array(const char *val, const struct kernel_param *kp)
NeilBrownefeb53c2009-01-09 08:31:10 +11005394{
NeilBrown039b7222017-04-12 16:26:13 +10005395 /*
5396 * val must be "md_*" or "mdNNN".
5397 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005398 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005399 * For "mdNNN" we allocate an array with the minor number NNN
5400 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005401 */
5402 int len = strlen(val);
5403 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005404 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005405
5406 while (len && val[len-1] == '\n')
5407 len--;
5408 if (len >= DISK_NAME_LEN)
5409 return -E2BIG;
5410 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005411 if (strncmp(buf, "md_", 3) == 0)
5412 return md_alloc(0, buf);
5413 if (strncmp(buf, "md", 2) == 0 &&
5414 isdigit(buf[2]) &&
5415 kstrtoul(buf+2, 10, &devnum) == 0 &&
5416 devnum <= MINORMASK)
5417 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5418
5419 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005420}
5421
Kees Cook8376d3c2017-10-16 17:01:48 -07005422static void md_safemode_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423{
Kees Cook8376d3c2017-10-16 17:01:48 -07005424 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
NeilBrown4ad23a972017-03-15 14:05:14 +11005426 mddev->safemode = 1;
5427 if (mddev->external)
5428 sysfs_notify_dirent_safe(mddev->sysfs_state);
5429
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 md_wakeup_thread(mddev->thread);
5431}
5432
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005433static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434
NeilBrownfd01b882011-10-11 16:47:53 +11005435int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436{
NeilBrown2604b702006-01-06 00:20:36 -08005437 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005438 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005439 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
NeilBrowna757e642005-04-16 15:26:42 -07005441 if (list_empty(&mddev->disks))
5442 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
5445 if (mddev->pers)
5446 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005447 /* Cannot run until previous stop completes properly */
5448 if (mddev->sysfs_active)
5449 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005450
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 /*
5452 * Analyze all RAID superblock(s)
5453 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005454 if (!mddev->raid_disks) {
5455 if (!mddev->persistent)
5456 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005457 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
NeilBrownd9d166c2006-01-06 00:20:51 -08005460 if (mddev->level != LEVEL_NONE)
5461 request_module("md-level-%d", mddev->level);
5462 else if (mddev->clevel[0])
5463 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464
5465 /*
5466 * Drop all container device buffers, from now on
5467 * the only valid external interface is through the md
5468 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 */
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005470 mddev->has_superblocks = false;
NeilBrowndafb20f2012-03-19 12:46:39 +11005471 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005472 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 continue;
5474 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005475 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005476 if (mddev->ro != 1 &&
5477 (bdev_read_only(rdev->bdev) ||
5478 bdev_read_only(rdev->meta_bdev))) {
5479 mddev->ro = 1;
5480 if (mddev->gendisk)
5481 set_disk_ro(mddev->gendisk, 1);
5482 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005483
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005484 if (rdev->sb_page)
5485 mddev->has_superblocks = true;
5486
NeilBrownf0d76d72007-07-17 04:06:12 -07005487 /* perform some consistency tests on the device.
5488 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005489 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005490 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005491 if (rdev->meta_bdev) {
5492 /* Nothing to check */;
5493 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005494 if (mddev->dev_sectors &&
5495 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005496 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005497 pr_warn("md: %s: data overlaps metadata\n",
5498 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005499 return -EINVAL;
5500 }
5501 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005502 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005503 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005504 pr_warn("md: %s: metadata overlaps data\n",
5505 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005506 return -EINVAL;
5507 }
5508 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005509 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 }
5511
Kent Overstreetafeee512018-05-20 18:25:52 -04005512 if (!bioset_initialized(&mddev->bio_set)) {
5513 err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5514 if (err)
5515 return err;
Ming Lei10273172017-02-14 23:29:00 +08005516 }
Kent Overstreetafeee512018-05-20 18:25:52 -04005517 if (!bioset_initialized(&mddev->sync_set)) {
5518 err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5519 if (err)
Kent Overstreet28dec872018-06-07 20:52:54 -04005520 return err;
NeilBrown5a850712017-06-21 09:12:21 +10005521 }
Xiao Ni5a409b42018-05-21 11:49:54 +08005522 if (mddev->flush_pool == NULL) {
5523 mddev->flush_pool = mempool_create(NR_FLUSH_INFOS, flush_info_alloc,
5524 flush_info_free, mddev);
5525 if (!mddev->flush_pool) {
5526 err = -ENOMEM;
5527 goto abort;
5528 }
5529 }
5530 if (mddev->flush_bio_pool == NULL) {
5531 mddev->flush_bio_pool = mempool_create(NR_FLUSH_BIOS, flush_bio_alloc,
5532 flush_bio_free, mddev);
5533 if (!mddev->flush_bio_pool) {
5534 err = -ENOMEM;
5535 goto abort;
5536 }
5537 }
NeilBrowna167f662010-10-26 18:31:13 +11005538
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005540 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005541 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005543 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005544 pr_warn("md: personality for level %d is not loaded!\n",
5545 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005546 else
NeilBrown9d487392016-11-02 14:16:49 +11005547 pr_warn("md: personality for level %s is not loaded!\n",
5548 mddev->clevel);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005549 err = -EINVAL;
5550 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005553 if (mddev->level != pers->level) {
5554 mddev->level = pers->level;
5555 mddev->new_level = pers->level;
5556 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005557 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558
NeilBrownf6705572006-03-27 01:18:11 -08005559 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005560 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005561 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005562 module_put(pers->owner);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005563 err = -EINVAL;
5564 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08005565 }
5566
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005567 if (pers->sync_request) {
5568 /* Warn if this is a potentially silly
5569 * configuration.
5570 */
5571 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005572 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005573 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005574
NeilBrowndafb20f2012-03-19 12:46:39 +11005575 rdev_for_each(rdev, mddev)
5576 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005577 if (rdev < rdev2 &&
5578 rdev->bdev->bd_contains ==
5579 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005580 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5581 mdname(mddev),
5582 bdevname(rdev->bdev,b),
5583 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005584 warned = 1;
5585 }
5586 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005587
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005588 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005589 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005590 }
5591
NeilBrown657390d2005-08-26 18:34:16 -07005592 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005593 /* may be over-ridden by personality */
5594 mddev->resync_max_sectors = mddev->dev_sectors;
5595
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005596 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
NeilBrown0f9552b52009-12-30 12:08:50 +11005598 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005599 mddev->ro = 2; /* read-only, but switch on first write */
5600
NeilBrown36d091f2014-12-15 12:56:58 +11005601 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005602 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005603 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005604 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005605 WARN_ONCE(!mddev->external_size,
5606 "%s: default size too small, but 'external_size' not in effect?\n",
5607 __func__);
5608 pr_warn("md: invalid array_size %llu > default size %llu\n",
5609 (unsigned long long)mddev->array_sectors / 2,
5610 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005611 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005612 }
NeilBrown36d091f2014-12-15 12:56:58 +11005613 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005614 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005615 struct bitmap *bitmap;
5616
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005617 bitmap = md_bitmap_create(mddev, -1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005618 if (IS_ERR(bitmap)) {
5619 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005620 pr_warn("%s: failed to create bitmap (%d)\n",
5621 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005622 } else
5623 mddev->bitmap = bitmap;
5624
NeilBrownb15c2e52006-01-06 00:20:16 -08005625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005627 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005628 if (mddev->private)
5629 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005630 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005631 module_put(pers->owner);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005632 md_bitmap_destroy(mddev);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005633 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 }
NeilBrown5c675f82014-12-15 12:56:56 +11005635 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005636 bool nonrot = true;
5637
5638 rdev_for_each(rdev, mddev) {
5639 if (rdev->raid_disk >= 0 &&
5640 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5641 nonrot = false;
5642 break;
5643 }
5644 }
5645 if (mddev->degraded)
5646 nonrot = false;
5647 if (nonrot)
Bart Van Assche8b904b52018-03-07 17:10:10 -08005648 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
Shaohua Libb086a82016-09-30 09:45:40 -07005649 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08005650 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005651 mddev->queue->backing_dev_info->congested_data = mddev;
5652 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005653 }
NeilBrown36d091f2014-12-15 12:56:58 +11005654 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005655 if (mddev->kobj.sd &&
5656 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005657 pr_warn("md: cannot register extra attributes for %s\n",
5658 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005659 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005660 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005661 mddev->ro = 0;
5662
Robert Becker1e509152009-12-14 12:49:58 +11005663 atomic_set(&mddev->max_corr_read_errors,
5664 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005666 if (mddev_is_clustered(mddev))
5667 mddev->safemode_delay = 0;
5668 else
5669 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005671 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005672 spin_lock(&mddev->lock);
5673 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005674 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005675 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005676 if (rdev->raid_disk >= 0)
5677 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005678 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005679
NeilBrowna4a3d262015-07-17 11:57:30 +10005680 if (mddev->degraded && !mddev->ro)
5681 /* This ensures that recovering status is reported immediately
5682 * via sysfs - until a lack of spares is confirmed.
5683 */
5684 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005686
Shaohua Li29530792016-12-08 15:48:19 -08005687 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07005688 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689
NeilBrownd7603b72006-01-06 00:20:30 -08005690 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005691 sysfs_notify_dirent_safe(mddev->sysfs_state);
5692 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005693 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 return 0;
Xiao Nib1261942018-01-24 12:17:38 +08005695
5696abort:
Xiao Ni5a409b42018-05-21 11:49:54 +08005697 if (mddev->flush_bio_pool) {
5698 mempool_destroy(mddev->flush_bio_pool);
5699 mddev->flush_bio_pool = NULL;
5700 }
5701 if (mddev->flush_pool){
5702 mempool_destroy(mddev->flush_pool);
5703 mddev->flush_pool = NULL;
5704 }
Xiao Nib1261942018-01-24 12:17:38 +08005705
5706 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707}
NeilBrown390ee602010-06-01 19:37:27 +10005708EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
NeilBrownfd01b882011-10-11 16:47:53 +11005710static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005711{
5712 int err;
5713
5714 err = md_run(mddev);
5715 if (err)
5716 goto out;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005717 err = md_bitmap_load(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005718 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005719 md_bitmap_destroy(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005720 goto out;
5721 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005722
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005723 if (mddev_is_clustered(mddev))
5724 md_allow_write(mddev);
5725
Song Liud5d885f2017-11-19 22:17:01 -08005726 /* run start up tasks that require md_thread */
5727 md_start(mddev);
5728
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005729 md_wakeup_thread(mddev->thread);
5730 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5731
NeilBrownfe60b012010-03-29 11:10:42 +11005732 set_capacity(mddev->gendisk, mddev->array_sectors);
5733 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005734 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005735 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5736out:
5737 return err;
5738}
5739
Song Liud5d885f2017-11-19 22:17:01 -08005740int md_start(struct mddev *mddev)
5741{
5742 int ret = 0;
5743
5744 if (mddev->pers->start) {
5745 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
5746 md_wakeup_thread(mddev->thread);
5747 ret = mddev->pers->start(mddev);
5748 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
5749 md_wakeup_thread(mddev->sync_thread);
5750 }
5751 return ret;
5752}
5753EXPORT_SYMBOL_GPL(md_start);
5754
NeilBrownfd01b882011-10-11 16:47:53 +11005755static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756{
5757 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10005758 struct md_rdev *rdev;
5759 bool has_journal = false;
5760 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761
Andre Noll80fab1d2008-07-11 22:02:21 +10005762 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005764 return -ENXIO;
5765 if (!mddev->pers)
5766 return -EINVAL;
5767 if (!mddev->ro)
5768 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005769
NeilBrown97b20ef2017-04-13 08:53:48 +10005770 rcu_read_lock();
5771 rdev_for_each_rcu(rdev, mddev) {
5772 if (test_bit(Journal, &rdev->flags) &&
5773 !test_bit(Faulty, &rdev->flags))
5774 has_journal = true;
5775 if (bdev_read_only(rdev->bdev))
5776 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07005777 }
NeilBrown97b20ef2017-04-13 08:53:48 +10005778 rcu_read_unlock();
5779 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
5780 /* Don't restart rw with journal missing/faulty */
5781 return -EINVAL;
5782 if (has_readonly)
5783 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07005784
Andre Noll80fab1d2008-07-11 22:02:21 +10005785 mddev->safemode = 0;
5786 mddev->ro = 0;
5787 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11005788 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10005789 /* Kick recovery or resync if necessary */
5790 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5791 md_wakeup_thread(mddev->thread);
5792 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005793 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005794 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795}
5796
NeilBrownfd01b882011-10-11 16:47:53 +11005797static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005798{
5799 mddev->array_sectors = 0;
5800 mddev->external_size = 0;
5801 mddev->dev_sectors = 0;
5802 mddev->raid_disks = 0;
5803 mddev->recovery_cp = 0;
5804 mddev->resync_min = 0;
5805 mddev->resync_max = MaxSector;
5806 mddev->reshape_position = MaxSector;
5807 mddev->external = 0;
5808 mddev->persistent = 0;
5809 mddev->level = LEVEL_NONE;
5810 mddev->clevel[0] = 0;
5811 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08005812 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005813 mddev->ro = 0;
5814 mddev->metadata_type[0] = 0;
5815 mddev->chunk_sectors = 0;
5816 mddev->ctime = mddev->utime = 0;
5817 mddev->layout = 0;
5818 mddev->max_disks = 0;
5819 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005820 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005821 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005822 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005823 mddev->new_level = LEVEL_NONE;
5824 mddev->new_layout = 0;
5825 mddev->new_chunk_sectors = 0;
5826 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005827 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005828 mddev->suspend_lo = mddev->suspend_hi = 0;
5829 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5830 mddev->recovery = 0;
5831 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005832 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005833 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005834 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005835 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005836 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005837 mddev->bitmap_info.offset = 0;
5838 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005839 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005840 mddev->bitmap_info.chunksize = 0;
5841 mddev->bitmap_info.daemon_sleep = 0;
5842 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005843 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005844}
5845
NeilBrownfd01b882011-10-11 16:47:53 +11005846static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005847{
NeilBrown6b6204e2013-05-09 09:48:30 +10005848 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005849 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005850 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005851 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005852 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005853 }
5854
5855 del_timer_sync(&mddev->safemode_timer);
5856
Shaohua Li034e33f2016-11-21 10:29:19 -08005857 if (mddev->pers && mddev->pers->quiesce) {
5858 mddev->pers->quiesce(mddev, 1);
5859 mddev->pers->quiesce(mddev, 0);
5860 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005861 md_bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005862
NeilBrownb6d428c2013-04-24 11:42:42 +10005863 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005864 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08005865 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11005866 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005867 if (!mddev_is_clustered(mddev))
5868 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005869 md_update_sb(mddev, 1);
5870 }
5871}
NeilBrowndefad612011-01-14 09:14:33 +11005872
NeilBrownfd01b882011-10-11 16:47:53 +11005873void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005874{
NeilBrown29f097c2013-11-14 17:54:51 +11005875 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005876 __md_stop_writes(mddev);
5877 mddev_unlock(mddev);
5878}
NeilBrown390ee602010-06-01 19:37:27 +10005879EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005880
NeilBrown5aa61f42014-12-15 12:56:57 +11005881static void mddev_detach(struct mddev *mddev)
5882{
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005883 md_bitmap_wait_behind_writes(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005884 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005885 mddev->pers->quiesce(mddev, 1);
5886 mddev->pers->quiesce(mddev, 0);
5887 }
5888 md_unregister_thread(&mddev->thread);
5889 if (mddev->queue)
5890 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5891}
5892
NeilBrown5eff3c42012-11-19 10:47:48 +11005893static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005894{
NeilBrown36d091f2014-12-15 12:56:58 +11005895 struct md_personality *pers = mddev->pers;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005896 md_bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11005897 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005898 /* Ensure ->event_work is done */
5899 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005900 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005901 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005902 spin_unlock(&mddev->lock);
5903 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005904 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005905 if (pers->sync_request && mddev->to_remove == NULL)
5906 mddev->to_remove = &md_redundancy_group;
5907 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005908 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Xiao Ni5a409b42018-05-21 11:49:54 +08005909 if (mddev->flush_bio_pool) {
5910 mempool_destroy(mddev->flush_bio_pool);
5911 mddev->flush_bio_pool = NULL;
5912 }
5913 if (mddev->flush_pool) {
5914 mempool_destroy(mddev->flush_pool);
5915 mddev->flush_pool = NULL;
5916 }
Jack Wang6aaa58c2018-10-19 16:21:31 +02005917}
5918
5919void md_stop(struct mddev *mddev)
5920{
5921 /* stop the array and free an attached data structures.
5922 * This is called from dm-raid
5923 */
5924 __md_stop(mddev);
Kent Overstreetafeee512018-05-20 18:25:52 -04005925 bioset_exit(&mddev->bio_set);
5926 bioset_exit(&mddev->sync_set);
NeilBrown5eff3c42012-11-19 10:47:48 +11005927}
5928
NeilBrown390ee602010-06-01 19:37:27 +10005929EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005930
NeilBrowna05b7ea2012-07-19 15:59:18 +10005931static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005932{
5933 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005934 int did_freeze = 0;
5935
5936 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5937 did_freeze = 1;
5938 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5939 md_wakeup_thread(mddev->thread);
5940 }
NeilBrownf851b602014-12-11 10:02:10 +11005941 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005942 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005943 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005944 /* Thread might be blocked waiting for metadata update
5945 * which will now never happen */
5946 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005947
Shaohua Li29530792016-12-08 15:48:19 -08005948 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10005949 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005950 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005951 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5952 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005953 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005954 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005955 mddev_lock_nointr(mddev);
5956
NeilBrowna4bd82d2010-03-29 13:23:10 +11005957 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005958 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005959 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005960 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005961 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005962 if (did_freeze) {
5963 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005964 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005965 md_wakeup_thread(mddev->thread);
5966 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005967 err = -EBUSY;
5968 goto out;
5969 }
5970 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005971 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005972
5973 err = -ENXIO;
5974 if (mddev->ro==1)
5975 goto out;
5976 mddev->ro = 1;
5977 set_disk_ro(mddev->gendisk, 1);
5978 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005979 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5980 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005981 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005982 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005983 }
5984out:
5985 mutex_unlock(&mddev->open_mutex);
5986 return err;
5987}
5988
NeilBrown9e653b62006-06-26 00:27:58 -07005989/* mode:
5990 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005991 * 2 - stop but do not disassemble array
5992 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005993static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005994 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005997 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005998 int did_freeze = 0;
5999
6000 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6001 did_freeze = 1;
6002 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6003 md_wakeup_thread(mddev->thread);
6004 }
NeilBrownf851b602014-12-11 10:02:10 +11006005 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006006 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006007 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006008 /* Thread might be blocked waiting for metadata update
6009 * which will now never happen */
6010 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006011
NeilBrown30b8feb2013-11-14 15:16:17 +11006012 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006013 wait_event(resync_wait, (mddev->sync_thread == NULL &&
6014 !test_bit(MD_RECOVERY_RUNNING,
6015 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11006016 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017
NeilBrownc8c00a62009-08-10 12:50:52 +10006018 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006019 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006020 mddev->sysfs_active ||
6021 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006022 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006023 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10006024 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11006025 if (did_freeze) {
6026 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006027 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006028 md_wakeup_thread(mddev->thread);
6029 }
NeilBrown260fa032013-08-27 16:44:13 +10006030 return -EBUSY;
6031 }
NeilBrown6e17b022010-08-07 21:41:19 +10006032 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11006033 if (mddev->ro)
6034 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11006035
NeilBrowndefad612011-01-14 09:14:33 +11006036 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11006037 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01006038 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006039
NeilBrowna4bd82d2010-03-29 13:23:10 +11006040 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10006041 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08006042
NeilBrowndafb20f2012-03-19 12:46:39 +11006043 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10006044 if (rdev->raid_disk >= 0)
6045 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10006046
NeilBrowna4bd82d2010-03-29 13:23:10 +11006047 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10006048 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006049 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006050 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08006051
NeilBrowna4bd82d2010-03-29 13:23:10 +11006052 if (mddev->ro)
6053 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10006054 } else
6055 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 /*
6057 * Free resources if final stop
6058 */
NeilBrown9e653b62006-06-26 00:27:58 -07006059 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006060 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
NeilBrownc3d97142009-12-14 12:49:52 +11006062 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11006063 struct file *f = mddev->bitmap_info.file;
6064 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11006065 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11006066 spin_unlock(&mddev->lock);
6067 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08006068 }
NeilBrownc3d97142009-12-14 12:49:52 +11006069 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08006070
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 export_array(mddev);
6072
NeilBrown6177b472010-03-29 11:37:13 +11006073 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11006074 if (mddev->hold_active == UNTIL_STOP)
6075 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006076 }
NeilBrownd7603b72006-01-06 00:20:30 -08006077 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006078 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10006079 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080}
6081
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006082#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11006083static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084{
NeilBrown3cb03002011-10-11 16:45:26 +11006085 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 int err;
6087
NeilBrowna757e642005-04-16 15:26:42 -07006088 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090
NeilBrown9d487392016-11-02 14:16:49 +11006091 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092
NeilBrowndafb20f2012-03-19 12:46:39 +11006093 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11006095 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 }
NeilBrown9d487392016-11-02 14:16:49 +11006097 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098
NeilBrownd710e132008-10-13 11:55:12 +11006099 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006101 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10006102 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 }
6104}
6105
6106/*
6107 * lets try to run arrays based on all disks that have arrived
6108 * until now. (those are in pending_raid_disks)
6109 *
6110 * the method: pick the first pending disk, collect all disks with
6111 * the same UUID, remove all from the pending list and put them into
6112 * the 'same_array' list. Then order this list based on superblock
6113 * update time (freshest comes first), kick out 'old' disks and
6114 * compare superblocks. If everything's fine then run it.
6115 *
6116 * If "unit" is allocated, then bump its reference count
6117 */
6118static void autorun_devices(int part)
6119{
NeilBrown3cb03002011-10-11 16:45:26 +11006120 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006121 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 char b[BDEVNAME_SIZE];
6123
NeilBrown9d487392016-11-02 14:16:49 +11006124 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07006126 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08006128 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11006130 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
NeilBrown9d487392016-11-02 14:16:49 +11006132 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006134 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006136 pr_debug("md: adding %s ...\n",
6137 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 list_move(&rdev->same_set, &candidates);
6139 }
6140 /*
6141 * now we have a set of devices, with all of them having
6142 * mostly sane superblocks. It's time to allocate the
6143 * mddev.
6144 */
NeilBrowne8703fe2006-10-03 01:15:59 -07006145 if (part) {
6146 dev = MKDEV(mdp_major,
6147 rdev0->preferred_minor << MdpMinorShift);
6148 unit = MINOR(dev) >> MdpMinorShift;
6149 } else {
6150 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6151 unit = MINOR(dev);
6152 }
6153 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006154 pr_warn("md: unit number in %s is bad: %d\n",
6155 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 break;
6157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158
6159 md_probe(dev, NULL, NULL);
6160 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006161 if (!mddev || !mddev->gendisk) {
6162 if (mddev)
6163 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 break;
6165 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006166 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006167 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 else if (mddev->raid_disks || mddev->major_version
6169 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006170 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171 mdname(mddev), bdevname(rdev0->bdev,b));
6172 mddev_unlock(mddev);
6173 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006174 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006175 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006176 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 list_del_init(&rdev->same_set);
6178 if (bind_rdev_to_array(rdev, mddev))
6179 export_rdev(rdev);
6180 }
6181 autorun_array(mddev);
6182 mddev_unlock(mddev);
6183 }
6184 /* on success, candidates will be empty, on error
6185 * it won't...
6186 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006187 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006188 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 mddev_put(mddev);
6192 }
NeilBrown9d487392016-11-02 14:16:49 +11006193 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006195#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196
NeilBrownf72ffdd2014-09-30 14:23:59 +10006197static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198{
6199 mdu_version_t ver;
6200
6201 ver.major = MD_MAJOR_VERSION;
6202 ver.minor = MD_MINOR_VERSION;
6203 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6204
6205 if (copy_to_user(arg, &ver, sizeof(ver)))
6206 return -EFAULT;
6207
6208 return 0;
6209}
6210
NeilBrownf72ffdd2014-09-30 14:23:59 +10006211static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212{
6213 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006214 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006215 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216
NeilBrown1ca69c42012-10-11 13:37:33 +11006217 nr = working = insync = failed = spare = 0;
6218 rcu_read_lock();
6219 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006221 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 failed++;
6223 else {
6224 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006225 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006226 insync++;
Song Liub347af82016-08-11 17:14:45 -07006227 else if (test_bit(Journal, &rdev->flags))
6228 /* TODO: add journal count to md_u.h */
6229 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 else
6231 spare++;
6232 }
6233 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006234 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235
6236 info.major_version = mddev->major_version;
6237 info.minor_version = mddev->minor_version;
6238 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006239 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006241 info.size = mddev->dev_sectors / 2;
6242 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006243 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244 info.nr_disks = nr;
6245 info.raid_disks = mddev->raid_disks;
6246 info.md_minor = mddev->md_minor;
6247 info.not_persistent= !mddev->persistent;
6248
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006249 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 info.state = 0;
6251 if (mddev->in_sync)
6252 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006253 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006254 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006255 if (mddev_is_clustered(mddev))
6256 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006257 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 info.working_disks = working;
6259 info.failed_disks = failed;
6260 info.spare_disks = spare;
6261
6262 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006263 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264
6265 if (copy_to_user(arg, &info, sizeof(info)))
6266 return -EFAULT;
6267
6268 return 0;
6269}
6270
NeilBrownf72ffdd2014-09-30 14:23:59 +10006271static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006272{
6273 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006274 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006275 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006276
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006277 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006278 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006279 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006280
NeilBrown32a76272005-06-21 17:17:14 -07006281 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006282 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006283 /* bitmap enabled */
6284 if (mddev->bitmap_info.file) {
6285 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6286 sizeof(file->pathname));
6287 if (IS_ERR(ptr))
6288 err = PTR_ERR(ptr);
6289 else
6290 memmove(file->pathname, ptr,
6291 sizeof(file->pathname)-(ptr-file->pathname));
6292 }
NeilBrown4af1a042014-12-15 12:57:00 +11006293 spin_unlock(&mddev->lock);
6294
6295 if (err == 0 &&
6296 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006297 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006298
NeilBrown32a76272005-06-21 17:17:14 -07006299 kfree(file);
6300 return err;
6301}
6302
NeilBrownf72ffdd2014-09-30 14:23:59 +10006303static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304{
6305 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006306 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307
6308 if (copy_from_user(&info, arg, sizeof(info)))
6309 return -EFAULT;
6310
NeilBrown1ca69c42012-10-11 13:37:33 +11006311 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006312 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 if (rdev) {
6314 info.major = MAJOR(rdev->bdev->bd_dev);
6315 info.minor = MINOR(rdev->bdev->bd_dev);
6316 info.raid_disk = rdev->raid_disk;
6317 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006318 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006320 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 info.state |= (1<<MD_DISK_ACTIVE);
6322 info.state |= (1<<MD_DISK_SYNC);
6323 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006324 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006325 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006326 if (test_bit(WriteMostly, &rdev->flags))
6327 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006328 if (test_bit(FailFast, &rdev->flags))
6329 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 } else {
6331 info.major = info.minor = 0;
6332 info.raid_disk = -1;
6333 info.state = (1<<MD_DISK_REMOVED);
6334 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006335 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336
6337 if (copy_to_user(arg, &info, sizeof(info)))
6338 return -EFAULT;
6339
6340 return 0;
6341}
6342
NeilBrownf72ffdd2014-09-30 14:23:59 +10006343static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344{
6345 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006346 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 dev_t dev = MKDEV(info->major,info->minor);
6348
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006349 if (mddev_is_clustered(mddev) &&
6350 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006351 pr_warn("%s: Cannot add to clustered mddev.\n",
6352 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006353 return -EINVAL;
6354 }
6355
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6357 return -EOVERFLOW;
6358
6359 if (!mddev->raid_disks) {
6360 int err;
6361 /* expecting a device which has a superblock */
6362 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6363 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006364 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 PTR_ERR(rdev));
6366 return PTR_ERR(rdev);
6367 }
6368 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006369 struct md_rdev *rdev0
6370 = list_entry(mddev->disks.next,
6371 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006372 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 .load_super(rdev, rdev0, mddev->minor_version);
6374 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006375 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006376 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 bdevname(rdev0->bdev,b2));
6378 export_rdev(rdev);
6379 return -EINVAL;
6380 }
6381 }
6382 err = bind_rdev_to_array(rdev, mddev);
6383 if (err)
6384 export_rdev(rdev);
6385 return err;
6386 }
6387
6388 /*
6389 * add_new_disk can be used once the array is assembled
6390 * to add "hot spares". They must already have a superblock
6391 * written
6392 */
6393 if (mddev->pers) {
6394 int err;
6395 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006396 pr_warn("%s: personality does not support diskops!\n",
6397 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 return -EINVAL;
6399 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006400 if (mddev->persistent)
6401 rdev = md_import_device(dev, mddev->major_version,
6402 mddev->minor_version);
6403 else
6404 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006406 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 PTR_ERR(rdev));
6408 return PTR_ERR(rdev);
6409 }
NeilBrown1a855a02010-12-09 16:36:28 +11006410 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006411 if (!mddev->persistent) {
6412 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006413 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006414 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006415 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006416 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006417 } else
NeilBrown41158c72005-06-21 17:17:25 -07006418 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006419 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006420 } else
6421 super_types[mddev->major_version].
6422 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006423 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006424 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006425 /* This was a hot-add request, but events doesn't
6426 * match, so reject it.
6427 */
6428 export_rdev(rdev);
6429 return -EINVAL;
6430 }
6431
NeilBrownb2d444d2005-11-08 21:39:31 -08006432 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006433 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6434 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006435 else
6436 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006437 if (info->state & (1<<MD_DISK_FAILFAST))
6438 set_bit(FailFast, &rdev->flags);
6439 else
6440 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006441
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006442 if (info->state & (1<<MD_DISK_JOURNAL)) {
6443 struct md_rdev *rdev2;
6444 bool has_journal = false;
6445
6446 /* make sure no existing journal disk */
6447 rdev_for_each(rdev2, mddev) {
6448 if (test_bit(Journal, &rdev2->flags)) {
6449 has_journal = true;
6450 break;
6451 }
6452 }
NeilBrown230b55f2017-10-17 14:24:09 +11006453 if (has_journal || mddev->bitmap) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006454 export_rdev(rdev);
6455 return -EBUSY;
6456 }
Song Liubac624f2015-08-13 14:31:55 -07006457 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006458 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006459 /*
6460 * check whether the device shows up in other nodes
6461 */
6462 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006463 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006464 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006465 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006466 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006467 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006468 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006469 export_rdev(rdev);
6470 return err;
6471 }
6472 }
6473 }
6474
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 rdev->raid_disk = -1;
6476 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006477
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 if (err)
6479 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006480
6481 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006482 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6483 if (!err) {
6484 err = md_cluster_ops->new_disk_ack(mddev,
6485 err == 0);
6486 if (err)
6487 md_kick_rdev_from_array(rdev);
6488 }
6489 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006490 if (err)
6491 md_cluster_ops->add_new_disk_cancel(mddev);
6492 else
6493 err = add_bound_rdev(rdev);
6494 }
6495
6496 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006497 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006498
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 return err;
6500 }
6501
6502 /* otherwise, add_new_disk is only allowed
6503 * for major_version==0 superblocks
6504 */
6505 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006506 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 return -EINVAL;
6508 }
6509
6510 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6511 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006512 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006514 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 PTR_ERR(rdev));
6516 return PTR_ERR(rdev);
6517 }
6518 rdev->desc_nr = info->number;
6519 if (info->raid_disk < mddev->raid_disks)
6520 rdev->raid_disk = info->raid_disk;
6521 else
6522 rdev->raid_disk = -1;
6523
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006525 if (info->state & (1<<MD_DISK_SYNC))
6526 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006528 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6529 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006530 if (info->state & (1<<MD_DISK_FAILFAST))
6531 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006532
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006534 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006535 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6536 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006537 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006538 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539
NeilBrown2bf071b2006-01-06 00:20:55 -08006540 err = bind_rdev_to_array(rdev, mddev);
6541 if (err) {
6542 export_rdev(rdev);
6543 return err;
6544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545 }
6546
6547 return 0;
6548}
6549
NeilBrownf72ffdd2014-09-30 14:23:59 +10006550static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551{
6552 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006553 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554
Yufen Yuc42a0e22018-05-04 18:08:10 +08006555 if (!mddev->pers)
6556 return -ENODEV;
6557
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 rdev = find_rdev(mddev, dev);
6559 if (!rdev)
6560 return -ENXIO;
6561
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006562 if (rdev->raid_disk < 0)
6563 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006564
NeilBrown3ea8929d2013-04-24 11:42:41 +10006565 clear_bit(Blocked, &rdev->flags);
6566 remove_and_add_spares(mddev, rdev);
6567
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 if (rdev->raid_disk >= 0)
6569 goto busy;
6570
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006571kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006572 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006573 md_cluster_ops->remove_disk(mddev, rdev);
6574
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006575 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006576 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006577 if (mddev->thread)
6578 md_wakeup_thread(mddev->thread);
6579 else
6580 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006581 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582
6583 return 0;
6584busy:
NeilBrown9d487392016-11-02 14:16:49 +11006585 pr_debug("md: cannot remove active disk %s from %s ...\n",
6586 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 return -EBUSY;
6588}
6589
NeilBrownf72ffdd2014-09-30 14:23:59 +10006590static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591{
6592 char b[BDEVNAME_SIZE];
6593 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006594 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595
6596 if (!mddev->pers)
6597 return -ENODEV;
6598
6599 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006600 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 mdname(mddev));
6602 return -EINVAL;
6603 }
6604 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006605 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 mdname(mddev));
6607 return -EINVAL;
6608 }
6609
NeilBrownd710e132008-10-13 11:55:12 +11006610 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006612 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 PTR_ERR(rdev));
6614 return -EINVAL;
6615 }
6616
6617 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006618 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006620 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621
NeilBrown8190e752009-06-18 08:48:58 +10006622 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623
NeilBrownb2d444d2005-11-08 21:39:31 -08006624 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006625 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 bdevname(rdev->bdev,b), mdname(mddev));
6627 err = -EINVAL;
6628 goto abort_export;
6629 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006630
NeilBrownb2d444d2005-11-08 21:39:31 -08006631 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006633 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006634 err = bind_rdev_to_array(rdev, mddev);
6635 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006636 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637
6638 /*
6639 * The rest should better be atomic, we can have disk failures
6640 * noticed in interrupt contexts ...
6641 */
6642
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 rdev->raid_disk = -1;
6644
Shaohua Li29530792016-12-08 15:48:19 -08006645 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006646 if (!mddev->thread)
6647 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 /*
6649 * Kick recovery, maybe this spare has to be added to the
6650 * array immediately.
6651 */
6652 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6653 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006654 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655 return 0;
6656
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657abort_export:
6658 export_rdev(rdev);
6659 return err;
6660}
6661
NeilBrownfd01b882011-10-11 16:47:53 +11006662static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006663{
NeilBrown035328c2014-04-09 12:25:40 +10006664 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006665
NeilBrown36fa3062005-09-09 16:23:45 -07006666 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006667 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006668 return -EBUSY;
6669 if (mddev->recovery || mddev->sync_thread)
6670 return -EBUSY;
6671 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006672 }
6673
NeilBrown36fa3062005-09-09 16:23:45 -07006674 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006675 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006676 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006677
NeilBrown1e594bb2014-12-15 12:57:00 +11006678 if (mddev->bitmap || mddev->bitmap_info.file)
6679 return -EEXIST; /* cannot add when bitmap is present */
6680 f = fget(fd);
6681
6682 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006683 pr_warn("%s: error: failed to get bitmap file\n",
6684 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006685 return -EBADF;
6686 }
6687
NeilBrown1e594bb2014-12-15 12:57:00 +11006688 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006689 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006690 pr_warn("%s: error: bitmap file must be a regular file\n",
6691 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006692 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006693 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006694 pr_warn("%s: error: bitmap file must open for write\n",
6695 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006696 err = -EBADF;
6697 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006698 pr_warn("%s: error: bitmap file is already in use\n",
6699 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006700 err = -EBUSY;
6701 }
6702 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006703 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006704 return err;
6705 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006706 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006707 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006708 } else if (mddev->bitmap == NULL)
6709 return -ENOENT; /* cannot remove what isn't there */
6710 err = 0;
6711 if (mddev->pers) {
NeilBrown69e51b42010-06-01 19:37:35 +10006712 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006713 struct bitmap *bitmap;
6714
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006715 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006716 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006717 if (!IS_ERR(bitmap)) {
6718 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006719 err = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006720 } else
6721 err = PTR_ERR(bitmap);
NeilBrown52a0d492017-10-17 13:46:43 +11006722 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006723 md_bitmap_destroy(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11006724 fd = -1;
6725 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006726 mddev_resume(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11006727 } else if (fd < 0) {
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006728 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006729 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006730 mddev_resume(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006731 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006732 }
6733 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006734 struct file *f = mddev->bitmap_info.file;
6735 if (f) {
6736 spin_lock(&mddev->lock);
6737 mddev->bitmap_info.file = NULL;
6738 spin_unlock(&mddev->lock);
6739 fput(f);
6740 }
NeilBrown36fa3062005-09-09 16:23:45 -07006741 }
6742
NeilBrown32a76272005-06-21 17:17:14 -07006743 return err;
6744}
6745
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746/*
6747 * set_array_info is used two different ways
6748 * The original usage is when creating a new array.
6749 * In this usage, raid_disks is > 0 and it together with
6750 * level, size, not_persistent,layout,chunksize determine the
6751 * shape of the array.
6752 * This will always create an array with a type-0.90.0 superblock.
6753 * The newer usage is when assembling an array.
6754 * In this case raid_disks will be 0, and the major_version field is
6755 * use to determine which style super-blocks are to be found on the devices.
6756 * The minor and patch _version numbers are also kept incase the
6757 * super_block handler wishes to interpret them.
6758 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006759static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760{
6761
6762 if (info->raid_disks == 0) {
6763 /* just setting version number for superblock loading */
6764 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006765 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766 super_types[info->major_version].name == NULL) {
6767 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11006768 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 info->major_version);
6770 return -EINVAL;
6771 }
6772 mddev->major_version = info->major_version;
6773 mddev->minor_version = info->minor_version;
6774 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006775 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006776 /* ensure mddev_put doesn't delete this now that there
6777 * is some minimal configuration.
6778 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006779 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780 return 0;
6781 }
6782 mddev->major_version = MD_MAJOR_VERSION;
6783 mddev->minor_version = MD_MINOR_VERSION;
6784 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006785 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786
6787 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006788 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006789 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 mddev->raid_disks = info->raid_disks;
6791 /* don't set md_minor, it is determined by which /dev/md* was
6792 * openned
6793 */
6794 if (info->state & (1<<MD_SB_CLEAN))
6795 mddev->recovery_cp = MaxSector;
6796 else
6797 mddev->recovery_cp = 0;
6798 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006799 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800
6801 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006802 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803
Shaohua Li29530792016-12-08 15:48:19 -08006804 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11006805 mddev->max_disks = MD_SB_DISKS;
6806 mddev->flags = 0;
6807 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006808 }
6809 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810
NeilBrownc3d97142009-12-14 12:49:52 +11006811 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006812 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006813 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006814
NeilBrownf6705572006-03-27 01:18:11 -08006815 mddev->reshape_position = MaxSector;
6816
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 /*
6818 * Generate a 128 bit UUID
6819 */
6820 get_random_bytes(mddev->uuid, 16);
6821
NeilBrownf6705572006-03-27 01:18:11 -08006822 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006823 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006824 mddev->new_layout = mddev->layout;
6825 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006826 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006827
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 return 0;
6829}
6830
NeilBrownfd01b882011-10-11 16:47:53 +11006831void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006832{
Shaohua Liefa4b772017-10-18 22:08:13 -07006833 lockdep_assert_held(&mddev->reconfig_mutex);
Dan Williamsb522adc2009-03-31 15:00:31 +11006834
6835 if (mddev->external_size)
6836 return;
6837
Dan Williams1f403622009-03-31 14:59:03 +11006838 mddev->array_sectors = array_sectors;
6839}
6840EXPORT_SYMBOL(md_set_array_sectors);
6841
NeilBrownfd01b882011-10-11 16:47:53 +11006842static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006843{
NeilBrown3cb03002011-10-11 16:45:26 +11006844 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006845 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006846 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08006847 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006848
NeilBrowna35b0d62006-01-06 00:20:49 -08006849 if (mddev->pers->resize == NULL)
6850 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006851 /* The "num_sectors" is the number of sectors of each device that
6852 * is used. This can only make sense for arrays with redundancy.
6853 * linear and raid0 always use whatever space is available. We can only
6854 * consider changing this number if no resync or reconstruction is
6855 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006856 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006857 * of each device. If num_sectors is zero, we find the largest size
6858 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006859 */
NeilBrownf851b602014-12-11 10:02:10 +11006860 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6861 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006862 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006863 if (mddev->ro)
6864 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006865
NeilBrowndafb20f2012-03-19 12:46:39 +11006866 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006867 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006868
Andre Nolld71f9f82008-07-11 22:02:22 +10006869 if (fit && (num_sectors == 0 || num_sectors > avail))
6870 num_sectors = avail;
6871 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006872 return -ENOSPC;
6873 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006874 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08006875 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08006876 if (mddev_is_clustered(mddev))
6877 md_cluster_ops->update_size(mddev, old_dev_sectors);
6878 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08006879 set_capacity(mddev->gendisk, mddev->array_sectors);
6880 revalidate_disk(mddev->gendisk);
6881 }
6882 }
NeilBrowna35b0d62006-01-06 00:20:49 -08006883 return rv;
6884}
6885
NeilBrownfd01b882011-10-11 16:47:53 +11006886static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006887{
6888 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006889 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006890 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006891 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006892 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006893 if (mddev->ro)
6894 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006895 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006896 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006897 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006898 if (mddev->sync_thread ||
6899 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6900 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006901 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006902
6903 rdev_for_each(rdev, mddev) {
6904 if (mddev->raid_disks < raid_disks &&
6905 rdev->data_offset < rdev->new_data_offset)
6906 return -EINVAL;
6907 if (mddev->raid_disks > raid_disks &&
6908 rdev->data_offset > rdev->new_data_offset)
6909 return -EINVAL;
6910 }
6911
NeilBrown63c70c42006-03-27 01:18:13 -08006912 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006913 if (mddev->delta_disks < 0)
6914 mddev->reshape_backwards = 1;
6915 else if (mddev->delta_disks > 0)
6916 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006917
6918 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006919 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006920 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006921 mddev->reshape_backwards = 0;
6922 }
NeilBrownda943b992006-01-06 00:20:54 -08006923 return rv;
6924}
6925
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926/*
6927 * update_array_info is used to change the configuration of an
6928 * on-line array.
6929 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6930 * fields in the info are checked against the array.
6931 * Any differences that cannot be handled will cause an error.
6932 * Normally, only one change can be managed at a time.
6933 */
NeilBrownfd01b882011-10-11 16:47:53 +11006934static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935{
6936 int rv = 0;
6937 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006938 int state = 0;
6939
6940 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006941 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006942 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943
6944 if (mddev->major_version != info->major_version ||
6945 mddev->minor_version != info->minor_version ||
6946/* mddev->patch_version != info->patch_version || */
6947 mddev->ctime != info->ctime ||
6948 mddev->level != info->level ||
6949/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006950 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006951 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006952 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6953 ((state^info->state) & 0xfffffe00)
6954 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955 return -EINVAL;
6956 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006957 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6958 cnt++;
6959 if (mddev->raid_disks != info->raid_disks)
6960 cnt++;
6961 if (mddev->layout != info->layout)
6962 cnt++;
6963 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6964 cnt++;
6965 if (cnt == 0)
6966 return 0;
6967 if (cnt > 1)
6968 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969
6970 if (mddev->layout != info->layout) {
6971 /* Change layout
6972 * we don't need to do anything at the md level, the
6973 * personality will take care of it all.
6974 */
NeilBrown50ac1682009-06-18 08:47:55 +10006975 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006977 else {
6978 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006979 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006980 if (rv)
6981 mddev->new_layout = mddev->layout;
6982 return rv;
6983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006985 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006986 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987
NeilBrownda943b992006-01-06 00:20:54 -08006988 if (mddev->raid_disks != info->raid_disks)
6989 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990
NeilBrown36fa3062005-09-09 16:23:45 -07006991 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006992 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6993 rv = -EINVAL;
6994 goto err;
6995 }
6996 if (mddev->recovery || mddev->sync_thread) {
6997 rv = -EBUSY;
6998 goto err;
6999 }
NeilBrown36fa3062005-09-09 16:23:45 -07007000 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007001 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07007002 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007003 if (mddev->bitmap) {
7004 rv = -EEXIST;
7005 goto err;
7006 }
7007 if (mddev->bitmap_info.default_offset == 0) {
7008 rv = -EINVAL;
7009 goto err;
7010 }
NeilBrownc3d97142009-12-14 12:49:52 +11007011 mddev->bitmap_info.offset =
7012 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10007013 mddev->bitmap_info.space =
7014 mddev->bitmap_info.default_space;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007015 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007016 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007017 if (!IS_ERR(bitmap)) {
7018 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007019 rv = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007020 } else
7021 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07007022 if (rv)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007023 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007024 mddev_resume(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07007025 } else {
7026 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007027 if (!mddev->bitmap) {
7028 rv = -ENOENT;
7029 goto err;
7030 }
7031 if (mddev->bitmap->storage.file) {
7032 rv = -EINVAL;
7033 goto err;
7034 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11007035 if (mddev->bitmap_info.nodes) {
7036 /* hold PW on all the bitmap lock */
7037 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11007038 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 +11007039 rv = -EPERM;
7040 md_cluster_ops->unlock_all_bitmaps(mddev);
7041 goto err;
7042 }
7043
7044 mddev->bitmap_info.nodes = 0;
7045 md_cluster_ops->leave(mddev);
7046 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007047 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007048 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007049 mddev_resume(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11007050 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007051 }
7052 }
NeilBrown850b2b422006-10-03 01:15:46 -07007053 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007054 return rv;
7055err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 return rv;
7057}
7058
NeilBrownfd01b882011-10-11 16:47:53 +11007059static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060{
NeilBrown3cb03002011-10-11 16:45:26 +11007061 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11007062 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063
7064 if (mddev->pers == NULL)
7065 return -ENODEV;
7066
NeilBrown1ca69c42012-10-11 13:37:33 +11007067 rcu_read_lock();
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01007068 rdev = md_find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11007070 err = -ENODEV;
7071 else {
7072 md_error(mddev, rdev);
7073 if (!test_bit(Faulty, &rdev->flags))
7074 err = -EBUSY;
7075 }
7076 rcu_read_unlock();
7077 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078}
7079
Andre Noll2f9618c2008-04-25 18:57:58 +02007080/*
7081 * We have a problem here : there is no easy way to give a CHS
7082 * virtual geometry. We currently pretend that we have a 2 heads
7083 * 4 sectors (with a BIG number of cylinders...). This drives
7084 * dosfs just mad... ;-)
7085 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007086static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7087{
NeilBrownfd01b882011-10-11 16:47:53 +11007088 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007089
7090 geo->heads = 2;
7091 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11007092 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007093 return 0;
7094}
7095
Nicolas Schichancb335f82014-01-15 16:58:52 +01007096static inline bool md_ioctl_valid(unsigned int cmd)
7097{
7098 switch (cmd) {
7099 case ADD_NEW_DISK:
7100 case BLKROSET:
7101 case GET_ARRAY_INFO:
7102 case GET_BITMAP_FILE:
7103 case GET_DISK_INFO:
7104 case HOT_ADD_DISK:
7105 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007106 case RAID_AUTORUN:
7107 case RAID_VERSION:
7108 case RESTART_ARRAY_RW:
7109 case RUN_ARRAY:
7110 case SET_ARRAY_INFO:
7111 case SET_BITMAP_FILE:
7112 case SET_DISK_FAULTY:
7113 case STOP_ARRAY:
7114 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007115 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007116 return true;
7117 default:
7118 return false;
7119 }
7120}
7121
Al Viroa39907f2008-03-02 10:31:15 -05007122static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123 unsigned int cmd, unsigned long arg)
7124{
7125 int err = 0;
7126 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11007127 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10007128 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08007129 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130
Nicolas Schichancb335f82014-01-15 16:58:52 +01007131 if (!md_ioctl_valid(cmd))
7132 return -ENOTTY;
7133
NeilBrown506c9e42011-12-23 10:17:26 +11007134 switch (cmd) {
7135 case RAID_VERSION:
7136 case GET_ARRAY_INFO:
7137 case GET_DISK_INFO:
7138 break;
7139 default:
7140 if (!capable(CAP_SYS_ADMIN))
7141 return -EACCES;
7142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143
7144 /*
7145 * Commands dealing with the RAID driver but not any
7146 * particular array:
7147 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007148 switch (cmd) {
7149 case RAID_VERSION:
7150 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007151 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11007154 case RAID_AUTORUN:
7155 err = 0;
7156 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007157 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11007159 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160 }
7161
7162 /*
7163 * Commands creating/starting a new array:
7164 */
7165
Al Viroa39907f2008-03-02 10:31:15 -05007166 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167
7168 if (!mddev) {
7169 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007170 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171 }
7172
NeilBrown1ca69c42012-10-11 13:37:33 +11007173 /* Some actions do not requires the mutex */
7174 switch (cmd) {
7175 case GET_ARRAY_INFO:
7176 if (!mddev->raid_disks && !mddev->external)
7177 err = -ENODEV;
7178 else
7179 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007180 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007181
7182 case GET_DISK_INFO:
7183 if (!mddev->raid_disks && !mddev->external)
7184 err = -ENODEV;
7185 else
7186 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007187 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007188
7189 case SET_DISK_FAULTY:
7190 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007191 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007192
7193 case GET_BITMAP_FILE:
7194 err = get_bitmap_file(mddev, argp);
7195 goto out;
7196
NeilBrown1ca69c42012-10-11 13:37:33 +11007197 }
7198
NeilBrowna7a3f082012-12-11 13:35:54 +11007199 if (cmd == ADD_NEW_DISK)
7200 /* need to ensure md_delayed_delete() has completed */
7201 flush_workqueue(md_misc_wq);
7202
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007203 if (cmd == HOT_REMOVE_DISK)
7204 /* need to ensure recovery thread has run */
7205 wait_event_interruptible_timeout(mddev->sb_wait,
7206 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007207 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007208 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007209 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7210 /* Need to flush page cache, and ensure no-one else opens
7211 * and writes
7212 */
7213 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007214 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007215 mutex_unlock(&mddev->open_mutex);
7216 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007217 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007218 }
NeilBrown065e5192017-04-06 11:16:33 +08007219 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007220 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007221 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007222 mutex_unlock(&mddev->open_mutex);
7223 sync_blockdev(bdev);
7224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225 err = mddev_lock(mddev);
7226 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007227 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7228 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007229 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230 }
7231
NeilBrownc02c0ae2012-12-11 13:39:21 +11007232 if (cmd == SET_ARRAY_INFO) {
7233 mdu_array_info_t info;
7234 if (!arg)
7235 memset(&info, 0, sizeof(info));
7236 else if (copy_from_user(&info, argp, sizeof(info))) {
7237 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007238 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007239 }
7240 if (mddev->pers) {
7241 err = update_array_info(mddev, &info);
7242 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007243 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007244 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007246 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007247 }
7248 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007249 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007250 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007251 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007252 }
7253 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007254 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007255 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007256 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007257 }
7258 err = set_array_info(mddev, &info);
7259 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007260 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007261 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007262 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007263 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264 }
7265
7266 /*
7267 * Commands querying/configuring an existing array:
7268 */
NeilBrown32a76272005-06-21 17:17:14 -07007269 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007270 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007271 if ((!mddev->raid_disks && !mddev->external)
7272 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7273 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7274 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007276 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 }
7278
7279 /*
7280 * Commands even a read-only array can execute:
7281 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007282 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007283 case RESTART_ARRAY_RW:
7284 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007285 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007286
7287 case STOP_ARRAY:
7288 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007289 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007290
7291 case STOP_ARRAY_RO:
7292 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007293 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007294
NeilBrown3ea8929d2013-04-24 11:42:41 +10007295 case HOT_REMOVE_DISK:
7296 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007297 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007298
NeilBrown7ceb17e2013-04-24 11:42:42 +10007299 case ADD_NEW_DISK:
7300 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007301 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007302 * So require mddev->pers and MD_DISK_SYNC.
7303 */
7304 if (mddev->pers) {
7305 mdu_disk_info_t info;
7306 if (copy_from_user(&info, argp, sizeof(info)))
7307 err = -EFAULT;
7308 else if (!(info.state & (1<<MD_DISK_SYNC)))
7309 /* Need to clear read-only for this */
7310 break;
7311 else
7312 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007313 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007314 }
7315 break;
7316
NeilBrownc02c0ae2012-12-11 13:39:21 +11007317 case BLKROSET:
7318 if (get_user(ro, (int __user *)(arg))) {
7319 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007320 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007321 }
7322 err = -EINVAL;
7323
7324 /* if the bdev is going readonly the value of mddev->ro
7325 * does not matter, no writes are coming
7326 */
7327 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007328 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007329
NeilBrownc02c0ae2012-12-11 13:39:21 +11007330 /* are we are already prepared for writes? */
7331 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007332 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007333
7334 /* transitioning to readauto need only happen for
7335 * arrays that call md_write_start
7336 */
7337 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007339 if (err == 0) {
7340 mddev->ro = 2;
7341 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007342 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007343 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007344 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345 }
7346
7347 /*
7348 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007349 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 */
NeilBrown326eb172014-09-30 15:36:28 +10007351 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007352 if (mddev->ro == 2) {
7353 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007354 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007355 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007356 /* mddev_unlock will wake thread */
7357 /* If a device failed while we were read-only, we
7358 * need to make sure the metadata is updated now.
7359 */
Shaohua Li29530792016-12-08 15:48:19 -08007360 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007361 mddev_unlock(mddev);
7362 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007363 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7364 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007365 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007366 }
NeilBrownf91de922005-11-08 21:39:36 -08007367 } else {
7368 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007369 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371 }
7372
NeilBrownc02c0ae2012-12-11 13:39:21 +11007373 switch (cmd) {
7374 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007376 mdu_disk_info_t info;
7377 if (copy_from_user(&info, argp, sizeof(info)))
7378 err = -EFAULT;
7379 else
7380 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007381 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007384 case CLUSTERED_DISK_NACK:
7385 if (mddev_is_clustered(mddev))
7386 md_cluster_ops->new_disk_ack(mddev, false);
7387 else
7388 err = -EINVAL;
7389 goto unlock;
7390
NeilBrownc02c0ae2012-12-11 13:39:21 +11007391 case HOT_ADD_DISK:
7392 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007393 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394
NeilBrownc02c0ae2012-12-11 13:39:21 +11007395 case RUN_ARRAY:
7396 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007397 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398
NeilBrownc02c0ae2012-12-11 13:39:21 +11007399 case SET_BITMAP_FILE:
7400 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007401 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007402
NeilBrownc02c0ae2012-12-11 13:39:21 +11007403 default:
7404 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007405 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406 }
7407
NeilBrown3adc28d2014-09-30 15:46:41 +10007408unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007409 if (mddev->hold_active == UNTIL_IOCTL &&
7410 err != -EINVAL)
7411 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007413out:
NeilBrown065e5192017-04-06 11:16:33 +08007414 if(did_set_md_closing)
7415 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416 return err;
7417}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007418#ifdef CONFIG_COMPAT
7419static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7420 unsigned int cmd, unsigned long arg)
7421{
7422 switch (cmd) {
7423 case HOT_REMOVE_DISK:
7424 case HOT_ADD_DISK:
7425 case SET_DISK_FAULTY:
7426 case SET_BITMAP_FILE:
7427 /* These take in integer arg, do not convert */
7428 break;
7429 default:
7430 arg = (unsigned long)compat_ptr(arg);
7431 break;
7432 }
7433
7434 return md_ioctl(bdev, mode, cmd, arg);
7435}
7436#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437
Al Viroa39907f2008-03-02 10:31:15 -05007438static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439{
7440 /*
7441 * Succeed if we can lock the mddev, which confirms that
7442 * it isn't being stopped right now.
7443 */
NeilBrownfd01b882011-10-11 16:47:53 +11007444 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445 int err;
7446
Yuanhan Liu0c098222012-05-22 13:55:32 +10007447 if (!mddev)
7448 return -ENODEV;
7449
NeilBrownd3374822009-01-09 08:31:10 +11007450 if (mddev->gendisk != bdev->bd_disk) {
7451 /* we are racing with mddev_put which is discarding this
7452 * bd_disk.
7453 */
7454 mddev_put(mddev);
7455 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007456 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007457 /* Then retry the open from the top */
7458 return -ERESTARTSYS;
7459 }
7460 BUG_ON(mddev != bdev->bd_disk->private_data);
7461
NeilBrownc8c00a62009-08-10 12:50:52 +10007462 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463 goto out;
7464
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007465 if (test_bit(MD_CLOSING, &mddev->flags)) {
7466 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007467 err = -ENODEV;
7468 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007469 }
7470
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007472 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007473 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007475 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007477 if (err)
7478 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479 return err;
7480}
7481
Al Virodb2a1442013-05-05 21:52:57 -04007482static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007484 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007486 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007487 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007490
7491static int md_media_changed(struct gendisk *disk)
7492{
NeilBrownfd01b882011-10-11 16:47:53 +11007493 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007494
7495 return mddev->changed;
7496}
7497
7498static int md_revalidate(struct gendisk *disk)
7499{
NeilBrownfd01b882011-10-11 16:47:53 +11007500 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007501
7502 mddev->changed = 0;
7503 return 0;
7504}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007505static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506{
7507 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007508 .open = md_open,
7509 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007510 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007511#ifdef CONFIG_COMPAT
7512 .compat_ioctl = md_compat_ioctl,
7513#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007514 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007515 .media_changed = md_media_changed,
7516 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007517};
7518
NeilBrownf72ffdd2014-09-30 14:23:59 +10007519static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520{
NeilBrown2b8bf342011-10-11 16:48:23 +11007521 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523 /*
7524 * md_thread is a 'system-thread', it's priority should be very
7525 * high. We avoid resource deadlocks individually in each
7526 * raid personality. (RAID5 does preallocation) We also use RR and
7527 * the very same RT priority as kswapd, thus we will never get
7528 * into a priority inversion deadlock.
7529 *
7530 * we definitely have to have equal or higher priority than
7531 * bdflush, otherwise bdflush will deadlock if there are too
7532 * many dirty RAID5 blocks.
7533 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534
NeilBrown6985c432005-10-19 21:23:47 -07007535 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007536 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537
NeilBrown93588e22005-11-15 00:09:12 -08007538 /* We need to wait INTERRUPTIBLE so that
7539 * we don't add to the load-average.
7540 * That means we need to be sure no signals are
7541 * pending
7542 */
7543 if (signal_pending(current))
7544 flush_signals(current);
7545
7546 wait_event_interruptible_timeout
7547 (thread->wqueue,
7548 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007549 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007550 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551
NeilBrown6c987912011-01-14 09:13:53 +11007552 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007553 if (kthread_should_park())
7554 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007555 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007556 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007558
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559 return 0;
7560}
7561
NeilBrown2b8bf342011-10-11 16:48:23 +11007562void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563{
7564 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007565 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Guoqing Jiangd1d90142017-10-09 10:32:48 +08007566 set_bit(THREAD_WAKEUP, &thread->flags);
7567 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 }
7569}
NeilBrown6c144d32014-09-30 16:15:38 +10007570EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571
Shaohua Li4ed87312012-10-11 13:34:00 +11007572struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7573 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574{
NeilBrown2b8bf342011-10-11 16:48:23 +11007575 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576
NeilBrown2b8bf342011-10-11 16:48:23 +11007577 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578 if (!thread)
7579 return NULL;
7580
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 init_waitqueue_head(&thread->wqueue);
7582
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 thread->run = run;
7584 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007585 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007586 thread->tsk = kthread_run(md_thread, thread,
7587 "%s_%s",
7588 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007589 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007590 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 kfree(thread);
7592 return NULL;
7593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 return thread;
7595}
NeilBrown6c144d32014-09-30 16:15:38 +10007596EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007597
NeilBrown2b8bf342011-10-11 16:48:23 +11007598void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599{
NeilBrown2b8bf342011-10-11 16:48:23 +11007600 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007601 if (!thread)
7602 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007603 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007604 /* Locking ensures that mddev_unlock does not wake_up a
7605 * non-existent thread
7606 */
7607 spin_lock(&pers_lock);
7608 *threadp = NULL;
7609 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007610
7611 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612 kfree(thread);
7613}
NeilBrown6c144d32014-09-30 16:15:38 +10007614EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615
NeilBrownfd01b882011-10-11 16:47:53 +11007616void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617{
NeilBrownb2d444d2005-11-08 21:39:31 -08007618 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007620
NeilBrownde393cd2011-07-28 11:31:48 +10007621 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622 return;
7623 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007624 if (mddev->degraded)
7625 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007626 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7628 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7629 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007630 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007631 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007632 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633}
NeilBrown6c144d32014-09-30 16:15:38 +10007634EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635
7636/* seq_file implementation /proc/mdstat */
7637
7638static void status_unused(struct seq_file *seq)
7639{
7640 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007641 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642
7643 seq_printf(seq, "unused devices: ");
7644
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007645 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646 char b[BDEVNAME_SIZE];
7647 i++;
7648 seq_printf(seq, "%s ",
7649 bdevname(rdev->bdev,b));
7650 }
7651 if (!i)
7652 seq_printf(seq, "<none>");
7653
7654 seq_printf(seq, "\n");
7655}
7656
NeilBrownf7851be2015-07-02 17:12:58 +10007657static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658{
NeilBrowndd71cf62009-05-07 12:49:35 +10007659 sector_t max_sectors, resync, res;
7660 unsigned long dt, db;
7661 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007662 int scale;
7663 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664
NeilBrownc804cde2012-05-21 09:28:33 +10007665 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7666 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007667 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007669 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670
NeilBrownf7851be2015-07-02 17:12:58 +10007671 resync = mddev->curr_resync;
7672 if (resync <= 3) {
7673 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7674 /* Still cleaning up */
7675 resync = max_sectors;
Nate Daileyd2e2ec82017-11-30 11:33:30 -05007676 } else if (resync > max_sectors)
7677 resync = max_sectors;
7678 else
NeilBrownf7851be2015-07-02 17:12:58 +10007679 resync -= atomic_read(&mddev->recovery_active);
7680
7681 if (resync == 0) {
Guoqing Jiang0357ba22018-07-02 16:26:25 +08007682 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
7683 struct md_rdev *rdev;
7684
7685 rdev_for_each(rdev, mddev)
7686 if (rdev->raid_disk >= 0 &&
7687 !test_bit(Faulty, &rdev->flags) &&
7688 rdev->recovery_offset != MaxSector &&
7689 rdev->recovery_offset) {
7690 seq_printf(seq, "\trecover=REMOTE");
7691 return 1;
7692 }
7693 if (mddev->reshape_position != MaxSector)
7694 seq_printf(seq, "\treshape=REMOTE");
7695 else
7696 seq_printf(seq, "\tresync=REMOTE");
7697 return 1;
7698 }
NeilBrownf7851be2015-07-02 17:12:58 +10007699 if (mddev->recovery_cp < MaxSector) {
7700 seq_printf(seq, "\tresync=PENDING");
7701 return 1;
7702 }
7703 return 0;
7704 }
7705 if (resync < 3) {
7706 seq_printf(seq, "\tresync=DELAYED");
7707 return 1;
7708 }
7709
NeilBrown403df472014-09-30 15:52:29 +10007710 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007711 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007712 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007713 * u32, as those are the requirements for sector_div.
7714 * Thus 'scale' must be at least 10
7715 */
7716 scale = 10;
7717 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007718 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007719 scale++;
7720 }
7721 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007722 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007723
7724 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725 {
NeilBrown4588b422006-03-27 01:18:04 -08007726 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727 seq_printf(seq, "[");
7728 for (i = 0; i < x; i++)
7729 seq_printf(seq, "=");
7730 seq_printf(seq, ">");
7731 for (i = 0; i < y; i++)
7732 seq_printf(seq, ".");
7733 seq_printf(seq, "] ");
7734 }
NeilBrown4588b422006-03-27 01:18:04 -08007735 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007736 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7737 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007738 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7739 "check" :
7740 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7741 "resync" : "recovery"))),
7742 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007743 (unsigned long long) resync/2,
7744 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745
7746 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007747 * dt: time from mark until now
7748 * db: blocks written from mark until now
7749 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007750 *
7751 * rt is a sector_t, so could be 32bit or 64bit.
7752 * So we divide before multiply in case it is 32bit and close
7753 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007754 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007755 * near the end of resync when the number of remaining sectors
7756 * is close to 'db'.
7757 * We then divide rt by 32 after multiplying by db to compensate.
7758 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007759 */
7760 dt = ((jiffies - mddev->resync_mark) / HZ);
7761 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007762 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7763 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764
NeilBrowndd71cf62009-05-07 12:49:35 +10007765 rt = max_sectors - resync; /* number of remaining sectors */
7766 sector_div(rt, db/32+1);
7767 rt *= dt;
7768 rt >>= 5;
7769
7770 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7771 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772
NeilBrownff4e8d92006-07-10 04:44:16 -07007773 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007774 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007775}
7776
7777static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7778{
7779 struct list_head *tmp;
7780 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007781 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007782
7783 if (l >= 0x10000)
7784 return NULL;
7785 if (!l--)
7786 /* header */
7787 return (void*)1;
7788
7789 spin_lock(&all_mddevs_lock);
7790 list_for_each(tmp,&all_mddevs)
7791 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007792 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793 mddev_get(mddev);
7794 spin_unlock(&all_mddevs_lock);
7795 return mddev;
7796 }
7797 spin_unlock(&all_mddevs_lock);
7798 if (!l--)
7799 return (void*)2;/* tail */
7800 return NULL;
7801}
7802
7803static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7804{
7805 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007806 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007807
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808 ++*pos;
7809 if (v == (void*)2)
7810 return NULL;
7811
7812 spin_lock(&all_mddevs_lock);
7813 if (v == (void*)1)
7814 tmp = all_mddevs.next;
7815 else
7816 tmp = mddev->all_mddevs.next;
7817 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007818 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007819 else {
7820 next_mddev = (void*)2;
7821 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823 spin_unlock(&all_mddevs_lock);
7824
7825 if (v != (void*)1)
7826 mddev_put(mddev);
7827 return next_mddev;
7828
7829}
7830
7831static void md_seq_stop(struct seq_file *seq, void *v)
7832{
NeilBrownfd01b882011-10-11 16:47:53 +11007833 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007834
7835 if (mddev && v != (void*)1 && v != (void*)2)
7836 mddev_put(mddev);
7837}
7838
7839static int md_seq_show(struct seq_file *seq, void *v)
7840{
NeilBrownfd01b882011-10-11 16:47:53 +11007841 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007842 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007843 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844
7845 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007846 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847 seq_printf(seq, "Personalities : ");
7848 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007849 list_for_each_entry(pers, &pers_list, list)
7850 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851
7852 spin_unlock(&pers_lock);
7853 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007854 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007855 return 0;
7856 }
7857 if (v == (void*)2) {
7858 status_unused(seq);
7859 return 0;
7860 }
7861
NeilBrown36d091f2014-12-15 12:56:58 +11007862 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7864 seq_printf(seq, "%s : %sactive", mdname(mddev),
7865 mddev->pers ? "" : "in");
7866 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007867 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007869 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007870 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007871 seq_printf(seq, " %s", mddev->pers->name);
7872 }
7873
Andre Nolldd8ac332009-03-31 14:33:13 +11007874 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007875 rcu_read_lock();
7876 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877 char b[BDEVNAME_SIZE];
7878 seq_printf(seq, " %s[%d]",
7879 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007880 if (test_bit(WriteMostly, &rdev->flags))
7881 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007882 if (test_bit(Journal, &rdev->flags))
7883 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007884 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007885 seq_printf(seq, "(F)");
7886 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007887 }
7888 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007889 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007890 if (test_bit(Replacement, &rdev->flags))
7891 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007892 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007894 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007895
7896 if (!list_empty(&mddev->disks)) {
7897 if (mddev->pers)
7898 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007899 (unsigned long long)
7900 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901 else
7902 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007903 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007904 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007905 if (mddev->persistent) {
7906 if (mddev->major_version != 0 ||
7907 mddev->minor_version != 90) {
7908 seq_printf(seq," super %d.%d",
7909 mddev->major_version,
7910 mddev->minor_version);
7911 }
NeilBrowne6910632008-02-06 01:39:51 -08007912 } else if (mddev->external)
7913 seq_printf(seq, " super external:%s",
7914 mddev->metadata_type);
7915 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007916 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917
7918 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007919 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007920 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007921 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007922 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007923 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007924 }
NeilBrown32a76272005-06-21 17:17:14 -07007925 } else
7926 seq_printf(seq, "\n ");
7927
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007928 md_bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929
7930 seq_printf(seq, "\n");
7931 }
NeilBrown36d091f2014-12-15 12:56:58 +11007932 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007933
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934 return 0;
7935}
7936
Jan Engelhardt110518b2009-05-07 12:49:37 +10007937static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938 .start = md_seq_start,
7939 .next = md_seq_next,
7940 .stop = md_seq_stop,
7941 .show = md_seq_show,
7942};
7943
7944static int md_seq_open(struct inode *inode, struct file *file)
7945{
Kay Sieversf1514632011-07-12 20:48:39 +02007946 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007947 int error;
7948
7949 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007950 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007951 return error;
7952
7953 seq = file->private_data;
7954 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007955 return error;
7956}
7957
NeilBrowne2f23b62014-04-09 14:33:51 +10007958static int md_unloading;
Al Viroafc9a422017-07-03 06:39:46 -04007959static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
NeilBrownd7603b72006-01-06 00:20:30 -08007960{
Kay Sieversf1514632011-07-12 20:48:39 +02007961 struct seq_file *seq = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -04007962 __poll_t mask;
NeilBrownd7603b72006-01-06 00:20:30 -08007963
NeilBrowne2f23b62014-04-09 14:33:51 +10007964 if (md_unloading)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007965 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007966 poll_wait(filp, &md_event_waiters, wait);
7967
7968 /* always allow read */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007969 mask = EPOLLIN | EPOLLRDNORM;
NeilBrownd7603b72006-01-06 00:20:30 -08007970
Kay Sieversf1514632011-07-12 20:48:39 +02007971 if (seq->poll_event != atomic_read(&md_event_count))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007972 mask |= EPOLLERR | EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007973 return mask;
7974}
7975
Arjan van de Venfa027c22007-02-12 00:55:33 -08007976static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007977 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007978 .open = md_seq_open,
7979 .read = seq_read,
7980 .llseek = seq_lseek,
Cihangir Akturk26e13042017-07-29 19:52:45 +03007981 .release = seq_release,
NeilBrownd7603b72006-01-06 00:20:30 -08007982 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007983};
7984
NeilBrown84fc4b52011-10-11 16:49:58 +11007985int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007986{
NeilBrown9d487392016-11-02 14:16:49 +11007987 pr_debug("md: %s personality registered for level %d\n",
7988 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007990 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991 spin_unlock(&pers_lock);
7992 return 0;
7993}
NeilBrown6c144d32014-09-30 16:15:38 +10007994EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007995
NeilBrown84fc4b52011-10-11 16:49:58 +11007996int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997{
NeilBrown9d487392016-11-02 14:16:49 +11007998 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007999 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008000 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008001 spin_unlock(&pers_lock);
8002 return 0;
8003}
NeilBrown6c144d32014-09-30 16:15:38 +10008004EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005
NeilBrown6022e752015-08-13 12:32:55 +10008006int register_md_cluster_operations(struct md_cluster_operations *ops,
8007 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008008{
NeilBrown6022e752015-08-13 12:32:55 +10008009 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008010 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008011 if (md_cluster_ops != NULL)
8012 ret = -EALREADY;
8013 else {
8014 md_cluster_ops = ops;
8015 md_cluster_mod = module;
8016 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008017 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008018 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008019}
8020EXPORT_SYMBOL(register_md_cluster_operations);
8021
8022int unregister_md_cluster_operations(void)
8023{
8024 spin_lock(&pers_lock);
8025 md_cluster_ops = NULL;
8026 spin_unlock(&pers_lock);
8027 return 0;
8028}
8029EXPORT_SYMBOL(unregister_md_cluster_operations);
8030
8031int md_setup_cluster(struct mddev *mddev, int nodes)
8032{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008033 if (!md_cluster_ops)
8034 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008035 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008036 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008037 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11008038 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008039 spin_unlock(&pers_lock);
8040 return -ENOENT;
8041 }
8042 spin_unlock(&pers_lock);
8043
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05008044 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008045}
8046
8047void md_cluster_stop(struct mddev *mddev)
8048{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05008049 if (!md_cluster_ops)
8050 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008051 md_cluster_ops->leave(mddev);
8052 module_put(md_cluster_mod);
8053}
8054
NeilBrownfd01b882011-10-11 16:47:53 +11008055static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056{
NeilBrownf72ffdd2014-09-30 14:23:59 +10008057 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008058 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11008059 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008060
8061 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10008062 rcu_read_lock();
8063 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008064 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Michael Callahan59767fb2018-07-18 04:47:37 -07008065 curr_events = (int)part_stat_read_accum(&disk->part0, sectors) -
NeilBrowneea1bf32009-03-31 14:27:02 +11008066 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07008067 /* sync IO will cause sync_io to increase before the disk_stats
8068 * as sync_io is counted when a request starts, and
8069 * disk_stats is counted when it completes.
8070 * So resync activity will cause curr_events to be smaller than
8071 * when there was no such activity.
8072 * non-sync IO will cause disk_stat to increase without
8073 * increasing sync_io so curr_events will (eventually)
8074 * be larger than it was before. Once it becomes
8075 * substantially larger, the test below will cause
8076 * the array to appear non-idle, and resync will slow
8077 * down.
8078 * If there is a lot of outstanding resync activity when
8079 * we set last_event to curr_events, then all that activity
8080 * completing might cause the array to appear non-idle
8081 * and resync will be slowed down even though there might
8082 * not have been non-resync activity. This will only
8083 * happen once though. 'last_events' will soon reflect
8084 * the state where there is little or no outstanding
8085 * resync requests, and further resync activity will
8086 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08008087 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008088 */
NeilBrowneea1bf32009-03-31 14:27:02 +11008089 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090 rdev->last_events = curr_events;
8091 idle = 0;
8092 }
8093 }
NeilBrown4b809912008-07-21 17:05:25 +10008094 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008095 return idle;
8096}
8097
NeilBrownfd01b882011-10-11 16:47:53 +11008098void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099{
8100 /* another "blocks" (512byte) blocks have been synced */
8101 atomic_sub(blocks, &mddev->recovery_active);
8102 wake_up(&mddev->recovery_wait);
8103 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07008104 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08008105 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008106 md_wakeup_thread(mddev->thread);
8107 // stop recovery, signal do_sync ....
8108 }
8109}
NeilBrown6c144d32014-09-30 16:15:38 +10008110EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008111
NeilBrown06d91a52005-06-21 17:17:12 -07008112/* md_write_start(mddev, bi)
8113 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07008114 * in superblock) before writing, schedule a superblock update
8115 * and wait for it to complete.
NeilBrowncc27b0c2017-06-05 16:49:39 +10008116 * A return value of 'false' means that the write wasn't recorded
8117 * and cannot proceed as the array is being suspend.
NeilBrown06d91a52005-06-21 17:17:12 -07008118 */
NeilBrowncc27b0c2017-06-05 16:49:39 +10008119bool md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008120{
Neil Brown0fd62b82008-06-28 08:31:36 +10008121 int did_change = 0;
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008122
NeilBrown06d91a52005-06-21 17:17:12 -07008123 if (bio_data_dir(bi) != WRITE)
NeilBrowncc27b0c2017-06-05 16:49:39 +10008124 return true;
NeilBrown06d91a52005-06-21 17:17:12 -07008125
NeilBrownf91de922005-11-08 21:39:36 -08008126 BUG_ON(mddev->ro == 1);
8127 if (mddev->ro == 2) {
8128 /* need to switch to read/write */
8129 mddev->ro = 0;
8130 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8131 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08008132 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008133 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08008134 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008135 rcu_read_lock();
8136 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11008137 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07008138 if (mddev->safemode == 1)
8139 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11008140 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
NeilBrown81fe48e2017-08-08 16:56:36 +10008141 if (mddev->in_sync || mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11008142 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07008143 if (mddev->in_sync) {
8144 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008145 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8146 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07008147 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008148 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07008149 }
NeilBrown85572d72014-12-15 12:56:56 +11008150 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07008151 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008152 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10008153 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008154 sysfs_notify_dirent_safe(mddev->sysfs_state);
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008155 if (!mddev->has_superblocks)
8156 return true;
NeilBrown09a44cc2008-05-23 13:04:36 -07008157 wait_event(mddev->sb_wait,
NeilBrownd47c8ad2017-10-05 16:23:16 +11008158 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8159 mddev->suspended);
NeilBrowncc27b0c2017-06-05 16:49:39 +10008160 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8161 percpu_ref_put(&mddev->writes_pending);
8162 return false;
8163 }
8164 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165}
NeilBrown6c144d32014-09-30 16:15:38 +10008166EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008167
NeilBrown49728052017-03-15 14:05:12 +11008168/* md_write_inc can only be called when md_write_start() has
8169 * already been called at least once of the current request.
8170 * It increments the counter and is useful when a single request
8171 * is split into several parts. Each part causes an increment and
8172 * so needs a matching md_write_end().
8173 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8174 * a spinlocked region.
8175 */
8176void md_write_inc(struct mddev *mddev, struct bio *bi)
8177{
8178 if (bio_data_dir(bi) != WRITE)
8179 return;
8180 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008181 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008182}
8183EXPORT_SYMBOL(md_write_inc);
8184
NeilBrownfd01b882011-10-11 16:47:53 +11008185void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008186{
NeilBrown4ad23a972017-03-15 14:05:14 +11008187 percpu_ref_put(&mddev->writes_pending);
8188
8189 if (mddev->safemode == 2)
8190 md_wakeup_thread(mddev->thread);
8191 else if (mddev->safemode_delay)
8192 /* The roundup() ensures this only performs locking once
8193 * every ->safemode_delay jiffies
8194 */
8195 mod_timer(&mddev->safemode_timer,
8196 roundup(jiffies, mddev->safemode_delay) +
8197 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008198}
NeilBrown4ad23a972017-03-15 14:05:14 +11008199
NeilBrown6c144d32014-09-30 16:15:38 +10008200EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201
NeilBrown2a2275d2007-01-26 00:57:11 -08008202/* md_allow_write(mddev)
8203 * Calling this ensures that the array is marked 'active' so that writes
8204 * may proceed without blocking. It is important to call this before
8205 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8206 * Must be called with mddev_lock held.
8207 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008208void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008209{
8210 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008211 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008212 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008213 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008214 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008215 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008216
NeilBrown85572d72014-12-15 12:56:56 +11008217 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008218 if (mddev->in_sync) {
8219 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008220 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8221 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008222 if (mddev->safemode_delay &&
8223 mddev->safemode == 0)
8224 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008225 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008226 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008227 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008228 /* wait for the dirty state to be recorded in the metadata */
8229 wait_event(mddev->sb_wait,
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008230 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008231 } else
NeilBrown85572d72014-12-15 12:56:56 +11008232 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008233}
8234EXPORT_SYMBOL_GPL(md_allow_write);
8235
Linus Torvalds1da177e2005-04-16 15:20:36 -07008236#define SYNC_MARKS 10
8237#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008238#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008239void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240{
Shaohua Li4ed87312012-10-11 13:34:00 +11008241 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008242 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008243 unsigned int currspeed = 0,
8244 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008245 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008246 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008247 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248 sector_t mark_cnt[SYNC_MARKS];
8249 int last_mark,m;
8250 struct list_head *tmp;
8251 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008252 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008253 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008254 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008255 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008256 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008257
8258 /* just incase thread restarts... */
Song Liud5d885f2017-11-19 22:17:01 -08008259 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8260 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008261 return;
NeilBrown3991b312014-05-28 13:39:23 +10008262 if (mddev->ro) {/* never try to sync a read-only array */
8263 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008264 return;
NeilBrown3991b312014-05-28 13:39:23 +10008265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008266
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008267 if (mddev_is_clustered(mddev)) {
8268 ret = md_cluster_ops->resync_start(mddev);
8269 if (ret)
8270 goto skip;
8271
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008272 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008273 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8274 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8275 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8276 && ((unsigned long long)mddev->curr_resync_completed
8277 < (unsigned long long)mddev->resync_max_sectors))
8278 goto skip;
8279 }
8280
NeilBrown61df9d92006-10-03 01:15:57 -07008281 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008282 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008283 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008284 action = "check";
8285 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008286 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008287 action = "repair";
8288 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008289 desc = "resync";
8290 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8291 desc = "reshape";
8292 else
8293 desc = "recovery";
8294
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008295 mddev->last_sync_action = action ?: desc;
8296
Linus Torvalds1da177e2005-04-16 15:20:36 -07008297 /* we overload curr_resync somewhat here.
8298 * 0 == not engaged in resync at all
8299 * 2 == checking that there is no conflict with another sync
8300 * 1 == like 2, but have yielded to allow conflicting resync to
8301 * commense
8302 * other == active in resync - this many blocks
8303 *
8304 * Before starting a resync we must have set curr_resync to
8305 * 2, and then checked that every "conflicting" array has curr_resync
8306 * less than ours. When we find one that is the same or higher
8307 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8308 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8309 * This will mean we have to start checking from the beginning again.
8310 *
8311 */
8312
8313 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008314 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008315 mddev->curr_resync = 2;
8316
8317 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008318 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008319 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008320 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008321 if (mddev2 == mddev)
8322 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008323 if (!mddev->parallel_resync
8324 && mddev2->curr_resync
8325 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008326 DEFINE_WAIT(wq);
8327 if (mddev < mddev2 && mddev->curr_resync == 2) {
8328 /* arbitrarily yield */
8329 mddev->curr_resync = 1;
8330 wake_up(&resync_wait);
8331 }
8332 if (mddev > mddev2 && mddev->curr_resync == 1)
8333 /* no need to wait here, we can wait the next
8334 * time 'round when curr_resync == 2
8335 */
8336 continue;
NeilBrown97441972008-09-19 11:49:54 +10008337 /* We need to wait 'interruptible' so as not to
8338 * contribute to the load average, and not to
8339 * be caught by 'softlockup'
8340 */
8341 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008342 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008343 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008344 if (mddev2_minor != mddev2->md_minor) {
8345 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008346 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8347 desc, mdname(mddev),
8348 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008350 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008351 if (signal_pending(current))
8352 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008353 schedule();
8354 finish_wait(&resync_wait, &wq);
8355 goto try_again;
8356 }
8357 finish_wait(&resync_wait, &wq);
8358 }
8359 }
8360 } while (mddev->curr_resync < 2);
8361
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008362 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008363 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008364 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008365 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008366 */
8367 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008368 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008369 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008370 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8371 j = mddev->resync_min;
8372 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008373 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008374
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008375 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
NeilBrownc804cde2012-05-21 09:28:33 +10008376 max_sectors = mddev->resync_max_sectors;
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008377 /*
8378 * If the original node aborts reshaping then we continue the
8379 * reshaping, so set j again to avoid restart reshape from the
8380 * first beginning
8381 */
8382 if (mddev_is_clustered(mddev) &&
8383 mddev->reshape_position != MaxSector)
8384 j = mddev->reshape_position;
8385 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008387 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008388 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008389 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008390 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008391 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008392 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008393 !test_bit(Faulty, &rdev->flags) &&
8394 !test_bit(In_sync, &rdev->flags) &&
8395 rdev->recovery_offset < j)
8396 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008397 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008398
8399 /* If there is a bitmap, we need to make sure all
8400 * writes that started before we added a spare
8401 * complete before we start doing a recovery.
8402 * Otherwise the write might complete and (via
8403 * bitmap_endwrite) set a bit in the bitmap after the
8404 * recovery has checked that bit and skipped that
8405 * region.
8406 */
8407 if (mddev->bitmap) {
8408 mddev->pers->quiesce(mddev, 1);
8409 mddev->pers->quiesce(mddev, 0);
8410 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008412
NeilBrown9d487392016-11-02 14:16:49 +11008413 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8414 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8415 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8416 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008417
NeilBrowneea1bf32009-03-31 14:27:02 +11008418 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008419
NeilBrown57afd892005-06-21 17:17:13 -07008420 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008421 for (m = 0; m < SYNC_MARKS; m++) {
8422 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008423 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008424 }
8425 last_mark = 0;
8426 mddev->resync_mark = mark[last_mark];
8427 mddev->resync_mark_cnt = mark_cnt[last_mark];
8428
8429 /*
8430 * Tune reconstruction:
8431 */
8432 window = 32*(PAGE_SIZE/512);
NeilBrown9d487392016-11-02 14:16:49 +11008433 pr_debug("md: using %dk window, over a total of %lluk.\n",
8434 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008435
8436 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008437 last_check = 0;
8438
8439 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008440 pr_debug("md: resuming %s of %s from checkpoint.\n",
8441 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008443 } else
8444 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008445 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008446 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8447 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008448 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008449
majianpeng7c2c57c2012-07-03 12:12:26 +10008450 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008452 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008453
NeilBrown57afd892005-06-21 17:17:13 -07008454 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008455
NeilBrown7a91ee12009-05-26 12:57:21 +10008456 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8457 ((mddev->curr_resync > mddev->curr_resync_completed &&
8458 (mddev->curr_resync - mddev->curr_resync_completed)
8459 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008460 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008461 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008462 >= mddev->resync_max - mddev->curr_resync_completed ||
8463 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008464 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008465 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008466 wait_event(mddev->recovery_wait,
8467 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008468 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008469 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8470 j > mddev->recovery_cp)
8471 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008472 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008473 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008474 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008475 }
NeilBrownacb180b2009-04-14 16:28:34 +10008476
NeilBrownc91abf52013-11-19 12:02:01 +11008477 while (j >= mddev->resync_max &&
8478 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008479 /* As this condition is controlled by user-space,
8480 * we can block indefinitely, so use '_interruptible'
8481 * to avoid triggering warnings.
8482 */
8483 flush_signals(current); /* just in case */
8484 wait_event_interruptible(mddev->recovery_wait,
8485 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008486 || test_bit(MD_RECOVERY_INTR,
8487 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008488 }
NeilBrownacb180b2009-04-14 16:28:34 +10008489
NeilBrownc91abf52013-11-19 12:02:01 +11008490 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8491 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008492
NeilBrown09314792015-02-19 16:04:40 +11008493 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008494 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008495 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008496 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008497 }
NeilBrown57afd892005-06-21 17:17:13 -07008498
8499 if (!skipped) { /* actual IO requested */
8500 io_sectors += sectors;
8501 atomic_add(sectors, &mddev->recovery_active);
8502 }
8503
NeilBrowne875ece2011-07-28 11:39:24 +10008504 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8505 break;
8506
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008508 if (j > max_sectors)
8509 /* when skipping, extra large numbers can be returned. */
8510 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008511 if (j > 2)
8512 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008513 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008514 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008515 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008516 * visible in /proc/mdstat
8517 */
8518 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008519
8520 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008521 continue;
8522
NeilBrown57afd892005-06-21 17:17:13 -07008523 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008524 repeat:
8525 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8526 /* step marks */
8527 int next = (last_mark+1) % SYNC_MARKS;
8528
8529 mddev->resync_mark = mark[next];
8530 mddev->resync_mark_cnt = mark_cnt[next];
8531 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008532 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533 last_mark = next;
8534 }
8535
NeilBrownc91abf52013-11-19 12:02:01 +11008536 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8537 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538
8539 /*
8540 * this loop exits only if either when we are slower than
8541 * the 'hard' speed limit, or the system was IO-idle for
8542 * a jiffy.
8543 * the system might be non-idle CPU-wise, but we only care
8544 * about not overloading the IO subsystem. (things like an
8545 * e2fsck being done on the RAID array should execute fast)
8546 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008547 cond_resched();
8548
Xiao Niac7e50a2014-08-07 09:37:41 -04008549 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8550 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008551 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008552
NeilBrown88202a02006-01-06 00:21:36 -08008553 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008554 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008555 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556 goto repeat;
8557 }
NeilBrownac8fa412015-02-19 16:55:00 +11008558 if (!is_mddev_idle(mddev, 0)) {
8559 /*
8560 * Give other IO more of a chance.
8561 * The faster the devices, the less we wait.
8562 */
8563 wait_event(mddev->recovery_wait,
8564 !atomic_read(&mddev->recovery_active));
8565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008566 }
8567 }
NeilBrown9d487392016-11-02 14:16:49 +11008568 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8569 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8570 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008571 /*
8572 * this also signals 'finished resyncing' to md_stop
8573 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008574 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008575 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8576
NeilBrown5ed1df22015-07-24 13:27:08 +10008577 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8578 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008579 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008580 mddev->curr_resync_completed = mddev->curr_resync;
8581 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8582 }
NeilBrown09314792015-02-19 16:04:40 +11008583 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008584
NeilBrowndfc70642008-05-23 13:04:39 -07008585 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008586 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008587 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8588 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8589 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008590 pr_debug("md: checkpointing %s of %s.\n",
8591 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008592 if (test_bit(MD_RECOVERY_ERROR,
8593 &mddev->recovery))
8594 mddev->recovery_cp =
8595 mddev->curr_resync_completed;
8596 else
8597 mddev->recovery_cp =
8598 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008599 }
8600 } else
8601 mddev->recovery_cp = MaxSector;
8602 } else {
8603 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8604 mddev->curr_resync = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11008605 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8606 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
8607 rcu_read_lock();
8608 rdev_for_each_rcu(rdev, mddev)
8609 if (rdev->raid_disk >= 0 &&
8610 mddev->delta_disks >= 0 &&
8611 !test_bit(Journal, &rdev->flags) &&
8612 !test_bit(Faulty, &rdev->flags) &&
8613 !test_bit(In_sync, &rdev->flags) &&
8614 rdev->recovery_offset < mddev->curr_resync)
8615 rdev->recovery_offset = mddev->curr_resync;
8616 rcu_read_unlock();
8617 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008619 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008620 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008621 /* set CHANGE_PENDING here since maybe another update is needed,
8622 * so other nodes are informed. It should be harmless for normal
8623 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008624 set_mask_bits(&mddev->sb_flags, 0,
8625 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008626
BingJing Chang88763912018-02-22 13:34:46 +08008627 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8628 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8629 mddev->delta_disks > 0 &&
8630 mddev->pers->finish_reshape &&
8631 mddev->pers->size &&
8632 mddev->queue) {
8633 mddev_lock_nointr(mddev);
8634 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
8635 mddev_unlock(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008636 if (!mddev_is_clustered(mddev)) {
8637 set_capacity(mddev->gendisk, mddev->array_sectors);
8638 revalidate_disk(mddev->gendisk);
8639 }
BingJing Chang88763912018-02-22 13:34:46 +08008640 }
8641
NeilBrown23da4222014-12-15 12:57:01 +11008642 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008643 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8644 /* We completed so min/max setting can be forgotten if used. */
8645 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8646 mddev->resync_min = 0;
8647 mddev->resync_max = MaxSector;
8648 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8649 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008650 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008651 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008652 spin_unlock(&mddev->lock);
8653
Linus Torvalds1da177e2005-04-16 15:20:36 -07008654 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008655 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008656 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008657}
NeilBrown29269552006-03-27 01:18:10 -08008658EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008659
NeilBrown746d3202013-04-24 11:42:41 +10008660static int remove_and_add_spares(struct mddev *mddev,
8661 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008662{
NeilBrown3cb03002011-10-11 16:45:26 +11008663 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008664 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008665 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008666 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008667
NeilBrown39772f02018-02-03 09:19:30 +11008668 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
8669 /* Mustn't remove devices when resync thread is running */
8670 return 0;
8671
NeilBrownd787be42016-06-02 16:19:53 +10008672 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008673 if ((this == NULL || rdev == this) &&
8674 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008675 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008676 test_bit(Faulty, &rdev->flags) &&
8677 atomic_read(&rdev->nr_pending)==0) {
8678 /* Faulty non-Blocked devices with nr_pending == 0
8679 * never get nr_pending incremented,
8680 * never get Faulty cleared, and never get Blocked set.
8681 * So we can synchronize_rcu now rather than once per device
8682 */
8683 remove_some = true;
8684 set_bit(RemoveSynchronized, &rdev->flags);
8685 }
8686 }
8687
8688 if (remove_some)
8689 synchronize_rcu();
8690 rdev_for_each(rdev, mddev) {
8691 if ((this == NULL || rdev == this) &&
8692 rdev->raid_disk >= 0 &&
8693 !test_bit(Blocked, &rdev->flags) &&
8694 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008695 (!test_bit(In_sync, &rdev->flags) &&
8696 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008697 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008698 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008699 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008700 sysfs_unlink_rdev(mddev, rdev);
NeilBrown011abdc2018-04-26 14:46:29 +10008701 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008702 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008703 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008704 }
8705 }
NeilBrownd787be42016-06-02 16:19:53 +10008706 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8707 clear_bit(RemoveSynchronized, &rdev->flags);
8708 }
8709
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008710 if (removed && mddev->kobj.sd)
8711 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008712
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008713 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008714 goto no_add;
8715
NeilBrowndafb20f2012-03-19 12:46:39 +11008716 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008717 if (this && this != rdev)
8718 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008719 if (test_bit(Candidate, &rdev->flags))
8720 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008721 if (rdev->raid_disk >= 0 &&
8722 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008723 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008724 !test_bit(Faulty, &rdev->flags))
8725 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008726 if (rdev->raid_disk >= 0)
8727 continue;
8728 if (test_bit(Faulty, &rdev->flags))
8729 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008730 if (!test_bit(Journal, &rdev->flags)) {
8731 if (mddev->ro &&
8732 ! (rdev->saved_raid_disk >= 0 &&
8733 !test_bit(Bitmap_sync, &rdev->flags)))
8734 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008735
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008736 rdev->recovery_offset = 0;
8737 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008738 if (mddev->pers->
8739 hot_add_disk(mddev, rdev) == 0) {
8740 if (sysfs_link_rdev(mddev, rdev))
8741 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008742 if (!test_bit(Journal, &rdev->flags))
8743 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008744 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08008745 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008746 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008747 }
NeilBrown746d3202013-04-24 11:42:41 +10008748no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008749 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08008750 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008751 return spares;
8752}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008753
NeilBrownac05f252014-09-30 08:10:42 +10008754static void md_start_sync(struct work_struct *ws)
8755{
8756 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008757
NeilBrownac05f252014-09-30 08:10:42 +10008758 mddev->sync_thread = md_register_thread(md_do_sync,
8759 mddev,
8760 "resync");
8761 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11008762 pr_warn("%s: could not start resync thread...\n",
8763 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008764 /* leave the spares where they are, it shouldn't hurt */
8765 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8766 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8767 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8768 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8769 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008770 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008771 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8772 &mddev->recovery))
8773 if (mddev->sysfs_action)
8774 sysfs_notify_dirent_safe(mddev->sysfs_action);
8775 } else
8776 md_wakeup_thread(mddev->sync_thread);
8777 sysfs_notify_dirent_safe(mddev->sysfs_action);
8778 md_new_event(mddev);
8779}
8780
Linus Torvalds1da177e2005-04-16 15:20:36 -07008781/*
8782 * This routine is regularly called by all per-raid-array threads to
8783 * deal with generic issues like resync and super-block update.
8784 * Raid personalities that don't have a thread (linear/raid0) do not
8785 * need this as they never do any recovery or update the superblock.
8786 *
8787 * It does not do any resync itself, but rather "forks" off other threads
8788 * to do that as needed.
8789 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8790 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008791 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008792 * and wakeups up this thread which will reap the thread and finish up.
8793 * This thread also removes any faulty devices (with nr_pending == 0).
8794 *
8795 * The overall approach is:
8796 * 1/ if the superblock needs updating, update it.
8797 * 2/ If a recovery thread is running, don't do anything else.
8798 * 3/ If recovery has finished, clean up, possibly marking spares active.
8799 * 4/ If there are any faulty devices, remove them.
8800 * 5/ If array is degraded, try to add spares devices
8801 * 6/ If array has spares or is not in-sync, start a resync thread.
8802 */
NeilBrownfd01b882011-10-11 16:47:53 +11008803void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008804{
NeilBrown059421e2018-10-03 15:04:41 +10008805 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
8806 /* Write superblock - thread that called mddev_suspend()
8807 * holds reconfig_mutex for us.
8808 */
8809 set_bit(MD_UPDATING_SB, &mddev->flags);
8810 smp_mb__after_atomic();
8811 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
8812 md_update_sb(mddev, 0);
8813 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
8814 wake_up(&mddev->sb_wait);
8815 }
8816
Jonathan Brassow68866e42011-06-08 15:10:08 +10008817 if (mddev->suspended)
8818 return;
8819
NeilBrown5f404022005-06-21 17:17:16 -07008820 if (mddev->bitmap)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008821 md_bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008822
NeilBrownfca4d842005-06-21 17:17:11 -07008823 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008824 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11008825 pr_debug("md: %s in immediate safe mode\n",
8826 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07008827 mddev->safemode = 2;
8828 }
8829 flush_signals(current);
8830 }
8831
NeilBrownc89a8ee2008-08-05 15:54:13 +10008832 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8833 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008834 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08008835 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008836 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008837 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008838 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11008839 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07008840 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008841 ))
8842 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008843
NeilBrowndf5b89b2006-03-27 01:18:20 -08008844 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008845 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008846
Shaohua Liafc1f552017-08-11 20:34:45 -07008847 if (!mddev->external && mddev->safemode == 1)
NeilBrown33182d12017-08-08 16:56:36 +10008848 mddev->safemode = 0;
8849
NeilBrownc89a8ee2008-08-05 15:54:13 +10008850 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008851 struct md_rdev *rdev;
8852 if (!mddev->external && mddev->in_sync)
8853 /* 'Blocked' flag not needed as failed devices
8854 * will be recorded if array switched to read/write.
8855 * Leaving it set will prevent the device
8856 * from being removed.
8857 */
8858 rdev_for_each(rdev, mddev)
8859 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008860 /* On a read-only array we can:
8861 * - remove failed devices
8862 * - add already-in_sync devices if the array itself
8863 * is in-sync.
8864 * As we only add devices that are already in-sync,
8865 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008866 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008867 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008868 /* There is no thread, but we need to call
8869 * ->spare_active and clear saved_raid_disk
8870 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008871 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008872 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008873 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008874 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08008875 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008876 goto unlock;
8877 }
8878
Guoqing Jiang659b2542015-12-21 10:50:59 +11008879 if (mddev_is_clustered(mddev)) {
8880 struct md_rdev *rdev;
8881 /* kick the device if another node issued a
8882 * remove disk.
8883 */
8884 rdev_for_each(rdev, mddev) {
8885 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8886 rdev->raid_disk < 0)
8887 md_kick_rdev_from_array(rdev);
8888 }
8889 }
8890
NeilBrown6497709b2017-03-15 14:05:14 +11008891 if (!mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11008892 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11008893 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11008894 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07008895 }
NeilBrownfca4d842005-06-21 17:17:11 -07008896
Shaohua Li29530792016-12-08 15:48:19 -08008897 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07008898 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008899
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8901 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8902 /* resync/recovery still happening */
8903 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8904 goto unlock;
8905 }
8906 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008907 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008908 goto unlock;
8909 }
Neil Brown72a23c22008-06-28 08:31:41 +10008910 /* Set RUNNING before clearing NEEDED to avoid
8911 * any transients in the value of "sync_action".
8912 */
NeilBrown72f36d52012-10-11 14:25:57 +11008913 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008914 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008915 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008916 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008917 /* Clear some bits that don't mean anything, but
8918 * might be left set
8919 */
NeilBrown24dd4692005-11-08 21:39:26 -08008920 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8921 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008922
NeilBrowned209582012-04-24 10:23:14 +10008923 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8924 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008925 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008926 /* no recovery is running.
8927 * remove any failed drives, then
8928 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008929 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008930 * the personality to fail the re-add.
8931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008932
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008933 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008934 if (mddev->pers->check_reshape == NULL ||
8935 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008936 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008937 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008938 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008939 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008940 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008941 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8942 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008943 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008944 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008945 } else if (mddev->recovery_cp < MaxSector) {
8946 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008947 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008948 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8949 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008950 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008951
Linus Torvalds1da177e2005-04-16 15:20:36 -07008952 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008953 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008954 /* We are adding a device or devices to an array
8955 * which has the bitmap stored on all devices.
8956 * So make sure all bitmap pages get written
8957 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008958 md_bitmap_write_all(mddev->bitmap);
NeilBrowna654b9d82005-06-21 17:17:27 -07008959 }
NeilBrownac05f252014-09-30 08:10:42 +10008960 INIT_WORK(&mddev->del_work, md_start_sync);
8961 queue_work(md_misc_wq, &mddev->del_work);
8962 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008963 }
NeilBrownac05f252014-09-30 08:10:42 +10008964 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008965 if (!mddev->sync_thread) {
8966 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008967 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008968 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8969 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008970 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008971 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008972 }
NeilBrownac05f252014-09-30 08:10:42 +10008973 unlock:
8974 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008975 mddev_unlock(mddev);
8976 }
8977}
NeilBrown6c144d32014-09-30 16:15:38 +10008978EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008979
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008980void md_reap_sync_thread(struct mddev *mddev)
8981{
8982 struct md_rdev *rdev;
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008983 sector_t old_dev_sectors = mddev->dev_sectors;
8984 bool is_reshaped = false;
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008985
8986 /* resync has finished, collect result */
8987 md_unregister_thread(&mddev->sync_thread);
8988 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8989 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8990 /* success...*/
8991 /* activate any spares */
8992 if (mddev->pers->spare_active(mddev)) {
8993 sysfs_notify(&mddev->kobj, NULL,
8994 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08008995 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008996 }
8997 }
8998 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008999 mddev->pers->finish_reshape) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009000 mddev->pers->finish_reshape(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009001 if (mddev_is_clustered(mddev))
9002 is_reshaped = true;
9003 }
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009004
9005 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11009006 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009007 */
NeilBrownf4667222013-12-09 12:04:56 +11009008 if (!mddev->degraded)
9009 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009010 rdev->saved_raid_disk = -1;
9011
9012 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08009013 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04009014 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9015 * clustered raid */
9016 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9017 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009018 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10009019 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009020 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9021 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9022 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9023 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009024 /*
9025 * We call md_cluster_ops->update_size here because sync_size could
9026 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9027 * so it is time to update size across cluster.
9028 */
9029 if (mddev_is_clustered(mddev) && is_reshaped
9030 && !test_bit(MD_CLOSING, &mddev->flags))
9031 md_cluster_ops->update_size(mddev, old_dev_sectors);
NeilBrownf851b602014-12-11 10:02:10 +11009032 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009033 /* flag recovery needed just to double check */
9034 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9035 sysfs_notify_dirent_safe(mddev->sysfs_action);
9036 md_new_event(mddev);
9037 if (mddev->event_work.func)
9038 queue_work(md_misc_wq, &mddev->event_work);
9039}
NeilBrown6c144d32014-09-30 16:15:38 +10009040EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009041
NeilBrownfd01b882011-10-11 16:47:53 +11009042void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07009043{
NeilBrown00bcb4a2010-06-01 19:37:23 +10009044 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07009045 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10009046 !test_bit(Blocked, &rdev->flags) &&
9047 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07009048 msecs_to_jiffies(5000));
9049 rdev_dec_pending(rdev, mddev);
9050}
9051EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9052
NeilBrownc6563a82012-05-21 09:27:00 +10009053void md_finish_reshape(struct mddev *mddev)
9054{
9055 /* called be personality module when reshape completes. */
9056 struct md_rdev *rdev;
9057
9058 rdev_for_each(rdev, mddev) {
9059 if (rdev->data_offset > rdev->new_data_offset)
9060 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9061 else
9062 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9063 rdev->data_offset = rdev->new_data_offset;
9064 }
9065}
9066EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10009067
Vishal Vermafc974ee2015-12-24 19:20:34 -07009068/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10009069
Vishal Vermafc974ee2015-12-24 19:20:34 -07009070/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11009071int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10009072 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009073{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04009074 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10009075 int rv;
9076 if (is_new)
9077 s += rdev->new_data_offset;
9078 else
9079 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07009080 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9081 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10009082 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009083 if (test_bit(ExternalBbl, &rdev->flags))
9084 sysfs_notify(&rdev->kobj, NULL,
9085 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11009086 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08009087 set_mask_bits(&mddev->sb_flags, 0,
9088 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10009089 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07009090 return 1;
9091 } else
9092 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10009093}
9094EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9095
NeilBrownc6563a82012-05-21 09:27:00 +10009096int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9097 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009098{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009099 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10009100 if (is_new)
9101 s += rdev->new_data_offset;
9102 else
9103 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009104 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9105 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
9106 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
9107 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10009108}
9109EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9110
Adrian Bunk75c96f82005-05-05 16:16:09 -07009111static int md_notify_reboot(struct notifier_block *this,
9112 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009113{
9114 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009115 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009116 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009117
NeilBrownc744a652012-03-19 12:46:37 +11009118 for_each_mddev(mddev, tmp) {
9119 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009120 if (mddev->pers)
9121 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009122 if (mddev->persistent)
9123 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009124 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009125 }
NeilBrownc744a652012-03-19 12:46:37 +11009126 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009127 }
NeilBrownc744a652012-03-19 12:46:37 +11009128 /*
9129 * certain more exotic SCSI devices are known to be
9130 * volatile wrt too early system reboots. While the
9131 * right place to handle this issue is the given
9132 * driver, we do want to have a safe RAID driver ...
9133 */
9134 if (need_delay)
9135 mdelay(1000*1);
9136
Linus Torvalds1da177e2005-04-16 15:20:36 -07009137 return NOTIFY_DONE;
9138}
9139
Adrian Bunk75c96f82005-05-05 16:16:09 -07009140static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009141 .notifier_call = md_notify_reboot,
9142 .next = NULL,
9143 .priority = INT_MAX, /* before any real devices */
9144};
9145
9146static void md_geninit(void)
9147{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009148 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009149
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07009150 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009151}
9152
Adrian Bunk75c96f82005-05-05 16:16:09 -07009153static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009154{
Tejun Heoe804ac72010-10-15 15:36:08 +02009155 int ret = -ENOMEM;
9156
Tejun Heoada609e2011-01-25 14:35:54 +01009157 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009158 if (!md_wq)
9159 goto err_wq;
9160
9161 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9162 if (!md_misc_wq)
9163 goto err_misc_wq;
9164
9165 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9166 goto err_md;
9167
9168 if ((ret = register_blkdev(0, "mdp")) < 0)
9169 goto err_mdp;
9170 mdp_major = ret;
9171
NeilBrownaf5628f2014-07-31 13:54:54 +10009172 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009173 md_probe, NULL, NULL);
9174 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009175 md_probe, NULL, NULL);
9176
Linus Torvalds1da177e2005-04-16 15:20:36 -07009177 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009178 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009179
9180 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009182
Tejun Heoe804ac72010-10-15 15:36:08 +02009183err_mdp:
9184 unregister_blkdev(MD_MAJOR, "md");
9185err_md:
9186 destroy_workqueue(md_misc_wq);
9187err_misc_wq:
9188 destroy_workqueue(md_wq);
9189err_wq:
9190 return ret;
9191}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009192
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009193static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009194{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009195 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9196 struct md_rdev *rdev2;
9197 int role, ret;
9198 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009199
Guoqing Jiang818da592017-03-01 16:42:40 +08009200 /*
9201 * If size is changed in another node then we need to
9202 * do resize as well.
9203 */
9204 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9205 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9206 if (ret)
9207 pr_info("md-cluster: resize failed\n");
9208 else
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009209 md_bitmap_update_sb(mddev->bitmap);
Guoqing Jiang818da592017-03-01 16:42:40 +08009210 }
9211
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009212 /* Check for change of roles in the active devices */
9213 rdev_for_each(rdev2, mddev) {
9214 if (test_bit(Faulty, &rdev2->flags))
9215 continue;
9216
9217 /* Check if the roles changed */
9218 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009219
9220 if (test_bit(Candidate, &rdev2->flags)) {
9221 if (role == 0xfffe) {
9222 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9223 md_kick_rdev_from_array(rdev2);
9224 continue;
9225 }
9226 else
9227 clear_bit(Candidate, &rdev2->flags);
9228 }
9229
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009230 if (role != rdev2->raid_disk) {
Guoqing Jiangca1e98e2018-10-18 16:37:45 +08009231 /*
9232 * got activated except reshape is happening.
9233 */
9234 if (rdev2->raid_disk == -1 && role != 0xffff &&
9235 !(le32_to_cpu(sb->feature_map) &
9236 MD_FEATURE_RESHAPE_ACTIVE)) {
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009237 rdev2->saved_raid_disk = role;
9238 ret = remove_and_add_spares(mddev, rdev2);
9239 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009240 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009241 /* wakeup mddev->thread here, so array could
9242 * perform resync with the new activated disk */
9243 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9244 md_wakeup_thread(mddev->thread);
9245
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009246 }
9247 /* device faulty
9248 * We just want to do the minimum to mark the disk
9249 * as faulty. The recovery is performed by the
9250 * one who initiated the error.
9251 */
9252 if ((role == 0xfffe) || (role == 0xfffd)) {
9253 md_error(mddev, rdev2);
9254 clear_bit(Blocked, &rdev2->flags);
9255 }
9256 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009257 }
9258
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009259 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9260 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009261
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009262 /*
9263 * Since mddev->delta_disks has already updated in update_raid_disks,
9264 * so it is time to check reshape.
9265 */
9266 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9267 (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9268 /*
9269 * reshape is happening in the remote node, we need to
9270 * update reshape_position and call start_reshape.
9271 */
9272 mddev->reshape_position = sb->reshape_position;
9273 if (mddev->pers->update_reshape_pos)
9274 mddev->pers->update_reshape_pos(mddev);
9275 if (mddev->pers->start_reshape)
9276 mddev->pers->start_reshape(mddev);
9277 } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9278 mddev->reshape_position != MaxSector &&
9279 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9280 /* reshape is just done in another node. */
9281 mddev->reshape_position = MaxSector;
9282 if (mddev->pers->update_reshape_pos)
9283 mddev->pers->update_reshape_pos(mddev);
9284 }
9285
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009286 /* Finally set the event to be up to date */
9287 mddev->events = le64_to_cpu(sb->events);
9288}
9289
9290static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9291{
9292 int err;
9293 struct page *swapout = rdev->sb_page;
9294 struct mdp_superblock_1 *sb;
9295
9296 /* Store the sb page of the rdev in the swapout temporary
9297 * variable in case we err in the future
9298 */
9299 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009300 err = alloc_disk_sb(rdev);
9301 if (err == 0) {
9302 ClearPageUptodate(rdev->sb_page);
9303 rdev->sb_loaded = 0;
9304 err = super_types[mddev->major_version].
9305 load_super(rdev, NULL, mddev->minor_version);
9306 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009307 if (err < 0) {
9308 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9309 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009310 if (rdev->sb_page)
9311 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009312 rdev->sb_page = swapout;
9313 rdev->sb_loaded = 1;
9314 return err;
9315 }
9316
9317 sb = page_address(rdev->sb_page);
9318 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9319 * is not set
9320 */
9321
9322 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9323 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9324
9325 /* The other node finished recovery, call spare_active to set
9326 * device In_sync and mddev->degraded
9327 */
9328 if (rdev->recovery_offset == MaxSector &&
9329 !test_bit(In_sync, &rdev->flags) &&
9330 mddev->pers->spare_active(mddev))
9331 sysfs_notify(&mddev->kobj, NULL, "degraded");
9332
9333 put_page(swapout);
9334 return 0;
9335}
9336
9337void md_reload_sb(struct mddev *mddev, int nr)
9338{
9339 struct md_rdev *rdev;
9340 int err;
9341
9342 /* Find the rdev */
9343 rdev_for_each_rcu(rdev, mddev) {
9344 if (rdev->desc_nr == nr)
9345 break;
9346 }
9347
9348 if (!rdev || rdev->desc_nr != nr) {
9349 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9350 return;
9351 }
9352
9353 err = read_rdev(mddev, rdev);
9354 if (err < 0)
9355 return;
9356
9357 check_sb_changes(mddev, rdev);
9358
9359 /* Read all rdev's to update recovery_offset */
Guoqing Jiang0ea99242018-04-09 17:01:21 +08009360 rdev_for_each_rcu(rdev, mddev) {
9361 if (!test_bit(Faulty, &rdev->flags))
9362 read_rdev(mddev, rdev);
9363 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009364}
9365EXPORT_SYMBOL(md_reload_sb);
9366
Linus Torvalds1da177e2005-04-16 15:20:36 -07009367#ifndef MODULE
9368
9369/*
9370 * Searches all registered partitions for autorun RAID arrays
9371 * at boot time.
9372 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009373
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009374static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009375static LIST_HEAD(all_detected_devices);
9376struct detected_devices_node {
9377 struct list_head list;
9378 dev_t dev;
9379};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009380
9381void md_autodetect_dev(dev_t dev)
9382{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009383 struct detected_devices_node *node_detected_dev;
9384
9385 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9386 if (node_detected_dev) {
9387 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009388 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009389 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009390 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009392}
9393
Linus Torvalds1da177e2005-04-16 15:20:36 -07009394static void autostart_arrays(int part)
9395{
NeilBrown3cb03002011-10-11 16:45:26 +11009396 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009397 struct detected_devices_node *node_detected_dev;
9398 dev_t dev;
9399 int i_scanned, i_passed;
9400
9401 i_scanned = 0;
9402 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009403
NeilBrown9d487392016-11-02 14:16:49 +11009404 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009405
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009406 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009407 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9408 i_scanned++;
9409 node_detected_dev = list_entry(all_detected_devices.next,
9410 struct detected_devices_node, list);
9411 list_del(&node_detected_dev->list);
9412 dev = node_detected_dev->dev;
9413 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009414 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009415 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009416 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009417 if (IS_ERR(rdev))
9418 continue;
9419
NeilBrown403df472014-09-30 15:52:29 +10009420 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009421 continue;
NeilBrown403df472014-09-30 15:52:29 +10009422
NeilBrownd0fae182008-03-04 14:29:31 -08009423 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009425 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009426 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009427 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009428
NeilBrown9d487392016-11-02 14:16:49 +11009429 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009430
9431 autorun_devices(part);
9432}
9433
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009434#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009435
9436static __exit void md_exit(void)
9437{
NeilBrownfd01b882011-10-11 16:47:53 +11009438 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009440 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009441
NeilBrownaf5628f2014-07-31 13:54:54 +10009442 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009443 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009444
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009445 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009446 unregister_blkdev(mdp_major, "mdp");
9447 unregister_reboot_notifier(&md_notifier);
9448 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009449
9450 /* We cannot unload the modules while some process is
9451 * waiting for us in select() or poll() - wake them up
9452 */
9453 md_unloading = 1;
9454 while (waitqueue_active(&md_event_waiters)) {
9455 /* not safe to leave yet */
9456 wake_up(&md_event_waiters);
9457 msleep(delay);
9458 delay += delay;
9459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009460 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009461
NeilBrown29ac4aa2008-02-06 01:39:58 -08009462 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009463 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009464 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009465 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009466 /*
9467 * for_each_mddev() will call mddev_put() at the end of each
9468 * iteration. As the mddev is now fully clear, this will
9469 * schedule the mddev for destruction by a workqueue, and the
9470 * destroy_workqueue() below will wait for that to complete.
9471 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009472 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009473 destroy_workqueue(md_misc_wq);
9474 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009475}
9476
Dan Williams685784a2007-07-09 11:56:42 -07009477subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009478module_exit(md_exit)
9479
Kees Cooke4dca7b2017-10-17 19:04:42 -07009480static int get_ro(char *buffer, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009481{
9482 return sprintf(buffer, "%d", start_readonly);
9483}
Kees Cooke4dca7b2017-10-17 19:04:42 -07009484static int set_ro(const char *val, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009485{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009486 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009487}
9488
NeilBrown80ca3a42006-07-10 04:44:18 -07009489module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9490module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009491module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009492module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009493
Linus Torvalds1da177e2005-04-16 15:20:36 -07009494MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009495MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009496MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009497MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);