blob: edad9f8442e67e4e2892eca779b3edb391e996b6 [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
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050087struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050088EXPORT_SYMBOL(md_cluster_ops);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050089struct module *md_cluster_mod;
90EXPORT_SYMBOL(md_cluster_mod);
91
Bernd Schubert90b08712008-05-23 13:04:38 -070092static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020093static struct workqueue_struct *md_wq;
94static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070095
NeilBrown746d3202013-04-24 11:42:41 +100096static int remove_and_add_spares(struct mddev *mddev,
97 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110098static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
Robert Becker1e509152009-12-14 12:49:58 +1100101 * Default number of read corrections we'll attempt on an rdev
102 * before ejecting it from the array. We divide the read error
103 * count by 2 for every hour elapsed between read errors.
104 */
105#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
106/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
108 * is 1000 KB/sec, so the extra system load does not show up that much.
109 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -0700110 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * subsystem is idle. There is also an 'absolute maximum' reconstruction
112 * speed limit - in case reconstruction slows down your system despite
113 * idle IO detection.
114 *
115 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800116 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118
119static int sysctl_speed_limit_min = 1000;
120static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100121static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800122{
123 return mddev->sync_speed_min ?
124 mddev->sync_speed_min : sysctl_speed_limit_min;
125}
126
NeilBrownfd01b882011-10-11 16:47:53 +1100127static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800128{
129 return mddev->sync_speed_max ?
130 mddev->sync_speed_max : sysctl_speed_limit_max;
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133static struct ctl_table_header *raid_table_header;
134
Joe Perches82592c32013-11-14 15:16:18 +1100135static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .procname = "speed_limit_min",
138 .data = &sysctl_speed_limit_min,
139 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700140 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800141 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 },
143 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .procname = "speed_limit_max",
145 .data = &sysctl_speed_limit_max,
146 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700147 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800148 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800150 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Joe Perches82592c32013-11-14 15:16:18 +1100153static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .procname = "raid",
156 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700157 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .child = raid_table,
159 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800160 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
Joe Perches82592c32013-11-14 15:16:18 +1100163static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 .procname = "dev",
166 .maxlen = 0,
167 .mode = 0555,
168 .child = raid_dir_table,
169 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800170 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700173static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
NeilBrownf91de922005-11-08 21:39:36 -0800175static int start_readonly;
176
NeilBrown78b63502017-04-12 16:26:13 +1000177/*
178 * The original mechanism for creating an md device is to create
179 * a device node in /dev and to open it. This causes races with device-close.
180 * The preferred method is to write to the "new_array" module parameter.
181 * This can avoid races.
182 * Setting create_on_open to false disables the original mechanism
183 * so all the races disappear.
184 */
185static bool create_on_open = true;
186
NeilBrowna167f662010-10-26 18:31:13 +1100187/* bio_clone_mddev
NeilBrown9b10f6a2017-06-18 14:38:59 +1000188 * like bio_clone_bioset, but with a local bio set
NeilBrowna167f662010-10-26 18:31:13 +1100189 */
190
NeilBrowna167f662010-10-26 18:31:13 +1100191struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100192 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100193{
194 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100195
196 if (!mddev || !mddev->bio_set)
197 return bio_alloc(gfp_mask, nr_iovecs);
198
Kent Overstreet395c72a2012-09-06 15:34:55 -0700199 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100200 if (!b)
201 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100202 return b;
203}
204EXPORT_SYMBOL_GPL(bio_alloc_mddev);
205
NeilBrown5a850712017-06-21 09:12:21 +1000206static struct bio *md_bio_alloc_sync(struct mddev *mddev)
207{
Shaohua Li7f053a6a2017-06-23 09:19:49 -0700208 if (!mddev || !mddev->sync_set)
NeilBrown5a850712017-06-21 09:12:21 +1000209 return bio_alloc(GFP_NOIO, 1);
210
211 return bio_alloc_bioset(GFP_NOIO, 1, mddev->sync_set);
212}
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
NeilBrownd7603b72006-01-06 00:20:30 -0800215 * We have a system wide 'event count' that is incremented
216 * on any 'interesting' event, and readers of /proc/mdstat
217 * can use 'poll' or 'select' to find out when the event
218 * count increases.
219 *
220 * Events are:
221 * start array, stop array, error, add device, remove device,
222 * start build, activate spare
223 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800224static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800225static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100226void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800227{
228 atomic_inc(&md_event_count);
229 wake_up(&md_event_waiters);
230}
NeilBrown29269552006-03-27 01:18:10 -0800231EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800232
233/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * Enables to iterate over all existing md arrays
235 * all_mddevs_lock protects this list.
236 */
237static LIST_HEAD(all_mddevs);
238static DEFINE_SPINLOCK(all_mddevs_lock);
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
241 * iterates through all used mddevs in the system.
242 * We take care to grab the all_mddevs_lock whenever navigating
243 * the list, and to always hold a refcount when unlocked.
244 * Any code which breaks out of this loop while own
245 * a reference to the current mddev and must mddev_put it.
246 */
NeilBrownfd01b882011-10-11 16:47:53 +1100247#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000249 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100250 _tmp = all_mddevs.next; \
251 _mddev = NULL;}); \
252 ({ if (_tmp != &all_mddevs) \
253 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100255 if (_mddev) mddev_put(_mddev); \
256 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
257 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100259 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 )
261
NeilBrown409c57f2009-03-31 14:39:39 +1100262/* Rather than calling directly into the personality make_request function,
263 * IO requests come here first so that we can check if the device is
264 * being suspended pending a reconfiguration.
265 * We hold a refcount over the call to ->make_request. By the time that
266 * call has finished, the bio has been linked into some internal structure
267 * and so is visible to ->quiesce(), so we don't need the refcount any more.
268 */
NeilBrownb3143b92017-10-17 13:46:43 +1100269static bool is_suspended(struct mddev *mddev, struct bio *bio)
270{
271 if (mddev->suspended)
272 return true;
273 if (bio_data_dir(bio) != WRITE)
274 return false;
275 if (mddev->suspend_lo >= mddev->suspend_hi)
276 return false;
277 if (bio->bi_iter.bi_sector >= mddev->suspend_hi)
278 return false;
279 if (bio_end_sector(bio) < mddev->suspend_lo)
280 return false;
281 return true;
282}
283
Shaohua Li393debc2017-09-21 10:23:35 -0700284void md_handle_request(struct mddev *mddev, struct bio *bio)
285{
286check_suspended:
287 rcu_read_lock();
NeilBrownb3143b92017-10-17 13:46:43 +1100288 if (is_suspended(mddev, bio)) {
Shaohua Li393debc2017-09-21 10:23:35 -0700289 DEFINE_WAIT(__wait);
290 for (;;) {
291 prepare_to_wait(&mddev->sb_wait, &__wait,
292 TASK_UNINTERRUPTIBLE);
NeilBrownb3143b92017-10-17 13:46:43 +1100293 if (!is_suspended(mddev, bio))
Shaohua Li393debc2017-09-21 10:23:35 -0700294 break;
295 rcu_read_unlock();
296 schedule();
297 rcu_read_lock();
298 }
299 finish_wait(&mddev->sb_wait, &__wait);
300 }
301 atomic_inc(&mddev->active_io);
302 rcu_read_unlock();
303
304 if (!mddev->pers->make_request(mddev, bio)) {
305 atomic_dec(&mddev->active_io);
306 wake_up(&mddev->sb_wait);
307 goto check_suspended;
308 }
309
310 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
311 wake_up(&mddev->sb_wait);
312}
313EXPORT_SYMBOL(md_handle_request);
314
Jens Axboedece1632015-11-05 10:41:16 -0700315static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
NeilBrown49077322010-03-25 16:20:56 +1100317 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100318 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500319 unsigned int sectors;
Gu Zheng74672d02015-04-03 08:44:47 +0800320 int cpu;
NeilBrown49077322010-03-25 16:20:56 +1100321
NeilBrownaf67c312017-06-18 14:38:57 +1000322 blk_queue_split(q, &bio);
Kent Overstreet54efd502015-04-23 22:37:18 -0700323
NeilBrown274d8cb2016-01-04 16:16:58 +1100324 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100325 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700326 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100327 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100328 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200329 if (bio_sectors(bio) != 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200330 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200331 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700332 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100333 }
NeilBrown49077322010-03-25 16:20:56 +1100334
Chris Masone91ece52011-02-07 19:21:48 -0500335 /*
336 * save the sectors now since our bio can
337 * go away inside make_request
338 */
339 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700340 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600341 bio->bi_opf &= ~REQ_NOMERGE;
Shaohua Li393debc2017-09-21 10:23:35 -0700342
343 md_handle_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100344
Gu Zheng74672d02015-04-03 08:44:47 +0800345 cpu = part_stat_lock();
346 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
347 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
348 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100349
Jens Axboedece1632015-11-05 10:41:16 -0700350 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
NeilBrown9e35b992010-04-06 14:23:02 +1000353/* mddev_suspend makes sure no new requests are submitted
354 * to the device, and that any requests that have been submitted
355 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100356 * Once mddev_detach() is called and completes, the module will be
357 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000358 */
NeilBrownfd01b882011-10-11 16:47:53 +1100359void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100360{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200361 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
NeilBrown4d5324f2017-10-19 12:17:16 +1100362 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100363 if (mddev->suspended++)
364 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100365 synchronize_rcu();
NeilBrowncc27b0c2017-06-05 16:49:39 +1000366 wake_up(&mddev->sb_wait);
NeilBrown35bfc522017-10-17 13:46:43 +1100367 set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
368 smp_mb__after_atomic();
NeilBrown409c57f2009-03-31 14:39:39 +1100369 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
370 mddev->pers->quiesce(mddev, 1);
NeilBrown35bfc522017-10-17 13:46:43 +1100371 clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
372 wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500373
374 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100375}
NeilBrown390ee602010-06-01 19:37:27 +1000376EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100377
NeilBrownfd01b882011-10-11 16:47:53 +1100378void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100379{
NeilBrown4d5324f2017-10-19 12:17:16 +1100380 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100381 if (--mddev->suspended)
382 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100383 wake_up(&mddev->sb_wait);
384 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500385
Jonathan Brassow47525e52012-05-22 13:55:29 +1000386 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500387 md_wakeup_thread(mddev->thread);
388 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100389}
NeilBrown390ee602010-06-01 19:37:27 +1000390EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100391
NeilBrownfd01b882011-10-11 16:47:53 +1100392int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000393{
NeilBrown5c675f82014-12-15 12:56:56 +1100394 struct md_personality *pers = mddev->pers;
395 int ret = 0;
396
397 rcu_read_lock();
398 if (mddev->suspended)
399 ret = 1;
400 else if (pers && pers->congested)
401 ret = pers->congested(mddev, bits);
402 rcu_read_unlock();
403 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000404}
NeilBrown5c675f82014-12-15 12:56:56 +1100405EXPORT_SYMBOL_GPL(mddev_congested);
406static int md_congested(void *data, int bits)
407{
408 struct mddev *mddev = data;
409 return mddev_congested(mddev, bits);
410}
NeilBrown3fa841d2009-09-23 18:10:29 +1000411
NeilBrowna2826aa2009-12-14 12:49:49 +1100412/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200413 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100414 */
415
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200416static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100417{
NeilBrown3cb03002011-10-11 16:45:26 +1100418 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100419 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100420
421 rdev_dec_pending(rdev, mddev);
422
423 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200424 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200425 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100426 }
427 bio_put(bio);
428}
429
NeilBrowna7a07e62010-12-09 16:04:25 +1100430static void md_submit_flush_data(struct work_struct *ws);
431
NeilBrowna035fc32010-12-09 16:17:51 +1100432static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100433{
NeilBrownfd01b882011-10-11 16:47:53 +1100434 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100435 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100436
NeilBrowna7a07e62010-12-09 16:04:25 +1100437 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
438 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100439 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100440 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100441 if (rdev->raid_disk >= 0 &&
442 !test_bit(Faulty, &rdev->flags)) {
443 /* Take two references, one is dropped
444 * when request finishes, one after
445 * we reclaim rcu_read_lock
446 */
447 struct bio *bi;
448 atomic_inc(&rdev->nr_pending);
449 atomic_inc(&rdev->nr_pending);
450 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000451 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200452 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100453 bi->bi_private = rdev;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200454 bio_set_dev(bi, rdev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600455 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
NeilBrowna2826aa2009-12-14 12:49:49 +1100456 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500457 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100458 rcu_read_lock();
459 rdev_dec_pending(rdev, mddev);
460 }
461 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100462 if (atomic_dec_and_test(&mddev->flush_pending))
463 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100464}
465
Tejun Heoe9c74692010-09-03 11:56:18 +0200466static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100467{
NeilBrownfd01b882011-10-11 16:47:53 +1100468 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200469 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100470
Shaohua Li79bf31a2017-09-21 09:55:28 -0700471 /*
472 * must reset flush_bio before calling into md_handle_request to avoid a
473 * deadlock, because other bios passed md_handle_request suspend check
474 * could wait for this and below md_handle_request could wait for those
475 * bios because of suspend check
476 */
477 mddev->flush_bio = NULL;
478 wake_up(&mddev->sb_wait);
479
Kent Overstreet4f024f32013-10-11 15:44:27 -0700480 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100481 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200482 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100483 else {
Jens Axboe1eff9d32016-08-05 15:35:16 -0600484 bio->bi_opf &= ~REQ_PREFLUSH;
Shaohua Li79bf31a2017-09-21 09:55:28 -0700485 md_handle_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100486 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100487}
488
NeilBrownfd01b882011-10-11 16:47:53 +1100489void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100490{
NeilBrown85572d72014-12-15 12:56:56 +1100491 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100492 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200493 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100494 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200495 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100496 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100497
NeilBrowna035fc32010-12-09 16:17:51 +1100498 INIT_WORK(&mddev->flush_work, submit_flushes);
499 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100500}
Tejun Heoe9c74692010-09-03 11:56:18 +0200501EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100502
NeilBrownfd01b882011-10-11 16:47:53 +1100503static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 atomic_inc(&mddev->active);
506 return mddev;
507}
508
Dan Williams5fd3a172009-03-04 00:57:25 -0700509static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100510
NeilBrownfd01b882011-10-11 16:47:53 +1100511static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Shaohua Li7184ef82017-07-03 14:34:23 -0700513 struct bio_set *bs = NULL, *sync_bs = NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
516 return;
NeilBrownd3374822009-01-09 08:31:10 +1100517 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100518 mddev->ctime == 0 && !mddev->hold_active) {
519 /* Array is not configured at all, and not held active,
520 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100521 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100522 bs = mddev->bio_set;
Shaohua Li7184ef82017-07-03 14:34:23 -0700523 sync_bs = mddev->sync_set;
NeilBrowna167f662010-10-26 18:31:13 +1100524 mddev->bio_set = NULL;
NeilBrown5a850712017-06-21 09:12:21 +1000525 mddev->sync_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100526 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200527 /* We did a probe so need to clean up. Call
528 * queue_work inside the spinlock so that
529 * flush_workqueue() after mddev_find will
530 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100531 */
532 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200533 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100534 } else
535 kfree(mddev);
536 }
537 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100538 if (bs)
539 bioset_free(bs);
Shaohua Li7184ef82017-07-03 14:34:23 -0700540 if (sync_bs)
541 bioset_free(sync_bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Kees Cook8376d3c2017-10-16 17:01:48 -0700544static void md_safemode_timeout(struct timer_list *t);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400545
NeilBrownfd01b882011-10-11 16:47:53 +1100546void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100547{
548 mutex_init(&mddev->open_mutex);
549 mutex_init(&mddev->reconfig_mutex);
550 mutex_init(&mddev->bitmap_info.mutex);
551 INIT_LIST_HEAD(&mddev->disks);
552 INIT_LIST_HEAD(&mddev->all_mddevs);
Kees Cook8376d3c2017-10-16 17:01:48 -0700553 timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100554 atomic_set(&mddev->active, 1);
555 atomic_set(&mddev->openers, 0);
556 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100557 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100558 atomic_set(&mddev->flush_pending, 0);
559 init_waitqueue_head(&mddev->sb_wait);
560 init_waitqueue_head(&mddev->recovery_wait);
561 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000562 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500563 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100564 mddev->resync_min = 0;
565 mddev->resync_max = MaxSector;
566 mddev->level = LEVEL_NONE;
567}
NeilBrown390ee602010-06-01 19:37:27 +1000568EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100569
NeilBrownf72ffdd2014-09-30 14:23:59 +1000570static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
NeilBrownfd01b882011-10-11 16:47:53 +1100572 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
NeilBrown8f5f02c2011-02-16 13:58:51 +1100574 if (unit && MAJOR(unit) != MD_MAJOR)
575 unit &= ~((1<<MdpMinorShift)-1);
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 retry:
578 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
NeilBrownefeb53c2009-01-09 08:31:10 +1100580 if (unit) {
581 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
582 if (mddev->unit == unit) {
583 mddev_get(mddev);
584 spin_unlock(&all_mddevs_lock);
585 kfree(new);
586 return mddev;
587 }
588
589 if (new) {
590 list_add(&new->all_mddevs, &all_mddevs);
591 spin_unlock(&all_mddevs_lock);
592 new->hold_active = UNTIL_IOCTL;
593 return new;
594 }
595 } else if (new) {
596 /* find an unused unit number */
597 static int next_minor = 512;
598 int start = next_minor;
599 int is_free = 0;
600 int dev = 0;
601 while (!is_free) {
602 dev = MKDEV(MD_MAJOR, next_minor);
603 next_minor++;
604 if (next_minor > MINORMASK)
605 next_minor = 0;
606 if (next_minor == start) {
607 /* Oh dear, all in use. */
608 spin_unlock(&all_mddevs_lock);
609 kfree(new);
610 return NULL;
611 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000612
NeilBrownefeb53c2009-01-09 08:31:10 +1100613 is_free = 1;
614 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
615 if (mddev->unit == dev) {
616 is_free = 0;
617 break;
618 }
619 }
620 new->unit = dev;
621 new->md_minor = MINOR(dev);
622 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 list_add(&new->all_mddevs, &all_mddevs);
624 spin_unlock(&all_mddevs_lock);
625 return new;
626 }
627 spin_unlock(&all_mddevs_lock);
628
NeilBrown9ffae0c2006-01-06 00:20:32 -0800629 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (!new)
631 return NULL;
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 new->unit = unit;
634 if (MAJOR(unit) == MD_MAJOR)
635 new->md_minor = MINOR(unit);
636 else
637 new->md_minor = MINOR(unit) >> MdpMinorShift;
638
NeilBrownfafd7fb2010-04-01 15:55:30 +1100639 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 goto retry;
642}
643
NeilBrownb6eb1272010-04-15 10:13:47 +1000644static struct attribute_group md_redundancy_group;
645
NeilBrown5c47daf2014-12-15 12:57:01 +1100646void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
NeilBrowna64c8762010-04-14 17:15:37 +1000648 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000649 /* These cannot be removed under reconfig_mutex as
650 * an access to the files will try to take reconfig_mutex
651 * while holding the file unremovable, which leads to
652 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000653 * So hold set sysfs_active while the remove in happeing,
654 * and anything else which might set ->to_remove or my
655 * otherwise change the sysfs namespace will fail with
656 * -EBUSY if sysfs_active is still set.
657 * We set sysfs_active under reconfig_mutex and elsewhere
658 * test it under the same mutex to ensure its correct value
659 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000660 */
NeilBrowna64c8762010-04-14 17:15:37 +1000661 struct attribute_group *to_remove = mddev->to_remove;
662 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000663 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000664 mutex_unlock(&mddev->reconfig_mutex);
665
NeilBrown00bcb4a2010-06-01 19:37:23 +1000666 if (mddev->kobj.sd) {
667 if (to_remove != &md_redundancy_group)
668 sysfs_remove_group(&mddev->kobj, to_remove);
669 if (mddev->pers == NULL ||
670 mddev->pers->sync_request == NULL) {
671 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
672 if (mddev->sysfs_action)
673 sysfs_put(mddev->sysfs_action);
674 mddev->sysfs_action = NULL;
675 }
NeilBrowna64c8762010-04-14 17:15:37 +1000676 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000677 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000678 } else
679 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Chris Dunlop751e67c2011-10-19 16:48:26 +1100681 /* As we've dropped the mutex we need a spinlock to
682 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000683 */
684 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700685 md_wakeup_thread(mddev->thread);
NeilBrown4d5324f2017-10-19 12:17:16 +1100686 wake_up(&mddev->sb_wait);
NeilBrown01f96c02011-09-21 15:30:20 +1000687 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
NeilBrown5c47daf2014-12-15 12:57:01 +1100689EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500691struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100692{
693 struct md_rdev *rdev;
694
695 rdev_for_each_rcu(rdev, mddev)
696 if (rdev->desc_nr == nr)
697 return rdev;
698
699 return NULL;
700}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500701EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100702
703static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
NeilBrown3cb03002011-10-11 16:45:26 +1100705 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
NeilBrowndafb20f2012-03-19 12:46:39 +1100707 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (rdev->bdev->bd_dev == dev)
709 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return NULL;
712}
713
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100714struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
NeilBrown1ca69c42012-10-11 13:37:33 +1100715{
716 struct md_rdev *rdev;
717
718 rdev_for_each_rcu(rdev, mddev)
719 if (rdev->bdev->bd_dev == dev)
720 return rdev;
721
722 return NULL;
723}
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100724EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100725
NeilBrown84fc4b52011-10-11 16:49:58 +1100726static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800727{
NeilBrown84fc4b52011-10-11 16:49:58 +1100728 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800729 list_for_each_entry(pers, &pers_list, list) {
730 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800731 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800732 if (strcmp(pers->name, clevel)==0)
733 return pers;
734 }
NeilBrown2604b702006-01-06 00:20:36 -0800735 return NULL;
736}
737
Andre Nollb73df2d2008-07-11 22:02:23 +1000738/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100739static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100741 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000742 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
NeilBrownf72ffdd2014-09-30 14:23:59 +1000745static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100748 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000749 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return 0;
751}
752
NeilBrown545c8792012-05-22 13:54:30 +1000753void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800756 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 rdev->sb_loaded = 0;
758 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000759 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100760 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
NeilBrown2699b672011-07-28 11:31:47 +1000762 if (rdev->bb_page) {
763 put_page(rdev->bb_page);
764 rdev->bb_page = NULL;
765 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800766 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
NeilBrown545c8792012-05-22 13:54:30 +1000768EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200770static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700771{
NeilBrown3cb03002011-10-11 16:45:26 +1100772 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100773 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700774
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200775 if (bio->bi_status) {
776 pr_err("md: super_written gets error=%d\n", bio->bi_status);
NeilBrowna9701a32005-11-08 21:39:34 -0800777 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100778 if (!test_bit(Faulty, &rdev->flags)
779 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800780 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100781 set_bit(LastDev, &rdev->flags);
782 }
783 } else
784 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700785
NeilBrowna9701a32005-11-08 21:39:34 -0800786 if (atomic_dec_and_test(&mddev->pending_writes))
787 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700788 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700789 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700790}
791
NeilBrownfd01b882011-10-11 16:47:53 +1100792void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700793 sector_t sector, int size, struct page *page)
794{
795 /* write first size bytes of page to sector of rdev
796 * Increment mddev->pending_writes before returning
797 * and decrement it on completion, waking up sb_wait
798 * if zero is reached.
799 * If an error occurred, call md_error
800 */
NeilBrown46533ff2016-11-18 16:16:11 +1100801 struct bio *bio;
802 int ff = 0;
803
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +0100804 if (!page)
805 return;
806
NeilBrown46533ff2016-11-18 16:16:11 +1100807 if (test_bit(Faulty, &rdev->flags))
808 return;
809
NeilBrown5a850712017-06-21 09:12:21 +1000810 bio = md_bio_alloc_sync(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700811
Shaohua Lied3b98c2016-03-29 14:00:19 -0700812 atomic_inc(&rdev->nr_pending);
813
Christoph Hellwig74d46992017-08-23 19:10:32 +0200814 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700815 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700816 bio_add_page(bio, page, size, 0);
817 bio->bi_private = rdev;
818 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100819
820 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
821 test_bit(FailFast, &rdev->flags) &&
822 !test_bit(LastDev, &rdev->flags))
823 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +0200824 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -0800825
NeilBrown7bfa19f2005-06-21 17:17:28 -0700826 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500827 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800828}
829
NeilBrown46533ff2016-11-18 16:16:11 +1100830int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800831{
Tejun Heoe9c74692010-09-03 11:56:18 +0200832 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000833 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -0800834 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +1100835 return -EAGAIN;
836 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700837}
838
NeilBrown3cb03002011-10-11 16:45:26 +1100839int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -0500840 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
NeilBrown5a850712017-06-21 09:12:21 +1000842 struct bio *bio = md_bio_alloc_sync(rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 int ret;
844
Christoph Hellwig74d46992017-08-23 19:10:32 +0200845 if (metadata_op && rdev->meta_bdev)
846 bio_set_dev(bio, rdev->meta_bdev);
847 else
848 bio_set_dev(bio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -0500849 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100850 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700851 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000852 else if (rdev->mddev->reshape_position != MaxSector &&
853 (rdev->mddev->reshape_backwards ==
854 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700855 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100856 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700857 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500859
860 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200862 ret = !bio->bi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 bio_put(bio);
864 return ret;
865}
NeilBrowna8745db2006-01-06 00:20:34 -0800866EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
NeilBrownf72ffdd2014-09-30 14:23:59 +1000868static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (rdev->sb_loaded)
873 return 0;
874
Mike Christie796a5cf2016-06-05 14:32:07 -0500875 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 goto fail;
877 rdev->sb_loaded = 1;
878 return 0;
879
880fail:
NeilBrown9d487392016-11-02 14:16:49 +1100881 pr_err("md: disabled device %s, could not read superblock.\n",
882 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return -EINVAL;
884}
885
Amir Goldsteine6fd2092017-05-04 16:26:20 +0300886static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000888 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000889 sb1->set_uuid1 == sb2->set_uuid1 &&
890 sb1->set_uuid2 == sb2->set_uuid2 &&
891 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Amir Goldsteine6fd2092017-05-04 16:26:20 +0300894static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 int ret;
897 mdp_super_t *tmp1, *tmp2;
898
899 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
900 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
901
902 if (!tmp1 || !tmp2) {
903 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 goto abort;
905 }
906
907 *tmp1 = *sb1;
908 *tmp2 = *sb2;
909
910 /*
911 * nr_disks is not constant
912 */
913 tmp1->nr_disks = 0;
914 tmp2->nr_disks = 0;
915
Andre Nollce0c8e02008-07-11 22:02:20 +1000916 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700918 kfree(tmp1);
919 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return ret;
921}
922
NeilBrown4d167f02007-05-09 02:35:37 -0700923static u32 md_csum_fold(u32 csum)
924{
925 csum = (csum & 0xffff) + (csum >> 16);
926 return (csum & 0xffff) + (csum >> 16);
927}
928
NeilBrownf72ffdd2014-09-30 14:23:59 +1000929static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
NeilBrown4d167f02007-05-09 02:35:37 -0700931 u64 newcsum = 0;
932 u32 *sb32 = (u32*)sb;
933 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 unsigned int disk_csum, csum;
935
936 disk_csum = sb->sb_csum;
937 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700938
939 for (i = 0; i < MD_SB_BYTES/4 ; i++)
940 newcsum += sb32[i];
941 csum = (newcsum & 0xffffffff) + (newcsum>>32);
942
NeilBrown4d167f02007-05-09 02:35:37 -0700943#ifdef CONFIG_ALPHA
944 /* This used to use csum_partial, which was wrong for several
945 * reasons including that different results are returned on
946 * different architectures. It isn't critical that we get exactly
947 * the same return value as before (we always csum_fold before
948 * testing, and that removes any differences). However as we
949 * know that csum_partial always returned a 16bit value on
950 * alphas, do a fold to maximise conformity to previous behaviour.
951 */
952 sb->sb_csum = md_csum_fold(disk_csum);
953#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700955#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return csum;
957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959/*
960 * Handle superblock details.
961 * We want to be able to handle multiple superblock formats
962 * so we have a common interface to them all, and an array of
963 * different handlers.
964 * We rely on user-space to write the initial superblock, and support
965 * reading and updating of superblocks.
966 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100967 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 * loads and validates a superblock on dev.
969 * if refdev != NULL, compare superblocks on both devices
970 * Return:
971 * 0 - dev has a superblock that is compatible with refdev
972 * 1 - dev has a superblock that is compatible and newer than refdev
973 * so dev should be used as the refdev in future
974 * -EINVAL superblock incompatible or invalid
975 * -othererror e.g. -EIO
976 *
NeilBrownfd01b882011-10-11 16:47:53 +1100977 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 * Verify that dev is acceptable into mddev.
979 * The first time, mddev->raid_disks will be 0, and data from
980 * dev should be merged in. Subsequent calls check that dev
981 * is new enough. Return 0 or -EINVAL
982 *
NeilBrownfd01b882011-10-11 16:47:53 +1100983 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 * Update the superblock for rdev with data in mddev
985 * This does not write to disc.
986 *
987 */
988
989struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000990 char *name;
991 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000992 int (*load_super)(struct md_rdev *rdev,
993 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000994 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000995 int (*validate_super)(struct mddev *mddev,
996 struct md_rdev *rdev);
997 void (*sync_super)(struct mddev *mddev,
998 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100999 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001000 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +10001001 int (*allow_new_offset)(struct md_rdev *rdev,
1002 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003};
1004
1005/*
Andre Noll0894cc32009-06-18 08:49:23 +10001006 * Check that the given mddev has no bitmap.
1007 *
1008 * This function is called from the run method of all personalities that do not
1009 * support bitmaps. It prints an error message and returns non-zero if mddev
1010 * has a bitmap. Otherwise, it returns 0.
1011 *
1012 */
NeilBrownfd01b882011-10-11 16:47:53 +11001013int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001014{
NeilBrownc3d97142009-12-14 12:49:52 +11001015 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001016 return 0;
NeilBrown9d487392016-11-02 14:16:49 +11001017 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +10001018 mdname(mddev), mddev->pers->name);
1019 return 1;
1020}
1021EXPORT_SYMBOL(md_check_no_bitmap);
1022
1023/*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001024 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 */
NeilBrown3cb03002011-10-11 16:45:26 +11001026static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1029 mdp_super_t *sb;
1030 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 /*
Andre Noll0f420352008-07-11 22:02:23 +10001033 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 * it's at the end of the disk.
1035 *
1036 * It also happens to be a multiple of 4Kb.
1037 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001038 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
NeilBrown0002b272005-09-09 16:23:53 -07001040 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +11001041 if (ret)
1042 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 ret = -EINVAL;
1045
1046 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001047 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +11001050 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 goto abort;
1052 }
1053
1054 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001055 sb->minor_version < 90 ||
1056 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +11001057 pr_warn("Bad version number %d.%d on %s\n",
1058 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 goto abort;
1060 }
1061
1062 if (sb->raid_disks <= 0)
1063 goto abort;
1064
NeilBrown4d167f02007-05-09 02:35:37 -07001065 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001066 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 goto abort;
1068 }
1069
1070 rdev->preferred_minor = sb->md_minor;
1071 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001072 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001073 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001074 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 if (sb->level == LEVEL_MULTIPATH)
1077 rdev->desc_nr = -1;
1078 else
1079 rdev->desc_nr = sb->this_disk.number;
1080
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001081 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001083 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001085 mdp_super_t *refsb = page_address(refdev->sb_page);
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001086 if (!md_uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001087 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 b, bdevname(refdev->bdev,b2));
1089 goto abort;
1090 }
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001091 if (!md_sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001092 pr_warn("md: %s has same UUID but different superblock to %s\n",
1093 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 goto abort;
1095 }
1096 ev1 = md_event(sb);
1097 ev2 = md_event(refsb);
1098 if (ev1 > ev2)
1099 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001100 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 ret = 0;
1102 }
NeilBrown8190e752009-06-18 08:48:58 +10001103 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001104 /* Limit to 4TB as metadata cannot record more than that.
1105 * (not needed for Linear and RAID0 as metadata doesn't
1106 * record this size)
1107 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001108 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1109 sb->level >= 1)
1110 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
NeilBrown27a7b262011-09-10 17:21:28 +10001112 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001113 /* "this cannot possibly happen" ... */
1114 ret = -EINVAL;
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 abort:
1117 return ret;
1118}
1119
1120/*
1121 * validate_super for 0.90.0
1122 */
NeilBrownfd01b882011-10-11 16:47:53 +11001123static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
1125 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001126 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001127 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
NeilBrown41158c72005-06-21 17:17:25 -07001129 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001130 clear_bit(Faulty, &rdev->flags);
1131 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001132 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001133 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (mddev->raid_disks == 0) {
1136 mddev->major_version = 0;
1137 mddev->minor_version = sb->minor_version;
1138 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001139 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001140 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 mddev->ctime = sb->ctime;
1142 mddev->utime = sb->utime;
1143 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001144 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 mddev->layout = sb->layout;
1146 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001147 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001148 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001149 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001150 mddev->bitmap_info.space = 0;
1151 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001152 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001153 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001154 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
NeilBrownf6705572006-03-27 01:18:11 -08001156 if (mddev->minor_version >= 91) {
1157 mddev->reshape_position = sb->reshape_position;
1158 mddev->delta_disks = sb->delta_disks;
1159 mddev->new_level = sb->new_level;
1160 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001161 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001162 if (mddev->delta_disks < 0)
1163 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001164 } else {
1165 mddev->reshape_position = MaxSector;
1166 mddev->delta_disks = 0;
1167 mddev->new_level = mddev->level;
1168 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001169 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001170 }
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (sb->state & (1<<MD_SB_CLEAN))
1173 mddev->recovery_cp = MaxSector;
1174 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001175 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 sb->events_lo == sb->cp_events_lo) {
1177 mddev->recovery_cp = sb->recovery_cp;
1178 } else
1179 mddev->recovery_cp = 0;
1180 }
1181
1182 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1183 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1184 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1185 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1186
1187 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001188
1189 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001190 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001191 mddev->bitmap_info.offset =
1192 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001193 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001194 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001195 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001196
NeilBrown41158c72005-06-21 17:17:25 -07001197 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001198 /* Insist on good event counter while assembling, except
1199 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001201 if (sb->disks[rdev->desc_nr].state & (
1202 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001203 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001204 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001205 } else if (mddev->bitmap) {
1206 /* if adding to array with a bitmap, then we can accept an
1207 * older device ... but not too old.
1208 */
NeilBrown41158c72005-06-21 17:17:25 -07001209 if (ev1 < mddev->bitmap->events_cleared)
1210 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001211 if (ev1 < mddev->events)
1212 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001213 } else {
1214 if (ev1 < mddev->events)
1215 /* just a hot-add of a new device, leave raid_disk at -1 */
1216 return 0;
1217 }
NeilBrown41158c72005-06-21 17:17:25 -07001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 desc = sb->disks + rdev->desc_nr;
1221
1222 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001223 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001224 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1225 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001226 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001228 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001229 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1230 /* active but not in sync implies recovery up to
1231 * reshape position. We don't know exactly where
1232 * that is, so set to zero for now */
1233 if (mddev->minor_version >= 91) {
1234 rdev->recovery_offset = 0;
1235 rdev->raid_disk = desc->raid_disk;
1236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001238 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1239 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001240 if (desc->state & (1<<MD_DISK_FAILFAST))
1241 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001242 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001243 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return 0;
1245}
1246
1247/*
1248 * sync_super for 0.90.0
1249 */
NeilBrownfd01b882011-10-11 16:47:53 +11001250static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001253 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /* make rdev->sb match mddev data..
1257 *
1258 * 1/ zero out disks
1259 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1260 * 3/ any empty disks < next_spare become removed
1261 *
1262 * disks[0] gets initialised to REMOVED because
1263 * we cannot be sure from other fields if it has
1264 * been initialised or not.
1265 */
1266 int i;
1267 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1268
NeilBrown61181562005-09-09 16:24:02 -07001269 rdev->sb_size = MD_SB_BYTES;
1270
Namhyung Kim65a06f062011-07-27 11:00:36 +10001271 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 memset(sb, 0, sizeof(*sb));
1274
1275 sb->md_magic = MD_SB_MAGIC;
1276 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 sb->patch_version = mddev->patch_version;
1278 sb->gvalid_words = 0; /* ignored */
1279 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1280 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1281 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1282 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1283
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001284 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001286 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 sb->raid_disks = mddev->raid_disks;
1288 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001289 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001290 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 sb->state = 0;
1292 sb->events_hi = (mddev->events>>32);
1293 sb->events_lo = (u32)mddev->events;
1294
NeilBrownf6705572006-03-27 01:18:11 -08001295 if (mddev->reshape_position == MaxSector)
1296 sb->minor_version = 90;
1297 else {
1298 sb->minor_version = 91;
1299 sb->reshape_position = mddev->reshape_position;
1300 sb->new_level = mddev->new_level;
1301 sb->delta_disks = mddev->delta_disks;
1302 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001303 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001304 }
1305 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (mddev->in_sync)
1307 {
1308 sb->recovery_cp = mddev->recovery_cp;
1309 sb->cp_events_hi = (mddev->events>>32);
1310 sb->cp_events_lo = (u32)mddev->events;
1311 if (mddev->recovery_cp == MaxSector)
1312 sb->state = (1<< MD_SB_CLEAN);
1313 } else
1314 sb->recovery_cp = 0;
1315
1316 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001317 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
NeilBrownc3d97142009-12-14 12:49:52 +11001319 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001320 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001323 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001325 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001326 int is_active = test_bit(In_sync, &rdev2->flags);
1327
1328 if (rdev2->raid_disk >= 0 &&
1329 sb->minor_version >= 91)
1330 /* we have nowhere to store the recovery_offset,
1331 * but if it is not below the reshape_position,
1332 * we can piggy-back on that.
1333 */
1334 is_active = 1;
1335 if (rdev2->raid_disk < 0 ||
1336 test_bit(Faulty, &rdev2->flags))
1337 is_active = 0;
1338 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001339 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001341 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001342 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 d = &sb->disks[rdev2->desc_nr];
1344 nr_disks++;
1345 d->number = rdev2->desc_nr;
1346 d->major = MAJOR(rdev2->bdev->bd_dev);
1347 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001348 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 d->raid_disk = rdev2->raid_disk;
1350 else
1351 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001352 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001354 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001356 if (test_bit(In_sync, &rdev2->flags))
1357 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 active++;
1359 working++;
1360 } else {
1361 d->state = 0;
1362 spare++;
1363 working++;
1364 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001365 if (test_bit(WriteMostly, &rdev2->flags))
1366 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001367 if (test_bit(FailFast, &rdev2->flags))
1368 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 /* now set the "removed" and "faulty" bits on any missing devices */
1371 for (i=0 ; i < mddev->raid_disks ; i++) {
1372 mdp_disk_t *d = &sb->disks[i];
1373 if (d->state == 0 && d->number == 0) {
1374 d->number = i;
1375 d->raid_disk = i;
1376 d->state = (1<<MD_DISK_REMOVED);
1377 d->state |= (1<<MD_DISK_FAULTY);
1378 failed++;
1379 }
1380 }
1381 sb->nr_disks = nr_disks;
1382 sb->active_disks = active;
1383 sb->working_disks = working;
1384 sb->failed_disks = failed;
1385 sb->spare_disks = spare;
1386
1387 sb->this_disk = sb->disks[rdev->desc_nr];
1388 sb->sb_csum = calc_sb_csum(sb);
1389}
1390
1391/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001392 * rdev_size_change for 0.90.0
1393 */
1394static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001395super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001396{
Andre Noll58c0fed2009-03-31 14:33:13 +11001397 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001398 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001399 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001400 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001401 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001402 if (!num_sectors || num_sectors > rdev->sb_start)
1403 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001404 /* Limit to 4TB as metadata cannot record more than that.
1405 * 4TB == 2^32 KB, or 2*2^32 sectors.
1406 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001407 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1408 rdev->mddev->level >= 1)
1409 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001410 do {
1411 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001412 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001413 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001414 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001415}
1416
NeilBrownc6563a82012-05-21 09:27:00 +10001417static int
1418super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1419{
1420 /* non-zero offset changes not possible with v0.90 */
1421 return new_offset == 0;
1422}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001423
1424/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 * version 1 superblock
1426 */
1427
NeilBrownf72ffdd2014-09-30 14:23:59 +10001428static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001430 __le32 disk_csum;
1431 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 unsigned long long newcsum;
1433 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001434 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 disk_csum = sb->sb_csum;
1437 sb->sb_csum = 0;
1438 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001439 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 newcsum += le32_to_cpu(*isuper++);
1441
1442 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001443 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1446 sb->sb_csum = disk_csum;
1447 return cpu_to_le32(csum);
1448}
1449
NeilBrown3cb03002011-10-11 16:45:26 +11001450static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
1452 struct mdp_superblock_1 *sb;
1453 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001454 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001455 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001457 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 /*
Andre Noll0f420352008-07-11 22:02:23 +10001460 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 * It is always aligned to a 4K boundary and
1462 * depeding on minor_version, it can be:
1463 * 0: At least 8K, but less than 12K, from end of device
1464 * 1: At start of device
1465 * 2: 4K from start of device.
1466 */
1467 switch(minor_version) {
1468 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001469 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001470 sb_start -= 8*2;
1471 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 break;
1473 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001474 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 break;
1476 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001477 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 break;
1479 default:
1480 return -EINVAL;
1481 }
Andre Noll0f420352008-07-11 22:02:23 +10001482 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
NeilBrown0002b272005-09-09 16:23:53 -07001484 /* superblock is rarely larger than 1K, but it can be larger,
1485 * and it is safe to read 4k, so we do that
1486 */
1487 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (ret) return ret;
1489
Namhyung Kim65a06f062011-07-27 11:00:36 +10001490 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1493 sb->major_version != cpu_to_le32(1) ||
1494 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001495 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001496 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return -EINVAL;
1498
1499 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001500 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 bdevname(rdev->bdev,b));
1502 return -EINVAL;
1503 }
1504 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001505 pr_warn("md: data_size too small on %s\n",
1506 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return -EINVAL;
1508 }
NeilBrownc6563a82012-05-21 09:27:00 +10001509 if (sb->pad0 ||
1510 sb->pad3[0] ||
1511 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1512 /* Some padding is non-zero, might be a new feature */
1513 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 rdev->preferred_minor = 0xffff;
1516 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001517 rdev->new_data_offset = rdev->data_offset;
1518 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1519 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1520 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001521 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
NeilBrown0002b272005-09-09 16:23:53 -07001523 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001524 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001525 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001526 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1527
1528 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001529 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001530 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001531 if (minor_version
1532 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1533 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001534
NeilBrown31b65a02006-07-10 04:44:14 -07001535 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1536 rdev->desc_nr = -1;
1537 else
1538 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1539
NeilBrown2699b672011-07-28 11:31:47 +10001540 if (!rdev->bb_page) {
1541 rdev->bb_page = alloc_page(GFP_KERNEL);
1542 if (!rdev->bb_page)
1543 return -ENOMEM;
1544 }
1545 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1546 rdev->badblocks.count == 0) {
1547 /* need to load the bad block list.
1548 * Currently we limit it to one page.
1549 */
1550 s32 offset;
1551 sector_t bb_sector;
1552 u64 *bbp;
1553 int i;
1554 int sectors = le16_to_cpu(sb->bblog_size);
1555 if (sectors > (PAGE_SIZE / 512))
1556 return -EINVAL;
1557 offset = le32_to_cpu(sb->bblog_offset);
1558 if (offset == 0)
1559 return -EINVAL;
1560 bb_sector = (long long)offset;
1561 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001562 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001563 return -EIO;
1564 bbp = (u64 *)page_address(rdev->bb_page);
1565 rdev->badblocks.shift = sb->bblog_shift;
1566 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1567 u64 bb = le64_to_cpu(*bbp);
1568 int count = bb & (0x3ff);
1569 u64 sector = bb >> 10;
1570 sector <<= sb->bblog_shift;
1571 count <<= sb->bblog_shift;
1572 if (bb + 1 == 0)
1573 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001574 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001575 return -EINVAL;
1576 }
NeilBrown486adf72013-04-24 11:42:44 +10001577 } else if (sb->bblog_offset != 0)
1578 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001579
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001580 if ((le32_to_cpu(sb->feature_map) &
1581 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001582 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1583 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1584 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1585 }
1586
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001587 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001588 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001589 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001591 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1594 sb->level != refsb->level ||
1595 sb->layout != refsb->layout ||
1596 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001597 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 bdevname(rdev->bdev,b),
1599 bdevname(refdev->bdev,b2));
1600 return -EINVAL;
1601 }
1602 ev1 = le64_to_cpu(sb->events);
1603 ev2 = le64_to_cpu(refsb->events);
1604
1605 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001606 ret = 1;
1607 else
1608 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
NeilBrownc6563a82012-05-21 09:27:00 +10001610 if (minor_version) {
1611 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1612 sectors -= rdev->data_offset;
1613 } else
1614 sectors = rdev->sb_start;
1615 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001617 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001618 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
NeilBrownfd01b882011-10-11 16:47:53 +11001621static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001623 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001624 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
NeilBrown41158c72005-06-21 17:17:25 -07001626 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001627 clear_bit(Faulty, &rdev->flags);
1628 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001629 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001630 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 if (mddev->raid_disks == 0) {
1633 mddev->major_version = 1;
1634 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001635 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001636 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001637 mddev->ctime = le64_to_cpu(sb->ctime);
1638 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001640 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 mddev->layout = le32_to_cpu(sb->layout);
1642 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001643 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001644 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001645 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001646 mddev->bitmap_info.space = 0;
1647 /* Default location for bitmap is 1K after superblock
1648 * using 3K - total of 4K
1649 */
NeilBrownc3d97142009-12-14 12:49:52 +11001650 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001651 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001652 mddev->reshape_backwards = 0;
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1655 memcpy(mddev->uuid, sb->set_uuid, 16);
1656
1657 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001658
NeilBrown71c08052005-09-09 16:23:51 -07001659 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001660 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001661 mddev->bitmap_info.offset =
1662 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001663 /* Metadata doesn't record how much space is available.
1664 * For 1.0, we assume we can use up to the superblock
1665 * if before, else to 4K beyond superblock.
1666 * For others, assume no change is possible.
1667 */
1668 if (mddev->minor_version > 0)
1669 mddev->bitmap_info.space = 0;
1670 else if (mddev->bitmap_info.offset > 0)
1671 mddev->bitmap_info.space =
1672 8 - mddev->bitmap_info.offset;
1673 else
1674 mddev->bitmap_info.space =
1675 -mddev->bitmap_info.offset;
1676 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001677
NeilBrownf6705572006-03-27 01:18:11 -08001678 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1679 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1680 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1681 mddev->new_level = le32_to_cpu(sb->new_level);
1682 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001683 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001684 if (mddev->delta_disks < 0 ||
1685 (mddev->delta_disks == 0 &&
1686 (le32_to_cpu(sb->feature_map)
1687 & MD_FEATURE_RESHAPE_BACKWARDS)))
1688 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001689 } else {
1690 mddev->reshape_position = MaxSector;
1691 mddev->delta_disks = 0;
1692 mddev->new_level = mddev->level;
1693 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001694 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001695 }
1696
Song Liu486b0f72016-08-19 15:34:01 -07001697 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001698 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001699
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001700 if (le32_to_cpu(sb->feature_map) &
1701 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001702 if (le32_to_cpu(sb->feature_map) &
1703 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1704 return -EINVAL;
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001705 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1706 (le32_to_cpu(sb->feature_map) &
1707 MD_FEATURE_MULTIPLE_PPLS))
1708 return -EINVAL;
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001709 set_bit(MD_HAS_PPL, &mddev->flags);
1710 }
NeilBrown41158c72005-06-21 17:17:25 -07001711 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001712 /* Insist of good event counter while assembling, except for
1713 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001715 if (rdev->desc_nr >= 0 &&
1716 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001717 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1718 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001719 if (ev1 < mddev->events)
1720 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001721 } else if (mddev->bitmap) {
1722 /* If adding to array with a bitmap, then we can accept an
1723 * older device, but not too old.
1724 */
NeilBrown41158c72005-06-21 17:17:25 -07001725 if (ev1 < mddev->bitmap->events_cleared)
1726 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001727 if (ev1 < mddev->events)
1728 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001729 } else {
1730 if (ev1 < mddev->events)
1731 /* just a hot-add of a new device, leave raid_disk at -1 */
1732 return 0;
1733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (mddev->level != LEVEL_MULTIPATH) {
1735 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001736 if (rdev->desc_nr < 0 ||
1737 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001738 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001739 rdev->desc_nr = -1;
1740 } else
1741 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001743 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001745 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001746 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 break;
Song Liubac624f2015-08-13 14:31:55 -07001748 case MD_DISK_ROLE_JOURNAL: /* journal device */
1749 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1750 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001751 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001752 return -EINVAL;
1753 }
1754 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001755 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001756 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001757 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 default:
NeilBrownf4667222013-12-09 12:04:56 +11001759 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001760 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001761 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001762 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001763 if (!(le32_to_cpu(sb->feature_map) &
1764 MD_FEATURE_RECOVERY_BITMAP))
1765 rdev->saved_raid_disk = -1;
1766 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001767 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 rdev->raid_disk = role;
1769 break;
1770 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001771 if (sb->devflags & WriteMostly1)
1772 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001773 if (sb->devflags & FailFast1)
1774 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001775 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1776 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001777 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001778 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return 0;
1781}
1782
NeilBrownfd01b882011-10-11 16:47:53 +11001783static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001786 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 int max_dev, i;
1788 /* make rdev->sb match mddev and rdev data. */
1789
Namhyung Kim65a06f062011-07-27 11:00:36 +10001790 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 sb->feature_map = 0;
1793 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001794 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 memset(sb->pad3, 0, sizeof(sb->pad3));
1796
1797 sb->utime = cpu_to_le64((__u64)mddev->utime);
1798 sb->events = cpu_to_le64(mddev->events);
1799 if (mddev->in_sync)
1800 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001801 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1802 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 else
1804 sb->resync_offset = cpu_to_le64(0);
1805
NeilBrown1c05b4b2006-10-21 10:24:08 -07001806 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001807
NeilBrownf0ca3402006-02-02 14:28:04 -08001808 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001809 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001810 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001811 sb->level = cpu_to_le32(mddev->level);
1812 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11001813 if (test_bit(FailFast, &rdev->flags))
1814 sb->devflags |= FailFast1;
1815 else
1816 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08001817
NeilBrownaeb9b2112011-08-25 14:43:08 +10001818 if (test_bit(WriteMostly, &rdev->flags))
1819 sb->devflags |= WriteMostly1;
1820 else
1821 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001822 sb->data_offset = cpu_to_le64(rdev->data_offset);
1823 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001824
NeilBrownc3d97142009-12-14 12:49:52 +11001825 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1826 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001827 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001828 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001829
Shaohua Lif2076e72015-10-08 21:54:12 -07001830 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001831 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001832 sb->feature_map |=
1833 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1834 sb->recovery_offset =
1835 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001836 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1837 sb->feature_map |=
1838 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001839 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001840 /* Note: recovery_offset and journal_tail share space */
1841 if (test_bit(Journal, &rdev->flags))
1842 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001843 if (test_bit(Replacement, &rdev->flags))
1844 sb->feature_map |=
1845 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001846
NeilBrownf6705572006-03-27 01:18:11 -08001847 if (mddev->reshape_position != MaxSector) {
1848 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1849 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1850 sb->new_layout = cpu_to_le32(mddev->new_layout);
1851 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1852 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001853 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001854 if (mddev->delta_disks == 0 &&
1855 mddev->reshape_backwards)
1856 sb->feature_map
1857 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001858 if (rdev->new_data_offset != rdev->data_offset) {
1859 sb->feature_map
1860 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1861 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1862 - rdev->data_offset));
1863 }
NeilBrownf6705572006-03-27 01:18:11 -08001864 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001865
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001866 if (mddev_is_clustered(mddev))
1867 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1868
NeilBrown2699b672011-07-28 11:31:47 +10001869 if (rdev->badblocks.count == 0)
1870 /* Nothing to do for bad blocks*/ ;
1871 else if (sb->bblog_offset == 0)
1872 /* Cannot record bad blocks on this device */
1873 md_error(mddev, rdev);
1874 else {
1875 struct badblocks *bb = &rdev->badblocks;
1876 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1877 u64 *p = bb->page;
1878 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1879 if (bb->changed) {
1880 unsigned seq;
1881
1882retry:
1883 seq = read_seqbegin(&bb->lock);
1884
1885 memset(bbp, 0xff, PAGE_SIZE);
1886
1887 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001888 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001889 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1890 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001891 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001892 }
NeilBrownd0962932012-03-19 12:46:41 +11001893 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001894 if (read_seqretry(&bb->lock, seq))
1895 goto retry;
1896
1897 bb->sector = (rdev->sb_start +
1898 (int)le32_to_cpu(sb->bblog_offset));
1899 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001900 }
1901 }
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001904 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (rdev2->desc_nr+1 > max_dev)
1906 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001907
NeilBrown70471da2009-08-03 10:59:57 +10001908 if (max_dev > le32_to_cpu(sb->max_dev)) {
1909 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001910 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001911 rdev->sb_size = max_dev * 2 + 256;
1912 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1913 if (rdev->sb_size & bmask)
1914 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001915 } else
1916 max_dev = le32_to_cpu(sb->max_dev);
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 for (i=0; i<max_dev;i++)
Lidong Zhong8df72022017-06-12 10:45:55 +08001919 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001920
Song Liua97b7892015-10-08 21:54:09 -07001921 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1922 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001924 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001925 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
1926 sb->feature_map |=
1927 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
1928 else
1929 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001930 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
1931 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
1932 }
1933
NeilBrowndafb20f2012-03-19 12:46:39 +11001934 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001936 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001937 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001938 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001940 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001941 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001942 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001943 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 else
Song Liuc4d4c912015-08-13 14:31:54 -07001945 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 sb->sb_csum = calc_sb_1_csum(sb);
1949}
1950
Chris Webb0cd17fe2008-06-28 08:31:46 +10001951static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001952super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001953{
1954 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001955 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001956 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001957 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001958 if (rdev->data_offset != rdev->new_data_offset)
1959 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001960 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001961 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001962 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001963 max_sectors -= rdev->data_offset;
1964 if (!num_sectors || num_sectors > max_sectors)
1965 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001966 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001967 /* minor version 0 with bitmap we can't move */
1968 return 0;
1969 } else {
1970 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001971 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001972 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001973 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001974 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001975 if (!num_sectors || num_sectors > max_sectors)
1976 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001977 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001978 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001979 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001980 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08001981 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10001982 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11001983 do {
1984 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1985 rdev->sb_page);
1986 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001987 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001988
1989}
1990
1991static int
1992super_1_allow_new_offset(struct md_rdev *rdev,
1993 unsigned long long new_offset)
1994{
1995 /* All necessary checks on new >= old have been done */
1996 struct bitmap *bitmap;
1997 if (new_offset >= rdev->data_offset)
1998 return 1;
1999
2000 /* with 1.0 metadata, there is no metadata to tread on
2001 * so we can always move back */
2002 if (rdev->mddev->minor_version == 0)
2003 return 1;
2004
2005 /* otherwise we must be sure not to step on
2006 * any metadata, so stay:
2007 * 36K beyond start of superblock
2008 * beyond end of badblocks
2009 * beyond write-intent bitmap
2010 */
2011 if (rdev->sb_start + (32+4)*2 > new_offset)
2012 return 0;
2013 bitmap = rdev->mddev->bitmap;
2014 if (bitmap && !rdev->mddev->bitmap_info.file &&
2015 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10002016 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10002017 return 0;
2018 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2019 return 0;
2020
2021 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002022}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Adrian Bunk75c96f82005-05-05 16:16:09 -07002024static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 [0] = {
2026 .name = "0.90.0",
2027 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002028 .load_super = super_90_load,
2029 .validate_super = super_90_validate,
2030 .sync_super = super_90_sync,
2031 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002032 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 },
2034 [1] = {
2035 .name = "md-1",
2036 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002037 .load_super = super_1_load,
2038 .validate_super = super_1_validate,
2039 .sync_super = super_1_sync,
2040 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002041 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 },
2043};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
NeilBrownfd01b882011-10-11 16:47:53 +11002045static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05002046{
2047 if (mddev->sync_super) {
2048 mddev->sync_super(mddev, rdev);
2049 return;
2050 }
2051
2052 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2053
2054 super_types[mddev->major_version].sync_super(mddev, rdev);
2055}
2056
NeilBrownfd01b882011-10-11 16:47:53 +11002057static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
NeilBrown3cb03002011-10-11 16:45:26 +11002059 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
NeilBrown4b809912008-07-21 17:05:25 +10002061 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07002062 rdev_for_each_rcu(rdev, mddev1) {
2063 if (test_bit(Faulty, &rdev->flags) ||
2064 test_bit(Journal, &rdev->flags) ||
2065 rdev->raid_disk == -1)
2066 continue;
2067 rdev_for_each_rcu(rdev2, mddev2) {
2068 if (test_bit(Faulty, &rdev2->flags) ||
2069 test_bit(Journal, &rdev2->flags) ||
2070 rdev2->raid_disk == -1)
2071 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002072 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002073 rdev2->bdev->bd_contains) {
2074 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002075 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002076 }
Song Liu0b020e82015-09-03 23:00:35 -07002077 }
2078 }
NeilBrown4b809912008-07-21 17:05:25 +10002079 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return 0;
2081}
2082
2083static LIST_HEAD(pending_raid_disks);
2084
Andre Nollac5e7112009-08-03 10:59:47 +10002085/*
2086 * Try to register data integrity profile for an mddev
2087 *
2088 * This is called when an array is started and after a disk has been kicked
2089 * from the array. It only succeeds if all working and active component devices
2090 * are integrity capable with matching profiles.
2091 */
NeilBrownfd01b882011-10-11 16:47:53 +11002092int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002093{
NeilBrown3cb03002011-10-11 16:45:26 +11002094 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002095
Andre Nollac5e7112009-08-03 10:59:47 +10002096 if (list_empty(&mddev->disks))
2097 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002098 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2099 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002100 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002101 /* skip spares and non-functional disks */
2102 if (test_bit(Faulty, &rdev->flags))
2103 continue;
2104 if (rdev->raid_disk < 0)
2105 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002106 if (!reference) {
2107 /* Use the first rdev as the reference */
2108 reference = rdev;
2109 continue;
2110 }
2111 /* does this rdev's profile match the reference profile? */
2112 if (blk_integrity_compare(reference->bdev->bd_disk,
2113 rdev->bdev->bd_disk) < 0)
2114 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002115 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002116 if (!reference || !bdev_get_integrity(reference->bdev))
2117 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002118 /*
2119 * All component devices are integrity capable and have matching
2120 * profiles, register the common profile for the md device.
2121 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002122 blk_integrity_register(mddev->gendisk,
2123 bdev_get_integrity(reference->bdev));
2124
NeilBrown9d487392016-11-02 14:16:49 +11002125 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Martin K. Petersena91a2782011-03-17 11:11:05 +01002126 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002127 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002128 mdname(mddev));
2129 return -EINVAL;
2130 }
Andre Nollac5e7112009-08-03 10:59:47 +10002131 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002132}
Andre Nollac5e7112009-08-03 10:59:47 +10002133EXPORT_SYMBOL(md_integrity_register);
2134
Dan Williams1501efa2016-01-13 16:00:07 -08002135/*
2136 * Attempt to add an rdev, but only if it is consistent with the current
2137 * integrity profile
2138 */
2139int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002140{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002141 struct blk_integrity *bi_rdev;
2142 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002143 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002144
2145 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002146 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002147
2148 bi_rdev = bdev_get_integrity(rdev->bdev);
2149 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002150
2151 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002152 return 0;
2153
2154 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002155 pr_err("%s: incompatible integrity profile for %s\n",
2156 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002157 return -ENXIO;
2158 }
2159
2160 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002161}
2162EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002163
NeilBrownf72ffdd2014-09-30 14:23:59 +10002164static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002166 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002167 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002168 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Dan Williams11e2ede2008-04-30 00:52:32 -07002170 /* prevent duplicates */
2171 if (find_rdev(mddev, rdev->bdev->bd_dev))
2172 return -EEXIST;
2173
NeilBrown97b20ef2017-04-13 08:53:48 +10002174 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2175 mddev->pers)
2176 return -EROFS;
2177
Andre Nolldd8ac332009-03-31 14:33:13 +11002178 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002179 if (!test_bit(Journal, &rdev->flags) &&
2180 rdev->sectors &&
2181 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002182 if (mddev->pers) {
2183 /* Cannot change size, so fail
2184 * If mddev->level <= 0, then we don't care
2185 * about aligning sizes (e.g. linear)
2186 */
2187 if (mddev->level > 0)
2188 return -ENOSPC;
2189 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002190 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193 /* Verify rdev->desc_nr is unique.
2194 * If it is -1, assign a free number, else
2195 * check number is not in use
2196 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002197 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 if (rdev->desc_nr < 0) {
2199 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002200 if (mddev->pers)
2201 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002202 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 choice++;
2204 rdev->desc_nr = choice;
2205 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002206 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002207 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002211 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002212 if (!test_bit(Journal, &rdev->flags) &&
2213 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002214 pr_warn("md: %s: array is limited to %d devices\n",
2215 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002216 return -EBUSY;
2217 }
NeilBrown19133a42005-11-08 21:39:35 -08002218 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002219 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002222 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002223
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002224 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002225 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002226
Tejun Heo0762b8b2008-08-25 19:56:12 +09002227 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002228 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2229 /* failure here is OK */;
2230 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002231
NeilBrown4b809912008-07-21 17:05:25 +10002232 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002233 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002234
2235 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002236 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002239
2240 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002241 pr_warn("md: failed to register dev-%s for %s\n",
2242 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002243 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
NeilBrown177a99b2008-02-06 01:39:56 -08002246static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002247{
NeilBrown3cb03002011-10-11 16:45:26 +11002248 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002249 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002250 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002251}
2252
NeilBrownf72ffdd2014-09-30 14:23:59 +10002253static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
2255 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002256
Tejun Heo49731ba2011-01-14 18:43:57 +01002257 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002258 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002259 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002261 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002262 sysfs_put(rdev->sysfs_state);
2263 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002264 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002265 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002266 * writing to 'remove' to "dev/state". We also need
2267 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002268 */
NeilBrown4b809912008-07-21 17:05:25 +10002269 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002270 INIT_WORK(&rdev->del_work, md_delayed_delete);
2271 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002272 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273}
2274
2275/*
2276 * prevent the device from being mounted, repartitioned or
2277 * otherwise reused by a RAID array (or any other kernel
2278 * subsystem), by bd_claiming the device.
2279 */
NeilBrown3cb03002011-10-11 16:45:26 +11002280static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
2282 int err = 0;
2283 struct block_device *bdev;
2284 char b[BDEVNAME_SIZE];
2285
Tejun Heod4d77622010-11-13 11:55:18 +01002286 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002287 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002289 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return PTR_ERR(bdev);
2291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 rdev->bdev = bdev;
2293 return err;
2294}
2295
NeilBrown3cb03002011-10-11 16:45:26 +11002296static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
2298 struct block_device *bdev = rdev->bdev;
2299 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002300 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
2303void md_autodetect_dev(dev_t dev);
2304
NeilBrownf72ffdd2014-09-30 14:23:59 +10002305static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
2307 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002308
NeilBrown9d487392016-11-02 14:16:49 +11002309 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002310 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002312 if (test_bit(AutoDetected, &rdev->flags))
2313 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314#endif
2315 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002316 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317}
2318
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002319void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
2321 unbind_rdev_from_array(rdev);
2322 export_rdev(rdev);
2323}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002324EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
NeilBrownfd01b882011-10-11 16:47:53 +11002326static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
NeilBrown0638bb02014-09-25 17:43:47 +10002328 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
NeilBrown0638bb02014-09-25 17:43:47 +10002330 while (!list_empty(&mddev->disks)) {
2331 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2332 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002333 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 mddev->raid_disks = 0;
2336 mddev->major_version = 0;
2337}
2338
NeilBrown6497709b2017-03-15 14:05:14 +11002339static bool set_in_sync(struct mddev *mddev)
2340{
Shaohua Liefa4b772017-10-18 22:08:13 -07002341 lockdep_assert_held(&mddev->lock);
NeilBrown4ad23a972017-03-15 14:05:14 +11002342 if (!mddev->in_sync) {
2343 mddev->sync_checkers++;
2344 spin_unlock(&mddev->lock);
2345 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2346 spin_lock(&mddev->lock);
2347 if (!mddev->in_sync &&
2348 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002349 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002350 /*
2351 * Ensure ->in_sync is visible before we clear
2352 * ->sync_checkers.
2353 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002354 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002355 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2356 sysfs_notify_dirent_safe(mddev->sysfs_state);
2357 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002358 if (--mddev->sync_checkers == 0)
2359 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002360 }
2361 if (mddev->safemode == 1)
2362 mddev->safemode = 0;
2363 return mddev->in_sync;
2364}
2365
NeilBrownf72ffdd2014-09-30 14:23:59 +10002366static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
NeilBrown42543762006-06-26 00:27:57 -07002368 /* Update each superblock (in-memory image), but
2369 * if we are allowed to, skip spares which already
2370 * have the right event counter, or have one earlier
2371 * (which would mean they aren't being marked as dirty
2372 * with the rest of the array)
2373 */
NeilBrown3cb03002011-10-11 16:45:26 +11002374 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002375 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002376 if (rdev->sb_events == mddev->events ||
2377 (nospares &&
2378 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002379 rdev->sb_events+1 == mddev->events)) {
2380 /* Don't update this superblock */
2381 rdev->sb_loaded = 2;
2382 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002383 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002384 rdev->sb_loaded = 1;
2385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387}
2388
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002389static bool does_sb_need_changing(struct mddev *mddev)
2390{
2391 struct md_rdev *rdev;
2392 struct mdp_superblock_1 *sb;
2393 int role;
2394
2395 /* Find a good rdev */
2396 rdev_for_each(rdev, mddev)
2397 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2398 break;
2399
2400 /* No good device found. */
2401 if (!rdev)
2402 return false;
2403
2404 sb = page_address(rdev->sb_page);
2405 /* Check if a device has become faulty or a spare become active */
2406 rdev_for_each(rdev, mddev) {
2407 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2408 /* Device activated? */
2409 if (role == 0xffff && rdev->raid_disk >=0 &&
2410 !test_bit(Faulty, &rdev->flags))
2411 return true;
2412 /* Device turned faulty? */
2413 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2414 return true;
2415 }
2416
2417 /* Check if any mddev parameters have changed */
2418 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2419 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002420 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002421 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2422 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2423 return true;
2424
2425 return false;
2426}
2427
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002428void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
NeilBrown3cb03002011-10-11 16:45:26 +11002430 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002431 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002432 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002433 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002434 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
NeilBrownd87f0642013-04-24 11:42:40 +10002436 if (mddev->ro) {
2437 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002438 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002439 return;
2440 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002441
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002442repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002443 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002444 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002445 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002446 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002447 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002448 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002449 /* Has someone else has updated the sb */
2450 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002451 if (ret == 0)
2452 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002453 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2454 BIT(MD_SB_CHANGE_DEVS) |
2455 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002456 return;
2457 }
2458 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002459
NeilBrowndb0505d2017-10-17 16:18:36 +11002460 /*
2461 * First make sure individual recovery_offsets are correct
2462 * curr_resync_completed can only be used during recovery.
2463 * During reshape/resync it might use array-addresses rather
2464 * that device addresses.
2465 */
NeilBrowndafb20f2012-03-19 12:46:39 +11002466 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002467 if (rdev->raid_disk >= 0 &&
2468 mddev->delta_disks >= 0 &&
NeilBrowndb0505d2017-10-17 16:18:36 +11002469 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2470 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2471 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002472 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002473 !test_bit(In_sync, &rdev->flags) &&
2474 mddev->curr_resync_completed > rdev->recovery_offset)
2475 rdev->recovery_offset = mddev->curr_resync_completed;
2476
NeilBrownf72ffdd2014-09-30 14:23:59 +10002477 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002478 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002479 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2480 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002481 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002482 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002483 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002484 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002485 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002486 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002487 md_error(mddev, rdev);
2488 }
2489 clear_bit(Blocked, &rdev->flags);
2490 clear_bit(BlockedBadBlocks, &rdev->flags);
2491 wake_up(&rdev->blocked_wait);
2492 }
2493 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002494 wake_up(&mddev->sb_wait);
2495 return;
2496 }
2497
NeilBrown85572d72014-12-15 12:56:56 +11002498 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002499
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002500 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002501
Shaohua Li29530792016-12-08 15:48:19 -08002502 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002503 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002504 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002505 /* just a clean<-> dirty transition, possibly leave spares alone,
2506 * though if events isn't the right even/odd, we will have to do
2507 * spares after all
2508 */
2509 nospares = 1;
2510 if (force_change)
2511 nospares = 0;
2512 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002513 /* If the array is degraded, then skipping spares is both
2514 * dangerous and fairly pointless.
2515 * Dangerous because a device that was removed from the array
2516 * might have a event_count that still looks up-to-date,
2517 * so it can be re-added without a resync.
2518 * Pointless because if there are any spares to skip,
2519 * then a recovery will happen and soon that array won't
2520 * be degraded any more and the spare can go back to sleep then.
2521 */
NeilBrown850b2b422006-10-03 01:15:46 -07002522 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002523
NeilBrown06d91a52005-06-21 17:17:12 -07002524 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002525
2526 /* If this is just a dirty<->clean transition, and the array is clean
2527 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002528 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002529 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002530 && mddev->can_decrease_events
2531 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002532 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002533 mddev->can_decrease_events = 0;
2534 } else {
NeilBrown42543762006-06-26 00:27:57 -07002535 /* otherwise we have to go forward and ... */
2536 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002537 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
NeilBrown403df472014-09-30 15:52:29 +10002540 /*
2541 * This 64-bit counter should never wrap.
2542 * Either we are in around ~1 trillion A.C., assuming
2543 * 1 reboot per second, or we have a bug...
2544 */
2545 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002546
NeilBrowndafb20f2012-03-19 12:46:39 +11002547 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002548 if (rdev->badblocks.changed)
2549 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002550 if (test_bit(Faulty, &rdev->flags))
2551 set_bit(FaultRecorded, &rdev->flags);
2552 }
NeilBrown2699b672011-07-28 11:31:47 +10002553
NeilBrowne6910632008-02-06 01:39:51 -08002554 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002555 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
NeilBrown36a4e1f2011-10-07 14:23:17 +11002557 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2558 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Shaohua Li504634f2016-11-18 09:44:08 -08002560 if (mddev->queue)
2561 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002562rewrite:
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002563 md_bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002564 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002566
NeilBrown42543762006-06-26 00:27:57 -07002567 if (rdev->sb_loaded != 1)
2568 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
NeilBrownf4667222013-12-09 12:04:56 +11002570 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002571 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002572 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002573 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002574 pr_debug("md: (write) %s's sb offset: %llu\n",
2575 bdevname(rdev->bdev, b),
2576 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002577 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002578 if (rdev->badblocks.size) {
2579 md_super_write(mddev, rdev,
2580 rdev->badblocks.sector,
2581 rdev->badblocks.size << 9,
2582 rdev->bb_page);
2583 rdev->badblocks.size = 0;
2584 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002585
NeilBrownf4667222013-12-09 12:04:56 +11002586 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002587 pr_debug("md: %s (skipping faulty)\n",
2588 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002589
NeilBrown7bfa19f2005-06-21 17:17:28 -07002590 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 /* only need to write one superblock... */
2592 break;
2593 }
NeilBrown46533ff2016-11-18 16:16:11 +11002594 if (md_super_wait(mddev) < 0)
2595 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002596 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002597
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002598 if (mddev_is_clustered(mddev) && ret == 0)
2599 md_cluster_ops->metadata_update_finish(mddev);
2600
NeilBrown850b2b422006-10-03 01:15:46 -07002601 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002602 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2603 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002604 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002605 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002606 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002607 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2608 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002609
NeilBrowndafb20f2012-03-19 12:46:39 +11002610 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002611 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2612 clear_bit(Blocked, &rdev->flags);
2613
2614 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002615 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002616 clear_bit(BlockedBadBlocks, &rdev->flags);
2617 wake_up(&rdev->blocked_wait);
2618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002620EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002622static int add_bound_rdev(struct md_rdev *rdev)
2623{
2624 struct mddev *mddev = rdev->mddev;
2625 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002626 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002627
Shaohua Li87d4d912016-01-06 14:37:14 -08002628 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002629 /* If there is hot_add_disk but no hot_remove_disk
2630 * then added disks for geometry changes,
2631 * and should be added immediately.
2632 */
2633 super_types[mddev->major_version].
2634 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002635 if (add_journal)
2636 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002637 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002638 if (add_journal)
2639 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002640 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002641 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002642 return err;
2643 }
2644 }
2645 sysfs_notify_dirent_safe(rdev->sysfs_state);
2646
Shaohua Li29530792016-12-08 15:48:19 -08002647 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002648 if (mddev->degraded)
2649 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2650 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2651 md_new_event(mddev);
2652 md_wakeup_thread(mddev->thread);
2653 return 0;
2654}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Andre Noll7f6ce762008-03-23 18:34:54 +01002656/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002657 * We want to accept with case. For this we use cmd_match.
2658 */
2659static int cmd_match(const char *cmd, const char *str)
2660{
2661 /* See if cmd, written into a sysfs file, matches
2662 * str. They must either be the same, or cmd can
2663 * have a trailing newline
2664 */
2665 while (*cmd && *str && *cmd == *str) {
2666 cmd++;
2667 str++;
2668 }
2669 if (*cmd == '\n')
2670 cmd++;
2671 if (*str || *cmd)
2672 return 0;
2673 return 1;
2674}
2675
NeilBrown86e6ffd2005-11-08 21:39:24 -08002676struct rdev_sysfs_entry {
2677 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002678 ssize_t (*show)(struct md_rdev *, char *);
2679 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002680};
2681
2682static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002683state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002684{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002685 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002686 size_t len = 0;
Mark Rutland6aa7de02017-10-23 14:07:29 -07002687 unsigned long flags = READ_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002688
NeilBrown758bfc82014-12-15 12:56:59 +11002689 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002690 (!test_bit(ExternalBbl, &flags) &&
2691 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002692 len += sprintf(page+len, "faulty%s", sep);
2693 if (test_bit(In_sync, &flags))
2694 len += sprintf(page+len, "in_sync%s", sep);
2695 if (test_bit(Journal, &flags))
2696 len += sprintf(page+len, "journal%s", sep);
2697 if (test_bit(WriteMostly, &flags))
2698 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002699 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002700 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002701 && !test_bit(Faulty, &flags)))
2702 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002703 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002704 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002705 !test_bit(In_sync, &flags))
2706 len += sprintf(page+len, "spare%s", sep);
2707 if (test_bit(WriteErrorSeen, &flags))
2708 len += sprintf(page+len, "write_error%s", sep);
2709 if (test_bit(WantReplacement, &flags))
2710 len += sprintf(page+len, "want_replacement%s", sep);
2711 if (test_bit(Replacement, &flags))
2712 len += sprintf(page+len, "replacement%s", sep);
2713 if (test_bit(ExternalBbl, &flags))
2714 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002715 if (test_bit(FailFast, &flags))
2716 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002717
2718 if (len)
2719 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002720
NeilBrown86e6ffd2005-11-08 21:39:24 -08002721 return len+sprintf(page+len, "\n");
2722}
2723
NeilBrown45dc2de2006-06-26 00:27:58 -07002724static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002725state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002726{
2727 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002728 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002729 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002730 * writemostly - sets write_mostly
2731 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002732 * blocked - sets the Blocked flags
2733 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002734 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002735 * -insync - clear Insync for a device with a slot assigned,
2736 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002737 * write_error - sets WriteErrorSeen
2738 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002739 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002740 */
2741 int err = -EINVAL;
2742 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2743 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002744 if (test_bit(Faulty, &rdev->flags))
2745 err = 0;
2746 else
2747 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002748 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002749 if (rdev->mddev->pers) {
2750 clear_bit(Blocked, &rdev->flags);
2751 remove_and_add_spares(rdev->mddev, rdev);
2752 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002753 if (rdev->raid_disk >= 0)
2754 err = -EBUSY;
2755 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002756 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002757 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002758 if (mddev_is_clustered(mddev))
2759 err = md_cluster_ops->remove_disk(mddev, rdev);
2760
2761 if (err == 0) {
2762 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002763 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002764 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002765 md_wakeup_thread(mddev->thread);
2766 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002767 md_new_event(mddev);
2768 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002769 }
NeilBrownf6556752006-06-26 00:28:01 -07002770 } else if (cmd_match(buf, "writemostly")) {
2771 set_bit(WriteMostly, &rdev->flags);
2772 err = 0;
2773 } else if (cmd_match(buf, "-writemostly")) {
2774 clear_bit(WriteMostly, &rdev->flags);
2775 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002776 } else if (cmd_match(buf, "blocked")) {
2777 set_bit(Blocked, &rdev->flags);
2778 err = 0;
2779 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002780 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002781 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002782 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002783 /* metadata handler doesn't understand badblocks,
2784 * so we need to fail the device
2785 */
2786 md_error(rdev->mddev, rdev);
2787 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002788 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002789 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002790 wake_up(&rdev->blocked_wait);
2791 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2792 md_wakeup_thread(rdev->mddev->thread);
2793
2794 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002795 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2796 set_bit(In_sync, &rdev->flags);
2797 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11002798 } else if (cmd_match(buf, "failfast")) {
2799 set_bit(FailFast, &rdev->flags);
2800 err = 0;
2801 } else if (cmd_match(buf, "-failfast")) {
2802 clear_bit(FailFast, &rdev->flags);
2803 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002804 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2805 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002806 if (rdev->mddev->pers == NULL) {
2807 clear_bit(In_sync, &rdev->flags);
2808 rdev->saved_raid_disk = rdev->raid_disk;
2809 rdev->raid_disk = -1;
2810 err = 0;
2811 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002812 } else if (cmd_match(buf, "write_error")) {
2813 set_bit(WriteErrorSeen, &rdev->flags);
2814 err = 0;
2815 } else if (cmd_match(buf, "-write_error")) {
2816 clear_bit(WriteErrorSeen, &rdev->flags);
2817 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002818 } else if (cmd_match(buf, "want_replacement")) {
2819 /* Any non-spare device that is not a replacement can
2820 * become want_replacement at any time, but we then need to
2821 * check if recovery is needed.
2822 */
2823 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002824 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002825 !test_bit(Replacement, &rdev->flags))
2826 set_bit(WantReplacement, &rdev->flags);
2827 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2828 md_wakeup_thread(rdev->mddev->thread);
2829 err = 0;
2830 } else if (cmd_match(buf, "-want_replacement")) {
2831 /* Clearing 'want_replacement' is always allowed.
2832 * Once replacements starts it is too late though.
2833 */
2834 err = 0;
2835 clear_bit(WantReplacement, &rdev->flags);
2836 } else if (cmd_match(buf, "replacement")) {
2837 /* Can only set a device as a replacement when array has not
2838 * yet been started. Once running, replacement is automatic
2839 * from spares, or by assigning 'slot'.
2840 */
2841 if (rdev->mddev->pers)
2842 err = -EBUSY;
2843 else {
2844 set_bit(Replacement, &rdev->flags);
2845 err = 0;
2846 }
2847 } else if (cmd_match(buf, "-replacement")) {
2848 /* Similarly, can only clear Replacement before start */
2849 if (rdev->mddev->pers)
2850 err = -EBUSY;
2851 else {
2852 clear_bit(Replacement, &rdev->flags);
2853 err = 0;
2854 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002855 } else if (cmd_match(buf, "re-add")) {
2856 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002857 /* clear_bit is performed _after_ all the devices
2858 * have their local Faulty bit cleared. If any writes
2859 * happen in the meantime in the local node, they
2860 * will land in the local bitmap, which will be synced
2861 * by this node eventually
2862 */
2863 if (!mddev_is_clustered(rdev->mddev) ||
2864 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2865 clear_bit(Faulty, &rdev->flags);
2866 err = add_bound_rdev(rdev);
2867 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002868 } else
2869 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002870 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2871 set_bit(ExternalBbl, &rdev->flags);
2872 rdev->badblocks.shift = 0;
2873 err = 0;
2874 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2875 clear_bit(ExternalBbl, &rdev->flags);
2876 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002877 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002878 if (!err)
2879 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002880 return err ? err : len;
2881}
NeilBrown80ca3a42006-07-10 04:44:18 -07002882static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002883__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002884
2885static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002886errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002887{
2888 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2889}
2890
2891static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002892errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002893{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002894 unsigned int n;
2895 int rv;
2896
2897 rv = kstrtouint(buf, 10, &n);
2898 if (rv < 0)
2899 return rv;
2900 atomic_set(&rdev->corrected_errors, n);
2901 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002902}
2903static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002904__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002905
NeilBrown014236d2006-01-06 00:20:55 -08002906static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002907slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002908{
Shaohua Lif2076e72015-10-08 21:54:12 -07002909 if (test_bit(Journal, &rdev->flags))
2910 return sprintf(page, "journal\n");
2911 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002912 return sprintf(page, "none\n");
2913 else
2914 return sprintf(page, "%d\n", rdev->raid_disk);
2915}
2916
2917static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002918slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002919{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002920 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002921 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002922
Shaohua Lif2076e72015-10-08 21:54:12 -07002923 if (test_bit(Journal, &rdev->flags))
2924 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002925 if (strncmp(buf, "none", 4)==0)
2926 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002927 else {
2928 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2929 if (err < 0)
2930 return err;
2931 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002932 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002933 /* Setting 'slot' on an active array requires also
2934 * updating the 'rd%d' link, and communicating
2935 * with the personality with ->hot_*_disk.
2936 * For now we only support removing
2937 * failed/spare devices. This normally happens automatically,
2938 * but not when the metadata is externally managed.
2939 */
NeilBrownc303da62008-02-06 01:39:51 -08002940 if (rdev->raid_disk == -1)
2941 return -EEXIST;
2942 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002943 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002944 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002945 clear_bit(Blocked, &rdev->flags);
2946 remove_and_add_spares(rdev->mddev, rdev);
2947 if (rdev->raid_disk >= 0)
2948 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002949 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2950 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002951 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002952 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002953 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002954 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002955 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002956
2957 if (rdev->raid_disk != -1)
2958 return -EBUSY;
2959
NeilBrownc6751b22011-02-02 11:57:13 +11002960 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2961 return -EBUSY;
2962
Neil Brown6c2fce22008-06-28 08:31:31 +10002963 if (rdev->mddev->pers->hot_add_disk == NULL)
2964 return -EINVAL;
2965
NeilBrownba1b41b2011-01-14 09:14:34 +11002966 if (slot >= rdev->mddev->raid_disks &&
2967 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2968 return -ENOSPC;
2969
Neil Brown6c2fce22008-06-28 08:31:31 +10002970 rdev->raid_disk = slot;
2971 if (test_bit(In_sync, &rdev->flags))
2972 rdev->saved_raid_disk = slot;
2973 else
2974 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002975 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002976 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002977 err = rdev->mddev->pers->
2978 hot_add_disk(rdev->mddev, rdev);
2979 if (err) {
2980 rdev->raid_disk = -1;
2981 return err;
2982 } else
2983 sysfs_notify_dirent_safe(rdev->sysfs_state);
2984 if (sysfs_link_rdev(rdev->mddev, rdev))
2985 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002986 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002987 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002988 if (slot >= rdev->mddev->raid_disks &&
2989 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002990 return -ENOSPC;
2991 rdev->raid_disk = slot;
2992 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002993 clear_bit(Faulty, &rdev->flags);
2994 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002995 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002996 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002997 }
NeilBrown014236d2006-01-06 00:20:55 -08002998 return len;
2999}
3000
NeilBrown014236d2006-01-06 00:20:55 -08003001static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07003002__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08003003
NeilBrown93c8cad2006-01-06 00:20:56 -08003004static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003005offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08003006{
Andrew Morton6961ece2006-01-06 00:20:59 -08003007 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08003008}
3009
3010static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003011offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08003012{
NeilBrownc6563a82012-05-21 09:27:00 +10003013 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003014 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003015 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10003016 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003017 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003018 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08003019 /* Must set offset before size, so overlap checks
3020 * can be sane */
3021 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08003022 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10003023 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08003024 return len;
3025}
3026
3027static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07003028__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08003029
NeilBrownc6563a82012-05-21 09:27:00 +10003030static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3031{
3032 return sprintf(page, "%llu\n",
3033 (unsigned long long)rdev->new_data_offset);
3034}
3035
3036static ssize_t new_offset_store(struct md_rdev *rdev,
3037 const char *buf, size_t len)
3038{
3039 unsigned long long new_offset;
3040 struct mddev *mddev = rdev->mddev;
3041
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003042 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10003043 return -EINVAL;
3044
NeilBrownf851b602014-12-11 10:02:10 +11003045 if (mddev->sync_thread ||
3046 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10003047 return -EBUSY;
3048 if (new_offset == rdev->data_offset)
3049 /* reset is always permitted */
3050 ;
3051 else if (new_offset > rdev->data_offset) {
3052 /* must not push array size beyond rdev_sectors */
3053 if (new_offset - rdev->data_offset
3054 + mddev->dev_sectors > rdev->sectors)
3055 return -E2BIG;
3056 }
3057 /* Metadata worries about other space details. */
3058
3059 /* decreasing the offset is inconsistent with a backwards
3060 * reshape.
3061 */
3062 if (new_offset < rdev->data_offset &&
3063 mddev->reshape_backwards)
3064 return -EINVAL;
3065 /* Increasing offset is inconsistent with forwards
3066 * reshape. reshape_direction should be set to
3067 * 'backwards' first.
3068 */
3069 if (new_offset > rdev->data_offset &&
3070 !mddev->reshape_backwards)
3071 return -EINVAL;
3072
3073 if (mddev->pers && mddev->persistent &&
3074 !super_types[mddev->major_version]
3075 .allow_new_offset(rdev, new_offset))
3076 return -E2BIG;
3077 rdev->new_data_offset = new_offset;
3078 if (new_offset > rdev->data_offset)
3079 mddev->reshape_backwards = 1;
3080 else if (new_offset < rdev->data_offset)
3081 mddev->reshape_backwards = 0;
3082
3083 return len;
3084}
3085static struct rdev_sysfs_entry rdev_new_offset =
3086__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3087
NeilBrown83303b62006-01-06 00:21:06 -08003088static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003089rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003090{
Andre Nolldd8ac332009-03-31 14:33:13 +11003091 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003092}
3093
NeilBrownc5d79ad2008-02-06 01:39:54 -08003094static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3095{
3096 /* check if two start/length pairs overlap */
3097 if (s1+l1 <= s2)
3098 return 0;
3099 if (s2+l2 <= s1)
3100 return 0;
3101 return 1;
3102}
3103
Dan Williamsb522adc2009-03-31 15:00:31 +11003104static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3105{
3106 unsigned long long blocks;
3107 sector_t new;
3108
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003109 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003110 return -EINVAL;
3111
3112 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3113 return -EINVAL; /* sector conversion overflow */
3114
3115 new = blocks * 2;
3116 if (new != blocks * 2)
3117 return -EINVAL; /* unsigned long long to sector_t overflow */
3118
3119 *sectors = new;
3120 return 0;
3121}
3122
NeilBrown83303b62006-01-06 00:21:06 -08003123static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003124rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003125{
NeilBrownfd01b882011-10-11 16:47:53 +11003126 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003127 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003128 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003129
Shaohua Lif2076e72015-10-08 21:54:12 -07003130 if (test_bit(Journal, &rdev->flags))
3131 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003132 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003133 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003134 if (rdev->data_offset != rdev->new_data_offset)
3135 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003136 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003137 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003138 sectors = super_types[my_mddev->major_version].
3139 rdev_size_change(rdev, sectors);
3140 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003141 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003142 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003143 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003144 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003145 if (!my_mddev->pers->resize)
3146 /* Cannot change size for RAID0 or Linear etc */
3147 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003148 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003149 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003150 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003151
Andre Nolldd8ac332009-03-31 14:33:13 +11003152 rdev->sectors = sectors;
3153 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003154 /* Need to check that all other rdevs with the same
3155 * ->bdev do not overlap. 'rcu' is sufficient to walk
3156 * the rdev lists safely.
3157 * This check does not provide a hard guarantee, it
3158 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003159 */
NeilBrownfd01b882011-10-11 16:47:53 +11003160 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003161 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003162 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003163
NeilBrown8b1afc32014-09-29 15:33:20 +10003164 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003165 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003166 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003167
NeilBrowndafb20f2012-03-19 12:46:39 +11003168 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003169 if (rdev->bdev == rdev2->bdev &&
3170 rdev != rdev2 &&
3171 overlaps(rdev->data_offset, rdev->sectors,
3172 rdev2->data_offset,
3173 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003174 overlap = 1;
3175 break;
3176 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003177 if (overlap) {
3178 mddev_put(mddev);
3179 break;
3180 }
3181 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003182 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003183 if (overlap) {
3184 /* Someone else could have slipped in a size
3185 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003186 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003187 * safe, and trust userspace not to race with
3188 * itself
3189 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003190 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003191 return -EBUSY;
3192 }
3193 }
NeilBrown83303b62006-01-06 00:21:06 -08003194 return len;
3195}
3196
3197static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003198__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003199
NeilBrown3cb03002011-10-11 16:45:26 +11003200static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003201{
3202 unsigned long long recovery_start = rdev->recovery_offset;
3203
3204 if (test_bit(In_sync, &rdev->flags) ||
3205 recovery_start == MaxSector)
3206 return sprintf(page, "none\n");
3207
3208 return sprintf(page, "%llu\n", recovery_start);
3209}
3210
NeilBrown3cb03002011-10-11 16:45:26 +11003211static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003212{
3213 unsigned long long recovery_start;
3214
3215 if (cmd_match(buf, "none"))
3216 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003217 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003218 return -EINVAL;
3219
3220 if (rdev->mddev->pers &&
3221 rdev->raid_disk >= 0)
3222 return -EBUSY;
3223
3224 rdev->recovery_offset = recovery_start;
3225 if (recovery_start == MaxSector)
3226 set_bit(In_sync, &rdev->flags);
3227 else
3228 clear_bit(In_sync, &rdev->flags);
3229 return len;
3230}
3231
3232static struct rdev_sysfs_entry rdev_recovery_start =
3233__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3234
Vishal Vermafc974ee2015-12-24 19:20:34 -07003235/* sysfs access to bad-blocks list.
3236 * We present two files.
3237 * 'bad-blocks' lists sector numbers and lengths of ranges that
3238 * are recorded as bad. The list is truncated to fit within
3239 * the one-page limit of sysfs.
3240 * Writing "sector length" to this file adds an acknowledged
3241 * bad block list.
3242 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3243 * been acknowledged. Writing to this file adds bad blocks
3244 * without acknowledging them. This is largely for testing.
3245 */
NeilBrown3cb03002011-10-11 16:45:26 +11003246static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003247{
3248 return badblocks_show(&rdev->badblocks, page, 0);
3249}
NeilBrown3cb03002011-10-11 16:45:26 +11003250static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003251{
NeilBrownde393cd2011-07-28 11:31:48 +10003252 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3253 /* Maybe that ack was all we needed */
3254 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3255 wake_up(&rdev->blocked_wait);
3256 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003257}
3258static struct rdev_sysfs_entry rdev_bad_blocks =
3259__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3260
NeilBrown3cb03002011-10-11 16:45:26 +11003261static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003262{
3263 return badblocks_show(&rdev->badblocks, page, 1);
3264}
NeilBrown3cb03002011-10-11 16:45:26 +11003265static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003266{
3267 return badblocks_store(&rdev->badblocks, page, len, 1);
3268}
3269static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3270__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3271
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003272static ssize_t
3273ppl_sector_show(struct md_rdev *rdev, char *page)
3274{
3275 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3276}
3277
3278static ssize_t
3279ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3280{
3281 unsigned long long sector;
3282
3283 if (kstrtoull(buf, 10, &sector) < 0)
3284 return -EINVAL;
3285 if (sector != (sector_t)sector)
3286 return -EINVAL;
3287
3288 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3289 rdev->raid_disk >= 0)
3290 return -EBUSY;
3291
3292 if (rdev->mddev->persistent) {
3293 if (rdev->mddev->major_version == 0)
3294 return -EINVAL;
3295 if ((sector > rdev->sb_start &&
3296 sector - rdev->sb_start > S16_MAX) ||
3297 (sector < rdev->sb_start &&
3298 rdev->sb_start - sector > -S16_MIN))
3299 return -EINVAL;
3300 rdev->ppl.offset = sector - rdev->sb_start;
3301 } else if (!rdev->mddev->external) {
3302 return -EBUSY;
3303 }
3304 rdev->ppl.sector = sector;
3305 return len;
3306}
3307
3308static struct rdev_sysfs_entry rdev_ppl_sector =
3309__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3310
3311static ssize_t
3312ppl_size_show(struct md_rdev *rdev, char *page)
3313{
3314 return sprintf(page, "%u\n", rdev->ppl.size);
3315}
3316
3317static ssize_t
3318ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3319{
3320 unsigned int size;
3321
3322 if (kstrtouint(buf, 10, &size) < 0)
3323 return -EINVAL;
3324
3325 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3326 rdev->raid_disk >= 0)
3327 return -EBUSY;
3328
3329 if (rdev->mddev->persistent) {
3330 if (rdev->mddev->major_version == 0)
3331 return -EINVAL;
3332 if (size > U16_MAX)
3333 return -EINVAL;
3334 } else if (!rdev->mddev->external) {
3335 return -EBUSY;
3336 }
3337 rdev->ppl.size = size;
3338 return len;
3339}
3340
3341static struct rdev_sysfs_entry rdev_ppl_size =
3342__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3343
NeilBrown86e6ffd2005-11-08 21:39:24 -08003344static struct attribute *rdev_default_attrs[] = {
3345 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003346 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003347 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003348 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003349 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003350 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003351 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003352 &rdev_bad_blocks.attr,
3353 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003354 &rdev_ppl_sector.attr,
3355 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003356 NULL,
3357};
3358static ssize_t
3359rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3360{
3361 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003362 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003363
3364 if (!entry->show)
3365 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003366 if (!rdev->mddev)
3367 return -EBUSY;
3368 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003369}
3370
3371static ssize_t
3372rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3373 const char *page, size_t length)
3374{
3375 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003376 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003377 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003378 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003379
3380 if (!entry->store)
3381 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003382 if (!capable(CAP_SYS_ADMIN))
3383 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003384 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003385 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003386 if (rdev->mddev == NULL)
3387 rv = -EBUSY;
3388 else
3389 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003390 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003391 }
3392 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003393}
3394
3395static void rdev_free(struct kobject *ko)
3396{
NeilBrown3cb03002011-10-11 16:45:26 +11003397 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003398 kfree(rdev);
3399}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003400static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003401 .show = rdev_attr_show,
3402 .store = rdev_attr_store,
3403};
3404static struct kobj_type rdev_ktype = {
3405 .release = rdev_free,
3406 .sysfs_ops = &rdev_sysfs_ops,
3407 .default_attrs = rdev_default_attrs,
3408};
3409
NeilBrown3cb03002011-10-11 16:45:26 +11003410int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003411{
3412 rdev->desc_nr = -1;
3413 rdev->saved_raid_disk = -1;
3414 rdev->raid_disk = -1;
3415 rdev->flags = 0;
3416 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003417 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003418 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003419 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003420 rdev->sb_loaded = 0;
3421 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003422 atomic_set(&rdev->nr_pending, 0);
3423 atomic_set(&rdev->read_errors, 0);
3424 atomic_set(&rdev->corrected_errors, 0);
3425
3426 INIT_LIST_HEAD(&rdev->same_set);
3427 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003428
3429 /* Add space to store bad block list.
3430 * This reserves the space even on arrays where it cannot
3431 * be used - I wonder if that matters
3432 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003433 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003434}
3435EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436/*
3437 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3438 *
3439 * mark the device faulty if:
3440 *
3441 * - the device is nonexistent (zero size)
3442 * - the device has no valid superblock
3443 *
3444 * a faulty rdev _never_ has rdev->sb set.
3445 */
NeilBrown3cb03002011-10-11 16:45:26 +11003446static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447{
3448 char b[BDEVNAME_SIZE];
3449 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003450 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 sector_t size;
3452
NeilBrown9ffae0c2006-01-06 00:20:32 -08003453 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003454 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
NeilBrown2230dfe2011-07-28 11:31:46 +10003457 err = md_rdev_init(rdev);
3458 if (err)
3459 goto abort_free;
3460 err = alloc_disk_sb(rdev);
3461 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 goto abort_free;
3463
NeilBrownc5d79ad2008-02-06 01:39:54 -08003464 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 if (err)
3466 goto abort_free;
3467
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003468 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003469
Mike Snitzer77304d22010-11-08 14:39:12 +01003470 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003472 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 bdevname(rdev->bdev,b));
3474 err = -EINVAL;
3475 goto abort_free;
3476 }
3477
3478 if (super_format >= 0) {
3479 err = super_types[super_format].
3480 load_super(rdev, NULL, super_minor);
3481 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003482 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003483 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003484 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 goto abort_free;
3486 }
3487 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003488 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 bdevname(rdev->bdev,b));
3490 goto abort_free;
3491 }
3492 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003493
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 return rdev;
3495
3496abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003497 if (rdev->bdev)
3498 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003499 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 kfree(rdev);
3501 return ERR_PTR(err);
3502}
3503
3504/*
3505 * Check a full RAID array for plausibility
3506 */
3507
NeilBrownf72ffdd2014-09-30 14:23:59 +10003508static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509{
3510 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003511 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 char b[BDEVNAME_SIZE];
3513
3514 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003515 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 switch (super_types[mddev->major_version].
3517 load_super(rdev, freshest, mddev->minor_version)) {
3518 case 1:
3519 freshest = rdev;
3520 break;
3521 case 0:
3522 break;
3523 default:
NeilBrown9d487392016-11-02 14:16:49 +11003524 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003526 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 }
3528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 super_types[mddev->major_version].
3530 validate_super(mddev, freshest);
3531
3532 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003533 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003534 if (mddev->max_disks &&
3535 (rdev->desc_nr >= mddev->max_disks ||
3536 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003537 pr_warn("md: %s: %s: only %d devices permitted\n",
3538 mdname(mddev), bdevname(rdev->bdev, b),
3539 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003540 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003541 continue;
3542 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003543 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 if (super_types[mddev->major_version].
3545 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003546 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003548 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 continue;
3550 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 if (mddev->level == LEVEL_MULTIPATH) {
3553 rdev->desc_nr = i++;
3554 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003555 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003556 } else if (rdev->raid_disk >=
3557 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3558 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003559 rdev->raid_disk = -1;
3560 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 }
3562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563}
3564
NeilBrown72e02072009-12-14 12:49:55 +11003565/* Read a fixed-point number.
3566 * Numbers in sysfs attributes should be in "standard" units where
3567 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003568 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003569 * milliseconds or jiffies.
3570 * This function takes a decimal number with a possible fractional
3571 * component, and produces an integer which is the result of
3572 * multiplying that number by 10^'scale'.
3573 * all without any floating-point arithmetic.
3574 */
3575int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3576{
3577 unsigned long result = 0;
3578 long decimals = -1;
3579 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3580 if (*cp == '.')
3581 decimals = 0;
3582 else if (decimals < scale) {
3583 unsigned int value;
3584 value = *cp - '0';
3585 result = result * 10 + value;
3586 if (decimals >= 0)
3587 decimals++;
3588 }
3589 cp++;
3590 }
3591 if (*cp == '\n')
3592 cp++;
3593 if (*cp)
3594 return -EINVAL;
3595 if (decimals < 0)
3596 decimals = 0;
3597 while (decimals < scale) {
3598 result *= 10;
3599 decimals ++;
3600 }
3601 *res = result;
3602 return 0;
3603}
3604
NeilBrowneae17012005-11-08 21:39:23 -08003605static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003606safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003607{
3608 int msec = (mddev->safemode_delay*1000)/HZ;
3609 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3610}
3611static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003612safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003613{
NeilBrown16f17b32006-06-26 00:27:37 -07003614 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003615
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003616 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003617 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003618 return -EINVAL;
3619 }
3620
NeilBrown72e02072009-12-14 12:49:55 +11003621 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003622 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003623 if (msec == 0)
3624 mddev->safemode_delay = 0;
3625 else {
NeilBrown19052c02008-08-05 15:54:13 +10003626 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003627 unsigned long new_delay = (msec*HZ)/1000;
3628
3629 if (new_delay == 0)
3630 new_delay = 1;
3631 mddev->safemode_delay = new_delay;
3632 if (new_delay < old_delay || old_delay == 0)
3633 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003634 }
3635 return len;
3636}
3637static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003638__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003639
3640static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003641level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003642{
NeilBrown36d091f2014-12-15 12:56:58 +11003643 struct md_personality *p;
3644 int ret;
3645 spin_lock(&mddev->lock);
3646 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003647 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003648 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003649 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003650 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003651 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003652 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003653 else
NeilBrown36d091f2014-12-15 12:56:58 +11003654 ret = 0;
3655 spin_unlock(&mddev->lock);
3656 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003657}
3658
NeilBrownd9d166c2006-01-06 00:20:51 -08003659static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003660level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003661{
Dan Williamsf2859af2010-05-02 10:04:16 -07003662 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003663 ssize_t rv;
3664 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003665 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003666 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003667 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003668 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003669
NeilBrown67918752014-12-15 12:57:01 +11003670 if (slen == 0 || slen >= sizeof(clevel))
3671 return -EINVAL;
3672
3673 rv = mddev_lock(mddev);
3674 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003675 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003676
3677 if (mddev->pers == NULL) {
3678 strncpy(mddev->clevel, buf, slen);
3679 if (mddev->clevel[slen-1] == '\n')
3680 slen--;
3681 mddev->clevel[slen] = 0;
3682 mddev->level = LEVEL_NONE;
3683 rv = len;
3684 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003685 }
NeilBrown67918752014-12-15 12:57:01 +11003686 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003687 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003688 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003689
3690 /* request to change the personality. Need to ensure:
3691 * - array is not engaged in resync/recovery/reshape
3692 * - old personality can be suspended
3693 * - new personality will access other array.
3694 */
3695
NeilBrown67918752014-12-15 12:57:01 +11003696 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003697 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003698 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003699 mddev->reshape_position != MaxSector ||
3700 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003701 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003702
NeilBrown67918752014-12-15 12:57:01 +11003703 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003704 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003705 pr_warn("md: %s: %s does not support online personality change\n",
3706 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003707 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003708 }
3709
3710 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003711 strncpy(clevel, buf, slen);
3712 if (clevel[slen-1] == '\n')
3713 slen--;
3714 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003715 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003716 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003717
Dan Williamsf2859af2010-05-02 10:04:16 -07003718 if (request_module("md-%s", clevel) != 0)
3719 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003720 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003721 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003722 if (!pers || !try_module_get(pers->owner)) {
3723 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003724 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003725 rv = -EINVAL;
3726 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003727 }
3728 spin_unlock(&pers_lock);
3729
3730 if (pers == mddev->pers) {
3731 /* Nothing to do! */
3732 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003733 rv = len;
3734 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003735 }
3736 if (!pers->takeover) {
3737 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003738 pr_warn("md: %s: %s does not support personality takeover\n",
3739 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003740 rv = -EINVAL;
3741 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003742 }
3743
NeilBrowndafb20f2012-03-19 12:46:39 +11003744 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003745 rdev->new_raid_disk = rdev->raid_disk;
3746
NeilBrown245f46c2009-03-31 14:39:39 +11003747 /* ->takeover must set new_* and/or delta_disks
3748 * if it succeeds, and may set them when it fails.
3749 */
3750 priv = pers->takeover(mddev);
3751 if (IS_ERR(priv)) {
3752 mddev->new_level = mddev->level;
3753 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003754 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003755 mddev->raid_disks -= mddev->delta_disks;
3756 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003757 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003758 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003759 pr_warn("md: %s: %s would not accept array\n",
3760 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003761 rv = PTR_ERR(priv);
3762 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003763 }
3764
3765 /* Looks like we have a winner */
3766 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003767 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003768
3769 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003770 oldpers = mddev->pers;
3771 oldpriv = mddev->private;
3772 mddev->pers = pers;
3773 mddev->private = priv;
3774 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3775 mddev->level = mddev->new_level;
3776 mddev->layout = mddev->new_layout;
3777 mddev->chunk_sectors = mddev->new_chunk_sectors;
3778 mddev->delta_disks = 0;
3779 mddev->reshape_backwards = 0;
3780 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003781 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003782
NeilBrowndb721d32014-12-15 12:56:58 +11003783 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003784 mddev->external) {
3785 /* We are converting from a no-redundancy array
3786 * to a redundancy array and metadata is managed
3787 * externally so we need to be sure that writes
3788 * won't block due to a need to transition
3789 * clean->dirty
3790 * until external management is started.
3791 */
3792 mddev->in_sync = 0;
3793 mddev->safemode_delay = 0;
3794 mddev->safemode = 0;
3795 }
3796
NeilBrowndb721d32014-12-15 12:56:58 +11003797 oldpers->free(mddev, oldpriv);
3798
3799 if (oldpers->sync_request == NULL &&
3800 pers->sync_request != NULL) {
3801 /* need to add the md_redundancy_group */
3802 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11003803 pr_warn("md: cannot register extra attributes for %s\n",
3804 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11003805 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3806 }
3807 if (oldpers->sync_request != NULL &&
3808 pers->sync_request == NULL) {
3809 /* need to remove the md_redundancy_group */
3810 if (mddev->to_remove == NULL)
3811 mddev->to_remove = &md_redundancy_group;
3812 }
3813
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003814 module_put(oldpers->owner);
3815
NeilBrowndafb20f2012-03-19 12:46:39 +11003816 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003817 if (rdev->raid_disk < 0)
3818 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003819 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003820 rdev->new_raid_disk = -1;
3821 if (rdev->new_raid_disk == rdev->raid_disk)
3822 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003823 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003824 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003825 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003826 if (rdev->raid_disk < 0)
3827 continue;
3828 if (rdev->new_raid_disk == rdev->raid_disk)
3829 continue;
3830 rdev->raid_disk = rdev->new_raid_disk;
3831 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003832 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003833 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003834 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11003835 pr_warn("md: cannot register rd%d for %s after level change\n",
3836 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003837 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003838 }
3839
NeilBrowndb721d32014-12-15 12:56:58 +11003840 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003841 /* this is now an array without redundancy, so
3842 * it must always be in_sync
3843 */
3844 mddev->in_sync = 1;
3845 del_timer_sync(&mddev->safemode_timer);
3846 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003847 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003848 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08003849 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003850 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003851 if (!mddev->thread)
3852 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003853 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003854 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003855 rv = len;
3856out_unlock:
3857 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003858 return rv;
3859}
3860
3861static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003862__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003863
NeilBrownd4dbd022006-06-26 00:27:59 -07003864static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003865layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003866{
3867 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003868 if (mddev->reshape_position != MaxSector &&
3869 mddev->layout != mddev->new_layout)
3870 return sprintf(page, "%d (%d)\n",
3871 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003872 return sprintf(page, "%d\n", mddev->layout);
3873}
3874
3875static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003876layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003877{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003878 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003879 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003880
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003881 err = kstrtouint(buf, 10, &n);
3882 if (err < 0)
3883 return err;
NeilBrown67918752014-12-15 12:57:01 +11003884 err = mddev_lock(mddev);
3885 if (err)
3886 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003887
NeilBrownb3546032009-03-31 14:56:41 +11003888 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003889 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003890 err = -EBUSY;
3891 else if (mddev->ro)
3892 err = -EROFS;
3893 else {
3894 mddev->new_layout = n;
3895 err = mddev->pers->check_reshape(mddev);
3896 if (err)
3897 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003898 }
NeilBrownb3546032009-03-31 14:56:41 +11003899 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003900 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003901 if (mddev->reshape_position == MaxSector)
3902 mddev->layout = n;
3903 }
NeilBrown67918752014-12-15 12:57:01 +11003904 mddev_unlock(mddev);
3905 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003906}
3907static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003908__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003909
NeilBrowneae17012005-11-08 21:39:23 -08003910static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003911raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003912{
NeilBrownbb636542005-11-08 21:39:45 -08003913 if (mddev->raid_disks == 0)
3914 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003915 if (mddev->reshape_position != MaxSector &&
3916 mddev->delta_disks != 0)
3917 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3918 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003919 return sprintf(page, "%d\n", mddev->raid_disks);
3920}
3921
NeilBrownfd01b882011-10-11 16:47:53 +11003922static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003923
3924static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003925raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003926{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003927 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003928 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003929
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003930 err = kstrtouint(buf, 10, &n);
3931 if (err < 0)
3932 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003933
NeilBrown67918752014-12-15 12:57:01 +11003934 err = mddev_lock(mddev);
3935 if (err)
3936 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003937 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003938 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003939 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003940 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003941 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003942
NeilBrown67918752014-12-15 12:57:01 +11003943 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003944 rdev_for_each(rdev, mddev) {
3945 if (olddisks < n &&
3946 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003947 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003948 if (olddisks > n &&
3949 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003950 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003951 }
NeilBrown67918752014-12-15 12:57:01 +11003952 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003953 mddev->delta_disks = n - olddisks;
3954 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003955 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003956 } else
NeilBrownda943b992006-01-06 00:20:54 -08003957 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003958out_unlock:
3959 mddev_unlock(mddev);
3960 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003961}
3962static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003963__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003964
NeilBrown24dd4692005-11-08 21:39:26 -08003965static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003966chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003967{
NeilBrown08a02ec2007-05-09 02:35:38 -07003968 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003969 mddev->chunk_sectors != mddev->new_chunk_sectors)
3970 return sprintf(page, "%d (%d)\n",
3971 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003972 mddev->chunk_sectors << 9);
3973 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003974}
3975
3976static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003977chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003978{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003979 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003980 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003981
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003982 err = kstrtoul(buf, 10, &n);
3983 if (err < 0)
3984 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003985
NeilBrown67918752014-12-15 12:57:01 +11003986 err = mddev_lock(mddev);
3987 if (err)
3988 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003989 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003990 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003991 err = -EBUSY;
3992 else if (mddev->ro)
3993 err = -EROFS;
3994 else {
3995 mddev->new_chunk_sectors = n >> 9;
3996 err = mddev->pers->check_reshape(mddev);
3997 if (err)
3998 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003999 }
NeilBrownb3546032009-03-31 14:56:41 +11004000 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10004001 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004002 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10004003 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004004 }
NeilBrown67918752014-12-15 12:57:01 +11004005 mddev_unlock(mddev);
4006 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08004007}
4008static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004009__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08004010
NeilBrowna94213b2006-06-26 00:28:00 -07004011static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004012resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07004013{
NeilBrownd1a7c502009-03-31 15:24:32 +11004014 if (mddev->recovery_cp == MaxSector)
4015 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07004016 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4017}
4018
4019static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004020resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07004021{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004022 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11004023 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004024
4025 if (cmd_match(buf, "none"))
4026 n = MaxSector;
4027 else {
4028 err = kstrtoull(buf, 10, &n);
4029 if (err < 0)
4030 return err;
4031 if (n != (sector_t)n)
4032 return -EINVAL;
4033 }
NeilBrowna94213b2006-06-26 00:28:00 -07004034
NeilBrown67918752014-12-15 12:57:01 +11004035 err = mddev_lock(mddev);
4036 if (err)
4037 return err;
NeilBrownb0986362011-05-11 15:52:21 +10004038 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11004039 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07004040
NeilBrown67918752014-12-15 12:57:01 +11004041 if (!err) {
4042 mddev->recovery_cp = n;
4043 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08004044 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004045 }
4046 mddev_unlock(mddev);
4047 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07004048}
4049static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10004050__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4051 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07004052
NeilBrown9e653b62006-06-26 00:27:58 -07004053/*
4054 * The array state can be:
4055 *
4056 * clear
4057 * No devices, no size, no level
4058 * Equivalent to STOP_ARRAY ioctl
4059 * inactive
4060 * May have some settings, but array is not active
4061 * all IO results in error
4062 * When written, doesn't tear down array, but just stops it
4063 * suspended (not supported yet)
4064 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01004065 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07004066 * readonly
4067 * no resync can happen. no superblocks get written.
4068 * write requests fail
4069 * read-auto
4070 * like readonly, but behaves like 'clean' on a write request.
4071 *
4072 * clean - no pending writes, but otherwise active.
4073 * When written to inactive array, starts without resync
4074 * If a write request arrives then
4075 * if metadata is known, mark 'dirty' and switch to 'active'.
4076 * if not known, block and switch to write-pending
4077 * If written to an active array that has pending writes, then fails.
4078 * active
4079 * fully active: IO and resync can be happening.
4080 * When written to inactive array, starts with resync
4081 *
4082 * write-pending
4083 * clean, but writes are blocked waiting for 'active' to be written.
4084 *
4085 * active-idle
4086 * like active, but no writes have been seen for a while (100msec).
4087 *
4088 */
4089enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4090 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004091static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004092 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4093 "write-pending", "active-idle", NULL };
4094
4095static int match_word(const char *word, char **list)
4096{
4097 int n;
4098 for (n=0; list[n]; n++)
4099 if (cmd_match(word, list[n]))
4100 break;
4101 return n;
4102}
4103
4104static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004105array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004106{
4107 enum array_state st = inactive;
4108
4109 if (mddev->pers)
4110 switch(mddev->ro) {
4111 case 1:
4112 st = readonly;
4113 break;
4114 case 2:
4115 st = read_auto;
4116 break;
4117 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004118 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004119 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004120 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004121 else if (mddev->in_sync)
4122 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004123 else if (mddev->safemode)
4124 st = active_idle;
4125 else
4126 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004127 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004128 }
4129 else {
4130 if (list_empty(&mddev->disks) &&
4131 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004132 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004133 st = clear;
4134 else
4135 st = inactive;
4136 }
4137 return sprintf(page, "%s\n", array_states[st]);
4138}
4139
NeilBrownf72ffdd2014-09-30 14:23:59 +10004140static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4141static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4142static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004143static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004144
4145static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004146array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004147{
NeilBrown6497709b2017-03-15 14:05:14 +11004148 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004149 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004150
4151 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4152 /* don't take reconfig_mutex when toggling between
4153 * clean and active
4154 */
4155 spin_lock(&mddev->lock);
4156 if (st == active) {
4157 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004158 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004159 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004160 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004161 } else /* st == clean */ {
4162 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004163 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004164 err = -EBUSY;
4165 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004166 if (!err)
4167 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004168 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004169 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004170 }
4171 err = mddev_lock(mddev);
4172 if (err)
4173 return err;
4174 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004175 switch(st) {
4176 case bad_word:
4177 break;
4178 case clear:
4179 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004180 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004181 break;
4182 case inactive:
4183 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004184 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004185 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004186 else
NeilBrowne6910632008-02-06 01:39:51 -08004187 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004188 break;
4189 case suspended:
4190 break; /* not supported yet */
4191 case readonly:
4192 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004193 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004194 else {
4195 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004196 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004197 err = do_md_run(mddev);
4198 }
4199 break;
4200 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004201 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004202 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004203 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004204 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004205 err = restart_array(mddev);
4206 if (err == 0) {
4207 mddev->ro = 2;
4208 set_disk_ro(mddev->gendisk, 0);
4209 }
NeilBrown9e653b62006-06-26 00:27:58 -07004210 } else {
4211 mddev->ro = 2;
4212 err = do_md_run(mddev);
4213 }
4214 break;
4215 case clean:
4216 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004217 err = restart_array(mddev);
4218 if (err)
4219 break;
NeilBrown85572d72014-12-15 12:56:56 +11004220 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004221 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004222 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004223 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004224 } else
4225 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004226 break;
4227 case active:
4228 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004229 err = restart_array(mddev);
4230 if (err)
4231 break;
Shaohua Li29530792016-12-08 15:48:19 -08004232 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004233 wake_up(&mddev->sb_wait);
4234 err = 0;
4235 } else {
4236 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004237 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004238 err = do_md_run(mddev);
4239 }
4240 break;
4241 case write_pending:
4242 case active_idle:
4243 /* these cannot be set */
4244 break;
4245 }
NeilBrown67918752014-12-15 12:57:01 +11004246
4247 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004248 if (mddev->hold_active == UNTIL_IOCTL)
4249 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004250 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004251 }
NeilBrown67918752014-12-15 12:57:01 +11004252 mddev_unlock(mddev);
4253 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004254}
NeilBrown80ca3a42006-07-10 04:44:18 -07004255static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004256__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004257
NeilBrown6d7ff7382006-01-06 00:21:16 -08004258static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004259max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004260 return sprintf(page, "%d\n",
4261 atomic_read(&mddev->max_corr_read_errors));
4262}
4263
4264static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004265max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004266{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004267 unsigned int n;
4268 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004269
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004270 rv = kstrtouint(buf, 10, &n);
4271 if (rv < 0)
4272 return rv;
4273 atomic_set(&mddev->max_corr_read_errors, n);
4274 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004275}
4276
4277static struct md_sysfs_entry max_corr_read_errors =
4278__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4279 max_corrected_read_errors_store);
4280
4281static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004282null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004283{
4284 return -EINVAL;
4285}
4286
4287static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004288new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004289{
4290 /* buf must be %d:%d\n? giving major and minor numbers */
4291 /* The new device is added to the array.
4292 * If the array has a persistent superblock, we read the
4293 * superblock to initialise info and check validity.
4294 * Otherwise, only checking done is that in bind_rdev_to_array,
4295 * which mainly checks size.
4296 */
4297 char *e;
4298 int major = simple_strtoul(buf, &e, 10);
4299 int minor;
4300 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004301 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004302 int err;
4303
4304 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4305 return -EINVAL;
4306 minor = simple_strtoul(e+1, &e, 10);
4307 if (*e && *e != '\n')
4308 return -EINVAL;
4309 dev = MKDEV(major, minor);
4310 if (major != MAJOR(dev) ||
4311 minor != MINOR(dev))
4312 return -EOVERFLOW;
4313
NeilBrown67918752014-12-15 12:57:01 +11004314 flush_workqueue(md_misc_wq);
4315
4316 err = mddev_lock(mddev);
4317 if (err)
4318 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004319 if (mddev->persistent) {
4320 rdev = md_import_device(dev, mddev->major_version,
4321 mddev->minor_version);
4322 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004323 struct md_rdev *rdev0
4324 = list_entry(mddev->disks.next,
4325 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004326 err = super_types[mddev->major_version]
4327 .load_super(rdev, rdev0, mddev->minor_version);
4328 if (err < 0)
4329 goto out;
4330 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004331 } else if (mddev->external)
4332 rdev = md_import_device(dev, -2, -1);
4333 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004334 rdev = md_import_device(dev, -1, -1);
4335
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004336 if (IS_ERR(rdev)) {
4337 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004338 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004339 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004340 err = bind_rdev_to_array(rdev, mddev);
4341 out:
4342 if (err)
4343 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004344 mddev_unlock(mddev);
Alexey Obitotskiy5492c462017-07-28 15:49:25 +02004345 if (!err)
4346 md_new_event(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004347 return err ? err : len;
4348}
4349
4350static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004351__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004352
4353static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004354bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004355{
4356 char *end;
4357 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004358 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004359
NeilBrown67918752014-12-15 12:57:01 +11004360 err = mddev_lock(mddev);
4361 if (err)
4362 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004363 if (!mddev->bitmap)
4364 goto out;
4365 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4366 while (*buf) {
4367 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4368 if (buf == end) break;
4369 if (*end == '-') { /* range */
4370 buf = end + 1;
4371 end_chunk = simple_strtoul(buf, &end, 0);
4372 if (buf == end) break;
4373 }
4374 if (*end && !isspace(*end)) break;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004375 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004376 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004377 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004378 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
Paul Clements9b1d1da2006-10-03 01:15:49 -07004379out:
NeilBrown67918752014-12-15 12:57:01 +11004380 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004381 return len;
4382}
4383
4384static struct md_sysfs_entry md_bitmap =
4385__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4386
4387static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004388size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004389{
Andre Noll58c0fed2009-03-31 14:33:13 +11004390 return sprintf(page, "%llu\n",
4391 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004392}
4393
NeilBrownfd01b882011-10-11 16:47:53 +11004394static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004395
4396static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004397size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004398{
4399 /* If array is inactive, we can reduce the component size, but
4400 * not increase it (except from 0).
4401 * If array is active, we can try an on-line resize
4402 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004403 sector_t sectors;
4404 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004405
Andre Noll58c0fed2009-03-31 14:33:13 +11004406 if (err < 0)
4407 return err;
NeilBrown67918752014-12-15 12:57:01 +11004408 err = mddev_lock(mddev);
4409 if (err)
4410 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004411 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004412 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004413 if (err == 0)
4414 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004415 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004416 if (mddev->dev_sectors == 0 ||
4417 mddev->dev_sectors > sectors)
4418 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004419 else
4420 err = -ENOSPC;
4421 }
NeilBrown67918752014-12-15 12:57:01 +11004422 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004423 return err ? err : len;
4424}
4425
4426static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004427__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004428
Masanari Iida83f0d772012-10-30 00:18:08 +09004429/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004430 * This is one of
4431 * 'none' for arrays with no metadata (good luck...)
4432 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004433 * or N.M for internally known formats
4434 */
4435static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004436metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004437{
4438 if (mddev->persistent)
4439 return sprintf(page, "%d.%d\n",
4440 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004441 else if (mddev->external)
4442 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004443 else
4444 return sprintf(page, "none\n");
4445}
4446
4447static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004448metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004449{
4450 int major, minor;
4451 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004452 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004453 /* Changing the details of 'external' metadata is
4454 * always permitted. Otherwise there must be
4455 * no devices attached to the array.
4456 */
NeilBrown67918752014-12-15 12:57:01 +11004457
4458 err = mddev_lock(mddev);
4459 if (err)
4460 return err;
4461 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004462 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4463 ;
4464 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004465 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004466
NeilBrown67918752014-12-15 12:57:01 +11004467 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004468 if (cmd_match(buf, "none")) {
4469 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004470 mddev->external = 0;
4471 mddev->major_version = 0;
4472 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004473 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004474 }
4475 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004476 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004477 if (namelen >= sizeof(mddev->metadata_type))
4478 namelen = sizeof(mddev->metadata_type)-1;
4479 strncpy(mddev->metadata_type, buf+9, namelen);
4480 mddev->metadata_type[namelen] = 0;
4481 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4482 mddev->metadata_type[--namelen] = 0;
4483 mddev->persistent = 0;
4484 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004485 mddev->major_version = 0;
4486 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004487 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004488 }
4489 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004490 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004491 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004492 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004493 buf = e+1;
4494 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004495 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004496 goto out_unlock;
4497 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004498 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004499 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004500 mddev->major_version = major;
4501 mddev->minor_version = minor;
4502 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004503 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004504 err = 0;
4505out_unlock:
4506 mddev_unlock(mddev);
4507 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004508}
4509
4510static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004511__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004512
NeilBrowna35b0d62006-01-06 00:20:49 -08004513static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004514action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004515{
NeilBrown7eec3142005-11-08 21:39:44 -08004516 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004517 unsigned long recovery = mddev->recovery;
4518 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004519 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004520 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4521 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4522 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004523 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004524 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4525 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004526 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004527 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004528 type = "check";
4529 else
4530 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004531 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004532 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004533 else if (mddev->reshape_position != MaxSector)
4534 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004535 }
4536 return sprintf(page, "%s\n", type);
4537}
4538
4539static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004540action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004541{
NeilBrown7eec3142005-11-08 21:39:44 -08004542 if (!mddev->pers || !mddev->pers->sync_request)
4543 return -EINVAL;
4544
NeilBrownb6a9ce62009-05-26 09:41:17 +10004545
4546 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004547 if (cmd_match(page, "frozen"))
4548 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4549 else
4550 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004551 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4552 mddev_lock(mddev) == 0) {
4553 flush_workqueue(md_misc_wq);
4554 if (mddev->sync_thread) {
4555 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004556 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004557 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004558 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004559 }
NeilBrown312045e2015-12-21 11:01:21 +11004560 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004561 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004562 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004563 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004564 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004565 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004566 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004567 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004568 int err;
4569 if (mddev->pers->start_reshape == NULL)
4570 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004571 err = mddev_lock(mddev);
4572 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004573 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4574 err = -EBUSY;
4575 else {
4576 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4577 err = mddev->pers->start_reshape(mddev);
4578 }
NeilBrown67918752014-12-15 12:57:01 +11004579 mddev_unlock(mddev);
4580 }
NeilBrown16484bf2006-03-27 01:18:13 -08004581 if (err)
4582 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004583 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004584 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004585 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004586 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004587 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004588 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004589 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004590 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4591 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004592 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004593 if (mddev->ro == 2) {
4594 /* A write to sync_action is enough to justify
4595 * canceling read-auto mode
4596 */
4597 mddev->ro = 0;
4598 md_wakeup_thread(mddev->sync_thread);
4599 }
NeilBrown03c902e2006-01-06 00:20:46 -08004600 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004601 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004602 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004603 return len;
4604}
4605
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004606static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004607__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004608
4609static ssize_t
4610last_sync_action_show(struct mddev *mddev, char *page)
4611{
4612 return sprintf(page, "%s\n", mddev->last_sync_action);
4613}
4614
4615static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4616
NeilBrown9d888832005-11-08 21:39:26 -08004617static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004618mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004619{
4620 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004621 (unsigned long long)
4622 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004623}
4624
NeilBrown80ca3a42006-07-10 04:44:18 -07004625static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004626
NeilBrown88202a02006-01-06 00:21:36 -08004627static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004628sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004629{
4630 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4631 mddev->sync_speed_min ? "local": "system");
4632}
4633
4634static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004635sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004636{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004637 unsigned int min;
4638 int rv;
4639
NeilBrown88202a02006-01-06 00:21:36 -08004640 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004641 min = 0;
4642 } else {
4643 rv = kstrtouint(buf, 10, &min);
4644 if (rv < 0)
4645 return rv;
4646 if (min == 0)
4647 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004648 }
NeilBrown88202a02006-01-06 00:21:36 -08004649 mddev->sync_speed_min = min;
4650 return len;
4651}
4652
4653static struct md_sysfs_entry md_sync_min =
4654__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4655
4656static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004657sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004658{
4659 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4660 mddev->sync_speed_max ? "local": "system");
4661}
4662
4663static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004664sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004665{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004666 unsigned int max;
4667 int rv;
4668
NeilBrown88202a02006-01-06 00:21:36 -08004669 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004670 max = 0;
4671 } else {
4672 rv = kstrtouint(buf, 10, &max);
4673 if (rv < 0)
4674 return rv;
4675 if (max == 0)
4676 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004677 }
NeilBrown88202a02006-01-06 00:21:36 -08004678 mddev->sync_speed_max = max;
4679 return len;
4680}
4681
4682static struct md_sysfs_entry md_sync_max =
4683__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4684
Iustin Popd7f3d292007-10-16 23:30:54 -07004685static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004686degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004687{
4688 return sprintf(page, "%d\n", mddev->degraded);
4689}
4690static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004691
4692static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004693sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004694{
4695 return sprintf(page, "%d\n", mddev->parallel_resync);
4696}
4697
4698static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004699sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004700{
4701 long n;
4702
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004703 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004704 return -EINVAL;
4705
4706 if (n != 0 && n != 1)
4707 return -EINVAL;
4708
4709 mddev->parallel_resync = n;
4710
4711 if (mddev->sync_thread)
4712 wake_up(&resync_wait);
4713
4714 return len;
4715}
4716
4717/* force parallel resync, even with shared block devices */
4718static struct md_sysfs_entry md_sync_force_parallel =
4719__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4720 sync_force_parallel_show, sync_force_parallel_store);
4721
4722static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004723sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004724{
4725 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004726 if (mddev->curr_resync == 0)
4727 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004728 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4729 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004730 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004731 db = resync - mddev->resync_mark_cnt;
4732 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004733}
4734
NeilBrown80ca3a42006-07-10 04:44:18 -07004735static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004736
4737static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004738sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004739{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004740 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004741
NeilBrownacb180b2009-04-14 16:28:34 +10004742 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4743 return sprintf(page, "none\n");
4744
NeilBrown72f36d52012-10-11 14:25:57 +11004745 if (mddev->curr_resync == 1 ||
4746 mddev->curr_resync == 2)
4747 return sprintf(page, "delayed\n");
4748
NeilBrownc804cde2012-05-21 09:28:33 +10004749 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4750 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004751 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004752 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004753 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004754
NeilBrownacb180b2009-04-14 16:28:34 +10004755 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004756 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004757}
4758
NeilBrown750f1992014-09-30 08:53:05 +10004759static struct md_sysfs_entry md_sync_completed =
4760 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004761
NeilBrowne464eaf2006-03-27 01:18:14 -08004762static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004763min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004764{
4765 return sprintf(page, "%llu\n",
4766 (unsigned long long)mddev->resync_min);
4767}
4768static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004769min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004770{
4771 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004772 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004773
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004774 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004775 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004776
4777 spin_lock(&mddev->lock);
4778 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004779 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004780 goto out_unlock;
4781
4782 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004783 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004784 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004785
NeilBrown50c37b12015-03-23 17:36:38 +11004786 /* Round down to multiple of 4K for safety */
4787 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004788 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004789
NeilBrown23da4222014-12-15 12:57:01 +11004790out_unlock:
4791 spin_unlock(&mddev->lock);
4792 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004793}
4794
4795static struct md_sysfs_entry md_min_sync =
4796__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4797
4798static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004799max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004800{
4801 if (mddev->resync_max == MaxSector)
4802 return sprintf(page, "max\n");
4803 else
4804 return sprintf(page, "%llu\n",
4805 (unsigned long long)mddev->resync_max);
4806}
4807static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004808max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004809{
NeilBrown23da4222014-12-15 12:57:01 +11004810 int err;
4811 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004812 if (strncmp(buf, "max", 3) == 0)
4813 mddev->resync_max = MaxSector;
4814 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004815 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004816 int chunk;
4817
4818 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004819 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004820 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004821 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004822 goto out_unlock;
4823
4824 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004825 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004826 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004827 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004828 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004829
4830 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004831 chunk = mddev->chunk_sectors;
4832 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004833 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004834
4835 err = -EINVAL;
4836 if (sector_div(temp, chunk))
4837 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004838 }
4839 mddev->resync_max = max;
4840 }
4841 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004842 err = 0;
4843out_unlock:
4844 spin_unlock(&mddev->lock);
4845 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004846}
4847
4848static struct md_sysfs_entry md_max_sync =
4849__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4850
4851static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004852suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004853{
4854 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4855}
4856
4857static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004858suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004859{
NeilBrownb03e0cc2017-10-19 12:49:15 +11004860 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004861 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004862
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004863 err = kstrtoull(buf, 10, &new);
4864 if (err < 0)
4865 return err;
4866 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004867 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004868
NeilBrown67918752014-12-15 12:57:01 +11004869 err = mddev_lock(mddev);
4870 if (err)
4871 return err;
4872 err = -EINVAL;
4873 if (mddev->pers == NULL ||
4874 mddev->pers->quiesce == NULL)
4875 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004876 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11004877 mddev->suspend_lo = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004878 mddev_resume(mddev);
4879
NeilBrown67918752014-12-15 12:57:01 +11004880 err = 0;
4881unlock:
4882 mddev_unlock(mddev);
4883 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004884}
4885static struct md_sysfs_entry md_suspend_lo =
4886__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4887
NeilBrowne464eaf2006-03-27 01:18:14 -08004888static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004889suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004890{
4891 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4892}
4893
4894static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004895suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004896{
NeilBrownb03e0cc2017-10-19 12:49:15 +11004897 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004898 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004899
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004900 err = kstrtoull(buf, 10, &new);
4901 if (err < 0)
4902 return err;
4903 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004904 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004905
NeilBrown67918752014-12-15 12:57:01 +11004906 err = mddev_lock(mddev);
4907 if (err)
4908 return err;
4909 err = -EINVAL;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004910 if (mddev->pers == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004911 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004912
4913 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11004914 mddev->suspend_hi = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004915 mddev_resume(mddev);
4916
NeilBrown67918752014-12-15 12:57:01 +11004917 err = 0;
4918unlock:
4919 mddev_unlock(mddev);
4920 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004921}
4922static struct md_sysfs_entry md_suspend_hi =
4923__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4924
NeilBrown08a02ec2007-05-09 02:35:38 -07004925static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004926reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004927{
4928 if (mddev->reshape_position != MaxSector)
4929 return sprintf(page, "%llu\n",
4930 (unsigned long long)mddev->reshape_position);
4931 strcpy(page, "none\n");
4932 return 5;
4933}
4934
4935static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004936reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004937{
NeilBrownc6563a82012-05-21 09:27:00 +10004938 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004939 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004940 int err;
NeilBrown67918752014-12-15 12:57:01 +11004941
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004942 err = kstrtoull(buf, 10, &new);
4943 if (err < 0)
4944 return err;
4945 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004946 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004947 err = mddev_lock(mddev);
4948 if (err)
4949 return err;
4950 err = -EBUSY;
4951 if (mddev->pers)
4952 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004953 mddev->reshape_position = new;
4954 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004955 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004956 mddev->new_level = mddev->level;
4957 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004958 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004959 rdev_for_each(rdev, mddev)
4960 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004961 err = 0;
4962unlock:
4963 mddev_unlock(mddev);
4964 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004965}
4966
4967static struct md_sysfs_entry md_reshape_position =
4968__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4969 reshape_position_store);
4970
Dan Williamsb522adc2009-03-31 15:00:31 +11004971static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004972reshape_direction_show(struct mddev *mddev, char *page)
4973{
4974 return sprintf(page, "%s\n",
4975 mddev->reshape_backwards ? "backwards" : "forwards");
4976}
4977
4978static ssize_t
4979reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4980{
4981 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004982 int err;
4983
NeilBrown2c810cd2012-05-21 09:27:00 +10004984 if (cmd_match(buf, "forwards"))
4985 backwards = 0;
4986 else if (cmd_match(buf, "backwards"))
4987 backwards = 1;
4988 else
4989 return -EINVAL;
4990 if (mddev->reshape_backwards == backwards)
4991 return len;
4992
NeilBrown67918752014-12-15 12:57:01 +11004993 err = mddev_lock(mddev);
4994 if (err)
4995 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004996 /* check if we are allowed to change */
4997 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004998 err = -EBUSY;
4999 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10005000 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11005001 err = -EINVAL;
5002 else
5003 mddev->reshape_backwards = backwards;
5004 mddev_unlock(mddev);
5005 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10005006}
5007
5008static struct md_sysfs_entry md_reshape_direction =
5009__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5010 reshape_direction_store);
5011
5012static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005013array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11005014{
5015 if (mddev->external_size)
5016 return sprintf(page, "%llu\n",
5017 (unsigned long long)mddev->array_sectors/2);
5018 else
5019 return sprintf(page, "default\n");
5020}
5021
5022static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005023array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11005024{
5025 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11005026 int err;
5027
5028 err = mddev_lock(mddev);
5029 if (err)
5030 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11005031
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005032 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08005033 if (mddev_is_clustered(mddev)) {
5034 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005035 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08005036 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005037
Dan Williamsb522adc2009-03-31 15:00:31 +11005038 if (strncmp(buf, "default", 7) == 0) {
5039 if (mddev->pers)
5040 sectors = mddev->pers->size(mddev, 0, 0);
5041 else
5042 sectors = mddev->array_sectors;
5043
5044 mddev->external_size = 0;
5045 } else {
5046 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11005047 err = -EINVAL;
5048 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5049 err = -E2BIG;
5050 else
5051 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11005052 }
5053
NeilBrown67918752014-12-15 12:57:01 +11005054 if (!err) {
5055 mddev->array_sectors = sectors;
5056 if (mddev->pers) {
5057 set_capacity(mddev->gendisk, mddev->array_sectors);
5058 revalidate_disk(mddev->gendisk);
5059 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11005060 }
NeilBrown67918752014-12-15 12:57:01 +11005061 mddev_unlock(mddev);
5062 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11005063}
5064
5065static struct md_sysfs_entry md_array_size =
5066__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5067 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005068
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005069static ssize_t
5070consistency_policy_show(struct mddev *mddev, char *page)
5071{
5072 int ret;
5073
5074 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5075 ret = sprintf(page, "journal\n");
5076 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5077 ret = sprintf(page, "ppl\n");
5078 } else if (mddev->bitmap) {
5079 ret = sprintf(page, "bitmap\n");
5080 } else if (mddev->pers) {
5081 if (mddev->pers->sync_request)
5082 ret = sprintf(page, "resync\n");
5083 else
5084 ret = sprintf(page, "none\n");
5085 } else {
5086 ret = sprintf(page, "unknown\n");
5087 }
5088
5089 return ret;
5090}
5091
5092static ssize_t
5093consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5094{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005095 int err = 0;
5096
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005097 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005098 if (mddev->pers->change_consistency_policy)
5099 err = mddev->pers->change_consistency_policy(mddev, buf);
5100 else
5101 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005102 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5103 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005104 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005105 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005106 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005107
5108 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005109}
5110
5111static struct md_sysfs_entry md_consistency_policy =
5112__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5113 consistency_policy_store);
5114
NeilBrowneae17012005-11-08 21:39:23 -08005115static struct attribute *md_default_attrs[] = {
5116 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005117 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005118 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005119 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005120 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005121 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005122 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005123 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005124 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005125 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005126 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005127 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005128 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005129 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005130 &md_consistency_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005131 NULL,
5132};
5133
5134static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005135 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005136 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005137 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005138 &md_sync_min.attr,
5139 &md_sync_max.attr,
5140 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005141 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005142 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005143 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005144 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005145 &md_suspend_lo.attr,
5146 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005147 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005148 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005149 NULL,
5150};
NeilBrown411036f2005-11-08 21:39:40 -08005151static struct attribute_group md_redundancy_group = {
5152 .name = NULL,
5153 .attrs = md_redundancy_attrs,
5154};
5155
NeilBrowneae17012005-11-08 21:39:23 -08005156static ssize_t
5157md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5158{
5159 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005160 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005161 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005162
5163 if (!entry->show)
5164 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005165 spin_lock(&all_mddevs_lock);
5166 if (list_empty(&mddev->all_mddevs)) {
5167 spin_unlock(&all_mddevs_lock);
5168 return -EBUSY;
5169 }
5170 mddev_get(mddev);
5171 spin_unlock(&all_mddevs_lock);
5172
NeilBrownb7b17c92014-12-15 12:56:59 +11005173 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005174 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005175 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005176}
5177
5178static ssize_t
5179md_attr_store(struct kobject *kobj, struct attribute *attr,
5180 const char *page, size_t length)
5181{
5182 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005183 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005184 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005185
5186 if (!entry->store)
5187 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005188 if (!capable(CAP_SYS_ADMIN))
5189 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005190 spin_lock(&all_mddevs_lock);
5191 if (list_empty(&mddev->all_mddevs)) {
5192 spin_unlock(&all_mddevs_lock);
5193 return -EBUSY;
5194 }
5195 mddev_get(mddev);
5196 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005197 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005198 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005199 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005200}
5201
5202static void md_free(struct kobject *ko)
5203{
NeilBrownfd01b882011-10-11 16:47:53 +11005204 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005205
5206 if (mddev->sysfs_state)
5207 sysfs_put(mddev->sysfs_state);
5208
Bart Van Assched8115c352018-02-28 10:15:29 -08005209 if (mddev->gendisk)
5210 del_gendisk(mddev->gendisk);
NeilBrown6cd18e72015-04-27 14:12:22 +10005211 if (mddev->queue)
5212 blk_cleanup_queue(mddev->queue);
Bart Van Assched8115c352018-02-28 10:15:29 -08005213 if (mddev->gendisk)
NeilBrowna21d1502009-01-09 08:31:09 +11005214 put_disk(mddev->gendisk);
NeilBrown4ad23a972017-03-15 14:05:14 +11005215 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005216
NeilBrowneae17012005-11-08 21:39:23 -08005217 kfree(mddev);
5218}
5219
Emese Revfy52cf25d2010-01-19 02:58:23 +01005220static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005221 .show = md_attr_show,
5222 .store = md_attr_store,
5223};
5224static struct kobj_type md_ktype = {
5225 .release = md_free,
5226 .sysfs_ops = &md_sysfs_ops,
5227 .default_attrs = md_default_attrs,
5228};
5229
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230int mdp_major = 0;
5231
Dan Williams5fd3a172009-03-04 00:57:25 -07005232static void mddev_delayed_delete(struct work_struct *ws)
5233{
NeilBrownfd01b882011-10-11 16:47:53 +11005234 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005235
NeilBrown43a70502009-12-14 12:49:55 +11005236 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005237 kobject_del(&mddev->kobj);
5238 kobject_put(&mddev->kobj);
5239}
5240
NeilBrown4ad23a972017-03-15 14:05:14 +11005241static void no_op(struct percpu_ref *r) {}
5242
NeilBrowna415c0f2017-06-05 16:05:13 +10005243int mddev_init_writes_pending(struct mddev *mddev)
5244{
5245 if (mddev->writes_pending.percpu_count_ptr)
5246 return 0;
5247 if (percpu_ref_init(&mddev->writes_pending, no_op, 0, GFP_KERNEL) < 0)
5248 return -ENOMEM;
5249 /* We want to start with the refcount at zero */
5250 percpu_ref_put(&mddev->writes_pending);
5251 return 0;
5252}
5253EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5254
NeilBrownefeb53c2009-01-09 08:31:10 +11005255static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256{
NeilBrown039b7222017-04-12 16:26:13 +10005257 /*
5258 * If dev is zero, name is the name of a device to allocate with
5259 * an arbitrary minor number. It will be "md_???"
5260 * If dev is non-zero it must be a device number with a MAJOR of
5261 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5262 * the device is being created by opening a node in /dev.
5263 * If "name" is not NULL, the device is being created by
5264 * writing to /sys/module/md_mod/parameters/new_array.
5265 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005266 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005267 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005269 int partitioned;
5270 int shift;
5271 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005272 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273
5274 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005275 return -ENODEV;
5276
5277 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5278 shift = partitioned ? MdpMinorShift : 0;
5279 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280
Tejun Heoe804ac72010-10-15 15:36:08 +02005281 /* wait for any previous instance of this device to be
5282 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005283 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005284 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005285
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005286 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005287 error = -EEXIST;
5288 if (mddev->gendisk)
5289 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005290
NeilBrown039b7222017-04-12 16:26:13 +10005291 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005292 /* Need to ensure that 'name' is not a duplicate.
5293 */
NeilBrownfd01b882011-10-11 16:47:53 +11005294 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005295 spin_lock(&all_mddevs_lock);
5296
5297 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5298 if (mddev2->gendisk &&
5299 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5300 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005301 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005302 }
5303 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 }
NeilBrown039b7222017-04-12 16:26:13 +10005305 if (name && dev)
5306 /*
5307 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5308 */
5309 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005310
NeilBrown0909dc42009-07-01 12:27:21 +10005311 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005312 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005313 if (!mddev->queue)
5314 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005315 mddev->queue->queuedata = mddev;
5316
NeilBrown409c57f2009-03-31 14:39:39 +11005317 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005318 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005319
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 disk = alloc_disk(1 << shift);
5321 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005322 blk_cleanup_queue(mddev->queue);
5323 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005324 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005326 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005328 if (name)
5329 strcpy(disk->disk_name, name);
5330 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005332 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 disk->fops = &md_fops;
5335 disk->private_data = mddev;
5336 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005337 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005338 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005339 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005340 * remove it now.
5341 */
5342 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005344 /* As soon as we call add_disk(), another thread could get
5345 * through to md_open, so make sure it doesn't get too far
5346 */
5347 mutex_lock(&mddev->open_mutex);
5348 add_disk(disk);
5349
Tejun Heoed9e1982008-08-25 19:56:05 +09005350 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5351 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005352 if (error) {
5353 /* This isn't possible, but as kobject_init_and_add is marked
5354 * __must_check, we must do something with the result
5355 */
NeilBrown9d487392016-11-02 14:16:49 +11005356 pr_debug("md: cannot register %s/md - name in use\n",
5357 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005358 error = 0;
5359 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005360 if (mddev->kobj.sd &&
5361 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005362 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005363 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005364 abort:
5365 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005366 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005367 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005368 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005369 }
NeilBrownd3374822009-01-09 08:31:10 +11005370 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005371 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005372}
5373
5374static struct kobject *md_probe(dev_t dev, int *part, void *data)
5375{
NeilBrown78b63502017-04-12 16:26:13 +10005376 if (create_on_open)
5377 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 return NULL;
5379}
5380
Kees Cooke4dca7b2017-10-17 19:04:42 -07005381static int add_named_array(const char *val, const struct kernel_param *kp)
NeilBrownefeb53c2009-01-09 08:31:10 +11005382{
NeilBrown039b7222017-04-12 16:26:13 +10005383 /*
5384 * val must be "md_*" or "mdNNN".
5385 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005386 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005387 * For "mdNNN" we allocate an array with the minor number NNN
5388 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005389 */
5390 int len = strlen(val);
5391 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005392 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005393
5394 while (len && val[len-1] == '\n')
5395 len--;
5396 if (len >= DISK_NAME_LEN)
5397 return -E2BIG;
5398 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005399 if (strncmp(buf, "md_", 3) == 0)
5400 return md_alloc(0, buf);
5401 if (strncmp(buf, "md", 2) == 0 &&
5402 isdigit(buf[2]) &&
5403 kstrtoul(buf+2, 10, &devnum) == 0 &&
5404 devnum <= MINORMASK)
5405 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5406
5407 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005408}
5409
Kees Cook8376d3c2017-10-16 17:01:48 -07005410static void md_safemode_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411{
Kees Cook8376d3c2017-10-16 17:01:48 -07005412 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413
NeilBrown4ad23a972017-03-15 14:05:14 +11005414 mddev->safemode = 1;
5415 if (mddev->external)
5416 sysfs_notify_dirent_safe(mddev->sysfs_state);
5417
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 md_wakeup_thread(mddev->thread);
5419}
5420
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005421static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
NeilBrownfd01b882011-10-11 16:47:53 +11005423int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424{
NeilBrown2604b702006-01-06 00:20:36 -08005425 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005426 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005427 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428
NeilBrowna757e642005-04-16 15:26:42 -07005429 if (list_empty(&mddev->disks))
5430 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
5433 if (mddev->pers)
5434 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005435 /* Cannot run until previous stop completes properly */
5436 if (mddev->sysfs_active)
5437 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005438
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 /*
5440 * Analyze all RAID superblock(s)
5441 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005442 if (!mddev->raid_disks) {
5443 if (!mddev->persistent)
5444 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005445 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
NeilBrownd9d166c2006-01-06 00:20:51 -08005448 if (mddev->level != LEVEL_NONE)
5449 request_module("md-level-%d", mddev->level);
5450 else if (mddev->clevel[0])
5451 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452
5453 /*
5454 * Drop all container device buffers, from now on
5455 * the only valid external interface is through the md
5456 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 */
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005458 mddev->has_superblocks = false;
NeilBrowndafb20f2012-03-19 12:46:39 +11005459 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005460 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 continue;
5462 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005463 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005464 if (mddev->ro != 1 &&
5465 (bdev_read_only(rdev->bdev) ||
5466 bdev_read_only(rdev->meta_bdev))) {
5467 mddev->ro = 1;
5468 if (mddev->gendisk)
5469 set_disk_ro(mddev->gendisk, 1);
5470 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005471
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005472 if (rdev->sb_page)
5473 mddev->has_superblocks = true;
5474
NeilBrownf0d76d72007-07-17 04:06:12 -07005475 /* perform some consistency tests on the device.
5476 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005477 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005478 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005479 if (rdev->meta_bdev) {
5480 /* Nothing to check */;
5481 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005482 if (mddev->dev_sectors &&
5483 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005484 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005485 pr_warn("md: %s: data overlaps metadata\n",
5486 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005487 return -EINVAL;
5488 }
5489 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005490 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005491 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005492 pr_warn("md: %s: metadata overlaps data\n",
5493 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005494 return -EINVAL;
5495 }
5496 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005497 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 }
5499
Ming Lei10273172017-02-14 23:29:00 +08005500 if (mddev->bio_set == NULL) {
NeilBrown011067b2017-06-18 14:38:57 +10005501 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
Ming Lei10273172017-02-14 23:29:00 +08005502 if (!mddev->bio_set)
5503 return -ENOMEM;
5504 }
NeilBrown5a850712017-06-21 09:12:21 +10005505 if (mddev->sync_set == NULL) {
Linus Torvalds026d15f2017-07-08 12:50:18 -07005506 mddev->sync_set = bioset_create(BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
Xiao Nib1261942018-01-24 12:17:38 +08005507 if (!mddev->sync_set) {
5508 err = -ENOMEM;
5509 goto abort;
5510 }
NeilBrown5a850712017-06-21 09:12:21 +10005511 }
NeilBrowna167f662010-10-26 18:31:13 +11005512
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005514 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005515 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005517 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005518 pr_warn("md: personality for level %d is not loaded!\n",
5519 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005520 else
NeilBrown9d487392016-11-02 14:16:49 +11005521 pr_warn("md: personality for level %s is not loaded!\n",
5522 mddev->clevel);
Xiao Nib1261942018-01-24 12:17:38 +08005523 err = -EINVAL;
5524 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005527 if (mddev->level != pers->level) {
5528 mddev->level = pers->level;
5529 mddev->new_level = pers->level;
5530 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005531 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532
NeilBrownf6705572006-03-27 01:18:11 -08005533 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005534 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005535 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005536 module_put(pers->owner);
Xiao Nib1261942018-01-24 12:17:38 +08005537 err = -EINVAL;
5538 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08005539 }
5540
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005541 if (pers->sync_request) {
5542 /* Warn if this is a potentially silly
5543 * configuration.
5544 */
5545 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005546 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005547 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005548
NeilBrowndafb20f2012-03-19 12:46:39 +11005549 rdev_for_each(rdev, mddev)
5550 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005551 if (rdev < rdev2 &&
5552 rdev->bdev->bd_contains ==
5553 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005554 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5555 mdname(mddev),
5556 bdevname(rdev->bdev,b),
5557 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005558 warned = 1;
5559 }
5560 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005561
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005562 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005563 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005564 }
5565
NeilBrown657390d2005-08-26 18:34:16 -07005566 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005567 /* may be over-ridden by personality */
5568 mddev->resync_max_sectors = mddev->dev_sectors;
5569
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005570 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
NeilBrown0f9552b52009-12-30 12:08:50 +11005572 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005573 mddev->ro = 2; /* read-only, but switch on first write */
5574
NeilBrown36d091f2014-12-15 12:56:58 +11005575 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005576 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005577 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005578 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005579 WARN_ONCE(!mddev->external_size,
5580 "%s: default size too small, but 'external_size' not in effect?\n",
5581 __func__);
5582 pr_warn("md: invalid array_size %llu > default size %llu\n",
5583 (unsigned long long)mddev->array_sectors / 2,
5584 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005585 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005586 }
NeilBrown36d091f2014-12-15 12:56:58 +11005587 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005588 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005589 struct bitmap *bitmap;
5590
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005591 bitmap = md_bitmap_create(mddev, -1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005592 if (IS_ERR(bitmap)) {
5593 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005594 pr_warn("%s: failed to create bitmap (%d)\n",
5595 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005596 } else
5597 mddev->bitmap = bitmap;
5598
NeilBrownb15c2e52006-01-06 00:20:16 -08005599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005601 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005602 if (mddev->private)
5603 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005604 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005605 module_put(pers->owner);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005606 md_bitmap_destroy(mddev);
Xiao Nib1261942018-01-24 12:17:38 +08005607 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 }
NeilBrown5c675f82014-12-15 12:56:56 +11005609 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005610 bool nonrot = true;
5611
5612 rdev_for_each(rdev, mddev) {
5613 if (rdev->raid_disk >= 0 &&
5614 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5615 nonrot = false;
5616 break;
5617 }
5618 }
5619 if (mddev->degraded)
5620 nonrot = false;
5621 if (nonrot)
Bart Van Assche8b904b52018-03-07 17:10:10 -08005622 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
Shaohua Libb086a82016-09-30 09:45:40 -07005623 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08005624 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005625 mddev->queue->backing_dev_info->congested_data = mddev;
5626 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005627 }
NeilBrown36d091f2014-12-15 12:56:58 +11005628 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005629 if (mddev->kobj.sd &&
5630 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005631 pr_warn("md: cannot register extra attributes for %s\n",
5632 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005633 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005634 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005635 mddev->ro = 0;
5636
Robert Becker1e509152009-12-14 12:49:58 +11005637 atomic_set(&mddev->max_corr_read_errors,
5638 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005640 if (mddev_is_clustered(mddev))
5641 mddev->safemode_delay = 0;
5642 else
5643 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005645 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005646 spin_lock(&mddev->lock);
5647 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005648 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005649 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005650 if (rdev->raid_disk >= 0)
5651 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005652 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005653
NeilBrowna4a3d262015-07-17 11:57:30 +10005654 if (mddev->degraded && !mddev->ro)
5655 /* This ensures that recovering status is reported immediately
5656 * via sysfs - until a lack of spares is confirmed.
5657 */
5658 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005660
Shaohua Li29530792016-12-08 15:48:19 -08005661 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07005662 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663
NeilBrownd7603b72006-01-06 00:20:30 -08005664 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005665 sysfs_notify_dirent_safe(mddev->sysfs_state);
5666 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005667 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 return 0;
Xiao Nib1261942018-01-24 12:17:38 +08005669
5670abort:
5671 if (mddev->bio_set) {
5672 bioset_free(mddev->bio_set);
5673 mddev->bio_set = NULL;
5674 }
5675 if (mddev->sync_set) {
5676 bioset_free(mddev->sync_set);
5677 mddev->sync_set = NULL;
5678 }
5679
5680 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681}
NeilBrown390ee602010-06-01 19:37:27 +10005682EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
NeilBrownfd01b882011-10-11 16:47:53 +11005684static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005685{
5686 int err;
5687
5688 err = md_run(mddev);
5689 if (err)
5690 goto out;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005691 err = md_bitmap_load(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005692 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005693 md_bitmap_destroy(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005694 goto out;
5695 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005696
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005697 if (mddev_is_clustered(mddev))
5698 md_allow_write(mddev);
5699
Song Liud5d885f2017-11-19 22:17:01 -08005700 /* run start up tasks that require md_thread */
5701 md_start(mddev);
5702
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005703 md_wakeup_thread(mddev->thread);
5704 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5705
NeilBrownfe60b012010-03-29 11:10:42 +11005706 set_capacity(mddev->gendisk, mddev->array_sectors);
5707 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005708 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005709 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5710out:
5711 return err;
5712}
5713
Song Liud5d885f2017-11-19 22:17:01 -08005714int md_start(struct mddev *mddev)
5715{
5716 int ret = 0;
5717
5718 if (mddev->pers->start) {
5719 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
5720 md_wakeup_thread(mddev->thread);
5721 ret = mddev->pers->start(mddev);
5722 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
5723 md_wakeup_thread(mddev->sync_thread);
5724 }
5725 return ret;
5726}
5727EXPORT_SYMBOL_GPL(md_start);
5728
NeilBrownfd01b882011-10-11 16:47:53 +11005729static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730{
5731 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10005732 struct md_rdev *rdev;
5733 bool has_journal = false;
5734 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
Andre Noll80fab1d2008-07-11 22:02:21 +10005736 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005738 return -ENXIO;
5739 if (!mddev->pers)
5740 return -EINVAL;
5741 if (!mddev->ro)
5742 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005743
NeilBrown97b20ef2017-04-13 08:53:48 +10005744 rcu_read_lock();
5745 rdev_for_each_rcu(rdev, mddev) {
5746 if (test_bit(Journal, &rdev->flags) &&
5747 !test_bit(Faulty, &rdev->flags))
5748 has_journal = true;
5749 if (bdev_read_only(rdev->bdev))
5750 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07005751 }
NeilBrown97b20ef2017-04-13 08:53:48 +10005752 rcu_read_unlock();
5753 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
5754 /* Don't restart rw with journal missing/faulty */
5755 return -EINVAL;
5756 if (has_readonly)
5757 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07005758
Andre Noll80fab1d2008-07-11 22:02:21 +10005759 mddev->safemode = 0;
5760 mddev->ro = 0;
5761 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11005762 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10005763 /* Kick recovery or resync if necessary */
5764 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5765 md_wakeup_thread(mddev->thread);
5766 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005767 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769}
5770
NeilBrownfd01b882011-10-11 16:47:53 +11005771static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005772{
5773 mddev->array_sectors = 0;
5774 mddev->external_size = 0;
5775 mddev->dev_sectors = 0;
5776 mddev->raid_disks = 0;
5777 mddev->recovery_cp = 0;
5778 mddev->resync_min = 0;
5779 mddev->resync_max = MaxSector;
5780 mddev->reshape_position = MaxSector;
5781 mddev->external = 0;
5782 mddev->persistent = 0;
5783 mddev->level = LEVEL_NONE;
5784 mddev->clevel[0] = 0;
5785 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08005786 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005787 mddev->ro = 0;
5788 mddev->metadata_type[0] = 0;
5789 mddev->chunk_sectors = 0;
5790 mddev->ctime = mddev->utime = 0;
5791 mddev->layout = 0;
5792 mddev->max_disks = 0;
5793 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005794 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005795 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005796 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005797 mddev->new_level = LEVEL_NONE;
5798 mddev->new_layout = 0;
5799 mddev->new_chunk_sectors = 0;
5800 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005801 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005802 mddev->suspend_lo = mddev->suspend_hi = 0;
5803 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5804 mddev->recovery = 0;
5805 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005806 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005807 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005808 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005809 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005810 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005811 mddev->bitmap_info.offset = 0;
5812 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005813 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005814 mddev->bitmap_info.chunksize = 0;
5815 mddev->bitmap_info.daemon_sleep = 0;
5816 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005817 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005818}
5819
NeilBrownfd01b882011-10-11 16:47:53 +11005820static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005821{
NeilBrown6b6204e2013-05-09 09:48:30 +10005822 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005823 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005824 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005825 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005826 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005827 }
5828
5829 del_timer_sync(&mddev->safemode_timer);
5830
Shaohua Li034e33f2016-11-21 10:29:19 -08005831 if (mddev->pers && mddev->pers->quiesce) {
5832 mddev->pers->quiesce(mddev, 1);
5833 mddev->pers->quiesce(mddev, 0);
5834 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005835 md_bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005836
NeilBrownb6d428c2013-04-24 11:42:42 +10005837 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005838 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08005839 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11005840 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005841 if (!mddev_is_clustered(mddev))
5842 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005843 md_update_sb(mddev, 1);
5844 }
5845}
NeilBrowndefad612011-01-14 09:14:33 +11005846
NeilBrownfd01b882011-10-11 16:47:53 +11005847void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005848{
NeilBrown29f097c2013-11-14 17:54:51 +11005849 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005850 __md_stop_writes(mddev);
5851 mddev_unlock(mddev);
5852}
NeilBrown390ee602010-06-01 19:37:27 +10005853EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005854
NeilBrown5aa61f42014-12-15 12:56:57 +11005855static void mddev_detach(struct mddev *mddev)
5856{
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005857 md_bitmap_wait_behind_writes(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005858 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005859 mddev->pers->quiesce(mddev, 1);
5860 mddev->pers->quiesce(mddev, 0);
5861 }
5862 md_unregister_thread(&mddev->thread);
5863 if (mddev->queue)
5864 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5865}
5866
NeilBrown5eff3c42012-11-19 10:47:48 +11005867static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005868{
NeilBrown36d091f2014-12-15 12:56:58 +11005869 struct md_personality *pers = mddev->pers;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005870 md_bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11005871 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005872 /* Ensure ->event_work is done */
5873 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005874 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005875 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005876 spin_unlock(&mddev->lock);
5877 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005878 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005879 if (pers->sync_request && mddev->to_remove == NULL)
5880 mddev->to_remove = &md_redundancy_group;
5881 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005882 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005883}
NeilBrown5eff3c42012-11-19 10:47:48 +11005884
5885void md_stop(struct mddev *mddev)
5886{
5887 /* stop the array and free an attached data structures.
5888 * This is called from dm-raid
5889 */
5890 __md_stop(mddev);
Zdenek Kabelac0202ce82017-11-08 13:44:55 +01005891 if (mddev->bio_set) {
NeilBrown5eff3c42012-11-19 10:47:48 +11005892 bioset_free(mddev->bio_set);
Zdenek Kabelac0202ce82017-11-08 13:44:55 +01005893 mddev->bio_set = NULL;
5894 }
5895 if (mddev->sync_set) {
5896 bioset_free(mddev->sync_set);
5897 mddev->sync_set = NULL;
5898 }
NeilBrown5eff3c42012-11-19 10:47:48 +11005899}
5900
NeilBrown390ee602010-06-01 19:37:27 +10005901EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005902
NeilBrowna05b7ea2012-07-19 15:59:18 +10005903static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005904{
5905 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005906 int did_freeze = 0;
5907
5908 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5909 did_freeze = 1;
5910 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5911 md_wakeup_thread(mddev->thread);
5912 }
NeilBrownf851b602014-12-11 10:02:10 +11005913 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005914 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005915 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005916 /* Thread might be blocked waiting for metadata update
5917 * which will now never happen */
5918 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005919
Shaohua Li29530792016-12-08 15:48:19 -08005920 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10005921 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005922 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005923 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5924 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005925 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005926 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005927 mddev_lock_nointr(mddev);
5928
NeilBrowna4bd82d2010-03-29 13:23:10 +11005929 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005930 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005931 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005932 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005933 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005934 if (did_freeze) {
5935 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005936 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005937 md_wakeup_thread(mddev->thread);
5938 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005939 err = -EBUSY;
5940 goto out;
5941 }
5942 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005943 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005944
5945 err = -ENXIO;
5946 if (mddev->ro==1)
5947 goto out;
5948 mddev->ro = 1;
5949 set_disk_ro(mddev->gendisk, 1);
5950 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005951 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5952 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005953 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005954 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005955 }
5956out:
5957 mutex_unlock(&mddev->open_mutex);
5958 return err;
5959}
5960
NeilBrown9e653b62006-06-26 00:27:58 -07005961/* mode:
5962 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005963 * 2 - stop but do not disassemble array
5964 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005965static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005966 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005969 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005970 int did_freeze = 0;
5971
5972 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5973 did_freeze = 1;
5974 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5975 md_wakeup_thread(mddev->thread);
5976 }
NeilBrownf851b602014-12-11 10:02:10 +11005977 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005978 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005979 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005980 /* Thread might be blocked waiting for metadata update
5981 * which will now never happen */
5982 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005983
NeilBrown30b8feb2013-11-14 15:16:17 +11005984 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005985 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5986 !test_bit(MD_RECOVERY_RUNNING,
5987 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005988 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989
NeilBrownc8c00a62009-08-10 12:50:52 +10005990 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005991 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005992 mddev->sysfs_active ||
5993 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005994 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005995 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005996 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005997 if (did_freeze) {
5998 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005999 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006000 md_wakeup_thread(mddev->thread);
6001 }
NeilBrown260fa032013-08-27 16:44:13 +10006002 return -EBUSY;
6003 }
NeilBrown6e17b022010-08-07 21:41:19 +10006004 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11006005 if (mddev->ro)
6006 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11006007
NeilBrowndefad612011-01-14 09:14:33 +11006008 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11006009 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01006010 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006011
NeilBrowna4bd82d2010-03-29 13:23:10 +11006012 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10006013 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08006014
NeilBrowndafb20f2012-03-19 12:46:39 +11006015 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10006016 if (rdev->raid_disk >= 0)
6017 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10006018
NeilBrowna4bd82d2010-03-29 13:23:10 +11006019 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10006020 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006021 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006022 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08006023
NeilBrowna4bd82d2010-03-29 13:23:10 +11006024 if (mddev->ro)
6025 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10006026 } else
6027 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 /*
6029 * Free resources if final stop
6030 */
NeilBrown9e653b62006-06-26 00:27:58 -07006031 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006032 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033
NeilBrownc3d97142009-12-14 12:49:52 +11006034 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11006035 struct file *f = mddev->bitmap_info.file;
6036 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11006037 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11006038 spin_unlock(&mddev->lock);
6039 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08006040 }
NeilBrownc3d97142009-12-14 12:49:52 +11006041 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08006042
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 export_array(mddev);
6044
NeilBrown6177b472010-03-29 11:37:13 +11006045 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11006046 if (mddev->hold_active == UNTIL_STOP)
6047 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006048 }
NeilBrownd7603b72006-01-06 00:20:30 -08006049 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006050 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10006051 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052}
6053
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006054#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11006055static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056{
NeilBrown3cb03002011-10-11 16:45:26 +11006057 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 int err;
6059
NeilBrowna757e642005-04-16 15:26:42 -07006060 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062
NeilBrown9d487392016-11-02 14:16:49 +11006063 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064
NeilBrowndafb20f2012-03-19 12:46:39 +11006065 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11006067 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 }
NeilBrown9d487392016-11-02 14:16:49 +11006069 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
NeilBrownd710e132008-10-13 11:55:12 +11006071 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006073 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10006074 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 }
6076}
6077
6078/*
6079 * lets try to run arrays based on all disks that have arrived
6080 * until now. (those are in pending_raid_disks)
6081 *
6082 * the method: pick the first pending disk, collect all disks with
6083 * the same UUID, remove all from the pending list and put them into
6084 * the 'same_array' list. Then order this list based on superblock
6085 * update time (freshest comes first), kick out 'old' disks and
6086 * compare superblocks. If everything's fine then run it.
6087 *
6088 * If "unit" is allocated, then bump its reference count
6089 */
6090static void autorun_devices(int part)
6091{
NeilBrown3cb03002011-10-11 16:45:26 +11006092 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006093 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 char b[BDEVNAME_SIZE];
6095
NeilBrown9d487392016-11-02 14:16:49 +11006096 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07006098 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08006100 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11006102 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103
NeilBrown9d487392016-11-02 14:16:49 +11006104 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006106 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006108 pr_debug("md: adding %s ...\n",
6109 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 list_move(&rdev->same_set, &candidates);
6111 }
6112 /*
6113 * now we have a set of devices, with all of them having
6114 * mostly sane superblocks. It's time to allocate the
6115 * mddev.
6116 */
NeilBrowne8703fe2006-10-03 01:15:59 -07006117 if (part) {
6118 dev = MKDEV(mdp_major,
6119 rdev0->preferred_minor << MdpMinorShift);
6120 unit = MINOR(dev) >> MdpMinorShift;
6121 } else {
6122 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6123 unit = MINOR(dev);
6124 }
6125 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006126 pr_warn("md: unit number in %s is bad: %d\n",
6127 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 break;
6129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130
6131 md_probe(dev, NULL, NULL);
6132 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006133 if (!mddev || !mddev->gendisk) {
6134 if (mddev)
6135 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 break;
6137 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006138 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006139 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 else if (mddev->raid_disks || mddev->major_version
6141 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006142 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 mdname(mddev), bdevname(rdev0->bdev,b));
6144 mddev_unlock(mddev);
6145 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006146 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006147 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006148 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 list_del_init(&rdev->same_set);
6150 if (bind_rdev_to_array(rdev, mddev))
6151 export_rdev(rdev);
6152 }
6153 autorun_array(mddev);
6154 mddev_unlock(mddev);
6155 }
6156 /* on success, candidates will be empty, on error
6157 * it won't...
6158 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006159 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006160 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 mddev_put(mddev);
6164 }
NeilBrown9d487392016-11-02 14:16:49 +11006165 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006167#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168
NeilBrownf72ffdd2014-09-30 14:23:59 +10006169static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170{
6171 mdu_version_t ver;
6172
6173 ver.major = MD_MAJOR_VERSION;
6174 ver.minor = MD_MINOR_VERSION;
6175 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6176
6177 if (copy_to_user(arg, &ver, sizeof(ver)))
6178 return -EFAULT;
6179
6180 return 0;
6181}
6182
NeilBrownf72ffdd2014-09-30 14:23:59 +10006183static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184{
6185 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006186 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006187 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188
NeilBrown1ca69c42012-10-11 13:37:33 +11006189 nr = working = insync = failed = spare = 0;
6190 rcu_read_lock();
6191 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006193 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 failed++;
6195 else {
6196 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006197 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006198 insync++;
Song Liub347af82016-08-11 17:14:45 -07006199 else if (test_bit(Journal, &rdev->flags))
6200 /* TODO: add journal count to md_u.h */
6201 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 else
6203 spare++;
6204 }
6205 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006206 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207
6208 info.major_version = mddev->major_version;
6209 info.minor_version = mddev->minor_version;
6210 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006211 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006213 info.size = mddev->dev_sectors / 2;
6214 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006215 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 info.nr_disks = nr;
6217 info.raid_disks = mddev->raid_disks;
6218 info.md_minor = mddev->md_minor;
6219 info.not_persistent= !mddev->persistent;
6220
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006221 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 info.state = 0;
6223 if (mddev->in_sync)
6224 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006225 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006226 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006227 if (mddev_is_clustered(mddev))
6228 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006229 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 info.working_disks = working;
6231 info.failed_disks = failed;
6232 info.spare_disks = spare;
6233
6234 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006235 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236
6237 if (copy_to_user(arg, &info, sizeof(info)))
6238 return -EFAULT;
6239
6240 return 0;
6241}
6242
NeilBrownf72ffdd2014-09-30 14:23:59 +10006243static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006244{
6245 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006246 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006247 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006248
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006249 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006250 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006251 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006252
NeilBrown32a76272005-06-21 17:17:14 -07006253 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006254 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006255 /* bitmap enabled */
6256 if (mddev->bitmap_info.file) {
6257 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6258 sizeof(file->pathname));
6259 if (IS_ERR(ptr))
6260 err = PTR_ERR(ptr);
6261 else
6262 memmove(file->pathname, ptr,
6263 sizeof(file->pathname)-(ptr-file->pathname));
6264 }
NeilBrown4af1a042014-12-15 12:57:00 +11006265 spin_unlock(&mddev->lock);
6266
6267 if (err == 0 &&
6268 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006269 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006270
NeilBrown32a76272005-06-21 17:17:14 -07006271 kfree(file);
6272 return err;
6273}
6274
NeilBrownf72ffdd2014-09-30 14:23:59 +10006275static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276{
6277 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006278 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279
6280 if (copy_from_user(&info, arg, sizeof(info)))
6281 return -EFAULT;
6282
NeilBrown1ca69c42012-10-11 13:37:33 +11006283 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006284 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 if (rdev) {
6286 info.major = MAJOR(rdev->bdev->bd_dev);
6287 info.minor = MINOR(rdev->bdev->bd_dev);
6288 info.raid_disk = rdev->raid_disk;
6289 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006290 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006292 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 info.state |= (1<<MD_DISK_ACTIVE);
6294 info.state |= (1<<MD_DISK_SYNC);
6295 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006296 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006297 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006298 if (test_bit(WriteMostly, &rdev->flags))
6299 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006300 if (test_bit(FailFast, &rdev->flags))
6301 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 } else {
6303 info.major = info.minor = 0;
6304 info.raid_disk = -1;
6305 info.state = (1<<MD_DISK_REMOVED);
6306 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006307 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308
6309 if (copy_to_user(arg, &info, sizeof(info)))
6310 return -EFAULT;
6311
6312 return 0;
6313}
6314
NeilBrownf72ffdd2014-09-30 14:23:59 +10006315static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316{
6317 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006318 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 dev_t dev = MKDEV(info->major,info->minor);
6320
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006321 if (mddev_is_clustered(mddev) &&
6322 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006323 pr_warn("%s: Cannot add to clustered mddev.\n",
6324 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006325 return -EINVAL;
6326 }
6327
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6329 return -EOVERFLOW;
6330
6331 if (!mddev->raid_disks) {
6332 int err;
6333 /* expecting a device which has a superblock */
6334 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6335 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006336 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 PTR_ERR(rdev));
6338 return PTR_ERR(rdev);
6339 }
6340 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006341 struct md_rdev *rdev0
6342 = list_entry(mddev->disks.next,
6343 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006344 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 .load_super(rdev, rdev0, mddev->minor_version);
6346 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006347 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006348 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 bdevname(rdev0->bdev,b2));
6350 export_rdev(rdev);
6351 return -EINVAL;
6352 }
6353 }
6354 err = bind_rdev_to_array(rdev, mddev);
6355 if (err)
6356 export_rdev(rdev);
6357 return err;
6358 }
6359
6360 /*
6361 * add_new_disk can be used once the array is assembled
6362 * to add "hot spares". They must already have a superblock
6363 * written
6364 */
6365 if (mddev->pers) {
6366 int err;
6367 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006368 pr_warn("%s: personality does not support diskops!\n",
6369 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 return -EINVAL;
6371 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006372 if (mddev->persistent)
6373 rdev = md_import_device(dev, mddev->major_version,
6374 mddev->minor_version);
6375 else
6376 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006378 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 PTR_ERR(rdev));
6380 return PTR_ERR(rdev);
6381 }
NeilBrown1a855a02010-12-09 16:36:28 +11006382 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006383 if (!mddev->persistent) {
6384 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006385 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006386 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006387 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006388 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006389 } else
NeilBrown41158c72005-06-21 17:17:25 -07006390 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006391 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006392 } else
6393 super_types[mddev->major_version].
6394 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006395 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006396 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006397 /* This was a hot-add request, but events doesn't
6398 * match, so reject it.
6399 */
6400 export_rdev(rdev);
6401 return -EINVAL;
6402 }
6403
NeilBrownb2d444d2005-11-08 21:39:31 -08006404 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006405 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6406 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006407 else
6408 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006409 if (info->state & (1<<MD_DISK_FAILFAST))
6410 set_bit(FailFast, &rdev->flags);
6411 else
6412 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006413
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006414 if (info->state & (1<<MD_DISK_JOURNAL)) {
6415 struct md_rdev *rdev2;
6416 bool has_journal = false;
6417
6418 /* make sure no existing journal disk */
6419 rdev_for_each(rdev2, mddev) {
6420 if (test_bit(Journal, &rdev2->flags)) {
6421 has_journal = true;
6422 break;
6423 }
6424 }
NeilBrown230b55f2017-10-17 14:24:09 +11006425 if (has_journal || mddev->bitmap) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006426 export_rdev(rdev);
6427 return -EBUSY;
6428 }
Song Liubac624f2015-08-13 14:31:55 -07006429 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006430 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006431 /*
6432 * check whether the device shows up in other nodes
6433 */
6434 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006435 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006436 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006437 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006438 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006439 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006440 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006441 export_rdev(rdev);
6442 return err;
6443 }
6444 }
6445 }
6446
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 rdev->raid_disk = -1;
6448 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006449
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 if (err)
6451 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006452
6453 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006454 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6455 if (!err) {
6456 err = md_cluster_ops->new_disk_ack(mddev,
6457 err == 0);
6458 if (err)
6459 md_kick_rdev_from_array(rdev);
6460 }
6461 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006462 if (err)
6463 md_cluster_ops->add_new_disk_cancel(mddev);
6464 else
6465 err = add_bound_rdev(rdev);
6466 }
6467
6468 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006469 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006470
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 return err;
6472 }
6473
6474 /* otherwise, add_new_disk is only allowed
6475 * for major_version==0 superblocks
6476 */
6477 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006478 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479 return -EINVAL;
6480 }
6481
6482 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6483 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006484 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006486 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 PTR_ERR(rdev));
6488 return PTR_ERR(rdev);
6489 }
6490 rdev->desc_nr = info->number;
6491 if (info->raid_disk < mddev->raid_disks)
6492 rdev->raid_disk = info->raid_disk;
6493 else
6494 rdev->raid_disk = -1;
6495
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006497 if (info->state & (1<<MD_DISK_SYNC))
6498 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006500 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6501 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006502 if (info->state & (1<<MD_DISK_FAILFAST))
6503 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006504
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006506 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006507 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6508 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006509 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006510 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511
NeilBrown2bf071b2006-01-06 00:20:55 -08006512 err = bind_rdev_to_array(rdev, mddev);
6513 if (err) {
6514 export_rdev(rdev);
6515 return err;
6516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 }
6518
6519 return 0;
6520}
6521
NeilBrownf72ffdd2014-09-30 14:23:59 +10006522static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523{
6524 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006525 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527 rdev = find_rdev(mddev, dev);
6528 if (!rdev)
6529 return -ENXIO;
6530
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006531 if (rdev->raid_disk < 0)
6532 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006533
NeilBrown3ea8929d2013-04-24 11:42:41 +10006534 clear_bit(Blocked, &rdev->flags);
6535 remove_and_add_spares(mddev, rdev);
6536
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 if (rdev->raid_disk >= 0)
6538 goto busy;
6539
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006540kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006541 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006542 md_cluster_ops->remove_disk(mddev, rdev);
6543
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006544 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006545 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006546 if (mddev->thread)
6547 md_wakeup_thread(mddev->thread);
6548 else
6549 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006550 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551
6552 return 0;
6553busy:
NeilBrown9d487392016-11-02 14:16:49 +11006554 pr_debug("md: cannot remove active disk %s from %s ...\n",
6555 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 return -EBUSY;
6557}
6558
NeilBrownf72ffdd2014-09-30 14:23:59 +10006559static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560{
6561 char b[BDEVNAME_SIZE];
6562 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006563 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564
6565 if (!mddev->pers)
6566 return -ENODEV;
6567
6568 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006569 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 mdname(mddev));
6571 return -EINVAL;
6572 }
6573 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006574 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575 mdname(mddev));
6576 return -EINVAL;
6577 }
6578
NeilBrownd710e132008-10-13 11:55:12 +11006579 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006581 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 PTR_ERR(rdev));
6583 return -EINVAL;
6584 }
6585
6586 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006587 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006589 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590
NeilBrown8190e752009-06-18 08:48:58 +10006591 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592
NeilBrownb2d444d2005-11-08 21:39:31 -08006593 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006594 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595 bdevname(rdev->bdev,b), mdname(mddev));
6596 err = -EINVAL;
6597 goto abort_export;
6598 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006599
NeilBrownb2d444d2005-11-08 21:39:31 -08006600 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006602 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006603 err = bind_rdev_to_array(rdev, mddev);
6604 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006605 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606
6607 /*
6608 * The rest should better be atomic, we can have disk failures
6609 * noticed in interrupt contexts ...
6610 */
6611
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 rdev->raid_disk = -1;
6613
Shaohua Li29530792016-12-08 15:48:19 -08006614 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006615 if (!mddev->thread)
6616 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617 /*
6618 * Kick recovery, maybe this spare has to be added to the
6619 * array immediately.
6620 */
6621 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6622 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006623 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624 return 0;
6625
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626abort_export:
6627 export_rdev(rdev);
6628 return err;
6629}
6630
NeilBrownfd01b882011-10-11 16:47:53 +11006631static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006632{
NeilBrown035328c2014-04-09 12:25:40 +10006633 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006634
NeilBrown36fa3062005-09-09 16:23:45 -07006635 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006636 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006637 return -EBUSY;
6638 if (mddev->recovery || mddev->sync_thread)
6639 return -EBUSY;
6640 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006641 }
6642
NeilBrown36fa3062005-09-09 16:23:45 -07006643 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006644 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006645 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006646
NeilBrown1e594bb2014-12-15 12:57:00 +11006647 if (mddev->bitmap || mddev->bitmap_info.file)
6648 return -EEXIST; /* cannot add when bitmap is present */
6649 f = fget(fd);
6650
6651 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006652 pr_warn("%s: error: failed to get bitmap file\n",
6653 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006654 return -EBADF;
6655 }
6656
NeilBrown1e594bb2014-12-15 12:57:00 +11006657 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006658 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006659 pr_warn("%s: error: bitmap file must be a regular file\n",
6660 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006661 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006662 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006663 pr_warn("%s: error: bitmap file must open for write\n",
6664 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006665 err = -EBADF;
6666 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006667 pr_warn("%s: error: bitmap file is already in use\n",
6668 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006669 err = -EBUSY;
6670 }
6671 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006672 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006673 return err;
6674 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006675 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006676 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006677 } else if (mddev->bitmap == NULL)
6678 return -ENOENT; /* cannot remove what isn't there */
6679 err = 0;
6680 if (mddev->pers) {
NeilBrown69e51b42010-06-01 19:37:35 +10006681 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006682 struct bitmap *bitmap;
6683
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006684 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006685 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006686 if (!IS_ERR(bitmap)) {
6687 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006688 err = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006689 } else
6690 err = PTR_ERR(bitmap);
NeilBrown52a0d492017-10-17 13:46:43 +11006691 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006692 md_bitmap_destroy(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11006693 fd = -1;
6694 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006695 mddev_resume(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11006696 } else if (fd < 0) {
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006697 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006698 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006699 mddev_resume(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006700 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006701 }
6702 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006703 struct file *f = mddev->bitmap_info.file;
6704 if (f) {
6705 spin_lock(&mddev->lock);
6706 mddev->bitmap_info.file = NULL;
6707 spin_unlock(&mddev->lock);
6708 fput(f);
6709 }
NeilBrown36fa3062005-09-09 16:23:45 -07006710 }
6711
NeilBrown32a76272005-06-21 17:17:14 -07006712 return err;
6713}
6714
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715/*
6716 * set_array_info is used two different ways
6717 * The original usage is when creating a new array.
6718 * In this usage, raid_disks is > 0 and it together with
6719 * level, size, not_persistent,layout,chunksize determine the
6720 * shape of the array.
6721 * This will always create an array with a type-0.90.0 superblock.
6722 * The newer usage is when assembling an array.
6723 * In this case raid_disks will be 0, and the major_version field is
6724 * use to determine which style super-blocks are to be found on the devices.
6725 * The minor and patch _version numbers are also kept incase the
6726 * super_block handler wishes to interpret them.
6727 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006728static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729{
6730
6731 if (info->raid_disks == 0) {
6732 /* just setting version number for superblock loading */
6733 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006734 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 super_types[info->major_version].name == NULL) {
6736 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11006737 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738 info->major_version);
6739 return -EINVAL;
6740 }
6741 mddev->major_version = info->major_version;
6742 mddev->minor_version = info->minor_version;
6743 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006744 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006745 /* ensure mddev_put doesn't delete this now that there
6746 * is some minimal configuration.
6747 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006748 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749 return 0;
6750 }
6751 mddev->major_version = MD_MAJOR_VERSION;
6752 mddev->minor_version = MD_MINOR_VERSION;
6753 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006754 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755
6756 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006757 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006758 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 mddev->raid_disks = info->raid_disks;
6760 /* don't set md_minor, it is determined by which /dev/md* was
6761 * openned
6762 */
6763 if (info->state & (1<<MD_SB_CLEAN))
6764 mddev->recovery_cp = MaxSector;
6765 else
6766 mddev->recovery_cp = 0;
6767 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006768 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769
6770 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006771 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772
Shaohua Li29530792016-12-08 15:48:19 -08006773 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11006774 mddev->max_disks = MD_SB_DISKS;
6775 mddev->flags = 0;
6776 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006777 }
6778 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779
NeilBrownc3d97142009-12-14 12:49:52 +11006780 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006781 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006782 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006783
NeilBrownf6705572006-03-27 01:18:11 -08006784 mddev->reshape_position = MaxSector;
6785
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 /*
6787 * Generate a 128 bit UUID
6788 */
6789 get_random_bytes(mddev->uuid, 16);
6790
NeilBrownf6705572006-03-27 01:18:11 -08006791 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006792 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006793 mddev->new_layout = mddev->layout;
6794 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006795 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006796
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797 return 0;
6798}
6799
NeilBrownfd01b882011-10-11 16:47:53 +11006800void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006801{
Shaohua Liefa4b772017-10-18 22:08:13 -07006802 lockdep_assert_held(&mddev->reconfig_mutex);
Dan Williamsb522adc2009-03-31 15:00:31 +11006803
6804 if (mddev->external_size)
6805 return;
6806
Dan Williams1f403622009-03-31 14:59:03 +11006807 mddev->array_sectors = array_sectors;
6808}
6809EXPORT_SYMBOL(md_set_array_sectors);
6810
NeilBrownfd01b882011-10-11 16:47:53 +11006811static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006812{
NeilBrown3cb03002011-10-11 16:45:26 +11006813 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006814 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006815 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08006816 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006817
NeilBrowna35b0d62006-01-06 00:20:49 -08006818 if (mddev->pers->resize == NULL)
6819 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006820 /* The "num_sectors" is the number of sectors of each device that
6821 * is used. This can only make sense for arrays with redundancy.
6822 * linear and raid0 always use whatever space is available. We can only
6823 * consider changing this number if no resync or reconstruction is
6824 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006825 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006826 * of each device. If num_sectors is zero, we find the largest size
6827 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006828 */
NeilBrownf851b602014-12-11 10:02:10 +11006829 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6830 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006831 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006832 if (mddev->ro)
6833 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006834
NeilBrowndafb20f2012-03-19 12:46:39 +11006835 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006836 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006837
Andre Nolld71f9f82008-07-11 22:02:22 +10006838 if (fit && (num_sectors == 0 || num_sectors > avail))
6839 num_sectors = avail;
6840 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006841 return -ENOSPC;
6842 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006843 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08006844 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08006845 if (mddev_is_clustered(mddev))
6846 md_cluster_ops->update_size(mddev, old_dev_sectors);
6847 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08006848 set_capacity(mddev->gendisk, mddev->array_sectors);
6849 revalidate_disk(mddev->gendisk);
6850 }
6851 }
NeilBrowna35b0d62006-01-06 00:20:49 -08006852 return rv;
6853}
6854
NeilBrownfd01b882011-10-11 16:47:53 +11006855static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006856{
6857 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006858 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006859 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006860 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006861 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006862 if (mddev->ro)
6863 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006864 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006865 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006866 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006867 if (mddev->sync_thread ||
6868 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6869 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006870 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006871
6872 rdev_for_each(rdev, mddev) {
6873 if (mddev->raid_disks < raid_disks &&
6874 rdev->data_offset < rdev->new_data_offset)
6875 return -EINVAL;
6876 if (mddev->raid_disks > raid_disks &&
6877 rdev->data_offset > rdev->new_data_offset)
6878 return -EINVAL;
6879 }
6880
NeilBrown63c70c42006-03-27 01:18:13 -08006881 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006882 if (mddev->delta_disks < 0)
6883 mddev->reshape_backwards = 1;
6884 else if (mddev->delta_disks > 0)
6885 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006886
6887 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006888 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006889 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006890 mddev->reshape_backwards = 0;
6891 }
NeilBrownda943b992006-01-06 00:20:54 -08006892 return rv;
6893}
6894
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895/*
6896 * update_array_info is used to change the configuration of an
6897 * on-line array.
6898 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6899 * fields in the info are checked against the array.
6900 * Any differences that cannot be handled will cause an error.
6901 * Normally, only one change can be managed at a time.
6902 */
NeilBrownfd01b882011-10-11 16:47:53 +11006903static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904{
6905 int rv = 0;
6906 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006907 int state = 0;
6908
6909 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006910 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006911 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912
6913 if (mddev->major_version != info->major_version ||
6914 mddev->minor_version != info->minor_version ||
6915/* mddev->patch_version != info->patch_version || */
6916 mddev->ctime != info->ctime ||
6917 mddev->level != info->level ||
6918/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006919 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006920 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006921 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6922 ((state^info->state) & 0xfffffe00)
6923 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924 return -EINVAL;
6925 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006926 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6927 cnt++;
6928 if (mddev->raid_disks != info->raid_disks)
6929 cnt++;
6930 if (mddev->layout != info->layout)
6931 cnt++;
6932 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6933 cnt++;
6934 if (cnt == 0)
6935 return 0;
6936 if (cnt > 1)
6937 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938
6939 if (mddev->layout != info->layout) {
6940 /* Change layout
6941 * we don't need to do anything at the md level, the
6942 * personality will take care of it all.
6943 */
NeilBrown50ac1682009-06-18 08:47:55 +10006944 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006946 else {
6947 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006948 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006949 if (rv)
6950 mddev->new_layout = mddev->layout;
6951 return rv;
6952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006954 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006955 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956
NeilBrownda943b992006-01-06 00:20:54 -08006957 if (mddev->raid_disks != info->raid_disks)
6958 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959
NeilBrown36fa3062005-09-09 16:23:45 -07006960 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006961 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6962 rv = -EINVAL;
6963 goto err;
6964 }
6965 if (mddev->recovery || mddev->sync_thread) {
6966 rv = -EBUSY;
6967 goto err;
6968 }
NeilBrown36fa3062005-09-09 16:23:45 -07006969 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006970 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006971 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006972 if (mddev->bitmap) {
6973 rv = -EEXIST;
6974 goto err;
6975 }
6976 if (mddev->bitmap_info.default_offset == 0) {
6977 rv = -EINVAL;
6978 goto err;
6979 }
NeilBrownc3d97142009-12-14 12:49:52 +11006980 mddev->bitmap_info.offset =
6981 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006982 mddev->bitmap_info.space =
6983 mddev->bitmap_info.default_space;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006984 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006985 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006986 if (!IS_ERR(bitmap)) {
6987 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006988 rv = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006989 } else
6990 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006991 if (rv)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006992 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006993 mddev_resume(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006994 } else {
6995 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006996 if (!mddev->bitmap) {
6997 rv = -ENOENT;
6998 goto err;
6999 }
7000 if (mddev->bitmap->storage.file) {
7001 rv = -EINVAL;
7002 goto err;
7003 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11007004 if (mddev->bitmap_info.nodes) {
7005 /* hold PW on all the bitmap lock */
7006 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11007007 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 +11007008 rv = -EPERM;
7009 md_cluster_ops->unlock_all_bitmaps(mddev);
7010 goto err;
7011 }
7012
7013 mddev->bitmap_info.nodes = 0;
7014 md_cluster_ops->leave(mddev);
7015 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007016 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007017 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007018 mddev_resume(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11007019 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007020 }
7021 }
NeilBrown850b2b422006-10-03 01:15:46 -07007022 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007023 return rv;
7024err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025 return rv;
7026}
7027
NeilBrownfd01b882011-10-11 16:47:53 +11007028static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029{
NeilBrown3cb03002011-10-11 16:45:26 +11007030 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11007031 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032
7033 if (mddev->pers == NULL)
7034 return -ENODEV;
7035
NeilBrown1ca69c42012-10-11 13:37:33 +11007036 rcu_read_lock();
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01007037 rdev = md_find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11007039 err = -ENODEV;
7040 else {
7041 md_error(mddev, rdev);
7042 if (!test_bit(Faulty, &rdev->flags))
7043 err = -EBUSY;
7044 }
7045 rcu_read_unlock();
7046 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047}
7048
Andre Noll2f9618c2008-04-25 18:57:58 +02007049/*
7050 * We have a problem here : there is no easy way to give a CHS
7051 * virtual geometry. We currently pretend that we have a 2 heads
7052 * 4 sectors (with a BIG number of cylinders...). This drives
7053 * dosfs just mad... ;-)
7054 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007055static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7056{
NeilBrownfd01b882011-10-11 16:47:53 +11007057 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007058
7059 geo->heads = 2;
7060 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11007061 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007062 return 0;
7063}
7064
Nicolas Schichancb335f82014-01-15 16:58:52 +01007065static inline bool md_ioctl_valid(unsigned int cmd)
7066{
7067 switch (cmd) {
7068 case ADD_NEW_DISK:
7069 case BLKROSET:
7070 case GET_ARRAY_INFO:
7071 case GET_BITMAP_FILE:
7072 case GET_DISK_INFO:
7073 case HOT_ADD_DISK:
7074 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007075 case RAID_AUTORUN:
7076 case RAID_VERSION:
7077 case RESTART_ARRAY_RW:
7078 case RUN_ARRAY:
7079 case SET_ARRAY_INFO:
7080 case SET_BITMAP_FILE:
7081 case SET_DISK_FAULTY:
7082 case STOP_ARRAY:
7083 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007084 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007085 return true;
7086 default:
7087 return false;
7088 }
7089}
7090
Al Viroa39907f2008-03-02 10:31:15 -05007091static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 unsigned int cmd, unsigned long arg)
7093{
7094 int err = 0;
7095 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11007096 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10007097 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08007098 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099
Nicolas Schichancb335f82014-01-15 16:58:52 +01007100 if (!md_ioctl_valid(cmd))
7101 return -ENOTTY;
7102
NeilBrown506c9e42011-12-23 10:17:26 +11007103 switch (cmd) {
7104 case RAID_VERSION:
7105 case GET_ARRAY_INFO:
7106 case GET_DISK_INFO:
7107 break;
7108 default:
7109 if (!capable(CAP_SYS_ADMIN))
7110 return -EACCES;
7111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112
7113 /*
7114 * Commands dealing with the RAID driver but not any
7115 * particular array:
7116 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007117 switch (cmd) {
7118 case RAID_VERSION:
7119 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11007123 case RAID_AUTORUN:
7124 err = 0;
7125 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007126 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11007128 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 }
7130
7131 /*
7132 * Commands creating/starting a new array:
7133 */
7134
Al Viroa39907f2008-03-02 10:31:15 -05007135 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136
7137 if (!mddev) {
7138 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007139 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 }
7141
NeilBrown1ca69c42012-10-11 13:37:33 +11007142 /* Some actions do not requires the mutex */
7143 switch (cmd) {
7144 case GET_ARRAY_INFO:
7145 if (!mddev->raid_disks && !mddev->external)
7146 err = -ENODEV;
7147 else
7148 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007149 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007150
7151 case GET_DISK_INFO:
7152 if (!mddev->raid_disks && !mddev->external)
7153 err = -ENODEV;
7154 else
7155 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007156 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007157
7158 case SET_DISK_FAULTY:
7159 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007160 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007161
7162 case GET_BITMAP_FILE:
7163 err = get_bitmap_file(mddev, argp);
7164 goto out;
7165
NeilBrown1ca69c42012-10-11 13:37:33 +11007166 }
7167
NeilBrowna7a3f082012-12-11 13:35:54 +11007168 if (cmd == ADD_NEW_DISK)
7169 /* need to ensure md_delayed_delete() has completed */
7170 flush_workqueue(md_misc_wq);
7171
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007172 if (cmd == HOT_REMOVE_DISK)
7173 /* need to ensure recovery thread has run */
7174 wait_event_interruptible_timeout(mddev->sb_wait,
7175 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007176 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007177 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007178 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7179 /* Need to flush page cache, and ensure no-one else opens
7180 * and writes
7181 */
7182 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007183 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007184 mutex_unlock(&mddev->open_mutex);
7185 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007186 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007187 }
NeilBrown065e5192017-04-06 11:16:33 +08007188 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007189 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007190 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007191 mutex_unlock(&mddev->open_mutex);
7192 sync_blockdev(bdev);
7193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194 err = mddev_lock(mddev);
7195 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007196 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7197 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007198 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 }
7200
NeilBrownc02c0ae2012-12-11 13:39:21 +11007201 if (cmd == SET_ARRAY_INFO) {
7202 mdu_array_info_t info;
7203 if (!arg)
7204 memset(&info, 0, sizeof(info));
7205 else if (copy_from_user(&info, argp, sizeof(info))) {
7206 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007207 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007208 }
7209 if (mddev->pers) {
7210 err = update_array_info(mddev, &info);
7211 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007212 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007213 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007215 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007216 }
7217 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007218 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007219 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007220 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007221 }
7222 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007223 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007224 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007225 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007226 }
7227 err = set_array_info(mddev, &info);
7228 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007229 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007230 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007231 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007232 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233 }
7234
7235 /*
7236 * Commands querying/configuring an existing array:
7237 */
NeilBrown32a76272005-06-21 17:17:14 -07007238 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007239 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007240 if ((!mddev->raid_disks && !mddev->external)
7241 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7242 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7243 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007245 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 }
7247
7248 /*
7249 * Commands even a read-only array can execute:
7250 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007251 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007252 case RESTART_ARRAY_RW:
7253 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007254 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007255
7256 case STOP_ARRAY:
7257 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007258 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007259
7260 case STOP_ARRAY_RO:
7261 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007262 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007263
NeilBrown3ea8929d2013-04-24 11:42:41 +10007264 case HOT_REMOVE_DISK:
7265 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007266 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007267
NeilBrown7ceb17e2013-04-24 11:42:42 +10007268 case ADD_NEW_DISK:
7269 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007270 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007271 * So require mddev->pers and MD_DISK_SYNC.
7272 */
7273 if (mddev->pers) {
7274 mdu_disk_info_t info;
7275 if (copy_from_user(&info, argp, sizeof(info)))
7276 err = -EFAULT;
7277 else if (!(info.state & (1<<MD_DISK_SYNC)))
7278 /* Need to clear read-only for this */
7279 break;
7280 else
7281 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007282 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007283 }
7284 break;
7285
NeilBrownc02c0ae2012-12-11 13:39:21 +11007286 case BLKROSET:
7287 if (get_user(ro, (int __user *)(arg))) {
7288 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007289 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007290 }
7291 err = -EINVAL;
7292
7293 /* if the bdev is going readonly the value of mddev->ro
7294 * does not matter, no writes are coming
7295 */
7296 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007297 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007298
NeilBrownc02c0ae2012-12-11 13:39:21 +11007299 /* are we are already prepared for writes? */
7300 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007301 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007302
7303 /* transitioning to readauto need only happen for
7304 * arrays that call md_write_start
7305 */
7306 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007308 if (err == 0) {
7309 mddev->ro = 2;
7310 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007311 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007312 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007313 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314 }
7315
7316 /*
7317 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007318 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319 */
NeilBrown326eb172014-09-30 15:36:28 +10007320 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007321 if (mddev->ro == 2) {
7322 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007323 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007324 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007325 /* mddev_unlock will wake thread */
7326 /* If a device failed while we were read-only, we
7327 * need to make sure the metadata is updated now.
7328 */
Shaohua Li29530792016-12-08 15:48:19 -08007329 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007330 mddev_unlock(mddev);
7331 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007332 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7333 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007334 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007335 }
NeilBrownf91de922005-11-08 21:39:36 -08007336 } else {
7337 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007338 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340 }
7341
NeilBrownc02c0ae2012-12-11 13:39:21 +11007342 switch (cmd) {
7343 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007345 mdu_disk_info_t info;
7346 if (copy_from_user(&info, argp, sizeof(info)))
7347 err = -EFAULT;
7348 else
7349 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007350 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007353 case CLUSTERED_DISK_NACK:
7354 if (mddev_is_clustered(mddev))
7355 md_cluster_ops->new_disk_ack(mddev, false);
7356 else
7357 err = -EINVAL;
7358 goto unlock;
7359
NeilBrownc02c0ae2012-12-11 13:39:21 +11007360 case HOT_ADD_DISK:
7361 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007362 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363
NeilBrownc02c0ae2012-12-11 13:39:21 +11007364 case RUN_ARRAY:
7365 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007366 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367
NeilBrownc02c0ae2012-12-11 13:39:21 +11007368 case SET_BITMAP_FILE:
7369 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007370 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007371
NeilBrownc02c0ae2012-12-11 13:39:21 +11007372 default:
7373 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007374 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 }
7376
NeilBrown3adc28d2014-09-30 15:46:41 +10007377unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007378 if (mddev->hold_active == UNTIL_IOCTL &&
7379 err != -EINVAL)
7380 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007382out:
NeilBrown065e5192017-04-06 11:16:33 +08007383 if(did_set_md_closing)
7384 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 return err;
7386}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007387#ifdef CONFIG_COMPAT
7388static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7389 unsigned int cmd, unsigned long arg)
7390{
7391 switch (cmd) {
7392 case HOT_REMOVE_DISK:
7393 case HOT_ADD_DISK:
7394 case SET_DISK_FAULTY:
7395 case SET_BITMAP_FILE:
7396 /* These take in integer arg, do not convert */
7397 break;
7398 default:
7399 arg = (unsigned long)compat_ptr(arg);
7400 break;
7401 }
7402
7403 return md_ioctl(bdev, mode, cmd, arg);
7404}
7405#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406
Al Viroa39907f2008-03-02 10:31:15 -05007407static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408{
7409 /*
7410 * Succeed if we can lock the mddev, which confirms that
7411 * it isn't being stopped right now.
7412 */
NeilBrownfd01b882011-10-11 16:47:53 +11007413 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414 int err;
7415
Yuanhan Liu0c098222012-05-22 13:55:32 +10007416 if (!mddev)
7417 return -ENODEV;
7418
NeilBrownd3374822009-01-09 08:31:10 +11007419 if (mddev->gendisk != bdev->bd_disk) {
7420 /* we are racing with mddev_put which is discarding this
7421 * bd_disk.
7422 */
7423 mddev_put(mddev);
7424 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007425 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007426 /* Then retry the open from the top */
7427 return -ERESTARTSYS;
7428 }
7429 BUG_ON(mddev != bdev->bd_disk->private_data);
7430
NeilBrownc8c00a62009-08-10 12:50:52 +10007431 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432 goto out;
7433
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007434 if (test_bit(MD_CLOSING, &mddev->flags)) {
7435 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007436 err = -ENODEV;
7437 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007438 }
7439
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007441 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007442 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007444 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007446 if (err)
7447 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 return err;
7449}
7450
Al Virodb2a1442013-05-05 21:52:57 -04007451static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007453 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007455 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007456 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007459
7460static int md_media_changed(struct gendisk *disk)
7461{
NeilBrownfd01b882011-10-11 16:47:53 +11007462 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007463
7464 return mddev->changed;
7465}
7466
7467static int md_revalidate(struct gendisk *disk)
7468{
NeilBrownfd01b882011-10-11 16:47:53 +11007469 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007470
7471 mddev->changed = 0;
7472 return 0;
7473}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007474static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475{
7476 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007477 .open = md_open,
7478 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007479 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007480#ifdef CONFIG_COMPAT
7481 .compat_ioctl = md_compat_ioctl,
7482#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007483 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007484 .media_changed = md_media_changed,
7485 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007486};
7487
NeilBrownf72ffdd2014-09-30 14:23:59 +10007488static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489{
NeilBrown2b8bf342011-10-11 16:48:23 +11007490 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491
Linus Torvalds1da177e2005-04-16 15:20:36 -07007492 /*
7493 * md_thread is a 'system-thread', it's priority should be very
7494 * high. We avoid resource deadlocks individually in each
7495 * raid personality. (RAID5 does preallocation) We also use RR and
7496 * the very same RT priority as kswapd, thus we will never get
7497 * into a priority inversion deadlock.
7498 *
7499 * we definitely have to have equal or higher priority than
7500 * bdflush, otherwise bdflush will deadlock if there are too
7501 * many dirty RAID5 blocks.
7502 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503
NeilBrown6985c432005-10-19 21:23:47 -07007504 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007505 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506
NeilBrown93588e22005-11-15 00:09:12 -08007507 /* We need to wait INTERRUPTIBLE so that
7508 * we don't add to the load-average.
7509 * That means we need to be sure no signals are
7510 * pending
7511 */
7512 if (signal_pending(current))
7513 flush_signals(current);
7514
7515 wait_event_interruptible_timeout
7516 (thread->wqueue,
7517 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007518 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007519 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520
NeilBrown6c987912011-01-14 09:13:53 +11007521 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007522 if (kthread_should_park())
7523 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007524 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007525 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007527
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528 return 0;
7529}
7530
NeilBrown2b8bf342011-10-11 16:48:23 +11007531void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532{
7533 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007534 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Guoqing Jiangd1d90142017-10-09 10:32:48 +08007535 set_bit(THREAD_WAKEUP, &thread->flags);
7536 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537 }
7538}
NeilBrown6c144d32014-09-30 16:15:38 +10007539EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007540
Shaohua Li4ed87312012-10-11 13:34:00 +11007541struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7542 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007543{
NeilBrown2b8bf342011-10-11 16:48:23 +11007544 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545
NeilBrown2b8bf342011-10-11 16:48:23 +11007546 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 if (!thread)
7548 return NULL;
7549
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 init_waitqueue_head(&thread->wqueue);
7551
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552 thread->run = run;
7553 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007554 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007555 thread->tsk = kthread_run(md_thread, thread,
7556 "%s_%s",
7557 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007558 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007559 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 kfree(thread);
7561 return NULL;
7562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563 return thread;
7564}
NeilBrown6c144d32014-09-30 16:15:38 +10007565EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566
NeilBrown2b8bf342011-10-11 16:48:23 +11007567void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568{
NeilBrown2b8bf342011-10-11 16:48:23 +11007569 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007570 if (!thread)
7571 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007572 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007573 /* Locking ensures that mddev_unlock does not wake_up a
7574 * non-existent thread
7575 */
7576 spin_lock(&pers_lock);
7577 *threadp = NULL;
7578 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007579
7580 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 kfree(thread);
7582}
NeilBrown6c144d32014-09-30 16:15:38 +10007583EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584
NeilBrownfd01b882011-10-11 16:47:53 +11007585void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586{
NeilBrownb2d444d2005-11-08 21:39:31 -08007587 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007589
NeilBrownde393cd2011-07-28 11:31:48 +10007590 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 return;
7592 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007593 if (mddev->degraded)
7594 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007595 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7597 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7598 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007599 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007600 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007601 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007602}
NeilBrown6c144d32014-09-30 16:15:38 +10007603EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604
7605/* seq_file implementation /proc/mdstat */
7606
7607static void status_unused(struct seq_file *seq)
7608{
7609 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007610 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007611
7612 seq_printf(seq, "unused devices: ");
7613
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007614 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615 char b[BDEVNAME_SIZE];
7616 i++;
7617 seq_printf(seq, "%s ",
7618 bdevname(rdev->bdev,b));
7619 }
7620 if (!i)
7621 seq_printf(seq, "<none>");
7622
7623 seq_printf(seq, "\n");
7624}
7625
NeilBrownf7851be2015-07-02 17:12:58 +10007626static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627{
NeilBrowndd71cf62009-05-07 12:49:35 +10007628 sector_t max_sectors, resync, res;
7629 unsigned long dt, db;
7630 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007631 int scale;
7632 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633
NeilBrownc804cde2012-05-21 09:28:33 +10007634 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7635 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007636 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007638 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639
NeilBrownf7851be2015-07-02 17:12:58 +10007640 resync = mddev->curr_resync;
7641 if (resync <= 3) {
7642 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7643 /* Still cleaning up */
7644 resync = max_sectors;
Nate Daileyd2e2ec82017-11-30 11:33:30 -05007645 } else if (resync > max_sectors)
7646 resync = max_sectors;
7647 else
NeilBrownf7851be2015-07-02 17:12:58 +10007648 resync -= atomic_read(&mddev->recovery_active);
7649
7650 if (resync == 0) {
7651 if (mddev->recovery_cp < MaxSector) {
7652 seq_printf(seq, "\tresync=PENDING");
7653 return 1;
7654 }
7655 return 0;
7656 }
7657 if (resync < 3) {
7658 seq_printf(seq, "\tresync=DELAYED");
7659 return 1;
7660 }
7661
NeilBrown403df472014-09-30 15:52:29 +10007662 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007663 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007664 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007665 * u32, as those are the requirements for sector_div.
7666 * Thus 'scale' must be at least 10
7667 */
7668 scale = 10;
7669 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007670 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007671 scale++;
7672 }
7673 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007674 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007675
7676 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677 {
NeilBrown4588b422006-03-27 01:18:04 -08007678 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679 seq_printf(seq, "[");
7680 for (i = 0; i < x; i++)
7681 seq_printf(seq, "=");
7682 seq_printf(seq, ">");
7683 for (i = 0; i < y; i++)
7684 seq_printf(seq, ".");
7685 seq_printf(seq, "] ");
7686 }
NeilBrown4588b422006-03-27 01:18:04 -08007687 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007688 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7689 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007690 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7691 "check" :
7692 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7693 "resync" : "recovery"))),
7694 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007695 (unsigned long long) resync/2,
7696 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697
7698 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699 * dt: time from mark until now
7700 * db: blocks written from mark until now
7701 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007702 *
7703 * rt is a sector_t, so could be 32bit or 64bit.
7704 * So we divide before multiply in case it is 32bit and close
7705 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007706 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007707 * near the end of resync when the number of remaining sectors
7708 * is close to 'db'.
7709 * We then divide rt by 32 after multiplying by db to compensate.
7710 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711 */
7712 dt = ((jiffies - mddev->resync_mark) / HZ);
7713 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007714 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7715 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007716
NeilBrowndd71cf62009-05-07 12:49:35 +10007717 rt = max_sectors - resync; /* number of remaining sectors */
7718 sector_div(rt, db/32+1);
7719 rt *= dt;
7720 rt >>= 5;
7721
7722 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7723 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007724
NeilBrownff4e8d92006-07-10 04:44:16 -07007725 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007726 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727}
7728
7729static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7730{
7731 struct list_head *tmp;
7732 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007733 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007734
7735 if (l >= 0x10000)
7736 return NULL;
7737 if (!l--)
7738 /* header */
7739 return (void*)1;
7740
7741 spin_lock(&all_mddevs_lock);
7742 list_for_each(tmp,&all_mddevs)
7743 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007744 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745 mddev_get(mddev);
7746 spin_unlock(&all_mddevs_lock);
7747 return mddev;
7748 }
7749 spin_unlock(&all_mddevs_lock);
7750 if (!l--)
7751 return (void*)2;/* tail */
7752 return NULL;
7753}
7754
7755static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7756{
7757 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007758 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007759
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760 ++*pos;
7761 if (v == (void*)2)
7762 return NULL;
7763
7764 spin_lock(&all_mddevs_lock);
7765 if (v == (void*)1)
7766 tmp = all_mddevs.next;
7767 else
7768 tmp = mddev->all_mddevs.next;
7769 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007770 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771 else {
7772 next_mddev = (void*)2;
7773 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007775 spin_unlock(&all_mddevs_lock);
7776
7777 if (v != (void*)1)
7778 mddev_put(mddev);
7779 return next_mddev;
7780
7781}
7782
7783static void md_seq_stop(struct seq_file *seq, void *v)
7784{
NeilBrownfd01b882011-10-11 16:47:53 +11007785 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786
7787 if (mddev && v != (void*)1 && v != (void*)2)
7788 mddev_put(mddev);
7789}
7790
7791static int md_seq_show(struct seq_file *seq, void *v)
7792{
NeilBrownfd01b882011-10-11 16:47:53 +11007793 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007794 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007795 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796
7797 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007798 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007799 seq_printf(seq, "Personalities : ");
7800 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007801 list_for_each_entry(pers, &pers_list, list)
7802 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803
7804 spin_unlock(&pers_lock);
7805 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007806 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807 return 0;
7808 }
7809 if (v == (void*)2) {
7810 status_unused(seq);
7811 return 0;
7812 }
7813
NeilBrown36d091f2014-12-15 12:56:58 +11007814 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007815 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7816 seq_printf(seq, "%s : %sactive", mdname(mddev),
7817 mddev->pers ? "" : "in");
7818 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007819 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007821 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007822 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823 seq_printf(seq, " %s", mddev->pers->name);
7824 }
7825
Andre Nolldd8ac332009-03-31 14:33:13 +11007826 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007827 rcu_read_lock();
7828 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829 char b[BDEVNAME_SIZE];
7830 seq_printf(seq, " %s[%d]",
7831 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007832 if (test_bit(WriteMostly, &rdev->flags))
7833 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007834 if (test_bit(Journal, &rdev->flags))
7835 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007836 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837 seq_printf(seq, "(F)");
7838 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007839 }
7840 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007841 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007842 if (test_bit(Replacement, &rdev->flags))
7843 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007844 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007846 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847
7848 if (!list_empty(&mddev->disks)) {
7849 if (mddev->pers)
7850 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007851 (unsigned long long)
7852 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853 else
7854 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007855 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007857 if (mddev->persistent) {
7858 if (mddev->major_version != 0 ||
7859 mddev->minor_version != 90) {
7860 seq_printf(seq," super %d.%d",
7861 mddev->major_version,
7862 mddev->minor_version);
7863 }
NeilBrowne6910632008-02-06 01:39:51 -08007864 } else if (mddev->external)
7865 seq_printf(seq, " super external:%s",
7866 mddev->metadata_type);
7867 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007868 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869
7870 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007871 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007872 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007873 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007874 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007875 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007876 }
NeilBrown32a76272005-06-21 17:17:14 -07007877 } else
7878 seq_printf(seq, "\n ");
7879
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007880 md_bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881
7882 seq_printf(seq, "\n");
7883 }
NeilBrown36d091f2014-12-15 12:56:58 +11007884 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007885
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886 return 0;
7887}
7888
Jan Engelhardt110518b2009-05-07 12:49:37 +10007889static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890 .start = md_seq_start,
7891 .next = md_seq_next,
7892 .stop = md_seq_stop,
7893 .show = md_seq_show,
7894};
7895
7896static int md_seq_open(struct inode *inode, struct file *file)
7897{
Kay Sieversf1514632011-07-12 20:48:39 +02007898 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899 int error;
7900
7901 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007902 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007903 return error;
7904
7905 seq = file->private_data;
7906 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007907 return error;
7908}
7909
NeilBrowne2f23b62014-04-09 14:33:51 +10007910static int md_unloading;
Al Viroafc9a422017-07-03 06:39:46 -04007911static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
NeilBrownd7603b72006-01-06 00:20:30 -08007912{
Kay Sieversf1514632011-07-12 20:48:39 +02007913 struct seq_file *seq = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -04007914 __poll_t mask;
NeilBrownd7603b72006-01-06 00:20:30 -08007915
NeilBrowne2f23b62014-04-09 14:33:51 +10007916 if (md_unloading)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007917 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007918 poll_wait(filp, &md_event_waiters, wait);
7919
7920 /* always allow read */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007921 mask = EPOLLIN | EPOLLRDNORM;
NeilBrownd7603b72006-01-06 00:20:30 -08007922
Kay Sieversf1514632011-07-12 20:48:39 +02007923 if (seq->poll_event != atomic_read(&md_event_count))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007924 mask |= EPOLLERR | EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007925 return mask;
7926}
7927
Arjan van de Venfa027c22007-02-12 00:55:33 -08007928static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007929 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930 .open = md_seq_open,
7931 .read = seq_read,
7932 .llseek = seq_lseek,
Cihangir Akturk26e13042017-07-29 19:52:45 +03007933 .release = seq_release,
NeilBrownd7603b72006-01-06 00:20:30 -08007934 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935};
7936
NeilBrown84fc4b52011-10-11 16:49:58 +11007937int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938{
NeilBrown9d487392016-11-02 14:16:49 +11007939 pr_debug("md: %s personality registered for level %d\n",
7940 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007942 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007943 spin_unlock(&pers_lock);
7944 return 0;
7945}
NeilBrown6c144d32014-09-30 16:15:38 +10007946EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007947
NeilBrown84fc4b52011-10-11 16:49:58 +11007948int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949{
NeilBrown9d487392016-11-02 14:16:49 +11007950 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007951 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007952 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953 spin_unlock(&pers_lock);
7954 return 0;
7955}
NeilBrown6c144d32014-09-30 16:15:38 +10007956EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957
NeilBrown6022e752015-08-13 12:32:55 +10007958int register_md_cluster_operations(struct md_cluster_operations *ops,
7959 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007960{
NeilBrown6022e752015-08-13 12:32:55 +10007961 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007962 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007963 if (md_cluster_ops != NULL)
7964 ret = -EALREADY;
7965 else {
7966 md_cluster_ops = ops;
7967 md_cluster_mod = module;
7968 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007969 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007970 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007971}
7972EXPORT_SYMBOL(register_md_cluster_operations);
7973
7974int unregister_md_cluster_operations(void)
7975{
7976 spin_lock(&pers_lock);
7977 md_cluster_ops = NULL;
7978 spin_unlock(&pers_lock);
7979 return 0;
7980}
7981EXPORT_SYMBOL(unregister_md_cluster_operations);
7982
7983int md_setup_cluster(struct mddev *mddev, int nodes)
7984{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007985 if (!md_cluster_ops)
7986 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007987 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007988 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007989 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11007990 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007991 spin_unlock(&pers_lock);
7992 return -ENOENT;
7993 }
7994 spin_unlock(&pers_lock);
7995
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007996 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007997}
7998
7999void md_cluster_stop(struct mddev *mddev)
8000{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05008001 if (!md_cluster_ops)
8002 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008003 md_cluster_ops->leave(mddev);
8004 module_put(md_cluster_mod);
8005}
8006
NeilBrownfd01b882011-10-11 16:47:53 +11008007static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008008{
NeilBrownf72ffdd2014-09-30 14:23:59 +10008009 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008010 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11008011 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008012
8013 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10008014 rcu_read_lock();
8015 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008016 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11008017 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
8018 (int)part_stat_read(&disk->part0, sectors[1]) -
8019 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07008020 /* sync IO will cause sync_io to increase before the disk_stats
8021 * as sync_io is counted when a request starts, and
8022 * disk_stats is counted when it completes.
8023 * So resync activity will cause curr_events to be smaller than
8024 * when there was no such activity.
8025 * non-sync IO will cause disk_stat to increase without
8026 * increasing sync_io so curr_events will (eventually)
8027 * be larger than it was before. Once it becomes
8028 * substantially larger, the test below will cause
8029 * the array to appear non-idle, and resync will slow
8030 * down.
8031 * If there is a lot of outstanding resync activity when
8032 * we set last_event to curr_events, then all that activity
8033 * completing might cause the array to appear non-idle
8034 * and resync will be slowed down even though there might
8035 * not have been non-resync activity. This will only
8036 * happen once though. 'last_events' will soon reflect
8037 * the state where there is little or no outstanding
8038 * resync requests, and further resync activity will
8039 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08008040 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008041 */
NeilBrowneea1bf32009-03-31 14:27:02 +11008042 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008043 rdev->last_events = curr_events;
8044 idle = 0;
8045 }
8046 }
NeilBrown4b809912008-07-21 17:05:25 +10008047 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008048 return idle;
8049}
8050
NeilBrownfd01b882011-10-11 16:47:53 +11008051void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008052{
8053 /* another "blocks" (512byte) blocks have been synced */
8054 atomic_sub(blocks, &mddev->recovery_active);
8055 wake_up(&mddev->recovery_wait);
8056 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07008057 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08008058 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059 md_wakeup_thread(mddev->thread);
8060 // stop recovery, signal do_sync ....
8061 }
8062}
NeilBrown6c144d32014-09-30 16:15:38 +10008063EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008064
NeilBrown06d91a52005-06-21 17:17:12 -07008065/* md_write_start(mddev, bi)
8066 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07008067 * in superblock) before writing, schedule a superblock update
8068 * and wait for it to complete.
NeilBrowncc27b0c2017-06-05 16:49:39 +10008069 * A return value of 'false' means that the write wasn't recorded
8070 * and cannot proceed as the array is being suspend.
NeilBrown06d91a52005-06-21 17:17:12 -07008071 */
NeilBrowncc27b0c2017-06-05 16:49:39 +10008072bool md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073{
Neil Brown0fd62b82008-06-28 08:31:36 +10008074 int did_change = 0;
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008075
NeilBrown06d91a52005-06-21 17:17:12 -07008076 if (bio_data_dir(bi) != WRITE)
NeilBrowncc27b0c2017-06-05 16:49:39 +10008077 return true;
NeilBrown06d91a52005-06-21 17:17:12 -07008078
NeilBrownf91de922005-11-08 21:39:36 -08008079 BUG_ON(mddev->ro == 1);
8080 if (mddev->ro == 2) {
8081 /* need to switch to read/write */
8082 mddev->ro = 0;
8083 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8084 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08008085 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008086 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08008087 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008088 rcu_read_lock();
8089 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11008090 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07008091 if (mddev->safemode == 1)
8092 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11008093 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
NeilBrown81fe48e2017-08-08 16:56:36 +10008094 if (mddev->in_sync || mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11008095 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07008096 if (mddev->in_sync) {
8097 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008098 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8099 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07008100 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008101 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07008102 }
NeilBrown85572d72014-12-15 12:56:56 +11008103 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07008104 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008105 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10008106 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008107 sysfs_notify_dirent_safe(mddev->sysfs_state);
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008108 if (!mddev->has_superblocks)
8109 return true;
NeilBrown09a44cc2008-05-23 13:04:36 -07008110 wait_event(mddev->sb_wait,
NeilBrownd47c8ad2017-10-05 16:23:16 +11008111 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8112 mddev->suspended);
NeilBrowncc27b0c2017-06-05 16:49:39 +10008113 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8114 percpu_ref_put(&mddev->writes_pending);
8115 return false;
8116 }
8117 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118}
NeilBrown6c144d32014-09-30 16:15:38 +10008119EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008120
NeilBrown49728052017-03-15 14:05:12 +11008121/* md_write_inc can only be called when md_write_start() has
8122 * already been called at least once of the current request.
8123 * It increments the counter and is useful when a single request
8124 * is split into several parts. Each part causes an increment and
8125 * so needs a matching md_write_end().
8126 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8127 * a spinlocked region.
8128 */
8129void md_write_inc(struct mddev *mddev, struct bio *bi)
8130{
8131 if (bio_data_dir(bi) != WRITE)
8132 return;
8133 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008134 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008135}
8136EXPORT_SYMBOL(md_write_inc);
8137
NeilBrownfd01b882011-10-11 16:47:53 +11008138void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008139{
NeilBrown4ad23a972017-03-15 14:05:14 +11008140 percpu_ref_put(&mddev->writes_pending);
8141
8142 if (mddev->safemode == 2)
8143 md_wakeup_thread(mddev->thread);
8144 else if (mddev->safemode_delay)
8145 /* The roundup() ensures this only performs locking once
8146 * every ->safemode_delay jiffies
8147 */
8148 mod_timer(&mddev->safemode_timer,
8149 roundup(jiffies, mddev->safemode_delay) +
8150 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151}
NeilBrown4ad23a972017-03-15 14:05:14 +11008152
NeilBrown6c144d32014-09-30 16:15:38 +10008153EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008154
NeilBrown2a2275d2007-01-26 00:57:11 -08008155/* md_allow_write(mddev)
8156 * Calling this ensures that the array is marked 'active' so that writes
8157 * may proceed without blocking. It is important to call this before
8158 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8159 * Must be called with mddev_lock held.
8160 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008161void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008162{
8163 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008164 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008165 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008166 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008167 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008168 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008169
NeilBrown85572d72014-12-15 12:56:56 +11008170 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008171 if (mddev->in_sync) {
8172 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008173 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8174 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008175 if (mddev->safemode_delay &&
8176 mddev->safemode == 0)
8177 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008178 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008179 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008180 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008181 /* wait for the dirty state to be recorded in the metadata */
8182 wait_event(mddev->sb_wait,
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008183 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008184 } else
NeilBrown85572d72014-12-15 12:56:56 +11008185 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008186}
8187EXPORT_SYMBOL_GPL(md_allow_write);
8188
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189#define SYNC_MARKS 10
8190#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008191#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008192void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193{
Shaohua Li4ed87312012-10-11 13:34:00 +11008194 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008195 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196 unsigned int currspeed = 0,
8197 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008198 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008199 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008200 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201 sector_t mark_cnt[SYNC_MARKS];
8202 int last_mark,m;
8203 struct list_head *tmp;
8204 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008205 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008206 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008207 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008208 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008209 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008210
8211 /* just incase thread restarts... */
Song Liud5d885f2017-11-19 22:17:01 -08008212 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8213 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214 return;
NeilBrown3991b312014-05-28 13:39:23 +10008215 if (mddev->ro) {/* never try to sync a read-only array */
8216 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008217 return;
NeilBrown3991b312014-05-28 13:39:23 +10008218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008219
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008220 if (mddev_is_clustered(mddev)) {
8221 ret = md_cluster_ops->resync_start(mddev);
8222 if (ret)
8223 goto skip;
8224
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008225 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008226 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8227 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8228 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8229 && ((unsigned long long)mddev->curr_resync_completed
8230 < (unsigned long long)mddev->resync_max_sectors))
8231 goto skip;
8232 }
8233
NeilBrown61df9d92006-10-03 01:15:57 -07008234 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008235 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008236 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008237 action = "check";
8238 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008239 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008240 action = "repair";
8241 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008242 desc = "resync";
8243 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8244 desc = "reshape";
8245 else
8246 desc = "recovery";
8247
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008248 mddev->last_sync_action = action ?: desc;
8249
Linus Torvalds1da177e2005-04-16 15:20:36 -07008250 /* we overload curr_resync somewhat here.
8251 * 0 == not engaged in resync at all
8252 * 2 == checking that there is no conflict with another sync
8253 * 1 == like 2, but have yielded to allow conflicting resync to
8254 * commense
8255 * other == active in resync - this many blocks
8256 *
8257 * Before starting a resync we must have set curr_resync to
8258 * 2, and then checked that every "conflicting" array has curr_resync
8259 * less than ours. When we find one that is the same or higher
8260 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8261 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8262 * This will mean we have to start checking from the beginning again.
8263 *
8264 */
8265
8266 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008267 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008268 mddev->curr_resync = 2;
8269
8270 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008271 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008272 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008273 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008274 if (mddev2 == mddev)
8275 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008276 if (!mddev->parallel_resync
8277 && mddev2->curr_resync
8278 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008279 DEFINE_WAIT(wq);
8280 if (mddev < mddev2 && mddev->curr_resync == 2) {
8281 /* arbitrarily yield */
8282 mddev->curr_resync = 1;
8283 wake_up(&resync_wait);
8284 }
8285 if (mddev > mddev2 && mddev->curr_resync == 1)
8286 /* no need to wait here, we can wait the next
8287 * time 'round when curr_resync == 2
8288 */
8289 continue;
NeilBrown97441972008-09-19 11:49:54 +10008290 /* We need to wait 'interruptible' so as not to
8291 * contribute to the load average, and not to
8292 * be caught by 'softlockup'
8293 */
8294 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008295 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008296 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008297 if (mddev2_minor != mddev2->md_minor) {
8298 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008299 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8300 desc, mdname(mddev),
8301 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008303 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008304 if (signal_pending(current))
8305 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008306 schedule();
8307 finish_wait(&resync_wait, &wq);
8308 goto try_again;
8309 }
8310 finish_wait(&resync_wait, &wq);
8311 }
8312 }
8313 } while (mddev->curr_resync < 2);
8314
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008315 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008316 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008318 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008319 */
8320 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008321 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008322 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008323 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8324 j = mddev->resync_min;
8325 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008326 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008327
NeilBrownccfcc3c2006-03-27 01:18:09 -08008328 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10008329 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008330 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008331 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008332 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008333 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008334 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008335 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008336 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008337 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008338 !test_bit(Faulty, &rdev->flags) &&
8339 !test_bit(In_sync, &rdev->flags) &&
8340 rdev->recovery_offset < j)
8341 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008342 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008343
8344 /* If there is a bitmap, we need to make sure all
8345 * writes that started before we added a spare
8346 * complete before we start doing a recovery.
8347 * Otherwise the write might complete and (via
8348 * bitmap_endwrite) set a bit in the bitmap after the
8349 * recovery has checked that bit and skipped that
8350 * region.
8351 */
8352 if (mddev->bitmap) {
8353 mddev->pers->quiesce(mddev, 1);
8354 mddev->pers->quiesce(mddev, 0);
8355 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008357
NeilBrown9d487392016-11-02 14:16:49 +11008358 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8359 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8360 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8361 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362
NeilBrowneea1bf32009-03-31 14:27:02 +11008363 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008364
NeilBrown57afd892005-06-21 17:17:13 -07008365 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008366 for (m = 0; m < SYNC_MARKS; m++) {
8367 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008368 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008369 }
8370 last_mark = 0;
8371 mddev->resync_mark = mark[last_mark];
8372 mddev->resync_mark_cnt = mark_cnt[last_mark];
8373
8374 /*
8375 * Tune reconstruction:
8376 */
8377 window = 32*(PAGE_SIZE/512);
NeilBrown9d487392016-11-02 14:16:49 +11008378 pr_debug("md: using %dk window, over a total of %lluk.\n",
8379 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380
8381 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008382 last_check = 0;
8383
8384 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008385 pr_debug("md: resuming %s of %s from checkpoint.\n",
8386 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008387 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008388 } else
8389 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008390 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008391 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8392 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008393 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394
majianpeng7c2c57c2012-07-03 12:12:26 +10008395 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008396 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008397 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008398
NeilBrown57afd892005-06-21 17:17:13 -07008399 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008400
NeilBrown7a91ee12009-05-26 12:57:21 +10008401 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8402 ((mddev->curr_resync > mddev->curr_resync_completed &&
8403 (mddev->curr_resync - mddev->curr_resync_completed)
8404 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008405 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008406 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008407 >= mddev->resync_max - mddev->curr_resync_completed ||
8408 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008409 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008410 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008411 wait_event(mddev->recovery_wait,
8412 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008413 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008414 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8415 j > mddev->recovery_cp)
8416 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008417 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008418 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008419 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008420 }
NeilBrownacb180b2009-04-14 16:28:34 +10008421
NeilBrownc91abf52013-11-19 12:02:01 +11008422 while (j >= mddev->resync_max &&
8423 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008424 /* As this condition is controlled by user-space,
8425 * we can block indefinitely, so use '_interruptible'
8426 * to avoid triggering warnings.
8427 */
8428 flush_signals(current); /* just in case */
8429 wait_event_interruptible(mddev->recovery_wait,
8430 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008431 || test_bit(MD_RECOVERY_INTR,
8432 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008433 }
NeilBrownacb180b2009-04-14 16:28:34 +10008434
NeilBrownc91abf52013-11-19 12:02:01 +11008435 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8436 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008437
NeilBrown09314792015-02-19 16:04:40 +11008438 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008439 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008440 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442 }
NeilBrown57afd892005-06-21 17:17:13 -07008443
8444 if (!skipped) { /* actual IO requested */
8445 io_sectors += sectors;
8446 atomic_add(sectors, &mddev->recovery_active);
8447 }
8448
NeilBrowne875ece2011-07-28 11:39:24 +10008449 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8450 break;
8451
Linus Torvalds1da177e2005-04-16 15:20:36 -07008452 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008453 if (j > max_sectors)
8454 /* when skipping, extra large numbers can be returned. */
8455 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008456 if (j > 2)
8457 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008458 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008459 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008460 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008461 * visible in /proc/mdstat
8462 */
8463 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008464
8465 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008466 continue;
8467
NeilBrown57afd892005-06-21 17:17:13 -07008468 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008469 repeat:
8470 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8471 /* step marks */
8472 int next = (last_mark+1) % SYNC_MARKS;
8473
8474 mddev->resync_mark = mark[next];
8475 mddev->resync_mark_cnt = mark_cnt[next];
8476 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008477 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008478 last_mark = next;
8479 }
8480
NeilBrownc91abf52013-11-19 12:02:01 +11008481 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8482 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008483
8484 /*
8485 * this loop exits only if either when we are slower than
8486 * the 'hard' speed limit, or the system was IO-idle for
8487 * a jiffy.
8488 * the system might be non-idle CPU-wise, but we only care
8489 * about not overloading the IO subsystem. (things like an
8490 * e2fsck being done on the RAID array should execute fast)
8491 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008492 cond_resched();
8493
Xiao Niac7e50a2014-08-07 09:37:41 -04008494 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8495 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008496 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008497
NeilBrown88202a02006-01-06 00:21:36 -08008498 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008499 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008500 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 goto repeat;
8502 }
NeilBrownac8fa412015-02-19 16:55:00 +11008503 if (!is_mddev_idle(mddev, 0)) {
8504 /*
8505 * Give other IO more of a chance.
8506 * The faster the devices, the less we wait.
8507 */
8508 wait_event(mddev->recovery_wait,
8509 !atomic_read(&mddev->recovery_active));
8510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008511 }
8512 }
NeilBrown9d487392016-11-02 14:16:49 +11008513 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8514 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8515 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516 /*
8517 * this also signals 'finished resyncing' to md_stop
8518 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008519 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8521
NeilBrown5ed1df22015-07-24 13:27:08 +10008522 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8523 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008524 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008525 mddev->curr_resync_completed = mddev->curr_resync;
8526 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8527 }
NeilBrown09314792015-02-19 16:04:40 +11008528 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008529
NeilBrowndfc70642008-05-23 13:04:39 -07008530 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008531 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008532 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8533 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8534 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008535 pr_debug("md: checkpointing %s of %s.\n",
8536 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008537 if (test_bit(MD_RECOVERY_ERROR,
8538 &mddev->recovery))
8539 mddev->recovery_cp =
8540 mddev->curr_resync_completed;
8541 else
8542 mddev->recovery_cp =
8543 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008544 }
8545 } else
8546 mddev->recovery_cp = MaxSector;
8547 } else {
8548 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8549 mddev->curr_resync = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11008550 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8551 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
8552 rcu_read_lock();
8553 rdev_for_each_rcu(rdev, mddev)
8554 if (rdev->raid_disk >= 0 &&
8555 mddev->delta_disks >= 0 &&
8556 !test_bit(Journal, &rdev->flags) &&
8557 !test_bit(Faulty, &rdev->flags) &&
8558 !test_bit(In_sync, &rdev->flags) &&
8559 rdev->recovery_offset < mddev->curr_resync)
8560 rdev->recovery_offset = mddev->curr_resync;
8561 rcu_read_unlock();
8562 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008564 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008565 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008566 /* set CHANGE_PENDING here since maybe another update is needed,
8567 * so other nodes are informed. It should be harmless for normal
8568 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008569 set_mask_bits(&mddev->sb_flags, 0,
8570 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008571
BingJing Chang88763912018-02-22 13:34:46 +08008572 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8573 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8574 mddev->delta_disks > 0 &&
8575 mddev->pers->finish_reshape &&
8576 mddev->pers->size &&
8577 mddev->queue) {
8578 mddev_lock_nointr(mddev);
8579 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
8580 mddev_unlock(mddev);
8581 set_capacity(mddev->gendisk, mddev->array_sectors);
8582 revalidate_disk(mddev->gendisk);
8583 }
8584
NeilBrown23da4222014-12-15 12:57:01 +11008585 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008586 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8587 /* We completed so min/max setting can be forgotten if used. */
8588 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8589 mddev->resync_min = 0;
8590 mddev->resync_max = MaxSector;
8591 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8592 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008593 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008594 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008595 spin_unlock(&mddev->lock);
8596
Linus Torvalds1da177e2005-04-16 15:20:36 -07008597 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008598 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008599 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008600}
NeilBrown29269552006-03-27 01:18:10 -08008601EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008602
NeilBrown746d3202013-04-24 11:42:41 +10008603static int remove_and_add_spares(struct mddev *mddev,
8604 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008605{
NeilBrown3cb03002011-10-11 16:45:26 +11008606 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008607 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008608 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008609 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008610
NeilBrown39772f02018-02-03 09:19:30 +11008611 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
8612 /* Mustn't remove devices when resync thread is running */
8613 return 0;
8614
NeilBrownd787be42016-06-02 16:19:53 +10008615 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008616 if ((this == NULL || rdev == this) &&
8617 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008618 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008619 test_bit(Faulty, &rdev->flags) &&
8620 atomic_read(&rdev->nr_pending)==0) {
8621 /* Faulty non-Blocked devices with nr_pending == 0
8622 * never get nr_pending incremented,
8623 * never get Faulty cleared, and never get Blocked set.
8624 * So we can synchronize_rcu now rather than once per device
8625 */
8626 remove_some = true;
8627 set_bit(RemoveSynchronized, &rdev->flags);
8628 }
8629 }
8630
8631 if (remove_some)
8632 synchronize_rcu();
8633 rdev_for_each(rdev, mddev) {
8634 if ((this == NULL || rdev == this) &&
8635 rdev->raid_disk >= 0 &&
8636 !test_bit(Blocked, &rdev->flags) &&
8637 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008638 (!test_bit(In_sync, &rdev->flags) &&
8639 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008640 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008641 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008642 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008643 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008644 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008645 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008646 }
8647 }
NeilBrownd787be42016-06-02 16:19:53 +10008648 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8649 clear_bit(RemoveSynchronized, &rdev->flags);
8650 }
8651
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008652 if (removed && mddev->kobj.sd)
8653 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008654
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008655 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008656 goto no_add;
8657
NeilBrowndafb20f2012-03-19 12:46:39 +11008658 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008659 if (this && this != rdev)
8660 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008661 if (test_bit(Candidate, &rdev->flags))
8662 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008663 if (rdev->raid_disk >= 0 &&
8664 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008665 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008666 !test_bit(Faulty, &rdev->flags))
8667 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008668 if (rdev->raid_disk >= 0)
8669 continue;
8670 if (test_bit(Faulty, &rdev->flags))
8671 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008672 if (!test_bit(Journal, &rdev->flags)) {
8673 if (mddev->ro &&
8674 ! (rdev->saved_raid_disk >= 0 &&
8675 !test_bit(Bitmap_sync, &rdev->flags)))
8676 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008677
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008678 rdev->recovery_offset = 0;
8679 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008680 if (mddev->pers->
8681 hot_add_disk(mddev, rdev) == 0) {
8682 if (sysfs_link_rdev(mddev, rdev))
8683 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008684 if (!test_bit(Journal, &rdev->flags))
8685 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008686 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08008687 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008688 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008689 }
NeilBrown746d3202013-04-24 11:42:41 +10008690no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008691 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08008692 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008693 return spares;
8694}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008695
NeilBrownac05f252014-09-30 08:10:42 +10008696static void md_start_sync(struct work_struct *ws)
8697{
8698 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008699
NeilBrownac05f252014-09-30 08:10:42 +10008700 mddev->sync_thread = md_register_thread(md_do_sync,
8701 mddev,
8702 "resync");
8703 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11008704 pr_warn("%s: could not start resync thread...\n",
8705 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008706 /* leave the spares where they are, it shouldn't hurt */
8707 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8708 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8709 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8710 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8711 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008712 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008713 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8714 &mddev->recovery))
8715 if (mddev->sysfs_action)
8716 sysfs_notify_dirent_safe(mddev->sysfs_action);
8717 } else
8718 md_wakeup_thread(mddev->sync_thread);
8719 sysfs_notify_dirent_safe(mddev->sysfs_action);
8720 md_new_event(mddev);
8721}
8722
Linus Torvalds1da177e2005-04-16 15:20:36 -07008723/*
8724 * This routine is regularly called by all per-raid-array threads to
8725 * deal with generic issues like resync and super-block update.
8726 * Raid personalities that don't have a thread (linear/raid0) do not
8727 * need this as they never do any recovery or update the superblock.
8728 *
8729 * It does not do any resync itself, but rather "forks" off other threads
8730 * to do that as needed.
8731 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8732 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008733 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008734 * and wakeups up this thread which will reap the thread and finish up.
8735 * This thread also removes any faulty devices (with nr_pending == 0).
8736 *
8737 * The overall approach is:
8738 * 1/ if the superblock needs updating, update it.
8739 * 2/ If a recovery thread is running, don't do anything else.
8740 * 3/ If recovery has finished, clean up, possibly marking spares active.
8741 * 4/ If there are any faulty devices, remove them.
8742 * 5/ If array is degraded, try to add spares devices
8743 * 6/ If array has spares or is not in-sync, start a resync thread.
8744 */
NeilBrownfd01b882011-10-11 16:47:53 +11008745void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008746{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008747 if (mddev->suspended)
8748 return;
8749
NeilBrown5f404022005-06-21 17:17:16 -07008750 if (mddev->bitmap)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008751 md_bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008752
NeilBrownfca4d842005-06-21 17:17:11 -07008753 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008754 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11008755 pr_debug("md: %s in immediate safe mode\n",
8756 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07008757 mddev->safemode = 2;
8758 }
8759 flush_signals(current);
8760 }
8761
NeilBrownc89a8ee2008-08-05 15:54:13 +10008762 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8763 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008764 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08008765 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008766 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008767 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008768 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11008769 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07008770 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008771 ))
8772 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008773
NeilBrowndf5b89b2006-03-27 01:18:20 -08008774 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008775 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008776
Shaohua Liafc1f552017-08-11 20:34:45 -07008777 if (!mddev->external && mddev->safemode == 1)
NeilBrown33182d12017-08-08 16:56:36 +10008778 mddev->safemode = 0;
8779
NeilBrownc89a8ee2008-08-05 15:54:13 +10008780 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008781 struct md_rdev *rdev;
8782 if (!mddev->external && mddev->in_sync)
8783 /* 'Blocked' flag not needed as failed devices
8784 * will be recorded if array switched to read/write.
8785 * Leaving it set will prevent the device
8786 * from being removed.
8787 */
8788 rdev_for_each(rdev, mddev)
8789 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008790 /* On a read-only array we can:
8791 * - remove failed devices
8792 * - add already-in_sync devices if the array itself
8793 * is in-sync.
8794 * As we only add devices that are already in-sync,
8795 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008796 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008797 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008798 /* There is no thread, but we need to call
8799 * ->spare_active and clear saved_raid_disk
8800 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008801 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008802 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008803 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008804 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08008805 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008806 goto unlock;
8807 }
8808
Guoqing Jiang659b2542015-12-21 10:50:59 +11008809 if (mddev_is_clustered(mddev)) {
8810 struct md_rdev *rdev;
8811 /* kick the device if another node issued a
8812 * remove disk.
8813 */
8814 rdev_for_each(rdev, mddev) {
8815 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8816 rdev->raid_disk < 0)
8817 md_kick_rdev_from_array(rdev);
8818 }
8819 }
8820
NeilBrown6497709b2017-03-15 14:05:14 +11008821 if (!mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11008822 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11008823 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11008824 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07008825 }
NeilBrownfca4d842005-06-21 17:17:11 -07008826
Shaohua Li29530792016-12-08 15:48:19 -08008827 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07008828 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008829
Linus Torvalds1da177e2005-04-16 15:20:36 -07008830 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8831 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8832 /* resync/recovery still happening */
8833 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8834 goto unlock;
8835 }
8836 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008837 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008838 goto unlock;
8839 }
Neil Brown72a23c22008-06-28 08:31:41 +10008840 /* Set RUNNING before clearing NEEDED to avoid
8841 * any transients in the value of "sync_action".
8842 */
NeilBrown72f36d52012-10-11 14:25:57 +11008843 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008844 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008845 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008846 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008847 /* Clear some bits that don't mean anything, but
8848 * might be left set
8849 */
NeilBrown24dd4692005-11-08 21:39:26 -08008850 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8851 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008852
NeilBrowned209582012-04-24 10:23:14 +10008853 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8854 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008855 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008856 /* no recovery is running.
8857 * remove any failed drives, then
8858 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008859 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860 * the personality to fail the re-add.
8861 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008862
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008863 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008864 if (mddev->pers->check_reshape == NULL ||
8865 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008866 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008867 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008868 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008869 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008870 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008871 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8872 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008873 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008874 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008875 } else if (mddev->recovery_cp < MaxSector) {
8876 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008877 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008878 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8879 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008880 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008881
Linus Torvalds1da177e2005-04-16 15:20:36 -07008882 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008883 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008884 /* We are adding a device or devices to an array
8885 * which has the bitmap stored on all devices.
8886 * So make sure all bitmap pages get written
8887 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008888 md_bitmap_write_all(mddev->bitmap);
NeilBrowna654b9d82005-06-21 17:17:27 -07008889 }
NeilBrownac05f252014-09-30 08:10:42 +10008890 INIT_WORK(&mddev->del_work, md_start_sync);
8891 queue_work(md_misc_wq, &mddev->del_work);
8892 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008893 }
NeilBrownac05f252014-09-30 08:10:42 +10008894 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008895 if (!mddev->sync_thread) {
8896 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008897 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008898 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8899 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008900 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008901 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008902 }
NeilBrownac05f252014-09-30 08:10:42 +10008903 unlock:
8904 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008905 mddev_unlock(mddev);
NeilBrown35bfc522017-10-17 13:46:43 +11008906 } else if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
8907 /* Write superblock - thread that called mddev_suspend()
8908 * holds reconfig_mutex for us.
8909 */
8910 set_bit(MD_UPDATING_SB, &mddev->flags);
8911 smp_mb__after_atomic();
8912 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
8913 md_update_sb(mddev, 0);
8914 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
8915 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008916 }
8917}
NeilBrown6c144d32014-09-30 16:15:38 +10008918EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008919
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008920void md_reap_sync_thread(struct mddev *mddev)
8921{
8922 struct md_rdev *rdev;
8923
8924 /* resync has finished, collect result */
8925 md_unregister_thread(&mddev->sync_thread);
8926 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8927 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8928 /* success...*/
8929 /* activate any spares */
8930 if (mddev->pers->spare_active(mddev)) {
8931 sysfs_notify(&mddev->kobj, NULL,
8932 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08008933 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008934 }
8935 }
8936 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8937 mddev->pers->finish_reshape)
8938 mddev->pers->finish_reshape(mddev);
8939
8940 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008941 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008942 */
NeilBrownf4667222013-12-09 12:04:56 +11008943 if (!mddev->degraded)
8944 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008945 rdev->saved_raid_disk = -1;
8946
8947 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08008948 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008949 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8950 * clustered raid */
8951 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8952 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008953 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008954 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008955 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8956 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8957 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8958 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008959 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008960 /* flag recovery needed just to double check */
8961 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8962 sysfs_notify_dirent_safe(mddev->sysfs_action);
8963 md_new_event(mddev);
8964 if (mddev->event_work.func)
8965 queue_work(md_misc_wq, &mddev->event_work);
8966}
NeilBrown6c144d32014-09-30 16:15:38 +10008967EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008968
NeilBrownfd01b882011-10-11 16:47:53 +11008969void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008970{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008971 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008972 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008973 !test_bit(Blocked, &rdev->flags) &&
8974 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008975 msecs_to_jiffies(5000));
8976 rdev_dec_pending(rdev, mddev);
8977}
8978EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8979
NeilBrownc6563a82012-05-21 09:27:00 +10008980void md_finish_reshape(struct mddev *mddev)
8981{
8982 /* called be personality module when reshape completes. */
8983 struct md_rdev *rdev;
8984
8985 rdev_for_each(rdev, mddev) {
8986 if (rdev->data_offset > rdev->new_data_offset)
8987 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8988 else
8989 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8990 rdev->data_offset = rdev->new_data_offset;
8991 }
8992}
8993EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008994
Vishal Vermafc974ee2015-12-24 19:20:34 -07008995/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10008996
Vishal Vermafc974ee2015-12-24 19:20:34 -07008997/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11008998int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008999 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009000{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04009001 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10009002 int rv;
9003 if (is_new)
9004 s += rdev->new_data_offset;
9005 else
9006 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07009007 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9008 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10009009 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009010 if (test_bit(ExternalBbl, &rdev->flags))
9011 sysfs_notify(&rdev->kobj, NULL,
9012 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11009013 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08009014 set_mask_bits(&mddev->sb_flags, 0,
9015 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10009016 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07009017 return 1;
9018 } else
9019 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10009020}
9021EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9022
NeilBrownc6563a82012-05-21 09:27:00 +10009023int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9024 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009025{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009026 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10009027 if (is_new)
9028 s += rdev->new_data_offset;
9029 else
9030 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009031 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9032 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
9033 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
9034 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10009035}
9036EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9037
Adrian Bunk75c96f82005-05-05 16:16:09 -07009038static int md_notify_reboot(struct notifier_block *this,
9039 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009040{
9041 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009042 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009043 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009044
NeilBrownc744a652012-03-19 12:46:37 +11009045 for_each_mddev(mddev, tmp) {
9046 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009047 if (mddev->pers)
9048 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009049 if (mddev->persistent)
9050 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009051 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009052 }
NeilBrownc744a652012-03-19 12:46:37 +11009053 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009054 }
NeilBrownc744a652012-03-19 12:46:37 +11009055 /*
9056 * certain more exotic SCSI devices are known to be
9057 * volatile wrt too early system reboots. While the
9058 * right place to handle this issue is the given
9059 * driver, we do want to have a safe RAID driver ...
9060 */
9061 if (need_delay)
9062 mdelay(1000*1);
9063
Linus Torvalds1da177e2005-04-16 15:20:36 -07009064 return NOTIFY_DONE;
9065}
9066
Adrian Bunk75c96f82005-05-05 16:16:09 -07009067static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009068 .notifier_call = md_notify_reboot,
9069 .next = NULL,
9070 .priority = INT_MAX, /* before any real devices */
9071};
9072
9073static void md_geninit(void)
9074{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009075 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009076
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07009077 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009078}
9079
Adrian Bunk75c96f82005-05-05 16:16:09 -07009080static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009081{
Tejun Heoe804ac72010-10-15 15:36:08 +02009082 int ret = -ENOMEM;
9083
Tejun Heoada609e2011-01-25 14:35:54 +01009084 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009085 if (!md_wq)
9086 goto err_wq;
9087
9088 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9089 if (!md_misc_wq)
9090 goto err_misc_wq;
9091
9092 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9093 goto err_md;
9094
9095 if ((ret = register_blkdev(0, "mdp")) < 0)
9096 goto err_mdp;
9097 mdp_major = ret;
9098
NeilBrownaf5628f2014-07-31 13:54:54 +10009099 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009100 md_probe, NULL, NULL);
9101 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009102 md_probe, NULL, NULL);
9103
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009105 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009106
9107 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009109
Tejun Heoe804ac72010-10-15 15:36:08 +02009110err_mdp:
9111 unregister_blkdev(MD_MAJOR, "md");
9112err_md:
9113 destroy_workqueue(md_misc_wq);
9114err_misc_wq:
9115 destroy_workqueue(md_wq);
9116err_wq:
9117 return ret;
9118}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009119
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009120static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009121{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009122 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9123 struct md_rdev *rdev2;
9124 int role, ret;
9125 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009126
Guoqing Jiang818da592017-03-01 16:42:40 +08009127 /*
9128 * If size is changed in another node then we need to
9129 * do resize as well.
9130 */
9131 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9132 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9133 if (ret)
9134 pr_info("md-cluster: resize failed\n");
9135 else
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009136 md_bitmap_update_sb(mddev->bitmap);
Guoqing Jiang818da592017-03-01 16:42:40 +08009137 }
9138
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009139 /* Check for change of roles in the active devices */
9140 rdev_for_each(rdev2, mddev) {
9141 if (test_bit(Faulty, &rdev2->flags))
9142 continue;
9143
9144 /* Check if the roles changed */
9145 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009146
9147 if (test_bit(Candidate, &rdev2->flags)) {
9148 if (role == 0xfffe) {
9149 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9150 md_kick_rdev_from_array(rdev2);
9151 continue;
9152 }
9153 else
9154 clear_bit(Candidate, &rdev2->flags);
9155 }
9156
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009157 if (role != rdev2->raid_disk) {
9158 /* got activated */
9159 if (rdev2->raid_disk == -1 && role != 0xffff) {
9160 rdev2->saved_raid_disk = role;
9161 ret = remove_and_add_spares(mddev, rdev2);
9162 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009163 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009164 /* wakeup mddev->thread here, so array could
9165 * perform resync with the new activated disk */
9166 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9167 md_wakeup_thread(mddev->thread);
9168
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009169 }
9170 /* device faulty
9171 * We just want to do the minimum to mark the disk
9172 * as faulty. The recovery is performed by the
9173 * one who initiated the error.
9174 */
9175 if ((role == 0xfffe) || (role == 0xfffd)) {
9176 md_error(mddev, rdev2);
9177 clear_bit(Blocked, &rdev2->flags);
9178 }
9179 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009180 }
9181
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009182 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9183 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009184
9185 /* Finally set the event to be up to date */
9186 mddev->events = le64_to_cpu(sb->events);
9187}
9188
9189static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9190{
9191 int err;
9192 struct page *swapout = rdev->sb_page;
9193 struct mdp_superblock_1 *sb;
9194
9195 /* Store the sb page of the rdev in the swapout temporary
9196 * variable in case we err in the future
9197 */
9198 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009199 err = alloc_disk_sb(rdev);
9200 if (err == 0) {
9201 ClearPageUptodate(rdev->sb_page);
9202 rdev->sb_loaded = 0;
9203 err = super_types[mddev->major_version].
9204 load_super(rdev, NULL, mddev->minor_version);
9205 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009206 if (err < 0) {
9207 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9208 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009209 if (rdev->sb_page)
9210 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009211 rdev->sb_page = swapout;
9212 rdev->sb_loaded = 1;
9213 return err;
9214 }
9215
9216 sb = page_address(rdev->sb_page);
9217 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9218 * is not set
9219 */
9220
9221 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9222 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9223
9224 /* The other node finished recovery, call spare_active to set
9225 * device In_sync and mddev->degraded
9226 */
9227 if (rdev->recovery_offset == MaxSector &&
9228 !test_bit(In_sync, &rdev->flags) &&
9229 mddev->pers->spare_active(mddev))
9230 sysfs_notify(&mddev->kobj, NULL, "degraded");
9231
9232 put_page(swapout);
9233 return 0;
9234}
9235
9236void md_reload_sb(struct mddev *mddev, int nr)
9237{
9238 struct md_rdev *rdev;
9239 int err;
9240
9241 /* Find the rdev */
9242 rdev_for_each_rcu(rdev, mddev) {
9243 if (rdev->desc_nr == nr)
9244 break;
9245 }
9246
9247 if (!rdev || rdev->desc_nr != nr) {
9248 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9249 return;
9250 }
9251
9252 err = read_rdev(mddev, rdev);
9253 if (err < 0)
9254 return;
9255
9256 check_sb_changes(mddev, rdev);
9257
9258 /* Read all rdev's to update recovery_offset */
Guoqing Jiang0ea99242018-04-09 17:01:21 +08009259 rdev_for_each_rcu(rdev, mddev) {
9260 if (!test_bit(Faulty, &rdev->flags))
9261 read_rdev(mddev, rdev);
9262 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009263}
9264EXPORT_SYMBOL(md_reload_sb);
9265
Linus Torvalds1da177e2005-04-16 15:20:36 -07009266#ifndef MODULE
9267
9268/*
9269 * Searches all registered partitions for autorun RAID arrays
9270 * at boot time.
9271 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009272
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009273static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009274static LIST_HEAD(all_detected_devices);
9275struct detected_devices_node {
9276 struct list_head list;
9277 dev_t dev;
9278};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009279
9280void md_autodetect_dev(dev_t dev)
9281{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009282 struct detected_devices_node *node_detected_dev;
9283
9284 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9285 if (node_detected_dev) {
9286 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009287 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009288 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009289 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009291}
9292
Linus Torvalds1da177e2005-04-16 15:20:36 -07009293static void autostart_arrays(int part)
9294{
NeilBrown3cb03002011-10-11 16:45:26 +11009295 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009296 struct detected_devices_node *node_detected_dev;
9297 dev_t dev;
9298 int i_scanned, i_passed;
9299
9300 i_scanned = 0;
9301 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009302
NeilBrown9d487392016-11-02 14:16:49 +11009303 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009304
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009305 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009306 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9307 i_scanned++;
9308 node_detected_dev = list_entry(all_detected_devices.next,
9309 struct detected_devices_node, list);
9310 list_del(&node_detected_dev->list);
9311 dev = node_detected_dev->dev;
9312 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009313 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009314 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009315 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009316 if (IS_ERR(rdev))
9317 continue;
9318
NeilBrown403df472014-09-30 15:52:29 +10009319 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009320 continue;
NeilBrown403df472014-09-30 15:52:29 +10009321
NeilBrownd0fae182008-03-04 14:29:31 -08009322 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009324 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009325 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009326 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009327
NeilBrown9d487392016-11-02 14:16:49 +11009328 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009329
9330 autorun_devices(part);
9331}
9332
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009333#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009334
9335static __exit void md_exit(void)
9336{
NeilBrownfd01b882011-10-11 16:47:53 +11009337 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009338 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009339 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009340
NeilBrownaf5628f2014-07-31 13:54:54 +10009341 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009342 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009343
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009344 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009345 unregister_blkdev(mdp_major, "mdp");
9346 unregister_reboot_notifier(&md_notifier);
9347 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009348
9349 /* We cannot unload the modules while some process is
9350 * waiting for us in select() or poll() - wake them up
9351 */
9352 md_unloading = 1;
9353 while (waitqueue_active(&md_event_waiters)) {
9354 /* not safe to leave yet */
9355 wake_up(&md_event_waiters);
9356 msleep(delay);
9357 delay += delay;
9358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009359 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009360
NeilBrown29ac4aa2008-02-06 01:39:58 -08009361 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009362 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009363 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009364 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009365 /*
9366 * for_each_mddev() will call mddev_put() at the end of each
9367 * iteration. As the mddev is now fully clear, this will
9368 * schedule the mddev for destruction by a workqueue, and the
9369 * destroy_workqueue() below will wait for that to complete.
9370 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009371 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009372 destroy_workqueue(md_misc_wq);
9373 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009374}
9375
Dan Williams685784a2007-07-09 11:56:42 -07009376subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377module_exit(md_exit)
9378
Kees Cooke4dca7b2017-10-17 19:04:42 -07009379static int get_ro(char *buffer, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009380{
9381 return sprintf(buffer, "%d", start_readonly);
9382}
Kees Cooke4dca7b2017-10-17 19:04:42 -07009383static int set_ro(const char *val, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009384{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009385 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009386}
9387
NeilBrown80ca3a42006-07-10 04:44:18 -07009388module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9389module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009390module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009391module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009392
Linus Torvalds1da177e2005-04-16 15:20:36 -07009393MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009394MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009395MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009396MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);