blob: 87edc342ccb3d5c51bb45313f1218f9839528917 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
NeilBrownf72ffdd2014-09-30 14:23:59 +10003 Copyright (C) 1998, 1999, 2000 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
NeilBrown9d487392016-11-02 14:16:49 +110033
34 Errors, Warnings, etc.
35 Please use:
36 pr_crit() for error conditions that risk data loss
37 pr_err() for error conditions that are unexpected, like an IO error
38 or internal inconsistency
39 pr_warn() for error conditions that could have been predicated, like
40 adding a device to an array when it has incompatible metadata
41 pr_info() for every interesting, very rare events, like an array starting
42 or stopping, or resync starting or stopping
43 pr_debug() for everything else.
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045*/
46
Ingo Molnar3f07c012017-02-08 18:51:30 +010047#include <linux/sched/signal.h>
NeilBrowna6fb0932005-09-09 16:23:56 -070048#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110049#include <linux/blkdev.h>
Vishal Vermafc974ee2015-12-24 19:20:34 -070050#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040053#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080054#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070055#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080056#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110057#include <linux/hdreg.h>
58#include <linux/proc_fs.h>
59#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040060#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110061#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070062#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110063#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110064#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110065#include <linux/raid/md_p.h>
66#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090067#include <linux/slab.h>
NeilBrown4ad23a972017-03-15 14:05:14 +110068#include <linux/percpu-refcount.h>
69
Shaohua Li504634f2016-11-18 09:44:08 -080070#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110071#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110072#include "bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050073#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110076static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#endif
78
NeilBrown01f96c02011-09-21 15:30:20 +100079/* pers_list is a list of registered personalities protected
80 * by pers_lock.
81 * pers_lock does extra service to protect accesses to
82 * mddev->thread when the mutex cannot be held.
83 */
NeilBrown2604b702006-01-06 00:20:36 -080084static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static DEFINE_SPINLOCK(pers_lock);
86
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050087struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050088EXPORT_SYMBOL(md_cluster_ops);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050089struct module *md_cluster_mod;
90EXPORT_SYMBOL(md_cluster_mod);
91
Bernd Schubert90b08712008-05-23 13:04:38 -070092static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020093static struct workqueue_struct *md_wq;
94static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070095
NeilBrown746d3202013-04-24 11:42:41 +100096static int remove_and_add_spares(struct mddev *mddev,
97 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110098static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
Robert Becker1e509152009-12-14 12:49:58 +1100101 * Default number of read corrections we'll attempt on an rdev
102 * before ejecting it from the array. We divide the read error
103 * count by 2 for every hour elapsed between read errors.
104 */
105#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
106/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
108 * is 1000 KB/sec, so the extra system load does not show up that much.
109 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -0700110 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * subsystem is idle. There is also an 'absolute maximum' reconstruction
112 * speed limit - in case reconstruction slows down your system despite
113 * idle IO detection.
114 *
115 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800116 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118
119static int sysctl_speed_limit_min = 1000;
120static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100121static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800122{
123 return mddev->sync_speed_min ?
124 mddev->sync_speed_min : sysctl_speed_limit_min;
125}
126
NeilBrownfd01b882011-10-11 16:47:53 +1100127static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800128{
129 return mddev->sync_speed_max ?
130 mddev->sync_speed_max : sysctl_speed_limit_max;
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133static struct ctl_table_header *raid_table_header;
134
Joe Perches82592c32013-11-14 15:16:18 +1100135static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .procname = "speed_limit_min",
138 .data = &sysctl_speed_limit_min,
139 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700140 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800141 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 },
143 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .procname = "speed_limit_max",
145 .data = &sysctl_speed_limit_max,
146 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700147 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800148 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800150 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Joe Perches82592c32013-11-14 15:16:18 +1100153static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .procname = "raid",
156 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700157 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .child = raid_table,
159 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800160 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
Joe Perches82592c32013-11-14 15:16:18 +1100163static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 .procname = "dev",
166 .maxlen = 0,
167 .mode = 0555,
168 .child = raid_dir_table,
169 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800170 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700173static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
NeilBrownf91de922005-11-08 21:39:36 -0800175static int start_readonly;
176
NeilBrown78b63502017-04-12 16:26:13 +1000177/*
178 * The original mechanism for creating an md device is to create
179 * a device node in /dev and to open it. This causes races with device-close.
180 * The preferred method is to write to the "new_array" module parameter.
181 * This can avoid races.
182 * Setting create_on_open to false disables the original mechanism
183 * so all the races disappear.
184 */
185static bool create_on_open = true;
186
NeilBrowna167f662010-10-26 18:31:13 +1100187/* bio_clone_mddev
188 * like bio_clone, but with a local bio set
189 */
190
NeilBrowna167f662010-10-26 18:31:13 +1100191struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100192 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100193{
194 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100195
196 if (!mddev || !mddev->bio_set)
197 return bio_alloc(gfp_mask, nr_iovecs);
198
Kent Overstreet395c72a2012-09-06 15:34:55 -0700199 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100200 if (!b)
201 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100202 return b;
203}
204EXPORT_SYMBOL_GPL(bio_alloc_mddev);
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/*
NeilBrownd7603b72006-01-06 00:20:30 -0800207 * We have a system wide 'event count' that is incremented
208 * on any 'interesting' event, and readers of /proc/mdstat
209 * can use 'poll' or 'select' to find out when the event
210 * count increases.
211 *
212 * Events are:
213 * start array, stop array, error, add device, remove device,
214 * start build, activate spare
215 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800216static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800217static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100218void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800219{
220 atomic_inc(&md_event_count);
221 wake_up(&md_event_waiters);
222}
NeilBrown29269552006-03-27 01:18:10 -0800223EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800224
225/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 * Enables to iterate over all existing md arrays
227 * all_mddevs_lock protects this list.
228 */
229static LIST_HEAD(all_mddevs);
230static DEFINE_SPINLOCK(all_mddevs_lock);
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/*
233 * iterates through all used mddevs in the system.
234 * We take care to grab the all_mddevs_lock whenever navigating
235 * the list, and to always hold a refcount when unlocked.
236 * Any code which breaks out of this loop while own
237 * a reference to the current mddev and must mddev_put it.
238 */
NeilBrownfd01b882011-10-11 16:47:53 +1100239#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000241 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100242 _tmp = all_mddevs.next; \
243 _mddev = NULL;}); \
244 ({ if (_tmp != &all_mddevs) \
245 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100247 if (_mddev) mddev_put(_mddev); \
248 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
249 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100251 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 )
253
NeilBrown409c57f2009-03-31 14:39:39 +1100254/* Rather than calling directly into the personality make_request function,
255 * IO requests come here first so that we can check if the device is
256 * being suspended pending a reconfiguration.
257 * We hold a refcount over the call to ->make_request. By the time that
258 * call has finished, the bio has been linked into some internal structure
259 * and so is visible to ->quiesce(), so we don't need the refcount any more.
260 */
Jens Axboedece1632015-11-05 10:41:16 -0700261static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
NeilBrown49077322010-03-25 16:20:56 +1100263 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100264 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500265 unsigned int sectors;
Gu Zheng74672d02015-04-03 08:44:47 +0800266 int cpu;
NeilBrown49077322010-03-25 16:20:56 +1100267
Kent Overstreet54efd502015-04-23 22:37:18 -0700268 blk_queue_split(q, &bio, q->bio_split);
269
NeilBrown274d8cb2016-01-04 16:16:58 +1100270 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100271 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700272 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100273 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100274 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200275 if (bio_sectors(bio) != 0)
276 bio->bi_error = -EROFS;
277 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700278 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100279 }
NeilBrown0ca69882011-01-14 09:14:33 +1100280 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100281 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200282 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100283 DEFINE_WAIT(__wait);
284 for (;;) {
285 prepare_to_wait(&mddev->sb_wait, &__wait,
286 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200287 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100288 break;
289 rcu_read_unlock();
290 schedule();
291 rcu_read_lock();
292 }
293 finish_wait(&mddev->sb_wait, &__wait);
294 }
295 atomic_inc(&mddev->active_io);
296 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100297
Chris Masone91ece52011-02-07 19:21:48 -0500298 /*
299 * save the sectors now since our bio can
300 * go away inside make_request
301 */
302 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700303 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600304 bio->bi_opf &= ~REQ_NOMERGE;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200305 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100306
Gu Zheng74672d02015-04-03 08:44:47 +0800307 cpu = part_stat_lock();
308 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
309 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
310 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100311
NeilBrown409c57f2009-03-31 14:39:39 +1100312 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
313 wake_up(&mddev->sb_wait);
Jens Axboedece1632015-11-05 10:41:16 -0700314
315 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
NeilBrown9e35b992010-04-06 14:23:02 +1000318/* mddev_suspend makes sure no new requests are submitted
319 * to the device, and that any requests that have been submitted
320 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100321 * Once mddev_detach() is called and completes, the module will be
322 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000323 */
NeilBrownfd01b882011-10-11 16:47:53 +1100324void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100325{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200326 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100327 if (mddev->suspended++)
328 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100329 synchronize_rcu();
330 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
331 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500332
333 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100334}
NeilBrown390ee602010-06-01 19:37:27 +1000335EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100336
NeilBrownfd01b882011-10-11 16:47:53 +1100337void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100338{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100339 if (--mddev->suspended)
340 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100341 wake_up(&mddev->sb_wait);
342 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500343
Jonathan Brassow47525e52012-05-22 13:55:29 +1000344 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500345 md_wakeup_thread(mddev->thread);
346 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100347}
NeilBrown390ee602010-06-01 19:37:27 +1000348EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100349
NeilBrownfd01b882011-10-11 16:47:53 +1100350int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000351{
NeilBrown5c675f82014-12-15 12:56:56 +1100352 struct md_personality *pers = mddev->pers;
353 int ret = 0;
354
355 rcu_read_lock();
356 if (mddev->suspended)
357 ret = 1;
358 else if (pers && pers->congested)
359 ret = pers->congested(mddev, bits);
360 rcu_read_unlock();
361 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000362}
NeilBrown5c675f82014-12-15 12:56:56 +1100363EXPORT_SYMBOL_GPL(mddev_congested);
364static int md_congested(void *data, int bits)
365{
366 struct mddev *mddev = data;
367 return mddev_congested(mddev, bits);
368}
NeilBrown3fa841d2009-09-23 18:10:29 +1000369
NeilBrowna2826aa2009-12-14 12:49:49 +1100370/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200371 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100372 */
373
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200374static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100375{
NeilBrown3cb03002011-10-11 16:45:26 +1100376 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100377 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100378
379 rdev_dec_pending(rdev, mddev);
380
381 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200382 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200383 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100384 }
385 bio_put(bio);
386}
387
NeilBrowna7a07e62010-12-09 16:04:25 +1100388static void md_submit_flush_data(struct work_struct *ws);
389
NeilBrowna035fc32010-12-09 16:17:51 +1100390static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100391{
NeilBrownfd01b882011-10-11 16:47:53 +1100392 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100393 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100394
NeilBrowna7a07e62010-12-09 16:04:25 +1100395 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
396 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100397 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100398 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100399 if (rdev->raid_disk >= 0 &&
400 !test_bit(Faulty, &rdev->flags)) {
401 /* Take two references, one is dropped
402 * when request finishes, one after
403 * we reclaim rcu_read_lock
404 */
405 struct bio *bi;
406 atomic_inc(&rdev->nr_pending);
407 atomic_inc(&rdev->nr_pending);
408 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000409 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200410 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100411 bi->bi_private = rdev;
412 bi->bi_bdev = rdev->bdev;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600413 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
NeilBrowna2826aa2009-12-14 12:49:49 +1100414 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500415 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100416 rcu_read_lock();
417 rdev_dec_pending(rdev, mddev);
418 }
419 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100420 if (atomic_dec_and_test(&mddev->flush_pending))
421 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100422}
423
Tejun Heoe9c74692010-09-03 11:56:18 +0200424static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100425{
NeilBrownfd01b882011-10-11 16:47:53 +1100426 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200427 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100428
Kent Overstreet4f024f32013-10-11 15:44:27 -0700429 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100430 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200431 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100432 else {
Jens Axboe1eff9d32016-08-05 15:35:16 -0600433 bio->bi_opf &= ~REQ_PREFLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200434 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100435 }
NeilBrown2b74e122010-12-09 15:59:01 +1100436
437 mddev->flush_bio = NULL;
438 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100439}
440
NeilBrownfd01b882011-10-11 16:47:53 +1100441void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100442{
NeilBrown85572d72014-12-15 12:56:56 +1100443 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100444 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200445 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100446 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200447 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100448 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100449
NeilBrowna035fc32010-12-09 16:17:51 +1100450 INIT_WORK(&mddev->flush_work, submit_flushes);
451 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100452}
Tejun Heoe9c74692010-09-03 11:56:18 +0200453EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100454
NeilBrownfd01b882011-10-11 16:47:53 +1100455static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 atomic_inc(&mddev->active);
458 return mddev;
459}
460
Dan Williams5fd3a172009-03-04 00:57:25 -0700461static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100462
NeilBrownfd01b882011-10-11 16:47:53 +1100463static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
NeilBrowna167f662010-10-26 18:31:13 +1100465 struct bio_set *bs = NULL;
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
468 return;
NeilBrownd3374822009-01-09 08:31:10 +1100469 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100470 mddev->ctime == 0 && !mddev->hold_active) {
471 /* Array is not configured at all, and not held active,
472 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100473 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100474 bs = mddev->bio_set;
475 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100476 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200477 /* We did a probe so need to clean up. Call
478 * queue_work inside the spinlock so that
479 * flush_workqueue() after mddev_find will
480 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100481 */
482 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200483 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100484 } else
485 kfree(mddev);
486 }
487 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100488 if (bs)
489 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Sasha Levin25b2edf2015-07-24 18:19:58 -0400492static void md_safemode_timeout(unsigned long data);
493
NeilBrownfd01b882011-10-11 16:47:53 +1100494void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100495{
496 mutex_init(&mddev->open_mutex);
497 mutex_init(&mddev->reconfig_mutex);
498 mutex_init(&mddev->bitmap_info.mutex);
499 INIT_LIST_HEAD(&mddev->disks);
500 INIT_LIST_HEAD(&mddev->all_mddevs);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400501 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
502 (unsigned long) mddev);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100503 atomic_set(&mddev->active, 1);
504 atomic_set(&mddev->openers, 0);
505 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100506 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100507 atomic_set(&mddev->flush_pending, 0);
508 init_waitqueue_head(&mddev->sb_wait);
509 init_waitqueue_head(&mddev->recovery_wait);
510 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000511 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500512 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100513 mddev->resync_min = 0;
514 mddev->resync_max = MaxSector;
515 mddev->level = LEVEL_NONE;
516}
NeilBrown390ee602010-06-01 19:37:27 +1000517EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100518
NeilBrownf72ffdd2014-09-30 14:23:59 +1000519static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
NeilBrownfd01b882011-10-11 16:47:53 +1100521 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
NeilBrown8f5f02c2011-02-16 13:58:51 +1100523 if (unit && MAJOR(unit) != MD_MAJOR)
524 unit &= ~((1<<MdpMinorShift)-1);
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 retry:
527 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
NeilBrownefeb53c2009-01-09 08:31:10 +1100529 if (unit) {
530 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
531 if (mddev->unit == unit) {
532 mddev_get(mddev);
533 spin_unlock(&all_mddevs_lock);
534 kfree(new);
535 return mddev;
536 }
537
538 if (new) {
539 list_add(&new->all_mddevs, &all_mddevs);
540 spin_unlock(&all_mddevs_lock);
541 new->hold_active = UNTIL_IOCTL;
542 return new;
543 }
544 } else if (new) {
545 /* find an unused unit number */
546 static int next_minor = 512;
547 int start = next_minor;
548 int is_free = 0;
549 int dev = 0;
550 while (!is_free) {
551 dev = MKDEV(MD_MAJOR, next_minor);
552 next_minor++;
553 if (next_minor > MINORMASK)
554 next_minor = 0;
555 if (next_minor == start) {
556 /* Oh dear, all in use. */
557 spin_unlock(&all_mddevs_lock);
558 kfree(new);
559 return NULL;
560 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000561
NeilBrownefeb53c2009-01-09 08:31:10 +1100562 is_free = 1;
563 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
564 if (mddev->unit == dev) {
565 is_free = 0;
566 break;
567 }
568 }
569 new->unit = dev;
570 new->md_minor = MINOR(dev);
571 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 list_add(&new->all_mddevs, &all_mddevs);
573 spin_unlock(&all_mddevs_lock);
574 return new;
575 }
576 spin_unlock(&all_mddevs_lock);
577
NeilBrown9ffae0c2006-01-06 00:20:32 -0800578 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (!new)
580 return NULL;
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 new->unit = unit;
583 if (MAJOR(unit) == MD_MAJOR)
584 new->md_minor = MINOR(unit);
585 else
586 new->md_minor = MINOR(unit) >> MdpMinorShift;
587
NeilBrownfafd7fb2010-04-01 15:55:30 +1100588 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 goto retry;
591}
592
NeilBrownb6eb1272010-04-15 10:13:47 +1000593static struct attribute_group md_redundancy_group;
594
NeilBrown5c47daf2014-12-15 12:57:01 +1100595void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
NeilBrowna64c8762010-04-14 17:15:37 +1000597 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000598 /* These cannot be removed under reconfig_mutex as
599 * an access to the files will try to take reconfig_mutex
600 * while holding the file unremovable, which leads to
601 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000602 * So hold set sysfs_active while the remove in happeing,
603 * and anything else which might set ->to_remove or my
604 * otherwise change the sysfs namespace will fail with
605 * -EBUSY if sysfs_active is still set.
606 * We set sysfs_active under reconfig_mutex and elsewhere
607 * test it under the same mutex to ensure its correct value
608 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000609 */
NeilBrowna64c8762010-04-14 17:15:37 +1000610 struct attribute_group *to_remove = mddev->to_remove;
611 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000612 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000613 mutex_unlock(&mddev->reconfig_mutex);
614
NeilBrown00bcb4a2010-06-01 19:37:23 +1000615 if (mddev->kobj.sd) {
616 if (to_remove != &md_redundancy_group)
617 sysfs_remove_group(&mddev->kobj, to_remove);
618 if (mddev->pers == NULL ||
619 mddev->pers->sync_request == NULL) {
620 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
621 if (mddev->sysfs_action)
622 sysfs_put(mddev->sysfs_action);
623 mddev->sysfs_action = NULL;
624 }
NeilBrowna64c8762010-04-14 17:15:37 +1000625 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000626 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000627 } else
628 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Chris Dunlop751e67c2011-10-19 16:48:26 +1100630 /* As we've dropped the mutex we need a spinlock to
631 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000632 */
633 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700634 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000635 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
NeilBrown5c47daf2014-12-15 12:57:01 +1100637EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500639struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100640{
641 struct md_rdev *rdev;
642
643 rdev_for_each_rcu(rdev, mddev)
644 if (rdev->desc_nr == nr)
645 return rdev;
646
647 return NULL;
648}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500649EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100650
651static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
NeilBrown3cb03002011-10-11 16:45:26 +1100653 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
NeilBrowndafb20f2012-03-19 12:46:39 +1100655 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (rdev->bdev->bd_dev == dev)
657 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return NULL;
660}
661
NeilBrown1ca69c42012-10-11 13:37:33 +1100662static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
663{
664 struct md_rdev *rdev;
665
666 rdev_for_each_rcu(rdev, mddev)
667 if (rdev->bdev->bd_dev == dev)
668 return rdev;
669
670 return NULL;
671}
672
NeilBrown84fc4b52011-10-11 16:49:58 +1100673static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800674{
NeilBrown84fc4b52011-10-11 16:49:58 +1100675 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800676 list_for_each_entry(pers, &pers_list, list) {
677 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800678 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800679 if (strcmp(pers->name, clevel)==0)
680 return pers;
681 }
NeilBrown2604b702006-01-06 00:20:36 -0800682 return NULL;
683}
684
Andre Nollb73df2d2008-07-11 22:02:23 +1000685/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100686static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100688 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000689 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
NeilBrownf72ffdd2014-09-30 14:23:59 +1000692static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100695 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000696 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return 0;
698}
699
NeilBrown545c8792012-05-22 13:54:30 +1000700void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800703 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 rdev->sb_loaded = 0;
705 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000706 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100707 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
NeilBrown2699b672011-07-28 11:31:47 +1000709 if (rdev->bb_page) {
710 put_page(rdev->bb_page);
711 rdev->bb_page = NULL;
712 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800713 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
NeilBrown545c8792012-05-22 13:54:30 +1000715EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200717static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700718{
NeilBrown3cb03002011-10-11 16:45:26 +1100719 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100720 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700721
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200722 if (bio->bi_error) {
NeilBrown9d487392016-11-02 14:16:49 +1100723 pr_err("md: super_written gets error=%d\n", bio->bi_error);
NeilBrowna9701a32005-11-08 21:39:34 -0800724 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100725 if (!test_bit(Faulty, &rdev->flags)
726 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800727 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100728 set_bit(LastDev, &rdev->flags);
729 }
730 } else
731 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700732
NeilBrowna9701a32005-11-08 21:39:34 -0800733 if (atomic_dec_and_test(&mddev->pending_writes))
734 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700735 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700736 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700737}
738
NeilBrownfd01b882011-10-11 16:47:53 +1100739void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700740 sector_t sector, int size, struct page *page)
741{
742 /* write first size bytes of page to sector of rdev
743 * Increment mddev->pending_writes before returning
744 * and decrement it on completion, waking up sb_wait
745 * if zero is reached.
746 * If an error occurred, call md_error
747 */
NeilBrown46533ff2016-11-18 16:16:11 +1100748 struct bio *bio;
749 int ff = 0;
750
751 if (test_bit(Faulty, &rdev->flags))
752 return;
753
754 bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700755
Shaohua Lied3b98c2016-03-29 14:00:19 -0700756 atomic_inc(&rdev->nr_pending);
757
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100758 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700759 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700760 bio_add_page(bio, page, size, 0);
761 bio->bi_private = rdev;
762 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100763
764 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
765 test_bit(FailFast, &rdev->flags) &&
766 !test_bit(LastDev, &rdev->flags))
767 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +0200768 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -0800769
NeilBrown7bfa19f2005-06-21 17:17:28 -0700770 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500771 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800772}
773
NeilBrown46533ff2016-11-18 16:16:11 +1100774int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800775{
Tejun Heoe9c74692010-09-03 11:56:18 +0200776 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000777 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -0800778 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +1100779 return -EAGAIN;
780 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700781}
782
NeilBrown3cb03002011-10-11 16:45:26 +1100783int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -0500784 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
NeilBrowna167f662010-10-26 18:31:13 +1100786 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 int ret;
788
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100789 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
790 rdev->meta_bdev : rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -0500791 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100792 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700793 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000794 else if (rdev->mddev->reshape_position != MaxSector &&
795 (rdev->mddev->reshape_backwards ==
796 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700797 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100798 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700799 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500801
802 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200804 ret = !bio->bi_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 bio_put(bio);
806 return ret;
807}
NeilBrowna8745db2006-01-06 00:20:34 -0800808EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
NeilBrownf72ffdd2014-09-30 14:23:59 +1000810static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (rdev->sb_loaded)
815 return 0;
816
Mike Christie796a5cf2016-06-05 14:32:07 -0500817 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 goto fail;
819 rdev->sb_loaded = 1;
820 return 0;
821
822fail:
NeilBrown9d487392016-11-02 14:16:49 +1100823 pr_err("md: disabled device %s, could not read superblock.\n",
824 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return -EINVAL;
826}
827
828static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
829{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000830 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000831 sb1->set_uuid1 == sb2->set_uuid1 &&
832 sb1->set_uuid2 == sb2->set_uuid2 &&
833 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
837{
838 int ret;
839 mdp_super_t *tmp1, *tmp2;
840
841 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
842 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
843
844 if (!tmp1 || !tmp2) {
845 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 goto abort;
847 }
848
849 *tmp1 = *sb1;
850 *tmp2 = *sb2;
851
852 /*
853 * nr_disks is not constant
854 */
855 tmp1->nr_disks = 0;
856 tmp2->nr_disks = 0;
857
Andre Nollce0c8e02008-07-11 22:02:20 +1000858 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700860 kfree(tmp1);
861 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return ret;
863}
864
NeilBrown4d167f02007-05-09 02:35:37 -0700865static u32 md_csum_fold(u32 csum)
866{
867 csum = (csum & 0xffff) + (csum >> 16);
868 return (csum & 0xffff) + (csum >> 16);
869}
870
NeilBrownf72ffdd2014-09-30 14:23:59 +1000871static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
NeilBrown4d167f02007-05-09 02:35:37 -0700873 u64 newcsum = 0;
874 u32 *sb32 = (u32*)sb;
875 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 unsigned int disk_csum, csum;
877
878 disk_csum = sb->sb_csum;
879 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700880
881 for (i = 0; i < MD_SB_BYTES/4 ; i++)
882 newcsum += sb32[i];
883 csum = (newcsum & 0xffffffff) + (newcsum>>32);
884
NeilBrown4d167f02007-05-09 02:35:37 -0700885#ifdef CONFIG_ALPHA
886 /* This used to use csum_partial, which was wrong for several
887 * reasons including that different results are returned on
888 * different architectures. It isn't critical that we get exactly
889 * the same return value as before (we always csum_fold before
890 * testing, and that removes any differences). However as we
891 * know that csum_partial always returned a 16bit value on
892 * alphas, do a fold to maximise conformity to previous behaviour.
893 */
894 sb->sb_csum = md_csum_fold(disk_csum);
895#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700897#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return csum;
899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901/*
902 * Handle superblock details.
903 * We want to be able to handle multiple superblock formats
904 * so we have a common interface to them all, and an array of
905 * different handlers.
906 * We rely on user-space to write the initial superblock, and support
907 * reading and updating of superblocks.
908 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100909 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * loads and validates a superblock on dev.
911 * if refdev != NULL, compare superblocks on both devices
912 * Return:
913 * 0 - dev has a superblock that is compatible with refdev
914 * 1 - dev has a superblock that is compatible and newer than refdev
915 * so dev should be used as the refdev in future
916 * -EINVAL superblock incompatible or invalid
917 * -othererror e.g. -EIO
918 *
NeilBrownfd01b882011-10-11 16:47:53 +1100919 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 * Verify that dev is acceptable into mddev.
921 * The first time, mddev->raid_disks will be 0, and data from
922 * dev should be merged in. Subsequent calls check that dev
923 * is new enough. Return 0 or -EINVAL
924 *
NeilBrownfd01b882011-10-11 16:47:53 +1100925 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 * Update the superblock for rdev with data in mddev
927 * This does not write to disc.
928 *
929 */
930
931struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000932 char *name;
933 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000934 int (*load_super)(struct md_rdev *rdev,
935 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000936 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000937 int (*validate_super)(struct mddev *mddev,
938 struct md_rdev *rdev);
939 void (*sync_super)(struct mddev *mddev,
940 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100941 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000942 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000943 int (*allow_new_offset)(struct md_rdev *rdev,
944 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945};
946
947/*
Andre Noll0894cc32009-06-18 08:49:23 +1000948 * Check that the given mddev has no bitmap.
949 *
950 * This function is called from the run method of all personalities that do not
951 * support bitmaps. It prints an error message and returns non-zero if mddev
952 * has a bitmap. Otherwise, it returns 0.
953 *
954 */
NeilBrownfd01b882011-10-11 16:47:53 +1100955int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000956{
NeilBrownc3d97142009-12-14 12:49:52 +1100957 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000958 return 0;
NeilBrown9d487392016-11-02 14:16:49 +1100959 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +1000960 mdname(mddev), mddev->pers->name);
961 return 1;
962}
963EXPORT_SYMBOL(md_check_no_bitmap);
964
965/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000966 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
NeilBrown3cb03002011-10-11 16:45:26 +1100968static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
971 mdp_super_t *sb;
972 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 /*
Andre Noll0f420352008-07-11 22:02:23 +1000975 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 * it's at the end of the disk.
977 *
978 * It also happens to be a multiple of 4Kb.
979 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100980 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
NeilBrown0002b272005-09-09 16:23:53 -0700982 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +1100983 if (ret)
984 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 ret = -EINVAL;
987
988 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000989 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +1100992 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 goto abort;
994 }
995
996 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800997 sb->minor_version < 90 ||
998 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +1100999 pr_warn("Bad version number %d.%d on %s\n",
1000 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 goto abort;
1002 }
1003
1004 if (sb->raid_disks <= 0)
1005 goto abort;
1006
NeilBrown4d167f02007-05-09 02:35:37 -07001007 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001008 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 goto abort;
1010 }
1011
1012 rdev->preferred_minor = sb->md_minor;
1013 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001014 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001015 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001016 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 if (sb->level == LEVEL_MULTIPATH)
1019 rdev->desc_nr = -1;
1020 else
1021 rdev->desc_nr = sb->this_disk.number;
1022
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001023 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001025 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001027 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (!uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001029 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 b, bdevname(refdev->bdev,b2));
1031 goto abort;
1032 }
1033 if (!sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001034 pr_warn("md: %s has same UUID but different superblock to %s\n",
1035 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 goto abort;
1037 }
1038 ev1 = md_event(sb);
1039 ev2 = md_event(refsb);
1040 if (ev1 > ev2)
1041 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001042 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 ret = 0;
1044 }
NeilBrown8190e752009-06-18 08:48:58 +10001045 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001046 /* Limit to 4TB as metadata cannot record more than that.
1047 * (not needed for Linear and RAID0 as metadata doesn't
1048 * record this size)
1049 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001050 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1051 sb->level >= 1)
1052 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
NeilBrown27a7b262011-09-10 17:21:28 +10001054 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001055 /* "this cannot possibly happen" ... */
1056 ret = -EINVAL;
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 abort:
1059 return ret;
1060}
1061
1062/*
1063 * validate_super for 0.90.0
1064 */
NeilBrownfd01b882011-10-11 16:47:53 +11001065static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001068 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001069 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
NeilBrown41158c72005-06-21 17:17:25 -07001071 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001072 clear_bit(Faulty, &rdev->flags);
1073 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001074 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001075 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (mddev->raid_disks == 0) {
1078 mddev->major_version = 0;
1079 mddev->minor_version = sb->minor_version;
1080 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001081 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001082 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 mddev->ctime = sb->ctime;
1084 mddev->utime = sb->utime;
1085 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001086 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 mddev->layout = sb->layout;
1088 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001089 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001090 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001091 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001092 mddev->bitmap_info.space = 0;
1093 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001094 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001095 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001096 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
NeilBrownf6705572006-03-27 01:18:11 -08001098 if (mddev->minor_version >= 91) {
1099 mddev->reshape_position = sb->reshape_position;
1100 mddev->delta_disks = sb->delta_disks;
1101 mddev->new_level = sb->new_level;
1102 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001103 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001104 if (mddev->delta_disks < 0)
1105 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001106 } else {
1107 mddev->reshape_position = MaxSector;
1108 mddev->delta_disks = 0;
1109 mddev->new_level = mddev->level;
1110 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001111 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001112 }
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (sb->state & (1<<MD_SB_CLEAN))
1115 mddev->recovery_cp = MaxSector;
1116 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001117 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 sb->events_lo == sb->cp_events_lo) {
1119 mddev->recovery_cp = sb->recovery_cp;
1120 } else
1121 mddev->recovery_cp = 0;
1122 }
1123
1124 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1125 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1126 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1127 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1128
1129 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001130
1131 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001132 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001133 mddev->bitmap_info.offset =
1134 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001135 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001136 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001137 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001138
NeilBrown41158c72005-06-21 17:17:25 -07001139 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001140 /* Insist on good event counter while assembling, except
1141 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001143 if (sb->disks[rdev->desc_nr].state & (
1144 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001145 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001146 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001147 } else if (mddev->bitmap) {
1148 /* if adding to array with a bitmap, then we can accept an
1149 * older device ... but not too old.
1150 */
NeilBrown41158c72005-06-21 17:17:25 -07001151 if (ev1 < mddev->bitmap->events_cleared)
1152 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001153 if (ev1 < mddev->events)
1154 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001155 } else {
1156 if (ev1 < mddev->events)
1157 /* just a hot-add of a new device, leave raid_disk at -1 */
1158 return 0;
1159 }
NeilBrown41158c72005-06-21 17:17:25 -07001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 desc = sb->disks + rdev->desc_nr;
1163
1164 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001165 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001166 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1167 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001168 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001170 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001171 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1172 /* active but not in sync implies recovery up to
1173 * reshape position. We don't know exactly where
1174 * that is, so set to zero for now */
1175 if (mddev->minor_version >= 91) {
1176 rdev->recovery_offset = 0;
1177 rdev->raid_disk = desc->raid_disk;
1178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001180 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1181 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001182 if (desc->state & (1<<MD_DISK_FAILFAST))
1183 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001184 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001185 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return 0;
1187}
1188
1189/*
1190 * sync_super for 0.90.0
1191 */
NeilBrownfd01b882011-10-11 16:47:53 +11001192static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001195 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /* make rdev->sb match mddev data..
1199 *
1200 * 1/ zero out disks
1201 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1202 * 3/ any empty disks < next_spare become removed
1203 *
1204 * disks[0] gets initialised to REMOVED because
1205 * we cannot be sure from other fields if it has
1206 * been initialised or not.
1207 */
1208 int i;
1209 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1210
NeilBrown61181562005-09-09 16:24:02 -07001211 rdev->sb_size = MD_SB_BYTES;
1212
Namhyung Kim65a06f062011-07-27 11:00:36 +10001213 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 memset(sb, 0, sizeof(*sb));
1216
1217 sb->md_magic = MD_SB_MAGIC;
1218 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 sb->patch_version = mddev->patch_version;
1220 sb->gvalid_words = 0; /* ignored */
1221 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1222 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1223 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1224 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1225
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001226 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001228 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 sb->raid_disks = mddev->raid_disks;
1230 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001231 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001232 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 sb->state = 0;
1234 sb->events_hi = (mddev->events>>32);
1235 sb->events_lo = (u32)mddev->events;
1236
NeilBrownf6705572006-03-27 01:18:11 -08001237 if (mddev->reshape_position == MaxSector)
1238 sb->minor_version = 90;
1239 else {
1240 sb->minor_version = 91;
1241 sb->reshape_position = mddev->reshape_position;
1242 sb->new_level = mddev->new_level;
1243 sb->delta_disks = mddev->delta_disks;
1244 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001245 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001246 }
1247 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (mddev->in_sync)
1249 {
1250 sb->recovery_cp = mddev->recovery_cp;
1251 sb->cp_events_hi = (mddev->events>>32);
1252 sb->cp_events_lo = (u32)mddev->events;
1253 if (mddev->recovery_cp == MaxSector)
1254 sb->state = (1<< MD_SB_CLEAN);
1255 } else
1256 sb->recovery_cp = 0;
1257
1258 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001259 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
NeilBrownc3d97142009-12-14 12:49:52 +11001261 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001262 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001265 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001267 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001268 int is_active = test_bit(In_sync, &rdev2->flags);
1269
1270 if (rdev2->raid_disk >= 0 &&
1271 sb->minor_version >= 91)
1272 /* we have nowhere to store the recovery_offset,
1273 * but if it is not below the reshape_position,
1274 * we can piggy-back on that.
1275 */
1276 is_active = 1;
1277 if (rdev2->raid_disk < 0 ||
1278 test_bit(Faulty, &rdev2->flags))
1279 is_active = 0;
1280 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001281 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001283 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001284 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 d = &sb->disks[rdev2->desc_nr];
1286 nr_disks++;
1287 d->number = rdev2->desc_nr;
1288 d->major = MAJOR(rdev2->bdev->bd_dev);
1289 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001290 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 d->raid_disk = rdev2->raid_disk;
1292 else
1293 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001294 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001296 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001298 if (test_bit(In_sync, &rdev2->flags))
1299 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 active++;
1301 working++;
1302 } else {
1303 d->state = 0;
1304 spare++;
1305 working++;
1306 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001307 if (test_bit(WriteMostly, &rdev2->flags))
1308 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001309 if (test_bit(FailFast, &rdev2->flags))
1310 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 /* now set the "removed" and "faulty" bits on any missing devices */
1313 for (i=0 ; i < mddev->raid_disks ; i++) {
1314 mdp_disk_t *d = &sb->disks[i];
1315 if (d->state == 0 && d->number == 0) {
1316 d->number = i;
1317 d->raid_disk = i;
1318 d->state = (1<<MD_DISK_REMOVED);
1319 d->state |= (1<<MD_DISK_FAULTY);
1320 failed++;
1321 }
1322 }
1323 sb->nr_disks = nr_disks;
1324 sb->active_disks = active;
1325 sb->working_disks = working;
1326 sb->failed_disks = failed;
1327 sb->spare_disks = spare;
1328
1329 sb->this_disk = sb->disks[rdev->desc_nr];
1330 sb->sb_csum = calc_sb_csum(sb);
1331}
1332
1333/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001334 * rdev_size_change for 0.90.0
1335 */
1336static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001337super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001338{
Andre Noll58c0fed2009-03-31 14:33:13 +11001339 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001340 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001341 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001342 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001343 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001344 if (!num_sectors || num_sectors > rdev->sb_start)
1345 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001346 /* Limit to 4TB as metadata cannot record more than that.
1347 * 4TB == 2^32 KB, or 2*2^32 sectors.
1348 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001349 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1350 rdev->mddev->level >= 1)
1351 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001352 do {
1353 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001354 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001355 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001356 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001357}
1358
NeilBrownc6563a82012-05-21 09:27:00 +10001359static int
1360super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1361{
1362 /* non-zero offset changes not possible with v0.90 */
1363 return new_offset == 0;
1364}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001365
1366/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 * version 1 superblock
1368 */
1369
NeilBrownf72ffdd2014-09-30 14:23:59 +10001370static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001372 __le32 disk_csum;
1373 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 unsigned long long newcsum;
1375 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001376 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 disk_csum = sb->sb_csum;
1379 sb->sb_csum = 0;
1380 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001381 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 newcsum += le32_to_cpu(*isuper++);
1383
1384 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001385 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1388 sb->sb_csum = disk_csum;
1389 return cpu_to_le32(csum);
1390}
1391
NeilBrown3cb03002011-10-11 16:45:26 +11001392static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
1394 struct mdp_superblock_1 *sb;
1395 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001396 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001397 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001399 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 /*
Andre Noll0f420352008-07-11 22:02:23 +10001402 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 * It is always aligned to a 4K boundary and
1404 * depeding on minor_version, it can be:
1405 * 0: At least 8K, but less than 12K, from end of device
1406 * 1: At start of device
1407 * 2: 4K from start of device.
1408 */
1409 switch(minor_version) {
1410 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001411 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001412 sb_start -= 8*2;
1413 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 break;
1415 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001416 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 break;
1418 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001419 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 break;
1421 default:
1422 return -EINVAL;
1423 }
Andre Noll0f420352008-07-11 22:02:23 +10001424 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
NeilBrown0002b272005-09-09 16:23:53 -07001426 /* superblock is rarely larger than 1K, but it can be larger,
1427 * and it is safe to read 4k, so we do that
1428 */
1429 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (ret) return ret;
1431
Namhyung Kim65a06f062011-07-27 11:00:36 +10001432 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1435 sb->major_version != cpu_to_le32(1) ||
1436 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001437 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001438 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return -EINVAL;
1440
1441 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001442 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 bdevname(rdev->bdev,b));
1444 return -EINVAL;
1445 }
1446 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001447 pr_warn("md: data_size too small on %s\n",
1448 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return -EINVAL;
1450 }
NeilBrownc6563a82012-05-21 09:27:00 +10001451 if (sb->pad0 ||
1452 sb->pad3[0] ||
1453 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1454 /* Some padding is non-zero, might be a new feature */
1455 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 rdev->preferred_minor = 0xffff;
1458 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001459 rdev->new_data_offset = rdev->data_offset;
1460 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1461 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1462 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001463 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
NeilBrown0002b272005-09-09 16:23:53 -07001465 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001466 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001467 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001468 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1469
1470 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001471 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001472 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001473 if (minor_version
1474 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1475 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001476
NeilBrown31b65a02006-07-10 04:44:14 -07001477 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1478 rdev->desc_nr = -1;
1479 else
1480 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1481
NeilBrown2699b672011-07-28 11:31:47 +10001482 if (!rdev->bb_page) {
1483 rdev->bb_page = alloc_page(GFP_KERNEL);
1484 if (!rdev->bb_page)
1485 return -ENOMEM;
1486 }
1487 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1488 rdev->badblocks.count == 0) {
1489 /* need to load the bad block list.
1490 * Currently we limit it to one page.
1491 */
1492 s32 offset;
1493 sector_t bb_sector;
1494 u64 *bbp;
1495 int i;
1496 int sectors = le16_to_cpu(sb->bblog_size);
1497 if (sectors > (PAGE_SIZE / 512))
1498 return -EINVAL;
1499 offset = le32_to_cpu(sb->bblog_offset);
1500 if (offset == 0)
1501 return -EINVAL;
1502 bb_sector = (long long)offset;
1503 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001504 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001505 return -EIO;
1506 bbp = (u64 *)page_address(rdev->bb_page);
1507 rdev->badblocks.shift = sb->bblog_shift;
1508 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1509 u64 bb = le64_to_cpu(*bbp);
1510 int count = bb & (0x3ff);
1511 u64 sector = bb >> 10;
1512 sector <<= sb->bblog_shift;
1513 count <<= sb->bblog_shift;
1514 if (bb + 1 == 0)
1515 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001516 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001517 return -EINVAL;
1518 }
NeilBrown486adf72013-04-24 11:42:44 +10001519 } else if (sb->bblog_offset != 0)
1520 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001521
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001522 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) {
1523 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1524 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1525 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1526 }
1527
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001528 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001529 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001530 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001532 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1535 sb->level != refsb->level ||
1536 sb->layout != refsb->layout ||
1537 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001538 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 bdevname(rdev->bdev,b),
1540 bdevname(refdev->bdev,b2));
1541 return -EINVAL;
1542 }
1543 ev1 = le64_to_cpu(sb->events);
1544 ev2 = le64_to_cpu(refsb->events);
1545
1546 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001547 ret = 1;
1548 else
1549 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
NeilBrownc6563a82012-05-21 09:27:00 +10001551 if (minor_version) {
1552 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1553 sectors -= rdev->data_offset;
1554 } else
1555 sectors = rdev->sb_start;
1556 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001558 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001559 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
NeilBrownfd01b882011-10-11 16:47:53 +11001562static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001564 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001565 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
NeilBrown41158c72005-06-21 17:17:25 -07001567 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001568 clear_bit(Faulty, &rdev->flags);
1569 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001570 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001571 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (mddev->raid_disks == 0) {
1574 mddev->major_version = 1;
1575 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001576 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001577 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001578 mddev->ctime = le64_to_cpu(sb->ctime);
1579 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001581 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 mddev->layout = le32_to_cpu(sb->layout);
1583 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001584 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001585 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001586 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001587 mddev->bitmap_info.space = 0;
1588 /* Default location for bitmap is 1K after superblock
1589 * using 3K - total of 4K
1590 */
NeilBrownc3d97142009-12-14 12:49:52 +11001591 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001592 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001593 mddev->reshape_backwards = 0;
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1596 memcpy(mddev->uuid, sb->set_uuid, 16);
1597
1598 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001599
NeilBrown71c08052005-09-09 16:23:51 -07001600 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001601 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001602 mddev->bitmap_info.offset =
1603 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001604 /* Metadata doesn't record how much space is available.
1605 * For 1.0, we assume we can use up to the superblock
1606 * if before, else to 4K beyond superblock.
1607 * For others, assume no change is possible.
1608 */
1609 if (mddev->minor_version > 0)
1610 mddev->bitmap_info.space = 0;
1611 else if (mddev->bitmap_info.offset > 0)
1612 mddev->bitmap_info.space =
1613 8 - mddev->bitmap_info.offset;
1614 else
1615 mddev->bitmap_info.space =
1616 -mddev->bitmap_info.offset;
1617 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001618
NeilBrownf6705572006-03-27 01:18:11 -08001619 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1620 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1621 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1622 mddev->new_level = le32_to_cpu(sb->new_level);
1623 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001624 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001625 if (mddev->delta_disks < 0 ||
1626 (mddev->delta_disks == 0 &&
1627 (le32_to_cpu(sb->feature_map)
1628 & MD_FEATURE_RESHAPE_BACKWARDS)))
1629 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001630 } else {
1631 mddev->reshape_position = MaxSector;
1632 mddev->delta_disks = 0;
1633 mddev->new_level = mddev->level;
1634 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001635 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001636 }
1637
Song Liu486b0f72016-08-19 15:34:01 -07001638 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001639 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001640
1641 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) {
1642 if (le32_to_cpu(sb->feature_map) &
1643 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1644 return -EINVAL;
1645 set_bit(MD_HAS_PPL, &mddev->flags);
1646 }
NeilBrown41158c72005-06-21 17:17:25 -07001647 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001648 /* Insist of good event counter while assembling, except for
1649 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001651 if (rdev->desc_nr >= 0 &&
1652 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001653 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1654 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001655 if (ev1 < mddev->events)
1656 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001657 } else if (mddev->bitmap) {
1658 /* If adding to array with a bitmap, then we can accept an
1659 * older device, but not too old.
1660 */
NeilBrown41158c72005-06-21 17:17:25 -07001661 if (ev1 < mddev->bitmap->events_cleared)
1662 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001663 if (ev1 < mddev->events)
1664 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001665 } else {
1666 if (ev1 < mddev->events)
1667 /* just a hot-add of a new device, leave raid_disk at -1 */
1668 return 0;
1669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (mddev->level != LEVEL_MULTIPATH) {
1671 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001672 if (rdev->desc_nr < 0 ||
1673 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001674 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001675 rdev->desc_nr = -1;
1676 } else
1677 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001679 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001681 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001682 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 break;
Song Liubac624f2015-08-13 14:31:55 -07001684 case MD_DISK_ROLE_JOURNAL: /* journal device */
1685 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1686 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001687 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001688 return -EINVAL;
1689 }
1690 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001691 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001692 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001693 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 default:
NeilBrownf4667222013-12-09 12:04:56 +11001695 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001696 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001697 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001698 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001699 if (!(le32_to_cpu(sb->feature_map) &
1700 MD_FEATURE_RECOVERY_BITMAP))
1701 rdev->saved_raid_disk = -1;
1702 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001703 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 rdev->raid_disk = role;
1705 break;
1706 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001707 if (sb->devflags & WriteMostly1)
1708 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001709 if (sb->devflags & FailFast1)
1710 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001711 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1712 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001713 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001714 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return 0;
1717}
1718
NeilBrownfd01b882011-10-11 16:47:53 +11001719static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
1721 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001722 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 int max_dev, i;
1724 /* make rdev->sb match mddev and rdev data. */
1725
Namhyung Kim65a06f062011-07-27 11:00:36 +10001726 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 sb->feature_map = 0;
1729 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001730 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 memset(sb->pad3, 0, sizeof(sb->pad3));
1732
1733 sb->utime = cpu_to_le64((__u64)mddev->utime);
1734 sb->events = cpu_to_le64(mddev->events);
1735 if (mddev->in_sync)
1736 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001737 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1738 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 else
1740 sb->resync_offset = cpu_to_le64(0);
1741
NeilBrown1c05b4b2006-10-21 10:24:08 -07001742 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001743
NeilBrownf0ca3402006-02-02 14:28:04 -08001744 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001745 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001746 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001747 sb->level = cpu_to_le32(mddev->level);
1748 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11001749 if (test_bit(FailFast, &rdev->flags))
1750 sb->devflags |= FailFast1;
1751 else
1752 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08001753
NeilBrownaeb9b212011-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);
NeilBrownaeb9b212011-08-25 14:43:08 +10001760
NeilBrownc3d97142009-12-14 12:49:52 +11001761 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1762 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001763 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001764 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001765
Shaohua Lif2076e72015-10-08 21:54:12 -07001766 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001767 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001768 sb->feature_map |=
1769 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1770 sb->recovery_offset =
1771 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001772 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1773 sb->feature_map |=
1774 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001775 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001776 /* Note: recovery_offset and journal_tail share space */
1777 if (test_bit(Journal, &rdev->flags))
1778 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001779 if (test_bit(Replacement, &rdev->flags))
1780 sb->feature_map |=
1781 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001782
NeilBrownf6705572006-03-27 01:18:11 -08001783 if (mddev->reshape_position != MaxSector) {
1784 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1785 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1786 sb->new_layout = cpu_to_le32(mddev->new_layout);
1787 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1788 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001789 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001790 if (mddev->delta_disks == 0 &&
1791 mddev->reshape_backwards)
1792 sb->feature_map
1793 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001794 if (rdev->new_data_offset != rdev->data_offset) {
1795 sb->feature_map
1796 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1797 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1798 - rdev->data_offset));
1799 }
NeilBrownf6705572006-03-27 01:18:11 -08001800 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001801
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001802 if (mddev_is_clustered(mddev))
1803 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1804
NeilBrown2699b672011-07-28 11:31:47 +10001805 if (rdev->badblocks.count == 0)
1806 /* Nothing to do for bad blocks*/ ;
1807 else if (sb->bblog_offset == 0)
1808 /* Cannot record bad blocks on this device */
1809 md_error(mddev, rdev);
1810 else {
1811 struct badblocks *bb = &rdev->badblocks;
1812 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1813 u64 *p = bb->page;
1814 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1815 if (bb->changed) {
1816 unsigned seq;
1817
1818retry:
1819 seq = read_seqbegin(&bb->lock);
1820
1821 memset(bbp, 0xff, PAGE_SIZE);
1822
1823 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001824 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001825 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1826 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001827 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001828 }
NeilBrownd0962932012-03-19 12:46:41 +11001829 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001830 if (read_seqretry(&bb->lock, seq))
1831 goto retry;
1832
1833 bb->sector = (rdev->sb_start +
1834 (int)le32_to_cpu(sb->bblog_offset));
1835 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001836 }
1837 }
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001840 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (rdev2->desc_nr+1 > max_dev)
1842 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001843
NeilBrown70471da2009-08-03 10:59:57 +10001844 if (max_dev > le32_to_cpu(sb->max_dev)) {
1845 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001846 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001847 rdev->sb_size = max_dev * 2 + 256;
1848 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1849 if (rdev->sb_size & bmask)
1850 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001851 } else
1852 max_dev = le32_to_cpu(sb->max_dev);
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 for (i=0; i<max_dev;i++)
Song Liuc4d4c912015-08-13 14:31:54 -07001855 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001856
Song Liua97b7892015-10-08 21:54:09 -07001857 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1858 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001860 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
1861 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
1862 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
1863 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
1864 }
1865
NeilBrowndafb20f2012-03-19 12:46:39 +11001866 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001868 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001869 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001870 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001872 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001873 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001874 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001875 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 else
Song Liuc4d4c912015-08-13 14:31:54 -07001877 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 sb->sb_csum = calc_sb_1_csum(sb);
1881}
1882
Chris Webb0cd17fe2008-06-28 08:31:46 +10001883static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001884super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001885{
1886 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001887 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001888 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001889 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001890 if (rdev->data_offset != rdev->new_data_offset)
1891 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001892 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001893 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001894 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001895 max_sectors -= rdev->data_offset;
1896 if (!num_sectors || num_sectors > max_sectors)
1897 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001898 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001899 /* minor version 0 with bitmap we can't move */
1900 return 0;
1901 } else {
1902 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001903 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001904 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001905 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001906 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001907 if (!num_sectors || num_sectors > max_sectors)
1908 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001909 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001910 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001911 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001912 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08001913 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10001914 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11001915 do {
1916 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1917 rdev->sb_page);
1918 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001919 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001920
1921}
1922
1923static int
1924super_1_allow_new_offset(struct md_rdev *rdev,
1925 unsigned long long new_offset)
1926{
1927 /* All necessary checks on new >= old have been done */
1928 struct bitmap *bitmap;
1929 if (new_offset >= rdev->data_offset)
1930 return 1;
1931
1932 /* with 1.0 metadata, there is no metadata to tread on
1933 * so we can always move back */
1934 if (rdev->mddev->minor_version == 0)
1935 return 1;
1936
1937 /* otherwise we must be sure not to step on
1938 * any metadata, so stay:
1939 * 36K beyond start of superblock
1940 * beyond end of badblocks
1941 * beyond write-intent bitmap
1942 */
1943 if (rdev->sb_start + (32+4)*2 > new_offset)
1944 return 0;
1945 bitmap = rdev->mddev->bitmap;
1946 if (bitmap && !rdev->mddev->bitmap_info.file &&
1947 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001948 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001949 return 0;
1950 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1951 return 0;
1952
1953 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001954}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Adrian Bunk75c96f82005-05-05 16:16:09 -07001956static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 [0] = {
1958 .name = "0.90.0",
1959 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001960 .load_super = super_90_load,
1961 .validate_super = super_90_validate,
1962 .sync_super = super_90_sync,
1963 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001964 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 },
1966 [1] = {
1967 .name = "md-1",
1968 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001969 .load_super = super_1_load,
1970 .validate_super = super_1_validate,
1971 .sync_super = super_1_sync,
1972 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001973 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 },
1975};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
NeilBrownfd01b882011-10-11 16:47:53 +11001977static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001978{
1979 if (mddev->sync_super) {
1980 mddev->sync_super(mddev, rdev);
1981 return;
1982 }
1983
1984 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1985
1986 super_types[mddev->major_version].sync_super(mddev, rdev);
1987}
1988
NeilBrownfd01b882011-10-11 16:47:53 +11001989static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
NeilBrown3cb03002011-10-11 16:45:26 +11001991 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
NeilBrown4b809912008-07-21 17:05:25 +10001993 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07001994 rdev_for_each_rcu(rdev, mddev1) {
1995 if (test_bit(Faulty, &rdev->flags) ||
1996 test_bit(Journal, &rdev->flags) ||
1997 rdev->raid_disk == -1)
1998 continue;
1999 rdev_for_each_rcu(rdev2, mddev2) {
2000 if (test_bit(Faulty, &rdev2->flags) ||
2001 test_bit(Journal, &rdev2->flags) ||
2002 rdev2->raid_disk == -1)
2003 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002004 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002005 rdev2->bdev->bd_contains) {
2006 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002007 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002008 }
Song Liu0b020e82015-09-03 23:00:35 -07002009 }
2010 }
NeilBrown4b809912008-07-21 17:05:25 +10002011 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return 0;
2013}
2014
2015static LIST_HEAD(pending_raid_disks);
2016
Andre Nollac5e7112009-08-03 10:59:47 +10002017/*
2018 * Try to register data integrity profile for an mddev
2019 *
2020 * This is called when an array is started and after a disk has been kicked
2021 * from the array. It only succeeds if all working and active component devices
2022 * are integrity capable with matching profiles.
2023 */
NeilBrownfd01b882011-10-11 16:47:53 +11002024int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002025{
NeilBrown3cb03002011-10-11 16:45:26 +11002026 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002027
Andre Nollac5e7112009-08-03 10:59:47 +10002028 if (list_empty(&mddev->disks))
2029 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002030 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2031 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002032 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002033 /* skip spares and non-functional disks */
2034 if (test_bit(Faulty, &rdev->flags))
2035 continue;
2036 if (rdev->raid_disk < 0)
2037 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002038 if (!reference) {
2039 /* Use the first rdev as the reference */
2040 reference = rdev;
2041 continue;
2042 }
2043 /* does this rdev's profile match the reference profile? */
2044 if (blk_integrity_compare(reference->bdev->bd_disk,
2045 rdev->bdev->bd_disk) < 0)
2046 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002047 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002048 if (!reference || !bdev_get_integrity(reference->bdev))
2049 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002050 /*
2051 * All component devices are integrity capable and have matching
2052 * profiles, register the common profile for the md device.
2053 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002054 blk_integrity_register(mddev->gendisk,
2055 bdev_get_integrity(reference->bdev));
2056
NeilBrown9d487392016-11-02 14:16:49 +11002057 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Martin K. Petersena91a2782011-03-17 11:11:05 +01002058 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002059 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002060 mdname(mddev));
2061 return -EINVAL;
2062 }
Andre Nollac5e7112009-08-03 10:59:47 +10002063 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002064}
Andre Nollac5e7112009-08-03 10:59:47 +10002065EXPORT_SYMBOL(md_integrity_register);
2066
Dan Williams1501efa2016-01-13 16:00:07 -08002067/*
2068 * Attempt to add an rdev, but only if it is consistent with the current
2069 * integrity profile
2070 */
2071int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002072{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002073 struct blk_integrity *bi_rdev;
2074 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002075 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002076
2077 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002078 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002079
2080 bi_rdev = bdev_get_integrity(rdev->bdev);
2081 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002082
2083 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002084 return 0;
2085
2086 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002087 pr_err("%s: incompatible integrity profile for %s\n",
2088 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002089 return -ENXIO;
2090 }
2091
2092 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002093}
2094EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002095
NeilBrownf72ffdd2014-09-30 14:23:59 +10002096static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002098 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002099 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002100 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Dan Williams11e2ede2008-04-30 00:52:32 -07002102 /* prevent duplicates */
2103 if (find_rdev(mddev, rdev->bdev->bd_dev))
2104 return -EEXIST;
2105
NeilBrown97b20ef2017-04-13 08:53:48 +10002106 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2107 mddev->pers)
2108 return -EROFS;
2109
Andre Nolldd8ac332009-03-31 14:33:13 +11002110 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002111 if (!test_bit(Journal, &rdev->flags) &&
2112 rdev->sectors &&
2113 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002114 if (mddev->pers) {
2115 /* Cannot change size, so fail
2116 * If mddev->level <= 0, then we don't care
2117 * about aligning sizes (e.g. linear)
2118 */
2119 if (mddev->level > 0)
2120 return -ENOSPC;
2121 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002122 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 /* Verify rdev->desc_nr is unique.
2126 * If it is -1, assign a free number, else
2127 * check number is not in use
2128 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002129 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if (rdev->desc_nr < 0) {
2131 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002132 if (mddev->pers)
2133 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002134 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 choice++;
2136 rdev->desc_nr = choice;
2137 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002138 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002139 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002143 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002144 if (!test_bit(Journal, &rdev->flags) &&
2145 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002146 pr_warn("md: %s: array is limited to %d devices\n",
2147 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002148 return -EBUSY;
2149 }
NeilBrown19133a42005-11-08 21:39:35 -08002150 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002151 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002154 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002155
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002156 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002157 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002158
Tejun Heo0762b8b2008-08-25 19:56:12 +09002159 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002160 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2161 /* failure here is OK */;
2162 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002163
NeilBrown4b809912008-07-21 17:05:25 +10002164 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002165 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002166
2167 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002168 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002171
2172 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002173 pr_warn("md: failed to register dev-%s for %s\n",
2174 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002175 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
NeilBrown177a99b2008-02-06 01:39:56 -08002178static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002179{
NeilBrown3cb03002011-10-11 16:45:26 +11002180 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002181 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002182 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002183}
2184
NeilBrownf72ffdd2014-09-30 14:23:59 +10002185static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
2187 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002188
Tejun Heo49731ba2011-01-14 18:43:57 +01002189 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002190 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002191 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002193 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002194 sysfs_put(rdev->sysfs_state);
2195 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002196 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002197 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002198 * writing to 'remove' to "dev/state". We also need
2199 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002200 */
NeilBrown4b809912008-07-21 17:05:25 +10002201 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002202 INIT_WORK(&rdev->del_work, md_delayed_delete);
2203 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002204 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
2207/*
2208 * prevent the device from being mounted, repartitioned or
2209 * otherwise reused by a RAID array (or any other kernel
2210 * subsystem), by bd_claiming the device.
2211 */
NeilBrown3cb03002011-10-11 16:45:26 +11002212static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
2214 int err = 0;
2215 struct block_device *bdev;
2216 char b[BDEVNAME_SIZE];
2217
Tejun Heod4d77622010-11-13 11:55:18 +01002218 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002219 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002221 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 return PTR_ERR(bdev);
2223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 rdev->bdev = bdev;
2225 return err;
2226}
2227
NeilBrown3cb03002011-10-11 16:45:26 +11002228static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
2230 struct block_device *bdev = rdev->bdev;
2231 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002232 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
2235void md_autodetect_dev(dev_t dev);
2236
NeilBrownf72ffdd2014-09-30 14:23:59 +10002237static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
2239 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002240
NeilBrown9d487392016-11-02 14:16:49 +11002241 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002242 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002244 if (test_bit(AutoDetected, &rdev->flags))
2245 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246#endif
2247 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002248 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002251void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
2253 unbind_rdev_from_array(rdev);
2254 export_rdev(rdev);
2255}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002256EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
NeilBrownfd01b882011-10-11 16:47:53 +11002258static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
NeilBrown0638bb02014-09-25 17:43:47 +10002260 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
NeilBrown0638bb02014-09-25 17:43:47 +10002262 while (!list_empty(&mddev->disks)) {
2263 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2264 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002265 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 mddev->raid_disks = 0;
2268 mddev->major_version = 0;
2269}
2270
NeilBrown6497709b2017-03-15 14:05:14 +11002271static bool set_in_sync(struct mddev *mddev)
2272{
2273 WARN_ON_ONCE(!spin_is_locked(&mddev->lock));
NeilBrown4ad23a972017-03-15 14:05:14 +11002274 if (!mddev->in_sync) {
2275 mddev->sync_checkers++;
2276 spin_unlock(&mddev->lock);
2277 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2278 spin_lock(&mddev->lock);
2279 if (!mddev->in_sync &&
2280 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002281 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002282 /*
2283 * Ensure ->in_sync is visible before we clear
2284 * ->sync_checkers.
2285 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002286 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002287 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2288 sysfs_notify_dirent_safe(mddev->sysfs_state);
2289 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002290 if (--mddev->sync_checkers == 0)
2291 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002292 }
2293 if (mddev->safemode == 1)
2294 mddev->safemode = 0;
2295 return mddev->in_sync;
2296}
2297
NeilBrownf72ffdd2014-09-30 14:23:59 +10002298static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
NeilBrown42543762006-06-26 00:27:57 -07002300 /* Update each superblock (in-memory image), but
2301 * if we are allowed to, skip spares which already
2302 * have the right event counter, or have one earlier
2303 * (which would mean they aren't being marked as dirty
2304 * with the rest of the array)
2305 */
NeilBrown3cb03002011-10-11 16:45:26 +11002306 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002307 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002308 if (rdev->sb_events == mddev->events ||
2309 (nospares &&
2310 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002311 rdev->sb_events+1 == mddev->events)) {
2312 /* Don't update this superblock */
2313 rdev->sb_loaded = 2;
2314 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002315 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002316 rdev->sb_loaded = 1;
2317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 }
2319}
2320
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002321static bool does_sb_need_changing(struct mddev *mddev)
2322{
2323 struct md_rdev *rdev;
2324 struct mdp_superblock_1 *sb;
2325 int role;
2326
2327 /* Find a good rdev */
2328 rdev_for_each(rdev, mddev)
2329 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2330 break;
2331
2332 /* No good device found. */
2333 if (!rdev)
2334 return false;
2335
2336 sb = page_address(rdev->sb_page);
2337 /* Check if a device has become faulty or a spare become active */
2338 rdev_for_each(rdev, mddev) {
2339 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2340 /* Device activated? */
2341 if (role == 0xffff && rdev->raid_disk >=0 &&
2342 !test_bit(Faulty, &rdev->flags))
2343 return true;
2344 /* Device turned faulty? */
2345 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2346 return true;
2347 }
2348
2349 /* Check if any mddev parameters have changed */
2350 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2351 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002352 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002353 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2354 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2355 return true;
2356
2357 return false;
2358}
2359
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002360void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
NeilBrown3cb03002011-10-11 16:45:26 +11002362 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002363 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002364 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002365 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002366 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
NeilBrownd87f0642013-04-24 11:42:40 +10002368 if (mddev->ro) {
2369 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002370 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002371 return;
2372 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002373
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002374repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002375 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002376 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002377 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002378 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002379 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002380 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002381 /* Has someone else has updated the sb */
2382 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002383 if (ret == 0)
2384 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002385 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2386 BIT(MD_SB_CHANGE_DEVS) |
2387 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002388 return;
2389 }
2390 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002391
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002392 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002393 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002394 if (rdev->raid_disk >= 0 &&
2395 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002396 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002397 !test_bit(In_sync, &rdev->flags) &&
2398 mddev->curr_resync_completed > rdev->recovery_offset)
2399 rdev->recovery_offset = mddev->curr_resync_completed;
2400
NeilBrownf72ffdd2014-09-30 14:23:59 +10002401 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002402 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002403 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2404 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002405 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002406 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002407 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002408 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002409 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002410 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002411 md_error(mddev, rdev);
2412 }
2413 clear_bit(Blocked, &rdev->flags);
2414 clear_bit(BlockedBadBlocks, &rdev->flags);
2415 wake_up(&rdev->blocked_wait);
2416 }
2417 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002418 wake_up(&mddev->sb_wait);
2419 return;
2420 }
2421
NeilBrown85572d72014-12-15 12:56:56 +11002422 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002423
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002424 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002425
Shaohua Li29530792016-12-08 15:48:19 -08002426 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b42006-10-03 01:15:46 -07002427 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002428 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b42006-10-03 01:15:46 -07002429 /* just a clean<-> dirty transition, possibly leave spares alone,
2430 * though if events isn't the right even/odd, we will have to do
2431 * spares after all
2432 */
2433 nospares = 1;
2434 if (force_change)
2435 nospares = 0;
2436 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002437 /* If the array is degraded, then skipping spares is both
2438 * dangerous and fairly pointless.
2439 * Dangerous because a device that was removed from the array
2440 * might have a event_count that still looks up-to-date,
2441 * so it can be re-added without a resync.
2442 * Pointless because if there are any spares to skip,
2443 * then a recovery will happen and soon that array won't
2444 * be degraded any more and the spare can go back to sleep then.
2445 */
NeilBrown850b2b42006-10-03 01:15:46 -07002446 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002447
NeilBrown06d91a52005-06-21 17:17:12 -07002448 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002449
2450 /* If this is just a dirty<->clean transition, and the array is clean
2451 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002452 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002453 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002454 && mddev->can_decrease_events
2455 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002456 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002457 mddev->can_decrease_events = 0;
2458 } else {
NeilBrown42543762006-06-26 00:27:57 -07002459 /* otherwise we have to go forward and ... */
2460 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002461 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
NeilBrown403df472014-09-30 15:52:29 +10002464 /*
2465 * This 64-bit counter should never wrap.
2466 * Either we are in around ~1 trillion A.C., assuming
2467 * 1 reboot per second, or we have a bug...
2468 */
2469 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002470
NeilBrowndafb20f2012-03-19 12:46:39 +11002471 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002472 if (rdev->badblocks.changed)
2473 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002474 if (test_bit(Faulty, &rdev->flags))
2475 set_bit(FaultRecorded, &rdev->flags);
2476 }
NeilBrown2699b672011-07-28 11:31:47 +10002477
NeilBrowne6910632008-02-06 01:39:51 -08002478 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002479 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
NeilBrown36a4e1f2011-10-07 14:23:17 +11002481 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2482 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Shaohua Li504634f2016-11-18 09:44:08 -08002484 if (mddev->queue)
2485 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002486rewrite:
NeilBrown4ad13662007-07-17 04:06:13 -07002487 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002488 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002490
NeilBrown42543762006-06-26 00:27:57 -07002491 if (rdev->sb_loaded != 1)
2492 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
NeilBrownf4667222013-12-09 12:04:56 +11002494 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002495 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002496 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002497 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002498 pr_debug("md: (write) %s's sb offset: %llu\n",
2499 bdevname(rdev->bdev, b),
2500 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002501 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002502 if (rdev->badblocks.size) {
2503 md_super_write(mddev, rdev,
2504 rdev->badblocks.sector,
2505 rdev->badblocks.size << 9,
2506 rdev->bb_page);
2507 rdev->badblocks.size = 0;
2508 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002509
NeilBrownf4667222013-12-09 12:04:56 +11002510 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002511 pr_debug("md: %s (skipping faulty)\n",
2512 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002513
NeilBrown7bfa19f2005-06-21 17:17:28 -07002514 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 /* only need to write one superblock... */
2516 break;
2517 }
NeilBrown46533ff2016-11-18 16:16:11 +11002518 if (md_super_wait(mddev) < 0)
2519 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002520 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002521
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002522 if (mddev_is_clustered(mddev) && ret == 0)
2523 md_cluster_ops->metadata_update_finish(mddev);
2524
NeilBrown850b2b42006-10-03 01:15:46 -07002525 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002526 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2527 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002528 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002529 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002530 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002531 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2532 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002533
NeilBrowndafb20f2012-03-19 12:46:39 +11002534 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002535 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2536 clear_bit(Blocked, &rdev->flags);
2537
2538 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002539 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002540 clear_bit(BlockedBadBlocks, &rdev->flags);
2541 wake_up(&rdev->blocked_wait);
2542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002544EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002546static int add_bound_rdev(struct md_rdev *rdev)
2547{
2548 struct mddev *mddev = rdev->mddev;
2549 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002550 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002551
Shaohua Li87d4d912016-01-06 14:37:14 -08002552 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002553 /* If there is hot_add_disk but no hot_remove_disk
2554 * then added disks for geometry changes,
2555 * and should be added immediately.
2556 */
2557 super_types[mddev->major_version].
2558 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002559 if (add_journal)
2560 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002561 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002562 if (add_journal)
2563 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002564 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002565 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002566 return err;
2567 }
2568 }
2569 sysfs_notify_dirent_safe(rdev->sysfs_state);
2570
Shaohua Li29530792016-12-08 15:48:19 -08002571 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002572 if (mddev->degraded)
2573 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2574 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2575 md_new_event(mddev);
2576 md_wakeup_thread(mddev->thread);
2577 return 0;
2578}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Andre Noll7f6ce762008-03-23 18:34:54 +01002580/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002581 * We want to accept with case. For this we use cmd_match.
2582 */
2583static int cmd_match(const char *cmd, const char *str)
2584{
2585 /* See if cmd, written into a sysfs file, matches
2586 * str. They must either be the same, or cmd can
2587 * have a trailing newline
2588 */
2589 while (*cmd && *str && *cmd == *str) {
2590 cmd++;
2591 str++;
2592 }
2593 if (*cmd == '\n')
2594 cmd++;
2595 if (*str || *cmd)
2596 return 0;
2597 return 1;
2598}
2599
NeilBrown86e6ffd2005-11-08 21:39:24 -08002600struct rdev_sysfs_entry {
2601 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002602 ssize_t (*show)(struct md_rdev *, char *);
2603 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002604};
2605
2606static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002607state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002608{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002609 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002610 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002611 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002612
NeilBrown758bfc82014-12-15 12:56:59 +11002613 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002614 (!test_bit(ExternalBbl, &flags) &&
2615 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002616 len += sprintf(page+len, "faulty%s", sep);
2617 if (test_bit(In_sync, &flags))
2618 len += sprintf(page+len, "in_sync%s", sep);
2619 if (test_bit(Journal, &flags))
2620 len += sprintf(page+len, "journal%s", sep);
2621 if (test_bit(WriteMostly, &flags))
2622 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002623 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002624 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002625 && !test_bit(Faulty, &flags)))
2626 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002627 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002628 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002629 !test_bit(In_sync, &flags))
2630 len += sprintf(page+len, "spare%s", sep);
2631 if (test_bit(WriteErrorSeen, &flags))
2632 len += sprintf(page+len, "write_error%s", sep);
2633 if (test_bit(WantReplacement, &flags))
2634 len += sprintf(page+len, "want_replacement%s", sep);
2635 if (test_bit(Replacement, &flags))
2636 len += sprintf(page+len, "replacement%s", sep);
2637 if (test_bit(ExternalBbl, &flags))
2638 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002639 if (test_bit(FailFast, &flags))
2640 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002641
2642 if (len)
2643 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002644
NeilBrown86e6ffd2005-11-08 21:39:24 -08002645 return len+sprintf(page+len, "\n");
2646}
2647
NeilBrown45dc2de2006-06-26 00:27:58 -07002648static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002649state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002650{
2651 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002652 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002653 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002654 * writemostly - sets write_mostly
2655 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002656 * blocked - sets the Blocked flags
2657 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002658 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002659 * -insync - clear Insync for a device with a slot assigned,
2660 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002661 * write_error - sets WriteErrorSeen
2662 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002663 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002664 */
2665 int err = -EINVAL;
2666 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2667 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002668 if (test_bit(Faulty, &rdev->flags))
2669 err = 0;
2670 else
2671 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002672 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002673 if (rdev->mddev->pers) {
2674 clear_bit(Blocked, &rdev->flags);
2675 remove_and_add_spares(rdev->mddev, rdev);
2676 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002677 if (rdev->raid_disk >= 0)
2678 err = -EBUSY;
2679 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002680 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002681 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002682 if (mddev_is_clustered(mddev))
2683 err = md_cluster_ops->remove_disk(mddev, rdev);
2684
2685 if (err == 0) {
2686 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002687 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002688 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002689 md_wakeup_thread(mddev->thread);
2690 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002691 md_new_event(mddev);
2692 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002693 }
NeilBrownf6556752006-06-26 00:28:01 -07002694 } else if (cmd_match(buf, "writemostly")) {
2695 set_bit(WriteMostly, &rdev->flags);
2696 err = 0;
2697 } else if (cmd_match(buf, "-writemostly")) {
2698 clear_bit(WriteMostly, &rdev->flags);
2699 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002700 } else if (cmd_match(buf, "blocked")) {
2701 set_bit(Blocked, &rdev->flags);
2702 err = 0;
2703 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002704 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002705 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002706 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002707 /* metadata handler doesn't understand badblocks,
2708 * so we need to fail the device
2709 */
2710 md_error(rdev->mddev, rdev);
2711 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002712 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002713 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002714 wake_up(&rdev->blocked_wait);
2715 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2716 md_wakeup_thread(rdev->mddev->thread);
2717
2718 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002719 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2720 set_bit(In_sync, &rdev->flags);
2721 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11002722 } else if (cmd_match(buf, "failfast")) {
2723 set_bit(FailFast, &rdev->flags);
2724 err = 0;
2725 } else if (cmd_match(buf, "-failfast")) {
2726 clear_bit(FailFast, &rdev->flags);
2727 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002728 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2729 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002730 if (rdev->mddev->pers == NULL) {
2731 clear_bit(In_sync, &rdev->flags);
2732 rdev->saved_raid_disk = rdev->raid_disk;
2733 rdev->raid_disk = -1;
2734 err = 0;
2735 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002736 } else if (cmd_match(buf, "write_error")) {
2737 set_bit(WriteErrorSeen, &rdev->flags);
2738 err = 0;
2739 } else if (cmd_match(buf, "-write_error")) {
2740 clear_bit(WriteErrorSeen, &rdev->flags);
2741 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002742 } else if (cmd_match(buf, "want_replacement")) {
2743 /* Any non-spare device that is not a replacement can
2744 * become want_replacement at any time, but we then need to
2745 * check if recovery is needed.
2746 */
2747 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002748 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002749 !test_bit(Replacement, &rdev->flags))
2750 set_bit(WantReplacement, &rdev->flags);
2751 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2752 md_wakeup_thread(rdev->mddev->thread);
2753 err = 0;
2754 } else if (cmd_match(buf, "-want_replacement")) {
2755 /* Clearing 'want_replacement' is always allowed.
2756 * Once replacements starts it is too late though.
2757 */
2758 err = 0;
2759 clear_bit(WantReplacement, &rdev->flags);
2760 } else if (cmd_match(buf, "replacement")) {
2761 /* Can only set a device as a replacement when array has not
2762 * yet been started. Once running, replacement is automatic
2763 * from spares, or by assigning 'slot'.
2764 */
2765 if (rdev->mddev->pers)
2766 err = -EBUSY;
2767 else {
2768 set_bit(Replacement, &rdev->flags);
2769 err = 0;
2770 }
2771 } else if (cmd_match(buf, "-replacement")) {
2772 /* Similarly, can only clear Replacement before start */
2773 if (rdev->mddev->pers)
2774 err = -EBUSY;
2775 else {
2776 clear_bit(Replacement, &rdev->flags);
2777 err = 0;
2778 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002779 } else if (cmd_match(buf, "re-add")) {
2780 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002781 /* clear_bit is performed _after_ all the devices
2782 * have their local Faulty bit cleared. If any writes
2783 * happen in the meantime in the local node, they
2784 * will land in the local bitmap, which will be synced
2785 * by this node eventually
2786 */
2787 if (!mddev_is_clustered(rdev->mddev) ||
2788 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2789 clear_bit(Faulty, &rdev->flags);
2790 err = add_bound_rdev(rdev);
2791 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002792 } else
2793 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002794 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2795 set_bit(ExternalBbl, &rdev->flags);
2796 rdev->badblocks.shift = 0;
2797 err = 0;
2798 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2799 clear_bit(ExternalBbl, &rdev->flags);
2800 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002801 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002802 if (!err)
2803 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002804 return err ? err : len;
2805}
NeilBrown80ca3a42006-07-10 04:44:18 -07002806static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002807__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002808
2809static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002810errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002811{
2812 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2813}
2814
2815static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002816errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002817{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002818 unsigned int n;
2819 int rv;
2820
2821 rv = kstrtouint(buf, 10, &n);
2822 if (rv < 0)
2823 return rv;
2824 atomic_set(&rdev->corrected_errors, n);
2825 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002826}
2827static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002828__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002829
NeilBrown014236d2006-01-06 00:20:55 -08002830static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002831slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002832{
Shaohua Lif2076e72015-10-08 21:54:12 -07002833 if (test_bit(Journal, &rdev->flags))
2834 return sprintf(page, "journal\n");
2835 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002836 return sprintf(page, "none\n");
2837 else
2838 return sprintf(page, "%d\n", rdev->raid_disk);
2839}
2840
2841static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002842slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002843{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002844 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002845 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002846
Shaohua Lif2076e72015-10-08 21:54:12 -07002847 if (test_bit(Journal, &rdev->flags))
2848 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002849 if (strncmp(buf, "none", 4)==0)
2850 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002851 else {
2852 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2853 if (err < 0)
2854 return err;
2855 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002856 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002857 /* Setting 'slot' on an active array requires also
2858 * updating the 'rd%d' link, and communicating
2859 * with the personality with ->hot_*_disk.
2860 * For now we only support removing
2861 * failed/spare devices. This normally happens automatically,
2862 * but not when the metadata is externally managed.
2863 */
NeilBrownc303da62008-02-06 01:39:51 -08002864 if (rdev->raid_disk == -1)
2865 return -EEXIST;
2866 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002867 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002868 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002869 clear_bit(Blocked, &rdev->flags);
2870 remove_and_add_spares(rdev->mddev, rdev);
2871 if (rdev->raid_disk >= 0)
2872 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002873 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2874 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002875 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002876 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002877 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002878 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002879 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002880
2881 if (rdev->raid_disk != -1)
2882 return -EBUSY;
2883
NeilBrownc6751b22011-02-02 11:57:13 +11002884 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2885 return -EBUSY;
2886
Neil Brown6c2fce22008-06-28 08:31:31 +10002887 if (rdev->mddev->pers->hot_add_disk == NULL)
2888 return -EINVAL;
2889
NeilBrownba1b41b2011-01-14 09:14:34 +11002890 if (slot >= rdev->mddev->raid_disks &&
2891 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2892 return -ENOSPC;
2893
Neil Brown6c2fce22008-06-28 08:31:31 +10002894 rdev->raid_disk = slot;
2895 if (test_bit(In_sync, &rdev->flags))
2896 rdev->saved_raid_disk = slot;
2897 else
2898 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002899 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002900 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002901 err = rdev->mddev->pers->
2902 hot_add_disk(rdev->mddev, rdev);
2903 if (err) {
2904 rdev->raid_disk = -1;
2905 return err;
2906 } else
2907 sysfs_notify_dirent_safe(rdev->sysfs_state);
2908 if (sysfs_link_rdev(rdev->mddev, rdev))
2909 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002910 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002911 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002912 if (slot >= rdev->mddev->raid_disks &&
2913 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002914 return -ENOSPC;
2915 rdev->raid_disk = slot;
2916 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002917 clear_bit(Faulty, &rdev->flags);
2918 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002919 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002920 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002921 }
NeilBrown014236d2006-01-06 00:20:55 -08002922 return len;
2923}
2924
NeilBrown014236d2006-01-06 00:20:55 -08002925static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002926__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002927
NeilBrown93c8cad2006-01-06 00:20:56 -08002928static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002929offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002930{
Andrew Morton6961ece2006-01-06 00:20:59 -08002931 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002932}
2933
2934static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002935offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002936{
NeilBrownc6563a82012-05-21 09:27:00 +10002937 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002938 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002939 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002940 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002941 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002942 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002943 /* Must set offset before size, so overlap checks
2944 * can be sane */
2945 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002946 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002947 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002948 return len;
2949}
2950
2951static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002952__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002953
NeilBrownc6563a82012-05-21 09:27:00 +10002954static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2955{
2956 return sprintf(page, "%llu\n",
2957 (unsigned long long)rdev->new_data_offset);
2958}
2959
2960static ssize_t new_offset_store(struct md_rdev *rdev,
2961 const char *buf, size_t len)
2962{
2963 unsigned long long new_offset;
2964 struct mddev *mddev = rdev->mddev;
2965
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002966 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002967 return -EINVAL;
2968
NeilBrownf851b602014-12-11 10:02:10 +11002969 if (mddev->sync_thread ||
2970 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002971 return -EBUSY;
2972 if (new_offset == rdev->data_offset)
2973 /* reset is always permitted */
2974 ;
2975 else if (new_offset > rdev->data_offset) {
2976 /* must not push array size beyond rdev_sectors */
2977 if (new_offset - rdev->data_offset
2978 + mddev->dev_sectors > rdev->sectors)
2979 return -E2BIG;
2980 }
2981 /* Metadata worries about other space details. */
2982
2983 /* decreasing the offset is inconsistent with a backwards
2984 * reshape.
2985 */
2986 if (new_offset < rdev->data_offset &&
2987 mddev->reshape_backwards)
2988 return -EINVAL;
2989 /* Increasing offset is inconsistent with forwards
2990 * reshape. reshape_direction should be set to
2991 * 'backwards' first.
2992 */
2993 if (new_offset > rdev->data_offset &&
2994 !mddev->reshape_backwards)
2995 return -EINVAL;
2996
2997 if (mddev->pers && mddev->persistent &&
2998 !super_types[mddev->major_version]
2999 .allow_new_offset(rdev, new_offset))
3000 return -E2BIG;
3001 rdev->new_data_offset = new_offset;
3002 if (new_offset > rdev->data_offset)
3003 mddev->reshape_backwards = 1;
3004 else if (new_offset < rdev->data_offset)
3005 mddev->reshape_backwards = 0;
3006
3007 return len;
3008}
3009static struct rdev_sysfs_entry rdev_new_offset =
3010__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3011
NeilBrown83303b62006-01-06 00:21:06 -08003012static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003013rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003014{
Andre Nolldd8ac332009-03-31 14:33:13 +11003015 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003016}
3017
NeilBrownc5d79ad2008-02-06 01:39:54 -08003018static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3019{
3020 /* check if two start/length pairs overlap */
3021 if (s1+l1 <= s2)
3022 return 0;
3023 if (s2+l2 <= s1)
3024 return 0;
3025 return 1;
3026}
3027
Dan Williamsb522adc2009-03-31 15:00:31 +11003028static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3029{
3030 unsigned long long blocks;
3031 sector_t new;
3032
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003033 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003034 return -EINVAL;
3035
3036 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3037 return -EINVAL; /* sector conversion overflow */
3038
3039 new = blocks * 2;
3040 if (new != blocks * 2)
3041 return -EINVAL; /* unsigned long long to sector_t overflow */
3042
3043 *sectors = new;
3044 return 0;
3045}
3046
NeilBrown83303b62006-01-06 00:21:06 -08003047static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003048rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003049{
NeilBrownfd01b882011-10-11 16:47:53 +11003050 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003051 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003052 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003053
Shaohua Lif2076e72015-10-08 21:54:12 -07003054 if (test_bit(Journal, &rdev->flags))
3055 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003056 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003057 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003058 if (rdev->data_offset != rdev->new_data_offset)
3059 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003060 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003061 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003062 sectors = super_types[my_mddev->major_version].
3063 rdev_size_change(rdev, sectors);
3064 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003065 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003066 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003067 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003068 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003069 if (!my_mddev->pers->resize)
3070 /* Cannot change size for RAID0 or Linear etc */
3071 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003072 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003073 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003074 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003075
Andre Nolldd8ac332009-03-31 14:33:13 +11003076 rdev->sectors = sectors;
3077 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003078 /* Need to check that all other rdevs with the same
3079 * ->bdev do not overlap. 'rcu' is sufficient to walk
3080 * the rdev lists safely.
3081 * This check does not provide a hard guarantee, it
3082 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003083 */
NeilBrownfd01b882011-10-11 16:47:53 +11003084 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003085 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003086 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003087
NeilBrown8b1afc32014-09-29 15:33:20 +10003088 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003089 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003090 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003091
NeilBrowndafb20f2012-03-19 12:46:39 +11003092 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003093 if (rdev->bdev == rdev2->bdev &&
3094 rdev != rdev2 &&
3095 overlaps(rdev->data_offset, rdev->sectors,
3096 rdev2->data_offset,
3097 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003098 overlap = 1;
3099 break;
3100 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003101 if (overlap) {
3102 mddev_put(mddev);
3103 break;
3104 }
3105 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003106 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003107 if (overlap) {
3108 /* Someone else could have slipped in a size
3109 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003110 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003111 * safe, and trust userspace not to race with
3112 * itself
3113 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003114 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003115 return -EBUSY;
3116 }
3117 }
NeilBrown83303b62006-01-06 00:21:06 -08003118 return len;
3119}
3120
3121static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003122__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003123
NeilBrown3cb03002011-10-11 16:45:26 +11003124static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003125{
3126 unsigned long long recovery_start = rdev->recovery_offset;
3127
3128 if (test_bit(In_sync, &rdev->flags) ||
3129 recovery_start == MaxSector)
3130 return sprintf(page, "none\n");
3131
3132 return sprintf(page, "%llu\n", recovery_start);
3133}
3134
NeilBrown3cb03002011-10-11 16:45:26 +11003135static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003136{
3137 unsigned long long recovery_start;
3138
3139 if (cmd_match(buf, "none"))
3140 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003141 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003142 return -EINVAL;
3143
3144 if (rdev->mddev->pers &&
3145 rdev->raid_disk >= 0)
3146 return -EBUSY;
3147
3148 rdev->recovery_offset = recovery_start;
3149 if (recovery_start == MaxSector)
3150 set_bit(In_sync, &rdev->flags);
3151 else
3152 clear_bit(In_sync, &rdev->flags);
3153 return len;
3154}
3155
3156static struct rdev_sysfs_entry rdev_recovery_start =
3157__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3158
Vishal Vermafc974ee2015-12-24 19:20:34 -07003159/* sysfs access to bad-blocks list.
3160 * We present two files.
3161 * 'bad-blocks' lists sector numbers and lengths of ranges that
3162 * are recorded as bad. The list is truncated to fit within
3163 * the one-page limit of sysfs.
3164 * Writing "sector length" to this file adds an acknowledged
3165 * bad block list.
3166 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3167 * been acknowledged. Writing to this file adds bad blocks
3168 * without acknowledging them. This is largely for testing.
3169 */
NeilBrown3cb03002011-10-11 16:45:26 +11003170static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003171{
3172 return badblocks_show(&rdev->badblocks, page, 0);
3173}
NeilBrown3cb03002011-10-11 16:45:26 +11003174static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003175{
NeilBrownde393cd2011-07-28 11:31:48 +10003176 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3177 /* Maybe that ack was all we needed */
3178 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3179 wake_up(&rdev->blocked_wait);
3180 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003181}
3182static struct rdev_sysfs_entry rdev_bad_blocks =
3183__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3184
NeilBrown3cb03002011-10-11 16:45:26 +11003185static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003186{
3187 return badblocks_show(&rdev->badblocks, page, 1);
3188}
NeilBrown3cb03002011-10-11 16:45:26 +11003189static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003190{
3191 return badblocks_store(&rdev->badblocks, page, len, 1);
3192}
3193static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3194__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3195
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003196static ssize_t
3197ppl_sector_show(struct md_rdev *rdev, char *page)
3198{
3199 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3200}
3201
3202static ssize_t
3203ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3204{
3205 unsigned long long sector;
3206
3207 if (kstrtoull(buf, 10, &sector) < 0)
3208 return -EINVAL;
3209 if (sector != (sector_t)sector)
3210 return -EINVAL;
3211
3212 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3213 rdev->raid_disk >= 0)
3214 return -EBUSY;
3215
3216 if (rdev->mddev->persistent) {
3217 if (rdev->mddev->major_version == 0)
3218 return -EINVAL;
3219 if ((sector > rdev->sb_start &&
3220 sector - rdev->sb_start > S16_MAX) ||
3221 (sector < rdev->sb_start &&
3222 rdev->sb_start - sector > -S16_MIN))
3223 return -EINVAL;
3224 rdev->ppl.offset = sector - rdev->sb_start;
3225 } else if (!rdev->mddev->external) {
3226 return -EBUSY;
3227 }
3228 rdev->ppl.sector = sector;
3229 return len;
3230}
3231
3232static struct rdev_sysfs_entry rdev_ppl_sector =
3233__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3234
3235static ssize_t
3236ppl_size_show(struct md_rdev *rdev, char *page)
3237{
3238 return sprintf(page, "%u\n", rdev->ppl.size);
3239}
3240
3241static ssize_t
3242ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3243{
3244 unsigned int size;
3245
3246 if (kstrtouint(buf, 10, &size) < 0)
3247 return -EINVAL;
3248
3249 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3250 rdev->raid_disk >= 0)
3251 return -EBUSY;
3252
3253 if (rdev->mddev->persistent) {
3254 if (rdev->mddev->major_version == 0)
3255 return -EINVAL;
3256 if (size > U16_MAX)
3257 return -EINVAL;
3258 } else if (!rdev->mddev->external) {
3259 return -EBUSY;
3260 }
3261 rdev->ppl.size = size;
3262 return len;
3263}
3264
3265static struct rdev_sysfs_entry rdev_ppl_size =
3266__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3267
NeilBrown86e6ffd2005-11-08 21:39:24 -08003268static struct attribute *rdev_default_attrs[] = {
3269 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003270 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003271 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003272 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003273 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003274 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003275 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003276 &rdev_bad_blocks.attr,
3277 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003278 &rdev_ppl_sector.attr,
3279 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003280 NULL,
3281};
3282static ssize_t
3283rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3284{
3285 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003286 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003287
3288 if (!entry->show)
3289 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003290 if (!rdev->mddev)
3291 return -EBUSY;
3292 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003293}
3294
3295static ssize_t
3296rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3297 const char *page, size_t length)
3298{
3299 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003300 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003301 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003302 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003303
3304 if (!entry->store)
3305 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003306 if (!capable(CAP_SYS_ADMIN))
3307 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003308 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003309 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003310 if (rdev->mddev == NULL)
3311 rv = -EBUSY;
3312 else
3313 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003314 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003315 }
3316 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003317}
3318
3319static void rdev_free(struct kobject *ko)
3320{
NeilBrown3cb03002011-10-11 16:45:26 +11003321 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003322 kfree(rdev);
3323}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003324static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003325 .show = rdev_attr_show,
3326 .store = rdev_attr_store,
3327};
3328static struct kobj_type rdev_ktype = {
3329 .release = rdev_free,
3330 .sysfs_ops = &rdev_sysfs_ops,
3331 .default_attrs = rdev_default_attrs,
3332};
3333
NeilBrown3cb03002011-10-11 16:45:26 +11003334int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003335{
3336 rdev->desc_nr = -1;
3337 rdev->saved_raid_disk = -1;
3338 rdev->raid_disk = -1;
3339 rdev->flags = 0;
3340 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003341 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003342 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003343 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003344 rdev->sb_loaded = 0;
3345 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003346 atomic_set(&rdev->nr_pending, 0);
3347 atomic_set(&rdev->read_errors, 0);
3348 atomic_set(&rdev->corrected_errors, 0);
3349
3350 INIT_LIST_HEAD(&rdev->same_set);
3351 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003352
3353 /* Add space to store bad block list.
3354 * This reserves the space even on arrays where it cannot
3355 * be used - I wonder if that matters
3356 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003357 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003358}
3359EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360/*
3361 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3362 *
3363 * mark the device faulty if:
3364 *
3365 * - the device is nonexistent (zero size)
3366 * - the device has no valid superblock
3367 *
3368 * a faulty rdev _never_ has rdev->sb set.
3369 */
NeilBrown3cb03002011-10-11 16:45:26 +11003370static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
3372 char b[BDEVNAME_SIZE];
3373 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003374 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 sector_t size;
3376
NeilBrown9ffae0c2006-01-06 00:20:32 -08003377 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003378 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
NeilBrown2230dfe2011-07-28 11:31:46 +10003381 err = md_rdev_init(rdev);
3382 if (err)
3383 goto abort_free;
3384 err = alloc_disk_sb(rdev);
3385 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 goto abort_free;
3387
NeilBrownc5d79ad2008-02-06 01:39:54 -08003388 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (err)
3390 goto abort_free;
3391
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003392 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003393
Mike Snitzer77304d22010-11-08 14:39:12 +01003394 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003396 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 bdevname(rdev->bdev,b));
3398 err = -EINVAL;
3399 goto abort_free;
3400 }
3401
3402 if (super_format >= 0) {
3403 err = super_types[super_format].
3404 load_super(rdev, NULL, super_minor);
3405 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003406 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003407 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003408 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 goto abort_free;
3410 }
3411 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003412 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 bdevname(rdev->bdev,b));
3414 goto abort_free;
3415 }
3416 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 return rdev;
3419
3420abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003421 if (rdev->bdev)
3422 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003423 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 kfree(rdev);
3425 return ERR_PTR(err);
3426}
3427
3428/*
3429 * Check a full RAID array for plausibility
3430 */
3431
NeilBrownf72ffdd2014-09-30 14:23:59 +10003432static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
3434 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003435 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 char b[BDEVNAME_SIZE];
3437
3438 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003439 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 switch (super_types[mddev->major_version].
3441 load_super(rdev, freshest, mddev->minor_version)) {
3442 case 1:
3443 freshest = rdev;
3444 break;
3445 case 0:
3446 break;
3447 default:
NeilBrown9d487392016-11-02 14:16:49 +11003448 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003450 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 }
3452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 super_types[mddev->major_version].
3454 validate_super(mddev, freshest);
3455
3456 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003457 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003458 if (mddev->max_disks &&
3459 (rdev->desc_nr >= mddev->max_disks ||
3460 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003461 pr_warn("md: %s: %s: only %d devices permitted\n",
3462 mdname(mddev), bdevname(rdev->bdev, b),
3463 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003464 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003465 continue;
3466 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003467 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 if (super_types[mddev->major_version].
3469 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003470 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003472 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 continue;
3474 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 if (mddev->level == LEVEL_MULTIPATH) {
3477 rdev->desc_nr = i++;
3478 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003479 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003480 } else if (rdev->raid_disk >=
3481 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3482 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003483 rdev->raid_disk = -1;
3484 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 }
3486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487}
3488
NeilBrown72e02072009-12-14 12:49:55 +11003489/* Read a fixed-point number.
3490 * Numbers in sysfs attributes should be in "standard" units where
3491 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003492 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003493 * milliseconds or jiffies.
3494 * This function takes a decimal number with a possible fractional
3495 * component, and produces an integer which is the result of
3496 * multiplying that number by 10^'scale'.
3497 * all without any floating-point arithmetic.
3498 */
3499int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3500{
3501 unsigned long result = 0;
3502 long decimals = -1;
3503 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3504 if (*cp == '.')
3505 decimals = 0;
3506 else if (decimals < scale) {
3507 unsigned int value;
3508 value = *cp - '0';
3509 result = result * 10 + value;
3510 if (decimals >= 0)
3511 decimals++;
3512 }
3513 cp++;
3514 }
3515 if (*cp == '\n')
3516 cp++;
3517 if (*cp)
3518 return -EINVAL;
3519 if (decimals < 0)
3520 decimals = 0;
3521 while (decimals < scale) {
3522 result *= 10;
3523 decimals ++;
3524 }
3525 *res = result;
3526 return 0;
3527}
3528
NeilBrowneae17012005-11-08 21:39:23 -08003529static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003530safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003531{
3532 int msec = (mddev->safemode_delay*1000)/HZ;
3533 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3534}
3535static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003536safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003537{
NeilBrown16f17b32006-06-26 00:27:37 -07003538 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003539
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003540 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003541 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003542 return -EINVAL;
3543 }
3544
NeilBrown72e02072009-12-14 12:49:55 +11003545 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003546 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003547 if (msec == 0)
3548 mddev->safemode_delay = 0;
3549 else {
NeilBrown19052c02008-08-05 15:54:13 +10003550 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003551 unsigned long new_delay = (msec*HZ)/1000;
3552
3553 if (new_delay == 0)
3554 new_delay = 1;
3555 mddev->safemode_delay = new_delay;
3556 if (new_delay < old_delay || old_delay == 0)
3557 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003558 }
3559 return len;
3560}
3561static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003562__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003563
3564static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003565level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003566{
NeilBrown36d091f2014-12-15 12:56:58 +11003567 struct md_personality *p;
3568 int ret;
3569 spin_lock(&mddev->lock);
3570 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003571 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003572 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003573 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003574 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003575 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003576 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003577 else
NeilBrown36d091f2014-12-15 12:56:58 +11003578 ret = 0;
3579 spin_unlock(&mddev->lock);
3580 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003581}
3582
NeilBrownd9d166c2006-01-06 00:20:51 -08003583static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003584level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003585{
Dan Williamsf2859af2010-05-02 10:04:16 -07003586 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003587 ssize_t rv;
3588 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003589 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003590 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003591 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003592 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003593
NeilBrown67918752014-12-15 12:57:01 +11003594 if (slen == 0 || slen >= sizeof(clevel))
3595 return -EINVAL;
3596
3597 rv = mddev_lock(mddev);
3598 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003599 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003600
3601 if (mddev->pers == NULL) {
3602 strncpy(mddev->clevel, buf, slen);
3603 if (mddev->clevel[slen-1] == '\n')
3604 slen--;
3605 mddev->clevel[slen] = 0;
3606 mddev->level = LEVEL_NONE;
3607 rv = len;
3608 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003609 }
NeilBrown67918752014-12-15 12:57:01 +11003610 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003611 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003612 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003613
3614 /* request to change the personality. Need to ensure:
3615 * - array is not engaged in resync/recovery/reshape
3616 * - old personality can be suspended
3617 * - new personality will access other array.
3618 */
3619
NeilBrown67918752014-12-15 12:57:01 +11003620 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003621 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003622 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003623 mddev->reshape_position != MaxSector ||
3624 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003625 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003626
NeilBrown67918752014-12-15 12:57:01 +11003627 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003628 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003629 pr_warn("md: %s: %s does not support online personality change\n",
3630 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003631 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003632 }
3633
3634 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003635 strncpy(clevel, buf, slen);
3636 if (clevel[slen-1] == '\n')
3637 slen--;
3638 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003639 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003640 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003641
Dan Williamsf2859af2010-05-02 10:04:16 -07003642 if (request_module("md-%s", clevel) != 0)
3643 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003644 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003645 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003646 if (!pers || !try_module_get(pers->owner)) {
3647 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003648 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003649 rv = -EINVAL;
3650 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003651 }
3652 spin_unlock(&pers_lock);
3653
3654 if (pers == mddev->pers) {
3655 /* Nothing to do! */
3656 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003657 rv = len;
3658 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003659 }
3660 if (!pers->takeover) {
3661 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003662 pr_warn("md: %s: %s does not support personality takeover\n",
3663 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003664 rv = -EINVAL;
3665 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003666 }
3667
NeilBrowndafb20f2012-03-19 12:46:39 +11003668 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003669 rdev->new_raid_disk = rdev->raid_disk;
3670
NeilBrown245f46c2009-03-31 14:39:39 +11003671 /* ->takeover must set new_* and/or delta_disks
3672 * if it succeeds, and may set them when it fails.
3673 */
3674 priv = pers->takeover(mddev);
3675 if (IS_ERR(priv)) {
3676 mddev->new_level = mddev->level;
3677 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003678 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003679 mddev->raid_disks -= mddev->delta_disks;
3680 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003681 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003682 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003683 pr_warn("md: %s: %s would not accept array\n",
3684 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003685 rv = PTR_ERR(priv);
3686 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003687 }
3688
3689 /* Looks like we have a winner */
3690 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003691 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003692
3693 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003694 oldpers = mddev->pers;
3695 oldpriv = mddev->private;
3696 mddev->pers = pers;
3697 mddev->private = priv;
3698 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3699 mddev->level = mddev->new_level;
3700 mddev->layout = mddev->new_layout;
3701 mddev->chunk_sectors = mddev->new_chunk_sectors;
3702 mddev->delta_disks = 0;
3703 mddev->reshape_backwards = 0;
3704 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003705 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003706
NeilBrowndb721d32014-12-15 12:56:58 +11003707 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003708 mddev->external) {
3709 /* We are converting from a no-redundancy array
3710 * to a redundancy array and metadata is managed
3711 * externally so we need to be sure that writes
3712 * won't block due to a need to transition
3713 * clean->dirty
3714 * until external management is started.
3715 */
3716 mddev->in_sync = 0;
3717 mddev->safemode_delay = 0;
3718 mddev->safemode = 0;
3719 }
3720
NeilBrowndb721d32014-12-15 12:56:58 +11003721 oldpers->free(mddev, oldpriv);
3722
3723 if (oldpers->sync_request == NULL &&
3724 pers->sync_request != NULL) {
3725 /* need to add the md_redundancy_group */
3726 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11003727 pr_warn("md: cannot register extra attributes for %s\n",
3728 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11003729 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3730 }
3731 if (oldpers->sync_request != NULL &&
3732 pers->sync_request == NULL) {
3733 /* need to remove the md_redundancy_group */
3734 if (mddev->to_remove == NULL)
3735 mddev->to_remove = &md_redundancy_group;
3736 }
3737
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003738 module_put(oldpers->owner);
3739
NeilBrowndafb20f2012-03-19 12:46:39 +11003740 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003741 if (rdev->raid_disk < 0)
3742 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003743 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003744 rdev->new_raid_disk = -1;
3745 if (rdev->new_raid_disk == rdev->raid_disk)
3746 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003747 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003748 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003749 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003750 if (rdev->raid_disk < 0)
3751 continue;
3752 if (rdev->new_raid_disk == rdev->raid_disk)
3753 continue;
3754 rdev->raid_disk = rdev->new_raid_disk;
3755 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003756 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003757 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003758 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11003759 pr_warn("md: cannot register rd%d for %s after level change\n",
3760 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003761 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003762 }
3763
NeilBrowndb721d32014-12-15 12:56:58 +11003764 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003765 /* this is now an array without redundancy, so
3766 * it must always be in_sync
3767 */
3768 mddev->in_sync = 1;
3769 del_timer_sync(&mddev->safemode_timer);
3770 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003771 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003772 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08003773 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003774 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003775 if (!mddev->thread)
3776 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003777 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003778 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003779 rv = len;
3780out_unlock:
3781 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003782 return rv;
3783}
3784
3785static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003786__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003787
NeilBrownd4dbd022006-06-26 00:27:59 -07003788static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003789layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003790{
3791 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003792 if (mddev->reshape_position != MaxSector &&
3793 mddev->layout != mddev->new_layout)
3794 return sprintf(page, "%d (%d)\n",
3795 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003796 return sprintf(page, "%d\n", mddev->layout);
3797}
3798
3799static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003800layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003801{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003802 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003803 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003804
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003805 err = kstrtouint(buf, 10, &n);
3806 if (err < 0)
3807 return err;
NeilBrown67918752014-12-15 12:57:01 +11003808 err = mddev_lock(mddev);
3809 if (err)
3810 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003811
NeilBrownb3546032009-03-31 14:56:41 +11003812 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003813 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003814 err = -EBUSY;
3815 else if (mddev->ro)
3816 err = -EROFS;
3817 else {
3818 mddev->new_layout = n;
3819 err = mddev->pers->check_reshape(mddev);
3820 if (err)
3821 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003822 }
NeilBrownb3546032009-03-31 14:56:41 +11003823 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003824 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003825 if (mddev->reshape_position == MaxSector)
3826 mddev->layout = n;
3827 }
NeilBrown67918752014-12-15 12:57:01 +11003828 mddev_unlock(mddev);
3829 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003830}
3831static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003832__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003833
NeilBrowneae17012005-11-08 21:39:23 -08003834static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003835raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003836{
NeilBrownbb636542005-11-08 21:39:45 -08003837 if (mddev->raid_disks == 0)
3838 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003839 if (mddev->reshape_position != MaxSector &&
3840 mddev->delta_disks != 0)
3841 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3842 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003843 return sprintf(page, "%d\n", mddev->raid_disks);
3844}
3845
NeilBrownfd01b882011-10-11 16:47:53 +11003846static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003847
3848static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003849raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003850{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003851 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003852 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003853
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003854 err = kstrtouint(buf, 10, &n);
3855 if (err < 0)
3856 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003857
NeilBrown67918752014-12-15 12:57:01 +11003858 err = mddev_lock(mddev);
3859 if (err)
3860 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003861 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003862 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003863 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003864 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003865 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003866
NeilBrown67918752014-12-15 12:57:01 +11003867 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003868 rdev_for_each(rdev, mddev) {
3869 if (olddisks < n &&
3870 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003871 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003872 if (olddisks > n &&
3873 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003874 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003875 }
NeilBrown67918752014-12-15 12:57:01 +11003876 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003877 mddev->delta_disks = n - olddisks;
3878 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003879 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003880 } else
NeilBrownda943b992006-01-06 00:20:54 -08003881 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003882out_unlock:
3883 mddev_unlock(mddev);
3884 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003885}
3886static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003887__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003888
NeilBrown24dd4692005-11-08 21:39:26 -08003889static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003890chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003891{
NeilBrown08a02ec2007-05-09 02:35:38 -07003892 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003893 mddev->chunk_sectors != mddev->new_chunk_sectors)
3894 return sprintf(page, "%d (%d)\n",
3895 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003896 mddev->chunk_sectors << 9);
3897 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003898}
3899
3900static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003901chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003902{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003903 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003904 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003905
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003906 err = kstrtoul(buf, 10, &n);
3907 if (err < 0)
3908 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003909
NeilBrown67918752014-12-15 12:57:01 +11003910 err = mddev_lock(mddev);
3911 if (err)
3912 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003913 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003914 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003915 err = -EBUSY;
3916 else if (mddev->ro)
3917 err = -EROFS;
3918 else {
3919 mddev->new_chunk_sectors = n >> 9;
3920 err = mddev->pers->check_reshape(mddev);
3921 if (err)
3922 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003923 }
NeilBrownb3546032009-03-31 14:56:41 +11003924 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003925 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003926 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003927 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003928 }
NeilBrown67918752014-12-15 12:57:01 +11003929 mddev_unlock(mddev);
3930 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003931}
3932static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003933__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003934
NeilBrowna94213b2006-06-26 00:28:00 -07003935static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003936resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003937{
NeilBrownd1a7c502009-03-31 15:24:32 +11003938 if (mddev->recovery_cp == MaxSector)
3939 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003940 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3941}
3942
3943static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003944resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003945{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003946 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003947 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003948
3949 if (cmd_match(buf, "none"))
3950 n = MaxSector;
3951 else {
3952 err = kstrtoull(buf, 10, &n);
3953 if (err < 0)
3954 return err;
3955 if (n != (sector_t)n)
3956 return -EINVAL;
3957 }
NeilBrowna94213b2006-06-26 00:28:00 -07003958
NeilBrown67918752014-12-15 12:57:01 +11003959 err = mddev_lock(mddev);
3960 if (err)
3961 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003962 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003963 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003964
NeilBrown67918752014-12-15 12:57:01 +11003965 if (!err) {
3966 mddev->recovery_cp = n;
3967 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08003968 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11003969 }
3970 mddev_unlock(mddev);
3971 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003972}
3973static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003974__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3975 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003976
NeilBrown9e653b62006-06-26 00:27:58 -07003977/*
3978 * The array state can be:
3979 *
3980 * clear
3981 * No devices, no size, no level
3982 * Equivalent to STOP_ARRAY ioctl
3983 * inactive
3984 * May have some settings, but array is not active
3985 * all IO results in error
3986 * When written, doesn't tear down array, but just stops it
3987 * suspended (not supported yet)
3988 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003989 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003990 * readonly
3991 * no resync can happen. no superblocks get written.
3992 * write requests fail
3993 * read-auto
3994 * like readonly, but behaves like 'clean' on a write request.
3995 *
3996 * clean - no pending writes, but otherwise active.
3997 * When written to inactive array, starts without resync
3998 * If a write request arrives then
3999 * if metadata is known, mark 'dirty' and switch to 'active'.
4000 * if not known, block and switch to write-pending
4001 * If written to an active array that has pending writes, then fails.
4002 * active
4003 * fully active: IO and resync can be happening.
4004 * When written to inactive array, starts with resync
4005 *
4006 * write-pending
4007 * clean, but writes are blocked waiting for 'active' to be written.
4008 *
4009 * active-idle
4010 * like active, but no writes have been seen for a while (100msec).
4011 *
4012 */
4013enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4014 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004015static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004016 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4017 "write-pending", "active-idle", NULL };
4018
4019static int match_word(const char *word, char **list)
4020{
4021 int n;
4022 for (n=0; list[n]; n++)
4023 if (cmd_match(word, list[n]))
4024 break;
4025 return n;
4026}
4027
4028static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004029array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004030{
4031 enum array_state st = inactive;
4032
4033 if (mddev->pers)
4034 switch(mddev->ro) {
4035 case 1:
4036 st = readonly;
4037 break;
4038 case 2:
4039 st = read_auto;
4040 break;
4041 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004042 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004043 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004044 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004045 else if (mddev->in_sync)
4046 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004047 else if (mddev->safemode)
4048 st = active_idle;
4049 else
4050 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004051 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004052 }
4053 else {
4054 if (list_empty(&mddev->disks) &&
4055 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004056 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004057 st = clear;
4058 else
4059 st = inactive;
4060 }
4061 return sprintf(page, "%s\n", array_states[st]);
4062}
4063
NeilBrownf72ffdd2014-09-30 14:23:59 +10004064static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4065static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4066static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004067static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004068
4069static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004070array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004071{
NeilBrown6497709b2017-03-15 14:05:14 +11004072 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004073 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004074
4075 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4076 /* don't take reconfig_mutex when toggling between
4077 * clean and active
4078 */
4079 spin_lock(&mddev->lock);
4080 if (st == active) {
4081 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004082 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004083 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004084 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004085 } else /* st == clean */ {
4086 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004087 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004088 err = -EBUSY;
4089 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004090 if (!err)
4091 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004092 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004093 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004094 }
4095 err = mddev_lock(mddev);
4096 if (err)
4097 return err;
4098 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004099 switch(st) {
4100 case bad_word:
4101 break;
4102 case clear:
4103 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004104 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004105 break;
4106 case inactive:
4107 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004108 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004109 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004110 else
NeilBrowne6910632008-02-06 01:39:51 -08004111 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004112 break;
4113 case suspended:
4114 break; /* not supported yet */
4115 case readonly:
4116 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004117 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004118 else {
4119 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004120 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004121 err = do_md_run(mddev);
4122 }
4123 break;
4124 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004125 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004126 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004127 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004128 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004129 err = restart_array(mddev);
4130 if (err == 0) {
4131 mddev->ro = 2;
4132 set_disk_ro(mddev->gendisk, 0);
4133 }
NeilBrown9e653b62006-06-26 00:27:58 -07004134 } else {
4135 mddev->ro = 2;
4136 err = do_md_run(mddev);
4137 }
4138 break;
4139 case clean:
4140 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004141 err = restart_array(mddev);
4142 if (err)
4143 break;
NeilBrown85572d72014-12-15 12:56:56 +11004144 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004145 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004146 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004147 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004148 } else
4149 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004150 break;
4151 case active:
4152 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004153 err = restart_array(mddev);
4154 if (err)
4155 break;
Shaohua Li29530792016-12-08 15:48:19 -08004156 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004157 wake_up(&mddev->sb_wait);
4158 err = 0;
4159 } else {
4160 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004161 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004162 err = do_md_run(mddev);
4163 }
4164 break;
4165 case write_pending:
4166 case active_idle:
4167 /* these cannot be set */
4168 break;
4169 }
NeilBrown67918752014-12-15 12:57:01 +11004170
4171 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004172 if (mddev->hold_active == UNTIL_IOCTL)
4173 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004174 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004175 }
NeilBrown67918752014-12-15 12:57:01 +11004176 mddev_unlock(mddev);
4177 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004178}
NeilBrown80ca3a42006-07-10 04:44:18 -07004179static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004180__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004181
NeilBrown6d7ff732006-01-06 00:21:16 -08004182static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004183max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004184 return sprintf(page, "%d\n",
4185 atomic_read(&mddev->max_corr_read_errors));
4186}
4187
4188static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004189max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004190{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004191 unsigned int n;
4192 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004193
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004194 rv = kstrtouint(buf, 10, &n);
4195 if (rv < 0)
4196 return rv;
4197 atomic_set(&mddev->max_corr_read_errors, n);
4198 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004199}
4200
4201static struct md_sysfs_entry max_corr_read_errors =
4202__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4203 max_corrected_read_errors_store);
4204
4205static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004206null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08004207{
4208 return -EINVAL;
4209}
4210
4211static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004212new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08004213{
4214 /* buf must be %d:%d\n? giving major and minor numbers */
4215 /* The new device is added to the array.
4216 * If the array has a persistent superblock, we read the
4217 * superblock to initialise info and check validity.
4218 * Otherwise, only checking done is that in bind_rdev_to_array,
4219 * which mainly checks size.
4220 */
4221 char *e;
4222 int major = simple_strtoul(buf, &e, 10);
4223 int minor;
4224 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004225 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08004226 int err;
4227
4228 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4229 return -EINVAL;
4230 minor = simple_strtoul(e+1, &e, 10);
4231 if (*e && *e != '\n')
4232 return -EINVAL;
4233 dev = MKDEV(major, minor);
4234 if (major != MAJOR(dev) ||
4235 minor != MINOR(dev))
4236 return -EOVERFLOW;
4237
NeilBrown67918752014-12-15 12:57:01 +11004238 flush_workqueue(md_misc_wq);
4239
4240 err = mddev_lock(mddev);
4241 if (err)
4242 return err;
NeilBrown6d7ff732006-01-06 00:21:16 -08004243 if (mddev->persistent) {
4244 rdev = md_import_device(dev, mddev->major_version,
4245 mddev->minor_version);
4246 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004247 struct md_rdev *rdev0
4248 = list_entry(mddev->disks.next,
4249 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08004250 err = super_types[mddev->major_version]
4251 .load_super(rdev, rdev0, mddev->minor_version);
4252 if (err < 0)
4253 goto out;
4254 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004255 } else if (mddev->external)
4256 rdev = md_import_device(dev, -2, -1);
4257 else
NeilBrown6d7ff732006-01-06 00:21:16 -08004258 rdev = md_import_device(dev, -1, -1);
4259
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004260 if (IS_ERR(rdev)) {
4261 mddev_unlock(mddev);
NeilBrown6d7ff732006-01-06 00:21:16 -08004262 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004263 }
NeilBrown6d7ff732006-01-06 00:21:16 -08004264 err = bind_rdev_to_array(rdev, mddev);
4265 out:
4266 if (err)
4267 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004268 mddev_unlock(mddev);
NeilBrown6d7ff732006-01-06 00:21:16 -08004269 return err ? err : len;
4270}
4271
4272static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004273__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004274
4275static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004276bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004277{
4278 char *end;
4279 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004280 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004281
NeilBrown67918752014-12-15 12:57:01 +11004282 err = mddev_lock(mddev);
4283 if (err)
4284 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004285 if (!mddev->bitmap)
4286 goto out;
4287 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4288 while (*buf) {
4289 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4290 if (buf == end) break;
4291 if (*end == '-') { /* range */
4292 buf = end + 1;
4293 end_chunk = simple_strtoul(buf, &end, 0);
4294 if (buf == end) break;
4295 }
4296 if (*end && !isspace(*end)) break;
4297 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004298 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004299 }
4300 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4301out:
NeilBrown67918752014-12-15 12:57:01 +11004302 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004303 return len;
4304}
4305
4306static struct md_sysfs_entry md_bitmap =
4307__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4308
4309static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004310size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004311{
Andre Noll58c0fed2009-03-31 14:33:13 +11004312 return sprintf(page, "%llu\n",
4313 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004314}
4315
NeilBrownfd01b882011-10-11 16:47:53 +11004316static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004317
4318static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004319size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004320{
4321 /* If array is inactive, we can reduce the component size, but
4322 * not increase it (except from 0).
4323 * If array is active, we can try an on-line resize
4324 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004325 sector_t sectors;
4326 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004327
Andre Noll58c0fed2009-03-31 14:33:13 +11004328 if (err < 0)
4329 return err;
NeilBrown67918752014-12-15 12:57:01 +11004330 err = mddev_lock(mddev);
4331 if (err)
4332 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004333 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004334 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004335 if (err == 0)
4336 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004337 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004338 if (mddev->dev_sectors == 0 ||
4339 mddev->dev_sectors > sectors)
4340 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004341 else
4342 err = -ENOSPC;
4343 }
NeilBrown67918752014-12-15 12:57:01 +11004344 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004345 return err ? err : len;
4346}
4347
4348static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004349__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004350
Masanari Iida83f0d772012-10-30 00:18:08 +09004351/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004352 * This is one of
4353 * 'none' for arrays with no metadata (good luck...)
4354 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004355 * or N.M for internally known formats
4356 */
4357static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004358metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004359{
4360 if (mddev->persistent)
4361 return sprintf(page, "%d.%d\n",
4362 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004363 else if (mddev->external)
4364 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004365 else
4366 return sprintf(page, "none\n");
4367}
4368
4369static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004370metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004371{
4372 int major, minor;
4373 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004374 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004375 /* Changing the details of 'external' metadata is
4376 * always permitted. Otherwise there must be
4377 * no devices attached to the array.
4378 */
NeilBrown67918752014-12-15 12:57:01 +11004379
4380 err = mddev_lock(mddev);
4381 if (err)
4382 return err;
4383 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004384 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4385 ;
4386 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004387 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004388
NeilBrown67918752014-12-15 12:57:01 +11004389 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004390 if (cmd_match(buf, "none")) {
4391 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004392 mddev->external = 0;
4393 mddev->major_version = 0;
4394 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004395 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004396 }
4397 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004398 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004399 if (namelen >= sizeof(mddev->metadata_type))
4400 namelen = sizeof(mddev->metadata_type)-1;
4401 strncpy(mddev->metadata_type, buf+9, namelen);
4402 mddev->metadata_type[namelen] = 0;
4403 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4404 mddev->metadata_type[--namelen] = 0;
4405 mddev->persistent = 0;
4406 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004407 mddev->major_version = 0;
4408 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004409 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004410 }
4411 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004412 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004413 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004414 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004415 buf = e+1;
4416 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004417 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004418 goto out_unlock;
4419 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004420 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004421 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004422 mddev->major_version = major;
4423 mddev->minor_version = minor;
4424 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004425 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004426 err = 0;
4427out_unlock:
4428 mddev_unlock(mddev);
4429 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004430}
4431
4432static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004433__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004434
NeilBrowna35b0d62006-01-06 00:20:49 -08004435static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004436action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004437{
NeilBrown7eec3142005-11-08 21:39:44 -08004438 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004439 unsigned long recovery = mddev->recovery;
4440 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004441 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004442 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4443 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4444 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004445 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004446 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4447 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004448 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004449 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004450 type = "check";
4451 else
4452 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004453 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004454 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004455 else if (mddev->reshape_position != MaxSector)
4456 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004457 }
4458 return sprintf(page, "%s\n", type);
4459}
4460
4461static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004462action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004463{
NeilBrown7eec3142005-11-08 21:39:44 -08004464 if (!mddev->pers || !mddev->pers->sync_request)
4465 return -EINVAL;
4466
NeilBrownb6a9ce62009-05-26 09:41:17 +10004467
4468 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004469 if (cmd_match(page, "frozen"))
4470 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4471 else
4472 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004473 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4474 mddev_lock(mddev) == 0) {
4475 flush_workqueue(md_misc_wq);
4476 if (mddev->sync_thread) {
4477 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004478 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004479 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004480 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004481 }
NeilBrown312045e2015-12-21 11:01:21 +11004482 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004483 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004484 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004485 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004486 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004487 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004488 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004489 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004490 int err;
4491 if (mddev->pers->start_reshape == NULL)
4492 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004493 err = mddev_lock(mddev);
4494 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004495 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4496 err = -EBUSY;
4497 else {
4498 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4499 err = mddev->pers->start_reshape(mddev);
4500 }
NeilBrown67918752014-12-15 12:57:01 +11004501 mddev_unlock(mddev);
4502 }
NeilBrown16484bf2006-03-27 01:18:13 -08004503 if (err)
4504 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004505 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004506 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004507 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004508 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004509 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004510 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004511 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004512 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4513 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004514 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004515 if (mddev->ro == 2) {
4516 /* A write to sync_action is enough to justify
4517 * canceling read-auto mode
4518 */
4519 mddev->ro = 0;
4520 md_wakeup_thread(mddev->sync_thread);
4521 }
NeilBrown03c902e2006-01-06 00:20:46 -08004522 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004523 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004524 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004525 return len;
4526}
4527
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004528static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004529__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004530
4531static ssize_t
4532last_sync_action_show(struct mddev *mddev, char *page)
4533{
4534 return sprintf(page, "%s\n", mddev->last_sync_action);
4535}
4536
4537static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4538
NeilBrown9d888832005-11-08 21:39:26 -08004539static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004540mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004541{
4542 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004543 (unsigned long long)
4544 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004545}
4546
NeilBrown80ca3a42006-07-10 04:44:18 -07004547static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004548
NeilBrown88202a02006-01-06 00:21:36 -08004549static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004550sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004551{
4552 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4553 mddev->sync_speed_min ? "local": "system");
4554}
4555
4556static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004557sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004558{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004559 unsigned int min;
4560 int rv;
4561
NeilBrown88202a02006-01-06 00:21:36 -08004562 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004563 min = 0;
4564 } else {
4565 rv = kstrtouint(buf, 10, &min);
4566 if (rv < 0)
4567 return rv;
4568 if (min == 0)
4569 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004570 }
NeilBrown88202a02006-01-06 00:21:36 -08004571 mddev->sync_speed_min = min;
4572 return len;
4573}
4574
4575static struct md_sysfs_entry md_sync_min =
4576__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4577
4578static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004579sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004580{
4581 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4582 mddev->sync_speed_max ? "local": "system");
4583}
4584
4585static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004586sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004587{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004588 unsigned int max;
4589 int rv;
4590
NeilBrown88202a02006-01-06 00:21:36 -08004591 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004592 max = 0;
4593 } else {
4594 rv = kstrtouint(buf, 10, &max);
4595 if (rv < 0)
4596 return rv;
4597 if (max == 0)
4598 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004599 }
NeilBrown88202a02006-01-06 00:21:36 -08004600 mddev->sync_speed_max = max;
4601 return len;
4602}
4603
4604static struct md_sysfs_entry md_sync_max =
4605__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4606
Iustin Popd7f3d292007-10-16 23:30:54 -07004607static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004608degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004609{
4610 return sprintf(page, "%d\n", mddev->degraded);
4611}
4612static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004613
4614static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004615sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004616{
4617 return sprintf(page, "%d\n", mddev->parallel_resync);
4618}
4619
4620static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004621sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004622{
4623 long n;
4624
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004625 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004626 return -EINVAL;
4627
4628 if (n != 0 && n != 1)
4629 return -EINVAL;
4630
4631 mddev->parallel_resync = n;
4632
4633 if (mddev->sync_thread)
4634 wake_up(&resync_wait);
4635
4636 return len;
4637}
4638
4639/* force parallel resync, even with shared block devices */
4640static struct md_sysfs_entry md_sync_force_parallel =
4641__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4642 sync_force_parallel_show, sync_force_parallel_store);
4643
4644static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004645sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004646{
4647 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004648 if (mddev->curr_resync == 0)
4649 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004650 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4651 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004652 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004653 db = resync - mddev->resync_mark_cnt;
4654 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004655}
4656
NeilBrown80ca3a42006-07-10 04:44:18 -07004657static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004658
4659static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004660sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004661{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004662 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004663
NeilBrownacb180b2009-04-14 16:28:34 +10004664 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4665 return sprintf(page, "none\n");
4666
NeilBrown72f36d52012-10-11 14:25:57 +11004667 if (mddev->curr_resync == 1 ||
4668 mddev->curr_resync == 2)
4669 return sprintf(page, "delayed\n");
4670
NeilBrownc804cde2012-05-21 09:28:33 +10004671 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4672 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004673 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004674 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004675 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004676
NeilBrownacb180b2009-04-14 16:28:34 +10004677 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004678 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004679}
4680
NeilBrown750f1992014-09-30 08:53:05 +10004681static struct md_sysfs_entry md_sync_completed =
4682 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004683
NeilBrowne464eaf2006-03-27 01:18:14 -08004684static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004685min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004686{
4687 return sprintf(page, "%llu\n",
4688 (unsigned long long)mddev->resync_min);
4689}
4690static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004691min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004692{
4693 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004694 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004695
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004696 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004697 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004698
4699 spin_lock(&mddev->lock);
4700 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004701 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004702 goto out_unlock;
4703
4704 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004705 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004706 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004707
NeilBrown50c37b12015-03-23 17:36:38 +11004708 /* Round down to multiple of 4K for safety */
4709 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004710 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004711
NeilBrown23da4222014-12-15 12:57:01 +11004712out_unlock:
4713 spin_unlock(&mddev->lock);
4714 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004715}
4716
4717static struct md_sysfs_entry md_min_sync =
4718__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4719
4720static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004721max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004722{
4723 if (mddev->resync_max == MaxSector)
4724 return sprintf(page, "max\n");
4725 else
4726 return sprintf(page, "%llu\n",
4727 (unsigned long long)mddev->resync_max);
4728}
4729static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004730max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004731{
NeilBrown23da4222014-12-15 12:57:01 +11004732 int err;
4733 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004734 if (strncmp(buf, "max", 3) == 0)
4735 mddev->resync_max = MaxSector;
4736 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004737 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004738 int chunk;
4739
4740 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004741 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004742 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004743 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004744 goto out_unlock;
4745
4746 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004747 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004748 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004749 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004750 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004751
4752 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004753 chunk = mddev->chunk_sectors;
4754 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004755 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004756
4757 err = -EINVAL;
4758 if (sector_div(temp, chunk))
4759 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004760 }
4761 mddev->resync_max = max;
4762 }
4763 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004764 err = 0;
4765out_unlock:
4766 spin_unlock(&mddev->lock);
4767 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004768}
4769
4770static struct md_sysfs_entry md_max_sync =
4771__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4772
4773static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004774suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004775{
4776 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4777}
4778
4779static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004780suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004781{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004782 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004783 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004784
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004785 err = kstrtoull(buf, 10, &new);
4786 if (err < 0)
4787 return err;
4788 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004789 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004790
NeilBrown67918752014-12-15 12:57:01 +11004791 err = mddev_lock(mddev);
4792 if (err)
4793 return err;
4794 err = -EINVAL;
4795 if (mddev->pers == NULL ||
4796 mddev->pers->quiesce == NULL)
4797 goto unlock;
4798 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004799 mddev->suspend_lo = new;
4800 if (new >= old)
4801 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004802 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004803 else {
4804 /* Expanding suspended region - need to wait */
4805 mddev->pers->quiesce(mddev, 1);
4806 mddev->pers->quiesce(mddev, 0);
4807 }
NeilBrown67918752014-12-15 12:57:01 +11004808 err = 0;
4809unlock:
4810 mddev_unlock(mddev);
4811 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004812}
4813static struct md_sysfs_entry md_suspend_lo =
4814__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4815
NeilBrowne464eaf2006-03-27 01:18:14 -08004816static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004817suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004818{
4819 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4820}
4821
4822static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004823suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004824{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004825 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004826 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004827
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004828 err = kstrtoull(buf, 10, &new);
4829 if (err < 0)
4830 return err;
4831 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004832 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004833
NeilBrown67918752014-12-15 12:57:01 +11004834 err = mddev_lock(mddev);
4835 if (err)
4836 return err;
4837 err = -EINVAL;
4838 if (mddev->pers == NULL ||
4839 mddev->pers->quiesce == NULL)
4840 goto unlock;
4841 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004842 mddev->suspend_hi = new;
4843 if (new <= old)
4844 /* Shrinking suspended region */
4845 mddev->pers->quiesce(mddev, 2);
4846 else {
4847 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004848 mddev->pers->quiesce(mddev, 1);
4849 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004850 }
NeilBrown67918752014-12-15 12:57:01 +11004851 err = 0;
4852unlock:
4853 mddev_unlock(mddev);
4854 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004855}
4856static struct md_sysfs_entry md_suspend_hi =
4857__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4858
NeilBrown08a02ec2007-05-09 02:35:38 -07004859static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004860reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004861{
4862 if (mddev->reshape_position != MaxSector)
4863 return sprintf(page, "%llu\n",
4864 (unsigned long long)mddev->reshape_position);
4865 strcpy(page, "none\n");
4866 return 5;
4867}
4868
4869static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004870reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004871{
NeilBrownc6563a82012-05-21 09:27:00 +10004872 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004873 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004874 int err;
NeilBrown67918752014-12-15 12:57:01 +11004875
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004876 err = kstrtoull(buf, 10, &new);
4877 if (err < 0)
4878 return err;
4879 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004880 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004881 err = mddev_lock(mddev);
4882 if (err)
4883 return err;
4884 err = -EBUSY;
4885 if (mddev->pers)
4886 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004887 mddev->reshape_position = new;
4888 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004889 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004890 mddev->new_level = mddev->level;
4891 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004892 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004893 rdev_for_each(rdev, mddev)
4894 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004895 err = 0;
4896unlock:
4897 mddev_unlock(mddev);
4898 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004899}
4900
4901static struct md_sysfs_entry md_reshape_position =
4902__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4903 reshape_position_store);
4904
Dan Williamsb522adc2009-03-31 15:00:31 +11004905static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004906reshape_direction_show(struct mddev *mddev, char *page)
4907{
4908 return sprintf(page, "%s\n",
4909 mddev->reshape_backwards ? "backwards" : "forwards");
4910}
4911
4912static ssize_t
4913reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4914{
4915 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004916 int err;
4917
NeilBrown2c810cd2012-05-21 09:27:00 +10004918 if (cmd_match(buf, "forwards"))
4919 backwards = 0;
4920 else if (cmd_match(buf, "backwards"))
4921 backwards = 1;
4922 else
4923 return -EINVAL;
4924 if (mddev->reshape_backwards == backwards)
4925 return len;
4926
NeilBrown67918752014-12-15 12:57:01 +11004927 err = mddev_lock(mddev);
4928 if (err)
4929 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004930 /* check if we are allowed to change */
4931 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004932 err = -EBUSY;
4933 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004934 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004935 err = -EINVAL;
4936 else
4937 mddev->reshape_backwards = backwards;
4938 mddev_unlock(mddev);
4939 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004940}
4941
4942static struct md_sysfs_entry md_reshape_direction =
4943__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4944 reshape_direction_store);
4945
4946static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004947array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004948{
4949 if (mddev->external_size)
4950 return sprintf(page, "%llu\n",
4951 (unsigned long long)mddev->array_sectors/2);
4952 else
4953 return sprintf(page, "default\n");
4954}
4955
4956static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004957array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004958{
4959 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004960 int err;
4961
4962 err = mddev_lock(mddev);
4963 if (err)
4964 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004965
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004966 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08004967 if (mddev_is_clustered(mddev)) {
4968 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004969 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08004970 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004971
Dan Williamsb522adc2009-03-31 15:00:31 +11004972 if (strncmp(buf, "default", 7) == 0) {
4973 if (mddev->pers)
4974 sectors = mddev->pers->size(mddev, 0, 0);
4975 else
4976 sectors = mddev->array_sectors;
4977
4978 mddev->external_size = 0;
4979 } else {
4980 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004981 err = -EINVAL;
4982 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4983 err = -E2BIG;
4984 else
4985 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004986 }
4987
NeilBrown67918752014-12-15 12:57:01 +11004988 if (!err) {
4989 mddev->array_sectors = sectors;
4990 if (mddev->pers) {
4991 set_capacity(mddev->gendisk, mddev->array_sectors);
4992 revalidate_disk(mddev->gendisk);
4993 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004994 }
NeilBrown67918752014-12-15 12:57:01 +11004995 mddev_unlock(mddev);
4996 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004997}
4998
4999static struct md_sysfs_entry md_array_size =
5000__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5001 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005002
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005003static ssize_t
5004consistency_policy_show(struct mddev *mddev, char *page)
5005{
5006 int ret;
5007
5008 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5009 ret = sprintf(page, "journal\n");
5010 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5011 ret = sprintf(page, "ppl\n");
5012 } else if (mddev->bitmap) {
5013 ret = sprintf(page, "bitmap\n");
5014 } else if (mddev->pers) {
5015 if (mddev->pers->sync_request)
5016 ret = sprintf(page, "resync\n");
5017 else
5018 ret = sprintf(page, "none\n");
5019 } else {
5020 ret = sprintf(page, "unknown\n");
5021 }
5022
5023 return ret;
5024}
5025
5026static ssize_t
5027consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5028{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005029 int err = 0;
5030
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005031 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005032 if (mddev->pers->change_consistency_policy)
5033 err = mddev->pers->change_consistency_policy(mddev, buf);
5034 else
5035 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005036 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5037 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005038 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005039 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005040 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005041
5042 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005043}
5044
5045static struct md_sysfs_entry md_consistency_policy =
5046__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5047 consistency_policy_store);
5048
NeilBrowneae17012005-11-08 21:39:23 -08005049static struct attribute *md_default_attrs[] = {
5050 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005051 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005052 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005053 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005054 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005055 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005056 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08005057 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005058 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005059 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005060 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005061 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005062 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005063 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005064 &md_consistency_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005065 NULL,
5066};
5067
5068static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005069 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005070 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005071 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005072 &md_sync_min.attr,
5073 &md_sync_max.attr,
5074 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005075 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005076 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005077 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005078 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005079 &md_suspend_lo.attr,
5080 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005081 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005082 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005083 NULL,
5084};
NeilBrown411036f2005-11-08 21:39:40 -08005085static struct attribute_group md_redundancy_group = {
5086 .name = NULL,
5087 .attrs = md_redundancy_attrs,
5088};
5089
NeilBrowneae17012005-11-08 21:39:23 -08005090static ssize_t
5091md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5092{
5093 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005094 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005095 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005096
5097 if (!entry->show)
5098 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005099 spin_lock(&all_mddevs_lock);
5100 if (list_empty(&mddev->all_mddevs)) {
5101 spin_unlock(&all_mddevs_lock);
5102 return -EBUSY;
5103 }
5104 mddev_get(mddev);
5105 spin_unlock(&all_mddevs_lock);
5106
NeilBrownb7b17c92014-12-15 12:56:59 +11005107 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005108 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005109 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005110}
5111
5112static ssize_t
5113md_attr_store(struct kobject *kobj, struct attribute *attr,
5114 const char *page, size_t length)
5115{
5116 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005117 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005118 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005119
5120 if (!entry->store)
5121 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005122 if (!capable(CAP_SYS_ADMIN))
5123 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005124 spin_lock(&all_mddevs_lock);
5125 if (list_empty(&mddev->all_mddevs)) {
5126 spin_unlock(&all_mddevs_lock);
5127 return -EBUSY;
5128 }
5129 mddev_get(mddev);
5130 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005131 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005132 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005133 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005134}
5135
5136static void md_free(struct kobject *ko)
5137{
NeilBrownfd01b882011-10-11 16:47:53 +11005138 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005139
5140 if (mddev->sysfs_state)
5141 sysfs_put(mddev->sysfs_state);
5142
NeilBrown6cd18e72015-04-27 14:12:22 +10005143 if (mddev->queue)
5144 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11005145 if (mddev->gendisk) {
5146 del_gendisk(mddev->gendisk);
5147 put_disk(mddev->gendisk);
5148 }
NeilBrown4ad23a972017-03-15 14:05:14 +11005149 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005150
NeilBrowneae17012005-11-08 21:39:23 -08005151 kfree(mddev);
5152}
5153
Emese Revfy52cf25d2010-01-19 02:58:23 +01005154static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005155 .show = md_attr_show,
5156 .store = md_attr_store,
5157};
5158static struct kobj_type md_ktype = {
5159 .release = md_free,
5160 .sysfs_ops = &md_sysfs_ops,
5161 .default_attrs = md_default_attrs,
5162};
5163
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164int mdp_major = 0;
5165
Dan Williams5fd3a172009-03-04 00:57:25 -07005166static void mddev_delayed_delete(struct work_struct *ws)
5167{
NeilBrownfd01b882011-10-11 16:47:53 +11005168 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005169
NeilBrown43a70502009-12-14 12:49:55 +11005170 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005171 kobject_del(&mddev->kobj);
5172 kobject_put(&mddev->kobj);
5173}
5174
NeilBrown4ad23a972017-03-15 14:05:14 +11005175static void no_op(struct percpu_ref *r) {}
5176
NeilBrowna415c0f2017-06-05 16:05:13 +10005177int mddev_init_writes_pending(struct mddev *mddev)
5178{
5179 if (mddev->writes_pending.percpu_count_ptr)
5180 return 0;
5181 if (percpu_ref_init(&mddev->writes_pending, no_op, 0, GFP_KERNEL) < 0)
5182 return -ENOMEM;
5183 /* We want to start with the refcount at zero */
5184 percpu_ref_put(&mddev->writes_pending);
5185 return 0;
5186}
5187EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5188
NeilBrownefeb53c2009-01-09 08:31:10 +11005189static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190{
NeilBrown039b7222017-04-12 16:26:13 +10005191 /*
5192 * If dev is zero, name is the name of a device to allocate with
5193 * an arbitrary minor number. It will be "md_???"
5194 * If dev is non-zero it must be a device number with a MAJOR of
5195 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5196 * the device is being created by opening a node in /dev.
5197 * If "name" is not NULL, the device is being created by
5198 * writing to /sys/module/md_mod/parameters/new_array.
5199 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005200 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005201 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005203 int partitioned;
5204 int shift;
5205 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005206 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
5208 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005209 return -ENODEV;
5210
5211 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5212 shift = partitioned ? MdpMinorShift : 0;
5213 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214
Tejun Heoe804ac72010-10-15 15:36:08 +02005215 /* wait for any previous instance of this device to be
5216 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005217 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005218 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005219
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005220 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005221 error = -EEXIST;
5222 if (mddev->gendisk)
5223 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005224
NeilBrown039b7222017-04-12 16:26:13 +10005225 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005226 /* Need to ensure that 'name' is not a duplicate.
5227 */
NeilBrownfd01b882011-10-11 16:47:53 +11005228 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005229 spin_lock(&all_mddevs_lock);
5230
5231 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5232 if (mddev2->gendisk &&
5233 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5234 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005235 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005236 }
5237 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 }
NeilBrown039b7222017-04-12 16:26:13 +10005239 if (name && dev)
5240 /*
5241 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5242 */
5243 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005244
NeilBrown0909dc42009-07-01 12:27:21 +10005245 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005246 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005247 if (!mddev->queue)
5248 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005249 mddev->queue->queuedata = mddev;
5250
NeilBrown409c57f2009-03-31 14:39:39 +11005251 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005252 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005253
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 disk = alloc_disk(1 << shift);
5255 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005256 blk_cleanup_queue(mddev->queue);
5257 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005258 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005260 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005262 if (name)
5263 strcpy(disk->disk_name, name);
5264 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07005266 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 disk->fops = &md_fops;
5269 disk->private_data = mddev;
5270 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005271 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005272 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005273 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005274 * remove it now.
5275 */
5276 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005278 /* As soon as we call add_disk(), another thread could get
5279 * through to md_open, so make sure it doesn't get too far
5280 */
5281 mutex_lock(&mddev->open_mutex);
5282 add_disk(disk);
5283
Tejun Heoed9e1982008-08-25 19:56:05 +09005284 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5285 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005286 if (error) {
5287 /* This isn't possible, but as kobject_init_and_add is marked
5288 * __must_check, we must do something with the result
5289 */
NeilBrown9d487392016-11-02 14:16:49 +11005290 pr_debug("md: cannot register %s/md - name in use\n",
5291 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005292 error = 0;
5293 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005294 if (mddev->kobj.sd &&
5295 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005296 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005297 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005298 abort:
5299 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005300 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005301 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005302 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005303 }
NeilBrownd3374822009-01-09 08:31:10 +11005304 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005305 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005306}
5307
5308static struct kobject *md_probe(dev_t dev, int *part, void *data)
5309{
NeilBrown78b63502017-04-12 16:26:13 +10005310 if (create_on_open)
5311 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 return NULL;
5313}
5314
NeilBrownefeb53c2009-01-09 08:31:10 +11005315static int add_named_array(const char *val, struct kernel_param *kp)
5316{
NeilBrown039b7222017-04-12 16:26:13 +10005317 /*
5318 * val must be "md_*" or "mdNNN".
5319 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005320 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005321 * For "mdNNN" we allocate an array with the minor number NNN
5322 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005323 */
5324 int len = strlen(val);
5325 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005326 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005327
5328 while (len && val[len-1] == '\n')
5329 len--;
5330 if (len >= DISK_NAME_LEN)
5331 return -E2BIG;
5332 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005333 if (strncmp(buf, "md_", 3) == 0)
5334 return md_alloc(0, buf);
5335 if (strncmp(buf, "md", 2) == 0 &&
5336 isdigit(buf[2]) &&
5337 kstrtoul(buf+2, 10, &devnum) == 0 &&
5338 devnum <= MINORMASK)
5339 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5340
5341 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005342}
5343
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344static void md_safemode_timeout(unsigned long data)
5345{
NeilBrownfd01b882011-10-11 16:47:53 +11005346 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347
NeilBrown4ad23a972017-03-15 14:05:14 +11005348 mddev->safemode = 1;
5349 if (mddev->external)
5350 sysfs_notify_dirent_safe(mddev->sysfs_state);
5351
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 md_wakeup_thread(mddev->thread);
5353}
5354
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005355static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356
NeilBrownfd01b882011-10-11 16:47:53 +11005357int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358{
NeilBrown2604b702006-01-06 00:20:36 -08005359 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005360 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005361 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362
NeilBrowna757e642005-04-16 15:26:42 -07005363 if (list_empty(&mddev->disks))
5364 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366
5367 if (mddev->pers)
5368 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005369 /* Cannot run until previous stop completes properly */
5370 if (mddev->sysfs_active)
5371 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005372
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 /*
5374 * Analyze all RAID superblock(s)
5375 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005376 if (!mddev->raid_disks) {
5377 if (!mddev->persistent)
5378 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005379 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381
NeilBrownd9d166c2006-01-06 00:20:51 -08005382 if (mddev->level != LEVEL_NONE)
5383 request_module("md-level-%d", mddev->level);
5384 else if (mddev->clevel[0])
5385 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
5387 /*
5388 * Drop all container device buffers, from now on
5389 * the only valid external interface is through the md
5390 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005392 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005393 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 continue;
5395 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005396 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005397 if (mddev->ro != 1 &&
5398 (bdev_read_only(rdev->bdev) ||
5399 bdev_read_only(rdev->meta_bdev))) {
5400 mddev->ro = 1;
5401 if (mddev->gendisk)
5402 set_disk_ro(mddev->gendisk, 1);
5403 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005404
5405 /* perform some consistency tests on the device.
5406 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005407 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005408 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005409 if (rdev->meta_bdev) {
5410 /* Nothing to check */;
5411 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005412 if (mddev->dev_sectors &&
5413 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005414 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005415 pr_warn("md: %s: data overlaps metadata\n",
5416 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005417 return -EINVAL;
5418 }
5419 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005420 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005421 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005422 pr_warn("md: %s: metadata overlaps data\n",
5423 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005424 return -EINVAL;
5425 }
5426 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005427 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 }
5429
Ming Lei10273172017-02-14 23:29:00 +08005430 if (mddev->bio_set == NULL) {
Kent Overstreet395c72a2012-09-06 15:34:55 -07005431 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
Ming Lei10273172017-02-14 23:29:00 +08005432 if (!mddev->bio_set)
5433 return -ENOMEM;
5434 }
NeilBrowna167f662010-10-26 18:31:13 +11005435
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005437 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005438 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005440 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005441 pr_warn("md: personality for level %d is not loaded!\n",
5442 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005443 else
NeilBrown9d487392016-11-02 14:16:49 +11005444 pr_warn("md: personality for level %s is not loaded!\n",
5445 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 return -EINVAL;
5447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005449 if (mddev->level != pers->level) {
5450 mddev->level = pers->level;
5451 mddev->new_level = pers->level;
5452 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005453 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454
NeilBrownf6705572006-03-27 01:18:11 -08005455 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005456 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005457 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005458 module_put(pers->owner);
5459 return -EINVAL;
5460 }
5461
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005462 if (pers->sync_request) {
5463 /* Warn if this is a potentially silly
5464 * configuration.
5465 */
5466 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005467 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005468 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005469
NeilBrowndafb20f2012-03-19 12:46:39 +11005470 rdev_for_each(rdev, mddev)
5471 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005472 if (rdev < rdev2 &&
5473 rdev->bdev->bd_contains ==
5474 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005475 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5476 mdname(mddev),
5477 bdevname(rdev->bdev,b),
5478 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005479 warned = 1;
5480 }
5481 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005482
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005483 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005484 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005485 }
5486
NeilBrown657390d2005-08-26 18:34:16 -07005487 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005488 /* may be over-ridden by personality */
5489 mddev->resync_max_sectors = mddev->dev_sectors;
5490
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005491 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492
NeilBrown0f9552b2009-12-30 12:08:50 +11005493 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005494 mddev->ro = 2; /* read-only, but switch on first write */
5495
Song Liua85dd7b2017-01-23 17:12:57 -08005496 /*
5497 * NOTE: some pers->run(), for example r5l_recovery_log(), wakes
5498 * up mddev->thread. It is important to initialize critical
5499 * resources for mddev->thread BEFORE calling pers->run().
5500 */
NeilBrown36d091f2014-12-15 12:56:58 +11005501 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005502 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005503 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005504 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005505 WARN_ONCE(!mddev->external_size,
5506 "%s: default size too small, but 'external_size' not in effect?\n",
5507 __func__);
5508 pr_warn("md: invalid array_size %llu > default size %llu\n",
5509 (unsigned long long)mddev->array_sectors / 2,
5510 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005511 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005512 }
NeilBrown36d091f2014-12-15 12:56:58 +11005513 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005514 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005515 struct bitmap *bitmap;
5516
5517 bitmap = bitmap_create(mddev, -1);
5518 if (IS_ERR(bitmap)) {
5519 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005520 pr_warn("%s: failed to create bitmap (%d)\n",
5521 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005522 } else
5523 mddev->bitmap = bitmap;
5524
NeilBrownb15c2e52006-01-06 00:20:16 -08005525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005527 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005528 if (mddev->private)
5529 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005530 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005531 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005532 bitmap_destroy(mddev);
5533 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 }
NeilBrown5c675f82014-12-15 12:56:56 +11005535 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005536 bool nonrot = true;
5537
5538 rdev_for_each(rdev, mddev) {
5539 if (rdev->raid_disk >= 0 &&
5540 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5541 nonrot = false;
5542 break;
5543 }
5544 }
5545 if (mddev->degraded)
5546 nonrot = false;
5547 if (nonrot)
5548 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5549 else
5550 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005551 mddev->queue->backing_dev_info->congested_data = mddev;
5552 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005553 }
NeilBrown36d091f2014-12-15 12:56:58 +11005554 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005555 if (mddev->kobj.sd &&
5556 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005557 pr_warn("md: cannot register extra attributes for %s\n",
5558 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005559 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005560 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005561 mddev->ro = 0;
5562
Robert Becker1e509152009-12-14 12:49:58 +11005563 atomic_set(&mddev->max_corr_read_errors,
5564 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005566 if (mddev_is_clustered(mddev))
5567 mddev->safemode_delay = 0;
5568 else
5569 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005571 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005572 spin_lock(&mddev->lock);
5573 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005574 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005575 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005576 if (rdev->raid_disk >= 0)
5577 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005578 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005579
NeilBrowna4a3d262015-07-17 11:57:30 +10005580 if (mddev->degraded && !mddev->ro)
5581 /* This ensures that recovering status is reported immediately
5582 * via sysfs - until a lack of spares is confirmed.
5583 */
5584 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005586
Shaohua Li29530792016-12-08 15:48:19 -08005587 if (mddev->sb_flags)
NeilBrown850b2b42006-10-03 01:15:46 -07005588 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589
NeilBrownd7603b72006-01-06 00:20:30 -08005590 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005591 sysfs_notify_dirent_safe(mddev->sysfs_state);
5592 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005593 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 return 0;
5595}
NeilBrown390ee602010-06-01 19:37:27 +10005596EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
NeilBrownfd01b882011-10-11 16:47:53 +11005598static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005599{
5600 int err;
5601
5602 err = md_run(mddev);
5603 if (err)
5604 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005605 err = bitmap_load(mddev);
5606 if (err) {
5607 bitmap_destroy(mddev);
5608 goto out;
5609 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005610
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005611 if (mddev_is_clustered(mddev))
5612 md_allow_write(mddev);
5613
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005614 md_wakeup_thread(mddev->thread);
5615 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5616
NeilBrownfe60b012010-03-29 11:10:42 +11005617 set_capacity(mddev->gendisk, mddev->array_sectors);
5618 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005619 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005620 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5621out:
5622 return err;
5623}
5624
NeilBrownfd01b882011-10-11 16:47:53 +11005625static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626{
5627 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10005628 struct md_rdev *rdev;
5629 bool has_journal = false;
5630 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631
Andre Noll80fab1d2008-07-11 22:02:21 +10005632 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005634 return -ENXIO;
5635 if (!mddev->pers)
5636 return -EINVAL;
5637 if (!mddev->ro)
5638 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005639
NeilBrown97b20ef2017-04-13 08:53:48 +10005640 rcu_read_lock();
5641 rdev_for_each_rcu(rdev, mddev) {
5642 if (test_bit(Journal, &rdev->flags) &&
5643 !test_bit(Faulty, &rdev->flags))
5644 has_journal = true;
5645 if (bdev_read_only(rdev->bdev))
5646 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07005647 }
NeilBrown97b20ef2017-04-13 08:53:48 +10005648 rcu_read_unlock();
5649 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
5650 /* Don't restart rw with journal missing/faulty */
5651 return -EINVAL;
5652 if (has_readonly)
5653 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07005654
Andre Noll80fab1d2008-07-11 22:02:21 +10005655 mddev->safemode = 0;
5656 mddev->ro = 0;
5657 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11005658 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10005659 /* Kick recovery or resync if necessary */
5660 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5661 md_wakeup_thread(mddev->thread);
5662 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005663 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665}
5666
NeilBrownfd01b882011-10-11 16:47:53 +11005667static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005668{
5669 mddev->array_sectors = 0;
5670 mddev->external_size = 0;
5671 mddev->dev_sectors = 0;
5672 mddev->raid_disks = 0;
5673 mddev->recovery_cp = 0;
5674 mddev->resync_min = 0;
5675 mddev->resync_max = MaxSector;
5676 mddev->reshape_position = MaxSector;
5677 mddev->external = 0;
5678 mddev->persistent = 0;
5679 mddev->level = LEVEL_NONE;
5680 mddev->clevel[0] = 0;
5681 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08005682 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005683 mddev->ro = 0;
5684 mddev->metadata_type[0] = 0;
5685 mddev->chunk_sectors = 0;
5686 mddev->ctime = mddev->utime = 0;
5687 mddev->layout = 0;
5688 mddev->max_disks = 0;
5689 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005690 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005691 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005692 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005693 mddev->new_level = LEVEL_NONE;
5694 mddev->new_layout = 0;
5695 mddev->new_chunk_sectors = 0;
5696 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005697 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005698 mddev->suspend_lo = mddev->suspend_hi = 0;
5699 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5700 mddev->recovery = 0;
5701 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005702 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005703 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005704 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005705 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005706 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005707 mddev->bitmap_info.offset = 0;
5708 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005709 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005710 mddev->bitmap_info.chunksize = 0;
5711 mddev->bitmap_info.daemon_sleep = 0;
5712 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005713 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005714}
5715
NeilBrownfd01b882011-10-11 16:47:53 +11005716static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005717{
NeilBrown6b6204e2013-05-09 09:48:30 +10005718 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005719 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005720 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005721 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005722 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005723 }
5724
5725 del_timer_sync(&mddev->safemode_timer);
5726
Shaohua Li034e33f2016-11-21 10:29:19 -08005727 if (mddev->pers && mddev->pers->quiesce) {
5728 mddev->pers->quiesce(mddev, 1);
5729 mddev->pers->quiesce(mddev, 0);
5730 }
NeilBrowna047e122010-03-29 12:07:53 +11005731 bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005732
NeilBrownb6d428c2013-04-24 11:42:42 +10005733 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005734 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08005735 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11005736 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005737 if (!mddev_is_clustered(mddev))
5738 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005739 md_update_sb(mddev, 1);
5740 }
5741}
NeilBrowndefad612011-01-14 09:14:33 +11005742
NeilBrownfd01b882011-10-11 16:47:53 +11005743void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005744{
NeilBrown29f097c2013-11-14 17:54:51 +11005745 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005746 __md_stop_writes(mddev);
5747 mddev_unlock(mddev);
5748}
NeilBrown390ee602010-06-01 19:37:27 +10005749EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005750
NeilBrown5aa61f42014-12-15 12:56:57 +11005751static void mddev_detach(struct mddev *mddev)
5752{
Guoqing Jiang48df4982017-03-14 09:40:20 +08005753 bitmap_wait_behind_writes(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005754 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005755 mddev->pers->quiesce(mddev, 1);
5756 mddev->pers->quiesce(mddev, 0);
5757 }
5758 md_unregister_thread(&mddev->thread);
5759 if (mddev->queue)
5760 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5761}
5762
NeilBrown5eff3c42012-11-19 10:47:48 +11005763static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005764{
NeilBrown36d091f2014-12-15 12:56:58 +11005765 struct md_personality *pers = mddev->pers;
Guoqing Jiang48df4982017-03-14 09:40:20 +08005766 bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11005767 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005768 /* Ensure ->event_work is done */
5769 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005770 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005771 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005772 spin_unlock(&mddev->lock);
5773 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005774 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005775 if (pers->sync_request && mddev->to_remove == NULL)
5776 mddev->to_remove = &md_redundancy_group;
5777 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005778 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005779}
NeilBrown5eff3c42012-11-19 10:47:48 +11005780
5781void md_stop(struct mddev *mddev)
5782{
5783 /* stop the array and free an attached data structures.
5784 * This is called from dm-raid
5785 */
5786 __md_stop(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005787 if (mddev->bio_set)
5788 bioset_free(mddev->bio_set);
5789}
5790
NeilBrown390ee602010-06-01 19:37:27 +10005791EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005792
NeilBrowna05b7ea2012-07-19 15:59:18 +10005793static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005794{
5795 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005796 int did_freeze = 0;
5797
5798 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5799 did_freeze = 1;
5800 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5801 md_wakeup_thread(mddev->thread);
5802 }
NeilBrownf851b602014-12-11 10:02:10 +11005803 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005804 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005805 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005806 /* Thread might be blocked waiting for metadata update
5807 * which will now never happen */
5808 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005809
Shaohua Li29530792016-12-08 15:48:19 -08005810 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10005811 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005812 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005813 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5814 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005815 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005816 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005817 mddev_lock_nointr(mddev);
5818
NeilBrowna4bd82d2010-03-29 13:23:10 +11005819 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005820 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005821 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005822 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005823 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005824 if (did_freeze) {
5825 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005826 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005827 md_wakeup_thread(mddev->thread);
5828 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005829 err = -EBUSY;
5830 goto out;
5831 }
5832 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005833 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005834
5835 err = -ENXIO;
5836 if (mddev->ro==1)
5837 goto out;
5838 mddev->ro = 1;
5839 set_disk_ro(mddev->gendisk, 1);
5840 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005841 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5842 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005843 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005844 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005845 }
5846out:
5847 mutex_unlock(&mddev->open_mutex);
5848 return err;
5849}
5850
NeilBrown9e653b62006-06-26 00:27:58 -07005851/* mode:
5852 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005853 * 2 - stop but do not disassemble array
5854 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005855static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005856 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005859 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005860 int did_freeze = 0;
5861
5862 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5863 did_freeze = 1;
5864 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5865 md_wakeup_thread(mddev->thread);
5866 }
NeilBrownf851b602014-12-11 10:02:10 +11005867 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005868 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005869 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005870 /* Thread might be blocked waiting for metadata update
5871 * which will now never happen */
5872 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005873
NeilBrown30b8feb2013-11-14 15:16:17 +11005874 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005875 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5876 !test_bit(MD_RECOVERY_RUNNING,
5877 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005878 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879
NeilBrownc8c00a62009-08-10 12:50:52 +10005880 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005881 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005882 mddev->sysfs_active ||
5883 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005884 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005885 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005886 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005887 if (did_freeze) {
5888 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005889 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005890 md_wakeup_thread(mddev->thread);
5891 }
NeilBrown260fa032013-08-27 16:44:13 +10005892 return -EBUSY;
5893 }
NeilBrown6e17b022010-08-07 21:41:19 +10005894 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005895 if (mddev->ro)
5896 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005897
NeilBrowndefad612011-01-14 09:14:33 +11005898 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005899 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01005900 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005901
NeilBrowna4bd82d2010-03-29 13:23:10 +11005902 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005903 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005904
NeilBrowndafb20f2012-03-19 12:46:39 +11005905 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005906 if (rdev->raid_disk >= 0)
5907 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005908
NeilBrowna4bd82d2010-03-29 13:23:10 +11005909 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005910 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005911 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005912 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005913
NeilBrowna4bd82d2010-03-29 13:23:10 +11005914 if (mddev->ro)
5915 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005916 } else
5917 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 /*
5919 * Free resources if final stop
5920 */
NeilBrown9e653b62006-06-26 00:27:58 -07005921 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005922 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
NeilBrownc3d97142009-12-14 12:49:52 +11005924 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005925 struct file *f = mddev->bitmap_info.file;
5926 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005927 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005928 spin_unlock(&mddev->lock);
5929 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005930 }
NeilBrownc3d97142009-12-14 12:49:52 +11005931 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005932
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 export_array(mddev);
5934
NeilBrown6177b472010-03-29 11:37:13 +11005935 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11005936 if (mddev->hold_active == UNTIL_STOP)
5937 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005938 }
NeilBrownd7603b72006-01-06 00:20:30 -08005939 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005940 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005941 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942}
5943
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005944#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005945static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946{
NeilBrown3cb03002011-10-11 16:45:26 +11005947 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 int err;
5949
NeilBrowna757e642005-04-16 15:26:42 -07005950 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
NeilBrown9d487392016-11-02 14:16:49 +11005953 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954
NeilBrowndafb20f2012-03-19 12:46:39 +11005955 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11005957 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 }
NeilBrown9d487392016-11-02 14:16:49 +11005959 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960
NeilBrownd710e132008-10-13 11:55:12 +11005961 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11005963 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005964 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 }
5966}
5967
5968/*
5969 * lets try to run arrays based on all disks that have arrived
5970 * until now. (those are in pending_raid_disks)
5971 *
5972 * the method: pick the first pending disk, collect all disks with
5973 * the same UUID, remove all from the pending list and put them into
5974 * the 'same_array' list. Then order this list based on superblock
5975 * update time (freshest comes first), kick out 'old' disks and
5976 * compare superblocks. If everything's fine then run it.
5977 *
5978 * If "unit" is allocated, then bump its reference count
5979 */
5980static void autorun_devices(int part)
5981{
NeilBrown3cb03002011-10-11 16:45:26 +11005982 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005983 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 char b[BDEVNAME_SIZE];
5985
NeilBrown9d487392016-11-02 14:16:49 +11005986 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005988 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005990 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005992 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993
NeilBrown9d487392016-11-02 14:16:49 +11005994 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005996 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005998 pr_debug("md: adding %s ...\n",
5999 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 list_move(&rdev->same_set, &candidates);
6001 }
6002 /*
6003 * now we have a set of devices, with all of them having
6004 * mostly sane superblocks. It's time to allocate the
6005 * mddev.
6006 */
NeilBrowne8703fe2006-10-03 01:15:59 -07006007 if (part) {
6008 dev = MKDEV(mdp_major,
6009 rdev0->preferred_minor << MdpMinorShift);
6010 unit = MINOR(dev) >> MdpMinorShift;
6011 } else {
6012 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6013 unit = MINOR(dev);
6014 }
6015 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006016 pr_warn("md: unit number in %s is bad: %d\n",
6017 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 break;
6019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
6021 md_probe(dev, NULL, NULL);
6022 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006023 if (!mddev || !mddev->gendisk) {
6024 if (mddev)
6025 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 break;
6027 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006028 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006029 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 else if (mddev->raid_disks || mddev->major_version
6031 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006032 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 mdname(mddev), bdevname(rdev0->bdev,b));
6034 mddev_unlock(mddev);
6035 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006036 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006037 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006038 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 list_del_init(&rdev->same_set);
6040 if (bind_rdev_to_array(rdev, mddev))
6041 export_rdev(rdev);
6042 }
6043 autorun_array(mddev);
6044 mddev_unlock(mddev);
6045 }
6046 /* on success, candidates will be empty, on error
6047 * it won't...
6048 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006049 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006050 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 mddev_put(mddev);
6054 }
NeilBrown9d487392016-11-02 14:16:49 +11006055 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006057#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058
NeilBrownf72ffdd2014-09-30 14:23:59 +10006059static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060{
6061 mdu_version_t ver;
6062
6063 ver.major = MD_MAJOR_VERSION;
6064 ver.minor = MD_MINOR_VERSION;
6065 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6066
6067 if (copy_to_user(arg, &ver, sizeof(ver)))
6068 return -EFAULT;
6069
6070 return 0;
6071}
6072
NeilBrownf72ffdd2014-09-30 14:23:59 +10006073static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074{
6075 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006076 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006077 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078
NeilBrown1ca69c42012-10-11 13:37:33 +11006079 nr = working = insync = failed = spare = 0;
6080 rcu_read_lock();
6081 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006083 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 failed++;
6085 else {
6086 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006087 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006088 insync++;
Song Liub347af82016-08-11 17:14:45 -07006089 else if (test_bit(Journal, &rdev->flags))
6090 /* TODO: add journal count to md_u.h */
6091 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 else
6093 spare++;
6094 }
6095 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006096 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097
6098 info.major_version = mddev->major_version;
6099 info.minor_version = mddev->minor_version;
6100 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006101 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006103 info.size = mddev->dev_sectors / 2;
6104 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006105 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 info.nr_disks = nr;
6107 info.raid_disks = mddev->raid_disks;
6108 info.md_minor = mddev->md_minor;
6109 info.not_persistent= !mddev->persistent;
6110
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006111 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 info.state = 0;
6113 if (mddev->in_sync)
6114 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006115 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006116 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006117 if (mddev_is_clustered(mddev))
6118 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006119 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 info.working_disks = working;
6121 info.failed_disks = failed;
6122 info.spare_disks = spare;
6123
6124 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006125 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126
6127 if (copy_to_user(arg, &info, sizeof(info)))
6128 return -EFAULT;
6129
6130 return 0;
6131}
6132
NeilBrownf72ffdd2014-09-30 14:23:59 +10006133static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006134{
6135 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006136 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006137 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006138
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006139 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006140 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006141 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006142
NeilBrown32a76272005-06-21 17:17:14 -07006143 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006144 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006145 /* bitmap enabled */
6146 if (mddev->bitmap_info.file) {
6147 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6148 sizeof(file->pathname));
6149 if (IS_ERR(ptr))
6150 err = PTR_ERR(ptr);
6151 else
6152 memmove(file->pathname, ptr,
6153 sizeof(file->pathname)-(ptr-file->pathname));
6154 }
NeilBrown4af1a042014-12-15 12:57:00 +11006155 spin_unlock(&mddev->lock);
6156
6157 if (err == 0 &&
6158 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006159 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006160
NeilBrown32a76272005-06-21 17:17:14 -07006161 kfree(file);
6162 return err;
6163}
6164
NeilBrownf72ffdd2014-09-30 14:23:59 +10006165static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166{
6167 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006168 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169
6170 if (copy_from_user(&info, arg, sizeof(info)))
6171 return -EFAULT;
6172
NeilBrown1ca69c42012-10-11 13:37:33 +11006173 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006174 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 if (rdev) {
6176 info.major = MAJOR(rdev->bdev->bd_dev);
6177 info.minor = MINOR(rdev->bdev->bd_dev);
6178 info.raid_disk = rdev->raid_disk;
6179 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006180 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006182 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 info.state |= (1<<MD_DISK_ACTIVE);
6184 info.state |= (1<<MD_DISK_SYNC);
6185 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006186 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006187 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006188 if (test_bit(WriteMostly, &rdev->flags))
6189 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006190 if (test_bit(FailFast, &rdev->flags))
6191 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 } else {
6193 info.major = info.minor = 0;
6194 info.raid_disk = -1;
6195 info.state = (1<<MD_DISK_REMOVED);
6196 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006197 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198
6199 if (copy_to_user(arg, &info, sizeof(info)))
6200 return -EFAULT;
6201
6202 return 0;
6203}
6204
NeilBrownf72ffdd2014-09-30 14:23:59 +10006205static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206{
6207 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006208 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 dev_t dev = MKDEV(info->major,info->minor);
6210
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006211 if (mddev_is_clustered(mddev) &&
6212 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006213 pr_warn("%s: Cannot add to clustered mddev.\n",
6214 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006215 return -EINVAL;
6216 }
6217
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6219 return -EOVERFLOW;
6220
6221 if (!mddev->raid_disks) {
6222 int err;
6223 /* expecting a device which has a superblock */
6224 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6225 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006226 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 PTR_ERR(rdev));
6228 return PTR_ERR(rdev);
6229 }
6230 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006231 struct md_rdev *rdev0
6232 = list_entry(mddev->disks.next,
6233 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006234 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 .load_super(rdev, rdev0, mddev->minor_version);
6236 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006237 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006238 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 bdevname(rdev0->bdev,b2));
6240 export_rdev(rdev);
6241 return -EINVAL;
6242 }
6243 }
6244 err = bind_rdev_to_array(rdev, mddev);
6245 if (err)
6246 export_rdev(rdev);
6247 return err;
6248 }
6249
6250 /*
6251 * add_new_disk can be used once the array is assembled
6252 * to add "hot spares". They must already have a superblock
6253 * written
6254 */
6255 if (mddev->pers) {
6256 int err;
6257 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006258 pr_warn("%s: personality does not support diskops!\n",
6259 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 return -EINVAL;
6261 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006262 if (mddev->persistent)
6263 rdev = md_import_device(dev, mddev->major_version,
6264 mddev->minor_version);
6265 else
6266 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006268 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 PTR_ERR(rdev));
6270 return PTR_ERR(rdev);
6271 }
NeilBrown1a855a02010-12-09 16:36:28 +11006272 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006273 if (!mddev->persistent) {
6274 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006275 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006276 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006277 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006278 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006279 } else
NeilBrown41158c72005-06-21 17:17:25 -07006280 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006281 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006282 } else
6283 super_types[mddev->major_version].
6284 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006285 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006286 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006287 /* This was a hot-add request, but events doesn't
6288 * match, so reject it.
6289 */
6290 export_rdev(rdev);
6291 return -EINVAL;
6292 }
6293
NeilBrownb2d444d2005-11-08 21:39:31 -08006294 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006295 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6296 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006297 else
6298 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006299 if (info->state & (1<<MD_DISK_FAILFAST))
6300 set_bit(FailFast, &rdev->flags);
6301 else
6302 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006303
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006304 if (info->state & (1<<MD_DISK_JOURNAL)) {
6305 struct md_rdev *rdev2;
6306 bool has_journal = false;
6307
6308 /* make sure no existing journal disk */
6309 rdev_for_each(rdev2, mddev) {
6310 if (test_bit(Journal, &rdev2->flags)) {
6311 has_journal = true;
6312 break;
6313 }
6314 }
6315 if (has_journal) {
6316 export_rdev(rdev);
6317 return -EBUSY;
6318 }
Song Liubac624f2015-08-13 14:31:55 -07006319 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006320 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006321 /*
6322 * check whether the device shows up in other nodes
6323 */
6324 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006325 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006326 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006327 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006328 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006329 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006330 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006331 export_rdev(rdev);
6332 return err;
6333 }
6334 }
6335 }
6336
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 rdev->raid_disk = -1;
6338 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006339
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 if (err)
6341 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006342
6343 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006344 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6345 if (!err) {
6346 err = md_cluster_ops->new_disk_ack(mddev,
6347 err == 0);
6348 if (err)
6349 md_kick_rdev_from_array(rdev);
6350 }
6351 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006352 if (err)
6353 md_cluster_ops->add_new_disk_cancel(mddev);
6354 else
6355 err = add_bound_rdev(rdev);
6356 }
6357
6358 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006359 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006360
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 return err;
6362 }
6363
6364 /* otherwise, add_new_disk is only allowed
6365 * for major_version==0 superblocks
6366 */
6367 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006368 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 return -EINVAL;
6370 }
6371
6372 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6373 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006374 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006376 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 PTR_ERR(rdev));
6378 return PTR_ERR(rdev);
6379 }
6380 rdev->desc_nr = info->number;
6381 if (info->raid_disk < mddev->raid_disks)
6382 rdev->raid_disk = info->raid_disk;
6383 else
6384 rdev->raid_disk = -1;
6385
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006387 if (info->state & (1<<MD_DISK_SYNC))
6388 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006390 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6391 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006392 if (info->state & (1<<MD_DISK_FAILFAST))
6393 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006394
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006396 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006397 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6398 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006399 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006400 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401
NeilBrown2bf071b2006-01-06 00:20:55 -08006402 err = bind_rdev_to_array(rdev, mddev);
6403 if (err) {
6404 export_rdev(rdev);
6405 return err;
6406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 }
6408
6409 return 0;
6410}
6411
NeilBrownf72ffdd2014-09-30 14:23:59 +10006412static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413{
6414 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006415 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 rdev = find_rdev(mddev, dev);
6418 if (!rdev)
6419 return -ENXIO;
6420
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006421 if (rdev->raid_disk < 0)
6422 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006423
NeilBrown3ea8929d2013-04-24 11:42:41 +10006424 clear_bit(Blocked, &rdev->flags);
6425 remove_and_add_spares(mddev, rdev);
6426
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 if (rdev->raid_disk >= 0)
6428 goto busy;
6429
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006430kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006431 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006432 md_cluster_ops->remove_disk(mddev, rdev);
6433
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006434 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006435 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006436 if (mddev->thread)
6437 md_wakeup_thread(mddev->thread);
6438 else
6439 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006440 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441
6442 return 0;
6443busy:
NeilBrown9d487392016-11-02 14:16:49 +11006444 pr_debug("md: cannot remove active disk %s from %s ...\n",
6445 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 return -EBUSY;
6447}
6448
NeilBrownf72ffdd2014-09-30 14:23:59 +10006449static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450{
6451 char b[BDEVNAME_SIZE];
6452 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006453 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454
6455 if (!mddev->pers)
6456 return -ENODEV;
6457
6458 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006459 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 mdname(mddev));
6461 return -EINVAL;
6462 }
6463 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006464 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 mdname(mddev));
6466 return -EINVAL;
6467 }
6468
NeilBrownd710e132008-10-13 11:55:12 +11006469 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006471 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 PTR_ERR(rdev));
6473 return -EINVAL;
6474 }
6475
6476 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006477 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006479 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480
NeilBrown8190e752009-06-18 08:48:58 +10006481 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482
NeilBrownb2d444d2005-11-08 21:39:31 -08006483 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006484 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485 bdevname(rdev->bdev,b), mdname(mddev));
6486 err = -EINVAL;
6487 goto abort_export;
6488 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006489
NeilBrownb2d444d2005-11-08 21:39:31 -08006490 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006492 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006493 err = bind_rdev_to_array(rdev, mddev);
6494 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006495 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496
6497 /*
6498 * The rest should better be atomic, we can have disk failures
6499 * noticed in interrupt contexts ...
6500 */
6501
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 rdev->raid_disk = -1;
6503
Shaohua Li29530792016-12-08 15:48:19 -08006504 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006505 if (!mddev->thread)
6506 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 /*
6508 * Kick recovery, maybe this spare has to be added to the
6509 * array immediately.
6510 */
6511 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6512 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006513 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514 return 0;
6515
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516abort_export:
6517 export_rdev(rdev);
6518 return err;
6519}
6520
NeilBrownfd01b882011-10-11 16:47:53 +11006521static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006522{
NeilBrown035328c2014-04-09 12:25:40 +10006523 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006524
NeilBrown36fa3062005-09-09 16:23:45 -07006525 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006526 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006527 return -EBUSY;
6528 if (mddev->recovery || mddev->sync_thread)
6529 return -EBUSY;
6530 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006531 }
6532
NeilBrown36fa3062005-09-09 16:23:45 -07006533 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006534 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006535 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006536
NeilBrown1e594bb2014-12-15 12:57:00 +11006537 if (mddev->bitmap || mddev->bitmap_info.file)
6538 return -EEXIST; /* cannot add when bitmap is present */
6539 f = fget(fd);
6540
6541 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006542 pr_warn("%s: error: failed to get bitmap file\n",
6543 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006544 return -EBADF;
6545 }
6546
NeilBrown1e594bb2014-12-15 12:57:00 +11006547 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006548 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006549 pr_warn("%s: error: bitmap file must be a regular file\n",
6550 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006551 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006552 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006553 pr_warn("%s: error: bitmap file must open for write\n",
6554 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006555 err = -EBADF;
6556 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006557 pr_warn("%s: error: bitmap file is already in use\n",
6558 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006559 err = -EBUSY;
6560 }
6561 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006562 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006563 return err;
6564 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006565 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006566 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006567 } else if (mddev->bitmap == NULL)
6568 return -ENOENT; /* cannot remove what isn't there */
6569 err = 0;
6570 if (mddev->pers) {
6571 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006572 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006573 struct bitmap *bitmap;
6574
6575 bitmap = bitmap_create(mddev, -1);
6576 if (!IS_ERR(bitmap)) {
6577 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006578 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006579 } else
6580 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006581 }
NeilBrownd7375ab322006-06-26 00:27:43 -07006582 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006583 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07006584 fd = -1; /* make sure to put the file */
6585 }
NeilBrown36fa3062005-09-09 16:23:45 -07006586 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07006587 }
6588 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006589 struct file *f = mddev->bitmap_info.file;
6590 if (f) {
6591 spin_lock(&mddev->lock);
6592 mddev->bitmap_info.file = NULL;
6593 spin_unlock(&mddev->lock);
6594 fput(f);
6595 }
NeilBrown36fa3062005-09-09 16:23:45 -07006596 }
6597
NeilBrown32a76272005-06-21 17:17:14 -07006598 return err;
6599}
6600
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601/*
6602 * set_array_info is used two different ways
6603 * The original usage is when creating a new array.
6604 * In this usage, raid_disks is > 0 and it together with
6605 * level, size, not_persistent,layout,chunksize determine the
6606 * shape of the array.
6607 * This will always create an array with a type-0.90.0 superblock.
6608 * The newer usage is when assembling an array.
6609 * In this case raid_disks will be 0, and the major_version field is
6610 * use to determine which style super-blocks are to be found on the devices.
6611 * The minor and patch _version numbers are also kept incase the
6612 * super_block handler wishes to interpret them.
6613 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006614static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615{
6616
6617 if (info->raid_disks == 0) {
6618 /* just setting version number for superblock loading */
6619 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006620 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621 super_types[info->major_version].name == NULL) {
6622 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11006623 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624 info->major_version);
6625 return -EINVAL;
6626 }
6627 mddev->major_version = info->major_version;
6628 mddev->minor_version = info->minor_version;
6629 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006630 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006631 /* ensure mddev_put doesn't delete this now that there
6632 * is some minimal configuration.
6633 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006634 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 return 0;
6636 }
6637 mddev->major_version = MD_MAJOR_VERSION;
6638 mddev->minor_version = MD_MINOR_VERSION;
6639 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006640 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641
6642 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006643 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006644 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 mddev->raid_disks = info->raid_disks;
6646 /* don't set md_minor, it is determined by which /dev/md* was
6647 * openned
6648 */
6649 if (info->state & (1<<MD_SB_CLEAN))
6650 mddev->recovery_cp = MaxSector;
6651 else
6652 mddev->recovery_cp = 0;
6653 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006654 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655
6656 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006657 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658
Shaohua Li29530792016-12-08 15:48:19 -08006659 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11006660 mddev->max_disks = MD_SB_DISKS;
6661 mddev->flags = 0;
6662 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006663 }
6664 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665
NeilBrownc3d97142009-12-14 12:49:52 +11006666 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006667 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006668 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006669
NeilBrownf6705572006-03-27 01:18:11 -08006670 mddev->reshape_position = MaxSector;
6671
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 /*
6673 * Generate a 128 bit UUID
6674 */
6675 get_random_bytes(mddev->uuid, 16);
6676
NeilBrownf6705572006-03-27 01:18:11 -08006677 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006678 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006679 mddev->new_layout = mddev->layout;
6680 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006681 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006682
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 return 0;
6684}
6685
NeilBrownfd01b882011-10-11 16:47:53 +11006686void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006687{
Dan Williamsb522adc2009-03-31 15:00:31 +11006688 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6689
6690 if (mddev->external_size)
6691 return;
6692
Dan Williams1f403622009-03-31 14:59:03 +11006693 mddev->array_sectors = array_sectors;
6694}
6695EXPORT_SYMBOL(md_set_array_sectors);
6696
NeilBrownfd01b882011-10-11 16:47:53 +11006697static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006698{
NeilBrown3cb03002011-10-11 16:45:26 +11006699 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006700 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006701 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08006702 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006703
NeilBrowna35b0d62006-01-06 00:20:49 -08006704 if (mddev->pers->resize == NULL)
6705 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006706 /* The "num_sectors" is the number of sectors of each device that
6707 * is used. This can only make sense for arrays with redundancy.
6708 * linear and raid0 always use whatever space is available. We can only
6709 * consider changing this number if no resync or reconstruction is
6710 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006711 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006712 * of each device. If num_sectors is zero, we find the largest size
6713 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006714 */
NeilBrownf851b602014-12-11 10:02:10 +11006715 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6716 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006717 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006718 if (mddev->ro)
6719 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006720
NeilBrowndafb20f2012-03-19 12:46:39 +11006721 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006722 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006723
Andre Nolld71f9f82008-07-11 22:02:22 +10006724 if (fit && (num_sectors == 0 || num_sectors > avail))
6725 num_sectors = avail;
6726 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006727 return -ENOSPC;
6728 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006729 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08006730 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08006731 if (mddev_is_clustered(mddev))
6732 md_cluster_ops->update_size(mddev, old_dev_sectors);
6733 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08006734 set_capacity(mddev->gendisk, mddev->array_sectors);
6735 revalidate_disk(mddev->gendisk);
6736 }
6737 }
NeilBrowna35b0d62006-01-06 00:20:49 -08006738 return rv;
6739}
6740
NeilBrownfd01b882011-10-11 16:47:53 +11006741static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006742{
6743 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006744 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006745 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006746 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006747 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006748 if (mddev->ro)
6749 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006750 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006751 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006752 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006753 if (mddev->sync_thread ||
6754 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6755 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006756 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006757
6758 rdev_for_each(rdev, mddev) {
6759 if (mddev->raid_disks < raid_disks &&
6760 rdev->data_offset < rdev->new_data_offset)
6761 return -EINVAL;
6762 if (mddev->raid_disks > raid_disks &&
6763 rdev->data_offset > rdev->new_data_offset)
6764 return -EINVAL;
6765 }
6766
NeilBrown63c70c42006-03-27 01:18:13 -08006767 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006768 if (mddev->delta_disks < 0)
6769 mddev->reshape_backwards = 1;
6770 else if (mddev->delta_disks > 0)
6771 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006772
6773 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006774 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006775 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006776 mddev->reshape_backwards = 0;
6777 }
NeilBrownda943b992006-01-06 00:20:54 -08006778 return rv;
6779}
6780
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781/*
6782 * update_array_info is used to change the configuration of an
6783 * on-line array.
6784 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6785 * fields in the info are checked against the array.
6786 * Any differences that cannot be handled will cause an error.
6787 * Normally, only one change can be managed at a time.
6788 */
NeilBrownfd01b882011-10-11 16:47:53 +11006789static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790{
6791 int rv = 0;
6792 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006793 int state = 0;
6794
6795 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006796 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006797 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798
6799 if (mddev->major_version != info->major_version ||
6800 mddev->minor_version != info->minor_version ||
6801/* mddev->patch_version != info->patch_version || */
6802 mddev->ctime != info->ctime ||
6803 mddev->level != info->level ||
6804/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006805 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006806 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006807 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6808 ((state^info->state) & 0xfffffe00)
6809 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 return -EINVAL;
6811 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006812 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6813 cnt++;
6814 if (mddev->raid_disks != info->raid_disks)
6815 cnt++;
6816 if (mddev->layout != info->layout)
6817 cnt++;
6818 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6819 cnt++;
6820 if (cnt == 0)
6821 return 0;
6822 if (cnt > 1)
6823 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824
6825 if (mddev->layout != info->layout) {
6826 /* Change layout
6827 * we don't need to do anything at the md level, the
6828 * personality will take care of it all.
6829 */
NeilBrown50ac1682009-06-18 08:47:55 +10006830 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006832 else {
6833 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006834 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006835 if (rv)
6836 mddev->new_layout = mddev->layout;
6837 return rv;
6838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006840 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006841 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842
NeilBrownda943b992006-01-06 00:20:54 -08006843 if (mddev->raid_disks != info->raid_disks)
6844 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845
NeilBrown36fa3062005-09-09 16:23:45 -07006846 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006847 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6848 rv = -EINVAL;
6849 goto err;
6850 }
6851 if (mddev->recovery || mddev->sync_thread) {
6852 rv = -EBUSY;
6853 goto err;
6854 }
NeilBrown36fa3062005-09-09 16:23:45 -07006855 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006856 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006857 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006858 if (mddev->bitmap) {
6859 rv = -EEXIST;
6860 goto err;
6861 }
6862 if (mddev->bitmap_info.default_offset == 0) {
6863 rv = -EINVAL;
6864 goto err;
6865 }
NeilBrownc3d97142009-12-14 12:49:52 +11006866 mddev->bitmap_info.offset =
6867 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006868 mddev->bitmap_info.space =
6869 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006870 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006871 bitmap = bitmap_create(mddev, -1);
6872 if (!IS_ERR(bitmap)) {
6873 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006874 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006875 } else
6876 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006877 if (rv)
6878 bitmap_destroy(mddev);
6879 mddev->pers->quiesce(mddev, 0);
6880 } else {
6881 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006882 if (!mddev->bitmap) {
6883 rv = -ENOENT;
6884 goto err;
6885 }
6886 if (mddev->bitmap->storage.file) {
6887 rv = -EINVAL;
6888 goto err;
6889 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006890 if (mddev->bitmap_info.nodes) {
6891 /* hold PW on all the bitmap lock */
6892 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006893 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 +11006894 rv = -EPERM;
6895 md_cluster_ops->unlock_all_bitmaps(mddev);
6896 goto err;
6897 }
6898
6899 mddev->bitmap_info.nodes = 0;
6900 md_cluster_ops->leave(mddev);
6901 }
NeilBrown36fa3062005-09-09 16:23:45 -07006902 mddev->pers->quiesce(mddev, 1);
6903 bitmap_destroy(mddev);
6904 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006905 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006906 }
6907 }
NeilBrown850b2b42006-10-03 01:15:46 -07006908 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006909 return rv;
6910err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 return rv;
6912}
6913
NeilBrownfd01b882011-10-11 16:47:53 +11006914static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915{
NeilBrown3cb03002011-10-11 16:45:26 +11006916 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006917 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918
6919 if (mddev->pers == NULL)
6920 return -ENODEV;
6921
NeilBrown1ca69c42012-10-11 13:37:33 +11006922 rcu_read_lock();
6923 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006925 err = -ENODEV;
6926 else {
6927 md_error(mddev, rdev);
6928 if (!test_bit(Faulty, &rdev->flags))
6929 err = -EBUSY;
6930 }
6931 rcu_read_unlock();
6932 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933}
6934
Andre Noll2f9618c2008-04-25 18:57:58 +02006935/*
6936 * We have a problem here : there is no easy way to give a CHS
6937 * virtual geometry. We currently pretend that we have a 2 heads
6938 * 4 sectors (with a BIG number of cylinders...). This drives
6939 * dosfs just mad... ;-)
6940 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006941static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6942{
NeilBrownfd01b882011-10-11 16:47:53 +11006943 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006944
6945 geo->heads = 2;
6946 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006947 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006948 return 0;
6949}
6950
Nicolas Schichancb335f82014-01-15 16:58:52 +01006951static inline bool md_ioctl_valid(unsigned int cmd)
6952{
6953 switch (cmd) {
6954 case ADD_NEW_DISK:
6955 case BLKROSET:
6956 case GET_ARRAY_INFO:
6957 case GET_BITMAP_FILE:
6958 case GET_DISK_INFO:
6959 case HOT_ADD_DISK:
6960 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006961 case RAID_AUTORUN:
6962 case RAID_VERSION:
6963 case RESTART_ARRAY_RW:
6964 case RUN_ARRAY:
6965 case SET_ARRAY_INFO:
6966 case SET_BITMAP_FILE:
6967 case SET_DISK_FAULTY:
6968 case STOP_ARRAY:
6969 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006970 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006971 return true;
6972 default:
6973 return false;
6974 }
6975}
6976
Al Viroa39907f2008-03-02 10:31:15 -05006977static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978 unsigned int cmd, unsigned long arg)
6979{
6980 int err = 0;
6981 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006982 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006983 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08006984 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985
Nicolas Schichancb335f82014-01-15 16:58:52 +01006986 if (!md_ioctl_valid(cmd))
6987 return -ENOTTY;
6988
NeilBrown506c9e42011-12-23 10:17:26 +11006989 switch (cmd) {
6990 case RAID_VERSION:
6991 case GET_ARRAY_INFO:
6992 case GET_DISK_INFO:
6993 break;
6994 default:
6995 if (!capable(CAP_SYS_ADMIN))
6996 return -EACCES;
6997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998
6999 /*
7000 * Commands dealing with the RAID driver but not any
7001 * particular array:
7002 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007003 switch (cmd) {
7004 case RAID_VERSION:
7005 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007006 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11007009 case RAID_AUTORUN:
7010 err = 0;
7011 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007012 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11007014 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015 }
7016
7017 /*
7018 * Commands creating/starting a new array:
7019 */
7020
Al Viroa39907f2008-03-02 10:31:15 -05007021 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022
7023 if (!mddev) {
7024 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007025 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 }
7027
NeilBrown1ca69c42012-10-11 13:37:33 +11007028 /* Some actions do not requires the mutex */
7029 switch (cmd) {
7030 case GET_ARRAY_INFO:
7031 if (!mddev->raid_disks && !mddev->external)
7032 err = -ENODEV;
7033 else
7034 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007035 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007036
7037 case GET_DISK_INFO:
7038 if (!mddev->raid_disks && !mddev->external)
7039 err = -ENODEV;
7040 else
7041 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007042 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007043
7044 case SET_DISK_FAULTY:
7045 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007046 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007047
7048 case GET_BITMAP_FILE:
7049 err = get_bitmap_file(mddev, argp);
7050 goto out;
7051
NeilBrown1ca69c42012-10-11 13:37:33 +11007052 }
7053
NeilBrowna7a3f082012-12-11 13:35:54 +11007054 if (cmd == ADD_NEW_DISK)
7055 /* need to ensure md_delayed_delete() has completed */
7056 flush_workqueue(md_misc_wq);
7057
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007058 if (cmd == HOT_REMOVE_DISK)
7059 /* need to ensure recovery thread has run */
7060 wait_event_interruptible_timeout(mddev->sb_wait,
7061 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007062 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007063 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007064 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7065 /* Need to flush page cache, and ensure no-one else opens
7066 * and writes
7067 */
7068 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007069 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007070 mutex_unlock(&mddev->open_mutex);
7071 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007072 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007073 }
NeilBrown065e5192017-04-06 11:16:33 +08007074 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007075 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007076 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007077 mutex_unlock(&mddev->open_mutex);
7078 sync_blockdev(bdev);
7079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 err = mddev_lock(mddev);
7081 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007082 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7083 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007084 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 }
7086
NeilBrownc02c0ae2012-12-11 13:39:21 +11007087 if (cmd == SET_ARRAY_INFO) {
7088 mdu_array_info_t info;
7089 if (!arg)
7090 memset(&info, 0, sizeof(info));
7091 else if (copy_from_user(&info, argp, sizeof(info))) {
7092 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007093 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007094 }
7095 if (mddev->pers) {
7096 err = update_array_info(mddev, &info);
7097 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007098 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007099 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007101 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007102 }
7103 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007104 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007105 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007106 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007107 }
7108 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007109 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007110 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007111 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007112 }
7113 err = set_array_info(mddev, &info);
7114 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007115 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007116 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007117 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007118 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119 }
7120
7121 /*
7122 * Commands querying/configuring an existing array:
7123 */
NeilBrown32a76272005-06-21 17:17:14 -07007124 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007125 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007126 if ((!mddev->raid_disks && !mddev->external)
7127 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7128 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7129 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007131 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132 }
7133
7134 /*
7135 * Commands even a read-only array can execute:
7136 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007137 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007138 case RESTART_ARRAY_RW:
7139 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007140 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007141
7142 case STOP_ARRAY:
7143 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007144 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007145
7146 case STOP_ARRAY_RO:
7147 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007148 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007149
NeilBrown3ea8929d2013-04-24 11:42:41 +10007150 case HOT_REMOVE_DISK:
7151 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007152 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007153
NeilBrown7ceb17e2013-04-24 11:42:42 +10007154 case ADD_NEW_DISK:
7155 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007156 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007157 * So require mddev->pers and MD_DISK_SYNC.
7158 */
7159 if (mddev->pers) {
7160 mdu_disk_info_t info;
7161 if (copy_from_user(&info, argp, sizeof(info)))
7162 err = -EFAULT;
7163 else if (!(info.state & (1<<MD_DISK_SYNC)))
7164 /* Need to clear read-only for this */
7165 break;
7166 else
7167 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007168 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007169 }
7170 break;
7171
NeilBrownc02c0ae2012-12-11 13:39:21 +11007172 case BLKROSET:
7173 if (get_user(ro, (int __user *)(arg))) {
7174 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007175 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007176 }
7177 err = -EINVAL;
7178
7179 /* if the bdev is going readonly the value of mddev->ro
7180 * does not matter, no writes are coming
7181 */
7182 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007183 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007184
NeilBrownc02c0ae2012-12-11 13:39:21 +11007185 /* are we are already prepared for writes? */
7186 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007187 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007188
7189 /* transitioning to readauto need only happen for
7190 * arrays that call md_write_start
7191 */
7192 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007194 if (err == 0) {
7195 mddev->ro = 2;
7196 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007197 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007198 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007199 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200 }
7201
7202 /*
7203 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007204 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205 */
NeilBrown326eb172014-09-30 15:36:28 +10007206 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007207 if (mddev->ro == 2) {
7208 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007209 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007210 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007211 /* mddev_unlock will wake thread */
7212 /* If a device failed while we were read-only, we
7213 * need to make sure the metadata is updated now.
7214 */
Shaohua Li29530792016-12-08 15:48:19 -08007215 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007216 mddev_unlock(mddev);
7217 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007218 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7219 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007220 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007221 }
NeilBrownf91de922005-11-08 21:39:36 -08007222 } else {
7223 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007224 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 }
7227
NeilBrownc02c0ae2012-12-11 13:39:21 +11007228 switch (cmd) {
7229 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007231 mdu_disk_info_t info;
7232 if (copy_from_user(&info, argp, sizeof(info)))
7233 err = -EFAULT;
7234 else
7235 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007236 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007239 case CLUSTERED_DISK_NACK:
7240 if (mddev_is_clustered(mddev))
7241 md_cluster_ops->new_disk_ack(mddev, false);
7242 else
7243 err = -EINVAL;
7244 goto unlock;
7245
NeilBrownc02c0ae2012-12-11 13:39:21 +11007246 case HOT_ADD_DISK:
7247 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007248 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249
NeilBrownc02c0ae2012-12-11 13:39:21 +11007250 case RUN_ARRAY:
7251 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007252 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253
NeilBrownc02c0ae2012-12-11 13:39:21 +11007254 case SET_BITMAP_FILE:
7255 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007256 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007257
NeilBrownc02c0ae2012-12-11 13:39:21 +11007258 default:
7259 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007260 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261 }
7262
NeilBrown3adc28d2014-09-30 15:46:41 +10007263unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007264 if (mddev->hold_active == UNTIL_IOCTL &&
7265 err != -EINVAL)
7266 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007268out:
NeilBrown065e5192017-04-06 11:16:33 +08007269 if(did_set_md_closing)
7270 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007271 return err;
7272}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007273#ifdef CONFIG_COMPAT
7274static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7275 unsigned int cmd, unsigned long arg)
7276{
7277 switch (cmd) {
7278 case HOT_REMOVE_DISK:
7279 case HOT_ADD_DISK:
7280 case SET_DISK_FAULTY:
7281 case SET_BITMAP_FILE:
7282 /* These take in integer arg, do not convert */
7283 break;
7284 default:
7285 arg = (unsigned long)compat_ptr(arg);
7286 break;
7287 }
7288
7289 return md_ioctl(bdev, mode, cmd, arg);
7290}
7291#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292
Al Viroa39907f2008-03-02 10:31:15 -05007293static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294{
7295 /*
7296 * Succeed if we can lock the mddev, which confirms that
7297 * it isn't being stopped right now.
7298 */
NeilBrownfd01b882011-10-11 16:47:53 +11007299 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300 int err;
7301
Yuanhan Liu0c098222012-05-22 13:55:32 +10007302 if (!mddev)
7303 return -ENODEV;
7304
NeilBrownd3374822009-01-09 08:31:10 +11007305 if (mddev->gendisk != bdev->bd_disk) {
7306 /* we are racing with mddev_put which is discarding this
7307 * bd_disk.
7308 */
7309 mddev_put(mddev);
7310 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007311 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007312 /* Then retry the open from the top */
7313 return -ERESTARTSYS;
7314 }
7315 BUG_ON(mddev != bdev->bd_disk->private_data);
7316
NeilBrownc8c00a62009-08-10 12:50:52 +10007317 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 goto out;
7319
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007320 if (test_bit(MD_CLOSING, &mddev->flags)) {
7321 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007322 err = -ENODEV;
7323 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007324 }
7325
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007327 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007328 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007330 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007332 if (err)
7333 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 return err;
7335}
7336
Al Virodb2a1442013-05-05 21:52:57 -04007337static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007339 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007341 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007342 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007345
7346static int md_media_changed(struct gendisk *disk)
7347{
NeilBrownfd01b882011-10-11 16:47:53 +11007348 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007349
7350 return mddev->changed;
7351}
7352
7353static int md_revalidate(struct gendisk *disk)
7354{
NeilBrownfd01b882011-10-11 16:47:53 +11007355 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007356
7357 mddev->changed = 0;
7358 return 0;
7359}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007360static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361{
7362 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007363 .open = md_open,
7364 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007365 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007366#ifdef CONFIG_COMPAT
7367 .compat_ioctl = md_compat_ioctl,
7368#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007369 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007370 .media_changed = md_media_changed,
7371 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372};
7373
NeilBrownf72ffdd2014-09-30 14:23:59 +10007374static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375{
NeilBrown2b8bf342011-10-11 16:48:23 +11007376 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378 /*
7379 * md_thread is a 'system-thread', it's priority should be very
7380 * high. We avoid resource deadlocks individually in each
7381 * raid personality. (RAID5 does preallocation) We also use RR and
7382 * the very same RT priority as kswapd, thus we will never get
7383 * into a priority inversion deadlock.
7384 *
7385 * we definitely have to have equal or higher priority than
7386 * bdflush, otherwise bdflush will deadlock if there are too
7387 * many dirty RAID5 blocks.
7388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389
NeilBrown6985c432005-10-19 21:23:47 -07007390 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007391 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392
NeilBrown93588e22005-11-15 00:09:12 -08007393 /* We need to wait INTERRUPTIBLE so that
7394 * we don't add to the load-average.
7395 * That means we need to be sure no signals are
7396 * pending
7397 */
7398 if (signal_pending(current))
7399 flush_signals(current);
7400
7401 wait_event_interruptible_timeout
7402 (thread->wqueue,
7403 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007404 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007405 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406
NeilBrown6c987912011-01-14 09:13:53 +11007407 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007408 if (kthread_should_park())
7409 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007410 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007411 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007413
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414 return 0;
7415}
7416
NeilBrown2b8bf342011-10-11 16:48:23 +11007417void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418{
7419 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007420 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
NeilBrown4ad23a972017-03-15 14:05:14 +11007421 if (!test_and_set_bit(THREAD_WAKEUP, &thread->flags))
7422 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423 }
7424}
NeilBrown6c144d32014-09-30 16:15:38 +10007425EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426
Shaohua Li4ed87312012-10-11 13:34:00 +11007427struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7428 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429{
NeilBrown2b8bf342011-10-11 16:48:23 +11007430 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431
NeilBrown2b8bf342011-10-11 16:48:23 +11007432 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 if (!thread)
7434 return NULL;
7435
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 init_waitqueue_head(&thread->wqueue);
7437
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438 thread->run = run;
7439 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007440 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007441 thread->tsk = kthread_run(md_thread, thread,
7442 "%s_%s",
7443 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007444 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007445 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446 kfree(thread);
7447 return NULL;
7448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449 return thread;
7450}
NeilBrown6c144d32014-09-30 16:15:38 +10007451EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452
NeilBrown2b8bf342011-10-11 16:48:23 +11007453void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454{
NeilBrown2b8bf342011-10-11 16:48:23 +11007455 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007456 if (!thread)
7457 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007458 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007459 /* Locking ensures that mddev_unlock does not wake_up a
7460 * non-existent thread
7461 */
7462 spin_lock(&pers_lock);
7463 *threadp = NULL;
7464 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007465
7466 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467 kfree(thread);
7468}
NeilBrown6c144d32014-09-30 16:15:38 +10007469EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470
NeilBrownfd01b882011-10-11 16:47:53 +11007471void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472{
NeilBrownb2d444d2005-11-08 21:39:31 -08007473 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007475
NeilBrownde393cd2011-07-28 11:31:48 +10007476 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477 return;
7478 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007479 if (mddev->degraded)
7480 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007481 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7483 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7484 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007485 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007486 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007487 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488}
NeilBrown6c144d32014-09-30 16:15:38 +10007489EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490
7491/* seq_file implementation /proc/mdstat */
7492
7493static void status_unused(struct seq_file *seq)
7494{
7495 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007496 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497
7498 seq_printf(seq, "unused devices: ");
7499
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007500 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501 char b[BDEVNAME_SIZE];
7502 i++;
7503 seq_printf(seq, "%s ",
7504 bdevname(rdev->bdev,b));
7505 }
7506 if (!i)
7507 seq_printf(seq, "<none>");
7508
7509 seq_printf(seq, "\n");
7510}
7511
NeilBrownf7851be2015-07-02 17:12:58 +10007512static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513{
NeilBrowndd71cf62009-05-07 12:49:35 +10007514 sector_t max_sectors, resync, res;
7515 unsigned long dt, db;
7516 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007517 int scale;
7518 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519
NeilBrownc804cde2012-05-21 09:28:33 +10007520 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7521 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007522 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007524 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525
NeilBrownf7851be2015-07-02 17:12:58 +10007526 resync = mddev->curr_resync;
7527 if (resync <= 3) {
7528 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7529 /* Still cleaning up */
7530 resync = max_sectors;
7531 } else
7532 resync -= atomic_read(&mddev->recovery_active);
7533
7534 if (resync == 0) {
7535 if (mddev->recovery_cp < MaxSector) {
7536 seq_printf(seq, "\tresync=PENDING");
7537 return 1;
7538 }
7539 return 0;
7540 }
7541 if (resync < 3) {
7542 seq_printf(seq, "\tresync=DELAYED");
7543 return 1;
7544 }
7545
NeilBrown403df472014-09-30 15:52:29 +10007546 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007547 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007548 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007549 * u32, as those are the requirements for sector_div.
7550 * Thus 'scale' must be at least 10
7551 */
7552 scale = 10;
7553 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007554 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007555 scale++;
7556 }
7557 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007558 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007559
7560 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561 {
NeilBrown4588b422006-03-27 01:18:04 -08007562 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563 seq_printf(seq, "[");
7564 for (i = 0; i < x; i++)
7565 seq_printf(seq, "=");
7566 seq_printf(seq, ">");
7567 for (i = 0; i < y; i++)
7568 seq_printf(seq, ".");
7569 seq_printf(seq, "] ");
7570 }
NeilBrown4588b422006-03-27 01:18:04 -08007571 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007572 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7573 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007574 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7575 "check" :
7576 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7577 "resync" : "recovery"))),
7578 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007579 (unsigned long long) resync/2,
7580 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581
7582 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 * dt: time from mark until now
7584 * db: blocks written from mark until now
7585 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007586 *
7587 * rt is a sector_t, so could be 32bit or 64bit.
7588 * So we divide before multiply in case it is 32bit and close
7589 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007590 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007591 * near the end of resync when the number of remaining sectors
7592 * is close to 'db'.
7593 * We then divide rt by 32 after multiplying by db to compensate.
7594 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595 */
7596 dt = ((jiffies - mddev->resync_mark) / HZ);
7597 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007598 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7599 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007600
NeilBrowndd71cf62009-05-07 12:49:35 +10007601 rt = max_sectors - resync; /* number of remaining sectors */
7602 sector_div(rt, db/32+1);
7603 rt *= dt;
7604 rt >>= 5;
7605
7606 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7607 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007608
NeilBrownff4e8d92006-07-10 04:44:16 -07007609 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007610 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007611}
7612
7613static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7614{
7615 struct list_head *tmp;
7616 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007617 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618
7619 if (l >= 0x10000)
7620 return NULL;
7621 if (!l--)
7622 /* header */
7623 return (void*)1;
7624
7625 spin_lock(&all_mddevs_lock);
7626 list_for_each(tmp,&all_mddevs)
7627 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007628 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629 mddev_get(mddev);
7630 spin_unlock(&all_mddevs_lock);
7631 return mddev;
7632 }
7633 spin_unlock(&all_mddevs_lock);
7634 if (!l--)
7635 return (void*)2;/* tail */
7636 return NULL;
7637}
7638
7639static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7640{
7641 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007642 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007643
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644 ++*pos;
7645 if (v == (void*)2)
7646 return NULL;
7647
7648 spin_lock(&all_mddevs_lock);
7649 if (v == (void*)1)
7650 tmp = all_mddevs.next;
7651 else
7652 tmp = mddev->all_mddevs.next;
7653 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007654 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655 else {
7656 next_mddev = (void*)2;
7657 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007659 spin_unlock(&all_mddevs_lock);
7660
7661 if (v != (void*)1)
7662 mddev_put(mddev);
7663 return next_mddev;
7664
7665}
7666
7667static void md_seq_stop(struct seq_file *seq, void *v)
7668{
NeilBrownfd01b882011-10-11 16:47:53 +11007669 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670
7671 if (mddev && v != (void*)1 && v != (void*)2)
7672 mddev_put(mddev);
7673}
7674
7675static int md_seq_show(struct seq_file *seq, void *v)
7676{
NeilBrownfd01b882011-10-11 16:47:53 +11007677 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007678 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007679 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680
7681 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007682 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683 seq_printf(seq, "Personalities : ");
7684 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007685 list_for_each_entry(pers, &pers_list, list)
7686 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007687
7688 spin_unlock(&pers_lock);
7689 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007690 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007691 return 0;
7692 }
7693 if (v == (void*)2) {
7694 status_unused(seq);
7695 return 0;
7696 }
7697
NeilBrown36d091f2014-12-15 12:56:58 +11007698 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7700 seq_printf(seq, "%s : %sactive", mdname(mddev),
7701 mddev->pers ? "" : "in");
7702 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007703 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007704 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007705 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007706 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007707 seq_printf(seq, " %s", mddev->pers->name);
7708 }
7709
Andre Nolldd8ac332009-03-31 14:33:13 +11007710 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007711 rcu_read_lock();
7712 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713 char b[BDEVNAME_SIZE];
7714 seq_printf(seq, " %s[%d]",
7715 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007716 if (test_bit(WriteMostly, &rdev->flags))
7717 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007718 if (test_bit(Journal, &rdev->flags))
7719 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007720 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007721 seq_printf(seq, "(F)");
7722 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007723 }
7724 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007725 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007726 if (test_bit(Replacement, &rdev->flags))
7727 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007728 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007730 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007731
7732 if (!list_empty(&mddev->disks)) {
7733 if (mddev->pers)
7734 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007735 (unsigned long long)
7736 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007737 else
7738 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007739 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007741 if (mddev->persistent) {
7742 if (mddev->major_version != 0 ||
7743 mddev->minor_version != 90) {
7744 seq_printf(seq," super %d.%d",
7745 mddev->major_version,
7746 mddev->minor_version);
7747 }
NeilBrowne6910632008-02-06 01:39:51 -08007748 } else if (mddev->external)
7749 seq_printf(seq, " super external:%s",
7750 mddev->metadata_type);
7751 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007752 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007753
7754 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007755 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007756 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007757 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007758 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007759 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007760 }
NeilBrown32a76272005-06-21 17:17:14 -07007761 } else
7762 seq_printf(seq, "\n ");
7763
NeilBrown57148962012-03-19 12:46:40 +11007764 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007765
7766 seq_printf(seq, "\n");
7767 }
NeilBrown36d091f2014-12-15 12:56:58 +11007768 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007769
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770 return 0;
7771}
7772
Jan Engelhardt110518b2009-05-07 12:49:37 +10007773static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774 .start = md_seq_start,
7775 .next = md_seq_next,
7776 .stop = md_seq_stop,
7777 .show = md_seq_show,
7778};
7779
7780static int md_seq_open(struct inode *inode, struct file *file)
7781{
Kay Sieversf1514632011-07-12 20:48:39 +02007782 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783 int error;
7784
7785 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007786 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007787 return error;
7788
7789 seq = file->private_data;
7790 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791 return error;
7792}
7793
NeilBrowne2f23b62014-04-09 14:33:51 +10007794static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007795static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7796{
Kay Sieversf1514632011-07-12 20:48:39 +02007797 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007798 int mask;
7799
NeilBrowne2f23b62014-04-09 14:33:51 +10007800 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007801 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007802 poll_wait(filp, &md_event_waiters, wait);
7803
7804 /* always allow read */
7805 mask = POLLIN | POLLRDNORM;
7806
Kay Sieversf1514632011-07-12 20:48:39 +02007807 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007808 mask |= POLLERR | POLLPRI;
7809 return mask;
7810}
7811
Arjan van de Venfa027c22007-02-12 00:55:33 -08007812static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007813 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007814 .open = md_seq_open,
7815 .read = seq_read,
7816 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007817 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007818 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007819};
7820
NeilBrown84fc4b52011-10-11 16:49:58 +11007821int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822{
NeilBrown9d487392016-11-02 14:16:49 +11007823 pr_debug("md: %s personality registered for level %d\n",
7824 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007826 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827 spin_unlock(&pers_lock);
7828 return 0;
7829}
NeilBrown6c144d32014-09-30 16:15:38 +10007830EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007831
NeilBrown84fc4b52011-10-11 16:49:58 +11007832int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833{
NeilBrown9d487392016-11-02 14:16:49 +11007834 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007836 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837 spin_unlock(&pers_lock);
7838 return 0;
7839}
NeilBrown6c144d32014-09-30 16:15:38 +10007840EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841
NeilBrown6022e752015-08-13 12:32:55 +10007842int register_md_cluster_operations(struct md_cluster_operations *ops,
7843 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007844{
NeilBrown6022e752015-08-13 12:32:55 +10007845 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007846 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007847 if (md_cluster_ops != NULL)
7848 ret = -EALREADY;
7849 else {
7850 md_cluster_ops = ops;
7851 md_cluster_mod = module;
7852 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007853 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007854 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007855}
7856EXPORT_SYMBOL(register_md_cluster_operations);
7857
7858int unregister_md_cluster_operations(void)
7859{
7860 spin_lock(&pers_lock);
7861 md_cluster_ops = NULL;
7862 spin_unlock(&pers_lock);
7863 return 0;
7864}
7865EXPORT_SYMBOL(unregister_md_cluster_operations);
7866
7867int md_setup_cluster(struct mddev *mddev, int nodes)
7868{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007869 if (!md_cluster_ops)
7870 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007871 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007872 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007873 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11007874 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007875 spin_unlock(&pers_lock);
7876 return -ENOENT;
7877 }
7878 spin_unlock(&pers_lock);
7879
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007880 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007881}
7882
7883void md_cluster_stop(struct mddev *mddev)
7884{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007885 if (!md_cluster_ops)
7886 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007887 md_cluster_ops->leave(mddev);
7888 module_put(md_cluster_mod);
7889}
7890
NeilBrownfd01b882011-10-11 16:47:53 +11007891static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007893 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007895 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007896
7897 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007898 rcu_read_lock();
7899 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007900 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007901 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7902 (int)part_stat_read(&disk->part0, sectors[1]) -
7903 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007904 /* sync IO will cause sync_io to increase before the disk_stats
7905 * as sync_io is counted when a request starts, and
7906 * disk_stats is counted when it completes.
7907 * So resync activity will cause curr_events to be smaller than
7908 * when there was no such activity.
7909 * non-sync IO will cause disk_stat to increase without
7910 * increasing sync_io so curr_events will (eventually)
7911 * be larger than it was before. Once it becomes
7912 * substantially larger, the test below will cause
7913 * the array to appear non-idle, and resync will slow
7914 * down.
7915 * If there is a lot of outstanding resync activity when
7916 * we set last_event to curr_events, then all that activity
7917 * completing might cause the array to appear non-idle
7918 * and resync will be slowed down even though there might
7919 * not have been non-resync activity. This will only
7920 * happen once though. 'last_events' will soon reflect
7921 * the state where there is little or no outstanding
7922 * resync requests, and further resync activity will
7923 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007924 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007926 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927 rdev->last_events = curr_events;
7928 idle = 0;
7929 }
7930 }
NeilBrown4b809912008-07-21 17:05:25 +10007931 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932 return idle;
7933}
7934
NeilBrownfd01b882011-10-11 16:47:53 +11007935void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936{
7937 /* another "blocks" (512byte) blocks have been synced */
7938 atomic_sub(blocks, &mddev->recovery_active);
7939 wake_up(&mddev->recovery_wait);
7940 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007941 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007942 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007943 md_wakeup_thread(mddev->thread);
7944 // stop recovery, signal do_sync ....
7945 }
7946}
NeilBrown6c144d32014-09-30 16:15:38 +10007947EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948
NeilBrown06d91a52005-06-21 17:17:12 -07007949/* md_write_start(mddev, bi)
7950 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007951 * in superblock) before writing, schedule a superblock update
7952 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007953 */
NeilBrownfd01b882011-10-11 16:47:53 +11007954void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007955{
Neil Brown0fd62b82008-06-28 08:31:36 +10007956 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007957 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007958 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007959
NeilBrownf91de922005-11-08 21:39:36 -08007960 BUG_ON(mddev->ro == 1);
7961 if (mddev->ro == 2) {
7962 /* need to switch to read/write */
7963 mddev->ro = 0;
7964 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7965 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007966 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007967 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007968 }
NeilBrown4ad23a972017-03-15 14:05:14 +11007969 rcu_read_lock();
7970 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11007971 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07007972 if (mddev->safemode == 1)
7973 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11007974 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
7975 if (mddev->in_sync || !mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11007976 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007977 if (mddev->in_sync) {
7978 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08007979 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
7980 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007981 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007982 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007983 }
NeilBrown85572d72014-12-15 12:56:56 +11007984 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007985 }
NeilBrown4ad23a972017-03-15 14:05:14 +11007986 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10007987 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007988 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007989 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007990 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991}
NeilBrown6c144d32014-09-30 16:15:38 +10007992EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993
NeilBrown49728052017-03-15 14:05:12 +11007994/* md_write_inc can only be called when md_write_start() has
7995 * already been called at least once of the current request.
7996 * It increments the counter and is useful when a single request
7997 * is split into several parts. Each part causes an increment and
7998 * so needs a matching md_write_end().
7999 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8000 * a spinlocked region.
8001 */
8002void md_write_inc(struct mddev *mddev, struct bio *bi)
8003{
8004 if (bio_data_dir(bi) != WRITE)
8005 return;
8006 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008007 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008008}
8009EXPORT_SYMBOL(md_write_inc);
8010
NeilBrownfd01b882011-10-11 16:47:53 +11008011void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008012{
NeilBrown4ad23a972017-03-15 14:05:14 +11008013 percpu_ref_put(&mddev->writes_pending);
8014
8015 if (mddev->safemode == 2)
8016 md_wakeup_thread(mddev->thread);
8017 else if (mddev->safemode_delay)
8018 /* The roundup() ensures this only performs locking once
8019 * every ->safemode_delay jiffies
8020 */
8021 mod_timer(&mddev->safemode_timer,
8022 roundup(jiffies, mddev->safemode_delay) +
8023 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024}
NeilBrown4ad23a972017-03-15 14:05:14 +11008025
NeilBrown6c144d32014-09-30 16:15:38 +10008026EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027
NeilBrown2a2275d2007-01-26 00:57:11 -08008028/* md_allow_write(mddev)
8029 * Calling this ensures that the array is marked 'active' so that writes
8030 * may proceed without blocking. It is important to call this before
8031 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8032 * Must be called with mddev_lock held.
8033 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008034void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008035{
8036 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008037 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008038 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008039 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008040 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008041 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008042
NeilBrown85572d72014-12-15 12:56:56 +11008043 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008044 if (mddev->in_sync) {
8045 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008046 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8047 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008048 if (mddev->safemode_delay &&
8049 mddev->safemode == 0)
8050 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008051 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008052 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008053 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008054 /* wait for the dirty state to be recorded in the metadata */
8055 wait_event(mddev->sb_wait,
8056 !test_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags) &&
8057 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008058 } else
NeilBrown85572d72014-12-15 12:56:56 +11008059 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008060}
8061EXPORT_SYMBOL_GPL(md_allow_write);
8062
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063#define SYNC_MARKS 10
8064#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008065#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008066void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008067{
Shaohua Li4ed87312012-10-11 13:34:00 +11008068 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008069 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070 unsigned int currspeed = 0,
8071 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008072 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008074 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008075 sector_t mark_cnt[SYNC_MARKS];
8076 int last_mark,m;
8077 struct list_head *tmp;
8078 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008079 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008080 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008081 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008082 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008083 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008084
8085 /* just incase thread restarts... */
8086 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
8087 return;
NeilBrown3991b312014-05-28 13:39:23 +10008088 if (mddev->ro) {/* never try to sync a read-only array */
8089 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008090 return;
NeilBrown3991b312014-05-28 13:39:23 +10008091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008093 if (mddev_is_clustered(mddev)) {
8094 ret = md_cluster_ops->resync_start(mddev);
8095 if (ret)
8096 goto skip;
8097
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008098 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008099 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8100 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8101 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8102 && ((unsigned long long)mddev->curr_resync_completed
8103 < (unsigned long long)mddev->resync_max_sectors))
8104 goto skip;
8105 }
8106
NeilBrown61df9d92006-10-03 01:15:57 -07008107 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008108 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008109 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008110 action = "check";
8111 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008112 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008113 action = "repair";
8114 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008115 desc = "resync";
8116 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8117 desc = "reshape";
8118 else
8119 desc = "recovery";
8120
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008121 mddev->last_sync_action = action ?: desc;
8122
Linus Torvalds1da177e2005-04-16 15:20:36 -07008123 /* we overload curr_resync somewhat here.
8124 * 0 == not engaged in resync at all
8125 * 2 == checking that there is no conflict with another sync
8126 * 1 == like 2, but have yielded to allow conflicting resync to
8127 * commense
8128 * other == active in resync - this many blocks
8129 *
8130 * Before starting a resync we must have set curr_resync to
8131 * 2, and then checked that every "conflicting" array has curr_resync
8132 * less than ours. When we find one that is the same or higher
8133 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8134 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8135 * This will mean we have to start checking from the beginning again.
8136 *
8137 */
8138
8139 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008140 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008141 mddev->curr_resync = 2;
8142
8143 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008144 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008145 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008146 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008147 if (mddev2 == mddev)
8148 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008149 if (!mddev->parallel_resync
8150 && mddev2->curr_resync
8151 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152 DEFINE_WAIT(wq);
8153 if (mddev < mddev2 && mddev->curr_resync == 2) {
8154 /* arbitrarily yield */
8155 mddev->curr_resync = 1;
8156 wake_up(&resync_wait);
8157 }
8158 if (mddev > mddev2 && mddev->curr_resync == 1)
8159 /* no need to wait here, we can wait the next
8160 * time 'round when curr_resync == 2
8161 */
8162 continue;
NeilBrown97441972008-09-19 11:49:54 +10008163 /* We need to wait 'interruptible' so as not to
8164 * contribute to the load average, and not to
8165 * be caught by 'softlockup'
8166 */
8167 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008168 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008169 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008170 if (mddev2_minor != mddev2->md_minor) {
8171 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008172 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8173 desc, mdname(mddev),
8174 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008176 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008177 if (signal_pending(current))
8178 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008179 schedule();
8180 finish_wait(&resync_wait, &wq);
8181 goto try_again;
8182 }
8183 finish_wait(&resync_wait, &wq);
8184 }
8185 }
8186 } while (mddev->curr_resync < 2);
8187
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008188 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008189 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008190 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008191 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192 */
8193 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008194 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008195 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008196 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8197 j = mddev->resync_min;
8198 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008199 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008200
NeilBrownccfcc3c2006-03-27 01:18:09 -08008201 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10008202 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008203 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008204 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008205 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008206 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008207 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008208 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008209 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008210 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008211 !test_bit(Faulty, &rdev->flags) &&
8212 !test_bit(In_sync, &rdev->flags) &&
8213 rdev->recovery_offset < j)
8214 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008215 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008216
8217 /* If there is a bitmap, we need to make sure all
8218 * writes that started before we added a spare
8219 * complete before we start doing a recovery.
8220 * Otherwise the write might complete and (via
8221 * bitmap_endwrite) set a bit in the bitmap after the
8222 * recovery has checked that bit and skipped that
8223 * region.
8224 */
8225 if (mddev->bitmap) {
8226 mddev->pers->quiesce(mddev, 1);
8227 mddev->pers->quiesce(mddev, 0);
8228 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008230
NeilBrown9d487392016-11-02 14:16:49 +11008231 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8232 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8233 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8234 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008235
NeilBrowneea1bf32009-03-31 14:27:02 +11008236 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008237
NeilBrown57afd892005-06-21 17:17:13 -07008238 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008239 for (m = 0; m < SYNC_MARKS; m++) {
8240 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008241 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242 }
8243 last_mark = 0;
8244 mddev->resync_mark = mark[last_mark];
8245 mddev->resync_mark_cnt = mark_cnt[last_mark];
8246
8247 /*
8248 * Tune reconstruction:
8249 */
8250 window = 32*(PAGE_SIZE/512);
NeilBrown9d487392016-11-02 14:16:49 +11008251 pr_debug("md: using %dk window, over a total of %lluk.\n",
8252 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008253
8254 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008255 last_check = 0;
8256
8257 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008258 pr_debug("md: resuming %s of %s from checkpoint.\n",
8259 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008260 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008261 } else
8262 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008263 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008264 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8265 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008266 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008267
majianpeng7c2c57c2012-07-03 12:12:26 +10008268 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008270 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008271
NeilBrown57afd892005-06-21 17:17:13 -07008272 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008273
NeilBrown7a91ee12009-05-26 12:57:21 +10008274 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8275 ((mddev->curr_resync > mddev->curr_resync_completed &&
8276 (mddev->curr_resync - mddev->curr_resync_completed)
8277 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008278 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008279 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008280 >= mddev->resync_max - mddev->curr_resync_completed ||
8281 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008282 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008283 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008284 wait_event(mddev->recovery_wait,
8285 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008286 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008287 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8288 j > mddev->recovery_cp)
8289 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008290 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008291 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008292 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008293 }
NeilBrownacb180b2009-04-14 16:28:34 +10008294
NeilBrownc91abf52013-11-19 12:02:01 +11008295 while (j >= mddev->resync_max &&
8296 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008297 /* As this condition is controlled by user-space,
8298 * we can block indefinitely, so use '_interruptible'
8299 * to avoid triggering warnings.
8300 */
8301 flush_signals(current); /* just in case */
8302 wait_event_interruptible(mddev->recovery_wait,
8303 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008304 || test_bit(MD_RECOVERY_INTR,
8305 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008306 }
NeilBrownacb180b2009-04-14 16:28:34 +10008307
NeilBrownc91abf52013-11-19 12:02:01 +11008308 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8309 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008310
NeilBrown09314792015-02-19 16:04:40 +11008311 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008312 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008313 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008314 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008315 }
NeilBrown57afd892005-06-21 17:17:13 -07008316
8317 if (!skipped) { /* actual IO requested */
8318 io_sectors += sectors;
8319 atomic_add(sectors, &mddev->recovery_active);
8320 }
8321
NeilBrowne875ece2011-07-28 11:39:24 +10008322 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8323 break;
8324
Linus Torvalds1da177e2005-04-16 15:20:36 -07008325 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008326 if (j > max_sectors)
8327 /* when skipping, extra large numbers can be returned. */
8328 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008329 if (j > 2)
8330 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008331 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008332 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008333 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008334 * visible in /proc/mdstat
8335 */
8336 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008337
8338 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339 continue;
8340
NeilBrown57afd892005-06-21 17:17:13 -07008341 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008342 repeat:
8343 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8344 /* step marks */
8345 int next = (last_mark+1) % SYNC_MARKS;
8346
8347 mddev->resync_mark = mark[next];
8348 mddev->resync_mark_cnt = mark_cnt[next];
8349 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008350 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008351 last_mark = next;
8352 }
8353
NeilBrownc91abf52013-11-19 12:02:01 +11008354 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8355 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008356
8357 /*
8358 * this loop exits only if either when we are slower than
8359 * the 'hard' speed limit, or the system was IO-idle for
8360 * a jiffy.
8361 * the system might be non-idle CPU-wise, but we only care
8362 * about not overloading the IO subsystem. (things like an
8363 * e2fsck being done on the RAID array should execute fast)
8364 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365 cond_resched();
8366
Xiao Niac7e50a2014-08-07 09:37:41 -04008367 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8368 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008369 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008370
NeilBrown88202a02006-01-06 00:21:36 -08008371 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008372 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008373 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374 goto repeat;
8375 }
NeilBrownac8fa412015-02-19 16:55:00 +11008376 if (!is_mddev_idle(mddev, 0)) {
8377 /*
8378 * Give other IO more of a chance.
8379 * The faster the devices, the less we wait.
8380 */
8381 wait_event(mddev->recovery_wait,
8382 !atomic_read(&mddev->recovery_active));
8383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384 }
8385 }
NeilBrown9d487392016-11-02 14:16:49 +11008386 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8387 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8388 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008389 /*
8390 * this also signals 'finished resyncing' to md_stop
8391 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008392 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008393 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8394
NeilBrown5ed1df22015-07-24 13:27:08 +10008395 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8396 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008397 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008398 mddev->curr_resync_completed = mddev->curr_resync;
8399 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8400 }
NeilBrown09314792015-02-19 16:04:40 +11008401 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402
NeilBrowndfc70642008-05-23 13:04:39 -07008403 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008404 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008405 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8406 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8407 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008408 pr_debug("md: checkpointing %s of %s.\n",
8409 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008410 if (test_bit(MD_RECOVERY_ERROR,
8411 &mddev->recovery))
8412 mddev->recovery_cp =
8413 mddev->curr_resync_completed;
8414 else
8415 mddev->recovery_cp =
8416 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008417 }
8418 } else
8419 mddev->recovery_cp = MaxSector;
8420 } else {
8421 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8422 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008423 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008424 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008425 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008426 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008427 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008428 !test_bit(Faulty, &rdev->flags) &&
8429 !test_bit(In_sync, &rdev->flags) &&
8430 rdev->recovery_offset < mddev->curr_resync)
8431 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008432 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008434 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008435 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008436 /* set CHANGE_PENDING here since maybe another update is needed,
8437 * so other nodes are informed. It should be harmless for normal
8438 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008439 set_mask_bits(&mddev->sb_flags, 0,
8440 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008441
NeilBrown23da4222014-12-15 12:57:01 +11008442 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008443 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8444 /* We completed so min/max setting can be forgotten if used. */
8445 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8446 mddev->resync_min = 0;
8447 mddev->resync_max = MaxSector;
8448 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8449 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008450 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008452 spin_unlock(&mddev->lock);
8453
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008455 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008456 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008457}
NeilBrown29269552006-03-27 01:18:10 -08008458EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008459
NeilBrown746d3202013-04-24 11:42:41 +10008460static int remove_and_add_spares(struct mddev *mddev,
8461 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008462{
NeilBrown3cb03002011-10-11 16:45:26 +11008463 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008464 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008465 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008466 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008467
NeilBrownd787be42016-06-02 16:19:53 +10008468 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008469 if ((this == NULL || rdev == this) &&
8470 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008471 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008472 test_bit(Faulty, &rdev->flags) &&
8473 atomic_read(&rdev->nr_pending)==0) {
8474 /* Faulty non-Blocked devices with nr_pending == 0
8475 * never get nr_pending incremented,
8476 * never get Faulty cleared, and never get Blocked set.
8477 * So we can synchronize_rcu now rather than once per device
8478 */
8479 remove_some = true;
8480 set_bit(RemoveSynchronized, &rdev->flags);
8481 }
8482 }
8483
8484 if (remove_some)
8485 synchronize_rcu();
8486 rdev_for_each(rdev, mddev) {
8487 if ((this == NULL || rdev == this) &&
8488 rdev->raid_disk >= 0 &&
8489 !test_bit(Blocked, &rdev->flags) &&
8490 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008491 (!test_bit(In_sync, &rdev->flags) &&
8492 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008493 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008494 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008495 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008496 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008497 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008498 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008499 }
8500 }
NeilBrownd787be42016-06-02 16:19:53 +10008501 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8502 clear_bit(RemoveSynchronized, &rdev->flags);
8503 }
8504
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008505 if (removed && mddev->kobj.sd)
8506 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008507
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008508 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008509 goto no_add;
8510
NeilBrowndafb20f2012-03-19 12:46:39 +11008511 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008512 if (this && this != rdev)
8513 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008514 if (test_bit(Candidate, &rdev->flags))
8515 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008516 if (rdev->raid_disk >= 0 &&
8517 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008518 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008519 !test_bit(Faulty, &rdev->flags))
8520 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008521 if (rdev->raid_disk >= 0)
8522 continue;
8523 if (test_bit(Faulty, &rdev->flags))
8524 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008525 if (!test_bit(Journal, &rdev->flags)) {
8526 if (mddev->ro &&
8527 ! (rdev->saved_raid_disk >= 0 &&
8528 !test_bit(Bitmap_sync, &rdev->flags)))
8529 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008530
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008531 rdev->recovery_offset = 0;
8532 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008533 if (mddev->pers->
8534 hot_add_disk(mddev, rdev) == 0) {
8535 if (sysfs_link_rdev(mddev, rdev))
8536 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008537 if (!test_bit(Journal, &rdev->flags))
8538 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008539 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08008540 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008541 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008542 }
NeilBrown746d3202013-04-24 11:42:41 +10008543no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008544 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08008545 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008546 return spares;
8547}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008548
NeilBrownac05f252014-09-30 08:10:42 +10008549static void md_start_sync(struct work_struct *ws)
8550{
8551 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008552
NeilBrownac05f252014-09-30 08:10:42 +10008553 mddev->sync_thread = md_register_thread(md_do_sync,
8554 mddev,
8555 "resync");
8556 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11008557 pr_warn("%s: could not start resync thread...\n",
8558 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008559 /* leave the spares where they are, it shouldn't hurt */
8560 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8561 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8562 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8563 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8564 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008565 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008566 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8567 &mddev->recovery))
8568 if (mddev->sysfs_action)
8569 sysfs_notify_dirent_safe(mddev->sysfs_action);
8570 } else
8571 md_wakeup_thread(mddev->sync_thread);
8572 sysfs_notify_dirent_safe(mddev->sysfs_action);
8573 md_new_event(mddev);
8574}
8575
Linus Torvalds1da177e2005-04-16 15:20:36 -07008576/*
8577 * This routine is regularly called by all per-raid-array threads to
8578 * deal with generic issues like resync and super-block update.
8579 * Raid personalities that don't have a thread (linear/raid0) do not
8580 * need this as they never do any recovery or update the superblock.
8581 *
8582 * It does not do any resync itself, but rather "forks" off other threads
8583 * to do that as needed.
8584 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8585 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008586 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008587 * and wakeups up this thread which will reap the thread and finish up.
8588 * This thread also removes any faulty devices (with nr_pending == 0).
8589 *
8590 * The overall approach is:
8591 * 1/ if the superblock needs updating, update it.
8592 * 2/ If a recovery thread is running, don't do anything else.
8593 * 3/ If recovery has finished, clean up, possibly marking spares active.
8594 * 4/ If there are any faulty devices, remove them.
8595 * 5/ If array is degraded, try to add spares devices
8596 * 6/ If array has spares or is not in-sync, start a resync thread.
8597 */
NeilBrownfd01b882011-10-11 16:47:53 +11008598void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008599{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008600 if (mddev->suspended)
8601 return;
8602
NeilBrown5f404022005-06-21 17:17:16 -07008603 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008604 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008605
NeilBrownfca4d842005-06-21 17:17:11 -07008606 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008607 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11008608 pr_debug("md: %s in immediate safe mode\n",
8609 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07008610 mddev->safemode = 2;
8611 }
8612 flush_signals(current);
8613 }
8614
NeilBrownc89a8ee2008-08-05 15:54:13 +10008615 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8616 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008617 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08008618 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008619 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008620 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008621 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11008622 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07008623 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008624 ))
8625 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008626
NeilBrowndf5b89b2006-03-27 01:18:20 -08008627 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008628 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008629
NeilBrownc89a8ee2008-08-05 15:54:13 +10008630 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008631 struct md_rdev *rdev;
8632 if (!mddev->external && mddev->in_sync)
8633 /* 'Blocked' flag not needed as failed devices
8634 * will be recorded if array switched to read/write.
8635 * Leaving it set will prevent the device
8636 * from being removed.
8637 */
8638 rdev_for_each(rdev, mddev)
8639 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008640 /* On a read-only array we can:
8641 * - remove failed devices
8642 * - add already-in_sync devices if the array itself
8643 * is in-sync.
8644 * As we only add devices that are already in-sync,
8645 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008646 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008647 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008648 /* There is no thread, but we need to call
8649 * ->spare_active and clear saved_raid_disk
8650 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008651 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008652 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008653 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008654 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08008655 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008656 goto unlock;
8657 }
8658
Guoqing Jiang659b2542015-12-21 10:50:59 +11008659 if (mddev_is_clustered(mddev)) {
8660 struct md_rdev *rdev;
8661 /* kick the device if another node issued a
8662 * remove disk.
8663 */
8664 rdev_for_each(rdev, mddev) {
8665 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8666 rdev->raid_disk < 0)
8667 md_kick_rdev_from_array(rdev);
8668 }
8669 }
8670
NeilBrown6497709b2017-03-15 14:05:14 +11008671 if (!mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11008672 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11008673 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11008674 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07008675 }
NeilBrownfca4d842005-06-21 17:17:11 -07008676
Shaohua Li29530792016-12-08 15:48:19 -08008677 if (mddev->sb_flags)
NeilBrown850b2b42006-10-03 01:15:46 -07008678 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008679
Linus Torvalds1da177e2005-04-16 15:20:36 -07008680 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8681 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8682 /* resync/recovery still happening */
8683 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8684 goto unlock;
8685 }
8686 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008687 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008688 goto unlock;
8689 }
Neil Brown72a23c22008-06-28 08:31:41 +10008690 /* Set RUNNING before clearing NEEDED to avoid
8691 * any transients in the value of "sync_action".
8692 */
NeilBrown72f36d52012-10-11 14:25:57 +11008693 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008694 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008695 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008696 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008697 /* Clear some bits that don't mean anything, but
8698 * might be left set
8699 */
NeilBrown24dd4692005-11-08 21:39:26 -08008700 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8701 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702
NeilBrowned209582012-04-24 10:23:14 +10008703 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8704 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008705 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008706 /* no recovery is running.
8707 * remove any failed drives, then
8708 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008709 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008710 * the personality to fail the re-add.
8711 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008712
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008713 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008714 if (mddev->pers->check_reshape == NULL ||
8715 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008716 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008717 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008718 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008719 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008720 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008721 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8722 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008723 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008724 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008725 } else if (mddev->recovery_cp < MaxSector) {
8726 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008727 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008728 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8729 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008730 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008731
Linus Torvalds1da177e2005-04-16 15:20:36 -07008732 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008733 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008734 /* We are adding a device or devices to an array
8735 * which has the bitmap stored on all devices.
8736 * So make sure all bitmap pages get written
8737 */
8738 bitmap_write_all(mddev->bitmap);
8739 }
NeilBrownac05f252014-09-30 08:10:42 +10008740 INIT_WORK(&mddev->del_work, md_start_sync);
8741 queue_work(md_misc_wq, &mddev->del_work);
8742 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008743 }
NeilBrownac05f252014-09-30 08:10:42 +10008744 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008745 if (!mddev->sync_thread) {
8746 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008747 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008748 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8749 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008750 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008751 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008752 }
NeilBrownac05f252014-09-30 08:10:42 +10008753 unlock:
8754 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008755 mddev_unlock(mddev);
8756 }
8757}
NeilBrown6c144d32014-09-30 16:15:38 +10008758EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008759
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008760void md_reap_sync_thread(struct mddev *mddev)
8761{
8762 struct md_rdev *rdev;
8763
8764 /* resync has finished, collect result */
8765 md_unregister_thread(&mddev->sync_thread);
8766 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8767 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8768 /* success...*/
8769 /* activate any spares */
8770 if (mddev->pers->spare_active(mddev)) {
8771 sysfs_notify(&mddev->kobj, NULL,
8772 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08008773 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008774 }
8775 }
8776 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8777 mddev->pers->finish_reshape)
8778 mddev->pers->finish_reshape(mddev);
8779
8780 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008781 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008782 */
NeilBrownf4667222013-12-09 12:04:56 +11008783 if (!mddev->degraded)
8784 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008785 rdev->saved_raid_disk = -1;
8786
8787 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08008788 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008789 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8790 * clustered raid */
8791 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8792 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008793 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008794 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008795 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8796 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8797 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8798 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008799 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008800 /* flag recovery needed just to double check */
8801 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8802 sysfs_notify_dirent_safe(mddev->sysfs_action);
8803 md_new_event(mddev);
8804 if (mddev->event_work.func)
8805 queue_work(md_misc_wq, &mddev->event_work);
8806}
NeilBrown6c144d32014-09-30 16:15:38 +10008807EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008808
NeilBrownfd01b882011-10-11 16:47:53 +11008809void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008810{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008811 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008812 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008813 !test_bit(Blocked, &rdev->flags) &&
8814 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008815 msecs_to_jiffies(5000));
8816 rdev_dec_pending(rdev, mddev);
8817}
8818EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8819
NeilBrownc6563a82012-05-21 09:27:00 +10008820void md_finish_reshape(struct mddev *mddev)
8821{
8822 /* called be personality module when reshape completes. */
8823 struct md_rdev *rdev;
8824
8825 rdev_for_each(rdev, mddev) {
8826 if (rdev->data_offset > rdev->new_data_offset)
8827 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8828 else
8829 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8830 rdev->data_offset = rdev->new_data_offset;
8831 }
8832}
8833EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008834
Vishal Vermafc974ee2015-12-24 19:20:34 -07008835/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10008836
Vishal Vermafc974ee2015-12-24 19:20:34 -07008837/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11008838int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008839 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008840{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008841 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10008842 int rv;
8843 if (is_new)
8844 s += rdev->new_data_offset;
8845 else
8846 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008847 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8848 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008849 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008850 if (test_bit(ExternalBbl, &rdev->flags))
8851 sysfs_notify(&rdev->kobj, NULL,
8852 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008853 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08008854 set_mask_bits(&mddev->sb_flags, 0,
8855 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10008856 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07008857 return 1;
8858 } else
8859 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008860}
8861EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8862
NeilBrownc6563a82012-05-21 09:27:00 +10008863int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8864 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008865{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008866 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10008867 if (is_new)
8868 s += rdev->new_data_offset;
8869 else
8870 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008871 rv = badblocks_clear(&rdev->badblocks, s, sectors);
8872 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
8873 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
8874 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008875}
8876EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8877
Adrian Bunk75c96f82005-05-05 16:16:09 -07008878static int md_notify_reboot(struct notifier_block *this,
8879 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008880{
8881 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008882 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008883 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008884
NeilBrownc744a652012-03-19 12:46:37 +11008885 for_each_mddev(mddev, tmp) {
8886 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008887 if (mddev->pers)
8888 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008889 if (mddev->persistent)
8890 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008891 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008892 }
NeilBrownc744a652012-03-19 12:46:37 +11008893 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894 }
NeilBrownc744a652012-03-19 12:46:37 +11008895 /*
8896 * certain more exotic SCSI devices are known to be
8897 * volatile wrt too early system reboots. While the
8898 * right place to handle this issue is the given
8899 * driver, we do want to have a safe RAID driver ...
8900 */
8901 if (need_delay)
8902 mdelay(1000*1);
8903
Linus Torvalds1da177e2005-04-16 15:20:36 -07008904 return NOTIFY_DONE;
8905}
8906
Adrian Bunk75c96f82005-05-05 16:16:09 -07008907static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008908 .notifier_call = md_notify_reboot,
8909 .next = NULL,
8910 .priority = INT_MAX, /* before any real devices */
8911};
8912
8913static void md_geninit(void)
8914{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008915 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008916
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008917 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008918}
8919
Adrian Bunk75c96f82005-05-05 16:16:09 -07008920static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921{
Tejun Heoe804ac72010-10-15 15:36:08 +02008922 int ret = -ENOMEM;
8923
Tejun Heoada609e2011-01-25 14:35:54 +01008924 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008925 if (!md_wq)
8926 goto err_wq;
8927
8928 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8929 if (!md_misc_wq)
8930 goto err_misc_wq;
8931
8932 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8933 goto err_md;
8934
8935 if ((ret = register_blkdev(0, "mdp")) < 0)
8936 goto err_mdp;
8937 mdp_major = ret;
8938
NeilBrownaf5628f2014-07-31 13:54:54 +10008939 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008940 md_probe, NULL, NULL);
8941 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008942 md_probe, NULL, NULL);
8943
Linus Torvalds1da177e2005-04-16 15:20:36 -07008944 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008945 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008946
8947 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008949
Tejun Heoe804ac72010-10-15 15:36:08 +02008950err_mdp:
8951 unregister_blkdev(MD_MAJOR, "md");
8952err_md:
8953 destroy_workqueue(md_misc_wq);
8954err_misc_wq:
8955 destroy_workqueue(md_wq);
8956err_wq:
8957 return ret;
8958}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008959
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008960static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008961{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008962 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8963 struct md_rdev *rdev2;
8964 int role, ret;
8965 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008966
Guoqing Jiang818da592017-03-01 16:42:40 +08008967 /*
8968 * If size is changed in another node then we need to
8969 * do resize as well.
8970 */
8971 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
8972 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
8973 if (ret)
8974 pr_info("md-cluster: resize failed\n");
8975 else
8976 bitmap_update_sb(mddev->bitmap);
8977 }
8978
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008979 /* Check for change of roles in the active devices */
8980 rdev_for_each(rdev2, mddev) {
8981 if (test_bit(Faulty, &rdev2->flags))
8982 continue;
8983
8984 /* Check if the roles changed */
8985 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008986
8987 if (test_bit(Candidate, &rdev2->flags)) {
8988 if (role == 0xfffe) {
8989 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
8990 md_kick_rdev_from_array(rdev2);
8991 continue;
8992 }
8993 else
8994 clear_bit(Candidate, &rdev2->flags);
8995 }
8996
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008997 if (role != rdev2->raid_disk) {
8998 /* got activated */
8999 if (rdev2->raid_disk == -1 && role != 0xffff) {
9000 rdev2->saved_raid_disk = role;
9001 ret = remove_and_add_spares(mddev, rdev2);
9002 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009003 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009004 /* wakeup mddev->thread here, so array could
9005 * perform resync with the new activated disk */
9006 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9007 md_wakeup_thread(mddev->thread);
9008
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009009 }
9010 /* device faulty
9011 * We just want to do the minimum to mark the disk
9012 * as faulty. The recovery is performed by the
9013 * one who initiated the error.
9014 */
9015 if ((role == 0xfffe) || (role == 0xfffd)) {
9016 md_error(mddev, rdev2);
9017 clear_bit(Blocked, &rdev2->flags);
9018 }
9019 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009020 }
9021
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009022 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9023 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009024
9025 /* Finally set the event to be up to date */
9026 mddev->events = le64_to_cpu(sb->events);
9027}
9028
9029static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9030{
9031 int err;
9032 struct page *swapout = rdev->sb_page;
9033 struct mdp_superblock_1 *sb;
9034
9035 /* Store the sb page of the rdev in the swapout temporary
9036 * variable in case we err in the future
9037 */
9038 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009039 err = alloc_disk_sb(rdev);
9040 if (err == 0) {
9041 ClearPageUptodate(rdev->sb_page);
9042 rdev->sb_loaded = 0;
9043 err = super_types[mddev->major_version].
9044 load_super(rdev, NULL, mddev->minor_version);
9045 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009046 if (err < 0) {
9047 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9048 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009049 if (rdev->sb_page)
9050 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009051 rdev->sb_page = swapout;
9052 rdev->sb_loaded = 1;
9053 return err;
9054 }
9055
9056 sb = page_address(rdev->sb_page);
9057 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9058 * is not set
9059 */
9060
9061 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9062 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9063
9064 /* The other node finished recovery, call spare_active to set
9065 * device In_sync and mddev->degraded
9066 */
9067 if (rdev->recovery_offset == MaxSector &&
9068 !test_bit(In_sync, &rdev->flags) &&
9069 mddev->pers->spare_active(mddev))
9070 sysfs_notify(&mddev->kobj, NULL, "degraded");
9071
9072 put_page(swapout);
9073 return 0;
9074}
9075
9076void md_reload_sb(struct mddev *mddev, int nr)
9077{
9078 struct md_rdev *rdev;
9079 int err;
9080
9081 /* Find the rdev */
9082 rdev_for_each_rcu(rdev, mddev) {
9083 if (rdev->desc_nr == nr)
9084 break;
9085 }
9086
9087 if (!rdev || rdev->desc_nr != nr) {
9088 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9089 return;
9090 }
9091
9092 err = read_rdev(mddev, rdev);
9093 if (err < 0)
9094 return;
9095
9096 check_sb_changes(mddev, rdev);
9097
9098 /* Read all rdev's to update recovery_offset */
9099 rdev_for_each_rcu(rdev, mddev)
9100 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009101}
9102EXPORT_SYMBOL(md_reload_sb);
9103
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104#ifndef MODULE
9105
9106/*
9107 * Searches all registered partitions for autorun RAID arrays
9108 * at boot time.
9109 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009110
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009111static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009112static LIST_HEAD(all_detected_devices);
9113struct detected_devices_node {
9114 struct list_head list;
9115 dev_t dev;
9116};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009117
9118void md_autodetect_dev(dev_t dev)
9119{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009120 struct detected_devices_node *node_detected_dev;
9121
9122 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9123 if (node_detected_dev) {
9124 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009125 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009126 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009127 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009129}
9130
Linus Torvalds1da177e2005-04-16 15:20:36 -07009131static void autostart_arrays(int part)
9132{
NeilBrown3cb03002011-10-11 16:45:26 +11009133 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009134 struct detected_devices_node *node_detected_dev;
9135 dev_t dev;
9136 int i_scanned, i_passed;
9137
9138 i_scanned = 0;
9139 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009140
NeilBrown9d487392016-11-02 14:16:49 +11009141 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009142
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009143 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009144 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9145 i_scanned++;
9146 node_detected_dev = list_entry(all_detected_devices.next,
9147 struct detected_devices_node, list);
9148 list_del(&node_detected_dev->list);
9149 dev = node_detected_dev->dev;
9150 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009151 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009152 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009153 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009154 if (IS_ERR(rdev))
9155 continue;
9156
NeilBrown403df472014-09-30 15:52:29 +10009157 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009158 continue;
NeilBrown403df472014-09-30 15:52:29 +10009159
NeilBrownd0fae182008-03-04 14:29:31 -08009160 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009161 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009162 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009163 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009164 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009165
NeilBrown9d487392016-11-02 14:16:49 +11009166 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009167
9168 autorun_devices(part);
9169}
9170
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009171#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009172
9173static __exit void md_exit(void)
9174{
NeilBrownfd01b882011-10-11 16:47:53 +11009175 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009176 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009177 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009178
NeilBrownaf5628f2014-07-31 13:54:54 +10009179 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009180 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009181
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009182 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009183 unregister_blkdev(mdp_major, "mdp");
9184 unregister_reboot_notifier(&md_notifier);
9185 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009186
9187 /* We cannot unload the modules while some process is
9188 * waiting for us in select() or poll() - wake them up
9189 */
9190 md_unloading = 1;
9191 while (waitqueue_active(&md_event_waiters)) {
9192 /* not safe to leave yet */
9193 wake_up(&md_event_waiters);
9194 msleep(delay);
9195 delay += delay;
9196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009197 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009198
NeilBrown29ac4aa2008-02-06 01:39:58 -08009199 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009201 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009202 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009203 /*
9204 * for_each_mddev() will call mddev_put() at the end of each
9205 * iteration. As the mddev is now fully clear, this will
9206 * schedule the mddev for destruction by a workqueue, and the
9207 * destroy_workqueue() below will wait for that to complete.
9208 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009209 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009210 destroy_workqueue(md_misc_wq);
9211 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009212}
9213
Dan Williams685784a2007-07-09 11:56:42 -07009214subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009215module_exit(md_exit)
9216
NeilBrownf91de922005-11-08 21:39:36 -08009217static int get_ro(char *buffer, struct kernel_param *kp)
9218{
9219 return sprintf(buffer, "%d", start_readonly);
9220}
9221static int set_ro(const char *val, struct kernel_param *kp)
9222{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009223 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009224}
9225
NeilBrown80ca3a42006-07-10 04:44:18 -07009226module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9227module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009228module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009229module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009230
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009232MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009233MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009234MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);