blob: 212a6777ff3172dd9e20401dd7bf87ad5d2f7468 [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"
Christoph Hellwigef740c32009-03-31 14:27:03 +110072#include "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
188 * like bio_clone, but with a local bio set
189 */
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/*
NeilBrownd7603b72006-01-06 00:20:30 -0800207 * We have a system wide 'event count' that is incremented
208 * on any 'interesting' event, and readers of /proc/mdstat
209 * can use 'poll' or 'select' to find out when the event
210 * count increases.
211 *
212 * Events are:
213 * start array, stop array, error, add device, remove device,
214 * start build, activate spare
215 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800216static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800217static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100218void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800219{
220 atomic_inc(&md_event_count);
221 wake_up(&md_event_waiters);
222}
NeilBrown29269552006-03-27 01:18:10 -0800223EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800224
225/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 * Enables to iterate over all existing md arrays
227 * all_mddevs_lock protects this list.
228 */
229static LIST_HEAD(all_mddevs);
230static DEFINE_SPINLOCK(all_mddevs_lock);
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/*
233 * iterates through all used mddevs in the system.
234 * We take care to grab the all_mddevs_lock whenever navigating
235 * the list, and to always hold a refcount when unlocked.
236 * Any code which breaks out of this loop while own
237 * a reference to the current mddev and must mddev_put it.
238 */
NeilBrownfd01b882011-10-11 16:47:53 +1100239#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000241 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100242 _tmp = all_mddevs.next; \
243 _mddev = NULL;}); \
244 ({ if (_tmp != &all_mddevs) \
245 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100247 if (_mddev) mddev_put(_mddev); \
248 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
249 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100251 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 )
253
NeilBrown409c57f2009-03-31 14:39:39 +1100254/* Rather than calling directly into the personality make_request function,
255 * IO requests come here first so that we can check if the device is
256 * being suspended pending a reconfiguration.
257 * We hold a refcount over the call to ->make_request. By the time that
258 * call has finished, the bio has been linked into some internal structure
259 * and so is visible to ->quiesce(), so we don't need the refcount any more.
260 */
Jens Axboedece1632015-11-05 10:41:16 -0700261static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
NeilBrown49077322010-03-25 16:20:56 +1100263 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100264 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500265 unsigned int sectors;
Gu Zheng74672d02015-04-03 08:44:47 +0800266 int cpu;
NeilBrown49077322010-03-25 16:20:56 +1100267
Kent Overstreet54efd502015-04-23 22:37:18 -0700268 blk_queue_split(q, &bio, q->bio_split);
269
NeilBrown274d8cb2016-01-04 16:16:58 +1100270 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100271 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700272 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100273 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100274 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200275 if (bio_sectors(bio) != 0)
276 bio->bi_error = -EROFS;
277 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700278 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100279 }
NeilBrown0ca69882011-01-14 09:14:33 +1100280 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100281 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200282 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100283 DEFINE_WAIT(__wait);
284 for (;;) {
285 prepare_to_wait(&mddev->sb_wait, &__wait,
286 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200287 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100288 break;
289 rcu_read_unlock();
290 schedule();
291 rcu_read_lock();
292 }
293 finish_wait(&mddev->sb_wait, &__wait);
294 }
295 atomic_inc(&mddev->active_io);
296 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100297
Chris Masone91ece52011-02-07 19:21:48 -0500298 /*
299 * save the sectors now since our bio can
300 * go away inside make_request
301 */
302 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700303 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600304 bio->bi_opf &= ~REQ_NOMERGE;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200305 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100306
Gu Zheng74672d02015-04-03 08:44:47 +0800307 cpu = part_stat_lock();
308 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
309 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
310 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100311
NeilBrown409c57f2009-03-31 14:39:39 +1100312 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
313 wake_up(&mddev->sb_wait);
Jens Axboedece1632015-11-05 10:41:16 -0700314
315 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
NeilBrown9e35b992010-04-06 14:23:02 +1000318/* mddev_suspend makes sure no new requests are submitted
319 * to the device, and that any requests that have been submitted
320 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100321 * Once mddev_detach() is called and completes, the module will be
322 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000323 */
NeilBrownfd01b882011-10-11 16:47:53 +1100324void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100325{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200326 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100327 if (mddev->suspended++)
328 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100329 synchronize_rcu();
330 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
331 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500332
333 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100334}
NeilBrown390ee602010-06-01 19:37:27 +1000335EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100336
NeilBrownfd01b882011-10-11 16:47:53 +1100337void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100338{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100339 if (--mddev->suspended)
340 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100341 wake_up(&mddev->sb_wait);
342 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500343
Jonathan Brassow47525e52012-05-22 13:55:29 +1000344 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500345 md_wakeup_thread(mddev->thread);
346 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100347}
NeilBrown390ee602010-06-01 19:37:27 +1000348EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100349
NeilBrownfd01b882011-10-11 16:47:53 +1100350int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000351{
NeilBrown5c675f82014-12-15 12:56:56 +1100352 struct md_personality *pers = mddev->pers;
353 int ret = 0;
354
355 rcu_read_lock();
356 if (mddev->suspended)
357 ret = 1;
358 else if (pers && pers->congested)
359 ret = pers->congested(mddev, bits);
360 rcu_read_unlock();
361 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000362}
NeilBrown5c675f82014-12-15 12:56:56 +1100363EXPORT_SYMBOL_GPL(mddev_congested);
364static int md_congested(void *data, int bits)
365{
366 struct mddev *mddev = data;
367 return mddev_congested(mddev, bits);
368}
NeilBrown3fa841d2009-09-23 18:10:29 +1000369
NeilBrowna2826aa2009-12-14 12:49:49 +1100370/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200371 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100372 */
373
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200374static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100375{
NeilBrown3cb03002011-10-11 16:45:26 +1100376 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100377 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100378
379 rdev_dec_pending(rdev, mddev);
380
381 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200382 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200383 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100384 }
385 bio_put(bio);
386}
387
NeilBrowna7a07e62010-12-09 16:04:25 +1100388static void md_submit_flush_data(struct work_struct *ws);
389
NeilBrowna035fc32010-12-09 16:17:51 +1100390static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100391{
NeilBrownfd01b882011-10-11 16:47:53 +1100392 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100393 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100394
NeilBrowna7a07e62010-12-09 16:04:25 +1100395 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
396 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100397 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100398 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100399 if (rdev->raid_disk >= 0 &&
400 !test_bit(Faulty, &rdev->flags)) {
401 /* Take two references, one is dropped
402 * when request finishes, one after
403 * we reclaim rcu_read_lock
404 */
405 struct bio *bi;
406 atomic_inc(&rdev->nr_pending);
407 atomic_inc(&rdev->nr_pending);
408 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000409 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200410 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100411 bi->bi_private = rdev;
412 bi->bi_bdev = rdev->bdev;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600413 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
NeilBrowna2826aa2009-12-14 12:49:49 +1100414 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500415 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100416 rcu_read_lock();
417 rdev_dec_pending(rdev, mddev);
418 }
419 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100420 if (atomic_dec_and_test(&mddev->flush_pending))
421 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100422}
423
Tejun Heoe9c74692010-09-03 11:56:18 +0200424static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100425{
NeilBrownfd01b882011-10-11 16:47:53 +1100426 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200427 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100428
Kent Overstreet4f024f32013-10-11 15:44:27 -0700429 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100430 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200431 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100432 else {
Jens Axboe1eff9d32016-08-05 15:35:16 -0600433 bio->bi_opf &= ~REQ_PREFLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200434 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100435 }
NeilBrown2b74e122010-12-09 15:59:01 +1100436
437 mddev->flush_bio = NULL;
438 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100439}
440
NeilBrownfd01b882011-10-11 16:47:53 +1100441void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100442{
NeilBrown85572d72014-12-15 12:56:56 +1100443 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100444 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200445 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100446 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200447 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100448 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100449
NeilBrowna035fc32010-12-09 16:17:51 +1100450 INIT_WORK(&mddev->flush_work, submit_flushes);
451 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100452}
Tejun Heoe9c74692010-09-03 11:56:18 +0200453EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100454
NeilBrownfd01b882011-10-11 16:47:53 +1100455static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 atomic_inc(&mddev->active);
458 return mddev;
459}
460
Dan Williams5fd3a172009-03-04 00:57:25 -0700461static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100462
NeilBrownfd01b882011-10-11 16:47:53 +1100463static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
NeilBrowna167f662010-10-26 18:31:13 +1100465 struct bio_set *bs = NULL;
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
468 return;
NeilBrownd3374822009-01-09 08:31:10 +1100469 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100470 mddev->ctime == 0 && !mddev->hold_active) {
471 /* Array is not configured at all, and not held active,
472 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100473 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100474 bs = mddev->bio_set;
475 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100476 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200477 /* We did a probe so need to clean up. Call
478 * queue_work inside the spinlock so that
479 * flush_workqueue() after mddev_find will
480 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100481 */
482 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200483 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100484 } else
485 kfree(mddev);
486 }
487 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100488 if (bs)
489 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Sasha Levin25b2edf2015-07-24 18:19:58 -0400492static void md_safemode_timeout(unsigned long data);
493
NeilBrownfd01b882011-10-11 16:47:53 +1100494void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100495{
496 mutex_init(&mddev->open_mutex);
497 mutex_init(&mddev->reconfig_mutex);
498 mutex_init(&mddev->bitmap_info.mutex);
499 INIT_LIST_HEAD(&mddev->disks);
500 INIT_LIST_HEAD(&mddev->all_mddevs);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400501 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
502 (unsigned long) mddev);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100503 atomic_set(&mddev->active, 1);
504 atomic_set(&mddev->openers, 0);
505 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100506 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100507 atomic_set(&mddev->flush_pending, 0);
508 init_waitqueue_head(&mddev->sb_wait);
509 init_waitqueue_head(&mddev->recovery_wait);
510 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000511 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500512 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100513 mddev->resync_min = 0;
514 mddev->resync_max = MaxSector;
515 mddev->level = LEVEL_NONE;
516}
NeilBrown390ee602010-06-01 19:37:27 +1000517EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100518
NeilBrownf72ffdd2014-09-30 14:23:59 +1000519static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
NeilBrownfd01b882011-10-11 16:47:53 +1100521 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
NeilBrown8f5f02c2011-02-16 13:58:51 +1100523 if (unit && MAJOR(unit) != MD_MAJOR)
524 unit &= ~((1<<MdpMinorShift)-1);
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 retry:
527 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
NeilBrownefeb53c2009-01-09 08:31:10 +1100529 if (unit) {
530 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
531 if (mddev->unit == unit) {
532 mddev_get(mddev);
533 spin_unlock(&all_mddevs_lock);
534 kfree(new);
535 return mddev;
536 }
537
538 if (new) {
539 list_add(&new->all_mddevs, &all_mddevs);
540 spin_unlock(&all_mddevs_lock);
541 new->hold_active = UNTIL_IOCTL;
542 return new;
543 }
544 } else if (new) {
545 /* find an unused unit number */
546 static int next_minor = 512;
547 int start = next_minor;
548 int is_free = 0;
549 int dev = 0;
550 while (!is_free) {
551 dev = MKDEV(MD_MAJOR, next_minor);
552 next_minor++;
553 if (next_minor > MINORMASK)
554 next_minor = 0;
555 if (next_minor == start) {
556 /* Oh dear, all in use. */
557 spin_unlock(&all_mddevs_lock);
558 kfree(new);
559 return NULL;
560 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000561
NeilBrownefeb53c2009-01-09 08:31:10 +1100562 is_free = 1;
563 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
564 if (mddev->unit == dev) {
565 is_free = 0;
566 break;
567 }
568 }
569 new->unit = dev;
570 new->md_minor = MINOR(dev);
571 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 list_add(&new->all_mddevs, &all_mddevs);
573 spin_unlock(&all_mddevs_lock);
574 return new;
575 }
576 spin_unlock(&all_mddevs_lock);
577
NeilBrown9ffae0c2006-01-06 00:20:32 -0800578 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (!new)
580 return NULL;
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 new->unit = unit;
583 if (MAJOR(unit) == MD_MAJOR)
584 new->md_minor = MINOR(unit);
585 else
586 new->md_minor = MINOR(unit) >> MdpMinorShift;
587
NeilBrownfafd7fb2010-04-01 15:55:30 +1100588 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 goto retry;
591}
592
NeilBrownb6eb1272010-04-15 10:13:47 +1000593static struct attribute_group md_redundancy_group;
594
NeilBrown5c47daf2014-12-15 12:57:01 +1100595void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
NeilBrowna64c8762010-04-14 17:15:37 +1000597 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000598 /* These cannot be removed under reconfig_mutex as
599 * an access to the files will try to take reconfig_mutex
600 * while holding the file unremovable, which leads to
601 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000602 * So hold set sysfs_active while the remove in happeing,
603 * and anything else which might set ->to_remove or my
604 * otherwise change the sysfs namespace will fail with
605 * -EBUSY if sysfs_active is still set.
606 * We set sysfs_active under reconfig_mutex and elsewhere
607 * test it under the same mutex to ensure its correct value
608 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000609 */
NeilBrowna64c8762010-04-14 17:15:37 +1000610 struct attribute_group *to_remove = mddev->to_remove;
611 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000612 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000613 mutex_unlock(&mddev->reconfig_mutex);
614
NeilBrown00bcb4a2010-06-01 19:37:23 +1000615 if (mddev->kobj.sd) {
616 if (to_remove != &md_redundancy_group)
617 sysfs_remove_group(&mddev->kobj, to_remove);
618 if (mddev->pers == NULL ||
619 mddev->pers->sync_request == NULL) {
620 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
621 if (mddev->sysfs_action)
622 sysfs_put(mddev->sysfs_action);
623 mddev->sysfs_action = NULL;
624 }
NeilBrowna64c8762010-04-14 17:15:37 +1000625 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000626 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000627 } else
628 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Chris Dunlop751e67c2011-10-19 16:48:26 +1100630 /* As we've dropped the mutex we need a spinlock to
631 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000632 */
633 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700634 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000635 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
NeilBrown5c47daf2014-12-15 12:57:01 +1100637EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500639struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100640{
641 struct md_rdev *rdev;
642
643 rdev_for_each_rcu(rdev, mddev)
644 if (rdev->desc_nr == nr)
645 return rdev;
646
647 return NULL;
648}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500649EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100650
651static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
NeilBrown3cb03002011-10-11 16:45:26 +1100653 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
NeilBrowndafb20f2012-03-19 12:46:39 +1100655 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (rdev->bdev->bd_dev == dev)
657 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return NULL;
660}
661
NeilBrown1ca69c42012-10-11 13:37:33 +1100662static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
663{
664 struct md_rdev *rdev;
665
666 rdev_for_each_rcu(rdev, mddev)
667 if (rdev->bdev->bd_dev == dev)
668 return rdev;
669
670 return NULL;
671}
672
NeilBrown84fc4b52011-10-11 16:49:58 +1100673static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800674{
NeilBrown84fc4b52011-10-11 16:49:58 +1100675 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800676 list_for_each_entry(pers, &pers_list, list) {
677 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800678 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800679 if (strcmp(pers->name, clevel)==0)
680 return pers;
681 }
NeilBrown2604b702006-01-06 00:20:36 -0800682 return NULL;
683}
684
Andre Nollb73df2d2008-07-11 22:02:23 +1000685/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100686static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100688 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000689 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
NeilBrownf72ffdd2014-09-30 14:23:59 +1000692static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100695 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000696 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return 0;
698}
699
NeilBrown545c8792012-05-22 13:54:30 +1000700void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800703 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 rdev->sb_loaded = 0;
705 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000706 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100707 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
NeilBrown2699b672011-07-28 11:31:47 +1000709 if (rdev->bb_page) {
710 put_page(rdev->bb_page);
711 rdev->bb_page = NULL;
712 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800713 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
NeilBrown545c8792012-05-22 13:54:30 +1000715EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200717static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700718{
NeilBrown3cb03002011-10-11 16:45:26 +1100719 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100720 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700721
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200722 if (bio->bi_error) {
NeilBrown9d487392016-11-02 14:16:49 +1100723 pr_err("md: super_written gets error=%d\n", bio->bi_error);
NeilBrowna9701a32005-11-08 21:39:34 -0800724 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100725 if (!test_bit(Faulty, &rdev->flags)
726 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800727 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100728 set_bit(LastDev, &rdev->flags);
729 }
730 } else
731 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700732
NeilBrowna9701a32005-11-08 21:39:34 -0800733 if (atomic_dec_and_test(&mddev->pending_writes))
734 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700735 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700736 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700737}
738
NeilBrownfd01b882011-10-11 16:47:53 +1100739void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700740 sector_t sector, int size, struct page *page)
741{
742 /* write first size bytes of page to sector of rdev
743 * Increment mddev->pending_writes before returning
744 * and decrement it on completion, waking up sb_wait
745 * if zero is reached.
746 * If an error occurred, call md_error
747 */
NeilBrown46533ff2016-11-18 16:16:11 +1100748 struct bio *bio;
749 int ff = 0;
750
751 if (test_bit(Faulty, &rdev->flags))
752 return;
753
754 bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700755
Shaohua Lied3b98c2016-03-29 14:00:19 -0700756 atomic_inc(&rdev->nr_pending);
757
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100758 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700759 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700760 bio_add_page(bio, page, size, 0);
761 bio->bi_private = rdev;
762 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100763
764 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
765 test_bit(FailFast, &rdev->flags) &&
766 !test_bit(LastDev, &rdev->flags))
767 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +0200768 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -0800769
NeilBrown7bfa19f2005-06-21 17:17:28 -0700770 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500771 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800772}
773
NeilBrown46533ff2016-11-18 16:16:11 +1100774int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800775{
Tejun Heoe9c74692010-09-03 11:56:18 +0200776 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000777 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -0800778 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +1100779 return -EAGAIN;
780 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700781}
782
NeilBrown3cb03002011-10-11 16:45:26 +1100783int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -0500784 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
NeilBrowna167f662010-10-26 18:31:13 +1100786 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 int ret;
788
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100789 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
790 rdev->meta_bdev : rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -0500791 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100792 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700793 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000794 else if (rdev->mddev->reshape_position != MaxSector &&
795 (rdev->mddev->reshape_backwards ==
796 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700797 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100798 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700799 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500801
802 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200804 ret = !bio->bi_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 bio_put(bio);
806 return ret;
807}
NeilBrowna8745db2006-01-06 00:20:34 -0800808EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
NeilBrownf72ffdd2014-09-30 14:23:59 +1000810static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (rdev->sb_loaded)
815 return 0;
816
Mike Christie796a5cf2016-06-05 14:32:07 -0500817 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 goto fail;
819 rdev->sb_loaded = 1;
820 return 0;
821
822fail:
NeilBrown9d487392016-11-02 14:16:49 +1100823 pr_err("md: disabled device %s, could not read superblock.\n",
824 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return -EINVAL;
826}
827
828static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
829{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000830 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000831 sb1->set_uuid1 == sb2->set_uuid1 &&
832 sb1->set_uuid2 == sb2->set_uuid2 &&
833 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
837{
838 int ret;
839 mdp_super_t *tmp1, *tmp2;
840
841 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
842 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
843
844 if (!tmp1 || !tmp2) {
845 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 goto abort;
847 }
848
849 *tmp1 = *sb1;
850 *tmp2 = *sb2;
851
852 /*
853 * nr_disks is not constant
854 */
855 tmp1->nr_disks = 0;
856 tmp2->nr_disks = 0;
857
Andre Nollce0c8e02008-07-11 22:02:20 +1000858 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700860 kfree(tmp1);
861 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return ret;
863}
864
NeilBrown4d167f02007-05-09 02:35:37 -0700865static u32 md_csum_fold(u32 csum)
866{
867 csum = (csum & 0xffff) + (csum >> 16);
868 return (csum & 0xffff) + (csum >> 16);
869}
870
NeilBrownf72ffdd2014-09-30 14:23:59 +1000871static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
NeilBrown4d167f02007-05-09 02:35:37 -0700873 u64 newcsum = 0;
874 u32 *sb32 = (u32*)sb;
875 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 unsigned int disk_csum, csum;
877
878 disk_csum = sb->sb_csum;
879 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700880
881 for (i = 0; i < MD_SB_BYTES/4 ; i++)
882 newcsum += sb32[i];
883 csum = (newcsum & 0xffffffff) + (newcsum>>32);
884
NeilBrown4d167f02007-05-09 02:35:37 -0700885#ifdef CONFIG_ALPHA
886 /* This used to use csum_partial, which was wrong for several
887 * reasons including that different results are returned on
888 * different architectures. It isn't critical that we get exactly
889 * the same return value as before (we always csum_fold before
890 * testing, and that removes any differences). However as we
891 * know that csum_partial always returned a 16bit value on
892 * alphas, do a fold to maximise conformity to previous behaviour.
893 */
894 sb->sb_csum = md_csum_fold(disk_csum);
895#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700897#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return csum;
899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901/*
902 * Handle superblock details.
903 * We want to be able to handle multiple superblock formats
904 * so we have a common interface to them all, and an array of
905 * different handlers.
906 * We rely on user-space to write the initial superblock, and support
907 * reading and updating of superblocks.
908 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100909 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * loads and validates a superblock on dev.
911 * if refdev != NULL, compare superblocks on both devices
912 * Return:
913 * 0 - dev has a superblock that is compatible with refdev
914 * 1 - dev has a superblock that is compatible and newer than refdev
915 * so dev should be used as the refdev in future
916 * -EINVAL superblock incompatible or invalid
917 * -othererror e.g. -EIO
918 *
NeilBrownfd01b882011-10-11 16:47:53 +1100919 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 * Verify that dev is acceptable into mddev.
921 * The first time, mddev->raid_disks will be 0, and data from
922 * dev should be merged in. Subsequent calls check that dev
923 * is new enough. Return 0 or -EINVAL
924 *
NeilBrownfd01b882011-10-11 16:47:53 +1100925 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 * Update the superblock for rdev with data in mddev
927 * This does not write to disc.
928 *
929 */
930
931struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000932 char *name;
933 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000934 int (*load_super)(struct md_rdev *rdev,
935 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000936 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000937 int (*validate_super)(struct mddev *mddev,
938 struct md_rdev *rdev);
939 void (*sync_super)(struct mddev *mddev,
940 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100941 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000942 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000943 int (*allow_new_offset)(struct md_rdev *rdev,
944 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945};
946
947/*
Andre Noll0894cc32009-06-18 08:49:23 +1000948 * Check that the given mddev has no bitmap.
949 *
950 * This function is called from the run method of all personalities that do not
951 * support bitmaps. It prints an error message and returns non-zero if mddev
952 * has a bitmap. Otherwise, it returns 0.
953 *
954 */
NeilBrownfd01b882011-10-11 16:47:53 +1100955int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000956{
NeilBrownc3d97142009-12-14 12:49:52 +1100957 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000958 return 0;
NeilBrown9d487392016-11-02 14:16:49 +1100959 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +1000960 mdname(mddev), mddev->pers->name);
961 return 1;
962}
963EXPORT_SYMBOL(md_check_no_bitmap);
964
965/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000966 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
NeilBrown3cb03002011-10-11 16:45:26 +1100968static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
971 mdp_super_t *sb;
972 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 /*
Andre Noll0f420352008-07-11 22:02:23 +1000975 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 * it's at the end of the disk.
977 *
978 * It also happens to be a multiple of 4Kb.
979 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100980 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
NeilBrown0002b272005-09-09 16:23:53 -0700982 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +1100983 if (ret)
984 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 ret = -EINVAL;
987
988 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000989 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +1100992 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 goto abort;
994 }
995
996 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800997 sb->minor_version < 90 ||
998 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +1100999 pr_warn("Bad version number %d.%d on %s\n",
1000 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 goto abort;
1002 }
1003
1004 if (sb->raid_disks <= 0)
1005 goto abort;
1006
NeilBrown4d167f02007-05-09 02:35:37 -07001007 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001008 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 goto abort;
1010 }
1011
1012 rdev->preferred_minor = sb->md_minor;
1013 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001014 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001015 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001016 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 if (sb->level == LEVEL_MULTIPATH)
1019 rdev->desc_nr = -1;
1020 else
1021 rdev->desc_nr = sb->this_disk.number;
1022
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001023 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001025 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001027 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (!uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001029 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 b, bdevname(refdev->bdev,b2));
1031 goto abort;
1032 }
1033 if (!sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001034 pr_warn("md: %s has same UUID but different superblock to %s\n",
1035 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 goto abort;
1037 }
1038 ev1 = md_event(sb);
1039 ev2 = md_event(refsb);
1040 if (ev1 > ev2)
1041 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001042 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 ret = 0;
1044 }
NeilBrown8190e752009-06-18 08:48:58 +10001045 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001046 /* Limit to 4TB as metadata cannot record more than that.
1047 * (not needed for Linear and RAID0 as metadata doesn't
1048 * record this size)
1049 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001050 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1051 sb->level >= 1)
1052 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
NeilBrown27a7b262011-09-10 17:21:28 +10001054 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001055 /* "this cannot possibly happen" ... */
1056 ret = -EINVAL;
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 abort:
1059 return ret;
1060}
1061
1062/*
1063 * validate_super for 0.90.0
1064 */
NeilBrownfd01b882011-10-11 16:47:53 +11001065static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001068 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001069 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
NeilBrown41158c72005-06-21 17:17:25 -07001071 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001072 clear_bit(Faulty, &rdev->flags);
1073 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001074 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001075 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (mddev->raid_disks == 0) {
1078 mddev->major_version = 0;
1079 mddev->minor_version = sb->minor_version;
1080 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001081 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001082 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 mddev->ctime = sb->ctime;
1084 mddev->utime = sb->utime;
1085 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001086 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 mddev->layout = sb->layout;
1088 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001089 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001090 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001091 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001092 mddev->bitmap_info.space = 0;
1093 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001094 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001095 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001096 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
NeilBrownf6705572006-03-27 01:18:11 -08001098 if (mddev->minor_version >= 91) {
1099 mddev->reshape_position = sb->reshape_position;
1100 mddev->delta_disks = sb->delta_disks;
1101 mddev->new_level = sb->new_level;
1102 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001103 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001104 if (mddev->delta_disks < 0)
1105 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001106 } else {
1107 mddev->reshape_position = MaxSector;
1108 mddev->delta_disks = 0;
1109 mddev->new_level = mddev->level;
1110 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001111 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001112 }
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (sb->state & (1<<MD_SB_CLEAN))
1115 mddev->recovery_cp = MaxSector;
1116 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001117 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 sb->events_lo == sb->cp_events_lo) {
1119 mddev->recovery_cp = sb->recovery_cp;
1120 } else
1121 mddev->recovery_cp = 0;
1122 }
1123
1124 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1125 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1126 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1127 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1128
1129 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001130
1131 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001132 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001133 mddev->bitmap_info.offset =
1134 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001135 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001136 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001137 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001138
NeilBrown41158c72005-06-21 17:17:25 -07001139 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001140 /* Insist on good event counter while assembling, except
1141 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001143 if (sb->disks[rdev->desc_nr].state & (
1144 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001145 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001146 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001147 } else if (mddev->bitmap) {
1148 /* if adding to array with a bitmap, then we can accept an
1149 * older device ... but not too old.
1150 */
NeilBrown41158c72005-06-21 17:17:25 -07001151 if (ev1 < mddev->bitmap->events_cleared)
1152 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001153 if (ev1 < mddev->events)
1154 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001155 } else {
1156 if (ev1 < mddev->events)
1157 /* just a hot-add of a new device, leave raid_disk at -1 */
1158 return 0;
1159 }
NeilBrown41158c72005-06-21 17:17:25 -07001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 desc = sb->disks + rdev->desc_nr;
1163
1164 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001165 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001166 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1167 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001168 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001170 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001171 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1172 /* active but not in sync implies recovery up to
1173 * reshape position. We don't know exactly where
1174 * that is, so set to zero for now */
1175 if (mddev->minor_version >= 91) {
1176 rdev->recovery_offset = 0;
1177 rdev->raid_disk = desc->raid_disk;
1178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001180 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1181 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001182 if (desc->state & (1<<MD_DISK_FAILFAST))
1183 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001184 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001185 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return 0;
1187}
1188
1189/*
1190 * sync_super for 0.90.0
1191 */
NeilBrownfd01b882011-10-11 16:47:53 +11001192static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001195 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /* make rdev->sb match mddev data..
1199 *
1200 * 1/ zero out disks
1201 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1202 * 3/ any empty disks < next_spare become removed
1203 *
1204 * disks[0] gets initialised to REMOVED because
1205 * we cannot be sure from other fields if it has
1206 * been initialised or not.
1207 */
1208 int i;
1209 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1210
NeilBrown61181562005-09-09 16:24:02 -07001211 rdev->sb_size = MD_SB_BYTES;
1212
Namhyung Kim65a06f062011-07-27 11:00:36 +10001213 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 memset(sb, 0, sizeof(*sb));
1216
1217 sb->md_magic = MD_SB_MAGIC;
1218 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 sb->patch_version = mddev->patch_version;
1220 sb->gvalid_words = 0; /* ignored */
1221 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1222 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1223 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1224 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1225
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001226 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001228 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 sb->raid_disks = mddev->raid_disks;
1230 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001231 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001232 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 sb->state = 0;
1234 sb->events_hi = (mddev->events>>32);
1235 sb->events_lo = (u32)mddev->events;
1236
NeilBrownf6705572006-03-27 01:18:11 -08001237 if (mddev->reshape_position == MaxSector)
1238 sb->minor_version = 90;
1239 else {
1240 sb->minor_version = 91;
1241 sb->reshape_position = mddev->reshape_position;
1242 sb->new_level = mddev->new_level;
1243 sb->delta_disks = mddev->delta_disks;
1244 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001245 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001246 }
1247 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (mddev->in_sync)
1249 {
1250 sb->recovery_cp = mddev->recovery_cp;
1251 sb->cp_events_hi = (mddev->events>>32);
1252 sb->cp_events_lo = (u32)mddev->events;
1253 if (mddev->recovery_cp == MaxSector)
1254 sb->state = (1<< MD_SB_CLEAN);
1255 } else
1256 sb->recovery_cp = 0;
1257
1258 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001259 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
NeilBrownc3d97142009-12-14 12:49:52 +11001261 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001262 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001265 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001267 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001268 int is_active = test_bit(In_sync, &rdev2->flags);
1269
1270 if (rdev2->raid_disk >= 0 &&
1271 sb->minor_version >= 91)
1272 /* we have nowhere to store the recovery_offset,
1273 * but if it is not below the reshape_position,
1274 * we can piggy-back on that.
1275 */
1276 is_active = 1;
1277 if (rdev2->raid_disk < 0 ||
1278 test_bit(Faulty, &rdev2->flags))
1279 is_active = 0;
1280 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001281 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001283 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001284 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 d = &sb->disks[rdev2->desc_nr];
1286 nr_disks++;
1287 d->number = rdev2->desc_nr;
1288 d->major = MAJOR(rdev2->bdev->bd_dev);
1289 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001290 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 d->raid_disk = rdev2->raid_disk;
1292 else
1293 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001294 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001296 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001298 if (test_bit(In_sync, &rdev2->flags))
1299 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 active++;
1301 working++;
1302 } else {
1303 d->state = 0;
1304 spare++;
1305 working++;
1306 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001307 if (test_bit(WriteMostly, &rdev2->flags))
1308 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001309 if (test_bit(FailFast, &rdev2->flags))
1310 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 /* now set the "removed" and "faulty" bits on any missing devices */
1313 for (i=0 ; i < mddev->raid_disks ; i++) {
1314 mdp_disk_t *d = &sb->disks[i];
1315 if (d->state == 0 && d->number == 0) {
1316 d->number = i;
1317 d->raid_disk = i;
1318 d->state = (1<<MD_DISK_REMOVED);
1319 d->state |= (1<<MD_DISK_FAULTY);
1320 failed++;
1321 }
1322 }
1323 sb->nr_disks = nr_disks;
1324 sb->active_disks = active;
1325 sb->working_disks = working;
1326 sb->failed_disks = failed;
1327 sb->spare_disks = spare;
1328
1329 sb->this_disk = sb->disks[rdev->desc_nr];
1330 sb->sb_csum = calc_sb_csum(sb);
1331}
1332
1333/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001334 * rdev_size_change for 0.90.0
1335 */
1336static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001337super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001338{
Andre Noll58c0fed2009-03-31 14:33:13 +11001339 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001340 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001341 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001342 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001343 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001344 if (!num_sectors || num_sectors > rdev->sb_start)
1345 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001346 /* Limit to 4TB as metadata cannot record more than that.
1347 * 4TB == 2^32 KB, or 2*2^32 sectors.
1348 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001349 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1350 rdev->mddev->level >= 1)
1351 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001352 do {
1353 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001354 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001355 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001356 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001357}
1358
NeilBrownc6563a82012-05-21 09:27:00 +10001359static int
1360super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1361{
1362 /* non-zero offset changes not possible with v0.90 */
1363 return new_offset == 0;
1364}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001365
1366/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 * version 1 superblock
1368 */
1369
NeilBrownf72ffdd2014-09-30 14:23:59 +10001370static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001372 __le32 disk_csum;
1373 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 unsigned long long newcsum;
1375 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001376 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 disk_csum = sb->sb_csum;
1379 sb->sb_csum = 0;
1380 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001381 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 newcsum += le32_to_cpu(*isuper++);
1383
1384 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001385 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1388 sb->sb_csum = disk_csum;
1389 return cpu_to_le32(csum);
1390}
1391
NeilBrown3cb03002011-10-11 16:45:26 +11001392static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
1394 struct mdp_superblock_1 *sb;
1395 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001396 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001397 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001399 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 /*
Andre Noll0f420352008-07-11 22:02:23 +10001402 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 * It is always aligned to a 4K boundary and
1404 * depeding on minor_version, it can be:
1405 * 0: At least 8K, but less than 12K, from end of device
1406 * 1: At start of device
1407 * 2: 4K from start of device.
1408 */
1409 switch(minor_version) {
1410 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001411 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001412 sb_start -= 8*2;
1413 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 break;
1415 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001416 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 break;
1418 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001419 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 break;
1421 default:
1422 return -EINVAL;
1423 }
Andre Noll0f420352008-07-11 22:02:23 +10001424 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
NeilBrown0002b272005-09-09 16:23:53 -07001426 /* superblock is rarely larger than 1K, but it can be larger,
1427 * and it is safe to read 4k, so we do that
1428 */
1429 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (ret) return ret;
1431
Namhyung Kim65a06f062011-07-27 11:00:36 +10001432 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1435 sb->major_version != cpu_to_le32(1) ||
1436 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001437 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001438 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return -EINVAL;
1440
1441 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001442 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 bdevname(rdev->bdev,b));
1444 return -EINVAL;
1445 }
1446 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001447 pr_warn("md: data_size too small on %s\n",
1448 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return -EINVAL;
1450 }
NeilBrownc6563a82012-05-21 09:27:00 +10001451 if (sb->pad0 ||
1452 sb->pad3[0] ||
1453 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1454 /* Some padding is non-zero, might be a new feature */
1455 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 rdev->preferred_minor = 0xffff;
1458 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001459 rdev->new_data_offset = rdev->data_offset;
1460 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1461 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1462 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001463 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
NeilBrown0002b272005-09-09 16:23:53 -07001465 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001466 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001467 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001468 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1469
1470 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001471 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001472 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001473 if (minor_version
1474 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1475 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001476
NeilBrown31b65a02006-07-10 04:44:14 -07001477 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1478 rdev->desc_nr = -1;
1479 else
1480 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1481
NeilBrown2699b672011-07-28 11:31:47 +10001482 if (!rdev->bb_page) {
1483 rdev->bb_page = alloc_page(GFP_KERNEL);
1484 if (!rdev->bb_page)
1485 return -ENOMEM;
1486 }
1487 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1488 rdev->badblocks.count == 0) {
1489 /* need to load the bad block list.
1490 * Currently we limit it to one page.
1491 */
1492 s32 offset;
1493 sector_t bb_sector;
1494 u64 *bbp;
1495 int i;
1496 int sectors = le16_to_cpu(sb->bblog_size);
1497 if (sectors > (PAGE_SIZE / 512))
1498 return -EINVAL;
1499 offset = le32_to_cpu(sb->bblog_offset);
1500 if (offset == 0)
1501 return -EINVAL;
1502 bb_sector = (long long)offset;
1503 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001504 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001505 return -EIO;
1506 bbp = (u64 *)page_address(rdev->bb_page);
1507 rdev->badblocks.shift = sb->bblog_shift;
1508 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1509 u64 bb = le64_to_cpu(*bbp);
1510 int count = bb & (0x3ff);
1511 u64 sector = bb >> 10;
1512 sector <<= sb->bblog_shift;
1513 count <<= sb->bblog_shift;
1514 if (bb + 1 == 0)
1515 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001516 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001517 return -EINVAL;
1518 }
NeilBrown486adf72013-04-24 11:42:44 +10001519 } else if (sb->bblog_offset != 0)
1520 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001521
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001522 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) {
1523 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1524 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1525 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1526 }
1527
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001528 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001529 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001530 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001532 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1535 sb->level != refsb->level ||
1536 sb->layout != refsb->layout ||
1537 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001538 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 bdevname(rdev->bdev,b),
1540 bdevname(refdev->bdev,b2));
1541 return -EINVAL;
1542 }
1543 ev1 = le64_to_cpu(sb->events);
1544 ev2 = le64_to_cpu(refsb->events);
1545
1546 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001547 ret = 1;
1548 else
1549 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
NeilBrownc6563a82012-05-21 09:27:00 +10001551 if (minor_version) {
1552 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1553 sectors -= rdev->data_offset;
1554 } else
1555 sectors = rdev->sb_start;
1556 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001558 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001559 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
NeilBrownfd01b882011-10-11 16:47:53 +11001562static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001564 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001565 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
NeilBrown41158c72005-06-21 17:17:25 -07001567 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001568 clear_bit(Faulty, &rdev->flags);
1569 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001570 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001571 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (mddev->raid_disks == 0) {
1574 mddev->major_version = 1;
1575 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001576 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001577 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001578 mddev->ctime = le64_to_cpu(sb->ctime);
1579 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001581 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 mddev->layout = le32_to_cpu(sb->layout);
1583 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001584 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001585 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001586 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001587 mddev->bitmap_info.space = 0;
1588 /* Default location for bitmap is 1K after superblock
1589 * using 3K - total of 4K
1590 */
NeilBrownc3d97142009-12-14 12:49:52 +11001591 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001592 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001593 mddev->reshape_backwards = 0;
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1596 memcpy(mddev->uuid, sb->set_uuid, 16);
1597
1598 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001599
NeilBrown71c08052005-09-09 16:23:51 -07001600 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001601 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001602 mddev->bitmap_info.offset =
1603 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001604 /* Metadata doesn't record how much space is available.
1605 * For 1.0, we assume we can use up to the superblock
1606 * if before, else to 4K beyond superblock.
1607 * For others, assume no change is possible.
1608 */
1609 if (mddev->minor_version > 0)
1610 mddev->bitmap_info.space = 0;
1611 else if (mddev->bitmap_info.offset > 0)
1612 mddev->bitmap_info.space =
1613 8 - mddev->bitmap_info.offset;
1614 else
1615 mddev->bitmap_info.space =
1616 -mddev->bitmap_info.offset;
1617 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001618
NeilBrownf6705572006-03-27 01:18:11 -08001619 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1620 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1621 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1622 mddev->new_level = le32_to_cpu(sb->new_level);
1623 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001624 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001625 if (mddev->delta_disks < 0 ||
1626 (mddev->delta_disks == 0 &&
1627 (le32_to_cpu(sb->feature_map)
1628 & MD_FEATURE_RESHAPE_BACKWARDS)))
1629 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001630 } else {
1631 mddev->reshape_position = MaxSector;
1632 mddev->delta_disks = 0;
1633 mddev->new_level = mddev->level;
1634 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001635 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001636 }
1637
Song Liu486b0f72016-08-19 15:34:01 -07001638 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001639 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001640
1641 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) {
1642 if (le32_to_cpu(sb->feature_map) &
1643 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1644 return -EINVAL;
1645 set_bit(MD_HAS_PPL, &mddev->flags);
1646 }
NeilBrown41158c72005-06-21 17:17:25 -07001647 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001648 /* Insist of good event counter while assembling, except for
1649 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001651 if (rdev->desc_nr >= 0 &&
1652 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001653 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1654 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001655 if (ev1 < mddev->events)
1656 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001657 } else if (mddev->bitmap) {
1658 /* If adding to array with a bitmap, then we can accept an
1659 * older device, but not too old.
1660 */
NeilBrown41158c72005-06-21 17:17:25 -07001661 if (ev1 < mddev->bitmap->events_cleared)
1662 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001663 if (ev1 < mddev->events)
1664 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001665 } else {
1666 if (ev1 < mddev->events)
1667 /* just a hot-add of a new device, leave raid_disk at -1 */
1668 return 0;
1669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (mddev->level != LEVEL_MULTIPATH) {
1671 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001672 if (rdev->desc_nr < 0 ||
1673 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001674 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001675 rdev->desc_nr = -1;
1676 } else
1677 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001679 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001681 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001682 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 break;
Song Liubac624f2015-08-13 14:31:55 -07001684 case MD_DISK_ROLE_JOURNAL: /* journal device */
1685 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1686 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001687 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001688 return -EINVAL;
1689 }
1690 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001691 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001692 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001693 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 default:
NeilBrownf4667222013-12-09 12:04:56 +11001695 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001696 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001697 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001698 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001699 if (!(le32_to_cpu(sb->feature_map) &
1700 MD_FEATURE_RECOVERY_BITMAP))
1701 rdev->saved_raid_disk = -1;
1702 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001703 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 rdev->raid_disk = role;
1705 break;
1706 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001707 if (sb->devflags & WriteMostly1)
1708 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001709 if (sb->devflags & FailFast1)
1710 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001711 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1712 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001713 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001714 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return 0;
1717}
1718
NeilBrownfd01b882011-10-11 16:47:53 +11001719static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
1721 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001722 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 int max_dev, i;
1724 /* make rdev->sb match mddev and rdev data. */
1725
Namhyung Kim65a06f062011-07-27 11:00:36 +10001726 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 sb->feature_map = 0;
1729 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001730 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 memset(sb->pad3, 0, sizeof(sb->pad3));
1732
1733 sb->utime = cpu_to_le64((__u64)mddev->utime);
1734 sb->events = cpu_to_le64(mddev->events);
1735 if (mddev->in_sync)
1736 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001737 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1738 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 else
1740 sb->resync_offset = cpu_to_le64(0);
1741
NeilBrown1c05b4b2006-10-21 10:24:08 -07001742 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001743
NeilBrownf0ca3402006-02-02 14:28:04 -08001744 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001745 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001746 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001747 sb->level = cpu_to_le32(mddev->level);
1748 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11001749 if (test_bit(FailFast, &rdev->flags))
1750 sb->devflags |= FailFast1;
1751 else
1752 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08001753
NeilBrownaeb9b2112011-08-25 14:43:08 +10001754 if (test_bit(WriteMostly, &rdev->flags))
1755 sb->devflags |= WriteMostly1;
1756 else
1757 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001758 sb->data_offset = cpu_to_le64(rdev->data_offset);
1759 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001760
NeilBrownc3d97142009-12-14 12:49:52 +11001761 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1762 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001763 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001764 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001765
Shaohua Lif2076e72015-10-08 21:54:12 -07001766 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001767 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001768 sb->feature_map |=
1769 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1770 sb->recovery_offset =
1771 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001772 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1773 sb->feature_map |=
1774 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001775 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001776 /* Note: recovery_offset and journal_tail share space */
1777 if (test_bit(Journal, &rdev->flags))
1778 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001779 if (test_bit(Replacement, &rdev->flags))
1780 sb->feature_map |=
1781 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001782
NeilBrownf6705572006-03-27 01:18:11 -08001783 if (mddev->reshape_position != MaxSector) {
1784 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1785 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1786 sb->new_layout = cpu_to_le32(mddev->new_layout);
1787 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1788 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001789 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001790 if (mddev->delta_disks == 0 &&
1791 mddev->reshape_backwards)
1792 sb->feature_map
1793 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001794 if (rdev->new_data_offset != rdev->data_offset) {
1795 sb->feature_map
1796 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1797 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1798 - rdev->data_offset));
1799 }
NeilBrownf6705572006-03-27 01:18:11 -08001800 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001801
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001802 if (mddev_is_clustered(mddev))
1803 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1804
NeilBrown2699b672011-07-28 11:31:47 +10001805 if (rdev->badblocks.count == 0)
1806 /* Nothing to do for bad blocks*/ ;
1807 else if (sb->bblog_offset == 0)
1808 /* Cannot record bad blocks on this device */
1809 md_error(mddev, rdev);
1810 else {
1811 struct badblocks *bb = &rdev->badblocks;
1812 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1813 u64 *p = bb->page;
1814 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1815 if (bb->changed) {
1816 unsigned seq;
1817
1818retry:
1819 seq = read_seqbegin(&bb->lock);
1820
1821 memset(bbp, 0xff, PAGE_SIZE);
1822
1823 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001824 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001825 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1826 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001827 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001828 }
NeilBrownd0962932012-03-19 12:46:41 +11001829 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001830 if (read_seqretry(&bb->lock, seq))
1831 goto retry;
1832
1833 bb->sector = (rdev->sb_start +
1834 (int)le32_to_cpu(sb->bblog_offset));
1835 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001836 }
1837 }
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001840 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (rdev2->desc_nr+1 > max_dev)
1842 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001843
NeilBrown70471da2009-08-03 10:59:57 +10001844 if (max_dev > le32_to_cpu(sb->max_dev)) {
1845 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001846 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001847 rdev->sb_size = max_dev * 2 + 256;
1848 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1849 if (rdev->sb_size & bmask)
1850 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001851 } else
1852 max_dev = le32_to_cpu(sb->max_dev);
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 for (i=0; i<max_dev;i++)
Song Liuc4d4c912015-08-13 14:31:54 -07001855 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001856
Song Liua97b7892015-10-08 21:54:09 -07001857 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1858 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001860 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
1861 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
1862 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
1863 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
1864 }
1865
NeilBrowndafb20f2012-03-19 12:46:39 +11001866 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001868 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001869 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001870 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001872 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001873 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001874 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001875 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 else
Song Liuc4d4c912015-08-13 14:31:54 -07001877 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 sb->sb_csum = calc_sb_1_csum(sb);
1881}
1882
Chris Webb0cd17fe2008-06-28 08:31:46 +10001883static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001884super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001885{
1886 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001887 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001888 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001889 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001890 if (rdev->data_offset != rdev->new_data_offset)
1891 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001892 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001893 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001894 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001895 max_sectors -= rdev->data_offset;
1896 if (!num_sectors || num_sectors > max_sectors)
1897 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001898 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001899 /* minor version 0 with bitmap we can't move */
1900 return 0;
1901 } else {
1902 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001903 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001904 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001905 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001906 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001907 if (!num_sectors || num_sectors > max_sectors)
1908 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001909 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001910 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001911 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001912 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08001913 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10001914 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11001915 do {
1916 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1917 rdev->sb_page);
1918 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001919 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001920
1921}
1922
1923static int
1924super_1_allow_new_offset(struct md_rdev *rdev,
1925 unsigned long long new_offset)
1926{
1927 /* All necessary checks on new >= old have been done */
1928 struct bitmap *bitmap;
1929 if (new_offset >= rdev->data_offset)
1930 return 1;
1931
1932 /* with 1.0 metadata, there is no metadata to tread on
1933 * so we can always move back */
1934 if (rdev->mddev->minor_version == 0)
1935 return 1;
1936
1937 /* otherwise we must be sure not to step on
1938 * any metadata, so stay:
1939 * 36K beyond start of superblock
1940 * beyond end of badblocks
1941 * beyond write-intent bitmap
1942 */
1943 if (rdev->sb_start + (32+4)*2 > new_offset)
1944 return 0;
1945 bitmap = rdev->mddev->bitmap;
1946 if (bitmap && !rdev->mddev->bitmap_info.file &&
1947 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001948 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001949 return 0;
1950 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1951 return 0;
1952
1953 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001954}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Adrian Bunk75c96f82005-05-05 16:16:09 -07001956static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 [0] = {
1958 .name = "0.90.0",
1959 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001960 .load_super = super_90_load,
1961 .validate_super = super_90_validate,
1962 .sync_super = super_90_sync,
1963 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001964 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 },
1966 [1] = {
1967 .name = "md-1",
1968 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001969 .load_super = super_1_load,
1970 .validate_super = super_1_validate,
1971 .sync_super = super_1_sync,
1972 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001973 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 },
1975};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
NeilBrownfd01b882011-10-11 16:47:53 +11001977static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001978{
1979 if (mddev->sync_super) {
1980 mddev->sync_super(mddev, rdev);
1981 return;
1982 }
1983
1984 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1985
1986 super_types[mddev->major_version].sync_super(mddev, rdev);
1987}
1988
NeilBrownfd01b882011-10-11 16:47:53 +11001989static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
NeilBrown3cb03002011-10-11 16:45:26 +11001991 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
NeilBrown4b809912008-07-21 17:05:25 +10001993 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07001994 rdev_for_each_rcu(rdev, mddev1) {
1995 if (test_bit(Faulty, &rdev->flags) ||
1996 test_bit(Journal, &rdev->flags) ||
1997 rdev->raid_disk == -1)
1998 continue;
1999 rdev_for_each_rcu(rdev2, mddev2) {
2000 if (test_bit(Faulty, &rdev2->flags) ||
2001 test_bit(Journal, &rdev2->flags) ||
2002 rdev2->raid_disk == -1)
2003 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002004 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002005 rdev2->bdev->bd_contains) {
2006 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002007 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002008 }
Song Liu0b020e82015-09-03 23:00:35 -07002009 }
2010 }
NeilBrown4b809912008-07-21 17:05:25 +10002011 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return 0;
2013}
2014
2015static LIST_HEAD(pending_raid_disks);
2016
Andre Nollac5e7112009-08-03 10:59:47 +10002017/*
2018 * Try to register data integrity profile for an mddev
2019 *
2020 * This is called when an array is started and after a disk has been kicked
2021 * from the array. It only succeeds if all working and active component devices
2022 * are integrity capable with matching profiles.
2023 */
NeilBrownfd01b882011-10-11 16:47:53 +11002024int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002025{
NeilBrown3cb03002011-10-11 16:45:26 +11002026 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002027
Andre Nollac5e7112009-08-03 10:59:47 +10002028 if (list_empty(&mddev->disks))
2029 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002030 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2031 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002032 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002033 /* skip spares and non-functional disks */
2034 if (test_bit(Faulty, &rdev->flags))
2035 continue;
2036 if (rdev->raid_disk < 0)
2037 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002038 if (!reference) {
2039 /* Use the first rdev as the reference */
2040 reference = rdev;
2041 continue;
2042 }
2043 /* does this rdev's profile match the reference profile? */
2044 if (blk_integrity_compare(reference->bdev->bd_disk,
2045 rdev->bdev->bd_disk) < 0)
2046 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002047 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002048 if (!reference || !bdev_get_integrity(reference->bdev))
2049 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002050 /*
2051 * All component devices are integrity capable and have matching
2052 * profiles, register the common profile for the md device.
2053 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002054 blk_integrity_register(mddev->gendisk,
2055 bdev_get_integrity(reference->bdev));
2056
NeilBrown9d487392016-11-02 14:16:49 +11002057 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Martin K. Petersena91a2782011-03-17 11:11:05 +01002058 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002059 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002060 mdname(mddev));
2061 return -EINVAL;
2062 }
Andre Nollac5e7112009-08-03 10:59:47 +10002063 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002064}
Andre Nollac5e7112009-08-03 10:59:47 +10002065EXPORT_SYMBOL(md_integrity_register);
2066
Dan Williams1501efa2016-01-13 16:00:07 -08002067/*
2068 * Attempt to add an rdev, but only if it is consistent with the current
2069 * integrity profile
2070 */
2071int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002072{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002073 struct blk_integrity *bi_rdev;
2074 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002075 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002076
2077 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002078 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002079
2080 bi_rdev = bdev_get_integrity(rdev->bdev);
2081 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002082
2083 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002084 return 0;
2085
2086 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002087 pr_err("%s: incompatible integrity profile for %s\n",
2088 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002089 return -ENXIO;
2090 }
2091
2092 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002093}
2094EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002095
NeilBrownf72ffdd2014-09-30 14:23:59 +10002096static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002098 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002099 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002100 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Dan Williams11e2ede2008-04-30 00:52:32 -07002102 /* prevent duplicates */
2103 if (find_rdev(mddev, rdev->bdev->bd_dev))
2104 return -EEXIST;
2105
NeilBrown97b20ef2017-04-13 08:53:48 +10002106 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2107 mddev->pers)
2108 return -EROFS;
2109
Andre Nolldd8ac332009-03-31 14:33:13 +11002110 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002111 if (!test_bit(Journal, &rdev->flags) &&
2112 rdev->sectors &&
2113 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002114 if (mddev->pers) {
2115 /* Cannot change size, so fail
2116 * If mddev->level <= 0, then we don't care
2117 * about aligning sizes (e.g. linear)
2118 */
2119 if (mddev->level > 0)
2120 return -ENOSPC;
2121 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002122 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 /* Verify rdev->desc_nr is unique.
2126 * If it is -1, assign a free number, else
2127 * check number is not in use
2128 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002129 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if (rdev->desc_nr < 0) {
2131 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002132 if (mddev->pers)
2133 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002134 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 choice++;
2136 rdev->desc_nr = choice;
2137 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002138 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002139 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002143 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002144 if (!test_bit(Journal, &rdev->flags) &&
2145 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002146 pr_warn("md: %s: array is limited to %d devices\n",
2147 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002148 return -EBUSY;
2149 }
NeilBrown19133a42005-11-08 21:39:35 -08002150 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002151 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002154 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002155
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002156 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002157 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002158
Tejun Heo0762b8b2008-08-25 19:56:12 +09002159 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002160 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2161 /* failure here is OK */;
2162 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002163
NeilBrown4b809912008-07-21 17:05:25 +10002164 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002165 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002166
2167 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002168 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002171
2172 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002173 pr_warn("md: failed to register dev-%s for %s\n",
2174 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002175 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
NeilBrown177a99b2008-02-06 01:39:56 -08002178static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002179{
NeilBrown3cb03002011-10-11 16:45:26 +11002180 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002181 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002182 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002183}
2184
NeilBrownf72ffdd2014-09-30 14:23:59 +10002185static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
2187 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002188
Tejun Heo49731ba2011-01-14 18:43:57 +01002189 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002190 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002191 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002193 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002194 sysfs_put(rdev->sysfs_state);
2195 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002196 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002197 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002198 * writing to 'remove' to "dev/state". We also need
2199 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002200 */
NeilBrown4b809912008-07-21 17:05:25 +10002201 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002202 INIT_WORK(&rdev->del_work, md_delayed_delete);
2203 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002204 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
2207/*
2208 * prevent the device from being mounted, repartitioned or
2209 * otherwise reused by a RAID array (or any other kernel
2210 * subsystem), by bd_claiming the device.
2211 */
NeilBrown3cb03002011-10-11 16:45:26 +11002212static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
2214 int err = 0;
2215 struct block_device *bdev;
2216 char b[BDEVNAME_SIZE];
2217
Tejun Heod4d77622010-11-13 11:55:18 +01002218 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002219 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002221 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 return PTR_ERR(bdev);
2223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 rdev->bdev = bdev;
2225 return err;
2226}
2227
NeilBrown3cb03002011-10-11 16:45:26 +11002228static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
2230 struct block_device *bdev = rdev->bdev;
2231 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002232 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
2235void md_autodetect_dev(dev_t dev);
2236
NeilBrownf72ffdd2014-09-30 14:23:59 +10002237static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
2239 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002240
NeilBrown9d487392016-11-02 14:16:49 +11002241 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002242 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002244 if (test_bit(AutoDetected, &rdev->flags))
2245 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246#endif
2247 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002248 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002251void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
2253 unbind_rdev_from_array(rdev);
2254 export_rdev(rdev);
2255}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002256EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
NeilBrownfd01b882011-10-11 16:47:53 +11002258static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
NeilBrown0638bb02014-09-25 17:43:47 +10002260 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
NeilBrown0638bb02014-09-25 17:43:47 +10002262 while (!list_empty(&mddev->disks)) {
2263 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2264 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002265 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 mddev->raid_disks = 0;
2268 mddev->major_version = 0;
2269}
2270
NeilBrown6497709b2017-03-15 14:05:14 +11002271static bool set_in_sync(struct mddev *mddev)
2272{
2273 WARN_ON_ONCE(!spin_is_locked(&mddev->lock));
NeilBrown4ad23a972017-03-15 14:05:14 +11002274 if (!mddev->in_sync) {
2275 mddev->sync_checkers++;
2276 spin_unlock(&mddev->lock);
2277 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2278 spin_lock(&mddev->lock);
2279 if (!mddev->in_sync &&
2280 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002281 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002282 /*
2283 * Ensure ->in_sync is visible before we clear
2284 * ->sync_checkers.
2285 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002286 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002287 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2288 sysfs_notify_dirent_safe(mddev->sysfs_state);
2289 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002290 if (--mddev->sync_checkers == 0)
2291 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002292 }
2293 if (mddev->safemode == 1)
2294 mddev->safemode = 0;
2295 return mddev->in_sync;
2296}
2297
NeilBrownf72ffdd2014-09-30 14:23:59 +10002298static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
NeilBrown42543762006-06-26 00:27:57 -07002300 /* Update each superblock (in-memory image), but
2301 * if we are allowed to, skip spares which already
2302 * have the right event counter, or have one earlier
2303 * (which would mean they aren't being marked as dirty
2304 * with the rest of the array)
2305 */
NeilBrown3cb03002011-10-11 16:45:26 +11002306 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002307 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002308 if (rdev->sb_events == mddev->events ||
2309 (nospares &&
2310 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002311 rdev->sb_events+1 == mddev->events)) {
2312 /* Don't update this superblock */
2313 rdev->sb_loaded = 2;
2314 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002315 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002316 rdev->sb_loaded = 1;
2317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 }
2319}
2320
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002321static bool does_sb_need_changing(struct mddev *mddev)
2322{
2323 struct md_rdev *rdev;
2324 struct mdp_superblock_1 *sb;
2325 int role;
2326
2327 /* Find a good rdev */
2328 rdev_for_each(rdev, mddev)
2329 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2330 break;
2331
2332 /* No good device found. */
2333 if (!rdev)
2334 return false;
2335
2336 sb = page_address(rdev->sb_page);
2337 /* Check if a device has become faulty or a spare become active */
2338 rdev_for_each(rdev, mddev) {
2339 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2340 /* Device activated? */
2341 if (role == 0xffff && rdev->raid_disk >=0 &&
2342 !test_bit(Faulty, &rdev->flags))
2343 return true;
2344 /* Device turned faulty? */
2345 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2346 return true;
2347 }
2348
2349 /* Check if any mddev parameters have changed */
2350 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2351 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002352 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002353 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2354 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2355 return true;
2356
2357 return false;
2358}
2359
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002360void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
NeilBrown3cb03002011-10-11 16:45:26 +11002362 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002363 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002364 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002365 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002366 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
NeilBrownd87f0642013-04-24 11:42:40 +10002368 if (mddev->ro) {
2369 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002370 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002371 return;
2372 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002373
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002374repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002375 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002376 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002377 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002378 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002379 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002380 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002381 /* Has someone else has updated the sb */
2382 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002383 if (ret == 0)
2384 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002385 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2386 BIT(MD_SB_CHANGE_DEVS) |
2387 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002388 return;
2389 }
2390 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002391
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002392 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002393 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002394 if (rdev->raid_disk >= 0 &&
2395 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002396 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002397 !test_bit(In_sync, &rdev->flags) &&
2398 mddev->curr_resync_completed > rdev->recovery_offset)
2399 rdev->recovery_offset = mddev->curr_resync_completed;
2400
NeilBrownf72ffdd2014-09-30 14:23:59 +10002401 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002402 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002403 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2404 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002405 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002406 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002407 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002408 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002409 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002410 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002411 md_error(mddev, rdev);
2412 }
2413 clear_bit(Blocked, &rdev->flags);
2414 clear_bit(BlockedBadBlocks, &rdev->flags);
2415 wake_up(&rdev->blocked_wait);
2416 }
2417 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002418 wake_up(&mddev->sb_wait);
2419 return;
2420 }
2421
NeilBrown85572d72014-12-15 12:56:56 +11002422 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002423
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002424 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002425
Shaohua Li29530792016-12-08 15:48:19 -08002426 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002427 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002428 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002429 /* just a clean<-> dirty transition, possibly leave spares alone,
2430 * though if events isn't the right even/odd, we will have to do
2431 * spares after all
2432 */
2433 nospares = 1;
2434 if (force_change)
2435 nospares = 0;
2436 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002437 /* If the array is degraded, then skipping spares is both
2438 * dangerous and fairly pointless.
2439 * Dangerous because a device that was removed from the array
2440 * might have a event_count that still looks up-to-date,
2441 * so it can be re-added without a resync.
2442 * Pointless because if there are any spares to skip,
2443 * then a recovery will happen and soon that array won't
2444 * be degraded any more and the spare can go back to sleep then.
2445 */
NeilBrown850b2b422006-10-03 01:15:46 -07002446 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002447
NeilBrown06d91a52005-06-21 17:17:12 -07002448 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002449
2450 /* If this is just a dirty<->clean transition, and the array is clean
2451 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002452 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002453 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002454 && mddev->can_decrease_events
2455 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002456 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002457 mddev->can_decrease_events = 0;
2458 } else {
NeilBrown42543762006-06-26 00:27:57 -07002459 /* otherwise we have to go forward and ... */
2460 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002461 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
NeilBrown403df472014-09-30 15:52:29 +10002464 /*
2465 * This 64-bit counter should never wrap.
2466 * Either we are in around ~1 trillion A.C., assuming
2467 * 1 reboot per second, or we have a bug...
2468 */
2469 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002470
NeilBrowndafb20f2012-03-19 12:46:39 +11002471 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002472 if (rdev->badblocks.changed)
2473 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002474 if (test_bit(Faulty, &rdev->flags))
2475 set_bit(FaultRecorded, &rdev->flags);
2476 }
NeilBrown2699b672011-07-28 11:31:47 +10002477
NeilBrowne6910632008-02-06 01:39:51 -08002478 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002479 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
NeilBrown36a4e1f2011-10-07 14:23:17 +11002481 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2482 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Shaohua Li504634f2016-11-18 09:44:08 -08002484 if (mddev->queue)
2485 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002486rewrite:
NeilBrown4ad13662007-07-17 04:06:13 -07002487 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002488 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002490
NeilBrown42543762006-06-26 00:27:57 -07002491 if (rdev->sb_loaded != 1)
2492 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
NeilBrownf4667222013-12-09 12:04:56 +11002494 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002495 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002496 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002497 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002498 pr_debug("md: (write) %s's sb offset: %llu\n",
2499 bdevname(rdev->bdev, b),
2500 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002501 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002502 if (rdev->badblocks.size) {
2503 md_super_write(mddev, rdev,
2504 rdev->badblocks.sector,
2505 rdev->badblocks.size << 9,
2506 rdev->bb_page);
2507 rdev->badblocks.size = 0;
2508 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002509
NeilBrownf4667222013-12-09 12:04:56 +11002510 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002511 pr_debug("md: %s (skipping faulty)\n",
2512 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002513
NeilBrown7bfa19f2005-06-21 17:17:28 -07002514 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 /* only need to write one superblock... */
2516 break;
2517 }
NeilBrown46533ff2016-11-18 16:16:11 +11002518 if (md_super_wait(mddev) < 0)
2519 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002520 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002521
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002522 if (mddev_is_clustered(mddev) && ret == 0)
2523 md_cluster_ops->metadata_update_finish(mddev);
2524
NeilBrown850b2b422006-10-03 01:15:46 -07002525 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002526 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2527 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002528 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002529 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002530 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002531 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2532 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002533
NeilBrowndafb20f2012-03-19 12:46:39 +11002534 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002535 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2536 clear_bit(Blocked, &rdev->flags);
2537
2538 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002539 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002540 clear_bit(BlockedBadBlocks, &rdev->flags);
2541 wake_up(&rdev->blocked_wait);
2542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002544EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002546static int add_bound_rdev(struct md_rdev *rdev)
2547{
2548 struct mddev *mddev = rdev->mddev;
2549 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002550 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002551
Shaohua Li87d4d912016-01-06 14:37:14 -08002552 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002553 /* If there is hot_add_disk but no hot_remove_disk
2554 * then added disks for geometry changes,
2555 * and should be added immediately.
2556 */
2557 super_types[mddev->major_version].
2558 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002559 if (add_journal)
2560 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002561 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002562 if (add_journal)
2563 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002564 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002565 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002566 return err;
2567 }
2568 }
2569 sysfs_notify_dirent_safe(rdev->sysfs_state);
2570
Shaohua Li29530792016-12-08 15:48:19 -08002571 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002572 if (mddev->degraded)
2573 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2574 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2575 md_new_event(mddev);
2576 md_wakeup_thread(mddev->thread);
2577 return 0;
2578}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Andre Noll7f6ce762008-03-23 18:34:54 +01002580/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002581 * We want to accept with case. For this we use cmd_match.
2582 */
2583static int cmd_match(const char *cmd, const char *str)
2584{
2585 /* See if cmd, written into a sysfs file, matches
2586 * str. They must either be the same, or cmd can
2587 * have a trailing newline
2588 */
2589 while (*cmd && *str && *cmd == *str) {
2590 cmd++;
2591 str++;
2592 }
2593 if (*cmd == '\n')
2594 cmd++;
2595 if (*str || *cmd)
2596 return 0;
2597 return 1;
2598}
2599
NeilBrown86e6ffd2005-11-08 21:39:24 -08002600struct rdev_sysfs_entry {
2601 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002602 ssize_t (*show)(struct md_rdev *, char *);
2603 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002604};
2605
2606static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002607state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002608{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002609 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002610 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002611 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002612
NeilBrown758bfc82014-12-15 12:56:59 +11002613 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002614 (!test_bit(ExternalBbl, &flags) &&
2615 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002616 len += sprintf(page+len, "faulty%s", sep);
2617 if (test_bit(In_sync, &flags))
2618 len += sprintf(page+len, "in_sync%s", sep);
2619 if (test_bit(Journal, &flags))
2620 len += sprintf(page+len, "journal%s", sep);
2621 if (test_bit(WriteMostly, &flags))
2622 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002623 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002624 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002625 && !test_bit(Faulty, &flags)))
2626 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002627 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002628 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002629 !test_bit(In_sync, &flags))
2630 len += sprintf(page+len, "spare%s", sep);
2631 if (test_bit(WriteErrorSeen, &flags))
2632 len += sprintf(page+len, "write_error%s", sep);
2633 if (test_bit(WantReplacement, &flags))
2634 len += sprintf(page+len, "want_replacement%s", sep);
2635 if (test_bit(Replacement, &flags))
2636 len += sprintf(page+len, "replacement%s", sep);
2637 if (test_bit(ExternalBbl, &flags))
2638 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002639 if (test_bit(FailFast, &flags))
2640 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002641
2642 if (len)
2643 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002644
NeilBrown86e6ffd2005-11-08 21:39:24 -08002645 return len+sprintf(page+len, "\n");
2646}
2647
NeilBrown45dc2de2006-06-26 00:27:58 -07002648static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002649state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002650{
2651 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002652 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002653 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002654 * writemostly - sets write_mostly
2655 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002656 * blocked - sets the Blocked flags
2657 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002658 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002659 * -insync - clear Insync for a device with a slot assigned,
2660 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002661 * write_error - sets WriteErrorSeen
2662 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002663 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002664 */
2665 int err = -EINVAL;
2666 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2667 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002668 if (test_bit(Faulty, &rdev->flags))
2669 err = 0;
2670 else
2671 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002672 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002673 if (rdev->mddev->pers) {
2674 clear_bit(Blocked, &rdev->flags);
2675 remove_and_add_spares(rdev->mddev, rdev);
2676 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002677 if (rdev->raid_disk >= 0)
2678 err = -EBUSY;
2679 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002680 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002681 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002682 if (mddev_is_clustered(mddev))
2683 err = md_cluster_ops->remove_disk(mddev, rdev);
2684
2685 if (err == 0) {
2686 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002687 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002688 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002689 md_wakeup_thread(mddev->thread);
2690 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002691 md_new_event(mddev);
2692 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002693 }
NeilBrownf6556752006-06-26 00:28:01 -07002694 } else if (cmd_match(buf, "writemostly")) {
2695 set_bit(WriteMostly, &rdev->flags);
2696 err = 0;
2697 } else if (cmd_match(buf, "-writemostly")) {
2698 clear_bit(WriteMostly, &rdev->flags);
2699 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002700 } else if (cmd_match(buf, "blocked")) {
2701 set_bit(Blocked, &rdev->flags);
2702 err = 0;
2703 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002704 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002705 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002706 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002707 /* metadata handler doesn't understand badblocks,
2708 * so we need to fail the device
2709 */
2710 md_error(rdev->mddev, rdev);
2711 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002712 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002713 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002714 wake_up(&rdev->blocked_wait);
2715 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2716 md_wakeup_thread(rdev->mddev->thread);
2717
2718 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002719 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2720 set_bit(In_sync, &rdev->flags);
2721 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11002722 } else if (cmd_match(buf, "failfast")) {
2723 set_bit(FailFast, &rdev->flags);
2724 err = 0;
2725 } else if (cmd_match(buf, "-failfast")) {
2726 clear_bit(FailFast, &rdev->flags);
2727 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002728 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2729 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002730 if (rdev->mddev->pers == NULL) {
2731 clear_bit(In_sync, &rdev->flags);
2732 rdev->saved_raid_disk = rdev->raid_disk;
2733 rdev->raid_disk = -1;
2734 err = 0;
2735 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002736 } else if (cmd_match(buf, "write_error")) {
2737 set_bit(WriteErrorSeen, &rdev->flags);
2738 err = 0;
2739 } else if (cmd_match(buf, "-write_error")) {
2740 clear_bit(WriteErrorSeen, &rdev->flags);
2741 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002742 } else if (cmd_match(buf, "want_replacement")) {
2743 /* Any non-spare device that is not a replacement can
2744 * become want_replacement at any time, but we then need to
2745 * check if recovery is needed.
2746 */
2747 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002748 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002749 !test_bit(Replacement, &rdev->flags))
2750 set_bit(WantReplacement, &rdev->flags);
2751 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2752 md_wakeup_thread(rdev->mddev->thread);
2753 err = 0;
2754 } else if (cmd_match(buf, "-want_replacement")) {
2755 /* Clearing 'want_replacement' is always allowed.
2756 * Once replacements starts it is too late though.
2757 */
2758 err = 0;
2759 clear_bit(WantReplacement, &rdev->flags);
2760 } else if (cmd_match(buf, "replacement")) {
2761 /* Can only set a device as a replacement when array has not
2762 * yet been started. Once running, replacement is automatic
2763 * from spares, or by assigning 'slot'.
2764 */
2765 if (rdev->mddev->pers)
2766 err = -EBUSY;
2767 else {
2768 set_bit(Replacement, &rdev->flags);
2769 err = 0;
2770 }
2771 } else if (cmd_match(buf, "-replacement")) {
2772 /* Similarly, can only clear Replacement before start */
2773 if (rdev->mddev->pers)
2774 err = -EBUSY;
2775 else {
2776 clear_bit(Replacement, &rdev->flags);
2777 err = 0;
2778 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002779 } else if (cmd_match(buf, "re-add")) {
2780 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002781 /* clear_bit is performed _after_ all the devices
2782 * have their local Faulty bit cleared. If any writes
2783 * happen in the meantime in the local node, they
2784 * will land in the local bitmap, which will be synced
2785 * by this node eventually
2786 */
2787 if (!mddev_is_clustered(rdev->mddev) ||
2788 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2789 clear_bit(Faulty, &rdev->flags);
2790 err = add_bound_rdev(rdev);
2791 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002792 } else
2793 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002794 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2795 set_bit(ExternalBbl, &rdev->flags);
2796 rdev->badblocks.shift = 0;
2797 err = 0;
2798 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2799 clear_bit(ExternalBbl, &rdev->flags);
2800 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002801 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002802 if (!err)
2803 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002804 return err ? err : len;
2805}
NeilBrown80ca3a42006-07-10 04:44:18 -07002806static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002807__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002808
2809static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002810errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002811{
2812 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2813}
2814
2815static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002816errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002817{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002818 unsigned int n;
2819 int rv;
2820
2821 rv = kstrtouint(buf, 10, &n);
2822 if (rv < 0)
2823 return rv;
2824 atomic_set(&rdev->corrected_errors, n);
2825 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002826}
2827static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002828__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002829
NeilBrown014236d2006-01-06 00:20:55 -08002830static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002831slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002832{
Shaohua Lif2076e72015-10-08 21:54:12 -07002833 if (test_bit(Journal, &rdev->flags))
2834 return sprintf(page, "journal\n");
2835 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002836 return sprintf(page, "none\n");
2837 else
2838 return sprintf(page, "%d\n", rdev->raid_disk);
2839}
2840
2841static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002842slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002843{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002844 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002845 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002846
Shaohua Lif2076e72015-10-08 21:54:12 -07002847 if (test_bit(Journal, &rdev->flags))
2848 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002849 if (strncmp(buf, "none", 4)==0)
2850 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002851 else {
2852 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2853 if (err < 0)
2854 return err;
2855 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002856 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002857 /* Setting 'slot' on an active array requires also
2858 * updating the 'rd%d' link, and communicating
2859 * with the personality with ->hot_*_disk.
2860 * For now we only support removing
2861 * failed/spare devices. This normally happens automatically,
2862 * but not when the metadata is externally managed.
2863 */
NeilBrownc303da62008-02-06 01:39:51 -08002864 if (rdev->raid_disk == -1)
2865 return -EEXIST;
2866 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002867 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002868 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002869 clear_bit(Blocked, &rdev->flags);
2870 remove_and_add_spares(rdev->mddev, rdev);
2871 if (rdev->raid_disk >= 0)
2872 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002873 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2874 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002875 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002876 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002877 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002878 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002879 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002880
2881 if (rdev->raid_disk != -1)
2882 return -EBUSY;
2883
NeilBrownc6751b22011-02-02 11:57:13 +11002884 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2885 return -EBUSY;
2886
Neil Brown6c2fce22008-06-28 08:31:31 +10002887 if (rdev->mddev->pers->hot_add_disk == NULL)
2888 return -EINVAL;
2889
NeilBrownba1b41b2011-01-14 09:14:34 +11002890 if (slot >= rdev->mddev->raid_disks &&
2891 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2892 return -ENOSPC;
2893
Neil Brown6c2fce22008-06-28 08:31:31 +10002894 rdev->raid_disk = slot;
2895 if (test_bit(In_sync, &rdev->flags))
2896 rdev->saved_raid_disk = slot;
2897 else
2898 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002899 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002900 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002901 err = rdev->mddev->pers->
2902 hot_add_disk(rdev->mddev, rdev);
2903 if (err) {
2904 rdev->raid_disk = -1;
2905 return err;
2906 } else
2907 sysfs_notify_dirent_safe(rdev->sysfs_state);
2908 if (sysfs_link_rdev(rdev->mddev, rdev))
2909 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002910 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002911 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002912 if (slot >= rdev->mddev->raid_disks &&
2913 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002914 return -ENOSPC;
2915 rdev->raid_disk = slot;
2916 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002917 clear_bit(Faulty, &rdev->flags);
2918 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002919 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002920 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002921 }
NeilBrown014236d2006-01-06 00:20:55 -08002922 return len;
2923}
2924
NeilBrown014236d2006-01-06 00:20:55 -08002925static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002926__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002927
NeilBrown93c8cad2006-01-06 00:20:56 -08002928static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002929offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002930{
Andrew Morton6961ece2006-01-06 00:20:59 -08002931 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002932}
2933
2934static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002935offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002936{
NeilBrownc6563a82012-05-21 09:27:00 +10002937 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002938 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002939 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002940 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002941 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002942 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002943 /* Must set offset before size, so overlap checks
2944 * can be sane */
2945 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002946 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002947 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002948 return len;
2949}
2950
2951static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002952__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002953
NeilBrownc6563a82012-05-21 09:27:00 +10002954static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2955{
2956 return sprintf(page, "%llu\n",
2957 (unsigned long long)rdev->new_data_offset);
2958}
2959
2960static ssize_t new_offset_store(struct md_rdev *rdev,
2961 const char *buf, size_t len)
2962{
2963 unsigned long long new_offset;
2964 struct mddev *mddev = rdev->mddev;
2965
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002966 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002967 return -EINVAL;
2968
NeilBrownf851b602014-12-11 10:02:10 +11002969 if (mddev->sync_thread ||
2970 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002971 return -EBUSY;
2972 if (new_offset == rdev->data_offset)
2973 /* reset is always permitted */
2974 ;
2975 else if (new_offset > rdev->data_offset) {
2976 /* must not push array size beyond rdev_sectors */
2977 if (new_offset - rdev->data_offset
2978 + mddev->dev_sectors > rdev->sectors)
2979 return -E2BIG;
2980 }
2981 /* Metadata worries about other space details. */
2982
2983 /* decreasing the offset is inconsistent with a backwards
2984 * reshape.
2985 */
2986 if (new_offset < rdev->data_offset &&
2987 mddev->reshape_backwards)
2988 return -EINVAL;
2989 /* Increasing offset is inconsistent with forwards
2990 * reshape. reshape_direction should be set to
2991 * 'backwards' first.
2992 */
2993 if (new_offset > rdev->data_offset &&
2994 !mddev->reshape_backwards)
2995 return -EINVAL;
2996
2997 if (mddev->pers && mddev->persistent &&
2998 !super_types[mddev->major_version]
2999 .allow_new_offset(rdev, new_offset))
3000 return -E2BIG;
3001 rdev->new_data_offset = new_offset;
3002 if (new_offset > rdev->data_offset)
3003 mddev->reshape_backwards = 1;
3004 else if (new_offset < rdev->data_offset)
3005 mddev->reshape_backwards = 0;
3006
3007 return len;
3008}
3009static struct rdev_sysfs_entry rdev_new_offset =
3010__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3011
NeilBrown83303b62006-01-06 00:21:06 -08003012static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003013rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003014{
Andre Nolldd8ac332009-03-31 14:33:13 +11003015 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003016}
3017
NeilBrownc5d79ad2008-02-06 01:39:54 -08003018static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3019{
3020 /* check if two start/length pairs overlap */
3021 if (s1+l1 <= s2)
3022 return 0;
3023 if (s2+l2 <= s1)
3024 return 0;
3025 return 1;
3026}
3027
Dan Williamsb522adc2009-03-31 15:00:31 +11003028static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3029{
3030 unsigned long long blocks;
3031 sector_t new;
3032
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003033 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003034 return -EINVAL;
3035
3036 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3037 return -EINVAL; /* sector conversion overflow */
3038
3039 new = blocks * 2;
3040 if (new != blocks * 2)
3041 return -EINVAL; /* unsigned long long to sector_t overflow */
3042
3043 *sectors = new;
3044 return 0;
3045}
3046
NeilBrown83303b62006-01-06 00:21:06 -08003047static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003048rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003049{
NeilBrownfd01b882011-10-11 16:47:53 +11003050 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003051 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003052 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003053
Shaohua Lif2076e72015-10-08 21:54:12 -07003054 if (test_bit(Journal, &rdev->flags))
3055 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003056 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003057 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003058 if (rdev->data_offset != rdev->new_data_offset)
3059 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003060 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003061 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003062 sectors = super_types[my_mddev->major_version].
3063 rdev_size_change(rdev, sectors);
3064 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003065 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003066 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003067 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003068 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003069 if (!my_mddev->pers->resize)
3070 /* Cannot change size for RAID0 or Linear etc */
3071 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003072 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003073 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003074 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003075
Andre Nolldd8ac332009-03-31 14:33:13 +11003076 rdev->sectors = sectors;
3077 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003078 /* Need to check that all other rdevs with the same
3079 * ->bdev do not overlap. 'rcu' is sufficient to walk
3080 * the rdev lists safely.
3081 * This check does not provide a hard guarantee, it
3082 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003083 */
NeilBrownfd01b882011-10-11 16:47:53 +11003084 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003085 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003086 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003087
NeilBrown8b1afc32014-09-29 15:33:20 +10003088 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003089 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003090 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003091
NeilBrowndafb20f2012-03-19 12:46:39 +11003092 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003093 if (rdev->bdev == rdev2->bdev &&
3094 rdev != rdev2 &&
3095 overlaps(rdev->data_offset, rdev->sectors,
3096 rdev2->data_offset,
3097 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003098 overlap = 1;
3099 break;
3100 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003101 if (overlap) {
3102 mddev_put(mddev);
3103 break;
3104 }
3105 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003106 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003107 if (overlap) {
3108 /* Someone else could have slipped in a size
3109 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003110 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003111 * safe, and trust userspace not to race with
3112 * itself
3113 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003114 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003115 return -EBUSY;
3116 }
3117 }
NeilBrown83303b62006-01-06 00:21:06 -08003118 return len;
3119}
3120
3121static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003122__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003123
NeilBrown3cb03002011-10-11 16:45:26 +11003124static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003125{
3126 unsigned long long recovery_start = rdev->recovery_offset;
3127
3128 if (test_bit(In_sync, &rdev->flags) ||
3129 recovery_start == MaxSector)
3130 return sprintf(page, "none\n");
3131
3132 return sprintf(page, "%llu\n", recovery_start);
3133}
3134
NeilBrown3cb03002011-10-11 16:45:26 +11003135static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003136{
3137 unsigned long long recovery_start;
3138
3139 if (cmd_match(buf, "none"))
3140 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003141 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003142 return -EINVAL;
3143
3144 if (rdev->mddev->pers &&
3145 rdev->raid_disk >= 0)
3146 return -EBUSY;
3147
3148 rdev->recovery_offset = recovery_start;
3149 if (recovery_start == MaxSector)
3150 set_bit(In_sync, &rdev->flags);
3151 else
3152 clear_bit(In_sync, &rdev->flags);
3153 return len;
3154}
3155
3156static struct rdev_sysfs_entry rdev_recovery_start =
3157__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3158
Vishal Vermafc974ee2015-12-24 19:20:34 -07003159/* sysfs access to bad-blocks list.
3160 * We present two files.
3161 * 'bad-blocks' lists sector numbers and lengths of ranges that
3162 * are recorded as bad. The list is truncated to fit within
3163 * the one-page limit of sysfs.
3164 * Writing "sector length" to this file adds an acknowledged
3165 * bad block list.
3166 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3167 * been acknowledged. Writing to this file adds bad blocks
3168 * without acknowledging them. This is largely for testing.
3169 */
NeilBrown3cb03002011-10-11 16:45:26 +11003170static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003171{
3172 return badblocks_show(&rdev->badblocks, page, 0);
3173}
NeilBrown3cb03002011-10-11 16:45:26 +11003174static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003175{
NeilBrownde393cd2011-07-28 11:31:48 +10003176 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3177 /* Maybe that ack was all we needed */
3178 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3179 wake_up(&rdev->blocked_wait);
3180 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003181}
3182static struct rdev_sysfs_entry rdev_bad_blocks =
3183__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3184
NeilBrown3cb03002011-10-11 16:45:26 +11003185static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003186{
3187 return badblocks_show(&rdev->badblocks, page, 1);
3188}
NeilBrown3cb03002011-10-11 16:45:26 +11003189static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003190{
3191 return badblocks_store(&rdev->badblocks, page, len, 1);
3192}
3193static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3194__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3195
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003196static ssize_t
3197ppl_sector_show(struct md_rdev *rdev, char *page)
3198{
3199 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3200}
3201
3202static ssize_t
3203ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3204{
3205 unsigned long long sector;
3206
3207 if (kstrtoull(buf, 10, &sector) < 0)
3208 return -EINVAL;
3209 if (sector != (sector_t)sector)
3210 return -EINVAL;
3211
3212 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3213 rdev->raid_disk >= 0)
3214 return -EBUSY;
3215
3216 if (rdev->mddev->persistent) {
3217 if (rdev->mddev->major_version == 0)
3218 return -EINVAL;
3219 if ((sector > rdev->sb_start &&
3220 sector - rdev->sb_start > S16_MAX) ||
3221 (sector < rdev->sb_start &&
3222 rdev->sb_start - sector > -S16_MIN))
3223 return -EINVAL;
3224 rdev->ppl.offset = sector - rdev->sb_start;
3225 } else if (!rdev->mddev->external) {
3226 return -EBUSY;
3227 }
3228 rdev->ppl.sector = sector;
3229 return len;
3230}
3231
3232static struct rdev_sysfs_entry rdev_ppl_sector =
3233__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3234
3235static ssize_t
3236ppl_size_show(struct md_rdev *rdev, char *page)
3237{
3238 return sprintf(page, "%u\n", rdev->ppl.size);
3239}
3240
3241static ssize_t
3242ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3243{
3244 unsigned int size;
3245
3246 if (kstrtouint(buf, 10, &size) < 0)
3247 return -EINVAL;
3248
3249 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3250 rdev->raid_disk >= 0)
3251 return -EBUSY;
3252
3253 if (rdev->mddev->persistent) {
3254 if (rdev->mddev->major_version == 0)
3255 return -EINVAL;
3256 if (size > U16_MAX)
3257 return -EINVAL;
3258 } else if (!rdev->mddev->external) {
3259 return -EBUSY;
3260 }
3261 rdev->ppl.size = size;
3262 return len;
3263}
3264
3265static struct rdev_sysfs_entry rdev_ppl_size =
3266__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3267
NeilBrown86e6ffd2005-11-08 21:39:24 -08003268static struct attribute *rdev_default_attrs[] = {
3269 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003270 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003271 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003272 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003273 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003274 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003275 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003276 &rdev_bad_blocks.attr,
3277 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003278 &rdev_ppl_sector.attr,
3279 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003280 NULL,
3281};
3282static ssize_t
3283rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3284{
3285 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003286 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003287
3288 if (!entry->show)
3289 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003290 if (!rdev->mddev)
3291 return -EBUSY;
3292 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003293}
3294
3295static ssize_t
3296rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3297 const char *page, size_t length)
3298{
3299 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003300 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003301 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003302 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003303
3304 if (!entry->store)
3305 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003306 if (!capable(CAP_SYS_ADMIN))
3307 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003308 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003309 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003310 if (rdev->mddev == NULL)
3311 rv = -EBUSY;
3312 else
3313 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003314 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003315 }
3316 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003317}
3318
3319static void rdev_free(struct kobject *ko)
3320{
NeilBrown3cb03002011-10-11 16:45:26 +11003321 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003322 kfree(rdev);
3323}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003324static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003325 .show = rdev_attr_show,
3326 .store = rdev_attr_store,
3327};
3328static struct kobj_type rdev_ktype = {
3329 .release = rdev_free,
3330 .sysfs_ops = &rdev_sysfs_ops,
3331 .default_attrs = rdev_default_attrs,
3332};
3333
NeilBrown3cb03002011-10-11 16:45:26 +11003334int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003335{
3336 rdev->desc_nr = -1;
3337 rdev->saved_raid_disk = -1;
3338 rdev->raid_disk = -1;
3339 rdev->flags = 0;
3340 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003341 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003342 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003343 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003344 rdev->sb_loaded = 0;
3345 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003346 atomic_set(&rdev->nr_pending, 0);
3347 atomic_set(&rdev->read_errors, 0);
3348 atomic_set(&rdev->corrected_errors, 0);
3349
3350 INIT_LIST_HEAD(&rdev->same_set);
3351 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003352
3353 /* Add space to store bad block list.
3354 * This reserves the space even on arrays where it cannot
3355 * be used - I wonder if that matters
3356 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003357 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003358}
3359EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360/*
3361 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3362 *
3363 * mark the device faulty if:
3364 *
3365 * - the device is nonexistent (zero size)
3366 * - the device has no valid superblock
3367 *
3368 * a faulty rdev _never_ has rdev->sb set.
3369 */
NeilBrown3cb03002011-10-11 16:45:26 +11003370static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
3372 char b[BDEVNAME_SIZE];
3373 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003374 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 sector_t size;
3376
NeilBrown9ffae0c2006-01-06 00:20:32 -08003377 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003378 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
NeilBrown2230dfe2011-07-28 11:31:46 +10003381 err = md_rdev_init(rdev);
3382 if (err)
3383 goto abort_free;
3384 err = alloc_disk_sb(rdev);
3385 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 goto abort_free;
3387
NeilBrownc5d79ad2008-02-06 01:39:54 -08003388 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (err)
3390 goto abort_free;
3391
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003392 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003393
Mike Snitzer77304d22010-11-08 14:39:12 +01003394 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003396 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 bdevname(rdev->bdev,b));
3398 err = -EINVAL;
3399 goto abort_free;
3400 }
3401
3402 if (super_format >= 0) {
3403 err = super_types[super_format].
3404 load_super(rdev, NULL, super_minor);
3405 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003406 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003407 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003408 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 goto abort_free;
3410 }
3411 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003412 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 bdevname(rdev->bdev,b));
3414 goto abort_free;
3415 }
3416 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 return rdev;
3419
3420abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003421 if (rdev->bdev)
3422 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003423 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 kfree(rdev);
3425 return ERR_PTR(err);
3426}
3427
3428/*
3429 * Check a full RAID array for plausibility
3430 */
3431
NeilBrownf72ffdd2014-09-30 14:23:59 +10003432static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
3434 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003435 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 char b[BDEVNAME_SIZE];
3437
3438 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003439 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 switch (super_types[mddev->major_version].
3441 load_super(rdev, freshest, mddev->minor_version)) {
3442 case 1:
3443 freshest = rdev;
3444 break;
3445 case 0:
3446 break;
3447 default:
NeilBrown9d487392016-11-02 14:16:49 +11003448 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003450 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 }
3452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 super_types[mddev->major_version].
3454 validate_super(mddev, freshest);
3455
3456 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003457 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003458 if (mddev->max_disks &&
3459 (rdev->desc_nr >= mddev->max_disks ||
3460 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003461 pr_warn("md: %s: %s: only %d devices permitted\n",
3462 mdname(mddev), bdevname(rdev->bdev, b),
3463 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003464 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003465 continue;
3466 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003467 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 if (super_types[mddev->major_version].
3469 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003470 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003472 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 continue;
3474 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 if (mddev->level == LEVEL_MULTIPATH) {
3477 rdev->desc_nr = i++;
3478 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003479 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003480 } else if (rdev->raid_disk >=
3481 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3482 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003483 rdev->raid_disk = -1;
3484 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 }
3486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487}
3488
NeilBrown72e02072009-12-14 12:49:55 +11003489/* Read a fixed-point number.
3490 * Numbers in sysfs attributes should be in "standard" units where
3491 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003492 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003493 * milliseconds or jiffies.
3494 * This function takes a decimal number with a possible fractional
3495 * component, and produces an integer which is the result of
3496 * multiplying that number by 10^'scale'.
3497 * all without any floating-point arithmetic.
3498 */
3499int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3500{
3501 unsigned long result = 0;
3502 long decimals = -1;
3503 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3504 if (*cp == '.')
3505 decimals = 0;
3506 else if (decimals < scale) {
3507 unsigned int value;
3508 value = *cp - '0';
3509 result = result * 10 + value;
3510 if (decimals >= 0)
3511 decimals++;
3512 }
3513 cp++;
3514 }
3515 if (*cp == '\n')
3516 cp++;
3517 if (*cp)
3518 return -EINVAL;
3519 if (decimals < 0)
3520 decimals = 0;
3521 while (decimals < scale) {
3522 result *= 10;
3523 decimals ++;
3524 }
3525 *res = result;
3526 return 0;
3527}
3528
NeilBrowneae17012005-11-08 21:39:23 -08003529static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003530safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003531{
3532 int msec = (mddev->safemode_delay*1000)/HZ;
3533 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3534}
3535static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003536safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003537{
NeilBrown16f17b32006-06-26 00:27:37 -07003538 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003539
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003540 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003541 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003542 return -EINVAL;
3543 }
3544
NeilBrown72e02072009-12-14 12:49:55 +11003545 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003546 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003547 if (msec == 0)
3548 mddev->safemode_delay = 0;
3549 else {
NeilBrown19052c02008-08-05 15:54:13 +10003550 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003551 unsigned long new_delay = (msec*HZ)/1000;
3552
3553 if (new_delay == 0)
3554 new_delay = 1;
3555 mddev->safemode_delay = new_delay;
3556 if (new_delay < old_delay || old_delay == 0)
3557 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003558 }
3559 return len;
3560}
3561static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003562__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003563
3564static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003565level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003566{
NeilBrown36d091f2014-12-15 12:56:58 +11003567 struct md_personality *p;
3568 int ret;
3569 spin_lock(&mddev->lock);
3570 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003571 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003572 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003573 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003574 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003575 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003576 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003577 else
NeilBrown36d091f2014-12-15 12:56:58 +11003578 ret = 0;
3579 spin_unlock(&mddev->lock);
3580 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003581}
3582
NeilBrownd9d166c2006-01-06 00:20:51 -08003583static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003584level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003585{
Dan Williamsf2859af2010-05-02 10:04:16 -07003586 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003587 ssize_t rv;
3588 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003589 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003590 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003591 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003592 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003593
NeilBrown67918752014-12-15 12:57:01 +11003594 if (slen == 0 || slen >= sizeof(clevel))
3595 return -EINVAL;
3596
3597 rv = mddev_lock(mddev);
3598 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003599 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003600
3601 if (mddev->pers == NULL) {
3602 strncpy(mddev->clevel, buf, slen);
3603 if (mddev->clevel[slen-1] == '\n')
3604 slen--;
3605 mddev->clevel[slen] = 0;
3606 mddev->level = LEVEL_NONE;
3607 rv = len;
3608 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003609 }
NeilBrown67918752014-12-15 12:57:01 +11003610 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003611 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003612 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003613
3614 /* request to change the personality. Need to ensure:
3615 * - array is not engaged in resync/recovery/reshape
3616 * - old personality can be suspended
3617 * - new personality will access other array.
3618 */
3619
NeilBrown67918752014-12-15 12:57:01 +11003620 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003621 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003622 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003623 mddev->reshape_position != MaxSector ||
3624 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003625 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003626
NeilBrown67918752014-12-15 12:57:01 +11003627 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003628 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003629 pr_warn("md: %s: %s does not support online personality change\n",
3630 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003631 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003632 }
3633
3634 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003635 strncpy(clevel, buf, slen);
3636 if (clevel[slen-1] == '\n')
3637 slen--;
3638 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003639 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003640 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003641
Dan Williamsf2859af2010-05-02 10:04:16 -07003642 if (request_module("md-%s", clevel) != 0)
3643 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003644 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003645 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003646 if (!pers || !try_module_get(pers->owner)) {
3647 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003648 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003649 rv = -EINVAL;
3650 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003651 }
3652 spin_unlock(&pers_lock);
3653
3654 if (pers == mddev->pers) {
3655 /* Nothing to do! */
3656 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003657 rv = len;
3658 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003659 }
3660 if (!pers->takeover) {
3661 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003662 pr_warn("md: %s: %s does not support personality takeover\n",
3663 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003664 rv = -EINVAL;
3665 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003666 }
3667
NeilBrowndafb20f2012-03-19 12:46:39 +11003668 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003669 rdev->new_raid_disk = rdev->raid_disk;
3670
NeilBrown245f46c2009-03-31 14:39:39 +11003671 /* ->takeover must set new_* and/or delta_disks
3672 * if it succeeds, and may set them when it fails.
3673 */
3674 priv = pers->takeover(mddev);
3675 if (IS_ERR(priv)) {
3676 mddev->new_level = mddev->level;
3677 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003678 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003679 mddev->raid_disks -= mddev->delta_disks;
3680 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003681 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003682 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003683 pr_warn("md: %s: %s would not accept array\n",
3684 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003685 rv = PTR_ERR(priv);
3686 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003687 }
3688
3689 /* Looks like we have a winner */
3690 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003691 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003692
3693 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003694 oldpers = mddev->pers;
3695 oldpriv = mddev->private;
3696 mddev->pers = pers;
3697 mddev->private = priv;
3698 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3699 mddev->level = mddev->new_level;
3700 mddev->layout = mddev->new_layout;
3701 mddev->chunk_sectors = mddev->new_chunk_sectors;
3702 mddev->delta_disks = 0;
3703 mddev->reshape_backwards = 0;
3704 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003705 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003706
NeilBrowndb721d32014-12-15 12:56:58 +11003707 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003708 mddev->external) {
3709 /* We are converting from a no-redundancy array
3710 * to a redundancy array and metadata is managed
3711 * externally so we need to be sure that writes
3712 * won't block due to a need to transition
3713 * clean->dirty
3714 * until external management is started.
3715 */
3716 mddev->in_sync = 0;
3717 mddev->safemode_delay = 0;
3718 mddev->safemode = 0;
3719 }
3720
NeilBrowndb721d32014-12-15 12:56:58 +11003721 oldpers->free(mddev, oldpriv);
3722
3723 if (oldpers->sync_request == NULL &&
3724 pers->sync_request != NULL) {
3725 /* need to add the md_redundancy_group */
3726 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11003727 pr_warn("md: cannot register extra attributes for %s\n",
3728 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11003729 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3730 }
3731 if (oldpers->sync_request != NULL &&
3732 pers->sync_request == NULL) {
3733 /* need to remove the md_redundancy_group */
3734 if (mddev->to_remove == NULL)
3735 mddev->to_remove = &md_redundancy_group;
3736 }
3737
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003738 module_put(oldpers->owner);
3739
NeilBrowndafb20f2012-03-19 12:46:39 +11003740 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003741 if (rdev->raid_disk < 0)
3742 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003743 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003744 rdev->new_raid_disk = -1;
3745 if (rdev->new_raid_disk == rdev->raid_disk)
3746 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003747 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003748 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003749 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003750 if (rdev->raid_disk < 0)
3751 continue;
3752 if (rdev->new_raid_disk == rdev->raid_disk)
3753 continue;
3754 rdev->raid_disk = rdev->new_raid_disk;
3755 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003756 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003757 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003758 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11003759 pr_warn("md: cannot register rd%d for %s after level change\n",
3760 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003761 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003762 }
3763
NeilBrowndb721d32014-12-15 12:56:58 +11003764 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003765 /* this is now an array without redundancy, so
3766 * it must always be in_sync
3767 */
3768 mddev->in_sync = 1;
3769 del_timer_sync(&mddev->safemode_timer);
3770 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003771 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003772 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08003773 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003774 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003775 if (!mddev->thread)
3776 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003777 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003778 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003779 rv = len;
3780out_unlock:
3781 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003782 return rv;
3783}
3784
3785static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003786__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003787
NeilBrownd4dbd022006-06-26 00:27:59 -07003788static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003789layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003790{
3791 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003792 if (mddev->reshape_position != MaxSector &&
3793 mddev->layout != mddev->new_layout)
3794 return sprintf(page, "%d (%d)\n",
3795 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003796 return sprintf(page, "%d\n", mddev->layout);
3797}
3798
3799static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003800layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003801{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003802 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003803 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003804
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003805 err = kstrtouint(buf, 10, &n);
3806 if (err < 0)
3807 return err;
NeilBrown67918752014-12-15 12:57:01 +11003808 err = mddev_lock(mddev);
3809 if (err)
3810 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003811
NeilBrownb3546032009-03-31 14:56:41 +11003812 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003813 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003814 err = -EBUSY;
3815 else if (mddev->ro)
3816 err = -EROFS;
3817 else {
3818 mddev->new_layout = n;
3819 err = mddev->pers->check_reshape(mddev);
3820 if (err)
3821 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003822 }
NeilBrownb3546032009-03-31 14:56:41 +11003823 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003824 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003825 if (mddev->reshape_position == MaxSector)
3826 mddev->layout = n;
3827 }
NeilBrown67918752014-12-15 12:57:01 +11003828 mddev_unlock(mddev);
3829 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003830}
3831static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003832__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003833
NeilBrowneae17012005-11-08 21:39:23 -08003834static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003835raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003836{
NeilBrownbb636542005-11-08 21:39:45 -08003837 if (mddev->raid_disks == 0)
3838 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003839 if (mddev->reshape_position != MaxSector &&
3840 mddev->delta_disks != 0)
3841 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3842 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003843 return sprintf(page, "%d\n", mddev->raid_disks);
3844}
3845
NeilBrownfd01b882011-10-11 16:47:53 +11003846static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003847
3848static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003849raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003850{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003851 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003852 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003853
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003854 err = kstrtouint(buf, 10, &n);
3855 if (err < 0)
3856 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003857
NeilBrown67918752014-12-15 12:57:01 +11003858 err = mddev_lock(mddev);
3859 if (err)
3860 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003861 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003862 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003863 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003864 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003865 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003866
NeilBrown67918752014-12-15 12:57:01 +11003867 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003868 rdev_for_each(rdev, mddev) {
3869 if (olddisks < n &&
3870 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003871 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003872 if (olddisks > n &&
3873 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003874 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003875 }
NeilBrown67918752014-12-15 12:57:01 +11003876 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003877 mddev->delta_disks = n - olddisks;
3878 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003879 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003880 } else
NeilBrownda943b992006-01-06 00:20:54 -08003881 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003882out_unlock:
3883 mddev_unlock(mddev);
3884 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003885}
3886static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003887__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003888
NeilBrown24dd4692005-11-08 21:39:26 -08003889static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003890chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003891{
NeilBrown08a02ec2007-05-09 02:35:38 -07003892 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003893 mddev->chunk_sectors != mddev->new_chunk_sectors)
3894 return sprintf(page, "%d (%d)\n",
3895 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003896 mddev->chunk_sectors << 9);
3897 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003898}
3899
3900static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003901chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003902{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003903 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003904 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003905
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003906 err = kstrtoul(buf, 10, &n);
3907 if (err < 0)
3908 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003909
NeilBrown67918752014-12-15 12:57:01 +11003910 err = mddev_lock(mddev);
3911 if (err)
3912 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003913 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003914 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003915 err = -EBUSY;
3916 else if (mddev->ro)
3917 err = -EROFS;
3918 else {
3919 mddev->new_chunk_sectors = n >> 9;
3920 err = mddev->pers->check_reshape(mddev);
3921 if (err)
3922 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003923 }
NeilBrownb3546032009-03-31 14:56:41 +11003924 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003925 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003926 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003927 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003928 }
NeilBrown67918752014-12-15 12:57:01 +11003929 mddev_unlock(mddev);
3930 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003931}
3932static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003933__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003934
NeilBrowna94213b2006-06-26 00:28:00 -07003935static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003936resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003937{
NeilBrownd1a7c502009-03-31 15:24:32 +11003938 if (mddev->recovery_cp == MaxSector)
3939 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003940 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3941}
3942
3943static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003944resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003945{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003946 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003947 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003948
3949 if (cmd_match(buf, "none"))
3950 n = MaxSector;
3951 else {
3952 err = kstrtoull(buf, 10, &n);
3953 if (err < 0)
3954 return err;
3955 if (n != (sector_t)n)
3956 return -EINVAL;
3957 }
NeilBrowna94213b2006-06-26 00:28:00 -07003958
NeilBrown67918752014-12-15 12:57:01 +11003959 err = mddev_lock(mddev);
3960 if (err)
3961 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003962 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003963 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003964
NeilBrown67918752014-12-15 12:57:01 +11003965 if (!err) {
3966 mddev->recovery_cp = n;
3967 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08003968 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11003969 }
3970 mddev_unlock(mddev);
3971 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003972}
3973static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003974__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3975 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003976
NeilBrown9e653b62006-06-26 00:27:58 -07003977/*
3978 * The array state can be:
3979 *
3980 * clear
3981 * No devices, no size, no level
3982 * Equivalent to STOP_ARRAY ioctl
3983 * inactive
3984 * May have some settings, but array is not active
3985 * all IO results in error
3986 * When written, doesn't tear down array, but just stops it
3987 * suspended (not supported yet)
3988 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003989 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003990 * readonly
3991 * no resync can happen. no superblocks get written.
3992 * write requests fail
3993 * read-auto
3994 * like readonly, but behaves like 'clean' on a write request.
3995 *
3996 * clean - no pending writes, but otherwise active.
3997 * When written to inactive array, starts without resync
3998 * If a write request arrives then
3999 * if metadata is known, mark 'dirty' and switch to 'active'.
4000 * if not known, block and switch to write-pending
4001 * If written to an active array that has pending writes, then fails.
4002 * active
4003 * fully active: IO and resync can be happening.
4004 * When written to inactive array, starts with resync
4005 *
4006 * write-pending
4007 * clean, but writes are blocked waiting for 'active' to be written.
4008 *
4009 * active-idle
4010 * like active, but no writes have been seen for a while (100msec).
4011 *
4012 */
4013enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4014 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004015static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004016 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4017 "write-pending", "active-idle", NULL };
4018
4019static int match_word(const char *word, char **list)
4020{
4021 int n;
4022 for (n=0; list[n]; n++)
4023 if (cmd_match(word, list[n]))
4024 break;
4025 return n;
4026}
4027
4028static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004029array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004030{
4031 enum array_state st = inactive;
4032
4033 if (mddev->pers)
4034 switch(mddev->ro) {
4035 case 1:
4036 st = readonly;
4037 break;
4038 case 2:
4039 st = read_auto;
4040 break;
4041 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004042 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004043 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004044 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004045 else if (mddev->in_sync)
4046 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004047 else if (mddev->safemode)
4048 st = active_idle;
4049 else
4050 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004051 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004052 }
4053 else {
4054 if (list_empty(&mddev->disks) &&
4055 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004056 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004057 st = clear;
4058 else
4059 st = inactive;
4060 }
4061 return sprintf(page, "%s\n", array_states[st]);
4062}
4063
NeilBrownf72ffdd2014-09-30 14:23:59 +10004064static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4065static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4066static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004067static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004068
4069static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004070array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004071{
NeilBrown6497709b2017-03-15 14:05:14 +11004072 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004073 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004074
4075 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4076 /* don't take reconfig_mutex when toggling between
4077 * clean and active
4078 */
4079 spin_lock(&mddev->lock);
4080 if (st == active) {
4081 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004082 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004083 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004084 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004085 } else /* st == clean */ {
4086 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004087 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004088 err = -EBUSY;
4089 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004090 if (!err)
4091 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004092 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004093 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004094 }
4095 err = mddev_lock(mddev);
4096 if (err)
4097 return err;
4098 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004099 switch(st) {
4100 case bad_word:
4101 break;
4102 case clear:
4103 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004104 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004105 break;
4106 case inactive:
4107 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004108 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004109 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004110 else
NeilBrowne6910632008-02-06 01:39:51 -08004111 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004112 break;
4113 case suspended:
4114 break; /* not supported yet */
4115 case readonly:
4116 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004117 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004118 else {
4119 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004120 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004121 err = do_md_run(mddev);
4122 }
4123 break;
4124 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004125 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004126 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004127 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004128 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004129 err = restart_array(mddev);
4130 if (err == 0) {
4131 mddev->ro = 2;
4132 set_disk_ro(mddev->gendisk, 0);
4133 }
NeilBrown9e653b62006-06-26 00:27:58 -07004134 } else {
4135 mddev->ro = 2;
4136 err = do_md_run(mddev);
4137 }
4138 break;
4139 case clean:
4140 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004141 err = restart_array(mddev);
4142 if (err)
4143 break;
NeilBrown85572d72014-12-15 12:56:56 +11004144 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004145 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004146 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004147 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004148 } else
4149 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004150 break;
4151 case active:
4152 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004153 err = restart_array(mddev);
4154 if (err)
4155 break;
Shaohua Li29530792016-12-08 15:48:19 -08004156 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004157 wake_up(&mddev->sb_wait);
4158 err = 0;
4159 } else {
4160 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004161 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004162 err = do_md_run(mddev);
4163 }
4164 break;
4165 case write_pending:
4166 case active_idle:
4167 /* these cannot be set */
4168 break;
4169 }
NeilBrown67918752014-12-15 12:57:01 +11004170
4171 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004172 if (mddev->hold_active == UNTIL_IOCTL)
4173 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004174 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004175 }
NeilBrown67918752014-12-15 12:57:01 +11004176 mddev_unlock(mddev);
4177 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004178}
NeilBrown80ca3a42006-07-10 04:44:18 -07004179static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004180__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004181
NeilBrown6d7ff7382006-01-06 00:21:16 -08004182static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004183max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004184 return sprintf(page, "%d\n",
4185 atomic_read(&mddev->max_corr_read_errors));
4186}
4187
4188static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004189max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004190{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004191 unsigned int n;
4192 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004193
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004194 rv = kstrtouint(buf, 10, &n);
4195 if (rv < 0)
4196 return rv;
4197 atomic_set(&mddev->max_corr_read_errors, n);
4198 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004199}
4200
4201static struct md_sysfs_entry max_corr_read_errors =
4202__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4203 max_corrected_read_errors_store);
4204
4205static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004206null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004207{
4208 return -EINVAL;
4209}
4210
4211static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004212new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004213{
4214 /* buf must be %d:%d\n? giving major and minor numbers */
4215 /* The new device is added to the array.
4216 * If the array has a persistent superblock, we read the
4217 * superblock to initialise info and check validity.
4218 * Otherwise, only checking done is that in bind_rdev_to_array,
4219 * which mainly checks size.
4220 */
4221 char *e;
4222 int major = simple_strtoul(buf, &e, 10);
4223 int minor;
4224 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004225 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004226 int err;
4227
4228 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4229 return -EINVAL;
4230 minor = simple_strtoul(e+1, &e, 10);
4231 if (*e && *e != '\n')
4232 return -EINVAL;
4233 dev = MKDEV(major, minor);
4234 if (major != MAJOR(dev) ||
4235 minor != MINOR(dev))
4236 return -EOVERFLOW;
4237
NeilBrown67918752014-12-15 12:57:01 +11004238 flush_workqueue(md_misc_wq);
4239
4240 err = mddev_lock(mddev);
4241 if (err)
4242 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004243 if (mddev->persistent) {
4244 rdev = md_import_device(dev, mddev->major_version,
4245 mddev->minor_version);
4246 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004247 struct md_rdev *rdev0
4248 = list_entry(mddev->disks.next,
4249 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004250 err = super_types[mddev->major_version]
4251 .load_super(rdev, rdev0, mddev->minor_version);
4252 if (err < 0)
4253 goto out;
4254 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004255 } else if (mddev->external)
4256 rdev = md_import_device(dev, -2, -1);
4257 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004258 rdev = md_import_device(dev, -1, -1);
4259
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004260 if (IS_ERR(rdev)) {
4261 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004262 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004263 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004264 err = bind_rdev_to_array(rdev, mddev);
4265 out:
4266 if (err)
4267 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004268 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004269 return err ? err : len;
4270}
4271
4272static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004273__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004274
4275static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004276bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004277{
4278 char *end;
4279 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004280 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004281
NeilBrown67918752014-12-15 12:57:01 +11004282 err = mddev_lock(mddev);
4283 if (err)
4284 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004285 if (!mddev->bitmap)
4286 goto out;
4287 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4288 while (*buf) {
4289 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4290 if (buf == end) break;
4291 if (*end == '-') { /* range */
4292 buf = end + 1;
4293 end_chunk = simple_strtoul(buf, &end, 0);
4294 if (buf == end) break;
4295 }
4296 if (*end && !isspace(*end)) break;
4297 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004298 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004299 }
4300 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4301out:
NeilBrown67918752014-12-15 12:57:01 +11004302 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004303 return len;
4304}
4305
4306static struct md_sysfs_entry md_bitmap =
4307__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4308
4309static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004310size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004311{
Andre Noll58c0fed2009-03-31 14:33:13 +11004312 return sprintf(page, "%llu\n",
4313 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004314}
4315
NeilBrownfd01b882011-10-11 16:47:53 +11004316static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004317
4318static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004319size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004320{
4321 /* If array is inactive, we can reduce the component size, but
4322 * not increase it (except from 0).
4323 * If array is active, we can try an on-line resize
4324 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004325 sector_t sectors;
4326 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004327
Andre Noll58c0fed2009-03-31 14:33:13 +11004328 if (err < 0)
4329 return err;
NeilBrown67918752014-12-15 12:57:01 +11004330 err = mddev_lock(mddev);
4331 if (err)
4332 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004333 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004334 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004335 if (err == 0)
4336 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004337 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004338 if (mddev->dev_sectors == 0 ||
4339 mddev->dev_sectors > sectors)
4340 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004341 else
4342 err = -ENOSPC;
4343 }
NeilBrown67918752014-12-15 12:57:01 +11004344 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004345 return err ? err : len;
4346}
4347
4348static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004349__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004350
Masanari Iida83f0d772012-10-30 00:18:08 +09004351/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004352 * This is one of
4353 * 'none' for arrays with no metadata (good luck...)
4354 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004355 * or N.M for internally known formats
4356 */
4357static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004358metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004359{
4360 if (mddev->persistent)
4361 return sprintf(page, "%d.%d\n",
4362 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004363 else if (mddev->external)
4364 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004365 else
4366 return sprintf(page, "none\n");
4367}
4368
4369static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004370metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004371{
4372 int major, minor;
4373 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004374 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004375 /* Changing the details of 'external' metadata is
4376 * always permitted. Otherwise there must be
4377 * no devices attached to the array.
4378 */
NeilBrown67918752014-12-15 12:57:01 +11004379
4380 err = mddev_lock(mddev);
4381 if (err)
4382 return err;
4383 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004384 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4385 ;
4386 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004387 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004388
NeilBrown67918752014-12-15 12:57:01 +11004389 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004390 if (cmd_match(buf, "none")) {
4391 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004392 mddev->external = 0;
4393 mddev->major_version = 0;
4394 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004395 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004396 }
4397 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004398 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004399 if (namelen >= sizeof(mddev->metadata_type))
4400 namelen = sizeof(mddev->metadata_type)-1;
4401 strncpy(mddev->metadata_type, buf+9, namelen);
4402 mddev->metadata_type[namelen] = 0;
4403 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4404 mddev->metadata_type[--namelen] = 0;
4405 mddev->persistent = 0;
4406 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004407 mddev->major_version = 0;
4408 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004409 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004410 }
4411 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004412 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004413 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004414 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004415 buf = e+1;
4416 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004417 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004418 goto out_unlock;
4419 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004420 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004421 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004422 mddev->major_version = major;
4423 mddev->minor_version = minor;
4424 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004425 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004426 err = 0;
4427out_unlock:
4428 mddev_unlock(mddev);
4429 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004430}
4431
4432static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004433__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004434
NeilBrowna35b0d62006-01-06 00:20:49 -08004435static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004436action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004437{
NeilBrown7eec3142005-11-08 21:39:44 -08004438 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004439 unsigned long recovery = mddev->recovery;
4440 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004441 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004442 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4443 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4444 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004445 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004446 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4447 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004448 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004449 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004450 type = "check";
4451 else
4452 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004453 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004454 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004455 else if (mddev->reshape_position != MaxSector)
4456 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004457 }
4458 return sprintf(page, "%s\n", type);
4459}
4460
4461static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004462action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004463{
NeilBrown7eec3142005-11-08 21:39:44 -08004464 if (!mddev->pers || !mddev->pers->sync_request)
4465 return -EINVAL;
4466
NeilBrownb6a9ce62009-05-26 09:41:17 +10004467
4468 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004469 if (cmd_match(page, "frozen"))
4470 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4471 else
4472 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004473 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4474 mddev_lock(mddev) == 0) {
4475 flush_workqueue(md_misc_wq);
4476 if (mddev->sync_thread) {
4477 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004478 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004479 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004480 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004481 }
NeilBrown312045e2015-12-21 11:01:21 +11004482 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004483 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004484 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004485 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004486 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004487 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004488 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004489 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004490 int err;
4491 if (mddev->pers->start_reshape == NULL)
4492 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004493 err = mddev_lock(mddev);
4494 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004495 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4496 err = -EBUSY;
4497 else {
4498 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4499 err = mddev->pers->start_reshape(mddev);
4500 }
NeilBrown67918752014-12-15 12:57:01 +11004501 mddev_unlock(mddev);
4502 }
NeilBrown16484bf2006-03-27 01:18:13 -08004503 if (err)
4504 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004505 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004506 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004507 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004508 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004509 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004510 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004511 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004512 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4513 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004514 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004515 if (mddev->ro == 2) {
4516 /* A write to sync_action is enough to justify
4517 * canceling read-auto mode
4518 */
4519 mddev->ro = 0;
4520 md_wakeup_thread(mddev->sync_thread);
4521 }
NeilBrown03c902e2006-01-06 00:20:46 -08004522 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004523 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004524 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004525 return len;
4526}
4527
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004528static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004529__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004530
4531static ssize_t
4532last_sync_action_show(struct mddev *mddev, char *page)
4533{
4534 return sprintf(page, "%s\n", mddev->last_sync_action);
4535}
4536
4537static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4538
NeilBrown9d888832005-11-08 21:39:26 -08004539static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004540mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004541{
4542 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004543 (unsigned long long)
4544 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004545}
4546
NeilBrown80ca3a42006-07-10 04:44:18 -07004547static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004548
NeilBrown88202a02006-01-06 00:21:36 -08004549static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004550sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004551{
4552 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4553 mddev->sync_speed_min ? "local": "system");
4554}
4555
4556static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004557sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004558{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004559 unsigned int min;
4560 int rv;
4561
NeilBrown88202a02006-01-06 00:21:36 -08004562 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004563 min = 0;
4564 } else {
4565 rv = kstrtouint(buf, 10, &min);
4566 if (rv < 0)
4567 return rv;
4568 if (min == 0)
4569 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004570 }
NeilBrown88202a02006-01-06 00:21:36 -08004571 mddev->sync_speed_min = min;
4572 return len;
4573}
4574
4575static struct md_sysfs_entry md_sync_min =
4576__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4577
4578static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004579sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004580{
4581 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4582 mddev->sync_speed_max ? "local": "system");
4583}
4584
4585static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004586sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004587{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004588 unsigned int max;
4589 int rv;
4590
NeilBrown88202a02006-01-06 00:21:36 -08004591 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004592 max = 0;
4593 } else {
4594 rv = kstrtouint(buf, 10, &max);
4595 if (rv < 0)
4596 return rv;
4597 if (max == 0)
4598 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004599 }
NeilBrown88202a02006-01-06 00:21:36 -08004600 mddev->sync_speed_max = max;
4601 return len;
4602}
4603
4604static struct md_sysfs_entry md_sync_max =
4605__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4606
Iustin Popd7f3d292007-10-16 23:30:54 -07004607static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004608degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004609{
4610 return sprintf(page, "%d\n", mddev->degraded);
4611}
4612static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004613
4614static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004615sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004616{
4617 return sprintf(page, "%d\n", mddev->parallel_resync);
4618}
4619
4620static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004621sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004622{
4623 long n;
4624
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004625 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004626 return -EINVAL;
4627
4628 if (n != 0 && n != 1)
4629 return -EINVAL;
4630
4631 mddev->parallel_resync = n;
4632
4633 if (mddev->sync_thread)
4634 wake_up(&resync_wait);
4635
4636 return len;
4637}
4638
4639/* force parallel resync, even with shared block devices */
4640static struct md_sysfs_entry md_sync_force_parallel =
4641__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4642 sync_force_parallel_show, sync_force_parallel_store);
4643
4644static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004645sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004646{
4647 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004648 if (mddev->curr_resync == 0)
4649 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004650 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4651 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004652 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004653 db = resync - mddev->resync_mark_cnt;
4654 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004655}
4656
NeilBrown80ca3a42006-07-10 04:44:18 -07004657static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004658
4659static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004660sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004661{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004662 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004663
NeilBrownacb180b2009-04-14 16:28:34 +10004664 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4665 return sprintf(page, "none\n");
4666
NeilBrown72f36d52012-10-11 14:25:57 +11004667 if (mddev->curr_resync == 1 ||
4668 mddev->curr_resync == 2)
4669 return sprintf(page, "delayed\n");
4670
NeilBrownc804cde2012-05-21 09:28:33 +10004671 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4672 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004673 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004674 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004675 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004676
NeilBrownacb180b2009-04-14 16:28:34 +10004677 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004678 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004679}
4680
NeilBrown750f1992014-09-30 08:53:05 +10004681static struct md_sysfs_entry md_sync_completed =
4682 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004683
NeilBrowne464eaf2006-03-27 01:18:14 -08004684static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004685min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004686{
4687 return sprintf(page, "%llu\n",
4688 (unsigned long long)mddev->resync_min);
4689}
4690static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004691min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004692{
4693 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004694 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004695
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004696 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004697 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004698
4699 spin_lock(&mddev->lock);
4700 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004701 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004702 goto out_unlock;
4703
4704 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004705 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004706 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004707
NeilBrown50c37b12015-03-23 17:36:38 +11004708 /* Round down to multiple of 4K for safety */
4709 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004710 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004711
NeilBrown23da4222014-12-15 12:57:01 +11004712out_unlock:
4713 spin_unlock(&mddev->lock);
4714 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004715}
4716
4717static struct md_sysfs_entry md_min_sync =
4718__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4719
4720static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004721max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004722{
4723 if (mddev->resync_max == MaxSector)
4724 return sprintf(page, "max\n");
4725 else
4726 return sprintf(page, "%llu\n",
4727 (unsigned long long)mddev->resync_max);
4728}
4729static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004730max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004731{
NeilBrown23da4222014-12-15 12:57:01 +11004732 int err;
4733 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004734 if (strncmp(buf, "max", 3) == 0)
4735 mddev->resync_max = MaxSector;
4736 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004737 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004738 int chunk;
4739
4740 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004741 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004742 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004743 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004744 goto out_unlock;
4745
4746 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004747 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004748 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004749 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004750 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004751
4752 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004753 chunk = mddev->chunk_sectors;
4754 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004755 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004756
4757 err = -EINVAL;
4758 if (sector_div(temp, chunk))
4759 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004760 }
4761 mddev->resync_max = max;
4762 }
4763 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004764 err = 0;
4765out_unlock:
4766 spin_unlock(&mddev->lock);
4767 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004768}
4769
4770static struct md_sysfs_entry md_max_sync =
4771__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4772
4773static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004774suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004775{
4776 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4777}
4778
4779static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004780suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004781{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004782 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004783 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004784
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004785 err = kstrtoull(buf, 10, &new);
4786 if (err < 0)
4787 return err;
4788 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004789 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004790
NeilBrown67918752014-12-15 12:57:01 +11004791 err = mddev_lock(mddev);
4792 if (err)
4793 return err;
4794 err = -EINVAL;
4795 if (mddev->pers == NULL ||
4796 mddev->pers->quiesce == NULL)
4797 goto unlock;
4798 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004799 mddev->suspend_lo = new;
4800 if (new >= old)
4801 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004802 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004803 else {
4804 /* Expanding suspended region - need to wait */
4805 mddev->pers->quiesce(mddev, 1);
4806 mddev->pers->quiesce(mddev, 0);
4807 }
NeilBrown67918752014-12-15 12:57:01 +11004808 err = 0;
4809unlock:
4810 mddev_unlock(mddev);
4811 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004812}
4813static struct md_sysfs_entry md_suspend_lo =
4814__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4815
NeilBrowne464eaf2006-03-27 01:18:14 -08004816static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004817suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004818{
4819 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4820}
4821
4822static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004823suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004824{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004825 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004826 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004827
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004828 err = kstrtoull(buf, 10, &new);
4829 if (err < 0)
4830 return err;
4831 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004832 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004833
NeilBrown67918752014-12-15 12:57:01 +11004834 err = mddev_lock(mddev);
4835 if (err)
4836 return err;
4837 err = -EINVAL;
4838 if (mddev->pers == NULL ||
4839 mddev->pers->quiesce == NULL)
4840 goto unlock;
4841 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004842 mddev->suspend_hi = new;
4843 if (new <= old)
4844 /* Shrinking suspended region */
4845 mddev->pers->quiesce(mddev, 2);
4846 else {
4847 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004848 mddev->pers->quiesce(mddev, 1);
4849 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004850 }
NeilBrown67918752014-12-15 12:57:01 +11004851 err = 0;
4852unlock:
4853 mddev_unlock(mddev);
4854 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004855}
4856static struct md_sysfs_entry md_suspend_hi =
4857__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4858
NeilBrown08a02ec2007-05-09 02:35:38 -07004859static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004860reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004861{
4862 if (mddev->reshape_position != MaxSector)
4863 return sprintf(page, "%llu\n",
4864 (unsigned long long)mddev->reshape_position);
4865 strcpy(page, "none\n");
4866 return 5;
4867}
4868
4869static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004870reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004871{
NeilBrownc6563a82012-05-21 09:27:00 +10004872 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004873 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004874 int err;
NeilBrown67918752014-12-15 12:57:01 +11004875
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004876 err = kstrtoull(buf, 10, &new);
4877 if (err < 0)
4878 return err;
4879 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004880 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004881 err = mddev_lock(mddev);
4882 if (err)
4883 return err;
4884 err = -EBUSY;
4885 if (mddev->pers)
4886 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004887 mddev->reshape_position = new;
4888 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004889 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004890 mddev->new_level = mddev->level;
4891 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004892 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004893 rdev_for_each(rdev, mddev)
4894 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004895 err = 0;
4896unlock:
4897 mddev_unlock(mddev);
4898 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004899}
4900
4901static struct md_sysfs_entry md_reshape_position =
4902__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4903 reshape_position_store);
4904
Dan Williamsb522adc2009-03-31 15:00:31 +11004905static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004906reshape_direction_show(struct mddev *mddev, char *page)
4907{
4908 return sprintf(page, "%s\n",
4909 mddev->reshape_backwards ? "backwards" : "forwards");
4910}
4911
4912static ssize_t
4913reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4914{
4915 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004916 int err;
4917
NeilBrown2c810cd2012-05-21 09:27:00 +10004918 if (cmd_match(buf, "forwards"))
4919 backwards = 0;
4920 else if (cmd_match(buf, "backwards"))
4921 backwards = 1;
4922 else
4923 return -EINVAL;
4924 if (mddev->reshape_backwards == backwards)
4925 return len;
4926
NeilBrown67918752014-12-15 12:57:01 +11004927 err = mddev_lock(mddev);
4928 if (err)
4929 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004930 /* check if we are allowed to change */
4931 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004932 err = -EBUSY;
4933 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004934 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004935 err = -EINVAL;
4936 else
4937 mddev->reshape_backwards = backwards;
4938 mddev_unlock(mddev);
4939 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004940}
4941
4942static struct md_sysfs_entry md_reshape_direction =
4943__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4944 reshape_direction_store);
4945
4946static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004947array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004948{
4949 if (mddev->external_size)
4950 return sprintf(page, "%llu\n",
4951 (unsigned long long)mddev->array_sectors/2);
4952 else
4953 return sprintf(page, "default\n");
4954}
4955
4956static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004957array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004958{
4959 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004960 int err;
4961
4962 err = mddev_lock(mddev);
4963 if (err)
4964 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004965
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004966 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08004967 if (mddev_is_clustered(mddev)) {
4968 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004969 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08004970 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004971
Dan Williamsb522adc2009-03-31 15:00:31 +11004972 if (strncmp(buf, "default", 7) == 0) {
4973 if (mddev->pers)
4974 sectors = mddev->pers->size(mddev, 0, 0);
4975 else
4976 sectors = mddev->array_sectors;
4977
4978 mddev->external_size = 0;
4979 } else {
4980 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004981 err = -EINVAL;
4982 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4983 err = -E2BIG;
4984 else
4985 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004986 }
4987
NeilBrown67918752014-12-15 12:57:01 +11004988 if (!err) {
4989 mddev->array_sectors = sectors;
4990 if (mddev->pers) {
4991 set_capacity(mddev->gendisk, mddev->array_sectors);
4992 revalidate_disk(mddev->gendisk);
4993 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004994 }
NeilBrown67918752014-12-15 12:57:01 +11004995 mddev_unlock(mddev);
4996 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004997}
4998
4999static struct md_sysfs_entry md_array_size =
5000__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5001 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005002
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005003static ssize_t
5004consistency_policy_show(struct mddev *mddev, char *page)
5005{
5006 int ret;
5007
5008 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5009 ret = sprintf(page, "journal\n");
5010 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5011 ret = sprintf(page, "ppl\n");
5012 } else if (mddev->bitmap) {
5013 ret = sprintf(page, "bitmap\n");
5014 } else if (mddev->pers) {
5015 if (mddev->pers->sync_request)
5016 ret = sprintf(page, "resync\n");
5017 else
5018 ret = sprintf(page, "none\n");
5019 } else {
5020 ret = sprintf(page, "unknown\n");
5021 }
5022
5023 return ret;
5024}
5025
5026static ssize_t
5027consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5028{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005029 int err = 0;
5030
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005031 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005032 if (mddev->pers->change_consistency_policy)
5033 err = mddev->pers->change_consistency_policy(mddev, buf);
5034 else
5035 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005036 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5037 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005038 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005039 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005040 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005041
5042 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005043}
5044
5045static struct md_sysfs_entry md_consistency_policy =
5046__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5047 consistency_policy_store);
5048
NeilBrowneae17012005-11-08 21:39:23 -08005049static struct attribute *md_default_attrs[] = {
5050 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005051 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005052 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005053 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005054 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005055 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005056 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005057 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005058 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005059 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005060 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005061 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005062 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005063 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005064 &md_consistency_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005065 NULL,
5066};
5067
5068static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005069 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005070 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005071 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005072 &md_sync_min.attr,
5073 &md_sync_max.attr,
5074 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005075 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005076 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005077 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005078 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005079 &md_suspend_lo.attr,
5080 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005081 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005082 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005083 NULL,
5084};
NeilBrown411036f2005-11-08 21:39:40 -08005085static struct attribute_group md_redundancy_group = {
5086 .name = NULL,
5087 .attrs = md_redundancy_attrs,
5088};
5089
NeilBrowneae17012005-11-08 21:39:23 -08005090static ssize_t
5091md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5092{
5093 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005094 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005095 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005096
5097 if (!entry->show)
5098 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005099 spin_lock(&all_mddevs_lock);
5100 if (list_empty(&mddev->all_mddevs)) {
5101 spin_unlock(&all_mddevs_lock);
5102 return -EBUSY;
5103 }
5104 mddev_get(mddev);
5105 spin_unlock(&all_mddevs_lock);
5106
NeilBrownb7b17c92014-12-15 12:56:59 +11005107 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005108 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005109 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005110}
5111
5112static ssize_t
5113md_attr_store(struct kobject *kobj, struct attribute *attr,
5114 const char *page, size_t length)
5115{
5116 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005117 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005118 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005119
5120 if (!entry->store)
5121 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005122 if (!capable(CAP_SYS_ADMIN))
5123 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005124 spin_lock(&all_mddevs_lock);
5125 if (list_empty(&mddev->all_mddevs)) {
5126 spin_unlock(&all_mddevs_lock);
5127 return -EBUSY;
5128 }
5129 mddev_get(mddev);
5130 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005131 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005132 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005133 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005134}
5135
5136static void md_free(struct kobject *ko)
5137{
NeilBrownfd01b882011-10-11 16:47:53 +11005138 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005139
5140 if (mddev->sysfs_state)
5141 sysfs_put(mddev->sysfs_state);
5142
NeilBrown6cd18e72015-04-27 14:12:22 +10005143 if (mddev->queue)
5144 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11005145 if (mddev->gendisk) {
5146 del_gendisk(mddev->gendisk);
5147 put_disk(mddev->gendisk);
5148 }
NeilBrown4ad23a972017-03-15 14:05:14 +11005149 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005150
NeilBrowneae17012005-11-08 21:39:23 -08005151 kfree(mddev);
5152}
5153
Emese Revfy52cf25d2010-01-19 02:58:23 +01005154static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005155 .show = md_attr_show,
5156 .store = md_attr_store,
5157};
5158static struct kobj_type md_ktype = {
5159 .release = md_free,
5160 .sysfs_ops = &md_sysfs_ops,
5161 .default_attrs = md_default_attrs,
5162};
5163
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164int mdp_major = 0;
5165
Dan Williams5fd3a172009-03-04 00:57:25 -07005166static void mddev_delayed_delete(struct work_struct *ws)
5167{
NeilBrownfd01b882011-10-11 16:47:53 +11005168 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005169
NeilBrown43a70502009-12-14 12:49:55 +11005170 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005171 kobject_del(&mddev->kobj);
5172 kobject_put(&mddev->kobj);
5173}
5174
NeilBrown4ad23a972017-03-15 14:05:14 +11005175static void no_op(struct percpu_ref *r) {}
5176
NeilBrownefeb53c2009-01-09 08:31:10 +11005177static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178{
NeilBrown039b7222017-04-12 16:26:13 +10005179 /*
5180 * If dev is zero, name is the name of a device to allocate with
5181 * an arbitrary minor number. It will be "md_???"
5182 * If dev is non-zero it must be a device number with a MAJOR of
5183 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5184 * the device is being created by opening a node in /dev.
5185 * If "name" is not NULL, the device is being created by
5186 * writing to /sys/module/md_mod/parameters/new_array.
5187 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005188 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005189 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005191 int partitioned;
5192 int shift;
5193 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005194 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
5196 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005197 return -ENODEV;
5198
5199 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5200 shift = partitioned ? MdpMinorShift : 0;
5201 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202
Tejun Heoe804ac72010-10-15 15:36:08 +02005203 /* wait for any previous instance of this device to be
5204 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005205 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005206 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005207
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005208 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005209 error = -EEXIST;
5210 if (mddev->gendisk)
5211 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005212
NeilBrown039b7222017-04-12 16:26:13 +10005213 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005214 /* Need to ensure that 'name' is not a duplicate.
5215 */
NeilBrownfd01b882011-10-11 16:47:53 +11005216 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005217 spin_lock(&all_mddevs_lock);
5218
5219 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5220 if (mddev2->gendisk &&
5221 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5222 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005223 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005224 }
5225 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 }
NeilBrown039b7222017-04-12 16:26:13 +10005227 if (name && dev)
5228 /*
5229 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5230 */
5231 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005232
NeilBrown0909dc42009-07-01 12:27:21 +10005233 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005234 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005235 if (!mddev->queue)
5236 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005237 mddev->queue->queuedata = mddev;
5238
NeilBrown409c57f2009-03-31 14:39:39 +11005239 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005240 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005241
NeilBrown4ad23a972017-03-15 14:05:14 +11005242 if (percpu_ref_init(&mddev->writes_pending, no_op, 0, GFP_KERNEL) < 0)
5243 goto abort;
5244 /* We want to start with the refcount at zero */
5245 percpu_ref_put(&mddev->writes_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 disk = alloc_disk(1 << shift);
5247 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005248 blk_cleanup_queue(mddev->queue);
5249 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005250 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005252 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005254 if (name)
5255 strcpy(disk->disk_name, name);
5256 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005258 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 disk->fops = &md_fops;
5261 disk->private_data = mddev;
5262 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005263 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005264 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005265 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005266 * remove it now.
5267 */
5268 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005270 /* As soon as we call add_disk(), another thread could get
5271 * through to md_open, so make sure it doesn't get too far
5272 */
5273 mutex_lock(&mddev->open_mutex);
5274 add_disk(disk);
5275
Tejun Heoed9e1982008-08-25 19:56:05 +09005276 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5277 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005278 if (error) {
5279 /* This isn't possible, but as kobject_init_and_add is marked
5280 * __must_check, we must do something with the result
5281 */
NeilBrown9d487392016-11-02 14:16:49 +11005282 pr_debug("md: cannot register %s/md - name in use\n",
5283 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005284 error = 0;
5285 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005286 if (mddev->kobj.sd &&
5287 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005288 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005289 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005290 abort:
5291 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005292 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005293 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005294 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005295 }
NeilBrownd3374822009-01-09 08:31:10 +11005296 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005297 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005298}
5299
5300static struct kobject *md_probe(dev_t dev, int *part, void *data)
5301{
NeilBrown78b63502017-04-12 16:26:13 +10005302 if (create_on_open)
5303 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 return NULL;
5305}
5306
NeilBrownefeb53c2009-01-09 08:31:10 +11005307static int add_named_array(const char *val, struct kernel_param *kp)
5308{
NeilBrown039b7222017-04-12 16:26:13 +10005309 /*
5310 * val must be "md_*" or "mdNNN".
5311 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005312 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005313 * For "mdNNN" we allocate an array with the minor number NNN
5314 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005315 */
5316 int len = strlen(val);
5317 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005318 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005319
5320 while (len && val[len-1] == '\n')
5321 len--;
5322 if (len >= DISK_NAME_LEN)
5323 return -E2BIG;
5324 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005325 if (strncmp(buf, "md_", 3) == 0)
5326 return md_alloc(0, buf);
5327 if (strncmp(buf, "md", 2) == 0 &&
5328 isdigit(buf[2]) &&
5329 kstrtoul(buf+2, 10, &devnum) == 0 &&
5330 devnum <= MINORMASK)
5331 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5332
5333 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005334}
5335
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336static void md_safemode_timeout(unsigned long data)
5337{
NeilBrownfd01b882011-10-11 16:47:53 +11005338 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339
NeilBrown4ad23a972017-03-15 14:05:14 +11005340 mddev->safemode = 1;
5341 if (mddev->external)
5342 sysfs_notify_dirent_safe(mddev->sysfs_state);
5343
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 md_wakeup_thread(mddev->thread);
5345}
5346
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005347static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348
NeilBrownfd01b882011-10-11 16:47:53 +11005349int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350{
NeilBrown2604b702006-01-06 00:20:36 -08005351 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005352 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005353 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354
NeilBrowna757e642005-04-16 15:26:42 -07005355 if (list_empty(&mddev->disks))
5356 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
5359 if (mddev->pers)
5360 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005361 /* Cannot run until previous stop completes properly */
5362 if (mddev->sysfs_active)
5363 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005364
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 /*
5366 * Analyze all RAID superblock(s)
5367 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005368 if (!mddev->raid_disks) {
5369 if (!mddev->persistent)
5370 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005371 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373
NeilBrownd9d166c2006-01-06 00:20:51 -08005374 if (mddev->level != LEVEL_NONE)
5375 request_module("md-level-%d", mddev->level);
5376 else if (mddev->clevel[0])
5377 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
5379 /*
5380 * Drop all container device buffers, from now on
5381 * the only valid external interface is through the md
5382 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005384 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005385 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 continue;
5387 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005388 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005389 if (mddev->ro != 1 &&
5390 (bdev_read_only(rdev->bdev) ||
5391 bdev_read_only(rdev->meta_bdev))) {
5392 mddev->ro = 1;
5393 if (mddev->gendisk)
5394 set_disk_ro(mddev->gendisk, 1);
5395 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005396
5397 /* perform some consistency tests on the device.
5398 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005399 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005400 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005401 if (rdev->meta_bdev) {
5402 /* Nothing to check */;
5403 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005404 if (mddev->dev_sectors &&
5405 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005406 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005407 pr_warn("md: %s: data overlaps metadata\n",
5408 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005409 return -EINVAL;
5410 }
5411 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005412 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005413 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005414 pr_warn("md: %s: metadata overlaps data\n",
5415 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005416 return -EINVAL;
5417 }
5418 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005419 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 }
5421
Ming Lei10273172017-02-14 23:29:00 +08005422 if (mddev->bio_set == NULL) {
Kent Overstreet395c72a2012-09-06 15:34:55 -07005423 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
Ming Lei10273172017-02-14 23:29:00 +08005424 if (!mddev->bio_set)
5425 return -ENOMEM;
5426 }
NeilBrowna167f662010-10-26 18:31:13 +11005427
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005429 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005430 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005432 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005433 pr_warn("md: personality for level %d is not loaded!\n",
5434 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005435 else
NeilBrown9d487392016-11-02 14:16:49 +11005436 pr_warn("md: personality for level %s is not loaded!\n",
5437 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 return -EINVAL;
5439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005441 if (mddev->level != pers->level) {
5442 mddev->level = pers->level;
5443 mddev->new_level = pers->level;
5444 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005445 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
NeilBrownf6705572006-03-27 01:18:11 -08005447 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005448 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005449 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005450 module_put(pers->owner);
5451 return -EINVAL;
5452 }
5453
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005454 if (pers->sync_request) {
5455 /* Warn if this is a potentially silly
5456 * configuration.
5457 */
5458 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005459 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005460 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005461
NeilBrowndafb20f2012-03-19 12:46:39 +11005462 rdev_for_each(rdev, mddev)
5463 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005464 if (rdev < rdev2 &&
5465 rdev->bdev->bd_contains ==
5466 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005467 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5468 mdname(mddev),
5469 bdevname(rdev->bdev,b),
5470 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005471 warned = 1;
5472 }
5473 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005474
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005475 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005476 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005477 }
5478
NeilBrown657390d2005-08-26 18:34:16 -07005479 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005480 /* may be over-ridden by personality */
5481 mddev->resync_max_sectors = mddev->dev_sectors;
5482
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005483 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484
NeilBrown0f9552b52009-12-30 12:08:50 +11005485 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005486 mddev->ro = 2; /* read-only, but switch on first write */
5487
Song Liua85dd7b2017-01-23 17:12:57 -08005488 /*
5489 * NOTE: some pers->run(), for example r5l_recovery_log(), wakes
5490 * up mddev->thread. It is important to initialize critical
5491 * resources for mddev->thread BEFORE calling pers->run().
5492 */
NeilBrown36d091f2014-12-15 12:56:58 +11005493 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005494 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005495 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005496 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005497 WARN_ONCE(!mddev->external_size,
5498 "%s: default size too small, but 'external_size' not in effect?\n",
5499 __func__);
5500 pr_warn("md: invalid array_size %llu > default size %llu\n",
5501 (unsigned long long)mddev->array_sectors / 2,
5502 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005503 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005504 }
NeilBrown36d091f2014-12-15 12:56:58 +11005505 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005506 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005507 struct bitmap *bitmap;
5508
5509 bitmap = bitmap_create(mddev, -1);
5510 if (IS_ERR(bitmap)) {
5511 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005512 pr_warn("%s: failed to create bitmap (%d)\n",
5513 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005514 } else
5515 mddev->bitmap = bitmap;
5516
NeilBrownb15c2e52006-01-06 00:20:16 -08005517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005519 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005520 if (mddev->private)
5521 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005522 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005523 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005524 bitmap_destroy(mddev);
5525 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 }
NeilBrown5c675f82014-12-15 12:56:56 +11005527 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005528 bool nonrot = true;
5529
5530 rdev_for_each(rdev, mddev) {
5531 if (rdev->raid_disk >= 0 &&
5532 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5533 nonrot = false;
5534 break;
5535 }
5536 }
5537 if (mddev->degraded)
5538 nonrot = false;
5539 if (nonrot)
5540 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5541 else
5542 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005543 mddev->queue->backing_dev_info->congested_data = mddev;
5544 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005545 }
NeilBrown36d091f2014-12-15 12:56:58 +11005546 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005547 if (mddev->kobj.sd &&
5548 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005549 pr_warn("md: cannot register extra attributes for %s\n",
5550 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005551 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005552 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005553 mddev->ro = 0;
5554
Robert Becker1e509152009-12-14 12:49:58 +11005555 atomic_set(&mddev->max_corr_read_errors,
5556 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005558 if (mddev_is_clustered(mddev))
5559 mddev->safemode_delay = 0;
5560 else
5561 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005563 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005564 spin_lock(&mddev->lock);
5565 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005566 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005567 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005568 if (rdev->raid_disk >= 0)
5569 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005570 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005571
NeilBrowna4a3d262015-07-17 11:57:30 +10005572 if (mddev->degraded && !mddev->ro)
5573 /* This ensures that recovering status is reported immediately
5574 * via sysfs - until a lack of spares is confirmed.
5575 */
5576 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005578
Shaohua Li29530792016-12-08 15:48:19 -08005579 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07005580 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581
NeilBrownd7603b72006-01-06 00:20:30 -08005582 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005583 sysfs_notify_dirent_safe(mddev->sysfs_state);
5584 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005585 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 return 0;
5587}
NeilBrown390ee602010-06-01 19:37:27 +10005588EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589
NeilBrownfd01b882011-10-11 16:47:53 +11005590static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005591{
5592 int err;
5593
5594 err = md_run(mddev);
5595 if (err)
5596 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005597 err = bitmap_load(mddev);
5598 if (err) {
5599 bitmap_destroy(mddev);
5600 goto out;
5601 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005602
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005603 if (mddev_is_clustered(mddev))
5604 md_allow_write(mddev);
5605
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005606 md_wakeup_thread(mddev->thread);
5607 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5608
NeilBrownfe60b012010-03-29 11:10:42 +11005609 set_capacity(mddev->gendisk, mddev->array_sectors);
5610 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005611 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005612 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5613out:
5614 return err;
5615}
5616
NeilBrownfd01b882011-10-11 16:47:53 +11005617static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618{
5619 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10005620 struct md_rdev *rdev;
5621 bool has_journal = false;
5622 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
Andre Noll80fab1d2008-07-11 22:02:21 +10005624 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005626 return -ENXIO;
5627 if (!mddev->pers)
5628 return -EINVAL;
5629 if (!mddev->ro)
5630 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005631
NeilBrown97b20ef2017-04-13 08:53:48 +10005632 rcu_read_lock();
5633 rdev_for_each_rcu(rdev, mddev) {
5634 if (test_bit(Journal, &rdev->flags) &&
5635 !test_bit(Faulty, &rdev->flags))
5636 has_journal = true;
5637 if (bdev_read_only(rdev->bdev))
5638 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07005639 }
NeilBrown97b20ef2017-04-13 08:53:48 +10005640 rcu_read_unlock();
5641 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
5642 /* Don't restart rw with journal missing/faulty */
5643 return -EINVAL;
5644 if (has_readonly)
5645 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07005646
Andre Noll80fab1d2008-07-11 22:02:21 +10005647 mddev->safemode = 0;
5648 mddev->ro = 0;
5649 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11005650 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10005651 /* Kick recovery or resync if necessary */
5652 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5653 md_wakeup_thread(mddev->thread);
5654 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005655 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005656 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657}
5658
NeilBrownfd01b882011-10-11 16:47:53 +11005659static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005660{
5661 mddev->array_sectors = 0;
5662 mddev->external_size = 0;
5663 mddev->dev_sectors = 0;
5664 mddev->raid_disks = 0;
5665 mddev->recovery_cp = 0;
5666 mddev->resync_min = 0;
5667 mddev->resync_max = MaxSector;
5668 mddev->reshape_position = MaxSector;
5669 mddev->external = 0;
5670 mddev->persistent = 0;
5671 mddev->level = LEVEL_NONE;
5672 mddev->clevel[0] = 0;
5673 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08005674 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005675 mddev->ro = 0;
5676 mddev->metadata_type[0] = 0;
5677 mddev->chunk_sectors = 0;
5678 mddev->ctime = mddev->utime = 0;
5679 mddev->layout = 0;
5680 mddev->max_disks = 0;
5681 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005682 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005683 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005684 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005685 mddev->new_level = LEVEL_NONE;
5686 mddev->new_layout = 0;
5687 mddev->new_chunk_sectors = 0;
5688 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005689 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005690 mddev->suspend_lo = mddev->suspend_hi = 0;
5691 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5692 mddev->recovery = 0;
5693 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005694 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005695 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005696 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005697 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005698 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005699 mddev->bitmap_info.offset = 0;
5700 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005701 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005702 mddev->bitmap_info.chunksize = 0;
5703 mddev->bitmap_info.daemon_sleep = 0;
5704 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005705 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005706}
5707
NeilBrownfd01b882011-10-11 16:47:53 +11005708static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005709{
NeilBrown6b6204e2013-05-09 09:48:30 +10005710 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005711 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005712 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005713 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005714 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005715 }
5716
5717 del_timer_sync(&mddev->safemode_timer);
5718
Shaohua Li034e33f2016-11-21 10:29:19 -08005719 if (mddev->pers && mddev->pers->quiesce) {
5720 mddev->pers->quiesce(mddev, 1);
5721 mddev->pers->quiesce(mddev, 0);
5722 }
NeilBrowna047e122010-03-29 12:07:53 +11005723 bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005724
NeilBrownb6d428c2013-04-24 11:42:42 +10005725 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005726 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08005727 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11005728 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005729 if (!mddev_is_clustered(mddev))
5730 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005731 md_update_sb(mddev, 1);
5732 }
5733}
NeilBrowndefad612011-01-14 09:14:33 +11005734
NeilBrownfd01b882011-10-11 16:47:53 +11005735void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005736{
NeilBrown29f097c2013-11-14 17:54:51 +11005737 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005738 __md_stop_writes(mddev);
5739 mddev_unlock(mddev);
5740}
NeilBrown390ee602010-06-01 19:37:27 +10005741EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005742
NeilBrown5aa61f42014-12-15 12:56:57 +11005743static void mddev_detach(struct mddev *mddev)
5744{
Guoqing Jiang48df4982017-03-14 09:40:20 +08005745 bitmap_wait_behind_writes(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005746 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005747 mddev->pers->quiesce(mddev, 1);
5748 mddev->pers->quiesce(mddev, 0);
5749 }
5750 md_unregister_thread(&mddev->thread);
5751 if (mddev->queue)
5752 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5753}
5754
NeilBrown5eff3c42012-11-19 10:47:48 +11005755static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005756{
NeilBrown36d091f2014-12-15 12:56:58 +11005757 struct md_personality *pers = mddev->pers;
Guoqing Jiang48df4982017-03-14 09:40:20 +08005758 bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11005759 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005760 /* Ensure ->event_work is done */
5761 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005762 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005763 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005764 spin_unlock(&mddev->lock);
5765 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005766 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005767 if (pers->sync_request && mddev->to_remove == NULL)
5768 mddev->to_remove = &md_redundancy_group;
5769 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005770 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005771}
NeilBrown5eff3c42012-11-19 10:47:48 +11005772
5773void md_stop(struct mddev *mddev)
5774{
5775 /* stop the array and free an attached data structures.
5776 * This is called from dm-raid
5777 */
5778 __md_stop(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005779 if (mddev->bio_set)
5780 bioset_free(mddev->bio_set);
5781}
5782
NeilBrown390ee602010-06-01 19:37:27 +10005783EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005784
NeilBrowna05b7ea2012-07-19 15:59:18 +10005785static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005786{
5787 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005788 int did_freeze = 0;
5789
5790 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5791 did_freeze = 1;
5792 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5793 md_wakeup_thread(mddev->thread);
5794 }
NeilBrownf851b602014-12-11 10:02:10 +11005795 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005796 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005797 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005798 /* Thread might be blocked waiting for metadata update
5799 * which will now never happen */
5800 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005801
Shaohua Li29530792016-12-08 15:48:19 -08005802 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10005803 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005804 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005805 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5806 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005807 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005808 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005809 mddev_lock_nointr(mddev);
5810
NeilBrowna4bd82d2010-03-29 13:23:10 +11005811 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005812 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005813 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005814 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005815 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005816 if (did_freeze) {
5817 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005818 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005819 md_wakeup_thread(mddev->thread);
5820 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005821 err = -EBUSY;
5822 goto out;
5823 }
5824 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005825 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005826
5827 err = -ENXIO;
5828 if (mddev->ro==1)
5829 goto out;
5830 mddev->ro = 1;
5831 set_disk_ro(mddev->gendisk, 1);
5832 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005833 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5834 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005835 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005836 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005837 }
5838out:
5839 mutex_unlock(&mddev->open_mutex);
5840 return err;
5841}
5842
NeilBrown9e653b62006-06-26 00:27:58 -07005843/* mode:
5844 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005845 * 2 - stop but do not disassemble array
5846 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005847static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005848 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005851 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005852 int did_freeze = 0;
5853
5854 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5855 did_freeze = 1;
5856 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5857 md_wakeup_thread(mddev->thread);
5858 }
NeilBrownf851b602014-12-11 10:02:10 +11005859 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005860 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005861 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005862 /* Thread might be blocked waiting for metadata update
5863 * which will now never happen */
5864 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005865
NeilBrown30b8feb2013-11-14 15:16:17 +11005866 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005867 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5868 !test_bit(MD_RECOVERY_RUNNING,
5869 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005870 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871
NeilBrownc8c00a62009-08-10 12:50:52 +10005872 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005873 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005874 mddev->sysfs_active ||
5875 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005876 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005877 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005878 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005879 if (did_freeze) {
5880 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005881 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005882 md_wakeup_thread(mddev->thread);
5883 }
NeilBrown260fa032013-08-27 16:44:13 +10005884 return -EBUSY;
5885 }
NeilBrown6e17b022010-08-07 21:41:19 +10005886 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005887 if (mddev->ro)
5888 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005889
NeilBrowndefad612011-01-14 09:14:33 +11005890 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005891 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01005892 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005893
NeilBrowna4bd82d2010-03-29 13:23:10 +11005894 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005895 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005896
NeilBrowndafb20f2012-03-19 12:46:39 +11005897 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005898 if (rdev->raid_disk >= 0)
5899 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005900
NeilBrowna4bd82d2010-03-29 13:23:10 +11005901 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005902 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005903 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005904 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005905
NeilBrowna4bd82d2010-03-29 13:23:10 +11005906 if (mddev->ro)
5907 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005908 } else
5909 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 /*
5911 * Free resources if final stop
5912 */
NeilBrown9e653b62006-06-26 00:27:58 -07005913 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005914 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
NeilBrownc3d97142009-12-14 12:49:52 +11005916 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005917 struct file *f = mddev->bitmap_info.file;
5918 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005919 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005920 spin_unlock(&mddev->lock);
5921 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005922 }
NeilBrownc3d97142009-12-14 12:49:52 +11005923 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005924
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 export_array(mddev);
5926
NeilBrown6177b472010-03-29 11:37:13 +11005927 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11005928 if (mddev->hold_active == UNTIL_STOP)
5929 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005930 }
NeilBrownd7603b72006-01-06 00:20:30 -08005931 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005932 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934}
5935
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005936#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005937static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938{
NeilBrown3cb03002011-10-11 16:45:26 +11005939 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 int err;
5941
NeilBrowna757e642005-04-16 15:26:42 -07005942 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944
NeilBrown9d487392016-11-02 14:16:49 +11005945 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946
NeilBrowndafb20f2012-03-19 12:46:39 +11005947 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11005949 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 }
NeilBrown9d487392016-11-02 14:16:49 +11005951 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
NeilBrownd710e132008-10-13 11:55:12 +11005953 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11005955 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005956 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 }
5958}
5959
5960/*
5961 * lets try to run arrays based on all disks that have arrived
5962 * until now. (those are in pending_raid_disks)
5963 *
5964 * the method: pick the first pending disk, collect all disks with
5965 * the same UUID, remove all from the pending list and put them into
5966 * the 'same_array' list. Then order this list based on superblock
5967 * update time (freshest comes first), kick out 'old' disks and
5968 * compare superblocks. If everything's fine then run it.
5969 *
5970 * If "unit" is allocated, then bump its reference count
5971 */
5972static void autorun_devices(int part)
5973{
NeilBrown3cb03002011-10-11 16:45:26 +11005974 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005975 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 char b[BDEVNAME_SIZE];
5977
NeilBrown9d487392016-11-02 14:16:49 +11005978 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005980 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005982 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005984 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985
NeilBrown9d487392016-11-02 14:16:49 +11005986 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005988 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005990 pr_debug("md: adding %s ...\n",
5991 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 list_move(&rdev->same_set, &candidates);
5993 }
5994 /*
5995 * now we have a set of devices, with all of them having
5996 * mostly sane superblocks. It's time to allocate the
5997 * mddev.
5998 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005999 if (part) {
6000 dev = MKDEV(mdp_major,
6001 rdev0->preferred_minor << MdpMinorShift);
6002 unit = MINOR(dev) >> MdpMinorShift;
6003 } else {
6004 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6005 unit = MINOR(dev);
6006 }
6007 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006008 pr_warn("md: unit number in %s is bad: %d\n",
6009 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 break;
6011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012
6013 md_probe(dev, NULL, NULL);
6014 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006015 if (!mddev || !mddev->gendisk) {
6016 if (mddev)
6017 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 break;
6019 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006020 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006021 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 else if (mddev->raid_disks || mddev->major_version
6023 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006024 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 mdname(mddev), bdevname(rdev0->bdev,b));
6026 mddev_unlock(mddev);
6027 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006028 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006029 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006030 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 list_del_init(&rdev->same_set);
6032 if (bind_rdev_to_array(rdev, mddev))
6033 export_rdev(rdev);
6034 }
6035 autorun_array(mddev);
6036 mddev_unlock(mddev);
6037 }
6038 /* on success, candidates will be empty, on error
6039 * it won't...
6040 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006041 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006042 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 mddev_put(mddev);
6046 }
NeilBrown9d487392016-11-02 14:16:49 +11006047 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006049#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050
NeilBrownf72ffdd2014-09-30 14:23:59 +10006051static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052{
6053 mdu_version_t ver;
6054
6055 ver.major = MD_MAJOR_VERSION;
6056 ver.minor = MD_MINOR_VERSION;
6057 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6058
6059 if (copy_to_user(arg, &ver, sizeof(ver)))
6060 return -EFAULT;
6061
6062 return 0;
6063}
6064
NeilBrownf72ffdd2014-09-30 14:23:59 +10006065static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066{
6067 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006068 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006069 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
NeilBrown1ca69c42012-10-11 13:37:33 +11006071 nr = working = insync = failed = spare = 0;
6072 rcu_read_lock();
6073 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006075 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 failed++;
6077 else {
6078 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006079 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006080 insync++;
Song Liub347af82016-08-11 17:14:45 -07006081 else if (test_bit(Journal, &rdev->flags))
6082 /* TODO: add journal count to md_u.h */
6083 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 else
6085 spare++;
6086 }
6087 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006088 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089
6090 info.major_version = mddev->major_version;
6091 info.minor_version = mddev->minor_version;
6092 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006093 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006095 info.size = mddev->dev_sectors / 2;
6096 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006097 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 info.nr_disks = nr;
6099 info.raid_disks = mddev->raid_disks;
6100 info.md_minor = mddev->md_minor;
6101 info.not_persistent= !mddev->persistent;
6102
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006103 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 info.state = 0;
6105 if (mddev->in_sync)
6106 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006107 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006108 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006109 if (mddev_is_clustered(mddev))
6110 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006111 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 info.working_disks = working;
6113 info.failed_disks = failed;
6114 info.spare_disks = spare;
6115
6116 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006117 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118
6119 if (copy_to_user(arg, &info, sizeof(info)))
6120 return -EFAULT;
6121
6122 return 0;
6123}
6124
NeilBrownf72ffdd2014-09-30 14:23:59 +10006125static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006126{
6127 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006128 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006129 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006130
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006131 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006132 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006133 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006134
NeilBrown32a76272005-06-21 17:17:14 -07006135 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006136 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006137 /* bitmap enabled */
6138 if (mddev->bitmap_info.file) {
6139 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6140 sizeof(file->pathname));
6141 if (IS_ERR(ptr))
6142 err = PTR_ERR(ptr);
6143 else
6144 memmove(file->pathname, ptr,
6145 sizeof(file->pathname)-(ptr-file->pathname));
6146 }
NeilBrown4af1a042014-12-15 12:57:00 +11006147 spin_unlock(&mddev->lock);
6148
6149 if (err == 0 &&
6150 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006151 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006152
NeilBrown32a76272005-06-21 17:17:14 -07006153 kfree(file);
6154 return err;
6155}
6156
NeilBrownf72ffdd2014-09-30 14:23:59 +10006157static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158{
6159 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006160 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161
6162 if (copy_from_user(&info, arg, sizeof(info)))
6163 return -EFAULT;
6164
NeilBrown1ca69c42012-10-11 13:37:33 +11006165 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006166 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 if (rdev) {
6168 info.major = MAJOR(rdev->bdev->bd_dev);
6169 info.minor = MINOR(rdev->bdev->bd_dev);
6170 info.raid_disk = rdev->raid_disk;
6171 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006172 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006174 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 info.state |= (1<<MD_DISK_ACTIVE);
6176 info.state |= (1<<MD_DISK_SYNC);
6177 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006178 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006179 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006180 if (test_bit(WriteMostly, &rdev->flags))
6181 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006182 if (test_bit(FailFast, &rdev->flags))
6183 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 } else {
6185 info.major = info.minor = 0;
6186 info.raid_disk = -1;
6187 info.state = (1<<MD_DISK_REMOVED);
6188 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006189 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190
6191 if (copy_to_user(arg, &info, sizeof(info)))
6192 return -EFAULT;
6193
6194 return 0;
6195}
6196
NeilBrownf72ffdd2014-09-30 14:23:59 +10006197static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198{
6199 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006200 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 dev_t dev = MKDEV(info->major,info->minor);
6202
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006203 if (mddev_is_clustered(mddev) &&
6204 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006205 pr_warn("%s: Cannot add to clustered mddev.\n",
6206 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006207 return -EINVAL;
6208 }
6209
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6211 return -EOVERFLOW;
6212
6213 if (!mddev->raid_disks) {
6214 int err;
6215 /* expecting a device which has a superblock */
6216 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6217 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006218 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 PTR_ERR(rdev));
6220 return PTR_ERR(rdev);
6221 }
6222 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006223 struct md_rdev *rdev0
6224 = list_entry(mddev->disks.next,
6225 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006226 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 .load_super(rdev, rdev0, mddev->minor_version);
6228 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006229 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006230 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 bdevname(rdev0->bdev,b2));
6232 export_rdev(rdev);
6233 return -EINVAL;
6234 }
6235 }
6236 err = bind_rdev_to_array(rdev, mddev);
6237 if (err)
6238 export_rdev(rdev);
6239 return err;
6240 }
6241
6242 /*
6243 * add_new_disk can be used once the array is assembled
6244 * to add "hot spares". They must already have a superblock
6245 * written
6246 */
6247 if (mddev->pers) {
6248 int err;
6249 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006250 pr_warn("%s: personality does not support diskops!\n",
6251 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 return -EINVAL;
6253 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006254 if (mddev->persistent)
6255 rdev = md_import_device(dev, mddev->major_version,
6256 mddev->minor_version);
6257 else
6258 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006260 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 PTR_ERR(rdev));
6262 return PTR_ERR(rdev);
6263 }
NeilBrown1a855a02010-12-09 16:36:28 +11006264 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006265 if (!mddev->persistent) {
6266 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006267 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006268 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006269 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006270 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006271 } else
NeilBrown41158c72005-06-21 17:17:25 -07006272 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006273 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006274 } else
6275 super_types[mddev->major_version].
6276 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006277 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006278 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006279 /* This was a hot-add request, but events doesn't
6280 * match, so reject it.
6281 */
6282 export_rdev(rdev);
6283 return -EINVAL;
6284 }
6285
NeilBrownb2d444d2005-11-08 21:39:31 -08006286 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006287 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6288 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006289 else
6290 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006291 if (info->state & (1<<MD_DISK_FAILFAST))
6292 set_bit(FailFast, &rdev->flags);
6293 else
6294 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006295
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006296 if (info->state & (1<<MD_DISK_JOURNAL)) {
6297 struct md_rdev *rdev2;
6298 bool has_journal = false;
6299
6300 /* make sure no existing journal disk */
6301 rdev_for_each(rdev2, mddev) {
6302 if (test_bit(Journal, &rdev2->flags)) {
6303 has_journal = true;
6304 break;
6305 }
6306 }
6307 if (has_journal) {
6308 export_rdev(rdev);
6309 return -EBUSY;
6310 }
Song Liubac624f2015-08-13 14:31:55 -07006311 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006312 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006313 /*
6314 * check whether the device shows up in other nodes
6315 */
6316 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006317 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006318 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006319 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006320 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006321 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006322 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006323 export_rdev(rdev);
6324 return err;
6325 }
6326 }
6327 }
6328
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329 rdev->raid_disk = -1;
6330 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006331
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 if (err)
6333 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006334
6335 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006336 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6337 if (!err) {
6338 err = md_cluster_ops->new_disk_ack(mddev,
6339 err == 0);
6340 if (err)
6341 md_kick_rdev_from_array(rdev);
6342 }
6343 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006344 if (err)
6345 md_cluster_ops->add_new_disk_cancel(mddev);
6346 else
6347 err = add_bound_rdev(rdev);
6348 }
6349
6350 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006351 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006352
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 return err;
6354 }
6355
6356 /* otherwise, add_new_disk is only allowed
6357 * for major_version==0 superblocks
6358 */
6359 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006360 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 return -EINVAL;
6362 }
6363
6364 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6365 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006366 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006368 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 PTR_ERR(rdev));
6370 return PTR_ERR(rdev);
6371 }
6372 rdev->desc_nr = info->number;
6373 if (info->raid_disk < mddev->raid_disks)
6374 rdev->raid_disk = info->raid_disk;
6375 else
6376 rdev->raid_disk = -1;
6377
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006379 if (info->state & (1<<MD_DISK_SYNC))
6380 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006382 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6383 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006384 if (info->state & (1<<MD_DISK_FAILFAST))
6385 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006386
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006388 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006389 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6390 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006391 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006392 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393
NeilBrown2bf071b2006-01-06 00:20:55 -08006394 err = bind_rdev_to_array(rdev, mddev);
6395 if (err) {
6396 export_rdev(rdev);
6397 return err;
6398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 }
6400
6401 return 0;
6402}
6403
NeilBrownf72ffdd2014-09-30 14:23:59 +10006404static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405{
6406 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006407 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 rdev = find_rdev(mddev, dev);
6410 if (!rdev)
6411 return -ENXIO;
6412
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006413 if (rdev->raid_disk < 0)
6414 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006415
NeilBrown3ea8929d2013-04-24 11:42:41 +10006416 clear_bit(Blocked, &rdev->flags);
6417 remove_and_add_spares(mddev, rdev);
6418
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 if (rdev->raid_disk >= 0)
6420 goto busy;
6421
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006422kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006423 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006424 md_cluster_ops->remove_disk(mddev, rdev);
6425
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006426 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006427 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006428 if (mddev->thread)
6429 md_wakeup_thread(mddev->thread);
6430 else
6431 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006432 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433
6434 return 0;
6435busy:
NeilBrown9d487392016-11-02 14:16:49 +11006436 pr_debug("md: cannot remove active disk %s from %s ...\n",
6437 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438 return -EBUSY;
6439}
6440
NeilBrownf72ffdd2014-09-30 14:23:59 +10006441static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442{
6443 char b[BDEVNAME_SIZE];
6444 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006445 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446
6447 if (!mddev->pers)
6448 return -ENODEV;
6449
6450 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006451 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 mdname(mddev));
6453 return -EINVAL;
6454 }
6455 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006456 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 mdname(mddev));
6458 return -EINVAL;
6459 }
6460
NeilBrownd710e132008-10-13 11:55:12 +11006461 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006463 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 PTR_ERR(rdev));
6465 return -EINVAL;
6466 }
6467
6468 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006469 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006471 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472
NeilBrown8190e752009-06-18 08:48:58 +10006473 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474
NeilBrownb2d444d2005-11-08 21:39:31 -08006475 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006476 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477 bdevname(rdev->bdev,b), mdname(mddev));
6478 err = -EINVAL;
6479 goto abort_export;
6480 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006481
NeilBrownb2d444d2005-11-08 21:39:31 -08006482 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006484 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006485 err = bind_rdev_to_array(rdev, mddev);
6486 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006487 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488
6489 /*
6490 * The rest should better be atomic, we can have disk failures
6491 * noticed in interrupt contexts ...
6492 */
6493
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494 rdev->raid_disk = -1;
6495
Shaohua Li29530792016-12-08 15:48:19 -08006496 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006497 if (!mddev->thread)
6498 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 /*
6500 * Kick recovery, maybe this spare has to be added to the
6501 * array immediately.
6502 */
6503 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6504 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006505 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 return 0;
6507
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508abort_export:
6509 export_rdev(rdev);
6510 return err;
6511}
6512
NeilBrownfd01b882011-10-11 16:47:53 +11006513static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006514{
NeilBrown035328c2014-04-09 12:25:40 +10006515 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006516
NeilBrown36fa3062005-09-09 16:23:45 -07006517 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006518 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006519 return -EBUSY;
6520 if (mddev->recovery || mddev->sync_thread)
6521 return -EBUSY;
6522 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006523 }
6524
NeilBrown36fa3062005-09-09 16:23:45 -07006525 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006526 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006527 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006528
NeilBrown1e594bb2014-12-15 12:57:00 +11006529 if (mddev->bitmap || mddev->bitmap_info.file)
6530 return -EEXIST; /* cannot add when bitmap is present */
6531 f = fget(fd);
6532
6533 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006534 pr_warn("%s: error: failed to get bitmap file\n",
6535 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006536 return -EBADF;
6537 }
6538
NeilBrown1e594bb2014-12-15 12:57:00 +11006539 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006540 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006541 pr_warn("%s: error: bitmap file must be a regular file\n",
6542 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006543 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006544 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006545 pr_warn("%s: error: bitmap file must open for write\n",
6546 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006547 err = -EBADF;
6548 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006549 pr_warn("%s: error: bitmap file is already in use\n",
6550 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006551 err = -EBUSY;
6552 }
6553 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006554 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006555 return err;
6556 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006557 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006558 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006559 } else if (mddev->bitmap == NULL)
6560 return -ENOENT; /* cannot remove what isn't there */
6561 err = 0;
6562 if (mddev->pers) {
6563 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006564 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006565 struct bitmap *bitmap;
6566
6567 bitmap = bitmap_create(mddev, -1);
6568 if (!IS_ERR(bitmap)) {
6569 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006570 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006571 } else
6572 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006573 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006574 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006575 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006576 fd = -1; /* make sure to put the file */
6577 }
NeilBrown36fa3062005-09-09 16:23:45 -07006578 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006579 }
6580 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006581 struct file *f = mddev->bitmap_info.file;
6582 if (f) {
6583 spin_lock(&mddev->lock);
6584 mddev->bitmap_info.file = NULL;
6585 spin_unlock(&mddev->lock);
6586 fput(f);
6587 }
NeilBrown36fa3062005-09-09 16:23:45 -07006588 }
6589
NeilBrown32a76272005-06-21 17:17:14 -07006590 return err;
6591}
6592
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593/*
6594 * set_array_info is used two different ways
6595 * The original usage is when creating a new array.
6596 * In this usage, raid_disks is > 0 and it together with
6597 * level, size, not_persistent,layout,chunksize determine the
6598 * shape of the array.
6599 * This will always create an array with a type-0.90.0 superblock.
6600 * The newer usage is when assembling an array.
6601 * In this case raid_disks will be 0, and the major_version field is
6602 * use to determine which style super-blocks are to be found on the devices.
6603 * The minor and patch _version numbers are also kept incase the
6604 * super_block handler wishes to interpret them.
6605 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006606static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607{
6608
6609 if (info->raid_disks == 0) {
6610 /* just setting version number for superblock loading */
6611 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006612 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 super_types[info->major_version].name == NULL) {
6614 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11006615 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 info->major_version);
6617 return -EINVAL;
6618 }
6619 mddev->major_version = info->major_version;
6620 mddev->minor_version = info->minor_version;
6621 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006622 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006623 /* ensure mddev_put doesn't delete this now that there
6624 * is some minimal configuration.
6625 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006626 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627 return 0;
6628 }
6629 mddev->major_version = MD_MAJOR_VERSION;
6630 mddev->minor_version = MD_MINOR_VERSION;
6631 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006632 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633
6634 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006635 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006636 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 mddev->raid_disks = info->raid_disks;
6638 /* don't set md_minor, it is determined by which /dev/md* was
6639 * openned
6640 */
6641 if (info->state & (1<<MD_SB_CLEAN))
6642 mddev->recovery_cp = MaxSector;
6643 else
6644 mddev->recovery_cp = 0;
6645 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006646 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647
6648 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006649 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650
Shaohua Li29530792016-12-08 15:48:19 -08006651 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11006652 mddev->max_disks = MD_SB_DISKS;
6653 mddev->flags = 0;
6654 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006655 }
6656 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657
NeilBrownc3d97142009-12-14 12:49:52 +11006658 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006659 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006660 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006661
NeilBrownf6705572006-03-27 01:18:11 -08006662 mddev->reshape_position = MaxSector;
6663
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 /*
6665 * Generate a 128 bit UUID
6666 */
6667 get_random_bytes(mddev->uuid, 16);
6668
NeilBrownf6705572006-03-27 01:18:11 -08006669 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006670 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006671 mddev->new_layout = mddev->layout;
6672 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006673 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006674
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675 return 0;
6676}
6677
NeilBrownfd01b882011-10-11 16:47:53 +11006678void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006679{
Dan Williamsb522adc2009-03-31 15:00:31 +11006680 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6681
6682 if (mddev->external_size)
6683 return;
6684
Dan Williams1f403622009-03-31 14:59:03 +11006685 mddev->array_sectors = array_sectors;
6686}
6687EXPORT_SYMBOL(md_set_array_sectors);
6688
NeilBrownfd01b882011-10-11 16:47:53 +11006689static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006690{
NeilBrown3cb03002011-10-11 16:45:26 +11006691 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006692 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006693 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08006694 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006695
NeilBrowna35b0d62006-01-06 00:20:49 -08006696 if (mddev->pers->resize == NULL)
6697 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006698 /* The "num_sectors" is the number of sectors of each device that
6699 * is used. This can only make sense for arrays with redundancy.
6700 * linear and raid0 always use whatever space is available. We can only
6701 * consider changing this number if no resync or reconstruction is
6702 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006703 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006704 * of each device. If num_sectors is zero, we find the largest size
6705 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006706 */
NeilBrownf851b602014-12-11 10:02:10 +11006707 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6708 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006709 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006710 if (mddev->ro)
6711 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006712
NeilBrowndafb20f2012-03-19 12:46:39 +11006713 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006714 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006715
Andre Nolld71f9f82008-07-11 22:02:22 +10006716 if (fit && (num_sectors == 0 || num_sectors > avail))
6717 num_sectors = avail;
6718 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006719 return -ENOSPC;
6720 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006721 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08006722 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08006723 if (mddev_is_clustered(mddev))
6724 md_cluster_ops->update_size(mddev, old_dev_sectors);
6725 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08006726 set_capacity(mddev->gendisk, mddev->array_sectors);
6727 revalidate_disk(mddev->gendisk);
6728 }
6729 }
NeilBrowna35b0d62006-01-06 00:20:49 -08006730 return rv;
6731}
6732
NeilBrownfd01b882011-10-11 16:47:53 +11006733static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006734{
6735 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006736 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006737 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006738 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006739 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006740 if (mddev->ro)
6741 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006742 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006743 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006744 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006745 if (mddev->sync_thread ||
6746 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6747 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006748 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006749
6750 rdev_for_each(rdev, mddev) {
6751 if (mddev->raid_disks < raid_disks &&
6752 rdev->data_offset < rdev->new_data_offset)
6753 return -EINVAL;
6754 if (mddev->raid_disks > raid_disks &&
6755 rdev->data_offset > rdev->new_data_offset)
6756 return -EINVAL;
6757 }
6758
NeilBrown63c70c42006-03-27 01:18:13 -08006759 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006760 if (mddev->delta_disks < 0)
6761 mddev->reshape_backwards = 1;
6762 else if (mddev->delta_disks > 0)
6763 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006764
6765 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006766 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006767 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006768 mddev->reshape_backwards = 0;
6769 }
NeilBrownda943b992006-01-06 00:20:54 -08006770 return rv;
6771}
6772
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773/*
6774 * update_array_info is used to change the configuration of an
6775 * on-line array.
6776 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6777 * fields in the info are checked against the array.
6778 * Any differences that cannot be handled will cause an error.
6779 * Normally, only one change can be managed at a time.
6780 */
NeilBrownfd01b882011-10-11 16:47:53 +11006781static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782{
6783 int rv = 0;
6784 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006785 int state = 0;
6786
6787 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006788 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006789 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790
6791 if (mddev->major_version != info->major_version ||
6792 mddev->minor_version != info->minor_version ||
6793/* mddev->patch_version != info->patch_version || */
6794 mddev->ctime != info->ctime ||
6795 mddev->level != info->level ||
6796/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006797 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006798 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006799 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6800 ((state^info->state) & 0xfffffe00)
6801 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802 return -EINVAL;
6803 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006804 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6805 cnt++;
6806 if (mddev->raid_disks != info->raid_disks)
6807 cnt++;
6808 if (mddev->layout != info->layout)
6809 cnt++;
6810 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6811 cnt++;
6812 if (cnt == 0)
6813 return 0;
6814 if (cnt > 1)
6815 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816
6817 if (mddev->layout != info->layout) {
6818 /* Change layout
6819 * we don't need to do anything at the md level, the
6820 * personality will take care of it all.
6821 */
NeilBrown50ac1682009-06-18 08:47:55 +10006822 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006824 else {
6825 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006826 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006827 if (rv)
6828 mddev->new_layout = mddev->layout;
6829 return rv;
6830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006832 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006833 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834
NeilBrownda943b992006-01-06 00:20:54 -08006835 if (mddev->raid_disks != info->raid_disks)
6836 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837
NeilBrown36fa3062005-09-09 16:23:45 -07006838 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006839 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6840 rv = -EINVAL;
6841 goto err;
6842 }
6843 if (mddev->recovery || mddev->sync_thread) {
6844 rv = -EBUSY;
6845 goto err;
6846 }
NeilBrown36fa3062005-09-09 16:23:45 -07006847 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006848 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006849 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006850 if (mddev->bitmap) {
6851 rv = -EEXIST;
6852 goto err;
6853 }
6854 if (mddev->bitmap_info.default_offset == 0) {
6855 rv = -EINVAL;
6856 goto err;
6857 }
NeilBrownc3d97142009-12-14 12:49:52 +11006858 mddev->bitmap_info.offset =
6859 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006860 mddev->bitmap_info.space =
6861 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006862 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006863 bitmap = bitmap_create(mddev, -1);
6864 if (!IS_ERR(bitmap)) {
6865 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006866 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006867 } else
6868 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006869 if (rv)
6870 bitmap_destroy(mddev);
6871 mddev->pers->quiesce(mddev, 0);
6872 } else {
6873 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006874 if (!mddev->bitmap) {
6875 rv = -ENOENT;
6876 goto err;
6877 }
6878 if (mddev->bitmap->storage.file) {
6879 rv = -EINVAL;
6880 goto err;
6881 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006882 if (mddev->bitmap_info.nodes) {
6883 /* hold PW on all the bitmap lock */
6884 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006885 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 +11006886 rv = -EPERM;
6887 md_cluster_ops->unlock_all_bitmaps(mddev);
6888 goto err;
6889 }
6890
6891 mddev->bitmap_info.nodes = 0;
6892 md_cluster_ops->leave(mddev);
6893 }
NeilBrown36fa3062005-09-09 16:23:45 -07006894 mddev->pers->quiesce(mddev, 1);
6895 bitmap_destroy(mddev);
6896 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006897 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006898 }
6899 }
NeilBrown850b2b422006-10-03 01:15:46 -07006900 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006901 return rv;
6902err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 return rv;
6904}
6905
NeilBrownfd01b882011-10-11 16:47:53 +11006906static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907{
NeilBrown3cb03002011-10-11 16:45:26 +11006908 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006909 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910
6911 if (mddev->pers == NULL)
6912 return -ENODEV;
6913
NeilBrown1ca69c42012-10-11 13:37:33 +11006914 rcu_read_lock();
6915 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006917 err = -ENODEV;
6918 else {
6919 md_error(mddev, rdev);
6920 if (!test_bit(Faulty, &rdev->flags))
6921 err = -EBUSY;
6922 }
6923 rcu_read_unlock();
6924 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925}
6926
Andre Noll2f9618c2008-04-25 18:57:58 +02006927/*
6928 * We have a problem here : there is no easy way to give a CHS
6929 * virtual geometry. We currently pretend that we have a 2 heads
6930 * 4 sectors (with a BIG number of cylinders...). This drives
6931 * dosfs just mad... ;-)
6932 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006933static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6934{
NeilBrownfd01b882011-10-11 16:47:53 +11006935 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006936
6937 geo->heads = 2;
6938 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006939 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006940 return 0;
6941}
6942
Nicolas Schichancb335f82014-01-15 16:58:52 +01006943static inline bool md_ioctl_valid(unsigned int cmd)
6944{
6945 switch (cmd) {
6946 case ADD_NEW_DISK:
6947 case BLKROSET:
6948 case GET_ARRAY_INFO:
6949 case GET_BITMAP_FILE:
6950 case GET_DISK_INFO:
6951 case HOT_ADD_DISK:
6952 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006953 case RAID_AUTORUN:
6954 case RAID_VERSION:
6955 case RESTART_ARRAY_RW:
6956 case RUN_ARRAY:
6957 case SET_ARRAY_INFO:
6958 case SET_BITMAP_FILE:
6959 case SET_DISK_FAULTY:
6960 case STOP_ARRAY:
6961 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006962 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006963 return true;
6964 default:
6965 return false;
6966 }
6967}
6968
Al Viroa39907f2008-03-02 10:31:15 -05006969static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 unsigned int cmd, unsigned long arg)
6971{
6972 int err = 0;
6973 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006974 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006975 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08006976 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977
Nicolas Schichancb335f82014-01-15 16:58:52 +01006978 if (!md_ioctl_valid(cmd))
6979 return -ENOTTY;
6980
NeilBrown506c9e42011-12-23 10:17:26 +11006981 switch (cmd) {
6982 case RAID_VERSION:
6983 case GET_ARRAY_INFO:
6984 case GET_DISK_INFO:
6985 break;
6986 default:
6987 if (!capable(CAP_SYS_ADMIN))
6988 return -EACCES;
6989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990
6991 /*
6992 * Commands dealing with the RAID driver but not any
6993 * particular array:
6994 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006995 switch (cmd) {
6996 case RAID_VERSION:
6997 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006998 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11007001 case RAID_AUTORUN:
7002 err = 0;
7003 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007004 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11007006 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 }
7008
7009 /*
7010 * Commands creating/starting a new array:
7011 */
7012
Al Viroa39907f2008-03-02 10:31:15 -05007013 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014
7015 if (!mddev) {
7016 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007017 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018 }
7019
NeilBrown1ca69c42012-10-11 13:37:33 +11007020 /* Some actions do not requires the mutex */
7021 switch (cmd) {
7022 case GET_ARRAY_INFO:
7023 if (!mddev->raid_disks && !mddev->external)
7024 err = -ENODEV;
7025 else
7026 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007027 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007028
7029 case GET_DISK_INFO:
7030 if (!mddev->raid_disks && !mddev->external)
7031 err = -ENODEV;
7032 else
7033 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007034 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007035
7036 case SET_DISK_FAULTY:
7037 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007038 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007039
7040 case GET_BITMAP_FILE:
7041 err = get_bitmap_file(mddev, argp);
7042 goto out;
7043
NeilBrown1ca69c42012-10-11 13:37:33 +11007044 }
7045
NeilBrowna7a3f082012-12-11 13:35:54 +11007046 if (cmd == ADD_NEW_DISK)
7047 /* need to ensure md_delayed_delete() has completed */
7048 flush_workqueue(md_misc_wq);
7049
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007050 if (cmd == HOT_REMOVE_DISK)
7051 /* need to ensure recovery thread has run */
7052 wait_event_interruptible_timeout(mddev->sb_wait,
7053 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007054 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007055 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007056 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7057 /* Need to flush page cache, and ensure no-one else opens
7058 * and writes
7059 */
7060 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007061 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007062 mutex_unlock(&mddev->open_mutex);
7063 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007064 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007065 }
NeilBrown065e5192017-04-06 11:16:33 +08007066 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007067 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007068 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007069 mutex_unlock(&mddev->open_mutex);
7070 sync_blockdev(bdev);
7071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072 err = mddev_lock(mddev);
7073 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007074 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7075 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007076 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077 }
7078
NeilBrownc02c0ae2012-12-11 13:39:21 +11007079 if (cmd == SET_ARRAY_INFO) {
7080 mdu_array_info_t info;
7081 if (!arg)
7082 memset(&info, 0, sizeof(info));
7083 else if (copy_from_user(&info, argp, sizeof(info))) {
7084 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007085 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007086 }
7087 if (mddev->pers) {
7088 err = update_array_info(mddev, &info);
7089 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007090 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007091 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007093 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007094 }
7095 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007096 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007097 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007098 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007099 }
7100 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007101 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007102 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007103 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007104 }
7105 err = set_array_info(mddev, &info);
7106 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007107 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007108 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007109 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007110 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111 }
7112
7113 /*
7114 * Commands querying/configuring an existing array:
7115 */
NeilBrown32a76272005-06-21 17:17:14 -07007116 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007117 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007118 if ((!mddev->raid_disks && !mddev->external)
7119 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7120 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7121 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007123 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 }
7125
7126 /*
7127 * Commands even a read-only array can execute:
7128 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007129 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007130 case RESTART_ARRAY_RW:
7131 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007132 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007133
7134 case STOP_ARRAY:
7135 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007136 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007137
7138 case STOP_ARRAY_RO:
7139 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007140 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007141
NeilBrown3ea8929d2013-04-24 11:42:41 +10007142 case HOT_REMOVE_DISK:
7143 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007144 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007145
NeilBrown7ceb17e2013-04-24 11:42:42 +10007146 case ADD_NEW_DISK:
7147 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007148 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007149 * So require mddev->pers and MD_DISK_SYNC.
7150 */
7151 if (mddev->pers) {
7152 mdu_disk_info_t info;
7153 if (copy_from_user(&info, argp, sizeof(info)))
7154 err = -EFAULT;
7155 else if (!(info.state & (1<<MD_DISK_SYNC)))
7156 /* Need to clear read-only for this */
7157 break;
7158 else
7159 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007160 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007161 }
7162 break;
7163
NeilBrownc02c0ae2012-12-11 13:39:21 +11007164 case BLKROSET:
7165 if (get_user(ro, (int __user *)(arg))) {
7166 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007167 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007168 }
7169 err = -EINVAL;
7170
7171 /* if the bdev is going readonly the value of mddev->ro
7172 * does not matter, no writes are coming
7173 */
7174 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007175 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007176
NeilBrownc02c0ae2012-12-11 13:39:21 +11007177 /* are we are already prepared for writes? */
7178 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007179 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007180
7181 /* transitioning to readauto need only happen for
7182 * arrays that call md_write_start
7183 */
7184 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007186 if (err == 0) {
7187 mddev->ro = 2;
7188 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007189 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007190 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007191 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192 }
7193
7194 /*
7195 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007196 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 */
NeilBrown326eb172014-09-30 15:36:28 +10007198 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007199 if (mddev->ro == 2) {
7200 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007201 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007202 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007203 /* mddev_unlock will wake thread */
7204 /* If a device failed while we were read-only, we
7205 * need to make sure the metadata is updated now.
7206 */
Shaohua Li29530792016-12-08 15:48:19 -08007207 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007208 mddev_unlock(mddev);
7209 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007210 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7211 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007212 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007213 }
NeilBrownf91de922005-11-08 21:39:36 -08007214 } else {
7215 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007216 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 }
7219
NeilBrownc02c0ae2012-12-11 13:39:21 +11007220 switch (cmd) {
7221 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007223 mdu_disk_info_t info;
7224 if (copy_from_user(&info, argp, sizeof(info)))
7225 err = -EFAULT;
7226 else
7227 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007228 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007231 case CLUSTERED_DISK_NACK:
7232 if (mddev_is_clustered(mddev))
7233 md_cluster_ops->new_disk_ack(mddev, false);
7234 else
7235 err = -EINVAL;
7236 goto unlock;
7237
NeilBrownc02c0ae2012-12-11 13:39:21 +11007238 case HOT_ADD_DISK:
7239 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007240 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241
NeilBrownc02c0ae2012-12-11 13:39:21 +11007242 case RUN_ARRAY:
7243 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007244 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245
NeilBrownc02c0ae2012-12-11 13:39:21 +11007246 case SET_BITMAP_FILE:
7247 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007248 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007249
NeilBrownc02c0ae2012-12-11 13:39:21 +11007250 default:
7251 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007252 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 }
7254
NeilBrown3adc28d2014-09-30 15:46:41 +10007255unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007256 if (mddev->hold_active == UNTIL_IOCTL &&
7257 err != -EINVAL)
7258 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007260out:
NeilBrown065e5192017-04-06 11:16:33 +08007261 if(did_set_md_closing)
7262 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263 return err;
7264}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007265#ifdef CONFIG_COMPAT
7266static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7267 unsigned int cmd, unsigned long arg)
7268{
7269 switch (cmd) {
7270 case HOT_REMOVE_DISK:
7271 case HOT_ADD_DISK:
7272 case SET_DISK_FAULTY:
7273 case SET_BITMAP_FILE:
7274 /* These take in integer arg, do not convert */
7275 break;
7276 default:
7277 arg = (unsigned long)compat_ptr(arg);
7278 break;
7279 }
7280
7281 return md_ioctl(bdev, mode, cmd, arg);
7282}
7283#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284
Al Viroa39907f2008-03-02 10:31:15 -05007285static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286{
7287 /*
7288 * Succeed if we can lock the mddev, which confirms that
7289 * it isn't being stopped right now.
7290 */
NeilBrownfd01b882011-10-11 16:47:53 +11007291 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292 int err;
7293
Yuanhan Liu0c098222012-05-22 13:55:32 +10007294 if (!mddev)
7295 return -ENODEV;
7296
NeilBrownd3374822009-01-09 08:31:10 +11007297 if (mddev->gendisk != bdev->bd_disk) {
7298 /* we are racing with mddev_put which is discarding this
7299 * bd_disk.
7300 */
7301 mddev_put(mddev);
7302 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007303 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007304 /* Then retry the open from the top */
7305 return -ERESTARTSYS;
7306 }
7307 BUG_ON(mddev != bdev->bd_disk->private_data);
7308
NeilBrownc8c00a62009-08-10 12:50:52 +10007309 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 goto out;
7311
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007312 if (test_bit(MD_CLOSING, &mddev->flags)) {
7313 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007314 err = -ENODEV;
7315 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007316 }
7317
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007319 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007320 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007321
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007322 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007324 if (err)
7325 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326 return err;
7327}
7328
Al Virodb2a1442013-05-05 21:52:57 -04007329static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007331 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007333 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007334 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007337
7338static int md_media_changed(struct gendisk *disk)
7339{
NeilBrownfd01b882011-10-11 16:47:53 +11007340 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007341
7342 return mddev->changed;
7343}
7344
7345static int md_revalidate(struct gendisk *disk)
7346{
NeilBrownfd01b882011-10-11 16:47:53 +11007347 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007348
7349 mddev->changed = 0;
7350 return 0;
7351}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007352static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353{
7354 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007355 .open = md_open,
7356 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007357 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007358#ifdef CONFIG_COMPAT
7359 .compat_ioctl = md_compat_ioctl,
7360#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007361 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007362 .media_changed = md_media_changed,
7363 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364};
7365
NeilBrownf72ffdd2014-09-30 14:23:59 +10007366static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367{
NeilBrown2b8bf342011-10-11 16:48:23 +11007368 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370 /*
7371 * md_thread is a 'system-thread', it's priority should be very
7372 * high. We avoid resource deadlocks individually in each
7373 * raid personality. (RAID5 does preallocation) We also use RR and
7374 * the very same RT priority as kswapd, thus we will never get
7375 * into a priority inversion deadlock.
7376 *
7377 * we definitely have to have equal or higher priority than
7378 * bdflush, otherwise bdflush will deadlock if there are too
7379 * many dirty RAID5 blocks.
7380 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381
NeilBrown6985c432005-10-19 21:23:47 -07007382 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007383 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384
NeilBrown93588e22005-11-15 00:09:12 -08007385 /* We need to wait INTERRUPTIBLE so that
7386 * we don't add to the load-average.
7387 * That means we need to be sure no signals are
7388 * pending
7389 */
7390 if (signal_pending(current))
7391 flush_signals(current);
7392
7393 wait_event_interruptible_timeout
7394 (thread->wqueue,
7395 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007396 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007397 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398
NeilBrown6c987912011-01-14 09:13:53 +11007399 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007400 if (kthread_should_park())
7401 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007402 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007403 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007405
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406 return 0;
7407}
7408
NeilBrown2b8bf342011-10-11 16:48:23 +11007409void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410{
7411 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007412 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
NeilBrown4ad23a972017-03-15 14:05:14 +11007413 if (!test_and_set_bit(THREAD_WAKEUP, &thread->flags))
7414 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415 }
7416}
NeilBrown6c144d32014-09-30 16:15:38 +10007417EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418
Shaohua Li4ed87312012-10-11 13:34:00 +11007419struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7420 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421{
NeilBrown2b8bf342011-10-11 16:48:23 +11007422 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423
NeilBrown2b8bf342011-10-11 16:48:23 +11007424 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425 if (!thread)
7426 return NULL;
7427
Linus Torvalds1da177e2005-04-16 15:20:36 -07007428 init_waitqueue_head(&thread->wqueue);
7429
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430 thread->run = run;
7431 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007432 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007433 thread->tsk = kthread_run(md_thread, thread,
7434 "%s_%s",
7435 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007436 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007437 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438 kfree(thread);
7439 return NULL;
7440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441 return thread;
7442}
NeilBrown6c144d32014-09-30 16:15:38 +10007443EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444
NeilBrown2b8bf342011-10-11 16:48:23 +11007445void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446{
NeilBrown2b8bf342011-10-11 16:48:23 +11007447 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007448 if (!thread)
7449 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007450 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007451 /* Locking ensures that mddev_unlock does not wake_up a
7452 * non-existent thread
7453 */
7454 spin_lock(&pers_lock);
7455 *threadp = NULL;
7456 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007457
7458 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 kfree(thread);
7460}
NeilBrown6c144d32014-09-30 16:15:38 +10007461EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462
NeilBrownfd01b882011-10-11 16:47:53 +11007463void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464{
NeilBrownb2d444d2005-11-08 21:39:31 -08007465 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007467
NeilBrownde393cd2011-07-28 11:31:48 +10007468 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 return;
7470 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007471 if (mddev->degraded)
7472 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007473 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7475 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7476 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007477 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007478 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007479 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480}
NeilBrown6c144d32014-09-30 16:15:38 +10007481EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482
7483/* seq_file implementation /proc/mdstat */
7484
7485static void status_unused(struct seq_file *seq)
7486{
7487 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007488 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489
7490 seq_printf(seq, "unused devices: ");
7491
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007492 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 char b[BDEVNAME_SIZE];
7494 i++;
7495 seq_printf(seq, "%s ",
7496 bdevname(rdev->bdev,b));
7497 }
7498 if (!i)
7499 seq_printf(seq, "<none>");
7500
7501 seq_printf(seq, "\n");
7502}
7503
NeilBrownf7851be2015-07-02 17:12:58 +10007504static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505{
NeilBrowndd71cf62009-05-07 12:49:35 +10007506 sector_t max_sectors, resync, res;
7507 unsigned long dt, db;
7508 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007509 int scale;
7510 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511
NeilBrownc804cde2012-05-21 09:28:33 +10007512 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7513 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007514 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007516 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007517
NeilBrownf7851be2015-07-02 17:12:58 +10007518 resync = mddev->curr_resync;
7519 if (resync <= 3) {
7520 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7521 /* Still cleaning up */
7522 resync = max_sectors;
7523 } else
7524 resync -= atomic_read(&mddev->recovery_active);
7525
7526 if (resync == 0) {
7527 if (mddev->recovery_cp < MaxSector) {
7528 seq_printf(seq, "\tresync=PENDING");
7529 return 1;
7530 }
7531 return 0;
7532 }
7533 if (resync < 3) {
7534 seq_printf(seq, "\tresync=DELAYED");
7535 return 1;
7536 }
7537
NeilBrown403df472014-09-30 15:52:29 +10007538 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007539 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007540 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007541 * u32, as those are the requirements for sector_div.
7542 * Thus 'scale' must be at least 10
7543 */
7544 scale = 10;
7545 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007546 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007547 scale++;
7548 }
7549 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007550 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007551
7552 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553 {
NeilBrown4588b422006-03-27 01:18:04 -08007554 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555 seq_printf(seq, "[");
7556 for (i = 0; i < x; i++)
7557 seq_printf(seq, "=");
7558 seq_printf(seq, ">");
7559 for (i = 0; i < y; i++)
7560 seq_printf(seq, ".");
7561 seq_printf(seq, "] ");
7562 }
NeilBrown4588b422006-03-27 01:18:04 -08007563 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007564 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7565 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007566 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7567 "check" :
7568 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7569 "resync" : "recovery"))),
7570 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007571 (unsigned long long) resync/2,
7572 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007573
7574 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575 * dt: time from mark until now
7576 * db: blocks written from mark until now
7577 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007578 *
7579 * rt is a sector_t, so could be 32bit or 64bit.
7580 * So we divide before multiply in case it is 32bit and close
7581 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007582 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007583 * near the end of resync when the number of remaining sectors
7584 * is close to 'db'.
7585 * We then divide rt by 32 after multiplying by db to compensate.
7586 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 */
7588 dt = ((jiffies - mddev->resync_mark) / HZ);
7589 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007590 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7591 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592
NeilBrowndd71cf62009-05-07 12:49:35 +10007593 rt = max_sectors - resync; /* number of remaining sectors */
7594 sector_div(rt, db/32+1);
7595 rt *= dt;
7596 rt >>= 5;
7597
7598 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7599 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007600
NeilBrownff4e8d92006-07-10 04:44:16 -07007601 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007602 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603}
7604
7605static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7606{
7607 struct list_head *tmp;
7608 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007609 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610
7611 if (l >= 0x10000)
7612 return NULL;
7613 if (!l--)
7614 /* header */
7615 return (void*)1;
7616
7617 spin_lock(&all_mddevs_lock);
7618 list_for_each(tmp,&all_mddevs)
7619 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007620 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621 mddev_get(mddev);
7622 spin_unlock(&all_mddevs_lock);
7623 return mddev;
7624 }
7625 spin_unlock(&all_mddevs_lock);
7626 if (!l--)
7627 return (void*)2;/* tail */
7628 return NULL;
7629}
7630
7631static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7632{
7633 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007634 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007635
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636 ++*pos;
7637 if (v == (void*)2)
7638 return NULL;
7639
7640 spin_lock(&all_mddevs_lock);
7641 if (v == (void*)1)
7642 tmp = all_mddevs.next;
7643 else
7644 tmp = mddev->all_mddevs.next;
7645 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007646 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647 else {
7648 next_mddev = (void*)2;
7649 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651 spin_unlock(&all_mddevs_lock);
7652
7653 if (v != (void*)1)
7654 mddev_put(mddev);
7655 return next_mddev;
7656
7657}
7658
7659static void md_seq_stop(struct seq_file *seq, void *v)
7660{
NeilBrownfd01b882011-10-11 16:47:53 +11007661 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007662
7663 if (mddev && v != (void*)1 && v != (void*)2)
7664 mddev_put(mddev);
7665}
7666
7667static int md_seq_show(struct seq_file *seq, void *v)
7668{
NeilBrownfd01b882011-10-11 16:47:53 +11007669 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007670 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007671 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672
7673 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007674 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007675 seq_printf(seq, "Personalities : ");
7676 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007677 list_for_each_entry(pers, &pers_list, list)
7678 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679
7680 spin_unlock(&pers_lock);
7681 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007682 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683 return 0;
7684 }
7685 if (v == (void*)2) {
7686 status_unused(seq);
7687 return 0;
7688 }
7689
NeilBrown36d091f2014-12-15 12:56:58 +11007690 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007691 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7692 seq_printf(seq, "%s : %sactive", mdname(mddev),
7693 mddev->pers ? "" : "in");
7694 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007695 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007697 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007698 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699 seq_printf(seq, " %s", mddev->pers->name);
7700 }
7701
Andre Nolldd8ac332009-03-31 14:33:13 +11007702 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007703 rcu_read_lock();
7704 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705 char b[BDEVNAME_SIZE];
7706 seq_printf(seq, " %s[%d]",
7707 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007708 if (test_bit(WriteMostly, &rdev->flags))
7709 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007710 if (test_bit(Journal, &rdev->flags))
7711 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007712 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713 seq_printf(seq, "(F)");
7714 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007715 }
7716 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007717 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007718 if (test_bit(Replacement, &rdev->flags))
7719 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007720 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007721 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007722 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723
7724 if (!list_empty(&mddev->disks)) {
7725 if (mddev->pers)
7726 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007727 (unsigned long long)
7728 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729 else
7730 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007731 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007732 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007733 if (mddev->persistent) {
7734 if (mddev->major_version != 0 ||
7735 mddev->minor_version != 90) {
7736 seq_printf(seq," super %d.%d",
7737 mddev->major_version,
7738 mddev->minor_version);
7739 }
NeilBrowne6910632008-02-06 01:39:51 -08007740 } else if (mddev->external)
7741 seq_printf(seq, " super external:%s",
7742 mddev->metadata_type);
7743 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007744 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745
7746 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007747 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007748 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007749 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007750 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007751 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007752 }
NeilBrown32a76272005-06-21 17:17:14 -07007753 } else
7754 seq_printf(seq, "\n ");
7755
NeilBrown57148962012-03-19 12:46:40 +11007756 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757
7758 seq_printf(seq, "\n");
7759 }
NeilBrown36d091f2014-12-15 12:56:58 +11007760 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007761
Linus Torvalds1da177e2005-04-16 15:20:36 -07007762 return 0;
7763}
7764
Jan Engelhardt110518b2009-05-07 12:49:37 +10007765static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766 .start = md_seq_start,
7767 .next = md_seq_next,
7768 .stop = md_seq_stop,
7769 .show = md_seq_show,
7770};
7771
7772static int md_seq_open(struct inode *inode, struct file *file)
7773{
Kay Sieversf1514632011-07-12 20:48:39 +02007774 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007775 int error;
7776
7777 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007778 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007779 return error;
7780
7781 seq = file->private_data;
7782 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783 return error;
7784}
7785
NeilBrowne2f23b62014-04-09 14:33:51 +10007786static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007787static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7788{
Kay Sieversf1514632011-07-12 20:48:39 +02007789 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007790 int mask;
7791
NeilBrowne2f23b62014-04-09 14:33:51 +10007792 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007793 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007794 poll_wait(filp, &md_event_waiters, wait);
7795
7796 /* always allow read */
7797 mask = POLLIN | POLLRDNORM;
7798
Kay Sieversf1514632011-07-12 20:48:39 +02007799 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007800 mask |= POLLERR | POLLPRI;
7801 return mask;
7802}
7803
Arjan van de Venfa027c22007-02-12 00:55:33 -08007804static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007805 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007806 .open = md_seq_open,
7807 .read = seq_read,
7808 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007809 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007810 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811};
7812
NeilBrown84fc4b52011-10-11 16:49:58 +11007813int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007814{
NeilBrown9d487392016-11-02 14:16:49 +11007815 pr_debug("md: %s personality registered for level %d\n",
7816 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007817 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007818 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007819 spin_unlock(&pers_lock);
7820 return 0;
7821}
NeilBrown6c144d32014-09-30 16:15:38 +10007822EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823
NeilBrown84fc4b52011-10-11 16:49:58 +11007824int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825{
NeilBrown9d487392016-11-02 14:16:49 +11007826 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007828 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829 spin_unlock(&pers_lock);
7830 return 0;
7831}
NeilBrown6c144d32014-09-30 16:15:38 +10007832EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833
NeilBrown6022e752015-08-13 12:32:55 +10007834int register_md_cluster_operations(struct md_cluster_operations *ops,
7835 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007836{
NeilBrown6022e752015-08-13 12:32:55 +10007837 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007838 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007839 if (md_cluster_ops != NULL)
7840 ret = -EALREADY;
7841 else {
7842 md_cluster_ops = ops;
7843 md_cluster_mod = module;
7844 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007845 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007846 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007847}
7848EXPORT_SYMBOL(register_md_cluster_operations);
7849
7850int unregister_md_cluster_operations(void)
7851{
7852 spin_lock(&pers_lock);
7853 md_cluster_ops = NULL;
7854 spin_unlock(&pers_lock);
7855 return 0;
7856}
7857EXPORT_SYMBOL(unregister_md_cluster_operations);
7858
7859int md_setup_cluster(struct mddev *mddev, int nodes)
7860{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007861 if (!md_cluster_ops)
7862 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007863 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007864 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007865 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11007866 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007867 spin_unlock(&pers_lock);
7868 return -ENOENT;
7869 }
7870 spin_unlock(&pers_lock);
7871
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007872 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007873}
7874
7875void md_cluster_stop(struct mddev *mddev)
7876{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007877 if (!md_cluster_ops)
7878 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007879 md_cluster_ops->leave(mddev);
7880 module_put(md_cluster_mod);
7881}
7882
NeilBrownfd01b882011-10-11 16:47:53 +11007883static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007884{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007885 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007887 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888
7889 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007890 rcu_read_lock();
7891 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007893 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7894 (int)part_stat_read(&disk->part0, sectors[1]) -
7895 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007896 /* sync IO will cause sync_io to increase before the disk_stats
7897 * as sync_io is counted when a request starts, and
7898 * disk_stats is counted when it completes.
7899 * So resync activity will cause curr_events to be smaller than
7900 * when there was no such activity.
7901 * non-sync IO will cause disk_stat to increase without
7902 * increasing sync_io so curr_events will (eventually)
7903 * be larger than it was before. Once it becomes
7904 * substantially larger, the test below will cause
7905 * the array to appear non-idle, and resync will slow
7906 * down.
7907 * If there is a lot of outstanding resync activity when
7908 * we set last_event to curr_events, then all that activity
7909 * completing might cause the array to appear non-idle
7910 * and resync will be slowed down even though there might
7911 * not have been non-resync activity. This will only
7912 * happen once though. 'last_events' will soon reflect
7913 * the state where there is little or no outstanding
7914 * resync requests, and further resync activity will
7915 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007916 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007918 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 rdev->last_events = curr_events;
7920 idle = 0;
7921 }
7922 }
NeilBrown4b809912008-07-21 17:05:25 +10007923 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924 return idle;
7925}
7926
NeilBrownfd01b882011-10-11 16:47:53 +11007927void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007928{
7929 /* another "blocks" (512byte) blocks have been synced */
7930 atomic_sub(blocks, &mddev->recovery_active);
7931 wake_up(&mddev->recovery_wait);
7932 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007933 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007934 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935 md_wakeup_thread(mddev->thread);
7936 // stop recovery, signal do_sync ....
7937 }
7938}
NeilBrown6c144d32014-09-30 16:15:38 +10007939EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940
NeilBrown06d91a52005-06-21 17:17:12 -07007941/* md_write_start(mddev, bi)
7942 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007943 * in superblock) before writing, schedule a superblock update
7944 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007945 */
NeilBrownfd01b882011-10-11 16:47:53 +11007946void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007947{
Neil Brown0fd62b82008-06-28 08:31:36 +10007948 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007949 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007950 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007951
NeilBrownf91de922005-11-08 21:39:36 -08007952 BUG_ON(mddev->ro == 1);
7953 if (mddev->ro == 2) {
7954 /* need to switch to read/write */
7955 mddev->ro = 0;
7956 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7957 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007958 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007959 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007960 }
NeilBrown4ad23a972017-03-15 14:05:14 +11007961 rcu_read_lock();
7962 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11007963 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07007964 if (mddev->safemode == 1)
7965 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11007966 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
7967 if (mddev->in_sync || !mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11007968 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007969 if (mddev->in_sync) {
7970 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08007971 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
7972 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007973 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007974 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007975 }
NeilBrown85572d72014-12-15 12:56:56 +11007976 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007977 }
NeilBrown4ad23a972017-03-15 14:05:14 +11007978 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10007979 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007980 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007981 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007982 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007983}
NeilBrown6c144d32014-09-30 16:15:38 +10007984EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985
NeilBrown49728052017-03-15 14:05:12 +11007986/* md_write_inc can only be called when md_write_start() has
7987 * already been called at least once of the current request.
7988 * It increments the counter and is useful when a single request
7989 * is split into several parts. Each part causes an increment and
7990 * so needs a matching md_write_end().
7991 * Unlike md_write_start(), it is safe to call md_write_inc() inside
7992 * a spinlocked region.
7993 */
7994void md_write_inc(struct mddev *mddev, struct bio *bi)
7995{
7996 if (bio_data_dir(bi) != WRITE)
7997 return;
7998 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11007999 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008000}
8001EXPORT_SYMBOL(md_write_inc);
8002
NeilBrownfd01b882011-10-11 16:47:53 +11008003void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008004{
NeilBrown4ad23a972017-03-15 14:05:14 +11008005 percpu_ref_put(&mddev->writes_pending);
8006
8007 if (mddev->safemode == 2)
8008 md_wakeup_thread(mddev->thread);
8009 else if (mddev->safemode_delay)
8010 /* The roundup() ensures this only performs locking once
8011 * every ->safemode_delay jiffies
8012 */
8013 mod_timer(&mddev->safemode_timer,
8014 roundup(jiffies, mddev->safemode_delay) +
8015 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008016}
NeilBrown4ad23a972017-03-15 14:05:14 +11008017
NeilBrown6c144d32014-09-30 16:15:38 +10008018EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008019
NeilBrown2a2275d2007-01-26 00:57:11 -08008020/* md_allow_write(mddev)
8021 * Calling this ensures that the array is marked 'active' so that writes
8022 * may proceed without blocking. It is important to call this before
8023 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8024 * Must be called with mddev_lock held.
8025 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008026void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008027{
8028 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008029 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008030 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008031 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008032 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008033 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008034
NeilBrown85572d72014-12-15 12:56:56 +11008035 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008036 if (mddev->in_sync) {
8037 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008038 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8039 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008040 if (mddev->safemode_delay &&
8041 mddev->safemode == 0)
8042 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008043 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008044 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008045 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008046 /* wait for the dirty state to be recorded in the metadata */
8047 wait_event(mddev->sb_wait,
8048 !test_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags) &&
8049 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008050 } else
NeilBrown85572d72014-12-15 12:56:56 +11008051 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008052}
8053EXPORT_SYMBOL_GPL(md_allow_write);
8054
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055#define SYNC_MARKS 10
8056#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008057#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008058void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059{
Shaohua Li4ed87312012-10-11 13:34:00 +11008060 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008061 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008062 unsigned int currspeed = 0,
8063 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008064 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008065 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008066 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008067 sector_t mark_cnt[SYNC_MARKS];
8068 int last_mark,m;
8069 struct list_head *tmp;
8070 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008071 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008072 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008073 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008074 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008075 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008076
8077 /* just incase thread restarts... */
8078 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
8079 return;
NeilBrown3991b312014-05-28 13:39:23 +10008080 if (mddev->ro) {/* never try to sync a read-only array */
8081 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008082 return;
NeilBrown3991b312014-05-28 13:39:23 +10008083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008084
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008085 if (mddev_is_clustered(mddev)) {
8086 ret = md_cluster_ops->resync_start(mddev);
8087 if (ret)
8088 goto skip;
8089
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008090 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008091 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8092 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8093 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8094 && ((unsigned long long)mddev->curr_resync_completed
8095 < (unsigned long long)mddev->resync_max_sectors))
8096 goto skip;
8097 }
8098
NeilBrown61df9d92006-10-03 01:15:57 -07008099 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008100 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008101 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008102 action = "check";
8103 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008104 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008105 action = "repair";
8106 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008107 desc = "resync";
8108 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8109 desc = "reshape";
8110 else
8111 desc = "recovery";
8112
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008113 mddev->last_sync_action = action ?: desc;
8114
Linus Torvalds1da177e2005-04-16 15:20:36 -07008115 /* we overload curr_resync somewhat here.
8116 * 0 == not engaged in resync at all
8117 * 2 == checking that there is no conflict with another sync
8118 * 1 == like 2, but have yielded to allow conflicting resync to
8119 * commense
8120 * other == active in resync - this many blocks
8121 *
8122 * Before starting a resync we must have set curr_resync to
8123 * 2, and then checked that every "conflicting" array has curr_resync
8124 * less than ours. When we find one that is the same or higher
8125 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8126 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8127 * This will mean we have to start checking from the beginning again.
8128 *
8129 */
8130
8131 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008132 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008133 mddev->curr_resync = 2;
8134
8135 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008136 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008137 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008138 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008139 if (mddev2 == mddev)
8140 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008141 if (!mddev->parallel_resync
8142 && mddev2->curr_resync
8143 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008144 DEFINE_WAIT(wq);
8145 if (mddev < mddev2 && mddev->curr_resync == 2) {
8146 /* arbitrarily yield */
8147 mddev->curr_resync = 1;
8148 wake_up(&resync_wait);
8149 }
8150 if (mddev > mddev2 && mddev->curr_resync == 1)
8151 /* no need to wait here, we can wait the next
8152 * time 'round when curr_resync == 2
8153 */
8154 continue;
NeilBrown97441972008-09-19 11:49:54 +10008155 /* We need to wait 'interruptible' so as not to
8156 * contribute to the load average, and not to
8157 * be caught by 'softlockup'
8158 */
8159 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008160 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008161 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008162 if (mddev2_minor != mddev2->md_minor) {
8163 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008164 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8165 desc, mdname(mddev),
8166 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008169 if (signal_pending(current))
8170 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008171 schedule();
8172 finish_wait(&resync_wait, &wq);
8173 goto try_again;
8174 }
8175 finish_wait(&resync_wait, &wq);
8176 }
8177 }
8178 } while (mddev->curr_resync < 2);
8179
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008180 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008181 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008182 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008183 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008184 */
8185 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008186 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008187 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008188 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8189 j = mddev->resync_min;
8190 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008191 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008192
NeilBrownccfcc3c2006-03-27 01:18:09 -08008193 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10008194 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008195 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008197 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008198 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008199 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008200 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008201 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008202 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008203 !test_bit(Faulty, &rdev->flags) &&
8204 !test_bit(In_sync, &rdev->flags) &&
8205 rdev->recovery_offset < j)
8206 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008207 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008208
8209 /* If there is a bitmap, we need to make sure all
8210 * writes that started before we added a spare
8211 * complete before we start doing a recovery.
8212 * Otherwise the write might complete and (via
8213 * bitmap_endwrite) set a bit in the bitmap after the
8214 * recovery has checked that bit and skipped that
8215 * region.
8216 */
8217 if (mddev->bitmap) {
8218 mddev->pers->quiesce(mddev, 1);
8219 mddev->pers->quiesce(mddev, 0);
8220 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008222
NeilBrown9d487392016-11-02 14:16:49 +11008223 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8224 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8225 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8226 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227
NeilBrowneea1bf32009-03-31 14:27:02 +11008228 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008229
NeilBrown57afd892005-06-21 17:17:13 -07008230 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008231 for (m = 0; m < SYNC_MARKS; m++) {
8232 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008233 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008234 }
8235 last_mark = 0;
8236 mddev->resync_mark = mark[last_mark];
8237 mddev->resync_mark_cnt = mark_cnt[last_mark];
8238
8239 /*
8240 * Tune reconstruction:
8241 */
8242 window = 32*(PAGE_SIZE/512);
NeilBrown9d487392016-11-02 14:16:49 +11008243 pr_debug("md: using %dk window, over a total of %lluk.\n",
8244 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008245
8246 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008247 last_check = 0;
8248
8249 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008250 pr_debug("md: resuming %s of %s from checkpoint.\n",
8251 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008252 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008253 } else
8254 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008255 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008256 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8257 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008258 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259
majianpeng7c2c57c2012-07-03 12:12:26 +10008260 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008261 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008262 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263
NeilBrown57afd892005-06-21 17:17:13 -07008264 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008265
NeilBrown7a91ee12009-05-26 12:57:21 +10008266 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8267 ((mddev->curr_resync > mddev->curr_resync_completed &&
8268 (mddev->curr_resync - mddev->curr_resync_completed)
8269 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008270 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008271 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008272 >= mddev->resync_max - mddev->curr_resync_completed ||
8273 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008274 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008275 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008276 wait_event(mddev->recovery_wait,
8277 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008278 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008279 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8280 j > mddev->recovery_cp)
8281 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008282 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008283 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008284 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008285 }
NeilBrownacb180b2009-04-14 16:28:34 +10008286
NeilBrownc91abf52013-11-19 12:02:01 +11008287 while (j >= mddev->resync_max &&
8288 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008289 /* As this condition is controlled by user-space,
8290 * we can block indefinitely, so use '_interruptible'
8291 * to avoid triggering warnings.
8292 */
8293 flush_signals(current); /* just in case */
8294 wait_event_interruptible(mddev->recovery_wait,
8295 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008296 || test_bit(MD_RECOVERY_INTR,
8297 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008298 }
NeilBrownacb180b2009-04-14 16:28:34 +10008299
NeilBrownc91abf52013-11-19 12:02:01 +11008300 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8301 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008302
NeilBrown09314792015-02-19 16:04:40 +11008303 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008304 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008305 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008306 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008307 }
NeilBrown57afd892005-06-21 17:17:13 -07008308
8309 if (!skipped) { /* actual IO requested */
8310 io_sectors += sectors;
8311 atomic_add(sectors, &mddev->recovery_active);
8312 }
8313
NeilBrowne875ece2011-07-28 11:39:24 +10008314 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8315 break;
8316
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008318 if (j > max_sectors)
8319 /* when skipping, extra large numbers can be returned. */
8320 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008321 if (j > 2)
8322 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008323 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008324 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008325 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008326 * visible in /proc/mdstat
8327 */
8328 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008329
8330 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008331 continue;
8332
NeilBrown57afd892005-06-21 17:17:13 -07008333 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008334 repeat:
8335 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8336 /* step marks */
8337 int next = (last_mark+1) % SYNC_MARKS;
8338
8339 mddev->resync_mark = mark[next];
8340 mddev->resync_mark_cnt = mark_cnt[next];
8341 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008342 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008343 last_mark = next;
8344 }
8345
NeilBrownc91abf52013-11-19 12:02:01 +11008346 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8347 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008348
8349 /*
8350 * this loop exits only if either when we are slower than
8351 * the 'hard' speed limit, or the system was IO-idle for
8352 * a jiffy.
8353 * the system might be non-idle CPU-wise, but we only care
8354 * about not overloading the IO subsystem. (things like an
8355 * e2fsck being done on the RAID array should execute fast)
8356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008357 cond_resched();
8358
Xiao Niac7e50a2014-08-07 09:37:41 -04008359 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8360 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008361 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362
NeilBrown88202a02006-01-06 00:21:36 -08008363 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008364 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008365 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008366 goto repeat;
8367 }
NeilBrownac8fa412015-02-19 16:55:00 +11008368 if (!is_mddev_idle(mddev, 0)) {
8369 /*
8370 * Give other IO more of a chance.
8371 * The faster the devices, the less we wait.
8372 */
8373 wait_event(mddev->recovery_wait,
8374 !atomic_read(&mddev->recovery_active));
8375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008376 }
8377 }
NeilBrown9d487392016-11-02 14:16:49 +11008378 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8379 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8380 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381 /*
8382 * this also signals 'finished resyncing' to md_stop
8383 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008384 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008385 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8386
NeilBrown5ed1df22015-07-24 13:27:08 +10008387 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8388 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008389 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008390 mddev->curr_resync_completed = mddev->curr_resync;
8391 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8392 }
NeilBrown09314792015-02-19 16:04:40 +11008393 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394
NeilBrowndfc70642008-05-23 13:04:39 -07008395 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008396 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008397 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8398 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8399 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008400 pr_debug("md: checkpointing %s of %s.\n",
8401 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008402 if (test_bit(MD_RECOVERY_ERROR,
8403 &mddev->recovery))
8404 mddev->recovery_cp =
8405 mddev->curr_resync_completed;
8406 else
8407 mddev->recovery_cp =
8408 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008409 }
8410 } else
8411 mddev->recovery_cp = MaxSector;
8412 } else {
8413 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8414 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008415 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008416 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008417 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008418 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008419 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008420 !test_bit(Faulty, &rdev->flags) &&
8421 !test_bit(In_sync, &rdev->flags) &&
8422 rdev->recovery_offset < mddev->curr_resync)
8423 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008424 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008426 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008427 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008428 /* set CHANGE_PENDING here since maybe another update is needed,
8429 * so other nodes are informed. It should be harmless for normal
8430 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008431 set_mask_bits(&mddev->sb_flags, 0,
8432 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008433
NeilBrown23da4222014-12-15 12:57:01 +11008434 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008435 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8436 /* We completed so min/max setting can be forgotten if used. */
8437 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8438 mddev->resync_min = 0;
8439 mddev->resync_max = MaxSector;
8440 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8441 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008442 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008444 spin_unlock(&mddev->lock);
8445
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008447 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008448 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008449}
NeilBrown29269552006-03-27 01:18:10 -08008450EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451
NeilBrown746d3202013-04-24 11:42:41 +10008452static int remove_and_add_spares(struct mddev *mddev,
8453 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008454{
NeilBrown3cb03002011-10-11 16:45:26 +11008455 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008456 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008457 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008458 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008459
NeilBrownd787be42016-06-02 16:19:53 +10008460 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008461 if ((this == NULL || rdev == this) &&
8462 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008463 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008464 test_bit(Faulty, &rdev->flags) &&
8465 atomic_read(&rdev->nr_pending)==0) {
8466 /* Faulty non-Blocked devices with nr_pending == 0
8467 * never get nr_pending incremented,
8468 * never get Faulty cleared, and never get Blocked set.
8469 * So we can synchronize_rcu now rather than once per device
8470 */
8471 remove_some = true;
8472 set_bit(RemoveSynchronized, &rdev->flags);
8473 }
8474 }
8475
8476 if (remove_some)
8477 synchronize_rcu();
8478 rdev_for_each(rdev, mddev) {
8479 if ((this == NULL || rdev == this) &&
8480 rdev->raid_disk >= 0 &&
8481 !test_bit(Blocked, &rdev->flags) &&
8482 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008483 (!test_bit(In_sync, &rdev->flags) &&
8484 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008485 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008486 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008487 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008488 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008489 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008490 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008491 }
8492 }
NeilBrownd787be42016-06-02 16:19:53 +10008493 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8494 clear_bit(RemoveSynchronized, &rdev->flags);
8495 }
8496
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008497 if (removed && mddev->kobj.sd)
8498 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008499
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008500 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008501 goto no_add;
8502
NeilBrowndafb20f2012-03-19 12:46:39 +11008503 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008504 if (this && this != rdev)
8505 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008506 if (test_bit(Candidate, &rdev->flags))
8507 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008508 if (rdev->raid_disk >= 0 &&
8509 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008510 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008511 !test_bit(Faulty, &rdev->flags))
8512 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008513 if (rdev->raid_disk >= 0)
8514 continue;
8515 if (test_bit(Faulty, &rdev->flags))
8516 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008517 if (!test_bit(Journal, &rdev->flags)) {
8518 if (mddev->ro &&
8519 ! (rdev->saved_raid_disk >= 0 &&
8520 !test_bit(Bitmap_sync, &rdev->flags)))
8521 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008522
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008523 rdev->recovery_offset = 0;
8524 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008525 if (mddev->pers->
8526 hot_add_disk(mddev, rdev) == 0) {
8527 if (sysfs_link_rdev(mddev, rdev))
8528 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008529 if (!test_bit(Journal, &rdev->flags))
8530 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008531 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08008532 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008533 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008534 }
NeilBrown746d3202013-04-24 11:42:41 +10008535no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008536 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08008537 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008538 return spares;
8539}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008540
NeilBrownac05f252014-09-30 08:10:42 +10008541static void md_start_sync(struct work_struct *ws)
8542{
8543 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008544
NeilBrownac05f252014-09-30 08:10:42 +10008545 mddev->sync_thread = md_register_thread(md_do_sync,
8546 mddev,
8547 "resync");
8548 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11008549 pr_warn("%s: could not start resync thread...\n",
8550 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008551 /* leave the spares where they are, it shouldn't hurt */
8552 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8553 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8554 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8555 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8556 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008557 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008558 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8559 &mddev->recovery))
8560 if (mddev->sysfs_action)
8561 sysfs_notify_dirent_safe(mddev->sysfs_action);
8562 } else
8563 md_wakeup_thread(mddev->sync_thread);
8564 sysfs_notify_dirent_safe(mddev->sysfs_action);
8565 md_new_event(mddev);
8566}
8567
Linus Torvalds1da177e2005-04-16 15:20:36 -07008568/*
8569 * This routine is regularly called by all per-raid-array threads to
8570 * deal with generic issues like resync and super-block update.
8571 * Raid personalities that don't have a thread (linear/raid0) do not
8572 * need this as they never do any recovery or update the superblock.
8573 *
8574 * It does not do any resync itself, but rather "forks" off other threads
8575 * to do that as needed.
8576 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8577 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008578 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008579 * and wakeups up this thread which will reap the thread and finish up.
8580 * This thread also removes any faulty devices (with nr_pending == 0).
8581 *
8582 * The overall approach is:
8583 * 1/ if the superblock needs updating, update it.
8584 * 2/ If a recovery thread is running, don't do anything else.
8585 * 3/ If recovery has finished, clean up, possibly marking spares active.
8586 * 4/ If there are any faulty devices, remove them.
8587 * 5/ If array is degraded, try to add spares devices
8588 * 6/ If array has spares or is not in-sync, start a resync thread.
8589 */
NeilBrownfd01b882011-10-11 16:47:53 +11008590void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008591{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008592 if (mddev->suspended)
8593 return;
8594
NeilBrown5f404022005-06-21 17:17:16 -07008595 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008596 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008597
NeilBrownfca4d842005-06-21 17:17:11 -07008598 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008599 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11008600 pr_debug("md: %s in immediate safe mode\n",
8601 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07008602 mddev->safemode = 2;
8603 }
8604 flush_signals(current);
8605 }
8606
NeilBrownc89a8ee2008-08-05 15:54:13 +10008607 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8608 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008609 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08008610 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008611 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008612 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008613 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11008614 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07008615 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008616 ))
8617 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008618
NeilBrowndf5b89b2006-03-27 01:18:20 -08008619 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008620 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008621
NeilBrownc89a8ee2008-08-05 15:54:13 +10008622 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008623 struct md_rdev *rdev;
8624 if (!mddev->external && mddev->in_sync)
8625 /* 'Blocked' flag not needed as failed devices
8626 * will be recorded if array switched to read/write.
8627 * Leaving it set will prevent the device
8628 * from being removed.
8629 */
8630 rdev_for_each(rdev, mddev)
8631 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008632 /* On a read-only array we can:
8633 * - remove failed devices
8634 * - add already-in_sync devices if the array itself
8635 * is in-sync.
8636 * As we only add devices that are already in-sync,
8637 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008638 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008639 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008640 /* There is no thread, but we need to call
8641 * ->spare_active and clear saved_raid_disk
8642 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008643 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008644 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008645 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008646 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08008647 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008648 goto unlock;
8649 }
8650
Guoqing Jiang659b2542015-12-21 10:50:59 +11008651 if (mddev_is_clustered(mddev)) {
8652 struct md_rdev *rdev;
8653 /* kick the device if another node issued a
8654 * remove disk.
8655 */
8656 rdev_for_each(rdev, mddev) {
8657 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8658 rdev->raid_disk < 0)
8659 md_kick_rdev_from_array(rdev);
8660 }
8661 }
8662
NeilBrown6497709b2017-03-15 14:05:14 +11008663 if (!mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11008664 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11008665 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11008666 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07008667 }
NeilBrownfca4d842005-06-21 17:17:11 -07008668
Shaohua Li29530792016-12-08 15:48:19 -08008669 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07008670 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008671
Linus Torvalds1da177e2005-04-16 15:20:36 -07008672 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8673 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8674 /* resync/recovery still happening */
8675 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8676 goto unlock;
8677 }
8678 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008679 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008680 goto unlock;
8681 }
Neil Brown72a23c22008-06-28 08:31:41 +10008682 /* Set RUNNING before clearing NEEDED to avoid
8683 * any transients in the value of "sync_action".
8684 */
NeilBrown72f36d52012-10-11 14:25:57 +11008685 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008686 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008687 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008688 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008689 /* Clear some bits that don't mean anything, but
8690 * might be left set
8691 */
NeilBrown24dd4692005-11-08 21:39:26 -08008692 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8693 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008694
NeilBrowned209582012-04-24 10:23:14 +10008695 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8696 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008697 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008698 /* no recovery is running.
8699 * remove any failed drives, then
8700 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008701 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702 * the personality to fail the re-add.
8703 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008704
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008705 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008706 if (mddev->pers->check_reshape == NULL ||
8707 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008708 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008709 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008710 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008711 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008712 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008713 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8714 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008715 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008716 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008717 } else if (mddev->recovery_cp < MaxSector) {
8718 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008719 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008720 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8721 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008722 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008723
Linus Torvalds1da177e2005-04-16 15:20:36 -07008724 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008725 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008726 /* We are adding a device or devices to an array
8727 * which has the bitmap stored on all devices.
8728 * So make sure all bitmap pages get written
8729 */
8730 bitmap_write_all(mddev->bitmap);
8731 }
NeilBrownac05f252014-09-30 08:10:42 +10008732 INIT_WORK(&mddev->del_work, md_start_sync);
8733 queue_work(md_misc_wq, &mddev->del_work);
8734 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008735 }
NeilBrownac05f252014-09-30 08:10:42 +10008736 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008737 if (!mddev->sync_thread) {
8738 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008739 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008740 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8741 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008742 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008743 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008744 }
NeilBrownac05f252014-09-30 08:10:42 +10008745 unlock:
8746 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008747 mddev_unlock(mddev);
8748 }
8749}
NeilBrown6c144d32014-09-30 16:15:38 +10008750EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008751
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008752void md_reap_sync_thread(struct mddev *mddev)
8753{
8754 struct md_rdev *rdev;
8755
8756 /* resync has finished, collect result */
8757 md_unregister_thread(&mddev->sync_thread);
8758 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8759 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8760 /* success...*/
8761 /* activate any spares */
8762 if (mddev->pers->spare_active(mddev)) {
8763 sysfs_notify(&mddev->kobj, NULL,
8764 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08008765 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008766 }
8767 }
8768 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8769 mddev->pers->finish_reshape)
8770 mddev->pers->finish_reshape(mddev);
8771
8772 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008773 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008774 */
NeilBrownf4667222013-12-09 12:04:56 +11008775 if (!mddev->degraded)
8776 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008777 rdev->saved_raid_disk = -1;
8778
8779 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08008780 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008781 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8782 * clustered raid */
8783 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8784 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008785 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008786 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008787 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8788 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8789 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8790 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008791 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008792 /* flag recovery needed just to double check */
8793 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8794 sysfs_notify_dirent_safe(mddev->sysfs_action);
8795 md_new_event(mddev);
8796 if (mddev->event_work.func)
8797 queue_work(md_misc_wq, &mddev->event_work);
8798}
NeilBrown6c144d32014-09-30 16:15:38 +10008799EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008800
NeilBrownfd01b882011-10-11 16:47:53 +11008801void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008802{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008803 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008804 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008805 !test_bit(Blocked, &rdev->flags) &&
8806 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008807 msecs_to_jiffies(5000));
8808 rdev_dec_pending(rdev, mddev);
8809}
8810EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8811
NeilBrownc6563a82012-05-21 09:27:00 +10008812void md_finish_reshape(struct mddev *mddev)
8813{
8814 /* called be personality module when reshape completes. */
8815 struct md_rdev *rdev;
8816
8817 rdev_for_each(rdev, mddev) {
8818 if (rdev->data_offset > rdev->new_data_offset)
8819 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8820 else
8821 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8822 rdev->data_offset = rdev->new_data_offset;
8823 }
8824}
8825EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008826
Vishal Vermafc974ee2015-12-24 19:20:34 -07008827/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10008828
Vishal Vermafc974ee2015-12-24 19:20:34 -07008829/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11008830int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008831 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008832{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008833 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10008834 int rv;
8835 if (is_new)
8836 s += rdev->new_data_offset;
8837 else
8838 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008839 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8840 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008841 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008842 if (test_bit(ExternalBbl, &rdev->flags))
8843 sysfs_notify(&rdev->kobj, NULL,
8844 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008845 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08008846 set_mask_bits(&mddev->sb_flags, 0,
8847 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10008848 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07008849 return 1;
8850 } else
8851 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008852}
8853EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8854
NeilBrownc6563a82012-05-21 09:27:00 +10008855int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8856 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008857{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008858 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10008859 if (is_new)
8860 s += rdev->new_data_offset;
8861 else
8862 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008863 rv = badblocks_clear(&rdev->badblocks, s, sectors);
8864 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
8865 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
8866 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008867}
8868EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8869
Adrian Bunk75c96f82005-05-05 16:16:09 -07008870static int md_notify_reboot(struct notifier_block *this,
8871 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008872{
8873 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008874 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008875 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008876
NeilBrownc744a652012-03-19 12:46:37 +11008877 for_each_mddev(mddev, tmp) {
8878 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008879 if (mddev->pers)
8880 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008881 if (mddev->persistent)
8882 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008883 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008884 }
NeilBrownc744a652012-03-19 12:46:37 +11008885 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008886 }
NeilBrownc744a652012-03-19 12:46:37 +11008887 /*
8888 * certain more exotic SCSI devices are known to be
8889 * volatile wrt too early system reboots. While the
8890 * right place to handle this issue is the given
8891 * driver, we do want to have a safe RAID driver ...
8892 */
8893 if (need_delay)
8894 mdelay(1000*1);
8895
Linus Torvalds1da177e2005-04-16 15:20:36 -07008896 return NOTIFY_DONE;
8897}
8898
Adrian Bunk75c96f82005-05-05 16:16:09 -07008899static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900 .notifier_call = md_notify_reboot,
8901 .next = NULL,
8902 .priority = INT_MAX, /* before any real devices */
8903};
8904
8905static void md_geninit(void)
8906{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008907 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008908
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008909 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008910}
8911
Adrian Bunk75c96f82005-05-05 16:16:09 -07008912static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008913{
Tejun Heoe804ac72010-10-15 15:36:08 +02008914 int ret = -ENOMEM;
8915
Tejun Heoada609e2011-01-25 14:35:54 +01008916 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008917 if (!md_wq)
8918 goto err_wq;
8919
8920 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8921 if (!md_misc_wq)
8922 goto err_misc_wq;
8923
8924 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8925 goto err_md;
8926
8927 if ((ret = register_blkdev(0, "mdp")) < 0)
8928 goto err_mdp;
8929 mdp_major = ret;
8930
NeilBrownaf5628f2014-07-31 13:54:54 +10008931 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008932 md_probe, NULL, NULL);
8933 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934 md_probe, NULL, NULL);
8935
Linus Torvalds1da177e2005-04-16 15:20:36 -07008936 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008937 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008938
8939 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008940 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008941
Tejun Heoe804ac72010-10-15 15:36:08 +02008942err_mdp:
8943 unregister_blkdev(MD_MAJOR, "md");
8944err_md:
8945 destroy_workqueue(md_misc_wq);
8946err_misc_wq:
8947 destroy_workqueue(md_wq);
8948err_wq:
8949 return ret;
8950}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008951
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008952static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008953{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008954 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8955 struct md_rdev *rdev2;
8956 int role, ret;
8957 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008958
Guoqing Jiang818da592017-03-01 16:42:40 +08008959 /*
8960 * If size is changed in another node then we need to
8961 * do resize as well.
8962 */
8963 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
8964 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
8965 if (ret)
8966 pr_info("md-cluster: resize failed\n");
8967 else
8968 bitmap_update_sb(mddev->bitmap);
8969 }
8970
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008971 /* Check for change of roles in the active devices */
8972 rdev_for_each(rdev2, mddev) {
8973 if (test_bit(Faulty, &rdev2->flags))
8974 continue;
8975
8976 /* Check if the roles changed */
8977 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008978
8979 if (test_bit(Candidate, &rdev2->flags)) {
8980 if (role == 0xfffe) {
8981 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
8982 md_kick_rdev_from_array(rdev2);
8983 continue;
8984 }
8985 else
8986 clear_bit(Candidate, &rdev2->flags);
8987 }
8988
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008989 if (role != rdev2->raid_disk) {
8990 /* got activated */
8991 if (rdev2->raid_disk == -1 && role != 0xffff) {
8992 rdev2->saved_raid_disk = role;
8993 ret = remove_and_add_spares(mddev, rdev2);
8994 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11008995 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04008996 /* wakeup mddev->thread here, so array could
8997 * perform resync with the new activated disk */
8998 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8999 md_wakeup_thread(mddev->thread);
9000
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009001 }
9002 /* device faulty
9003 * We just want to do the minimum to mark the disk
9004 * as faulty. The recovery is performed by the
9005 * one who initiated the error.
9006 */
9007 if ((role == 0xfffe) || (role == 0xfffd)) {
9008 md_error(mddev, rdev2);
9009 clear_bit(Blocked, &rdev2->flags);
9010 }
9011 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009012 }
9013
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009014 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9015 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009016
9017 /* Finally set the event to be up to date */
9018 mddev->events = le64_to_cpu(sb->events);
9019}
9020
9021static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9022{
9023 int err;
9024 struct page *swapout = rdev->sb_page;
9025 struct mdp_superblock_1 *sb;
9026
9027 /* Store the sb page of the rdev in the swapout temporary
9028 * variable in case we err in the future
9029 */
9030 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009031 err = alloc_disk_sb(rdev);
9032 if (err == 0) {
9033 ClearPageUptodate(rdev->sb_page);
9034 rdev->sb_loaded = 0;
9035 err = super_types[mddev->major_version].
9036 load_super(rdev, NULL, mddev->minor_version);
9037 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009038 if (err < 0) {
9039 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9040 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009041 if (rdev->sb_page)
9042 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009043 rdev->sb_page = swapout;
9044 rdev->sb_loaded = 1;
9045 return err;
9046 }
9047
9048 sb = page_address(rdev->sb_page);
9049 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9050 * is not set
9051 */
9052
9053 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9054 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9055
9056 /* The other node finished recovery, call spare_active to set
9057 * device In_sync and mddev->degraded
9058 */
9059 if (rdev->recovery_offset == MaxSector &&
9060 !test_bit(In_sync, &rdev->flags) &&
9061 mddev->pers->spare_active(mddev))
9062 sysfs_notify(&mddev->kobj, NULL, "degraded");
9063
9064 put_page(swapout);
9065 return 0;
9066}
9067
9068void md_reload_sb(struct mddev *mddev, int nr)
9069{
9070 struct md_rdev *rdev;
9071 int err;
9072
9073 /* Find the rdev */
9074 rdev_for_each_rcu(rdev, mddev) {
9075 if (rdev->desc_nr == nr)
9076 break;
9077 }
9078
9079 if (!rdev || rdev->desc_nr != nr) {
9080 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9081 return;
9082 }
9083
9084 err = read_rdev(mddev, rdev);
9085 if (err < 0)
9086 return;
9087
9088 check_sb_changes(mddev, rdev);
9089
9090 /* Read all rdev's to update recovery_offset */
9091 rdev_for_each_rcu(rdev, mddev)
9092 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009093}
9094EXPORT_SYMBOL(md_reload_sb);
9095
Linus Torvalds1da177e2005-04-16 15:20:36 -07009096#ifndef MODULE
9097
9098/*
9099 * Searches all registered partitions for autorun RAID arrays
9100 * at boot time.
9101 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009102
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009103static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009104static LIST_HEAD(all_detected_devices);
9105struct detected_devices_node {
9106 struct list_head list;
9107 dev_t dev;
9108};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009109
9110void md_autodetect_dev(dev_t dev)
9111{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009112 struct detected_devices_node *node_detected_dev;
9113
9114 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9115 if (node_detected_dev) {
9116 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009117 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009118 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009119 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009121}
9122
Linus Torvalds1da177e2005-04-16 15:20:36 -07009123static void autostart_arrays(int part)
9124{
NeilBrown3cb03002011-10-11 16:45:26 +11009125 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009126 struct detected_devices_node *node_detected_dev;
9127 dev_t dev;
9128 int i_scanned, i_passed;
9129
9130 i_scanned = 0;
9131 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009132
NeilBrown9d487392016-11-02 14:16:49 +11009133 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009134
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009135 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009136 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9137 i_scanned++;
9138 node_detected_dev = list_entry(all_detected_devices.next,
9139 struct detected_devices_node, list);
9140 list_del(&node_detected_dev->list);
9141 dev = node_detected_dev->dev;
9142 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009143 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009144 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009145 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009146 if (IS_ERR(rdev))
9147 continue;
9148
NeilBrown403df472014-09-30 15:52:29 +10009149 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009150 continue;
NeilBrown403df472014-09-30 15:52:29 +10009151
NeilBrownd0fae182008-03-04 14:29:31 -08009152 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009153 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009154 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009155 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009156 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009157
NeilBrown9d487392016-11-02 14:16:49 +11009158 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009159
9160 autorun_devices(part);
9161}
9162
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009163#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009164
9165static __exit void md_exit(void)
9166{
NeilBrownfd01b882011-10-11 16:47:53 +11009167 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009168 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009169 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009170
NeilBrownaf5628f2014-07-31 13:54:54 +10009171 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009172 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009173
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009174 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009175 unregister_blkdev(mdp_major, "mdp");
9176 unregister_reboot_notifier(&md_notifier);
9177 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009178
9179 /* We cannot unload the modules while some process is
9180 * waiting for us in select() or poll() - wake them up
9181 */
9182 md_unloading = 1;
9183 while (waitqueue_active(&md_event_waiters)) {
9184 /* not safe to leave yet */
9185 wake_up(&md_event_waiters);
9186 msleep(delay);
9187 delay += delay;
9188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009189 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009190
NeilBrown29ac4aa2008-02-06 01:39:58 -08009191 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009192 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009193 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009194 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009195 /*
9196 * for_each_mddev() will call mddev_put() at the end of each
9197 * iteration. As the mddev is now fully clear, this will
9198 * schedule the mddev for destruction by a workqueue, and the
9199 * destroy_workqueue() below will wait for that to complete.
9200 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009201 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009202 destroy_workqueue(md_misc_wq);
9203 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009204}
9205
Dan Williams685784a2007-07-09 11:56:42 -07009206subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009207module_exit(md_exit)
9208
NeilBrownf91de922005-11-08 21:39:36 -08009209static int get_ro(char *buffer, struct kernel_param *kp)
9210{
9211 return sprintf(buffer, "%d", start_readonly);
9212}
9213static int set_ro(const char *val, struct kernel_param *kp)
9214{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009215 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009216}
9217
NeilBrown80ca3a42006-07-10 04:44:18 -07009218module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9219module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009220module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009221module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009222
Linus Torvalds1da177e2005-04-16 15:20:36 -07009223MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009224MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009225MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009226MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);