blob: 6cc6dd74c153e885ab731e1d1cc302da3fa6ccd6 [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;
Shaohua Li20737732016-12-13 12:40:15 -0800768 bio->bi_opf = REQ_OP_WRITE | 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
Andre Nolldd8ac332009-03-31 14:33:13 +11002106 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002107 if (!test_bit(Journal, &rdev->flags) &&
2108 rdev->sectors &&
2109 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002110 if (mddev->pers) {
2111 /* Cannot change size, so fail
2112 * If mddev->level <= 0, then we don't care
2113 * about aligning sizes (e.g. linear)
2114 */
2115 if (mddev->level > 0)
2116 return -ENOSPC;
2117 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002118 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 /* Verify rdev->desc_nr is unique.
2122 * If it is -1, assign a free number, else
2123 * check number is not in use
2124 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002125 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (rdev->desc_nr < 0) {
2127 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002128 if (mddev->pers)
2129 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002130 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 choice++;
2132 rdev->desc_nr = choice;
2133 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002134 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002135 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002139 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002140 if (!test_bit(Journal, &rdev->flags) &&
2141 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002142 pr_warn("md: %s: array is limited to %d devices\n",
2143 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002144 return -EBUSY;
2145 }
NeilBrown19133a42005-11-08 21:39:35 -08002146 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002147 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002150 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002151
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002152 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002153 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002154
Tejun Heo0762b8b2008-08-25 19:56:12 +09002155 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002156 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2157 /* failure here is OK */;
2158 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002159
NeilBrown4b809912008-07-21 17:05:25 +10002160 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002161 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002162
2163 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002164 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002167
2168 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002169 pr_warn("md: failed to register dev-%s for %s\n",
2170 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002171 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
NeilBrown177a99b2008-02-06 01:39:56 -08002174static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002175{
NeilBrown3cb03002011-10-11 16:45:26 +11002176 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002177 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002178 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002179}
2180
NeilBrownf72ffdd2014-09-30 14:23:59 +10002181static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
2183 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002184
Tejun Heo49731ba2011-01-14 18:43:57 +01002185 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002186 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002187 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002189 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002190 sysfs_put(rdev->sysfs_state);
2191 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002192 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002193 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002194 * writing to 'remove' to "dev/state". We also need
2195 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002196 */
NeilBrown4b809912008-07-21 17:05:25 +10002197 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002198 INIT_WORK(&rdev->del_work, md_delayed_delete);
2199 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002200 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
2203/*
2204 * prevent the device from being mounted, repartitioned or
2205 * otherwise reused by a RAID array (or any other kernel
2206 * subsystem), by bd_claiming the device.
2207 */
NeilBrown3cb03002011-10-11 16:45:26 +11002208static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
2210 int err = 0;
2211 struct block_device *bdev;
2212 char b[BDEVNAME_SIZE];
2213
Tejun Heod4d77622010-11-13 11:55:18 +01002214 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002215 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002217 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 return PTR_ERR(bdev);
2219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 rdev->bdev = bdev;
2221 return err;
2222}
2223
NeilBrown3cb03002011-10-11 16:45:26 +11002224static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
2226 struct block_device *bdev = rdev->bdev;
2227 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002228 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
2231void md_autodetect_dev(dev_t dev);
2232
NeilBrownf72ffdd2014-09-30 14:23:59 +10002233static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
2235 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002236
NeilBrown9d487392016-11-02 14:16:49 +11002237 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002238 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002240 if (test_bit(AutoDetected, &rdev->flags))
2241 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242#endif
2243 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002244 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002247void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
2249 unbind_rdev_from_array(rdev);
2250 export_rdev(rdev);
2251}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002252EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
NeilBrownfd01b882011-10-11 16:47:53 +11002254static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
NeilBrown0638bb02014-09-25 17:43:47 +10002256 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
NeilBrown0638bb02014-09-25 17:43:47 +10002258 while (!list_empty(&mddev->disks)) {
2259 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2260 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002261 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 mddev->raid_disks = 0;
2264 mddev->major_version = 0;
2265}
2266
NeilBrown6497709b2017-03-15 14:05:14 +11002267static bool set_in_sync(struct mddev *mddev)
2268{
2269 WARN_ON_ONCE(!spin_is_locked(&mddev->lock));
NeilBrown4ad23a972017-03-15 14:05:14 +11002270 if (!mddev->in_sync) {
2271 mddev->sync_checkers++;
2272 spin_unlock(&mddev->lock);
2273 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2274 spin_lock(&mddev->lock);
2275 if (!mddev->in_sync &&
2276 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002277 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002278 /*
2279 * Ensure ->in_sync is visible before we clear
2280 * ->sync_checkers.
2281 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002282 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002283 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2284 sysfs_notify_dirent_safe(mddev->sysfs_state);
2285 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002286 if (--mddev->sync_checkers == 0)
2287 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002288 }
2289 if (mddev->safemode == 1)
2290 mddev->safemode = 0;
2291 return mddev->in_sync;
2292}
2293
NeilBrownf72ffdd2014-09-30 14:23:59 +10002294static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
NeilBrown42543762006-06-26 00:27:57 -07002296 /* Update each superblock (in-memory image), but
2297 * if we are allowed to, skip spares which already
2298 * have the right event counter, or have one earlier
2299 * (which would mean they aren't being marked as dirty
2300 * with the rest of the array)
2301 */
NeilBrown3cb03002011-10-11 16:45:26 +11002302 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002303 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002304 if (rdev->sb_events == mddev->events ||
2305 (nospares &&
2306 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002307 rdev->sb_events+1 == mddev->events)) {
2308 /* Don't update this superblock */
2309 rdev->sb_loaded = 2;
2310 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002311 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002312 rdev->sb_loaded = 1;
2313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
2315}
2316
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002317static bool does_sb_need_changing(struct mddev *mddev)
2318{
2319 struct md_rdev *rdev;
2320 struct mdp_superblock_1 *sb;
2321 int role;
2322
2323 /* Find a good rdev */
2324 rdev_for_each(rdev, mddev)
2325 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2326 break;
2327
2328 /* No good device found. */
2329 if (!rdev)
2330 return false;
2331
2332 sb = page_address(rdev->sb_page);
2333 /* Check if a device has become faulty or a spare become active */
2334 rdev_for_each(rdev, mddev) {
2335 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2336 /* Device activated? */
2337 if (role == 0xffff && rdev->raid_disk >=0 &&
2338 !test_bit(Faulty, &rdev->flags))
2339 return true;
2340 /* Device turned faulty? */
2341 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2342 return true;
2343 }
2344
2345 /* Check if any mddev parameters have changed */
2346 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2347 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002348 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002349 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2350 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2351 return true;
2352
2353 return false;
2354}
2355
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002356void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
NeilBrown3cb03002011-10-11 16:45:26 +11002358 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002359 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002360 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002361 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002362 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
NeilBrownd87f0642013-04-24 11:42:40 +10002364 if (mddev->ro) {
2365 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002366 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002367 return;
2368 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002369
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002370repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002371 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002372 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002373 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002374 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002375 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002376 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002377 /* Has someone else has updated the sb */
2378 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002379 if (ret == 0)
2380 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002381 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2382 BIT(MD_SB_CHANGE_DEVS) |
2383 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002384 return;
2385 }
2386 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002387
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002388 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002389 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002390 if (rdev->raid_disk >= 0 &&
2391 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002392 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002393 !test_bit(In_sync, &rdev->flags) &&
2394 mddev->curr_resync_completed > rdev->recovery_offset)
2395 rdev->recovery_offset = mddev->curr_resync_completed;
2396
NeilBrownf72ffdd2014-09-30 14:23:59 +10002397 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002398 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002399 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2400 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002401 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002402 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002403 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002404 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002405 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002406 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002407 md_error(mddev, rdev);
2408 }
2409 clear_bit(Blocked, &rdev->flags);
2410 clear_bit(BlockedBadBlocks, &rdev->flags);
2411 wake_up(&rdev->blocked_wait);
2412 }
2413 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002414 wake_up(&mddev->sb_wait);
2415 return;
2416 }
2417
NeilBrown85572d72014-12-15 12:56:56 +11002418 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002419
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002420 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002421
Shaohua Li29530792016-12-08 15:48:19 -08002422 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002423 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002424 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002425 /* just a clean<-> dirty transition, possibly leave spares alone,
2426 * though if events isn't the right even/odd, we will have to do
2427 * spares after all
2428 */
2429 nospares = 1;
2430 if (force_change)
2431 nospares = 0;
2432 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002433 /* If the array is degraded, then skipping spares is both
2434 * dangerous and fairly pointless.
2435 * Dangerous because a device that was removed from the array
2436 * might have a event_count that still looks up-to-date,
2437 * so it can be re-added without a resync.
2438 * Pointless because if there are any spares to skip,
2439 * then a recovery will happen and soon that array won't
2440 * be degraded any more and the spare can go back to sleep then.
2441 */
NeilBrown850b2b422006-10-03 01:15:46 -07002442 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002443
NeilBrown06d91a52005-06-21 17:17:12 -07002444 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002445
2446 /* If this is just a dirty<->clean transition, and the array is clean
2447 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002448 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002449 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002450 && mddev->can_decrease_events
2451 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002452 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002453 mddev->can_decrease_events = 0;
2454 } else {
NeilBrown42543762006-06-26 00:27:57 -07002455 /* otherwise we have to go forward and ... */
2456 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002457 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
NeilBrown403df472014-09-30 15:52:29 +10002460 /*
2461 * This 64-bit counter should never wrap.
2462 * Either we are in around ~1 trillion A.C., assuming
2463 * 1 reboot per second, or we have a bug...
2464 */
2465 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002466
NeilBrowndafb20f2012-03-19 12:46:39 +11002467 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002468 if (rdev->badblocks.changed)
2469 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002470 if (test_bit(Faulty, &rdev->flags))
2471 set_bit(FaultRecorded, &rdev->flags);
2472 }
NeilBrown2699b672011-07-28 11:31:47 +10002473
NeilBrowne6910632008-02-06 01:39:51 -08002474 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002475 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
NeilBrown36a4e1f2011-10-07 14:23:17 +11002477 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2478 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Shaohua Li504634f2016-11-18 09:44:08 -08002480 if (mddev->queue)
2481 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002482rewrite:
NeilBrown4ad13662007-07-17 04:06:13 -07002483 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002484 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002486
NeilBrown42543762006-06-26 00:27:57 -07002487 if (rdev->sb_loaded != 1)
2488 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
NeilBrownf4667222013-12-09 12:04:56 +11002490 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002491 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002492 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002493 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002494 pr_debug("md: (write) %s's sb offset: %llu\n",
2495 bdevname(rdev->bdev, b),
2496 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002497 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002498 if (rdev->badblocks.size) {
2499 md_super_write(mddev, rdev,
2500 rdev->badblocks.sector,
2501 rdev->badblocks.size << 9,
2502 rdev->bb_page);
2503 rdev->badblocks.size = 0;
2504 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002505
NeilBrownf4667222013-12-09 12:04:56 +11002506 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002507 pr_debug("md: %s (skipping faulty)\n",
2508 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002509
NeilBrown7bfa19f2005-06-21 17:17:28 -07002510 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 /* only need to write one superblock... */
2512 break;
2513 }
NeilBrown46533ff2016-11-18 16:16:11 +11002514 if (md_super_wait(mddev) < 0)
2515 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002516 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002517
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002518 if (mddev_is_clustered(mddev) && ret == 0)
2519 md_cluster_ops->metadata_update_finish(mddev);
2520
NeilBrown850b2b422006-10-03 01:15:46 -07002521 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002522 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2523 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002524 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002525 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002526 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002527 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2528 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002529
NeilBrowndafb20f2012-03-19 12:46:39 +11002530 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002531 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2532 clear_bit(Blocked, &rdev->flags);
2533
2534 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002535 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002536 clear_bit(BlockedBadBlocks, &rdev->flags);
2537 wake_up(&rdev->blocked_wait);
2538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002540EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002542static int add_bound_rdev(struct md_rdev *rdev)
2543{
2544 struct mddev *mddev = rdev->mddev;
2545 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002546 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002547
Shaohua Li87d4d912016-01-06 14:37:14 -08002548 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002549 /* If there is hot_add_disk but no hot_remove_disk
2550 * then added disks for geometry changes,
2551 * and should be added immediately.
2552 */
2553 super_types[mddev->major_version].
2554 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002555 if (add_journal)
2556 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002557 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002558 if (add_journal)
2559 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002560 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002561 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002562 return err;
2563 }
2564 }
2565 sysfs_notify_dirent_safe(rdev->sysfs_state);
2566
Shaohua Li29530792016-12-08 15:48:19 -08002567 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002568 if (mddev->degraded)
2569 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2570 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2571 md_new_event(mddev);
2572 md_wakeup_thread(mddev->thread);
2573 return 0;
2574}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Andre Noll7f6ce762008-03-23 18:34:54 +01002576/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002577 * We want to accept with case. For this we use cmd_match.
2578 */
2579static int cmd_match(const char *cmd, const char *str)
2580{
2581 /* See if cmd, written into a sysfs file, matches
2582 * str. They must either be the same, or cmd can
2583 * have a trailing newline
2584 */
2585 while (*cmd && *str && *cmd == *str) {
2586 cmd++;
2587 str++;
2588 }
2589 if (*cmd == '\n')
2590 cmd++;
2591 if (*str || *cmd)
2592 return 0;
2593 return 1;
2594}
2595
NeilBrown86e6ffd2005-11-08 21:39:24 -08002596struct rdev_sysfs_entry {
2597 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002598 ssize_t (*show)(struct md_rdev *, char *);
2599 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002600};
2601
2602static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002603state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002604{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002605 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002606 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002607 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002608
NeilBrown758bfc82014-12-15 12:56:59 +11002609 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002610 (!test_bit(ExternalBbl, &flags) &&
2611 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002612 len += sprintf(page+len, "faulty%s", sep);
2613 if (test_bit(In_sync, &flags))
2614 len += sprintf(page+len, "in_sync%s", sep);
2615 if (test_bit(Journal, &flags))
2616 len += sprintf(page+len, "journal%s", sep);
2617 if (test_bit(WriteMostly, &flags))
2618 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002619 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002620 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002621 && !test_bit(Faulty, &flags)))
2622 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002623 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002624 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002625 !test_bit(In_sync, &flags))
2626 len += sprintf(page+len, "spare%s", sep);
2627 if (test_bit(WriteErrorSeen, &flags))
2628 len += sprintf(page+len, "write_error%s", sep);
2629 if (test_bit(WantReplacement, &flags))
2630 len += sprintf(page+len, "want_replacement%s", sep);
2631 if (test_bit(Replacement, &flags))
2632 len += sprintf(page+len, "replacement%s", sep);
2633 if (test_bit(ExternalBbl, &flags))
2634 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002635 if (test_bit(FailFast, &flags))
2636 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002637
2638 if (len)
2639 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002640
NeilBrown86e6ffd2005-11-08 21:39:24 -08002641 return len+sprintf(page+len, "\n");
2642}
2643
NeilBrown45dc2de2006-06-26 00:27:58 -07002644static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002645state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002646{
2647 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002648 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002649 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002650 * writemostly - sets write_mostly
2651 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002652 * blocked - sets the Blocked flags
2653 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002654 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002655 * -insync - clear Insync for a device with a slot assigned,
2656 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002657 * write_error - sets WriteErrorSeen
2658 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002659 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002660 */
2661 int err = -EINVAL;
2662 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2663 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002664 if (test_bit(Faulty, &rdev->flags))
2665 err = 0;
2666 else
2667 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002668 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002669 if (rdev->mddev->pers) {
2670 clear_bit(Blocked, &rdev->flags);
2671 remove_and_add_spares(rdev->mddev, rdev);
2672 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002673 if (rdev->raid_disk >= 0)
2674 err = -EBUSY;
2675 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002676 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002677 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002678 if (mddev_is_clustered(mddev))
2679 err = md_cluster_ops->remove_disk(mddev, rdev);
2680
2681 if (err == 0) {
2682 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002683 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002684 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002685 md_wakeup_thread(mddev->thread);
2686 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002687 md_new_event(mddev);
2688 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002689 }
NeilBrownf6556752006-06-26 00:28:01 -07002690 } else if (cmd_match(buf, "writemostly")) {
2691 set_bit(WriteMostly, &rdev->flags);
2692 err = 0;
2693 } else if (cmd_match(buf, "-writemostly")) {
2694 clear_bit(WriteMostly, &rdev->flags);
2695 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002696 } else if (cmd_match(buf, "blocked")) {
2697 set_bit(Blocked, &rdev->flags);
2698 err = 0;
2699 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002700 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002701 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002702 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002703 /* metadata handler doesn't understand badblocks,
2704 * so we need to fail the device
2705 */
2706 md_error(rdev->mddev, rdev);
2707 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002708 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002709 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002710 wake_up(&rdev->blocked_wait);
2711 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2712 md_wakeup_thread(rdev->mddev->thread);
2713
2714 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002715 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2716 set_bit(In_sync, &rdev->flags);
2717 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11002718 } else if (cmd_match(buf, "failfast")) {
2719 set_bit(FailFast, &rdev->flags);
2720 err = 0;
2721 } else if (cmd_match(buf, "-failfast")) {
2722 clear_bit(FailFast, &rdev->flags);
2723 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002724 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2725 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002726 if (rdev->mddev->pers == NULL) {
2727 clear_bit(In_sync, &rdev->flags);
2728 rdev->saved_raid_disk = rdev->raid_disk;
2729 rdev->raid_disk = -1;
2730 err = 0;
2731 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002732 } else if (cmd_match(buf, "write_error")) {
2733 set_bit(WriteErrorSeen, &rdev->flags);
2734 err = 0;
2735 } else if (cmd_match(buf, "-write_error")) {
2736 clear_bit(WriteErrorSeen, &rdev->flags);
2737 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002738 } else if (cmd_match(buf, "want_replacement")) {
2739 /* Any non-spare device that is not a replacement can
2740 * become want_replacement at any time, but we then need to
2741 * check if recovery is needed.
2742 */
2743 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002744 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002745 !test_bit(Replacement, &rdev->flags))
2746 set_bit(WantReplacement, &rdev->flags);
2747 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2748 md_wakeup_thread(rdev->mddev->thread);
2749 err = 0;
2750 } else if (cmd_match(buf, "-want_replacement")) {
2751 /* Clearing 'want_replacement' is always allowed.
2752 * Once replacements starts it is too late though.
2753 */
2754 err = 0;
2755 clear_bit(WantReplacement, &rdev->flags);
2756 } else if (cmd_match(buf, "replacement")) {
2757 /* Can only set a device as a replacement when array has not
2758 * yet been started. Once running, replacement is automatic
2759 * from spares, or by assigning 'slot'.
2760 */
2761 if (rdev->mddev->pers)
2762 err = -EBUSY;
2763 else {
2764 set_bit(Replacement, &rdev->flags);
2765 err = 0;
2766 }
2767 } else if (cmd_match(buf, "-replacement")) {
2768 /* Similarly, can only clear Replacement before start */
2769 if (rdev->mddev->pers)
2770 err = -EBUSY;
2771 else {
2772 clear_bit(Replacement, &rdev->flags);
2773 err = 0;
2774 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002775 } else if (cmd_match(buf, "re-add")) {
2776 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002777 /* clear_bit is performed _after_ all the devices
2778 * have their local Faulty bit cleared. If any writes
2779 * happen in the meantime in the local node, they
2780 * will land in the local bitmap, which will be synced
2781 * by this node eventually
2782 */
2783 if (!mddev_is_clustered(rdev->mddev) ||
2784 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2785 clear_bit(Faulty, &rdev->flags);
2786 err = add_bound_rdev(rdev);
2787 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002788 } else
2789 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002790 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2791 set_bit(ExternalBbl, &rdev->flags);
2792 rdev->badblocks.shift = 0;
2793 err = 0;
2794 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2795 clear_bit(ExternalBbl, &rdev->flags);
2796 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002797 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002798 if (!err)
2799 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002800 return err ? err : len;
2801}
NeilBrown80ca3a42006-07-10 04:44:18 -07002802static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002803__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002804
2805static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002806errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002807{
2808 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2809}
2810
2811static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002812errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002813{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002814 unsigned int n;
2815 int rv;
2816
2817 rv = kstrtouint(buf, 10, &n);
2818 if (rv < 0)
2819 return rv;
2820 atomic_set(&rdev->corrected_errors, n);
2821 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002822}
2823static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002824__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002825
NeilBrown014236d2006-01-06 00:20:55 -08002826static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002827slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002828{
Shaohua Lif2076e72015-10-08 21:54:12 -07002829 if (test_bit(Journal, &rdev->flags))
2830 return sprintf(page, "journal\n");
2831 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002832 return sprintf(page, "none\n");
2833 else
2834 return sprintf(page, "%d\n", rdev->raid_disk);
2835}
2836
2837static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002838slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002839{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002840 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002841 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002842
Shaohua Lif2076e72015-10-08 21:54:12 -07002843 if (test_bit(Journal, &rdev->flags))
2844 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002845 if (strncmp(buf, "none", 4)==0)
2846 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002847 else {
2848 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2849 if (err < 0)
2850 return err;
2851 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002852 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002853 /* Setting 'slot' on an active array requires also
2854 * updating the 'rd%d' link, and communicating
2855 * with the personality with ->hot_*_disk.
2856 * For now we only support removing
2857 * failed/spare devices. This normally happens automatically,
2858 * but not when the metadata is externally managed.
2859 */
NeilBrownc303da62008-02-06 01:39:51 -08002860 if (rdev->raid_disk == -1)
2861 return -EEXIST;
2862 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002863 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002864 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002865 clear_bit(Blocked, &rdev->flags);
2866 remove_and_add_spares(rdev->mddev, rdev);
2867 if (rdev->raid_disk >= 0)
2868 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002869 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2870 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002871 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002872 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002873 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002874 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002875 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002876
2877 if (rdev->raid_disk != -1)
2878 return -EBUSY;
2879
NeilBrownc6751b22011-02-02 11:57:13 +11002880 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2881 return -EBUSY;
2882
Neil Brown6c2fce22008-06-28 08:31:31 +10002883 if (rdev->mddev->pers->hot_add_disk == NULL)
2884 return -EINVAL;
2885
NeilBrownba1b41b2011-01-14 09:14:34 +11002886 if (slot >= rdev->mddev->raid_disks &&
2887 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2888 return -ENOSPC;
2889
Neil Brown6c2fce22008-06-28 08:31:31 +10002890 rdev->raid_disk = slot;
2891 if (test_bit(In_sync, &rdev->flags))
2892 rdev->saved_raid_disk = slot;
2893 else
2894 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002895 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002896 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002897 err = rdev->mddev->pers->
2898 hot_add_disk(rdev->mddev, rdev);
2899 if (err) {
2900 rdev->raid_disk = -1;
2901 return err;
2902 } else
2903 sysfs_notify_dirent_safe(rdev->sysfs_state);
2904 if (sysfs_link_rdev(rdev->mddev, rdev))
2905 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002906 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002907 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002908 if (slot >= rdev->mddev->raid_disks &&
2909 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002910 return -ENOSPC;
2911 rdev->raid_disk = slot;
2912 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002913 clear_bit(Faulty, &rdev->flags);
2914 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002915 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002916 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002917 }
NeilBrown014236d2006-01-06 00:20:55 -08002918 return len;
2919}
2920
NeilBrown014236d2006-01-06 00:20:55 -08002921static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002922__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002923
NeilBrown93c8cad2006-01-06 00:20:56 -08002924static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002925offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002926{
Andrew Morton6961ece2006-01-06 00:20:59 -08002927 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002928}
2929
2930static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002931offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002932{
NeilBrownc6563a82012-05-21 09:27:00 +10002933 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002934 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002935 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002936 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002937 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002938 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002939 /* Must set offset before size, so overlap checks
2940 * can be sane */
2941 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002942 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002943 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002944 return len;
2945}
2946
2947static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002948__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002949
NeilBrownc6563a82012-05-21 09:27:00 +10002950static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2951{
2952 return sprintf(page, "%llu\n",
2953 (unsigned long long)rdev->new_data_offset);
2954}
2955
2956static ssize_t new_offset_store(struct md_rdev *rdev,
2957 const char *buf, size_t len)
2958{
2959 unsigned long long new_offset;
2960 struct mddev *mddev = rdev->mddev;
2961
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002962 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002963 return -EINVAL;
2964
NeilBrownf851b602014-12-11 10:02:10 +11002965 if (mddev->sync_thread ||
2966 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002967 return -EBUSY;
2968 if (new_offset == rdev->data_offset)
2969 /* reset is always permitted */
2970 ;
2971 else if (new_offset > rdev->data_offset) {
2972 /* must not push array size beyond rdev_sectors */
2973 if (new_offset - rdev->data_offset
2974 + mddev->dev_sectors > rdev->sectors)
2975 return -E2BIG;
2976 }
2977 /* Metadata worries about other space details. */
2978
2979 /* decreasing the offset is inconsistent with a backwards
2980 * reshape.
2981 */
2982 if (new_offset < rdev->data_offset &&
2983 mddev->reshape_backwards)
2984 return -EINVAL;
2985 /* Increasing offset is inconsistent with forwards
2986 * reshape. reshape_direction should be set to
2987 * 'backwards' first.
2988 */
2989 if (new_offset > rdev->data_offset &&
2990 !mddev->reshape_backwards)
2991 return -EINVAL;
2992
2993 if (mddev->pers && mddev->persistent &&
2994 !super_types[mddev->major_version]
2995 .allow_new_offset(rdev, new_offset))
2996 return -E2BIG;
2997 rdev->new_data_offset = new_offset;
2998 if (new_offset > rdev->data_offset)
2999 mddev->reshape_backwards = 1;
3000 else if (new_offset < rdev->data_offset)
3001 mddev->reshape_backwards = 0;
3002
3003 return len;
3004}
3005static struct rdev_sysfs_entry rdev_new_offset =
3006__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3007
NeilBrown83303b62006-01-06 00:21:06 -08003008static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003009rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003010{
Andre Nolldd8ac332009-03-31 14:33:13 +11003011 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003012}
3013
NeilBrownc5d79ad2008-02-06 01:39:54 -08003014static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3015{
3016 /* check if two start/length pairs overlap */
3017 if (s1+l1 <= s2)
3018 return 0;
3019 if (s2+l2 <= s1)
3020 return 0;
3021 return 1;
3022}
3023
Dan Williamsb522adc2009-03-31 15:00:31 +11003024static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3025{
3026 unsigned long long blocks;
3027 sector_t new;
3028
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003029 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003030 return -EINVAL;
3031
3032 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3033 return -EINVAL; /* sector conversion overflow */
3034
3035 new = blocks * 2;
3036 if (new != blocks * 2)
3037 return -EINVAL; /* unsigned long long to sector_t overflow */
3038
3039 *sectors = new;
3040 return 0;
3041}
3042
NeilBrown83303b62006-01-06 00:21:06 -08003043static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003044rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003045{
NeilBrownfd01b882011-10-11 16:47:53 +11003046 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003047 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003048 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003049
Shaohua Lif2076e72015-10-08 21:54:12 -07003050 if (test_bit(Journal, &rdev->flags))
3051 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003052 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003053 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003054 if (rdev->data_offset != rdev->new_data_offset)
3055 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003056 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003057 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003058 sectors = super_types[my_mddev->major_version].
3059 rdev_size_change(rdev, sectors);
3060 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003061 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003062 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003063 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003064 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003065 if (!my_mddev->pers->resize)
3066 /* Cannot change size for RAID0 or Linear etc */
3067 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003068 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003069 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003070 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003071
Andre Nolldd8ac332009-03-31 14:33:13 +11003072 rdev->sectors = sectors;
3073 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003074 /* Need to check that all other rdevs with the same
3075 * ->bdev do not overlap. 'rcu' is sufficient to walk
3076 * the rdev lists safely.
3077 * This check does not provide a hard guarantee, it
3078 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003079 */
NeilBrownfd01b882011-10-11 16:47:53 +11003080 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003081 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003082 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003083
NeilBrown8b1afc32014-09-29 15:33:20 +10003084 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003085 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003086 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003087
NeilBrowndafb20f2012-03-19 12:46:39 +11003088 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003089 if (rdev->bdev == rdev2->bdev &&
3090 rdev != rdev2 &&
3091 overlaps(rdev->data_offset, rdev->sectors,
3092 rdev2->data_offset,
3093 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003094 overlap = 1;
3095 break;
3096 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003097 if (overlap) {
3098 mddev_put(mddev);
3099 break;
3100 }
3101 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003102 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003103 if (overlap) {
3104 /* Someone else could have slipped in a size
3105 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003106 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003107 * safe, and trust userspace not to race with
3108 * itself
3109 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003110 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003111 return -EBUSY;
3112 }
3113 }
NeilBrown83303b62006-01-06 00:21:06 -08003114 return len;
3115}
3116
3117static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003118__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003119
NeilBrown3cb03002011-10-11 16:45:26 +11003120static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003121{
3122 unsigned long long recovery_start = rdev->recovery_offset;
3123
3124 if (test_bit(In_sync, &rdev->flags) ||
3125 recovery_start == MaxSector)
3126 return sprintf(page, "none\n");
3127
3128 return sprintf(page, "%llu\n", recovery_start);
3129}
3130
NeilBrown3cb03002011-10-11 16:45:26 +11003131static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003132{
3133 unsigned long long recovery_start;
3134
3135 if (cmd_match(buf, "none"))
3136 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003137 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003138 return -EINVAL;
3139
3140 if (rdev->mddev->pers &&
3141 rdev->raid_disk >= 0)
3142 return -EBUSY;
3143
3144 rdev->recovery_offset = recovery_start;
3145 if (recovery_start == MaxSector)
3146 set_bit(In_sync, &rdev->flags);
3147 else
3148 clear_bit(In_sync, &rdev->flags);
3149 return len;
3150}
3151
3152static struct rdev_sysfs_entry rdev_recovery_start =
3153__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3154
Vishal Vermafc974ee2015-12-24 19:20:34 -07003155/* sysfs access to bad-blocks list.
3156 * We present two files.
3157 * 'bad-blocks' lists sector numbers and lengths of ranges that
3158 * are recorded as bad. The list is truncated to fit within
3159 * the one-page limit of sysfs.
3160 * Writing "sector length" to this file adds an acknowledged
3161 * bad block list.
3162 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3163 * been acknowledged. Writing to this file adds bad blocks
3164 * without acknowledging them. This is largely for testing.
3165 */
NeilBrown3cb03002011-10-11 16:45:26 +11003166static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003167{
3168 return badblocks_show(&rdev->badblocks, page, 0);
3169}
NeilBrown3cb03002011-10-11 16:45:26 +11003170static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003171{
NeilBrownde393cd2011-07-28 11:31:48 +10003172 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3173 /* Maybe that ack was all we needed */
3174 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3175 wake_up(&rdev->blocked_wait);
3176 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003177}
3178static struct rdev_sysfs_entry rdev_bad_blocks =
3179__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3180
NeilBrown3cb03002011-10-11 16:45:26 +11003181static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003182{
3183 return badblocks_show(&rdev->badblocks, page, 1);
3184}
NeilBrown3cb03002011-10-11 16:45:26 +11003185static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003186{
3187 return badblocks_store(&rdev->badblocks, page, len, 1);
3188}
3189static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3190__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3191
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003192static ssize_t
3193ppl_sector_show(struct md_rdev *rdev, char *page)
3194{
3195 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3196}
3197
3198static ssize_t
3199ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3200{
3201 unsigned long long sector;
3202
3203 if (kstrtoull(buf, 10, &sector) < 0)
3204 return -EINVAL;
3205 if (sector != (sector_t)sector)
3206 return -EINVAL;
3207
3208 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3209 rdev->raid_disk >= 0)
3210 return -EBUSY;
3211
3212 if (rdev->mddev->persistent) {
3213 if (rdev->mddev->major_version == 0)
3214 return -EINVAL;
3215 if ((sector > rdev->sb_start &&
3216 sector - rdev->sb_start > S16_MAX) ||
3217 (sector < rdev->sb_start &&
3218 rdev->sb_start - sector > -S16_MIN))
3219 return -EINVAL;
3220 rdev->ppl.offset = sector - rdev->sb_start;
3221 } else if (!rdev->mddev->external) {
3222 return -EBUSY;
3223 }
3224 rdev->ppl.sector = sector;
3225 return len;
3226}
3227
3228static struct rdev_sysfs_entry rdev_ppl_sector =
3229__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3230
3231static ssize_t
3232ppl_size_show(struct md_rdev *rdev, char *page)
3233{
3234 return sprintf(page, "%u\n", rdev->ppl.size);
3235}
3236
3237static ssize_t
3238ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3239{
3240 unsigned int size;
3241
3242 if (kstrtouint(buf, 10, &size) < 0)
3243 return -EINVAL;
3244
3245 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3246 rdev->raid_disk >= 0)
3247 return -EBUSY;
3248
3249 if (rdev->mddev->persistent) {
3250 if (rdev->mddev->major_version == 0)
3251 return -EINVAL;
3252 if (size > U16_MAX)
3253 return -EINVAL;
3254 } else if (!rdev->mddev->external) {
3255 return -EBUSY;
3256 }
3257 rdev->ppl.size = size;
3258 return len;
3259}
3260
3261static struct rdev_sysfs_entry rdev_ppl_size =
3262__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3263
NeilBrown86e6ffd2005-11-08 21:39:24 -08003264static struct attribute *rdev_default_attrs[] = {
3265 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003266 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003267 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003268 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003269 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003270 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003271 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003272 &rdev_bad_blocks.attr,
3273 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003274 &rdev_ppl_sector.attr,
3275 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003276 NULL,
3277};
3278static ssize_t
3279rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3280{
3281 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003282 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003283
3284 if (!entry->show)
3285 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003286 if (!rdev->mddev)
3287 return -EBUSY;
3288 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003289}
3290
3291static ssize_t
3292rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3293 const char *page, size_t length)
3294{
3295 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003296 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003297 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003298 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003299
3300 if (!entry->store)
3301 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003302 if (!capable(CAP_SYS_ADMIN))
3303 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003304 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003305 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003306 if (rdev->mddev == NULL)
3307 rv = -EBUSY;
3308 else
3309 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003310 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003311 }
3312 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003313}
3314
3315static void rdev_free(struct kobject *ko)
3316{
NeilBrown3cb03002011-10-11 16:45:26 +11003317 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003318 kfree(rdev);
3319}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003320static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003321 .show = rdev_attr_show,
3322 .store = rdev_attr_store,
3323};
3324static struct kobj_type rdev_ktype = {
3325 .release = rdev_free,
3326 .sysfs_ops = &rdev_sysfs_ops,
3327 .default_attrs = rdev_default_attrs,
3328};
3329
NeilBrown3cb03002011-10-11 16:45:26 +11003330int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003331{
3332 rdev->desc_nr = -1;
3333 rdev->saved_raid_disk = -1;
3334 rdev->raid_disk = -1;
3335 rdev->flags = 0;
3336 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003337 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003338 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003339 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003340 rdev->sb_loaded = 0;
3341 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003342 atomic_set(&rdev->nr_pending, 0);
3343 atomic_set(&rdev->read_errors, 0);
3344 atomic_set(&rdev->corrected_errors, 0);
3345
3346 INIT_LIST_HEAD(&rdev->same_set);
3347 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003348
3349 /* Add space to store bad block list.
3350 * This reserves the space even on arrays where it cannot
3351 * be used - I wonder if that matters
3352 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003353 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003354}
3355EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356/*
3357 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3358 *
3359 * mark the device faulty if:
3360 *
3361 * - the device is nonexistent (zero size)
3362 * - the device has no valid superblock
3363 *
3364 * a faulty rdev _never_ has rdev->sb set.
3365 */
NeilBrown3cb03002011-10-11 16:45:26 +11003366static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367{
3368 char b[BDEVNAME_SIZE];
3369 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003370 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 sector_t size;
3372
NeilBrown9ffae0c2006-01-06 00:20:32 -08003373 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003374 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
NeilBrown2230dfe2011-07-28 11:31:46 +10003377 err = md_rdev_init(rdev);
3378 if (err)
3379 goto abort_free;
3380 err = alloc_disk_sb(rdev);
3381 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 goto abort_free;
3383
NeilBrownc5d79ad2008-02-06 01:39:54 -08003384 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 if (err)
3386 goto abort_free;
3387
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003388 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003389
Mike Snitzer77304d22010-11-08 14:39:12 +01003390 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003392 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 bdevname(rdev->bdev,b));
3394 err = -EINVAL;
3395 goto abort_free;
3396 }
3397
3398 if (super_format >= 0) {
3399 err = super_types[super_format].
3400 load_super(rdev, NULL, super_minor);
3401 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003402 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003403 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003404 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 goto abort_free;
3406 }
3407 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003408 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 bdevname(rdev->bdev,b));
3410 goto abort_free;
3411 }
3412 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003413
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 return rdev;
3415
3416abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003417 if (rdev->bdev)
3418 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003419 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 kfree(rdev);
3421 return ERR_PTR(err);
3422}
3423
3424/*
3425 * Check a full RAID array for plausibility
3426 */
3427
NeilBrownf72ffdd2014-09-30 14:23:59 +10003428static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429{
3430 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003431 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 char b[BDEVNAME_SIZE];
3433
3434 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003435 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 switch (super_types[mddev->major_version].
3437 load_super(rdev, freshest, mddev->minor_version)) {
3438 case 1:
3439 freshest = rdev;
3440 break;
3441 case 0:
3442 break;
3443 default:
NeilBrown9d487392016-11-02 14:16:49 +11003444 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003446 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 }
3448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 super_types[mddev->major_version].
3450 validate_super(mddev, freshest);
3451
3452 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003453 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003454 if (mddev->max_disks &&
3455 (rdev->desc_nr >= mddev->max_disks ||
3456 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003457 pr_warn("md: %s: %s: only %d devices permitted\n",
3458 mdname(mddev), bdevname(rdev->bdev, b),
3459 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003460 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003461 continue;
3462 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003463 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 if (super_types[mddev->major_version].
3465 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003466 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003468 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 continue;
3470 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 if (mddev->level == LEVEL_MULTIPATH) {
3473 rdev->desc_nr = i++;
3474 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003475 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003476 } else if (rdev->raid_disk >=
3477 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3478 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003479 rdev->raid_disk = -1;
3480 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 }
3482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
NeilBrown72e02072009-12-14 12:49:55 +11003485/* Read a fixed-point number.
3486 * Numbers in sysfs attributes should be in "standard" units where
3487 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003488 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003489 * milliseconds or jiffies.
3490 * This function takes a decimal number with a possible fractional
3491 * component, and produces an integer which is the result of
3492 * multiplying that number by 10^'scale'.
3493 * all without any floating-point arithmetic.
3494 */
3495int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3496{
3497 unsigned long result = 0;
3498 long decimals = -1;
3499 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3500 if (*cp == '.')
3501 decimals = 0;
3502 else if (decimals < scale) {
3503 unsigned int value;
3504 value = *cp - '0';
3505 result = result * 10 + value;
3506 if (decimals >= 0)
3507 decimals++;
3508 }
3509 cp++;
3510 }
3511 if (*cp == '\n')
3512 cp++;
3513 if (*cp)
3514 return -EINVAL;
3515 if (decimals < 0)
3516 decimals = 0;
3517 while (decimals < scale) {
3518 result *= 10;
3519 decimals ++;
3520 }
3521 *res = result;
3522 return 0;
3523}
3524
NeilBrowneae17012005-11-08 21:39:23 -08003525static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003526safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003527{
3528 int msec = (mddev->safemode_delay*1000)/HZ;
3529 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3530}
3531static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003532safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003533{
NeilBrown16f17b32006-06-26 00:27:37 -07003534 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003535
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003536 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003537 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003538 return -EINVAL;
3539 }
3540
NeilBrown72e02072009-12-14 12:49:55 +11003541 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003542 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003543 if (msec == 0)
3544 mddev->safemode_delay = 0;
3545 else {
NeilBrown19052c02008-08-05 15:54:13 +10003546 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003547 unsigned long new_delay = (msec*HZ)/1000;
3548
3549 if (new_delay == 0)
3550 new_delay = 1;
3551 mddev->safemode_delay = new_delay;
3552 if (new_delay < old_delay || old_delay == 0)
3553 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003554 }
3555 return len;
3556}
3557static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003558__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003559
3560static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003561level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003562{
NeilBrown36d091f2014-12-15 12:56:58 +11003563 struct md_personality *p;
3564 int ret;
3565 spin_lock(&mddev->lock);
3566 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003567 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003568 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003569 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003570 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003571 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003572 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003573 else
NeilBrown36d091f2014-12-15 12:56:58 +11003574 ret = 0;
3575 spin_unlock(&mddev->lock);
3576 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003577}
3578
NeilBrownd9d166c2006-01-06 00:20:51 -08003579static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003580level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003581{
Dan Williamsf2859af2010-05-02 10:04:16 -07003582 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003583 ssize_t rv;
3584 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003585 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003586 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003587 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003588 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003589
NeilBrown67918752014-12-15 12:57:01 +11003590 if (slen == 0 || slen >= sizeof(clevel))
3591 return -EINVAL;
3592
3593 rv = mddev_lock(mddev);
3594 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003595 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003596
3597 if (mddev->pers == NULL) {
3598 strncpy(mddev->clevel, buf, slen);
3599 if (mddev->clevel[slen-1] == '\n')
3600 slen--;
3601 mddev->clevel[slen] = 0;
3602 mddev->level = LEVEL_NONE;
3603 rv = len;
3604 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003605 }
NeilBrown67918752014-12-15 12:57:01 +11003606 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003607 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003608 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003609
3610 /* request to change the personality. Need to ensure:
3611 * - array is not engaged in resync/recovery/reshape
3612 * - old personality can be suspended
3613 * - new personality will access other array.
3614 */
3615
NeilBrown67918752014-12-15 12:57:01 +11003616 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003617 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003618 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003619 mddev->reshape_position != MaxSector ||
3620 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003621 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003622
NeilBrown67918752014-12-15 12:57:01 +11003623 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003624 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003625 pr_warn("md: %s: %s does not support online personality change\n",
3626 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003627 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003628 }
3629
3630 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003631 strncpy(clevel, buf, slen);
3632 if (clevel[slen-1] == '\n')
3633 slen--;
3634 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003635 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003636 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003637
Dan Williamsf2859af2010-05-02 10:04:16 -07003638 if (request_module("md-%s", clevel) != 0)
3639 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003640 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003641 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003642 if (!pers || !try_module_get(pers->owner)) {
3643 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003644 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003645 rv = -EINVAL;
3646 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003647 }
3648 spin_unlock(&pers_lock);
3649
3650 if (pers == mddev->pers) {
3651 /* Nothing to do! */
3652 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003653 rv = len;
3654 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003655 }
3656 if (!pers->takeover) {
3657 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003658 pr_warn("md: %s: %s does not support personality takeover\n",
3659 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003660 rv = -EINVAL;
3661 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003662 }
3663
NeilBrowndafb20f2012-03-19 12:46:39 +11003664 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003665 rdev->new_raid_disk = rdev->raid_disk;
3666
NeilBrown245f46c2009-03-31 14:39:39 +11003667 /* ->takeover must set new_* and/or delta_disks
3668 * if it succeeds, and may set them when it fails.
3669 */
3670 priv = pers->takeover(mddev);
3671 if (IS_ERR(priv)) {
3672 mddev->new_level = mddev->level;
3673 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003674 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003675 mddev->raid_disks -= mddev->delta_disks;
3676 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003677 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003678 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003679 pr_warn("md: %s: %s would not accept array\n",
3680 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003681 rv = PTR_ERR(priv);
3682 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003683 }
3684
3685 /* Looks like we have a winner */
3686 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003687 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003688
3689 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003690 oldpers = mddev->pers;
3691 oldpriv = mddev->private;
3692 mddev->pers = pers;
3693 mddev->private = priv;
3694 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3695 mddev->level = mddev->new_level;
3696 mddev->layout = mddev->new_layout;
3697 mddev->chunk_sectors = mddev->new_chunk_sectors;
3698 mddev->delta_disks = 0;
3699 mddev->reshape_backwards = 0;
3700 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003701 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003702
NeilBrowndb721d32014-12-15 12:56:58 +11003703 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003704 mddev->external) {
3705 /* We are converting from a no-redundancy array
3706 * to a redundancy array and metadata is managed
3707 * externally so we need to be sure that writes
3708 * won't block due to a need to transition
3709 * clean->dirty
3710 * until external management is started.
3711 */
3712 mddev->in_sync = 0;
3713 mddev->safemode_delay = 0;
3714 mddev->safemode = 0;
3715 }
3716
NeilBrowndb721d32014-12-15 12:56:58 +11003717 oldpers->free(mddev, oldpriv);
3718
3719 if (oldpers->sync_request == NULL &&
3720 pers->sync_request != NULL) {
3721 /* need to add the md_redundancy_group */
3722 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11003723 pr_warn("md: cannot register extra attributes for %s\n",
3724 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11003725 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3726 }
3727 if (oldpers->sync_request != NULL &&
3728 pers->sync_request == NULL) {
3729 /* need to remove the md_redundancy_group */
3730 if (mddev->to_remove == NULL)
3731 mddev->to_remove = &md_redundancy_group;
3732 }
3733
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003734 module_put(oldpers->owner);
3735
NeilBrowndafb20f2012-03-19 12:46:39 +11003736 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003737 if (rdev->raid_disk < 0)
3738 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003739 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003740 rdev->new_raid_disk = -1;
3741 if (rdev->new_raid_disk == rdev->raid_disk)
3742 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003743 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003744 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003745 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003746 if (rdev->raid_disk < 0)
3747 continue;
3748 if (rdev->new_raid_disk == rdev->raid_disk)
3749 continue;
3750 rdev->raid_disk = rdev->new_raid_disk;
3751 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003752 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003753 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003754 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11003755 pr_warn("md: cannot register rd%d for %s after level change\n",
3756 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003757 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003758 }
3759
NeilBrowndb721d32014-12-15 12:56:58 +11003760 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003761 /* this is now an array without redundancy, so
3762 * it must always be in_sync
3763 */
3764 mddev->in_sync = 1;
3765 del_timer_sync(&mddev->safemode_timer);
3766 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003767 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003768 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08003769 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003770 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003771 if (!mddev->thread)
3772 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003773 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003774 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003775 rv = len;
3776out_unlock:
3777 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003778 return rv;
3779}
3780
3781static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003782__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003783
NeilBrownd4dbd022006-06-26 00:27:59 -07003784static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003785layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003786{
3787 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003788 if (mddev->reshape_position != MaxSector &&
3789 mddev->layout != mddev->new_layout)
3790 return sprintf(page, "%d (%d)\n",
3791 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003792 return sprintf(page, "%d\n", mddev->layout);
3793}
3794
3795static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003796layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003797{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003798 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003799 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003800
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003801 err = kstrtouint(buf, 10, &n);
3802 if (err < 0)
3803 return err;
NeilBrown67918752014-12-15 12:57:01 +11003804 err = mddev_lock(mddev);
3805 if (err)
3806 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003807
NeilBrownb3546032009-03-31 14:56:41 +11003808 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003809 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003810 err = -EBUSY;
3811 else if (mddev->ro)
3812 err = -EROFS;
3813 else {
3814 mddev->new_layout = n;
3815 err = mddev->pers->check_reshape(mddev);
3816 if (err)
3817 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003818 }
NeilBrownb3546032009-03-31 14:56:41 +11003819 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003820 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003821 if (mddev->reshape_position == MaxSector)
3822 mddev->layout = n;
3823 }
NeilBrown67918752014-12-15 12:57:01 +11003824 mddev_unlock(mddev);
3825 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003826}
3827static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003828__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003829
NeilBrowneae17012005-11-08 21:39:23 -08003830static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003831raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003832{
NeilBrownbb636542005-11-08 21:39:45 -08003833 if (mddev->raid_disks == 0)
3834 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003835 if (mddev->reshape_position != MaxSector &&
3836 mddev->delta_disks != 0)
3837 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3838 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003839 return sprintf(page, "%d\n", mddev->raid_disks);
3840}
3841
NeilBrownfd01b882011-10-11 16:47:53 +11003842static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003843
3844static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003845raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003846{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003847 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003848 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003849
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003850 err = kstrtouint(buf, 10, &n);
3851 if (err < 0)
3852 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003853
NeilBrown67918752014-12-15 12:57:01 +11003854 err = mddev_lock(mddev);
3855 if (err)
3856 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003857 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003858 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003859 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003860 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003861 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003862
NeilBrown67918752014-12-15 12:57:01 +11003863 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003864 rdev_for_each(rdev, mddev) {
3865 if (olddisks < n &&
3866 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003867 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003868 if (olddisks > n &&
3869 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003870 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003871 }
NeilBrown67918752014-12-15 12:57:01 +11003872 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003873 mddev->delta_disks = n - olddisks;
3874 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003875 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003876 } else
NeilBrownda943b992006-01-06 00:20:54 -08003877 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003878out_unlock:
3879 mddev_unlock(mddev);
3880 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003881}
3882static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003883__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003884
NeilBrown24dd4692005-11-08 21:39:26 -08003885static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003886chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003887{
NeilBrown08a02ec2007-05-09 02:35:38 -07003888 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003889 mddev->chunk_sectors != mddev->new_chunk_sectors)
3890 return sprintf(page, "%d (%d)\n",
3891 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003892 mddev->chunk_sectors << 9);
3893 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003894}
3895
3896static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003897chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003898{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003899 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003900 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003901
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003902 err = kstrtoul(buf, 10, &n);
3903 if (err < 0)
3904 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003905
NeilBrown67918752014-12-15 12:57:01 +11003906 err = mddev_lock(mddev);
3907 if (err)
3908 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003909 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003910 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003911 err = -EBUSY;
3912 else if (mddev->ro)
3913 err = -EROFS;
3914 else {
3915 mddev->new_chunk_sectors = n >> 9;
3916 err = mddev->pers->check_reshape(mddev);
3917 if (err)
3918 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003919 }
NeilBrownb3546032009-03-31 14:56:41 +11003920 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003921 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003922 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003923 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003924 }
NeilBrown67918752014-12-15 12:57:01 +11003925 mddev_unlock(mddev);
3926 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003927}
3928static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003929__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003930
NeilBrowna94213b2006-06-26 00:28:00 -07003931static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003932resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003933{
NeilBrownd1a7c502009-03-31 15:24:32 +11003934 if (mddev->recovery_cp == MaxSector)
3935 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003936 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3937}
3938
3939static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003940resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003941{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003942 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003943 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003944
3945 if (cmd_match(buf, "none"))
3946 n = MaxSector;
3947 else {
3948 err = kstrtoull(buf, 10, &n);
3949 if (err < 0)
3950 return err;
3951 if (n != (sector_t)n)
3952 return -EINVAL;
3953 }
NeilBrowna94213b2006-06-26 00:28:00 -07003954
NeilBrown67918752014-12-15 12:57:01 +11003955 err = mddev_lock(mddev);
3956 if (err)
3957 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003958 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003959 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003960
NeilBrown67918752014-12-15 12:57:01 +11003961 if (!err) {
3962 mddev->recovery_cp = n;
3963 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08003964 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11003965 }
3966 mddev_unlock(mddev);
3967 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003968}
3969static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003970__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3971 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003972
NeilBrown9e653b62006-06-26 00:27:58 -07003973/*
3974 * The array state can be:
3975 *
3976 * clear
3977 * No devices, no size, no level
3978 * Equivalent to STOP_ARRAY ioctl
3979 * inactive
3980 * May have some settings, but array is not active
3981 * all IO results in error
3982 * When written, doesn't tear down array, but just stops it
3983 * suspended (not supported yet)
3984 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003985 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003986 * readonly
3987 * no resync can happen. no superblocks get written.
3988 * write requests fail
3989 * read-auto
3990 * like readonly, but behaves like 'clean' on a write request.
3991 *
3992 * clean - no pending writes, but otherwise active.
3993 * When written to inactive array, starts without resync
3994 * If a write request arrives then
3995 * if metadata is known, mark 'dirty' and switch to 'active'.
3996 * if not known, block and switch to write-pending
3997 * If written to an active array that has pending writes, then fails.
3998 * active
3999 * fully active: IO and resync can be happening.
4000 * When written to inactive array, starts with resync
4001 *
4002 * write-pending
4003 * clean, but writes are blocked waiting for 'active' to be written.
4004 *
4005 * active-idle
4006 * like active, but no writes have been seen for a while (100msec).
4007 *
4008 */
4009enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4010 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004011static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004012 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4013 "write-pending", "active-idle", NULL };
4014
4015static int match_word(const char *word, char **list)
4016{
4017 int n;
4018 for (n=0; list[n]; n++)
4019 if (cmd_match(word, list[n]))
4020 break;
4021 return n;
4022}
4023
4024static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004025array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004026{
4027 enum array_state st = inactive;
4028
4029 if (mddev->pers)
4030 switch(mddev->ro) {
4031 case 1:
4032 st = readonly;
4033 break;
4034 case 2:
4035 st = read_auto;
4036 break;
4037 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004038 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004039 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004040 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004041 else if (mddev->in_sync)
4042 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004043 else if (mddev->safemode)
4044 st = active_idle;
4045 else
4046 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004047 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004048 }
4049 else {
4050 if (list_empty(&mddev->disks) &&
4051 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004052 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004053 st = clear;
4054 else
4055 st = inactive;
4056 }
4057 return sprintf(page, "%s\n", array_states[st]);
4058}
4059
NeilBrownf72ffdd2014-09-30 14:23:59 +10004060static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4061static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4062static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004063static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004064
4065static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004066array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004067{
NeilBrown6497709b2017-03-15 14:05:14 +11004068 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004069 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004070
4071 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4072 /* don't take reconfig_mutex when toggling between
4073 * clean and active
4074 */
4075 spin_lock(&mddev->lock);
4076 if (st == active) {
4077 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004078 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004079 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004080 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004081 } else /* st == clean */ {
4082 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004083 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004084 err = -EBUSY;
4085 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004086 if (!err)
4087 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004088 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004089 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004090 }
4091 err = mddev_lock(mddev);
4092 if (err)
4093 return err;
4094 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004095 switch(st) {
4096 case bad_word:
4097 break;
4098 case clear:
4099 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004100 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004101 break;
4102 case inactive:
4103 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004104 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004105 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004106 else
NeilBrowne6910632008-02-06 01:39:51 -08004107 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004108 break;
4109 case suspended:
4110 break; /* not supported yet */
4111 case readonly:
4112 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004113 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004114 else {
4115 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004116 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004117 err = do_md_run(mddev);
4118 }
4119 break;
4120 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004121 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004122 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004123 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004124 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004125 err = restart_array(mddev);
4126 if (err == 0) {
4127 mddev->ro = 2;
4128 set_disk_ro(mddev->gendisk, 0);
4129 }
NeilBrown9e653b62006-06-26 00:27:58 -07004130 } else {
4131 mddev->ro = 2;
4132 err = do_md_run(mddev);
4133 }
4134 break;
4135 case clean:
4136 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004137 err = restart_array(mddev);
4138 if (err)
4139 break;
NeilBrown85572d72014-12-15 12:56:56 +11004140 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004141 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004142 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004143 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004144 } else
4145 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004146 break;
4147 case active:
4148 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004149 err = restart_array(mddev);
4150 if (err)
4151 break;
Shaohua Li29530792016-12-08 15:48:19 -08004152 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004153 wake_up(&mddev->sb_wait);
4154 err = 0;
4155 } else {
4156 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004157 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004158 err = do_md_run(mddev);
4159 }
4160 break;
4161 case write_pending:
4162 case active_idle:
4163 /* these cannot be set */
4164 break;
4165 }
NeilBrown67918752014-12-15 12:57:01 +11004166
4167 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004168 if (mddev->hold_active == UNTIL_IOCTL)
4169 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004170 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004171 }
NeilBrown67918752014-12-15 12:57:01 +11004172 mddev_unlock(mddev);
4173 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004174}
NeilBrown80ca3a42006-07-10 04:44:18 -07004175static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004176__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004177
NeilBrown6d7ff7382006-01-06 00:21:16 -08004178static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004179max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004180 return sprintf(page, "%d\n",
4181 atomic_read(&mddev->max_corr_read_errors));
4182}
4183
4184static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004185max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004186{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004187 unsigned int n;
4188 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004189
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004190 rv = kstrtouint(buf, 10, &n);
4191 if (rv < 0)
4192 return rv;
4193 atomic_set(&mddev->max_corr_read_errors, n);
4194 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004195}
4196
4197static struct md_sysfs_entry max_corr_read_errors =
4198__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4199 max_corrected_read_errors_store);
4200
4201static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004202null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004203{
4204 return -EINVAL;
4205}
4206
4207static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004208new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004209{
4210 /* buf must be %d:%d\n? giving major and minor numbers */
4211 /* The new device is added to the array.
4212 * If the array has a persistent superblock, we read the
4213 * superblock to initialise info and check validity.
4214 * Otherwise, only checking done is that in bind_rdev_to_array,
4215 * which mainly checks size.
4216 */
4217 char *e;
4218 int major = simple_strtoul(buf, &e, 10);
4219 int minor;
4220 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004221 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004222 int err;
4223
4224 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4225 return -EINVAL;
4226 minor = simple_strtoul(e+1, &e, 10);
4227 if (*e && *e != '\n')
4228 return -EINVAL;
4229 dev = MKDEV(major, minor);
4230 if (major != MAJOR(dev) ||
4231 minor != MINOR(dev))
4232 return -EOVERFLOW;
4233
NeilBrown67918752014-12-15 12:57:01 +11004234 flush_workqueue(md_misc_wq);
4235
4236 err = mddev_lock(mddev);
4237 if (err)
4238 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004239 if (mddev->persistent) {
4240 rdev = md_import_device(dev, mddev->major_version,
4241 mddev->minor_version);
4242 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004243 struct md_rdev *rdev0
4244 = list_entry(mddev->disks.next,
4245 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004246 err = super_types[mddev->major_version]
4247 .load_super(rdev, rdev0, mddev->minor_version);
4248 if (err < 0)
4249 goto out;
4250 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004251 } else if (mddev->external)
4252 rdev = md_import_device(dev, -2, -1);
4253 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004254 rdev = md_import_device(dev, -1, -1);
4255
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004256 if (IS_ERR(rdev)) {
4257 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004258 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004259 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004260 err = bind_rdev_to_array(rdev, mddev);
4261 out:
4262 if (err)
4263 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004264 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004265 return err ? err : len;
4266}
4267
4268static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004269__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004270
4271static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004272bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004273{
4274 char *end;
4275 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004276 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004277
NeilBrown67918752014-12-15 12:57:01 +11004278 err = mddev_lock(mddev);
4279 if (err)
4280 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004281 if (!mddev->bitmap)
4282 goto out;
4283 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4284 while (*buf) {
4285 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4286 if (buf == end) break;
4287 if (*end == '-') { /* range */
4288 buf = end + 1;
4289 end_chunk = simple_strtoul(buf, &end, 0);
4290 if (buf == end) break;
4291 }
4292 if (*end && !isspace(*end)) break;
4293 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004294 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004295 }
4296 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4297out:
NeilBrown67918752014-12-15 12:57:01 +11004298 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004299 return len;
4300}
4301
4302static struct md_sysfs_entry md_bitmap =
4303__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4304
4305static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004306size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004307{
Andre Noll58c0fed2009-03-31 14:33:13 +11004308 return sprintf(page, "%llu\n",
4309 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004310}
4311
NeilBrownfd01b882011-10-11 16:47:53 +11004312static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004313
4314static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004315size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004316{
4317 /* If array is inactive, we can reduce the component size, but
4318 * not increase it (except from 0).
4319 * If array is active, we can try an on-line resize
4320 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004321 sector_t sectors;
4322 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004323
Andre Noll58c0fed2009-03-31 14:33:13 +11004324 if (err < 0)
4325 return err;
NeilBrown67918752014-12-15 12:57:01 +11004326 err = mddev_lock(mddev);
4327 if (err)
4328 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004329 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004330 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004331 if (err == 0)
4332 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004333 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004334 if (mddev->dev_sectors == 0 ||
4335 mddev->dev_sectors > sectors)
4336 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004337 else
4338 err = -ENOSPC;
4339 }
NeilBrown67918752014-12-15 12:57:01 +11004340 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004341 return err ? err : len;
4342}
4343
4344static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004345__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004346
Masanari Iida83f0d772012-10-30 00:18:08 +09004347/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004348 * This is one of
4349 * 'none' for arrays with no metadata (good luck...)
4350 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004351 * or N.M for internally known formats
4352 */
4353static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004354metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004355{
4356 if (mddev->persistent)
4357 return sprintf(page, "%d.%d\n",
4358 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004359 else if (mddev->external)
4360 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004361 else
4362 return sprintf(page, "none\n");
4363}
4364
4365static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004366metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004367{
4368 int major, minor;
4369 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004370 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004371 /* Changing the details of 'external' metadata is
4372 * always permitted. Otherwise there must be
4373 * no devices attached to the array.
4374 */
NeilBrown67918752014-12-15 12:57:01 +11004375
4376 err = mddev_lock(mddev);
4377 if (err)
4378 return err;
4379 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004380 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4381 ;
4382 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004383 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004384
NeilBrown67918752014-12-15 12:57:01 +11004385 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004386 if (cmd_match(buf, "none")) {
4387 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004388 mddev->external = 0;
4389 mddev->major_version = 0;
4390 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004391 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004392 }
4393 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004394 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004395 if (namelen >= sizeof(mddev->metadata_type))
4396 namelen = sizeof(mddev->metadata_type)-1;
4397 strncpy(mddev->metadata_type, buf+9, namelen);
4398 mddev->metadata_type[namelen] = 0;
4399 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4400 mddev->metadata_type[--namelen] = 0;
4401 mddev->persistent = 0;
4402 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004403 mddev->major_version = 0;
4404 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004405 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004406 }
4407 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004408 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004409 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004410 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004411 buf = e+1;
4412 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004413 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004414 goto out_unlock;
4415 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004416 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004417 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004418 mddev->major_version = major;
4419 mddev->minor_version = minor;
4420 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004421 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004422 err = 0;
4423out_unlock:
4424 mddev_unlock(mddev);
4425 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004426}
4427
4428static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004429__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004430
NeilBrowna35b0d62006-01-06 00:20:49 -08004431static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004432action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004433{
NeilBrown7eec3142005-11-08 21:39:44 -08004434 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004435 unsigned long recovery = mddev->recovery;
4436 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004437 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004438 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4439 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4440 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004441 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004442 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4443 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004444 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004445 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004446 type = "check";
4447 else
4448 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004449 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004450 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004451 else if (mddev->reshape_position != MaxSector)
4452 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004453 }
4454 return sprintf(page, "%s\n", type);
4455}
4456
4457static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004458action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004459{
NeilBrown7eec3142005-11-08 21:39:44 -08004460 if (!mddev->pers || !mddev->pers->sync_request)
4461 return -EINVAL;
4462
NeilBrownb6a9ce62009-05-26 09:41:17 +10004463
4464 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004465 if (cmd_match(page, "frozen"))
4466 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4467 else
4468 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004469 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4470 mddev_lock(mddev) == 0) {
4471 flush_workqueue(md_misc_wq);
4472 if (mddev->sync_thread) {
4473 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004474 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004475 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004476 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004477 }
NeilBrown312045e2015-12-21 11:01:21 +11004478 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004479 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004480 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004481 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004482 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004483 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004484 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004485 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004486 int err;
4487 if (mddev->pers->start_reshape == NULL)
4488 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004489 err = mddev_lock(mddev);
4490 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004491 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4492 err = -EBUSY;
4493 else {
4494 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4495 err = mddev->pers->start_reshape(mddev);
4496 }
NeilBrown67918752014-12-15 12:57:01 +11004497 mddev_unlock(mddev);
4498 }
NeilBrown16484bf2006-03-27 01:18:13 -08004499 if (err)
4500 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004501 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004502 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004503 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004504 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004505 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004506 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004507 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004508 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4509 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004510 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004511 if (mddev->ro == 2) {
4512 /* A write to sync_action is enough to justify
4513 * canceling read-auto mode
4514 */
4515 mddev->ro = 0;
4516 md_wakeup_thread(mddev->sync_thread);
4517 }
NeilBrown03c902e2006-01-06 00:20:46 -08004518 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004519 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004520 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004521 return len;
4522}
4523
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004524static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004525__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004526
4527static ssize_t
4528last_sync_action_show(struct mddev *mddev, char *page)
4529{
4530 return sprintf(page, "%s\n", mddev->last_sync_action);
4531}
4532
4533static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4534
NeilBrown9d888832005-11-08 21:39:26 -08004535static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004536mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004537{
4538 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004539 (unsigned long long)
4540 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004541}
4542
NeilBrown80ca3a42006-07-10 04:44:18 -07004543static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004544
NeilBrown88202a02006-01-06 00:21:36 -08004545static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004546sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004547{
4548 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4549 mddev->sync_speed_min ? "local": "system");
4550}
4551
4552static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004553sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004554{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004555 unsigned int min;
4556 int rv;
4557
NeilBrown88202a02006-01-06 00:21:36 -08004558 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004559 min = 0;
4560 } else {
4561 rv = kstrtouint(buf, 10, &min);
4562 if (rv < 0)
4563 return rv;
4564 if (min == 0)
4565 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004566 }
NeilBrown88202a02006-01-06 00:21:36 -08004567 mddev->sync_speed_min = min;
4568 return len;
4569}
4570
4571static struct md_sysfs_entry md_sync_min =
4572__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4573
4574static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004575sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004576{
4577 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4578 mddev->sync_speed_max ? "local": "system");
4579}
4580
4581static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004582sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004583{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004584 unsigned int max;
4585 int rv;
4586
NeilBrown88202a02006-01-06 00:21:36 -08004587 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004588 max = 0;
4589 } else {
4590 rv = kstrtouint(buf, 10, &max);
4591 if (rv < 0)
4592 return rv;
4593 if (max == 0)
4594 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004595 }
NeilBrown88202a02006-01-06 00:21:36 -08004596 mddev->sync_speed_max = max;
4597 return len;
4598}
4599
4600static struct md_sysfs_entry md_sync_max =
4601__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4602
Iustin Popd7f3d292007-10-16 23:30:54 -07004603static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004604degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004605{
4606 return sprintf(page, "%d\n", mddev->degraded);
4607}
4608static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004609
4610static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004611sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004612{
4613 return sprintf(page, "%d\n", mddev->parallel_resync);
4614}
4615
4616static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004617sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004618{
4619 long n;
4620
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004621 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004622 return -EINVAL;
4623
4624 if (n != 0 && n != 1)
4625 return -EINVAL;
4626
4627 mddev->parallel_resync = n;
4628
4629 if (mddev->sync_thread)
4630 wake_up(&resync_wait);
4631
4632 return len;
4633}
4634
4635/* force parallel resync, even with shared block devices */
4636static struct md_sysfs_entry md_sync_force_parallel =
4637__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4638 sync_force_parallel_show, sync_force_parallel_store);
4639
4640static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004641sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004642{
4643 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004644 if (mddev->curr_resync == 0)
4645 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004646 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4647 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004648 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004649 db = resync - mddev->resync_mark_cnt;
4650 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004651}
4652
NeilBrown80ca3a42006-07-10 04:44:18 -07004653static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004654
4655static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004656sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004657{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004658 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004659
NeilBrownacb180b2009-04-14 16:28:34 +10004660 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4661 return sprintf(page, "none\n");
4662
NeilBrown72f36d52012-10-11 14:25:57 +11004663 if (mddev->curr_resync == 1 ||
4664 mddev->curr_resync == 2)
4665 return sprintf(page, "delayed\n");
4666
NeilBrownc804cde2012-05-21 09:28:33 +10004667 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4668 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004669 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004670 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004671 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004672
NeilBrownacb180b2009-04-14 16:28:34 +10004673 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004674 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004675}
4676
NeilBrown750f1992014-09-30 08:53:05 +10004677static struct md_sysfs_entry md_sync_completed =
4678 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004679
NeilBrowne464eaf2006-03-27 01:18:14 -08004680static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004681min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004682{
4683 return sprintf(page, "%llu\n",
4684 (unsigned long long)mddev->resync_min);
4685}
4686static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004687min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004688{
4689 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004690 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004691
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004692 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004693 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004694
4695 spin_lock(&mddev->lock);
4696 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004697 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004698 goto out_unlock;
4699
4700 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004701 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004702 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004703
NeilBrown50c37b12015-03-23 17:36:38 +11004704 /* Round down to multiple of 4K for safety */
4705 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004706 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004707
NeilBrown23da4222014-12-15 12:57:01 +11004708out_unlock:
4709 spin_unlock(&mddev->lock);
4710 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004711}
4712
4713static struct md_sysfs_entry md_min_sync =
4714__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4715
4716static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004717max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004718{
4719 if (mddev->resync_max == MaxSector)
4720 return sprintf(page, "max\n");
4721 else
4722 return sprintf(page, "%llu\n",
4723 (unsigned long long)mddev->resync_max);
4724}
4725static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004726max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004727{
NeilBrown23da4222014-12-15 12:57:01 +11004728 int err;
4729 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004730 if (strncmp(buf, "max", 3) == 0)
4731 mddev->resync_max = MaxSector;
4732 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004733 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004734 int chunk;
4735
4736 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004737 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004738 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004739 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004740 goto out_unlock;
4741
4742 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004743 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004744 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004745 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004746 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004747
4748 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004749 chunk = mddev->chunk_sectors;
4750 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004751 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004752
4753 err = -EINVAL;
4754 if (sector_div(temp, chunk))
4755 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004756 }
4757 mddev->resync_max = max;
4758 }
4759 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004760 err = 0;
4761out_unlock:
4762 spin_unlock(&mddev->lock);
4763 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004764}
4765
4766static struct md_sysfs_entry md_max_sync =
4767__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4768
4769static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004770suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004771{
4772 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4773}
4774
4775static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004776suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004777{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004778 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004779 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004780
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004781 err = kstrtoull(buf, 10, &new);
4782 if (err < 0)
4783 return err;
4784 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004785 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004786
NeilBrown67918752014-12-15 12:57:01 +11004787 err = mddev_lock(mddev);
4788 if (err)
4789 return err;
4790 err = -EINVAL;
4791 if (mddev->pers == NULL ||
4792 mddev->pers->quiesce == NULL)
4793 goto unlock;
4794 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004795 mddev->suspend_lo = new;
4796 if (new >= old)
4797 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004798 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004799 else {
4800 /* Expanding suspended region - need to wait */
4801 mddev->pers->quiesce(mddev, 1);
4802 mddev->pers->quiesce(mddev, 0);
4803 }
NeilBrown67918752014-12-15 12:57:01 +11004804 err = 0;
4805unlock:
4806 mddev_unlock(mddev);
4807 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004808}
4809static struct md_sysfs_entry md_suspend_lo =
4810__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4811
NeilBrowne464eaf2006-03-27 01:18:14 -08004812static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004813suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004814{
4815 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4816}
4817
4818static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004819suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004820{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004821 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004822 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004823
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004824 err = kstrtoull(buf, 10, &new);
4825 if (err < 0)
4826 return err;
4827 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004828 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004829
NeilBrown67918752014-12-15 12:57:01 +11004830 err = mddev_lock(mddev);
4831 if (err)
4832 return err;
4833 err = -EINVAL;
4834 if (mddev->pers == NULL ||
4835 mddev->pers->quiesce == NULL)
4836 goto unlock;
4837 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004838 mddev->suspend_hi = new;
4839 if (new <= old)
4840 /* Shrinking suspended region */
4841 mddev->pers->quiesce(mddev, 2);
4842 else {
4843 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004844 mddev->pers->quiesce(mddev, 1);
4845 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004846 }
NeilBrown67918752014-12-15 12:57:01 +11004847 err = 0;
4848unlock:
4849 mddev_unlock(mddev);
4850 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004851}
4852static struct md_sysfs_entry md_suspend_hi =
4853__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4854
NeilBrown08a02ec2007-05-09 02:35:38 -07004855static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004856reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004857{
4858 if (mddev->reshape_position != MaxSector)
4859 return sprintf(page, "%llu\n",
4860 (unsigned long long)mddev->reshape_position);
4861 strcpy(page, "none\n");
4862 return 5;
4863}
4864
4865static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004866reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004867{
NeilBrownc6563a82012-05-21 09:27:00 +10004868 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004869 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004870 int err;
NeilBrown67918752014-12-15 12:57:01 +11004871
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004872 err = kstrtoull(buf, 10, &new);
4873 if (err < 0)
4874 return err;
4875 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004876 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004877 err = mddev_lock(mddev);
4878 if (err)
4879 return err;
4880 err = -EBUSY;
4881 if (mddev->pers)
4882 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004883 mddev->reshape_position = new;
4884 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004885 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004886 mddev->new_level = mddev->level;
4887 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004888 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004889 rdev_for_each(rdev, mddev)
4890 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004891 err = 0;
4892unlock:
4893 mddev_unlock(mddev);
4894 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004895}
4896
4897static struct md_sysfs_entry md_reshape_position =
4898__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4899 reshape_position_store);
4900
Dan Williamsb522adc2009-03-31 15:00:31 +11004901static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004902reshape_direction_show(struct mddev *mddev, char *page)
4903{
4904 return sprintf(page, "%s\n",
4905 mddev->reshape_backwards ? "backwards" : "forwards");
4906}
4907
4908static ssize_t
4909reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4910{
4911 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004912 int err;
4913
NeilBrown2c810cd2012-05-21 09:27:00 +10004914 if (cmd_match(buf, "forwards"))
4915 backwards = 0;
4916 else if (cmd_match(buf, "backwards"))
4917 backwards = 1;
4918 else
4919 return -EINVAL;
4920 if (mddev->reshape_backwards == backwards)
4921 return len;
4922
NeilBrown67918752014-12-15 12:57:01 +11004923 err = mddev_lock(mddev);
4924 if (err)
4925 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004926 /* check if we are allowed to change */
4927 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004928 err = -EBUSY;
4929 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004930 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004931 err = -EINVAL;
4932 else
4933 mddev->reshape_backwards = backwards;
4934 mddev_unlock(mddev);
4935 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004936}
4937
4938static struct md_sysfs_entry md_reshape_direction =
4939__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4940 reshape_direction_store);
4941
4942static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004943array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004944{
4945 if (mddev->external_size)
4946 return sprintf(page, "%llu\n",
4947 (unsigned long long)mddev->array_sectors/2);
4948 else
4949 return sprintf(page, "default\n");
4950}
4951
4952static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004953array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004954{
4955 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004956 int err;
4957
4958 err = mddev_lock(mddev);
4959 if (err)
4960 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004961
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004962 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08004963 if (mddev_is_clustered(mddev)) {
4964 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004965 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08004966 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004967
Dan Williamsb522adc2009-03-31 15:00:31 +11004968 if (strncmp(buf, "default", 7) == 0) {
4969 if (mddev->pers)
4970 sectors = mddev->pers->size(mddev, 0, 0);
4971 else
4972 sectors = mddev->array_sectors;
4973
4974 mddev->external_size = 0;
4975 } else {
4976 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004977 err = -EINVAL;
4978 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4979 err = -E2BIG;
4980 else
4981 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004982 }
4983
NeilBrown67918752014-12-15 12:57:01 +11004984 if (!err) {
4985 mddev->array_sectors = sectors;
4986 if (mddev->pers) {
4987 set_capacity(mddev->gendisk, mddev->array_sectors);
4988 revalidate_disk(mddev->gendisk);
4989 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004990 }
NeilBrown67918752014-12-15 12:57:01 +11004991 mddev_unlock(mddev);
4992 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004993}
4994
4995static struct md_sysfs_entry md_array_size =
4996__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4997 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004998
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01004999static ssize_t
5000consistency_policy_show(struct mddev *mddev, char *page)
5001{
5002 int ret;
5003
5004 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5005 ret = sprintf(page, "journal\n");
5006 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5007 ret = sprintf(page, "ppl\n");
5008 } else if (mddev->bitmap) {
5009 ret = sprintf(page, "bitmap\n");
5010 } else if (mddev->pers) {
5011 if (mddev->pers->sync_request)
5012 ret = sprintf(page, "resync\n");
5013 else
5014 ret = sprintf(page, "none\n");
5015 } else {
5016 ret = sprintf(page, "unknown\n");
5017 }
5018
5019 return ret;
5020}
5021
5022static ssize_t
5023consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5024{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005025 int err = 0;
5026
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005027 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005028 if (mddev->pers->change_consistency_policy)
5029 err = mddev->pers->change_consistency_policy(mddev, buf);
5030 else
5031 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005032 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5033 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005034 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005035 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005036 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005037
5038 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005039}
5040
5041static struct md_sysfs_entry md_consistency_policy =
5042__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5043 consistency_policy_store);
5044
NeilBrowneae17012005-11-08 21:39:23 -08005045static struct attribute *md_default_attrs[] = {
5046 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005047 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005048 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005049 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005050 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005051 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005052 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005053 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005054 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005055 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005056 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005057 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005058 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005059 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005060 &md_consistency_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005061 NULL,
5062};
5063
5064static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005065 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005066 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005067 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005068 &md_sync_min.attr,
5069 &md_sync_max.attr,
5070 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005071 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005072 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005073 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005074 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005075 &md_suspend_lo.attr,
5076 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005077 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005078 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005079 NULL,
5080};
NeilBrown411036f2005-11-08 21:39:40 -08005081static struct attribute_group md_redundancy_group = {
5082 .name = NULL,
5083 .attrs = md_redundancy_attrs,
5084};
5085
NeilBrowneae17012005-11-08 21:39:23 -08005086static ssize_t
5087md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5088{
5089 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005090 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005091 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005092
5093 if (!entry->show)
5094 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005095 spin_lock(&all_mddevs_lock);
5096 if (list_empty(&mddev->all_mddevs)) {
5097 spin_unlock(&all_mddevs_lock);
5098 return -EBUSY;
5099 }
5100 mddev_get(mddev);
5101 spin_unlock(&all_mddevs_lock);
5102
NeilBrownb7b17c92014-12-15 12:56:59 +11005103 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005104 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005105 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005106}
5107
5108static ssize_t
5109md_attr_store(struct kobject *kobj, struct attribute *attr,
5110 const char *page, size_t length)
5111{
5112 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005113 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005114 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005115
5116 if (!entry->store)
5117 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005118 if (!capable(CAP_SYS_ADMIN))
5119 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005120 spin_lock(&all_mddevs_lock);
5121 if (list_empty(&mddev->all_mddevs)) {
5122 spin_unlock(&all_mddevs_lock);
5123 return -EBUSY;
5124 }
5125 mddev_get(mddev);
5126 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005127 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005128 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005129 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005130}
5131
5132static void md_free(struct kobject *ko)
5133{
NeilBrownfd01b882011-10-11 16:47:53 +11005134 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005135
5136 if (mddev->sysfs_state)
5137 sysfs_put(mddev->sysfs_state);
5138
NeilBrown6cd18e72015-04-27 14:12:22 +10005139 if (mddev->queue)
5140 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11005141 if (mddev->gendisk) {
5142 del_gendisk(mddev->gendisk);
5143 put_disk(mddev->gendisk);
5144 }
NeilBrown4ad23a972017-03-15 14:05:14 +11005145 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005146
NeilBrowneae17012005-11-08 21:39:23 -08005147 kfree(mddev);
5148}
5149
Emese Revfy52cf25d2010-01-19 02:58:23 +01005150static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005151 .show = md_attr_show,
5152 .store = md_attr_store,
5153};
5154static struct kobj_type md_ktype = {
5155 .release = md_free,
5156 .sysfs_ops = &md_sysfs_ops,
5157 .default_attrs = md_default_attrs,
5158};
5159
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160int mdp_major = 0;
5161
Dan Williams5fd3a172009-03-04 00:57:25 -07005162static void mddev_delayed_delete(struct work_struct *ws)
5163{
NeilBrownfd01b882011-10-11 16:47:53 +11005164 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005165
NeilBrown43a70502009-12-14 12:49:55 +11005166 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005167 kobject_del(&mddev->kobj);
5168 kobject_put(&mddev->kobj);
5169}
5170
NeilBrown4ad23a972017-03-15 14:05:14 +11005171static void no_op(struct percpu_ref *r) {}
5172
NeilBrownefeb53c2009-01-09 08:31:10 +11005173static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174{
NeilBrown039b7222017-04-12 16:26:13 +10005175 /*
5176 * If dev is zero, name is the name of a device to allocate with
5177 * an arbitrary minor number. It will be "md_???"
5178 * If dev is non-zero it must be a device number with a MAJOR of
5179 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5180 * the device is being created by opening a node in /dev.
5181 * If "name" is not NULL, the device is being created by
5182 * writing to /sys/module/md_mod/parameters/new_array.
5183 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005184 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005185 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005187 int partitioned;
5188 int shift;
5189 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005190 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191
5192 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005193 return -ENODEV;
5194
5195 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5196 shift = partitioned ? MdpMinorShift : 0;
5197 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198
Tejun Heoe804ac72010-10-15 15:36:08 +02005199 /* wait for any previous instance of this device to be
5200 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005201 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005202 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005203
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005204 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005205 error = -EEXIST;
5206 if (mddev->gendisk)
5207 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005208
NeilBrown039b7222017-04-12 16:26:13 +10005209 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005210 /* Need to ensure that 'name' is not a duplicate.
5211 */
NeilBrownfd01b882011-10-11 16:47:53 +11005212 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005213 spin_lock(&all_mddevs_lock);
5214
5215 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5216 if (mddev2->gendisk &&
5217 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5218 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005219 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005220 }
5221 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 }
NeilBrown039b7222017-04-12 16:26:13 +10005223 if (name && dev)
5224 /*
5225 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5226 */
5227 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005228
NeilBrown0909dc42009-07-01 12:27:21 +10005229 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005230 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005231 if (!mddev->queue)
5232 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005233 mddev->queue->queuedata = mddev;
5234
NeilBrown409c57f2009-03-31 14:39:39 +11005235 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005236 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005237
NeilBrown4ad23a972017-03-15 14:05:14 +11005238 if (percpu_ref_init(&mddev->writes_pending, no_op, 0, GFP_KERNEL) < 0)
5239 goto abort;
5240 /* We want to start with the refcount at zero */
5241 percpu_ref_put(&mddev->writes_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 disk = alloc_disk(1 << shift);
5243 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005244 blk_cleanup_queue(mddev->queue);
5245 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005246 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005248 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005250 if (name)
5251 strcpy(disk->disk_name, name);
5252 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005254 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 disk->fops = &md_fops;
5257 disk->private_data = mddev;
5258 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005259 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005260 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005261 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005262 * remove it now.
5263 */
5264 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005266 /* As soon as we call add_disk(), another thread could get
5267 * through to md_open, so make sure it doesn't get too far
5268 */
5269 mutex_lock(&mddev->open_mutex);
5270 add_disk(disk);
5271
Tejun Heoed9e1982008-08-25 19:56:05 +09005272 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5273 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005274 if (error) {
5275 /* This isn't possible, but as kobject_init_and_add is marked
5276 * __must_check, we must do something with the result
5277 */
NeilBrown9d487392016-11-02 14:16:49 +11005278 pr_debug("md: cannot register %s/md - name in use\n",
5279 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005280 error = 0;
5281 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005282 if (mddev->kobj.sd &&
5283 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005284 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005285 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005286 abort:
5287 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005288 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005289 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005290 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005291 }
NeilBrownd3374822009-01-09 08:31:10 +11005292 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005293 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005294}
5295
5296static struct kobject *md_probe(dev_t dev, int *part, void *data)
5297{
NeilBrown78b63502017-04-12 16:26:13 +10005298 if (create_on_open)
5299 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 return NULL;
5301}
5302
NeilBrownefeb53c2009-01-09 08:31:10 +11005303static int add_named_array(const char *val, struct kernel_param *kp)
5304{
NeilBrown039b7222017-04-12 16:26:13 +10005305 /*
5306 * val must be "md_*" or "mdNNN".
5307 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005308 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005309 * For "mdNNN" we allocate an array with the minor number NNN
5310 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005311 */
5312 int len = strlen(val);
5313 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005314 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005315
5316 while (len && val[len-1] == '\n')
5317 len--;
5318 if (len >= DISK_NAME_LEN)
5319 return -E2BIG;
5320 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005321 if (strncmp(buf, "md_", 3) == 0)
5322 return md_alloc(0, buf);
5323 if (strncmp(buf, "md", 2) == 0 &&
5324 isdigit(buf[2]) &&
5325 kstrtoul(buf+2, 10, &devnum) == 0 &&
5326 devnum <= MINORMASK)
5327 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5328
5329 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005330}
5331
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332static void md_safemode_timeout(unsigned long data)
5333{
NeilBrownfd01b882011-10-11 16:47:53 +11005334 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335
NeilBrown4ad23a972017-03-15 14:05:14 +11005336 mddev->safemode = 1;
5337 if (mddev->external)
5338 sysfs_notify_dirent_safe(mddev->sysfs_state);
5339
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 md_wakeup_thread(mddev->thread);
5341}
5342
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005343static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344
NeilBrownfd01b882011-10-11 16:47:53 +11005345int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346{
NeilBrown2604b702006-01-06 00:20:36 -08005347 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005348 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005349 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
NeilBrowna757e642005-04-16 15:26:42 -07005351 if (list_empty(&mddev->disks))
5352 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354
5355 if (mddev->pers)
5356 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005357 /* Cannot run until previous stop completes properly */
5358 if (mddev->sysfs_active)
5359 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005360
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 /*
5362 * Analyze all RAID superblock(s)
5363 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005364 if (!mddev->raid_disks) {
5365 if (!mddev->persistent)
5366 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005367 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
NeilBrownd9d166c2006-01-06 00:20:51 -08005370 if (mddev->level != LEVEL_NONE)
5371 request_module("md-level-%d", mddev->level);
5372 else if (mddev->clevel[0])
5373 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
5375 /*
5376 * Drop all container device buffers, from now on
5377 * the only valid external interface is through the md
5378 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005380 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005381 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 continue;
5383 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005384 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005385
5386 /* perform some consistency tests on the device.
5387 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005388 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005389 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005390 if (rdev->meta_bdev) {
5391 /* Nothing to check */;
5392 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005393 if (mddev->dev_sectors &&
5394 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005395 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005396 pr_warn("md: %s: data overlaps metadata\n",
5397 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005398 return -EINVAL;
5399 }
5400 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005401 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005402 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005403 pr_warn("md: %s: metadata overlaps data\n",
5404 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005405 return -EINVAL;
5406 }
5407 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005408 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 }
5410
Ming Lei10273172017-02-14 23:29:00 +08005411 if (mddev->bio_set == NULL) {
Kent Overstreet395c72a2012-09-06 15:34:55 -07005412 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
Ming Lei10273172017-02-14 23:29:00 +08005413 if (!mddev->bio_set)
5414 return -ENOMEM;
5415 }
NeilBrowna167f662010-10-26 18:31:13 +11005416
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005418 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005419 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005421 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005422 pr_warn("md: personality for level %d is not loaded!\n",
5423 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005424 else
NeilBrown9d487392016-11-02 14:16:49 +11005425 pr_warn("md: personality for level %s is not loaded!\n",
5426 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 return -EINVAL;
5428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005430 if (mddev->level != pers->level) {
5431 mddev->level = pers->level;
5432 mddev->new_level = pers->level;
5433 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005434 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435
NeilBrownf6705572006-03-27 01:18:11 -08005436 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005437 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005438 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005439 module_put(pers->owner);
5440 return -EINVAL;
5441 }
5442
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005443 if (pers->sync_request) {
5444 /* Warn if this is a potentially silly
5445 * configuration.
5446 */
5447 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005448 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005449 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005450
NeilBrowndafb20f2012-03-19 12:46:39 +11005451 rdev_for_each(rdev, mddev)
5452 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005453 if (rdev < rdev2 &&
5454 rdev->bdev->bd_contains ==
5455 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005456 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5457 mdname(mddev),
5458 bdevname(rdev->bdev,b),
5459 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005460 warned = 1;
5461 }
5462 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005463
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005464 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005465 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005466 }
5467
NeilBrown657390d2005-08-26 18:34:16 -07005468 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005469 /* may be over-ridden by personality */
5470 mddev->resync_max_sectors = mddev->dev_sectors;
5471
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005472 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
NeilBrown0f9552b52009-12-30 12:08:50 +11005474 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005475 mddev->ro = 2; /* read-only, but switch on first write */
5476
Song Liua85dd7b2017-01-23 17:12:57 -08005477 /*
5478 * NOTE: some pers->run(), for example r5l_recovery_log(), wakes
5479 * up mddev->thread. It is important to initialize critical
5480 * resources for mddev->thread BEFORE calling pers->run().
5481 */
NeilBrown36d091f2014-12-15 12:56:58 +11005482 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005483 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005484 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005485 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005486 WARN_ONCE(!mddev->external_size,
5487 "%s: default size too small, but 'external_size' not in effect?\n",
5488 __func__);
5489 pr_warn("md: invalid array_size %llu > default size %llu\n",
5490 (unsigned long long)mddev->array_sectors / 2,
5491 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005492 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005493 }
NeilBrown36d091f2014-12-15 12:56:58 +11005494 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005495 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005496 struct bitmap *bitmap;
5497
5498 bitmap = bitmap_create(mddev, -1);
5499 if (IS_ERR(bitmap)) {
5500 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005501 pr_warn("%s: failed to create bitmap (%d)\n",
5502 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005503 } else
5504 mddev->bitmap = bitmap;
5505
NeilBrownb15c2e52006-01-06 00:20:16 -08005506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005508 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005509 if (mddev->private)
5510 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005511 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005512 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005513 bitmap_destroy(mddev);
5514 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 }
NeilBrown5c675f82014-12-15 12:56:56 +11005516 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005517 bool nonrot = true;
5518
5519 rdev_for_each(rdev, mddev) {
5520 if (rdev->raid_disk >= 0 &&
5521 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5522 nonrot = false;
5523 break;
5524 }
5525 }
5526 if (mddev->degraded)
5527 nonrot = false;
5528 if (nonrot)
5529 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5530 else
5531 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005532 mddev->queue->backing_dev_info->congested_data = mddev;
5533 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005534 }
NeilBrown36d091f2014-12-15 12:56:58 +11005535 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005536 if (mddev->kobj.sd &&
5537 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005538 pr_warn("md: cannot register extra attributes for %s\n",
5539 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005540 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005541 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005542 mddev->ro = 0;
5543
Robert Becker1e509152009-12-14 12:49:58 +11005544 atomic_set(&mddev->max_corr_read_errors,
5545 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005547 if (mddev_is_clustered(mddev))
5548 mddev->safemode_delay = 0;
5549 else
5550 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005552 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005553 spin_lock(&mddev->lock);
5554 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005555 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005556 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005557 if (rdev->raid_disk >= 0)
5558 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005559 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005560
NeilBrowna4a3d262015-07-17 11:57:30 +10005561 if (mddev->degraded && !mddev->ro)
5562 /* This ensures that recovering status is reported immediately
5563 * via sysfs - until a lack of spares is confirmed.
5564 */
5565 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005567
Shaohua Li29530792016-12-08 15:48:19 -08005568 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07005569 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570
NeilBrownd7603b72006-01-06 00:20:30 -08005571 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005572 sysfs_notify_dirent_safe(mddev->sysfs_state);
5573 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005574 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 return 0;
5576}
NeilBrown390ee602010-06-01 19:37:27 +10005577EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
NeilBrownfd01b882011-10-11 16:47:53 +11005579static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005580{
5581 int err;
5582
5583 err = md_run(mddev);
5584 if (err)
5585 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005586 err = bitmap_load(mddev);
5587 if (err) {
5588 bitmap_destroy(mddev);
5589 goto out;
5590 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005591
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005592 if (mddev_is_clustered(mddev))
5593 md_allow_write(mddev);
5594
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005595 md_wakeup_thread(mddev->thread);
5596 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5597
NeilBrownfe60b012010-03-29 11:10:42 +11005598 set_capacity(mddev->gendisk, mddev->array_sectors);
5599 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005600 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005601 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5602out:
5603 return err;
5604}
5605
NeilBrownfd01b882011-10-11 16:47:53 +11005606static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607{
5608 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
Andre Noll80fab1d2008-07-11 22:02:21 +10005610 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005612 return -ENXIO;
5613 if (!mddev->pers)
5614 return -EINVAL;
5615 if (!mddev->ro)
5616 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005617 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5618 struct md_rdev *rdev;
5619 bool has_journal = false;
5620
5621 rcu_read_lock();
5622 rdev_for_each_rcu(rdev, mddev) {
5623 if (test_bit(Journal, &rdev->flags) &&
5624 !test_bit(Faulty, &rdev->flags)) {
5625 has_journal = true;
5626 break;
5627 }
5628 }
5629 rcu_read_unlock();
5630
5631 /* Don't restart rw with journal missing/faulty */
5632 if (!has_journal)
5633 return -EINVAL;
5634 }
5635
Andre Noll80fab1d2008-07-11 22:02:21 +10005636 mddev->safemode = 0;
5637 mddev->ro = 0;
5638 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11005639 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10005640 /* Kick recovery or resync if necessary */
5641 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5642 md_wakeup_thread(mddev->thread);
5643 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005644 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646}
5647
NeilBrownfd01b882011-10-11 16:47:53 +11005648static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005649{
5650 mddev->array_sectors = 0;
5651 mddev->external_size = 0;
5652 mddev->dev_sectors = 0;
5653 mddev->raid_disks = 0;
5654 mddev->recovery_cp = 0;
5655 mddev->resync_min = 0;
5656 mddev->resync_max = MaxSector;
5657 mddev->reshape_position = MaxSector;
5658 mddev->external = 0;
5659 mddev->persistent = 0;
5660 mddev->level = LEVEL_NONE;
5661 mddev->clevel[0] = 0;
5662 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08005663 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005664 mddev->ro = 0;
5665 mddev->metadata_type[0] = 0;
5666 mddev->chunk_sectors = 0;
5667 mddev->ctime = mddev->utime = 0;
5668 mddev->layout = 0;
5669 mddev->max_disks = 0;
5670 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005671 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005672 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005673 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005674 mddev->new_level = LEVEL_NONE;
5675 mddev->new_layout = 0;
5676 mddev->new_chunk_sectors = 0;
5677 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005678 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005679 mddev->suspend_lo = mddev->suspend_hi = 0;
5680 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5681 mddev->recovery = 0;
5682 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005683 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005684 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005685 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005686 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005687 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005688 mddev->bitmap_info.offset = 0;
5689 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005690 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005691 mddev->bitmap_info.chunksize = 0;
5692 mddev->bitmap_info.daemon_sleep = 0;
5693 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005694 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005695}
5696
NeilBrownfd01b882011-10-11 16:47:53 +11005697static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005698{
NeilBrown6b6204e2013-05-09 09:48:30 +10005699 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005700 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005701 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005702 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005703 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005704 }
5705
5706 del_timer_sync(&mddev->safemode_timer);
5707
Shaohua Li034e33f2016-11-21 10:29:19 -08005708 if (mddev->pers && mddev->pers->quiesce) {
5709 mddev->pers->quiesce(mddev, 1);
5710 mddev->pers->quiesce(mddev, 0);
5711 }
NeilBrowna047e122010-03-29 12:07:53 +11005712 bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005713
NeilBrownb6d428c2013-04-24 11:42:42 +10005714 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005715 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08005716 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11005717 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005718 if (!mddev_is_clustered(mddev))
5719 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005720 md_update_sb(mddev, 1);
5721 }
5722}
NeilBrowndefad612011-01-14 09:14:33 +11005723
NeilBrownfd01b882011-10-11 16:47:53 +11005724void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005725{
NeilBrown29f097c2013-11-14 17:54:51 +11005726 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005727 __md_stop_writes(mddev);
5728 mddev_unlock(mddev);
5729}
NeilBrown390ee602010-06-01 19:37:27 +10005730EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005731
NeilBrown5aa61f42014-12-15 12:56:57 +11005732static void mddev_detach(struct mddev *mddev)
5733{
Guoqing Jiang48df4982017-03-14 09:40:20 +08005734 bitmap_wait_behind_writes(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005735 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005736 mddev->pers->quiesce(mddev, 1);
5737 mddev->pers->quiesce(mddev, 0);
5738 }
5739 md_unregister_thread(&mddev->thread);
5740 if (mddev->queue)
5741 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5742}
5743
NeilBrown5eff3c42012-11-19 10:47:48 +11005744static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005745{
NeilBrown36d091f2014-12-15 12:56:58 +11005746 struct md_personality *pers = mddev->pers;
Guoqing Jiang48df4982017-03-14 09:40:20 +08005747 bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11005748 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005749 /* Ensure ->event_work is done */
5750 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005751 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005752 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005753 spin_unlock(&mddev->lock);
5754 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005755 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005756 if (pers->sync_request && mddev->to_remove == NULL)
5757 mddev->to_remove = &md_redundancy_group;
5758 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005759 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005760}
NeilBrown5eff3c42012-11-19 10:47:48 +11005761
5762void md_stop(struct mddev *mddev)
5763{
5764 /* stop the array and free an attached data structures.
5765 * This is called from dm-raid
5766 */
5767 __md_stop(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005768 if (mddev->bio_set)
5769 bioset_free(mddev->bio_set);
5770}
5771
NeilBrown390ee602010-06-01 19:37:27 +10005772EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005773
NeilBrowna05b7ea2012-07-19 15:59:18 +10005774static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005775{
5776 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005777 int did_freeze = 0;
5778
5779 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5780 did_freeze = 1;
5781 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5782 md_wakeup_thread(mddev->thread);
5783 }
NeilBrownf851b602014-12-11 10:02:10 +11005784 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005785 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005786 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005787 /* Thread might be blocked waiting for metadata update
5788 * which will now never happen */
5789 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005790
Shaohua Li29530792016-12-08 15:48:19 -08005791 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10005792 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005793 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005794 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5795 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005796 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005797 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005798 mddev_lock_nointr(mddev);
5799
NeilBrowna4bd82d2010-03-29 13:23:10 +11005800 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005801 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005802 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005803 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005804 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005805 if (did_freeze) {
5806 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005807 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005808 md_wakeup_thread(mddev->thread);
5809 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005810 err = -EBUSY;
5811 goto out;
5812 }
5813 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005814 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005815
5816 err = -ENXIO;
5817 if (mddev->ro==1)
5818 goto out;
5819 mddev->ro = 1;
5820 set_disk_ro(mddev->gendisk, 1);
5821 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005822 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5823 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005824 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005825 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005826 }
5827out:
5828 mutex_unlock(&mddev->open_mutex);
5829 return err;
5830}
5831
NeilBrown9e653b62006-06-26 00:27:58 -07005832/* mode:
5833 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005834 * 2 - stop but do not disassemble array
5835 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005836static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005837 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005840 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005841 int did_freeze = 0;
5842
5843 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5844 did_freeze = 1;
5845 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5846 md_wakeup_thread(mddev->thread);
5847 }
NeilBrownf851b602014-12-11 10:02:10 +11005848 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005849 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005850 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005851 /* Thread might be blocked waiting for metadata update
5852 * which will now never happen */
5853 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005854
NeilBrown30b8feb2013-11-14 15:16:17 +11005855 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005856 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5857 !test_bit(MD_RECOVERY_RUNNING,
5858 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005859 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860
NeilBrownc8c00a62009-08-10 12:50:52 +10005861 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005862 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005863 mddev->sysfs_active ||
5864 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005865 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005866 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005867 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005868 if (did_freeze) {
5869 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005870 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005871 md_wakeup_thread(mddev->thread);
5872 }
NeilBrown260fa032013-08-27 16:44:13 +10005873 return -EBUSY;
5874 }
NeilBrown6e17b022010-08-07 21:41:19 +10005875 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005876 if (mddev->ro)
5877 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005878
NeilBrowndefad612011-01-14 09:14:33 +11005879 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005880 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01005881 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005882
NeilBrowna4bd82d2010-03-29 13:23:10 +11005883 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005884 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005885
NeilBrowndafb20f2012-03-19 12:46:39 +11005886 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005887 if (rdev->raid_disk >= 0)
5888 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005889
NeilBrowna4bd82d2010-03-29 13:23:10 +11005890 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005891 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005892 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005893 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005894
NeilBrowna4bd82d2010-03-29 13:23:10 +11005895 if (mddev->ro)
5896 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005897 } else
5898 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 /*
5900 * Free resources if final stop
5901 */
NeilBrown9e653b62006-06-26 00:27:58 -07005902 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005903 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904
NeilBrownc3d97142009-12-14 12:49:52 +11005905 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005906 struct file *f = mddev->bitmap_info.file;
5907 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005908 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005909 spin_unlock(&mddev->lock);
5910 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005911 }
NeilBrownc3d97142009-12-14 12:49:52 +11005912 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005913
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 export_array(mddev);
5915
NeilBrown6177b472010-03-29 11:37:13 +11005916 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11005917 if (mddev->hold_active == UNTIL_STOP)
5918 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005919 }
NeilBrownd7603b72006-01-06 00:20:30 -08005920 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005921 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005922 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923}
5924
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005925#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005926static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927{
NeilBrown3cb03002011-10-11 16:45:26 +11005928 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 int err;
5930
NeilBrowna757e642005-04-16 15:26:42 -07005931 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
NeilBrown9d487392016-11-02 14:16:49 +11005934 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935
NeilBrowndafb20f2012-03-19 12:46:39 +11005936 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11005938 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 }
NeilBrown9d487392016-11-02 14:16:49 +11005940 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941
NeilBrownd710e132008-10-13 11:55:12 +11005942 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11005944 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005945 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 }
5947}
5948
5949/*
5950 * lets try to run arrays based on all disks that have arrived
5951 * until now. (those are in pending_raid_disks)
5952 *
5953 * the method: pick the first pending disk, collect all disks with
5954 * the same UUID, remove all from the pending list and put them into
5955 * the 'same_array' list. Then order this list based on superblock
5956 * update time (freshest comes first), kick out 'old' disks and
5957 * compare superblocks. If everything's fine then run it.
5958 *
5959 * If "unit" is allocated, then bump its reference count
5960 */
5961static void autorun_devices(int part)
5962{
NeilBrown3cb03002011-10-11 16:45:26 +11005963 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005964 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 char b[BDEVNAME_SIZE];
5966
NeilBrown9d487392016-11-02 14:16:49 +11005967 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005969 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005971 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005973 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974
NeilBrown9d487392016-11-02 14:16:49 +11005975 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005977 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005979 pr_debug("md: adding %s ...\n",
5980 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 list_move(&rdev->same_set, &candidates);
5982 }
5983 /*
5984 * now we have a set of devices, with all of them having
5985 * mostly sane superblocks. It's time to allocate the
5986 * mddev.
5987 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005988 if (part) {
5989 dev = MKDEV(mdp_major,
5990 rdev0->preferred_minor << MdpMinorShift);
5991 unit = MINOR(dev) >> MdpMinorShift;
5992 } else {
5993 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5994 unit = MINOR(dev);
5995 }
5996 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11005997 pr_warn("md: unit number in %s is bad: %d\n",
5998 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 break;
6000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001
6002 md_probe(dev, NULL, NULL);
6003 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006004 if (!mddev || !mddev->gendisk) {
6005 if (mddev)
6006 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 break;
6008 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006009 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006010 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 else if (mddev->raid_disks || mddev->major_version
6012 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006013 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 mdname(mddev), bdevname(rdev0->bdev,b));
6015 mddev_unlock(mddev);
6016 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006017 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006018 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006019 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 list_del_init(&rdev->same_set);
6021 if (bind_rdev_to_array(rdev, mddev))
6022 export_rdev(rdev);
6023 }
6024 autorun_array(mddev);
6025 mddev_unlock(mddev);
6026 }
6027 /* on success, candidates will be empty, on error
6028 * it won't...
6029 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006030 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006031 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 mddev_put(mddev);
6035 }
NeilBrown9d487392016-11-02 14:16:49 +11006036 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006038#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
NeilBrownf72ffdd2014-09-30 14:23:59 +10006040static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041{
6042 mdu_version_t ver;
6043
6044 ver.major = MD_MAJOR_VERSION;
6045 ver.minor = MD_MINOR_VERSION;
6046 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6047
6048 if (copy_to_user(arg, &ver, sizeof(ver)))
6049 return -EFAULT;
6050
6051 return 0;
6052}
6053
NeilBrownf72ffdd2014-09-30 14:23:59 +10006054static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055{
6056 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006057 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006058 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
NeilBrown1ca69c42012-10-11 13:37:33 +11006060 nr = working = insync = failed = spare = 0;
6061 rcu_read_lock();
6062 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006064 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 failed++;
6066 else {
6067 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006068 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006069 insync++;
Song Liub347af82016-08-11 17:14:45 -07006070 else if (test_bit(Journal, &rdev->flags))
6071 /* TODO: add journal count to md_u.h */
6072 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 else
6074 spare++;
6075 }
6076 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006077 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078
6079 info.major_version = mddev->major_version;
6080 info.minor_version = mddev->minor_version;
6081 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006082 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006084 info.size = mddev->dev_sectors / 2;
6085 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006086 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 info.nr_disks = nr;
6088 info.raid_disks = mddev->raid_disks;
6089 info.md_minor = mddev->md_minor;
6090 info.not_persistent= !mddev->persistent;
6091
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006092 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 info.state = 0;
6094 if (mddev->in_sync)
6095 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006096 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006097 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006098 if (mddev_is_clustered(mddev))
6099 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006100 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 info.working_disks = working;
6102 info.failed_disks = failed;
6103 info.spare_disks = spare;
6104
6105 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006106 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
6108 if (copy_to_user(arg, &info, sizeof(info)))
6109 return -EFAULT;
6110
6111 return 0;
6112}
6113
NeilBrownf72ffdd2014-09-30 14:23:59 +10006114static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006115{
6116 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006117 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006118 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006119
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006120 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006121 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006122 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006123
NeilBrown32a76272005-06-21 17:17:14 -07006124 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006125 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006126 /* bitmap enabled */
6127 if (mddev->bitmap_info.file) {
6128 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6129 sizeof(file->pathname));
6130 if (IS_ERR(ptr))
6131 err = PTR_ERR(ptr);
6132 else
6133 memmove(file->pathname, ptr,
6134 sizeof(file->pathname)-(ptr-file->pathname));
6135 }
NeilBrown4af1a042014-12-15 12:57:00 +11006136 spin_unlock(&mddev->lock);
6137
6138 if (err == 0 &&
6139 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006140 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006141
NeilBrown32a76272005-06-21 17:17:14 -07006142 kfree(file);
6143 return err;
6144}
6145
NeilBrownf72ffdd2014-09-30 14:23:59 +10006146static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147{
6148 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006149 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150
6151 if (copy_from_user(&info, arg, sizeof(info)))
6152 return -EFAULT;
6153
NeilBrown1ca69c42012-10-11 13:37:33 +11006154 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006155 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 if (rdev) {
6157 info.major = MAJOR(rdev->bdev->bd_dev);
6158 info.minor = MINOR(rdev->bdev->bd_dev);
6159 info.raid_disk = rdev->raid_disk;
6160 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006161 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006163 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 info.state |= (1<<MD_DISK_ACTIVE);
6165 info.state |= (1<<MD_DISK_SYNC);
6166 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006167 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006168 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006169 if (test_bit(WriteMostly, &rdev->flags))
6170 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006171 if (test_bit(FailFast, &rdev->flags))
6172 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 } else {
6174 info.major = info.minor = 0;
6175 info.raid_disk = -1;
6176 info.state = (1<<MD_DISK_REMOVED);
6177 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006178 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179
6180 if (copy_to_user(arg, &info, sizeof(info)))
6181 return -EFAULT;
6182
6183 return 0;
6184}
6185
NeilBrownf72ffdd2014-09-30 14:23:59 +10006186static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187{
6188 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006189 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 dev_t dev = MKDEV(info->major,info->minor);
6191
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006192 if (mddev_is_clustered(mddev) &&
6193 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006194 pr_warn("%s: Cannot add to clustered mddev.\n",
6195 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006196 return -EINVAL;
6197 }
6198
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6200 return -EOVERFLOW;
6201
6202 if (!mddev->raid_disks) {
6203 int err;
6204 /* expecting a device which has a superblock */
6205 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6206 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006207 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 PTR_ERR(rdev));
6209 return PTR_ERR(rdev);
6210 }
6211 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006212 struct md_rdev *rdev0
6213 = list_entry(mddev->disks.next,
6214 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006215 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 .load_super(rdev, rdev0, mddev->minor_version);
6217 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006218 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006219 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 bdevname(rdev0->bdev,b2));
6221 export_rdev(rdev);
6222 return -EINVAL;
6223 }
6224 }
6225 err = bind_rdev_to_array(rdev, mddev);
6226 if (err)
6227 export_rdev(rdev);
6228 return err;
6229 }
6230
6231 /*
6232 * add_new_disk can be used once the array is assembled
6233 * to add "hot spares". They must already have a superblock
6234 * written
6235 */
6236 if (mddev->pers) {
6237 int err;
6238 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006239 pr_warn("%s: personality does not support diskops!\n",
6240 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 return -EINVAL;
6242 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006243 if (mddev->persistent)
6244 rdev = md_import_device(dev, mddev->major_version,
6245 mddev->minor_version);
6246 else
6247 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006249 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 PTR_ERR(rdev));
6251 return PTR_ERR(rdev);
6252 }
NeilBrown1a855a02010-12-09 16:36:28 +11006253 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006254 if (!mddev->persistent) {
6255 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006256 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006257 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006258 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006259 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006260 } else
NeilBrown41158c72005-06-21 17:17:25 -07006261 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006262 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006263 } else
6264 super_types[mddev->major_version].
6265 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006266 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006267 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006268 /* This was a hot-add request, but events doesn't
6269 * match, so reject it.
6270 */
6271 export_rdev(rdev);
6272 return -EINVAL;
6273 }
6274
NeilBrownb2d444d2005-11-08 21:39:31 -08006275 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006276 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6277 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006278 else
6279 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006280 if (info->state & (1<<MD_DISK_FAILFAST))
6281 set_bit(FailFast, &rdev->flags);
6282 else
6283 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006284
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006285 if (info->state & (1<<MD_DISK_JOURNAL)) {
6286 struct md_rdev *rdev2;
6287 bool has_journal = false;
6288
6289 /* make sure no existing journal disk */
6290 rdev_for_each(rdev2, mddev) {
6291 if (test_bit(Journal, &rdev2->flags)) {
6292 has_journal = true;
6293 break;
6294 }
6295 }
6296 if (has_journal) {
6297 export_rdev(rdev);
6298 return -EBUSY;
6299 }
Song Liubac624f2015-08-13 14:31:55 -07006300 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006301 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006302 /*
6303 * check whether the device shows up in other nodes
6304 */
6305 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006306 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006307 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006308 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006309 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006310 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006311 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006312 export_rdev(rdev);
6313 return err;
6314 }
6315 }
6316 }
6317
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 rdev->raid_disk = -1;
6319 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006320
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 if (err)
6322 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006323
6324 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006325 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6326 if (!err) {
6327 err = md_cluster_ops->new_disk_ack(mddev,
6328 err == 0);
6329 if (err)
6330 md_kick_rdev_from_array(rdev);
6331 }
6332 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006333 if (err)
6334 md_cluster_ops->add_new_disk_cancel(mddev);
6335 else
6336 err = add_bound_rdev(rdev);
6337 }
6338
6339 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006340 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006341
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 return err;
6343 }
6344
6345 /* otherwise, add_new_disk is only allowed
6346 * for major_version==0 superblocks
6347 */
6348 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006349 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 return -EINVAL;
6351 }
6352
6353 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6354 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006355 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006357 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 PTR_ERR(rdev));
6359 return PTR_ERR(rdev);
6360 }
6361 rdev->desc_nr = info->number;
6362 if (info->raid_disk < mddev->raid_disks)
6363 rdev->raid_disk = info->raid_disk;
6364 else
6365 rdev->raid_disk = -1;
6366
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006368 if (info->state & (1<<MD_DISK_SYNC))
6369 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006371 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6372 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006373 if (info->state & (1<<MD_DISK_FAILFAST))
6374 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006375
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006377 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006378 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6379 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006380 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006381 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382
NeilBrown2bf071b2006-01-06 00:20:55 -08006383 err = bind_rdev_to_array(rdev, mddev);
6384 if (err) {
6385 export_rdev(rdev);
6386 return err;
6387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 }
6389
6390 return 0;
6391}
6392
NeilBrownf72ffdd2014-09-30 14:23:59 +10006393static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394{
6395 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006396 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 rdev = find_rdev(mddev, dev);
6399 if (!rdev)
6400 return -ENXIO;
6401
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006402 if (rdev->raid_disk < 0)
6403 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006404
NeilBrown3ea8929d2013-04-24 11:42:41 +10006405 clear_bit(Blocked, &rdev->flags);
6406 remove_and_add_spares(mddev, rdev);
6407
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 if (rdev->raid_disk >= 0)
6409 goto busy;
6410
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006411kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006412 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006413 md_cluster_ops->remove_disk(mddev, rdev);
6414
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006415 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006416 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006417 if (mddev->thread)
6418 md_wakeup_thread(mddev->thread);
6419 else
6420 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006421 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422
6423 return 0;
6424busy:
NeilBrown9d487392016-11-02 14:16:49 +11006425 pr_debug("md: cannot remove active disk %s from %s ...\n",
6426 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 return -EBUSY;
6428}
6429
NeilBrownf72ffdd2014-09-30 14:23:59 +10006430static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431{
6432 char b[BDEVNAME_SIZE];
6433 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006434 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435
6436 if (!mddev->pers)
6437 return -ENODEV;
6438
6439 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006440 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 mdname(mddev));
6442 return -EINVAL;
6443 }
6444 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006445 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 mdname(mddev));
6447 return -EINVAL;
6448 }
6449
NeilBrownd710e132008-10-13 11:55:12 +11006450 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006452 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 PTR_ERR(rdev));
6454 return -EINVAL;
6455 }
6456
6457 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006458 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006460 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461
NeilBrown8190e752009-06-18 08:48:58 +10006462 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463
NeilBrownb2d444d2005-11-08 21:39:31 -08006464 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006465 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 bdevname(rdev->bdev,b), mdname(mddev));
6467 err = -EINVAL;
6468 goto abort_export;
6469 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006470
NeilBrownb2d444d2005-11-08 21:39:31 -08006471 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006473 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006474 err = bind_rdev_to_array(rdev, mddev);
6475 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006476 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477
6478 /*
6479 * The rest should better be atomic, we can have disk failures
6480 * noticed in interrupt contexts ...
6481 */
6482
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483 rdev->raid_disk = -1;
6484
Shaohua Li29530792016-12-08 15:48:19 -08006485 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006486 if (!mddev->thread)
6487 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488 /*
6489 * Kick recovery, maybe this spare has to be added to the
6490 * array immediately.
6491 */
6492 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6493 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006494 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 return 0;
6496
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497abort_export:
6498 export_rdev(rdev);
6499 return err;
6500}
6501
NeilBrownfd01b882011-10-11 16:47:53 +11006502static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006503{
NeilBrown035328c2014-04-09 12:25:40 +10006504 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006505
NeilBrown36fa3062005-09-09 16:23:45 -07006506 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006507 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006508 return -EBUSY;
6509 if (mddev->recovery || mddev->sync_thread)
6510 return -EBUSY;
6511 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006512 }
6513
NeilBrown36fa3062005-09-09 16:23:45 -07006514 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006515 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006516 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006517
NeilBrown1e594bb2014-12-15 12:57:00 +11006518 if (mddev->bitmap || mddev->bitmap_info.file)
6519 return -EEXIST; /* cannot add when bitmap is present */
6520 f = fget(fd);
6521
6522 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006523 pr_warn("%s: error: failed to get bitmap file\n",
6524 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006525 return -EBADF;
6526 }
6527
NeilBrown1e594bb2014-12-15 12:57:00 +11006528 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006529 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006530 pr_warn("%s: error: bitmap file must be a regular file\n",
6531 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006532 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006533 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006534 pr_warn("%s: error: bitmap file must open for write\n",
6535 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006536 err = -EBADF;
6537 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006538 pr_warn("%s: error: bitmap file is already in use\n",
6539 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006540 err = -EBUSY;
6541 }
6542 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006543 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006544 return err;
6545 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006546 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006547 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006548 } else if (mddev->bitmap == NULL)
6549 return -ENOENT; /* cannot remove what isn't there */
6550 err = 0;
6551 if (mddev->pers) {
6552 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006553 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006554 struct bitmap *bitmap;
6555
6556 bitmap = bitmap_create(mddev, -1);
6557 if (!IS_ERR(bitmap)) {
6558 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006559 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006560 } else
6561 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006562 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006563 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006564 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006565 fd = -1; /* make sure to put the file */
6566 }
NeilBrown36fa3062005-09-09 16:23:45 -07006567 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006568 }
6569 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006570 struct file *f = mddev->bitmap_info.file;
6571 if (f) {
6572 spin_lock(&mddev->lock);
6573 mddev->bitmap_info.file = NULL;
6574 spin_unlock(&mddev->lock);
6575 fput(f);
6576 }
NeilBrown36fa3062005-09-09 16:23:45 -07006577 }
6578
NeilBrown32a76272005-06-21 17:17:14 -07006579 return err;
6580}
6581
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582/*
6583 * set_array_info is used two different ways
6584 * The original usage is when creating a new array.
6585 * In this usage, raid_disks is > 0 and it together with
6586 * level, size, not_persistent,layout,chunksize determine the
6587 * shape of the array.
6588 * This will always create an array with a type-0.90.0 superblock.
6589 * The newer usage is when assembling an array.
6590 * In this case raid_disks will be 0, and the major_version field is
6591 * use to determine which style super-blocks are to be found on the devices.
6592 * The minor and patch _version numbers are also kept incase the
6593 * super_block handler wishes to interpret them.
6594 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006595static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596{
6597
6598 if (info->raid_disks == 0) {
6599 /* just setting version number for superblock loading */
6600 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006601 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602 super_types[info->major_version].name == NULL) {
6603 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11006604 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 info->major_version);
6606 return -EINVAL;
6607 }
6608 mddev->major_version = info->major_version;
6609 mddev->minor_version = info->minor_version;
6610 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006611 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006612 /* ensure mddev_put doesn't delete this now that there
6613 * is some minimal configuration.
6614 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006615 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 return 0;
6617 }
6618 mddev->major_version = MD_MAJOR_VERSION;
6619 mddev->minor_version = MD_MINOR_VERSION;
6620 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006621 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622
6623 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006624 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006625 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 mddev->raid_disks = info->raid_disks;
6627 /* don't set md_minor, it is determined by which /dev/md* was
6628 * openned
6629 */
6630 if (info->state & (1<<MD_SB_CLEAN))
6631 mddev->recovery_cp = MaxSector;
6632 else
6633 mddev->recovery_cp = 0;
6634 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006635 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636
6637 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006638 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639
Shaohua Li29530792016-12-08 15:48:19 -08006640 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11006641 mddev->max_disks = MD_SB_DISKS;
6642 mddev->flags = 0;
6643 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006644 }
6645 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646
NeilBrownc3d97142009-12-14 12:49:52 +11006647 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006648 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006649 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006650
NeilBrownf6705572006-03-27 01:18:11 -08006651 mddev->reshape_position = MaxSector;
6652
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 /*
6654 * Generate a 128 bit UUID
6655 */
6656 get_random_bytes(mddev->uuid, 16);
6657
NeilBrownf6705572006-03-27 01:18:11 -08006658 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006659 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006660 mddev->new_layout = mddev->layout;
6661 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006662 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006663
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 return 0;
6665}
6666
NeilBrownfd01b882011-10-11 16:47:53 +11006667void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006668{
Dan Williamsb522adc2009-03-31 15:00:31 +11006669 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6670
6671 if (mddev->external_size)
6672 return;
6673
Dan Williams1f403622009-03-31 14:59:03 +11006674 mddev->array_sectors = array_sectors;
6675}
6676EXPORT_SYMBOL(md_set_array_sectors);
6677
NeilBrownfd01b882011-10-11 16:47:53 +11006678static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006679{
NeilBrown3cb03002011-10-11 16:45:26 +11006680 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006681 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006682 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08006683 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006684
NeilBrowna35b0d62006-01-06 00:20:49 -08006685 if (mddev->pers->resize == NULL)
6686 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006687 /* The "num_sectors" is the number of sectors of each device that
6688 * is used. This can only make sense for arrays with redundancy.
6689 * linear and raid0 always use whatever space is available. We can only
6690 * consider changing this number if no resync or reconstruction is
6691 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006692 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006693 * of each device. If num_sectors is zero, we find the largest size
6694 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006695 */
NeilBrownf851b602014-12-11 10:02:10 +11006696 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6697 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006698 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006699 if (mddev->ro)
6700 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006701
NeilBrowndafb20f2012-03-19 12:46:39 +11006702 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006703 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006704
Andre Nolld71f9f82008-07-11 22:02:22 +10006705 if (fit && (num_sectors == 0 || num_sectors > avail))
6706 num_sectors = avail;
6707 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006708 return -ENOSPC;
6709 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006710 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08006711 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08006712 if (mddev_is_clustered(mddev))
6713 md_cluster_ops->update_size(mddev, old_dev_sectors);
6714 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08006715 set_capacity(mddev->gendisk, mddev->array_sectors);
6716 revalidate_disk(mddev->gendisk);
6717 }
6718 }
NeilBrowna35b0d62006-01-06 00:20:49 -08006719 return rv;
6720}
6721
NeilBrownfd01b882011-10-11 16:47:53 +11006722static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006723{
6724 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006725 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006726 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006727 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006728 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006729 if (mddev->ro)
6730 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006731 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006732 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006733 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006734 if (mddev->sync_thread ||
6735 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6736 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006737 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006738
6739 rdev_for_each(rdev, mddev) {
6740 if (mddev->raid_disks < raid_disks &&
6741 rdev->data_offset < rdev->new_data_offset)
6742 return -EINVAL;
6743 if (mddev->raid_disks > raid_disks &&
6744 rdev->data_offset > rdev->new_data_offset)
6745 return -EINVAL;
6746 }
6747
NeilBrown63c70c42006-03-27 01:18:13 -08006748 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006749 if (mddev->delta_disks < 0)
6750 mddev->reshape_backwards = 1;
6751 else if (mddev->delta_disks > 0)
6752 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006753
6754 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006755 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006756 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006757 mddev->reshape_backwards = 0;
6758 }
NeilBrownda943b992006-01-06 00:20:54 -08006759 return rv;
6760}
6761
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762/*
6763 * update_array_info is used to change the configuration of an
6764 * on-line array.
6765 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6766 * fields in the info are checked against the array.
6767 * Any differences that cannot be handled will cause an error.
6768 * Normally, only one change can be managed at a time.
6769 */
NeilBrownfd01b882011-10-11 16:47:53 +11006770static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771{
6772 int rv = 0;
6773 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006774 int state = 0;
6775
6776 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006777 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006778 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779
6780 if (mddev->major_version != info->major_version ||
6781 mddev->minor_version != info->minor_version ||
6782/* mddev->patch_version != info->patch_version || */
6783 mddev->ctime != info->ctime ||
6784 mddev->level != info->level ||
6785/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006786 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006787 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006788 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6789 ((state^info->state) & 0xfffffe00)
6790 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 return -EINVAL;
6792 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006793 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6794 cnt++;
6795 if (mddev->raid_disks != info->raid_disks)
6796 cnt++;
6797 if (mddev->layout != info->layout)
6798 cnt++;
6799 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6800 cnt++;
6801 if (cnt == 0)
6802 return 0;
6803 if (cnt > 1)
6804 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805
6806 if (mddev->layout != info->layout) {
6807 /* Change layout
6808 * we don't need to do anything at the md level, the
6809 * personality will take care of it all.
6810 */
NeilBrown50ac1682009-06-18 08:47:55 +10006811 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006813 else {
6814 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006815 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006816 if (rv)
6817 mddev->new_layout = mddev->layout;
6818 return rv;
6819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006821 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006822 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823
NeilBrownda943b992006-01-06 00:20:54 -08006824 if (mddev->raid_disks != info->raid_disks)
6825 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826
NeilBrown36fa3062005-09-09 16:23:45 -07006827 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006828 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6829 rv = -EINVAL;
6830 goto err;
6831 }
6832 if (mddev->recovery || mddev->sync_thread) {
6833 rv = -EBUSY;
6834 goto err;
6835 }
NeilBrown36fa3062005-09-09 16:23:45 -07006836 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006837 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006838 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006839 if (mddev->bitmap) {
6840 rv = -EEXIST;
6841 goto err;
6842 }
6843 if (mddev->bitmap_info.default_offset == 0) {
6844 rv = -EINVAL;
6845 goto err;
6846 }
NeilBrownc3d97142009-12-14 12:49:52 +11006847 mddev->bitmap_info.offset =
6848 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006849 mddev->bitmap_info.space =
6850 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006851 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006852 bitmap = bitmap_create(mddev, -1);
6853 if (!IS_ERR(bitmap)) {
6854 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006855 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006856 } else
6857 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006858 if (rv)
6859 bitmap_destroy(mddev);
6860 mddev->pers->quiesce(mddev, 0);
6861 } else {
6862 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006863 if (!mddev->bitmap) {
6864 rv = -ENOENT;
6865 goto err;
6866 }
6867 if (mddev->bitmap->storage.file) {
6868 rv = -EINVAL;
6869 goto err;
6870 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006871 if (mddev->bitmap_info.nodes) {
6872 /* hold PW on all the bitmap lock */
6873 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006874 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 +11006875 rv = -EPERM;
6876 md_cluster_ops->unlock_all_bitmaps(mddev);
6877 goto err;
6878 }
6879
6880 mddev->bitmap_info.nodes = 0;
6881 md_cluster_ops->leave(mddev);
6882 }
NeilBrown36fa3062005-09-09 16:23:45 -07006883 mddev->pers->quiesce(mddev, 1);
6884 bitmap_destroy(mddev);
6885 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006886 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006887 }
6888 }
NeilBrown850b2b422006-10-03 01:15:46 -07006889 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006890 return rv;
6891err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 return rv;
6893}
6894
NeilBrownfd01b882011-10-11 16:47:53 +11006895static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896{
NeilBrown3cb03002011-10-11 16:45:26 +11006897 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006898 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899
6900 if (mddev->pers == NULL)
6901 return -ENODEV;
6902
NeilBrown1ca69c42012-10-11 13:37:33 +11006903 rcu_read_lock();
6904 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006906 err = -ENODEV;
6907 else {
6908 md_error(mddev, rdev);
6909 if (!test_bit(Faulty, &rdev->flags))
6910 err = -EBUSY;
6911 }
6912 rcu_read_unlock();
6913 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914}
6915
Andre Noll2f9618c2008-04-25 18:57:58 +02006916/*
6917 * We have a problem here : there is no easy way to give a CHS
6918 * virtual geometry. We currently pretend that we have a 2 heads
6919 * 4 sectors (with a BIG number of cylinders...). This drives
6920 * dosfs just mad... ;-)
6921 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006922static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6923{
NeilBrownfd01b882011-10-11 16:47:53 +11006924 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006925
6926 geo->heads = 2;
6927 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006928 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006929 return 0;
6930}
6931
Nicolas Schichancb335f82014-01-15 16:58:52 +01006932static inline bool md_ioctl_valid(unsigned int cmd)
6933{
6934 switch (cmd) {
6935 case ADD_NEW_DISK:
6936 case BLKROSET:
6937 case GET_ARRAY_INFO:
6938 case GET_BITMAP_FILE:
6939 case GET_DISK_INFO:
6940 case HOT_ADD_DISK:
6941 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006942 case RAID_AUTORUN:
6943 case RAID_VERSION:
6944 case RESTART_ARRAY_RW:
6945 case RUN_ARRAY:
6946 case SET_ARRAY_INFO:
6947 case SET_BITMAP_FILE:
6948 case SET_DISK_FAULTY:
6949 case STOP_ARRAY:
6950 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006951 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006952 return true;
6953 default:
6954 return false;
6955 }
6956}
6957
Al Viroa39907f2008-03-02 10:31:15 -05006958static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 unsigned int cmd, unsigned long arg)
6960{
6961 int err = 0;
6962 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006963 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006964 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08006965 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966
Nicolas Schichancb335f82014-01-15 16:58:52 +01006967 if (!md_ioctl_valid(cmd))
6968 return -ENOTTY;
6969
NeilBrown506c9e42011-12-23 10:17:26 +11006970 switch (cmd) {
6971 case RAID_VERSION:
6972 case GET_ARRAY_INFO:
6973 case GET_DISK_INFO:
6974 break;
6975 default:
6976 if (!capable(CAP_SYS_ADMIN))
6977 return -EACCES;
6978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979
6980 /*
6981 * Commands dealing with the RAID driver but not any
6982 * particular array:
6983 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006984 switch (cmd) {
6985 case RAID_VERSION:
6986 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006987 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006990 case RAID_AUTORUN:
6991 err = 0;
6992 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006993 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006995 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996 }
6997
6998 /*
6999 * Commands creating/starting a new array:
7000 */
7001
Al Viroa39907f2008-03-02 10:31:15 -05007002 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003
7004 if (!mddev) {
7005 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007006 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 }
7008
NeilBrown1ca69c42012-10-11 13:37:33 +11007009 /* Some actions do not requires the mutex */
7010 switch (cmd) {
7011 case GET_ARRAY_INFO:
7012 if (!mddev->raid_disks && !mddev->external)
7013 err = -ENODEV;
7014 else
7015 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007016 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007017
7018 case GET_DISK_INFO:
7019 if (!mddev->raid_disks && !mddev->external)
7020 err = -ENODEV;
7021 else
7022 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007023 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007024
7025 case SET_DISK_FAULTY:
7026 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007027 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007028
7029 case GET_BITMAP_FILE:
7030 err = get_bitmap_file(mddev, argp);
7031 goto out;
7032
NeilBrown1ca69c42012-10-11 13:37:33 +11007033 }
7034
NeilBrowna7a3f082012-12-11 13:35:54 +11007035 if (cmd == ADD_NEW_DISK)
7036 /* need to ensure md_delayed_delete() has completed */
7037 flush_workqueue(md_misc_wq);
7038
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007039 if (cmd == HOT_REMOVE_DISK)
7040 /* need to ensure recovery thread has run */
7041 wait_event_interruptible_timeout(mddev->sb_wait,
7042 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007043 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007044 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007045 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7046 /* Need to flush page cache, and ensure no-one else opens
7047 * and writes
7048 */
7049 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007050 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007051 mutex_unlock(&mddev->open_mutex);
7052 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007053 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007054 }
NeilBrown065e5192017-04-06 11:16:33 +08007055 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007056 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007057 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007058 mutex_unlock(&mddev->open_mutex);
7059 sync_blockdev(bdev);
7060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061 err = mddev_lock(mddev);
7062 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007063 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7064 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007065 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066 }
7067
NeilBrownc02c0ae2012-12-11 13:39:21 +11007068 if (cmd == SET_ARRAY_INFO) {
7069 mdu_array_info_t info;
7070 if (!arg)
7071 memset(&info, 0, sizeof(info));
7072 else if (copy_from_user(&info, argp, sizeof(info))) {
7073 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007074 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007075 }
7076 if (mddev->pers) {
7077 err = update_array_info(mddev, &info);
7078 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007079 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007080 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007082 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007083 }
7084 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007085 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007086 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007087 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007088 }
7089 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007090 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007091 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007092 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007093 }
7094 err = set_array_info(mddev, &info);
7095 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007096 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007097 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007098 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007099 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100 }
7101
7102 /*
7103 * Commands querying/configuring an existing array:
7104 */
NeilBrown32a76272005-06-21 17:17:14 -07007105 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007106 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007107 if ((!mddev->raid_disks && !mddev->external)
7108 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7109 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7110 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007112 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113 }
7114
7115 /*
7116 * Commands even a read-only array can execute:
7117 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007118 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007119 case RESTART_ARRAY_RW:
7120 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007121 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007122
7123 case STOP_ARRAY:
7124 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007125 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007126
7127 case STOP_ARRAY_RO:
7128 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007129 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007130
NeilBrown3ea8929d2013-04-24 11:42:41 +10007131 case HOT_REMOVE_DISK:
7132 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007133 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007134
NeilBrown7ceb17e2013-04-24 11:42:42 +10007135 case ADD_NEW_DISK:
7136 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007137 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007138 * So require mddev->pers and MD_DISK_SYNC.
7139 */
7140 if (mddev->pers) {
7141 mdu_disk_info_t info;
7142 if (copy_from_user(&info, argp, sizeof(info)))
7143 err = -EFAULT;
7144 else if (!(info.state & (1<<MD_DISK_SYNC)))
7145 /* Need to clear read-only for this */
7146 break;
7147 else
7148 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007149 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007150 }
7151 break;
7152
NeilBrownc02c0ae2012-12-11 13:39:21 +11007153 case BLKROSET:
7154 if (get_user(ro, (int __user *)(arg))) {
7155 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007156 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007157 }
7158 err = -EINVAL;
7159
7160 /* if the bdev is going readonly the value of mddev->ro
7161 * does not matter, no writes are coming
7162 */
7163 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007164 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007165
NeilBrownc02c0ae2012-12-11 13:39:21 +11007166 /* are we are already prepared for writes? */
7167 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007168 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007169
7170 /* transitioning to readauto need only happen for
7171 * arrays that call md_write_start
7172 */
7173 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007175 if (err == 0) {
7176 mddev->ro = 2;
7177 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007178 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007179 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007180 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 }
7182
7183 /*
7184 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007185 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186 */
NeilBrown326eb172014-09-30 15:36:28 +10007187 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007188 if (mddev->ro == 2) {
7189 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007190 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007191 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007192 /* mddev_unlock will wake thread */
7193 /* If a device failed while we were read-only, we
7194 * need to make sure the metadata is updated now.
7195 */
Shaohua Li29530792016-12-08 15:48:19 -08007196 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007197 mddev_unlock(mddev);
7198 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007199 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7200 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007201 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007202 }
NeilBrownf91de922005-11-08 21:39:36 -08007203 } else {
7204 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007205 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207 }
7208
NeilBrownc02c0ae2012-12-11 13:39:21 +11007209 switch (cmd) {
7210 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007212 mdu_disk_info_t info;
7213 if (copy_from_user(&info, argp, sizeof(info)))
7214 err = -EFAULT;
7215 else
7216 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007217 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007220 case CLUSTERED_DISK_NACK:
7221 if (mddev_is_clustered(mddev))
7222 md_cluster_ops->new_disk_ack(mddev, false);
7223 else
7224 err = -EINVAL;
7225 goto unlock;
7226
NeilBrownc02c0ae2012-12-11 13:39:21 +11007227 case HOT_ADD_DISK:
7228 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007229 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230
NeilBrownc02c0ae2012-12-11 13:39:21 +11007231 case RUN_ARRAY:
7232 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007233 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234
NeilBrownc02c0ae2012-12-11 13:39:21 +11007235 case SET_BITMAP_FILE:
7236 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007237 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007238
NeilBrownc02c0ae2012-12-11 13:39:21 +11007239 default:
7240 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007241 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242 }
7243
NeilBrown3adc28d2014-09-30 15:46:41 +10007244unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007245 if (mddev->hold_active == UNTIL_IOCTL &&
7246 err != -EINVAL)
7247 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007249out:
NeilBrown065e5192017-04-06 11:16:33 +08007250 if(did_set_md_closing)
7251 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 return err;
7253}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007254#ifdef CONFIG_COMPAT
7255static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7256 unsigned int cmd, unsigned long arg)
7257{
7258 switch (cmd) {
7259 case HOT_REMOVE_DISK:
7260 case HOT_ADD_DISK:
7261 case SET_DISK_FAULTY:
7262 case SET_BITMAP_FILE:
7263 /* These take in integer arg, do not convert */
7264 break;
7265 default:
7266 arg = (unsigned long)compat_ptr(arg);
7267 break;
7268 }
7269
7270 return md_ioctl(bdev, mode, cmd, arg);
7271}
7272#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273
Al Viroa39907f2008-03-02 10:31:15 -05007274static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275{
7276 /*
7277 * Succeed if we can lock the mddev, which confirms that
7278 * it isn't being stopped right now.
7279 */
NeilBrownfd01b882011-10-11 16:47:53 +11007280 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281 int err;
7282
Yuanhan Liu0c098222012-05-22 13:55:32 +10007283 if (!mddev)
7284 return -ENODEV;
7285
NeilBrownd3374822009-01-09 08:31:10 +11007286 if (mddev->gendisk != bdev->bd_disk) {
7287 /* we are racing with mddev_put which is discarding this
7288 * bd_disk.
7289 */
7290 mddev_put(mddev);
7291 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007292 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007293 /* Then retry the open from the top */
7294 return -ERESTARTSYS;
7295 }
7296 BUG_ON(mddev != bdev->bd_disk->private_data);
7297
NeilBrownc8c00a62009-08-10 12:50:52 +10007298 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299 goto out;
7300
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007301 if (test_bit(MD_CLOSING, &mddev->flags)) {
7302 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007303 err = -ENODEV;
7304 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007305 }
7306
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007308 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007309 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007311 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007313 if (err)
7314 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315 return err;
7316}
7317
Al Virodb2a1442013-05-05 21:52:57 -04007318static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007320 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007321
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007322 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007323 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007326
7327static int md_media_changed(struct gendisk *disk)
7328{
NeilBrownfd01b882011-10-11 16:47:53 +11007329 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007330
7331 return mddev->changed;
7332}
7333
7334static int md_revalidate(struct gendisk *disk)
7335{
NeilBrownfd01b882011-10-11 16:47:53 +11007336 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007337
7338 mddev->changed = 0;
7339 return 0;
7340}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007341static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342{
7343 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007344 .open = md_open,
7345 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007346 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007347#ifdef CONFIG_COMPAT
7348 .compat_ioctl = md_compat_ioctl,
7349#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007350 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007351 .media_changed = md_media_changed,
7352 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353};
7354
NeilBrownf72ffdd2014-09-30 14:23:59 +10007355static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356{
NeilBrown2b8bf342011-10-11 16:48:23 +11007357 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359 /*
7360 * md_thread is a 'system-thread', it's priority should be very
7361 * high. We avoid resource deadlocks individually in each
7362 * raid personality. (RAID5 does preallocation) We also use RR and
7363 * the very same RT priority as kswapd, thus we will never get
7364 * into a priority inversion deadlock.
7365 *
7366 * we definitely have to have equal or higher priority than
7367 * bdflush, otherwise bdflush will deadlock if there are too
7368 * many dirty RAID5 blocks.
7369 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370
NeilBrown6985c432005-10-19 21:23:47 -07007371 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007372 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373
NeilBrown93588e22005-11-15 00:09:12 -08007374 /* We need to wait INTERRUPTIBLE so that
7375 * we don't add to the load-average.
7376 * That means we need to be sure no signals are
7377 * pending
7378 */
7379 if (signal_pending(current))
7380 flush_signals(current);
7381
7382 wait_event_interruptible_timeout
7383 (thread->wqueue,
7384 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007385 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007386 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387
NeilBrown6c987912011-01-14 09:13:53 +11007388 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007389 if (kthread_should_park())
7390 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007391 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007392 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007394
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395 return 0;
7396}
7397
NeilBrown2b8bf342011-10-11 16:48:23 +11007398void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399{
7400 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007401 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
NeilBrown4ad23a972017-03-15 14:05:14 +11007402 if (!test_and_set_bit(THREAD_WAKEUP, &thread->flags))
7403 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 }
7405}
NeilBrown6c144d32014-09-30 16:15:38 +10007406EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407
Shaohua Li4ed87312012-10-11 13:34:00 +11007408struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7409 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410{
NeilBrown2b8bf342011-10-11 16:48:23 +11007411 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412
NeilBrown2b8bf342011-10-11 16:48:23 +11007413 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414 if (!thread)
7415 return NULL;
7416
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417 init_waitqueue_head(&thread->wqueue);
7418
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419 thread->run = run;
7420 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007421 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007422 thread->tsk = kthread_run(md_thread, thread,
7423 "%s_%s",
7424 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007425 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007426 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427 kfree(thread);
7428 return NULL;
7429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430 return thread;
7431}
NeilBrown6c144d32014-09-30 16:15:38 +10007432EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433
NeilBrown2b8bf342011-10-11 16:48:23 +11007434void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435{
NeilBrown2b8bf342011-10-11 16:48:23 +11007436 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007437 if (!thread)
7438 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007439 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007440 /* Locking ensures that mddev_unlock does not wake_up a
7441 * non-existent thread
7442 */
7443 spin_lock(&pers_lock);
7444 *threadp = NULL;
7445 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007446
7447 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 kfree(thread);
7449}
NeilBrown6c144d32014-09-30 16:15:38 +10007450EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451
NeilBrownfd01b882011-10-11 16:47:53 +11007452void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453{
NeilBrownb2d444d2005-11-08 21:39:31 -08007454 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007456
NeilBrownde393cd2011-07-28 11:31:48 +10007457 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458 return;
7459 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007460 if (mddev->degraded)
7461 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007462 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7464 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7465 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007466 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007467 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007468 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469}
NeilBrown6c144d32014-09-30 16:15:38 +10007470EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471
7472/* seq_file implementation /proc/mdstat */
7473
7474static void status_unused(struct seq_file *seq)
7475{
7476 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007477 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478
7479 seq_printf(seq, "unused devices: ");
7480
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007481 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 char b[BDEVNAME_SIZE];
7483 i++;
7484 seq_printf(seq, "%s ",
7485 bdevname(rdev->bdev,b));
7486 }
7487 if (!i)
7488 seq_printf(seq, "<none>");
7489
7490 seq_printf(seq, "\n");
7491}
7492
NeilBrownf7851be2015-07-02 17:12:58 +10007493static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494{
NeilBrowndd71cf62009-05-07 12:49:35 +10007495 sector_t max_sectors, resync, res;
7496 unsigned long dt, db;
7497 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007498 int scale;
7499 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500
NeilBrownc804cde2012-05-21 09:28:33 +10007501 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7502 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007503 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007505 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506
NeilBrownf7851be2015-07-02 17:12:58 +10007507 resync = mddev->curr_resync;
7508 if (resync <= 3) {
7509 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7510 /* Still cleaning up */
7511 resync = max_sectors;
7512 } else
7513 resync -= atomic_read(&mddev->recovery_active);
7514
7515 if (resync == 0) {
7516 if (mddev->recovery_cp < MaxSector) {
7517 seq_printf(seq, "\tresync=PENDING");
7518 return 1;
7519 }
7520 return 0;
7521 }
7522 if (resync < 3) {
7523 seq_printf(seq, "\tresync=DELAYED");
7524 return 1;
7525 }
7526
NeilBrown403df472014-09-30 15:52:29 +10007527 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007528 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007529 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007530 * u32, as those are the requirements for sector_div.
7531 * Thus 'scale' must be at least 10
7532 */
7533 scale = 10;
7534 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007535 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007536 scale++;
7537 }
7538 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007539 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007540
7541 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542 {
NeilBrown4588b422006-03-27 01:18:04 -08007543 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544 seq_printf(seq, "[");
7545 for (i = 0; i < x; i++)
7546 seq_printf(seq, "=");
7547 seq_printf(seq, ">");
7548 for (i = 0; i < y; i++)
7549 seq_printf(seq, ".");
7550 seq_printf(seq, "] ");
7551 }
NeilBrown4588b422006-03-27 01:18:04 -08007552 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007553 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7554 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007555 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7556 "check" :
7557 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7558 "resync" : "recovery"))),
7559 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007560 (unsigned long long) resync/2,
7561 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562
7563 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564 * dt: time from mark until now
7565 * db: blocks written from mark until now
7566 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007567 *
7568 * rt is a sector_t, so could be 32bit or 64bit.
7569 * So we divide before multiply in case it is 32bit and close
7570 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007571 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007572 * near the end of resync when the number of remaining sectors
7573 * is close to 'db'.
7574 * We then divide rt by 32 after multiplying by db to compensate.
7575 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576 */
7577 dt = ((jiffies - mddev->resync_mark) / HZ);
7578 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007579 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7580 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581
NeilBrowndd71cf62009-05-07 12:49:35 +10007582 rt = max_sectors - resync; /* number of remaining sectors */
7583 sector_div(rt, db/32+1);
7584 rt *= dt;
7585 rt >>= 5;
7586
7587 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7588 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589
NeilBrownff4e8d92006-07-10 04:44:16 -07007590 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007591 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592}
7593
7594static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7595{
7596 struct list_head *tmp;
7597 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007598 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599
7600 if (l >= 0x10000)
7601 return NULL;
7602 if (!l--)
7603 /* header */
7604 return (void*)1;
7605
7606 spin_lock(&all_mddevs_lock);
7607 list_for_each(tmp,&all_mddevs)
7608 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007609 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 mddev_get(mddev);
7611 spin_unlock(&all_mddevs_lock);
7612 return mddev;
7613 }
7614 spin_unlock(&all_mddevs_lock);
7615 if (!l--)
7616 return (void*)2;/* tail */
7617 return NULL;
7618}
7619
7620static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7621{
7622 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007623 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007624
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625 ++*pos;
7626 if (v == (void*)2)
7627 return NULL;
7628
7629 spin_lock(&all_mddevs_lock);
7630 if (v == (void*)1)
7631 tmp = all_mddevs.next;
7632 else
7633 tmp = mddev->all_mddevs.next;
7634 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007635 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636 else {
7637 next_mddev = (void*)2;
7638 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640 spin_unlock(&all_mddevs_lock);
7641
7642 if (v != (void*)1)
7643 mddev_put(mddev);
7644 return next_mddev;
7645
7646}
7647
7648static void md_seq_stop(struct seq_file *seq, void *v)
7649{
NeilBrownfd01b882011-10-11 16:47:53 +11007650 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651
7652 if (mddev && v != (void*)1 && v != (void*)2)
7653 mddev_put(mddev);
7654}
7655
7656static int md_seq_show(struct seq_file *seq, void *v)
7657{
NeilBrownfd01b882011-10-11 16:47:53 +11007658 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007659 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007660 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661
7662 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007663 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664 seq_printf(seq, "Personalities : ");
7665 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007666 list_for_each_entry(pers, &pers_list, list)
7667 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668
7669 spin_unlock(&pers_lock);
7670 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007671 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672 return 0;
7673 }
7674 if (v == (void*)2) {
7675 status_unused(seq);
7676 return 0;
7677 }
7678
NeilBrown36d091f2014-12-15 12:56:58 +11007679 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7681 seq_printf(seq, "%s : %sactive", mdname(mddev),
7682 mddev->pers ? "" : "in");
7683 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007684 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007686 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007687 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688 seq_printf(seq, " %s", mddev->pers->name);
7689 }
7690
Andre Nolldd8ac332009-03-31 14:33:13 +11007691 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007692 rcu_read_lock();
7693 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694 char b[BDEVNAME_SIZE];
7695 seq_printf(seq, " %s[%d]",
7696 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007697 if (test_bit(WriteMostly, &rdev->flags))
7698 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007699 if (test_bit(Journal, &rdev->flags))
7700 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007701 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007702 seq_printf(seq, "(F)");
7703 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007704 }
7705 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007706 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007707 if (test_bit(Replacement, &rdev->flags))
7708 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007709 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007711 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712
7713 if (!list_empty(&mddev->disks)) {
7714 if (mddev->pers)
7715 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007716 (unsigned long long)
7717 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007718 else
7719 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007720 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007721 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007722 if (mddev->persistent) {
7723 if (mddev->major_version != 0 ||
7724 mddev->minor_version != 90) {
7725 seq_printf(seq," super %d.%d",
7726 mddev->major_version,
7727 mddev->minor_version);
7728 }
NeilBrowne6910632008-02-06 01:39:51 -08007729 } else if (mddev->external)
7730 seq_printf(seq, " super external:%s",
7731 mddev->metadata_type);
7732 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007733 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007734
7735 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007736 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007737 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007738 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007739 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007740 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007741 }
NeilBrown32a76272005-06-21 17:17:14 -07007742 } else
7743 seq_printf(seq, "\n ");
7744
NeilBrown57148962012-03-19 12:46:40 +11007745 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007746
7747 seq_printf(seq, "\n");
7748 }
NeilBrown36d091f2014-12-15 12:56:58 +11007749 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007750
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751 return 0;
7752}
7753
Jan Engelhardt110518b2009-05-07 12:49:37 +10007754static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755 .start = md_seq_start,
7756 .next = md_seq_next,
7757 .stop = md_seq_stop,
7758 .show = md_seq_show,
7759};
7760
7761static int md_seq_open(struct inode *inode, struct file *file)
7762{
Kay Sieversf1514632011-07-12 20:48:39 +02007763 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764 int error;
7765
7766 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007767 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007768 return error;
7769
7770 seq = file->private_data;
7771 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772 return error;
7773}
7774
NeilBrowne2f23b62014-04-09 14:33:51 +10007775static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007776static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7777{
Kay Sieversf1514632011-07-12 20:48:39 +02007778 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007779 int mask;
7780
NeilBrowne2f23b62014-04-09 14:33:51 +10007781 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007782 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007783 poll_wait(filp, &md_event_waiters, wait);
7784
7785 /* always allow read */
7786 mask = POLLIN | POLLRDNORM;
7787
Kay Sieversf1514632011-07-12 20:48:39 +02007788 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007789 mask |= POLLERR | POLLPRI;
7790 return mask;
7791}
7792
Arjan van de Venfa027c22007-02-12 00:55:33 -08007793static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007794 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 .open = md_seq_open,
7796 .read = seq_read,
7797 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007798 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007799 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800};
7801
NeilBrown84fc4b52011-10-11 16:49:58 +11007802int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803{
NeilBrown9d487392016-11-02 14:16:49 +11007804 pr_debug("md: %s personality registered for level %d\n",
7805 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007806 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007807 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808 spin_unlock(&pers_lock);
7809 return 0;
7810}
NeilBrown6c144d32014-09-30 16:15:38 +10007811EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812
NeilBrown84fc4b52011-10-11 16:49:58 +11007813int unregister_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 unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007817 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007818 spin_unlock(&pers_lock);
7819 return 0;
7820}
NeilBrown6c144d32014-09-30 16:15:38 +10007821EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822
NeilBrown6022e752015-08-13 12:32:55 +10007823int register_md_cluster_operations(struct md_cluster_operations *ops,
7824 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007825{
NeilBrown6022e752015-08-13 12:32:55 +10007826 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007827 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007828 if (md_cluster_ops != NULL)
7829 ret = -EALREADY;
7830 else {
7831 md_cluster_ops = ops;
7832 md_cluster_mod = module;
7833 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007834 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007835 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007836}
7837EXPORT_SYMBOL(register_md_cluster_operations);
7838
7839int unregister_md_cluster_operations(void)
7840{
7841 spin_lock(&pers_lock);
7842 md_cluster_ops = NULL;
7843 spin_unlock(&pers_lock);
7844 return 0;
7845}
7846EXPORT_SYMBOL(unregister_md_cluster_operations);
7847
7848int md_setup_cluster(struct mddev *mddev, int nodes)
7849{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007850 if (!md_cluster_ops)
7851 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007852 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007853 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007854 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11007855 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007856 spin_unlock(&pers_lock);
7857 return -ENOENT;
7858 }
7859 spin_unlock(&pers_lock);
7860
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007861 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007862}
7863
7864void md_cluster_stop(struct mddev *mddev)
7865{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007866 if (!md_cluster_ops)
7867 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007868 md_cluster_ops->leave(mddev);
7869 module_put(md_cluster_mod);
7870}
7871
NeilBrownfd01b882011-10-11 16:47:53 +11007872static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007874 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007876 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877
7878 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007879 rcu_read_lock();
7880 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007882 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7883 (int)part_stat_read(&disk->part0, sectors[1]) -
7884 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007885 /* sync IO will cause sync_io to increase before the disk_stats
7886 * as sync_io is counted when a request starts, and
7887 * disk_stats is counted when it completes.
7888 * So resync activity will cause curr_events to be smaller than
7889 * when there was no such activity.
7890 * non-sync IO will cause disk_stat to increase without
7891 * increasing sync_io so curr_events will (eventually)
7892 * be larger than it was before. Once it becomes
7893 * substantially larger, the test below will cause
7894 * the array to appear non-idle, and resync will slow
7895 * down.
7896 * If there is a lot of outstanding resync activity when
7897 * we set last_event to curr_events, then all that activity
7898 * completing might cause the array to appear non-idle
7899 * and resync will be slowed down even though there might
7900 * not have been non-resync activity. This will only
7901 * happen once though. 'last_events' will soon reflect
7902 * the state where there is little or no outstanding
7903 * resync requests, and further resync activity will
7904 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007905 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007906 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007907 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007908 rdev->last_events = curr_events;
7909 idle = 0;
7910 }
7911 }
NeilBrown4b809912008-07-21 17:05:25 +10007912 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 return idle;
7914}
7915
NeilBrownfd01b882011-10-11 16:47:53 +11007916void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917{
7918 /* another "blocks" (512byte) blocks have been synced */
7919 atomic_sub(blocks, &mddev->recovery_active);
7920 wake_up(&mddev->recovery_wait);
7921 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007922 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007923 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924 md_wakeup_thread(mddev->thread);
7925 // stop recovery, signal do_sync ....
7926 }
7927}
NeilBrown6c144d32014-09-30 16:15:38 +10007928EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929
NeilBrown06d91a52005-06-21 17:17:12 -07007930/* md_write_start(mddev, bi)
7931 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007932 * in superblock) before writing, schedule a superblock update
7933 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007934 */
NeilBrownfd01b882011-10-11 16:47:53 +11007935void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936{
Neil Brown0fd62b82008-06-28 08:31:36 +10007937 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007938 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007939 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007940
NeilBrownf91de922005-11-08 21:39:36 -08007941 BUG_ON(mddev->ro == 1);
7942 if (mddev->ro == 2) {
7943 /* need to switch to read/write */
7944 mddev->ro = 0;
7945 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7946 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007947 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007948 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007949 }
NeilBrown4ad23a972017-03-15 14:05:14 +11007950 rcu_read_lock();
7951 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11007952 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07007953 if (mddev->safemode == 1)
7954 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11007955 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
7956 if (mddev->in_sync || !mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11007957 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007958 if (mddev->in_sync) {
7959 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08007960 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
7961 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007962 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007963 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007964 }
NeilBrown85572d72014-12-15 12:56:56 +11007965 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007966 }
NeilBrown4ad23a972017-03-15 14:05:14 +11007967 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10007968 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007969 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007970 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007971 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972}
NeilBrown6c144d32014-09-30 16:15:38 +10007973EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007974
NeilBrown49728052017-03-15 14:05:12 +11007975/* md_write_inc can only be called when md_write_start() has
7976 * already been called at least once of the current request.
7977 * It increments the counter and is useful when a single request
7978 * is split into several parts. Each part causes an increment and
7979 * so needs a matching md_write_end().
7980 * Unlike md_write_start(), it is safe to call md_write_inc() inside
7981 * a spinlocked region.
7982 */
7983void md_write_inc(struct mddev *mddev, struct bio *bi)
7984{
7985 if (bio_data_dir(bi) != WRITE)
7986 return;
7987 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11007988 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11007989}
7990EXPORT_SYMBOL(md_write_inc);
7991
NeilBrownfd01b882011-10-11 16:47:53 +11007992void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993{
NeilBrown4ad23a972017-03-15 14:05:14 +11007994 percpu_ref_put(&mddev->writes_pending);
7995
7996 if (mddev->safemode == 2)
7997 md_wakeup_thread(mddev->thread);
7998 else if (mddev->safemode_delay)
7999 /* The roundup() ensures this only performs locking once
8000 * every ->safemode_delay jiffies
8001 */
8002 mod_timer(&mddev->safemode_timer,
8003 roundup(jiffies, mddev->safemode_delay) +
8004 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005}
NeilBrown4ad23a972017-03-15 14:05:14 +11008006
NeilBrown6c144d32014-09-30 16:15:38 +10008007EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008008
NeilBrown2a2275d2007-01-26 00:57:11 -08008009/* md_allow_write(mddev)
8010 * Calling this ensures that the array is marked 'active' so that writes
8011 * may proceed without blocking. It is important to call this before
8012 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8013 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07008014 *
Shaohua Li29530792016-12-08 15:48:19 -08008015 * In the ->external case MD_SB_CHANGE_PENDING can not be cleared until mddev->lock
Dan Williamsb5470dc2008-06-27 21:44:04 -07008016 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08008017 */
NeilBrownfd01b882011-10-11 16:47:53 +11008018int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008019{
8020 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07008021 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08008022 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07008023 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10008024 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07008025 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08008026
NeilBrown85572d72014-12-15 12:56:56 +11008027 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008028 if (mddev->in_sync) {
8029 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008030 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8031 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008032 if (mddev->safemode_delay &&
8033 mddev->safemode == 0)
8034 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008035 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008036 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008037 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08008038 } else
NeilBrown85572d72014-12-15 12:56:56 +11008039 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07008040
Shaohua Li29530792016-12-08 15:48:19 -08008041 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07008042 return -EAGAIN;
8043 else
8044 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08008045}
8046EXPORT_SYMBOL_GPL(md_allow_write);
8047
Linus Torvalds1da177e2005-04-16 15:20:36 -07008048#define SYNC_MARKS 10
8049#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008050#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008051void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008052{
Shaohua Li4ed87312012-10-11 13:34:00 +11008053 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008054 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055 unsigned int currspeed = 0,
8056 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008057 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008058 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008059 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008060 sector_t mark_cnt[SYNC_MARKS];
8061 int last_mark,m;
8062 struct list_head *tmp;
8063 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008064 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008065 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008066 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008067 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008068 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008069
8070 /* just incase thread restarts... */
8071 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
8072 return;
NeilBrown3991b312014-05-28 13:39:23 +10008073 if (mddev->ro) {/* never try to sync a read-only array */
8074 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008075 return;
NeilBrown3991b312014-05-28 13:39:23 +10008076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008077
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008078 if (mddev_is_clustered(mddev)) {
8079 ret = md_cluster_ops->resync_start(mddev);
8080 if (ret)
8081 goto skip;
8082
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008083 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008084 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8085 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8086 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8087 && ((unsigned long long)mddev->curr_resync_completed
8088 < (unsigned long long)mddev->resync_max_sectors))
8089 goto skip;
8090 }
8091
NeilBrown61df9d92006-10-03 01:15:57 -07008092 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008093 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008094 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008095 action = "check";
8096 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008097 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008098 action = "repair";
8099 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008100 desc = "resync";
8101 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8102 desc = "reshape";
8103 else
8104 desc = "recovery";
8105
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008106 mddev->last_sync_action = action ?: desc;
8107
Linus Torvalds1da177e2005-04-16 15:20:36 -07008108 /* we overload curr_resync somewhat here.
8109 * 0 == not engaged in resync at all
8110 * 2 == checking that there is no conflict with another sync
8111 * 1 == like 2, but have yielded to allow conflicting resync to
8112 * commense
8113 * other == active in resync - this many blocks
8114 *
8115 * Before starting a resync we must have set curr_resync to
8116 * 2, and then checked that every "conflicting" array has curr_resync
8117 * less than ours. When we find one that is the same or higher
8118 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8119 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8120 * This will mean we have to start checking from the beginning again.
8121 *
8122 */
8123
8124 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008125 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008126 mddev->curr_resync = 2;
8127
8128 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008129 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008130 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008131 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008132 if (mddev2 == mddev)
8133 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008134 if (!mddev->parallel_resync
8135 && mddev2->curr_resync
8136 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008137 DEFINE_WAIT(wq);
8138 if (mddev < mddev2 && mddev->curr_resync == 2) {
8139 /* arbitrarily yield */
8140 mddev->curr_resync = 1;
8141 wake_up(&resync_wait);
8142 }
8143 if (mddev > mddev2 && mddev->curr_resync == 1)
8144 /* no need to wait here, we can wait the next
8145 * time 'round when curr_resync == 2
8146 */
8147 continue;
NeilBrown97441972008-09-19 11:49:54 +10008148 /* We need to wait 'interruptible' so as not to
8149 * contribute to the load average, and not to
8150 * be caught by 'softlockup'
8151 */
8152 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008153 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008154 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008155 if (mddev2_minor != mddev2->md_minor) {
8156 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008157 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8158 desc, mdname(mddev),
8159 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008162 if (signal_pending(current))
8163 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008164 schedule();
8165 finish_wait(&resync_wait, &wq);
8166 goto try_again;
8167 }
8168 finish_wait(&resync_wait, &wq);
8169 }
8170 }
8171 } while (mddev->curr_resync < 2);
8172
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008173 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008174 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008176 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008177 */
8178 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008179 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008180 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008181 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8182 j = mddev->resync_min;
8183 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008184 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008185
NeilBrownccfcc3c2006-03-27 01:18:09 -08008186 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10008187 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008188 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008190 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008191 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008192 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008193 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008194 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008195 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008196 !test_bit(Faulty, &rdev->flags) &&
8197 !test_bit(In_sync, &rdev->flags) &&
8198 rdev->recovery_offset < j)
8199 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008200 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008201
8202 /* If there is a bitmap, we need to make sure all
8203 * writes that started before we added a spare
8204 * complete before we start doing a recovery.
8205 * Otherwise the write might complete and (via
8206 * bitmap_endwrite) set a bit in the bitmap after the
8207 * recovery has checked that bit and skipped that
8208 * region.
8209 */
8210 if (mddev->bitmap) {
8211 mddev->pers->quiesce(mddev, 1);
8212 mddev->pers->quiesce(mddev, 0);
8213 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215
NeilBrown9d487392016-11-02 14:16:49 +11008216 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8217 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8218 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8219 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008220
NeilBrowneea1bf32009-03-31 14:27:02 +11008221 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008222
NeilBrown57afd892005-06-21 17:17:13 -07008223 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224 for (m = 0; m < SYNC_MARKS; m++) {
8225 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008226 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227 }
8228 last_mark = 0;
8229 mddev->resync_mark = mark[last_mark];
8230 mddev->resync_mark_cnt = mark_cnt[last_mark];
8231
8232 /*
8233 * Tune reconstruction:
8234 */
8235 window = 32*(PAGE_SIZE/512);
NeilBrown9d487392016-11-02 14:16:49 +11008236 pr_debug("md: using %dk window, over a total of %lluk.\n",
8237 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238
8239 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240 last_check = 0;
8241
8242 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008243 pr_debug("md: resuming %s of %s from checkpoint.\n",
8244 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008245 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008246 } else
8247 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008248 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008249 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8250 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008251 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008252
majianpeng7c2c57c2012-07-03 12:12:26 +10008253 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008254 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008255 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008256
NeilBrown57afd892005-06-21 17:17:13 -07008257 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008258
NeilBrown7a91ee12009-05-26 12:57:21 +10008259 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8260 ((mddev->curr_resync > mddev->curr_resync_completed &&
8261 (mddev->curr_resync - mddev->curr_resync_completed)
8262 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008263 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008264 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008265 >= mddev->resync_max - mddev->curr_resync_completed ||
8266 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008267 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008268 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008269 wait_event(mddev->recovery_wait,
8270 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008271 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008272 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8273 j > mddev->recovery_cp)
8274 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008275 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008276 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008277 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008278 }
NeilBrownacb180b2009-04-14 16:28:34 +10008279
NeilBrownc91abf52013-11-19 12:02:01 +11008280 while (j >= mddev->resync_max &&
8281 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008282 /* As this condition is controlled by user-space,
8283 * we can block indefinitely, so use '_interruptible'
8284 * to avoid triggering warnings.
8285 */
8286 flush_signals(current); /* just in case */
8287 wait_event_interruptible(mddev->recovery_wait,
8288 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008289 || test_bit(MD_RECOVERY_INTR,
8290 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008291 }
NeilBrownacb180b2009-04-14 16:28:34 +10008292
NeilBrownc91abf52013-11-19 12:02:01 +11008293 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8294 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008295
NeilBrown09314792015-02-19 16:04:40 +11008296 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008297 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008298 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008299 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008300 }
NeilBrown57afd892005-06-21 17:17:13 -07008301
8302 if (!skipped) { /* actual IO requested */
8303 io_sectors += sectors;
8304 atomic_add(sectors, &mddev->recovery_active);
8305 }
8306
NeilBrowne875ece2011-07-28 11:39:24 +10008307 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8308 break;
8309
Linus Torvalds1da177e2005-04-16 15:20:36 -07008310 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008311 if (j > max_sectors)
8312 /* when skipping, extra large numbers can be returned. */
8313 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008314 if (j > 2)
8315 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008316 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008317 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008318 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008319 * visible in /proc/mdstat
8320 */
8321 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008322
8323 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324 continue;
8325
NeilBrown57afd892005-06-21 17:17:13 -07008326 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008327 repeat:
8328 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8329 /* step marks */
8330 int next = (last_mark+1) % SYNC_MARKS;
8331
8332 mddev->resync_mark = mark[next];
8333 mddev->resync_mark_cnt = mark_cnt[next];
8334 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008335 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008336 last_mark = next;
8337 }
8338
NeilBrownc91abf52013-11-19 12:02:01 +11008339 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8340 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341
8342 /*
8343 * this loop exits only if either when we are slower than
8344 * the 'hard' speed limit, or the system was IO-idle for
8345 * a jiffy.
8346 * the system might be non-idle CPU-wise, but we only care
8347 * about not overloading the IO subsystem. (things like an
8348 * e2fsck being done on the RAID array should execute fast)
8349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008350 cond_resched();
8351
Xiao Niac7e50a2014-08-07 09:37:41 -04008352 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8353 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008354 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355
NeilBrown88202a02006-01-06 00:21:36 -08008356 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008357 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008358 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008359 goto repeat;
8360 }
NeilBrownac8fa412015-02-19 16:55:00 +11008361 if (!is_mddev_idle(mddev, 0)) {
8362 /*
8363 * Give other IO more of a chance.
8364 * The faster the devices, the less we wait.
8365 */
8366 wait_event(mddev->recovery_wait,
8367 !atomic_read(&mddev->recovery_active));
8368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008369 }
8370 }
NeilBrown9d487392016-11-02 14:16:49 +11008371 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8372 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8373 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374 /*
8375 * this also signals 'finished resyncing' to md_stop
8376 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008377 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8379
NeilBrown5ed1df22015-07-24 13:27:08 +10008380 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8381 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008382 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008383 mddev->curr_resync_completed = mddev->curr_resync;
8384 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8385 }
NeilBrown09314792015-02-19 16:04:40 +11008386 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008387
NeilBrowndfc70642008-05-23 13:04:39 -07008388 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008389 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008390 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8391 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8392 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008393 pr_debug("md: checkpointing %s of %s.\n",
8394 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008395 if (test_bit(MD_RECOVERY_ERROR,
8396 &mddev->recovery))
8397 mddev->recovery_cp =
8398 mddev->curr_resync_completed;
8399 else
8400 mddev->recovery_cp =
8401 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008402 }
8403 } else
8404 mddev->recovery_cp = MaxSector;
8405 } else {
8406 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8407 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008408 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008409 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008410 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008411 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008412 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008413 !test_bit(Faulty, &rdev->flags) &&
8414 !test_bit(In_sync, &rdev->flags) &&
8415 rdev->recovery_offset < mddev->curr_resync)
8416 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008417 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008419 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008420 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008421 /* set CHANGE_PENDING here since maybe another update is needed,
8422 * so other nodes are informed. It should be harmless for normal
8423 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008424 set_mask_bits(&mddev->sb_flags, 0,
8425 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008426
NeilBrown23da4222014-12-15 12:57:01 +11008427 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008428 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8429 /* We completed so min/max setting can be forgotten if used. */
8430 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8431 mddev->resync_min = 0;
8432 mddev->resync_max = MaxSector;
8433 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8434 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008435 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008436 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008437 spin_unlock(&mddev->lock);
8438
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008440 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008441 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442}
NeilBrown29269552006-03-27 01:18:10 -08008443EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008444
NeilBrown746d3202013-04-24 11:42:41 +10008445static int remove_and_add_spares(struct mddev *mddev,
8446 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008447{
NeilBrown3cb03002011-10-11 16:45:26 +11008448 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008449 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008450 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008451 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008452
NeilBrownd787be42016-06-02 16:19:53 +10008453 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008454 if ((this == NULL || rdev == this) &&
8455 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008456 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008457 test_bit(Faulty, &rdev->flags) &&
8458 atomic_read(&rdev->nr_pending)==0) {
8459 /* Faulty non-Blocked devices with nr_pending == 0
8460 * never get nr_pending incremented,
8461 * never get Faulty cleared, and never get Blocked set.
8462 * So we can synchronize_rcu now rather than once per device
8463 */
8464 remove_some = true;
8465 set_bit(RemoveSynchronized, &rdev->flags);
8466 }
8467 }
8468
8469 if (remove_some)
8470 synchronize_rcu();
8471 rdev_for_each(rdev, mddev) {
8472 if ((this == NULL || rdev == this) &&
8473 rdev->raid_disk >= 0 &&
8474 !test_bit(Blocked, &rdev->flags) &&
8475 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008476 (!test_bit(In_sync, &rdev->flags) &&
8477 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008478 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008479 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008480 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008481 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008482 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008483 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008484 }
8485 }
NeilBrownd787be42016-06-02 16:19:53 +10008486 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8487 clear_bit(RemoveSynchronized, &rdev->flags);
8488 }
8489
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008490 if (removed && mddev->kobj.sd)
8491 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008492
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008493 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008494 goto no_add;
8495
NeilBrowndafb20f2012-03-19 12:46:39 +11008496 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008497 if (this && this != rdev)
8498 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008499 if (test_bit(Candidate, &rdev->flags))
8500 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008501 if (rdev->raid_disk >= 0 &&
8502 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008503 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008504 !test_bit(Faulty, &rdev->flags))
8505 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008506 if (rdev->raid_disk >= 0)
8507 continue;
8508 if (test_bit(Faulty, &rdev->flags))
8509 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008510 if (!test_bit(Journal, &rdev->flags)) {
8511 if (mddev->ro &&
8512 ! (rdev->saved_raid_disk >= 0 &&
8513 !test_bit(Bitmap_sync, &rdev->flags)))
8514 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008515
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008516 rdev->recovery_offset = 0;
8517 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008518 if (mddev->pers->
8519 hot_add_disk(mddev, rdev) == 0) {
8520 if (sysfs_link_rdev(mddev, rdev))
8521 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008522 if (!test_bit(Journal, &rdev->flags))
8523 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008524 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08008525 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008526 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008527 }
NeilBrown746d3202013-04-24 11:42:41 +10008528no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008529 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08008530 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008531 return spares;
8532}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008533
NeilBrownac05f252014-09-30 08:10:42 +10008534static void md_start_sync(struct work_struct *ws)
8535{
8536 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008537
NeilBrownac05f252014-09-30 08:10:42 +10008538 mddev->sync_thread = md_register_thread(md_do_sync,
8539 mddev,
8540 "resync");
8541 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11008542 pr_warn("%s: could not start resync thread...\n",
8543 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008544 /* leave the spares where they are, it shouldn't hurt */
8545 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8546 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8547 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8548 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8549 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008550 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008551 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8552 &mddev->recovery))
8553 if (mddev->sysfs_action)
8554 sysfs_notify_dirent_safe(mddev->sysfs_action);
8555 } else
8556 md_wakeup_thread(mddev->sync_thread);
8557 sysfs_notify_dirent_safe(mddev->sysfs_action);
8558 md_new_event(mddev);
8559}
8560
Linus Torvalds1da177e2005-04-16 15:20:36 -07008561/*
8562 * This routine is regularly called by all per-raid-array threads to
8563 * deal with generic issues like resync and super-block update.
8564 * Raid personalities that don't have a thread (linear/raid0) do not
8565 * need this as they never do any recovery or update the superblock.
8566 *
8567 * It does not do any resync itself, but rather "forks" off other threads
8568 * to do that as needed.
8569 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8570 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008571 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008572 * and wakeups up this thread which will reap the thread and finish up.
8573 * This thread also removes any faulty devices (with nr_pending == 0).
8574 *
8575 * The overall approach is:
8576 * 1/ if the superblock needs updating, update it.
8577 * 2/ If a recovery thread is running, don't do anything else.
8578 * 3/ If recovery has finished, clean up, possibly marking spares active.
8579 * 4/ If there are any faulty devices, remove them.
8580 * 5/ If array is degraded, try to add spares devices
8581 * 6/ If array has spares or is not in-sync, start a resync thread.
8582 */
NeilBrownfd01b882011-10-11 16:47:53 +11008583void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008584{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008585 if (mddev->suspended)
8586 return;
8587
NeilBrown5f404022005-06-21 17:17:16 -07008588 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008589 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590
NeilBrownfca4d842005-06-21 17:17:11 -07008591 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008592 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11008593 pr_debug("md: %s in immediate safe mode\n",
8594 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07008595 mddev->safemode = 2;
8596 }
8597 flush_signals(current);
8598 }
8599
NeilBrownc89a8ee2008-08-05 15:54:13 +10008600 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8601 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008602 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08008603 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008604 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008605 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008606 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11008607 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07008608 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008609 ))
8610 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008611
NeilBrowndf5b89b2006-03-27 01:18:20 -08008612 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008613 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008614
NeilBrownc89a8ee2008-08-05 15:54:13 +10008615 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008616 struct md_rdev *rdev;
8617 if (!mddev->external && mddev->in_sync)
8618 /* 'Blocked' flag not needed as failed devices
8619 * will be recorded if array switched to read/write.
8620 * Leaving it set will prevent the device
8621 * from being removed.
8622 */
8623 rdev_for_each(rdev, mddev)
8624 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008625 /* On a read-only array we can:
8626 * - remove failed devices
8627 * - add already-in_sync devices if the array itself
8628 * is in-sync.
8629 * As we only add devices that are already in-sync,
8630 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008631 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008632 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008633 /* There is no thread, but we need to call
8634 * ->spare_active and clear saved_raid_disk
8635 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008636 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008637 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008638 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008639 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08008640 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008641 goto unlock;
8642 }
8643
Guoqing Jiang659b2542015-12-21 10:50:59 +11008644 if (mddev_is_clustered(mddev)) {
8645 struct md_rdev *rdev;
8646 /* kick the device if another node issued a
8647 * remove disk.
8648 */
8649 rdev_for_each(rdev, mddev) {
8650 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8651 rdev->raid_disk < 0)
8652 md_kick_rdev_from_array(rdev);
8653 }
8654 }
8655
NeilBrown6497709b2017-03-15 14:05:14 +11008656 if (!mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11008657 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11008658 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11008659 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07008660 }
NeilBrownfca4d842005-06-21 17:17:11 -07008661
Shaohua Li29530792016-12-08 15:48:19 -08008662 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07008663 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008664
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8666 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8667 /* resync/recovery still happening */
8668 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8669 goto unlock;
8670 }
8671 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008672 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008673 goto unlock;
8674 }
Neil Brown72a23c22008-06-28 08:31:41 +10008675 /* Set RUNNING before clearing NEEDED to avoid
8676 * any transients in the value of "sync_action".
8677 */
NeilBrown72f36d52012-10-11 14:25:57 +11008678 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008679 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008680 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008681 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008682 /* Clear some bits that don't mean anything, but
8683 * might be left set
8684 */
NeilBrown24dd4692005-11-08 21:39:26 -08008685 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8686 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008687
NeilBrowned209582012-04-24 10:23:14 +10008688 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8689 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008690 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008691 /* no recovery is running.
8692 * remove any failed drives, then
8693 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008694 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008695 * the personality to fail the re-add.
8696 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008697
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008698 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008699 if (mddev->pers->check_reshape == NULL ||
8700 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008701 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008702 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008703 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008704 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008705 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008706 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8707 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008708 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008709 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008710 } else if (mddev->recovery_cp < MaxSector) {
8711 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008712 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008713 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8714 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008715 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008716
Linus Torvalds1da177e2005-04-16 15:20:36 -07008717 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008718 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008719 /* We are adding a device or devices to an array
8720 * which has the bitmap stored on all devices.
8721 * So make sure all bitmap pages get written
8722 */
8723 bitmap_write_all(mddev->bitmap);
8724 }
NeilBrownac05f252014-09-30 08:10:42 +10008725 INIT_WORK(&mddev->del_work, md_start_sync);
8726 queue_work(md_misc_wq, &mddev->del_work);
8727 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008728 }
NeilBrownac05f252014-09-30 08:10:42 +10008729 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008730 if (!mddev->sync_thread) {
8731 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008732 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008733 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8734 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008735 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008736 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008737 }
NeilBrownac05f252014-09-30 08:10:42 +10008738 unlock:
8739 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740 mddev_unlock(mddev);
8741 }
8742}
NeilBrown6c144d32014-09-30 16:15:38 +10008743EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008744
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008745void md_reap_sync_thread(struct mddev *mddev)
8746{
8747 struct md_rdev *rdev;
8748
8749 /* resync has finished, collect result */
8750 md_unregister_thread(&mddev->sync_thread);
8751 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8752 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8753 /* success...*/
8754 /* activate any spares */
8755 if (mddev->pers->spare_active(mddev)) {
8756 sysfs_notify(&mddev->kobj, NULL,
8757 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08008758 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008759 }
8760 }
8761 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8762 mddev->pers->finish_reshape)
8763 mddev->pers->finish_reshape(mddev);
8764
8765 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008766 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008767 */
NeilBrownf4667222013-12-09 12:04:56 +11008768 if (!mddev->degraded)
8769 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008770 rdev->saved_raid_disk = -1;
8771
8772 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08008773 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008774 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8775 * clustered raid */
8776 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8777 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008778 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008779 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008780 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8781 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8782 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8783 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008784 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008785 /* flag recovery needed just to double check */
8786 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8787 sysfs_notify_dirent_safe(mddev->sysfs_action);
8788 md_new_event(mddev);
8789 if (mddev->event_work.func)
8790 queue_work(md_misc_wq, &mddev->event_work);
8791}
NeilBrown6c144d32014-09-30 16:15:38 +10008792EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008793
NeilBrownfd01b882011-10-11 16:47:53 +11008794void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008795{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008796 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008797 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008798 !test_bit(Blocked, &rdev->flags) &&
8799 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008800 msecs_to_jiffies(5000));
8801 rdev_dec_pending(rdev, mddev);
8802}
8803EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8804
NeilBrownc6563a82012-05-21 09:27:00 +10008805void md_finish_reshape(struct mddev *mddev)
8806{
8807 /* called be personality module when reshape completes. */
8808 struct md_rdev *rdev;
8809
8810 rdev_for_each(rdev, mddev) {
8811 if (rdev->data_offset > rdev->new_data_offset)
8812 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8813 else
8814 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8815 rdev->data_offset = rdev->new_data_offset;
8816 }
8817}
8818EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008819
Vishal Vermafc974ee2015-12-24 19:20:34 -07008820/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10008821
Vishal Vermafc974ee2015-12-24 19:20:34 -07008822/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11008823int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008824 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008825{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008826 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10008827 int rv;
8828 if (is_new)
8829 s += rdev->new_data_offset;
8830 else
8831 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008832 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8833 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008834 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008835 if (test_bit(ExternalBbl, &rdev->flags))
8836 sysfs_notify(&rdev->kobj, NULL,
8837 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008838 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08008839 set_mask_bits(&mddev->sb_flags, 0,
8840 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10008841 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07008842 return 1;
8843 } else
8844 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008845}
8846EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8847
NeilBrownc6563a82012-05-21 09:27:00 +10008848int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8849 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008850{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008851 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10008852 if (is_new)
8853 s += rdev->new_data_offset;
8854 else
8855 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008856 rv = badblocks_clear(&rdev->badblocks, s, sectors);
8857 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
8858 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
8859 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008860}
8861EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8862
Adrian Bunk75c96f82005-05-05 16:16:09 -07008863static int md_notify_reboot(struct notifier_block *this,
8864 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008865{
8866 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008867 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008868 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008869
NeilBrownc744a652012-03-19 12:46:37 +11008870 for_each_mddev(mddev, tmp) {
8871 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008872 if (mddev->pers)
8873 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008874 if (mddev->persistent)
8875 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008876 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008877 }
NeilBrownc744a652012-03-19 12:46:37 +11008878 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008879 }
NeilBrownc744a652012-03-19 12:46:37 +11008880 /*
8881 * certain more exotic SCSI devices are known to be
8882 * volatile wrt too early system reboots. While the
8883 * right place to handle this issue is the given
8884 * driver, we do want to have a safe RAID driver ...
8885 */
8886 if (need_delay)
8887 mdelay(1000*1);
8888
Linus Torvalds1da177e2005-04-16 15:20:36 -07008889 return NOTIFY_DONE;
8890}
8891
Adrian Bunk75c96f82005-05-05 16:16:09 -07008892static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008893 .notifier_call = md_notify_reboot,
8894 .next = NULL,
8895 .priority = INT_MAX, /* before any real devices */
8896};
8897
8898static void md_geninit(void)
8899{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008900 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008901
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008902 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008903}
8904
Adrian Bunk75c96f82005-05-05 16:16:09 -07008905static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008906{
Tejun Heoe804ac72010-10-15 15:36:08 +02008907 int ret = -ENOMEM;
8908
Tejun Heoada609e2011-01-25 14:35:54 +01008909 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008910 if (!md_wq)
8911 goto err_wq;
8912
8913 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8914 if (!md_misc_wq)
8915 goto err_misc_wq;
8916
8917 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8918 goto err_md;
8919
8920 if ((ret = register_blkdev(0, "mdp")) < 0)
8921 goto err_mdp;
8922 mdp_major = ret;
8923
NeilBrownaf5628f2014-07-31 13:54:54 +10008924 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008925 md_probe, NULL, NULL);
8926 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008927 md_probe, NULL, NULL);
8928
Linus Torvalds1da177e2005-04-16 15:20:36 -07008929 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008930 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008931
8932 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934
Tejun Heoe804ac72010-10-15 15:36:08 +02008935err_mdp:
8936 unregister_blkdev(MD_MAJOR, "md");
8937err_md:
8938 destroy_workqueue(md_misc_wq);
8939err_misc_wq:
8940 destroy_workqueue(md_wq);
8941err_wq:
8942 return ret;
8943}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008944
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008945static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008946{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008947 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8948 struct md_rdev *rdev2;
8949 int role, ret;
8950 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008951
Guoqing Jiang818da592017-03-01 16:42:40 +08008952 /*
8953 * If size is changed in another node then we need to
8954 * do resize as well.
8955 */
8956 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
8957 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
8958 if (ret)
8959 pr_info("md-cluster: resize failed\n");
8960 else
8961 bitmap_update_sb(mddev->bitmap);
8962 }
8963
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008964 /* Check for change of roles in the active devices */
8965 rdev_for_each(rdev2, mddev) {
8966 if (test_bit(Faulty, &rdev2->flags))
8967 continue;
8968
8969 /* Check if the roles changed */
8970 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008971
8972 if (test_bit(Candidate, &rdev2->flags)) {
8973 if (role == 0xfffe) {
8974 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
8975 md_kick_rdev_from_array(rdev2);
8976 continue;
8977 }
8978 else
8979 clear_bit(Candidate, &rdev2->flags);
8980 }
8981
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008982 if (role != rdev2->raid_disk) {
8983 /* got activated */
8984 if (rdev2->raid_disk == -1 && role != 0xffff) {
8985 rdev2->saved_raid_disk = role;
8986 ret = remove_and_add_spares(mddev, rdev2);
8987 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11008988 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04008989 /* wakeup mddev->thread here, so array could
8990 * perform resync with the new activated disk */
8991 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8992 md_wakeup_thread(mddev->thread);
8993
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008994 }
8995 /* device faulty
8996 * We just want to do the minimum to mark the disk
8997 * as faulty. The recovery is performed by the
8998 * one who initiated the error.
8999 */
9000 if ((role == 0xfffe) || (role == 0xfffd)) {
9001 md_error(mddev, rdev2);
9002 clear_bit(Blocked, &rdev2->flags);
9003 }
9004 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009005 }
9006
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009007 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9008 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009009
9010 /* Finally set the event to be up to date */
9011 mddev->events = le64_to_cpu(sb->events);
9012}
9013
9014static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9015{
9016 int err;
9017 struct page *swapout = rdev->sb_page;
9018 struct mdp_superblock_1 *sb;
9019
9020 /* Store the sb page of the rdev in the swapout temporary
9021 * variable in case we err in the future
9022 */
9023 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009024 err = alloc_disk_sb(rdev);
9025 if (err == 0) {
9026 ClearPageUptodate(rdev->sb_page);
9027 rdev->sb_loaded = 0;
9028 err = super_types[mddev->major_version].
9029 load_super(rdev, NULL, mddev->minor_version);
9030 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009031 if (err < 0) {
9032 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9033 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009034 if (rdev->sb_page)
9035 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009036 rdev->sb_page = swapout;
9037 rdev->sb_loaded = 1;
9038 return err;
9039 }
9040
9041 sb = page_address(rdev->sb_page);
9042 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9043 * is not set
9044 */
9045
9046 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9047 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9048
9049 /* The other node finished recovery, call spare_active to set
9050 * device In_sync and mddev->degraded
9051 */
9052 if (rdev->recovery_offset == MaxSector &&
9053 !test_bit(In_sync, &rdev->flags) &&
9054 mddev->pers->spare_active(mddev))
9055 sysfs_notify(&mddev->kobj, NULL, "degraded");
9056
9057 put_page(swapout);
9058 return 0;
9059}
9060
9061void md_reload_sb(struct mddev *mddev, int nr)
9062{
9063 struct md_rdev *rdev;
9064 int err;
9065
9066 /* Find the rdev */
9067 rdev_for_each_rcu(rdev, mddev) {
9068 if (rdev->desc_nr == nr)
9069 break;
9070 }
9071
9072 if (!rdev || rdev->desc_nr != nr) {
9073 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9074 return;
9075 }
9076
9077 err = read_rdev(mddev, rdev);
9078 if (err < 0)
9079 return;
9080
9081 check_sb_changes(mddev, rdev);
9082
9083 /* Read all rdev's to update recovery_offset */
9084 rdev_for_each_rcu(rdev, mddev)
9085 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009086}
9087EXPORT_SYMBOL(md_reload_sb);
9088
Linus Torvalds1da177e2005-04-16 15:20:36 -07009089#ifndef MODULE
9090
9091/*
9092 * Searches all registered partitions for autorun RAID arrays
9093 * at boot time.
9094 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009095
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009096static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009097static LIST_HEAD(all_detected_devices);
9098struct detected_devices_node {
9099 struct list_head list;
9100 dev_t dev;
9101};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009102
9103void md_autodetect_dev(dev_t dev)
9104{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009105 struct detected_devices_node *node_detected_dev;
9106
9107 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9108 if (node_detected_dev) {
9109 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009110 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009111 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009112 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009114}
9115
Linus Torvalds1da177e2005-04-16 15:20:36 -07009116static void autostart_arrays(int part)
9117{
NeilBrown3cb03002011-10-11 16:45:26 +11009118 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009119 struct detected_devices_node *node_detected_dev;
9120 dev_t dev;
9121 int i_scanned, i_passed;
9122
9123 i_scanned = 0;
9124 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009125
NeilBrown9d487392016-11-02 14:16:49 +11009126 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009127
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009128 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009129 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9130 i_scanned++;
9131 node_detected_dev = list_entry(all_detected_devices.next,
9132 struct detected_devices_node, list);
9133 list_del(&node_detected_dev->list);
9134 dev = node_detected_dev->dev;
9135 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009136 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009137 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009138 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009139 if (IS_ERR(rdev))
9140 continue;
9141
NeilBrown403df472014-09-30 15:52:29 +10009142 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009143 continue;
NeilBrown403df472014-09-30 15:52:29 +10009144
NeilBrownd0fae182008-03-04 14:29:31 -08009145 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009146 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009147 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009148 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009149 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009150
NeilBrown9d487392016-11-02 14:16:49 +11009151 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009152
9153 autorun_devices(part);
9154}
9155
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009156#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009157
9158static __exit void md_exit(void)
9159{
NeilBrownfd01b882011-10-11 16:47:53 +11009160 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009161 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009162 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009163
NeilBrownaf5628f2014-07-31 13:54:54 +10009164 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009165 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009166
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009167 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009168 unregister_blkdev(mdp_major, "mdp");
9169 unregister_reboot_notifier(&md_notifier);
9170 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009171
9172 /* We cannot unload the modules while some process is
9173 * waiting for us in select() or poll() - wake them up
9174 */
9175 md_unloading = 1;
9176 while (waitqueue_active(&md_event_waiters)) {
9177 /* not safe to leave yet */
9178 wake_up(&md_event_waiters);
9179 msleep(delay);
9180 delay += delay;
9181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009182 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009183
NeilBrown29ac4aa2008-02-06 01:39:58 -08009184 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009185 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009186 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009187 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009188 /*
9189 * for_each_mddev() will call mddev_put() at the end of each
9190 * iteration. As the mddev is now fully clear, this will
9191 * schedule the mddev for destruction by a workqueue, and the
9192 * destroy_workqueue() below will wait for that to complete.
9193 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009194 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009195 destroy_workqueue(md_misc_wq);
9196 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009197}
9198
Dan Williams685784a2007-07-09 11:56:42 -07009199subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200module_exit(md_exit)
9201
NeilBrownf91de922005-11-08 21:39:36 -08009202static int get_ro(char *buffer, struct kernel_param *kp)
9203{
9204 return sprintf(buffer, "%d", start_readonly);
9205}
9206static int set_ro(const char *val, struct kernel_param *kp)
9207{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009208 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009209}
9210
NeilBrown80ca3a42006-07-10 04:44:18 -07009211module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9212module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009213module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009214module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009215
Linus Torvalds1da177e2005-04-16 15:20:36 -07009216MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009217MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009218MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009219MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);