blob: af911871122853f7c10804b764a0e8d5978991ab [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>
Shaohua Li504634f2016-11-18 09:44:08 -080068#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110069#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110070#include "bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050071#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110074static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#endif
76
NeilBrown01f96c02011-09-21 15:30:20 +100077/* pers_list is a list of registered personalities protected
78 * by pers_lock.
79 * pers_lock does extra service to protect accesses to
80 * mddev->thread when the mutex cannot be held.
81 */
NeilBrown2604b702006-01-06 00:20:36 -080082static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static DEFINE_SPINLOCK(pers_lock);
84
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050085struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050086EXPORT_SYMBOL(md_cluster_ops);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050087struct module *md_cluster_mod;
88EXPORT_SYMBOL(md_cluster_mod);
89
Bernd Schubert90b08712008-05-23 13:04:38 -070090static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020091static struct workqueue_struct *md_wq;
92static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070093
NeilBrown746d3202013-04-24 11:42:41 +100094static int remove_and_add_spares(struct mddev *mddev,
95 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110096static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
Robert Becker1e509152009-12-14 12:49:58 +110099 * Default number of read corrections we'll attempt on an rdev
100 * before ejecting it from the array. We divide the read error
101 * count by 2 for every hour elapsed between read errors.
102 */
103#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
104/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
106 * is 1000 KB/sec, so the extra system load does not show up that much.
107 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -0700108 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * subsystem is idle. There is also an 'absolute maximum' reconstruction
110 * speed limit - in case reconstruction slows down your system despite
111 * idle IO detection.
112 *
113 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800114 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
116
117static int sysctl_speed_limit_min = 1000;
118static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100119static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800120{
121 return mddev->sync_speed_min ?
122 mddev->sync_speed_min : sysctl_speed_limit_min;
123}
124
NeilBrownfd01b882011-10-11 16:47:53 +1100125static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800126{
127 return mddev->sync_speed_max ?
128 mddev->sync_speed_max : sysctl_speed_limit_max;
129}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131static struct ctl_table_header *raid_table_header;
132
Joe Perches82592c32013-11-14 15:16:18 +1100133static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .procname = "speed_limit_min",
136 .data = &sysctl_speed_limit_min,
137 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700138 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800139 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 },
141 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .procname = "speed_limit_max",
143 .data = &sysctl_speed_limit_max,
144 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700145 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800146 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800148 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
Joe Perches82592c32013-11-14 15:16:18 +1100151static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 .procname = "raid",
154 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700155 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 .child = raid_table,
157 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800158 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159};
160
Joe Perches82592c32013-11-14 15:16:18 +1100161static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 .procname = "dev",
164 .maxlen = 0,
165 .mode = 0555,
166 .child = raid_dir_table,
167 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800168 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
170
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700171static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
NeilBrownf91de922005-11-08 21:39:36 -0800173static int start_readonly;
174
NeilBrowna167f662010-10-26 18:31:13 +1100175/* bio_clone_mddev
176 * like bio_clone, but with a local bio set
177 */
178
NeilBrowna167f662010-10-26 18:31:13 +1100179struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100180 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100181{
182 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100183
184 if (!mddev || !mddev->bio_set)
185 return bio_alloc(gfp_mask, nr_iovecs);
186
Kent Overstreet395c72a2012-09-06 15:34:55 -0700187 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100188 if (!b)
189 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100190 return b;
191}
192EXPORT_SYMBOL_GPL(bio_alloc_mddev);
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/*
NeilBrownd7603b72006-01-06 00:20:30 -0800195 * We have a system wide 'event count' that is incremented
196 * on any 'interesting' event, and readers of /proc/mdstat
197 * can use 'poll' or 'select' to find out when the event
198 * count increases.
199 *
200 * Events are:
201 * start array, stop array, error, add device, remove device,
202 * start build, activate spare
203 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800204static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800205static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100206void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800207{
208 atomic_inc(&md_event_count);
209 wake_up(&md_event_waiters);
210}
NeilBrown29269552006-03-27 01:18:10 -0800211EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800212
213/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * Enables to iterate over all existing md arrays
215 * all_mddevs_lock protects this list.
216 */
217static LIST_HEAD(all_mddevs);
218static DEFINE_SPINLOCK(all_mddevs_lock);
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/*
221 * iterates through all used mddevs in the system.
222 * We take care to grab the all_mddevs_lock whenever navigating
223 * the list, and to always hold a refcount when unlocked.
224 * Any code which breaks out of this loop while own
225 * a reference to the current mddev and must mddev_put it.
226 */
NeilBrownfd01b882011-10-11 16:47:53 +1100227#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000229 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100230 _tmp = all_mddevs.next; \
231 _mddev = NULL;}); \
232 ({ if (_tmp != &all_mddevs) \
233 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100235 if (_mddev) mddev_put(_mddev); \
236 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
237 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100239 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 )
241
NeilBrown409c57f2009-03-31 14:39:39 +1100242/* Rather than calling directly into the personality make_request function,
243 * IO requests come here first so that we can check if the device is
244 * being suspended pending a reconfiguration.
245 * We hold a refcount over the call to ->make_request. By the time that
246 * call has finished, the bio has been linked into some internal structure
247 * and so is visible to ->quiesce(), so we don't need the refcount any more.
248 */
Jens Axboedece1632015-11-05 10:41:16 -0700249static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
NeilBrown49077322010-03-25 16:20:56 +1100251 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100252 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500253 unsigned int sectors;
Gu Zheng74672d02015-04-03 08:44:47 +0800254 int cpu;
NeilBrown49077322010-03-25 16:20:56 +1100255
Kent Overstreet54efd502015-04-23 22:37:18 -0700256 blk_queue_split(q, &bio, q->bio_split);
257
NeilBrown274d8cb2016-01-04 16:16:58 +1100258 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100259 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700260 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100261 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100262 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200263 if (bio_sectors(bio) != 0)
264 bio->bi_error = -EROFS;
265 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700266 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100267 }
NeilBrown0ca69882011-01-14 09:14:33 +1100268 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100269 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200270 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100271 DEFINE_WAIT(__wait);
272 for (;;) {
273 prepare_to_wait(&mddev->sb_wait, &__wait,
274 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200275 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100276 break;
277 rcu_read_unlock();
278 schedule();
279 rcu_read_lock();
280 }
281 finish_wait(&mddev->sb_wait, &__wait);
282 }
283 atomic_inc(&mddev->active_io);
284 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100285
Chris Masone91ece52011-02-07 19:21:48 -0500286 /*
287 * save the sectors now since our bio can
288 * go away inside make_request
289 */
290 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700291 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600292 bio->bi_opf &= ~REQ_NOMERGE;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200293 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100294
Gu Zheng74672d02015-04-03 08:44:47 +0800295 cpu = part_stat_lock();
296 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
297 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
298 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100299
NeilBrown409c57f2009-03-31 14:39:39 +1100300 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
301 wake_up(&mddev->sb_wait);
Jens Axboedece1632015-11-05 10:41:16 -0700302
303 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
NeilBrown9e35b992010-04-06 14:23:02 +1000306/* mddev_suspend makes sure no new requests are submitted
307 * to the device, and that any requests that have been submitted
308 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100309 * Once mddev_detach() is called and completes, the module will be
310 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000311 */
NeilBrownfd01b882011-10-11 16:47:53 +1100312void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100313{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200314 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100315 if (mddev->suspended++)
316 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100317 synchronize_rcu();
318 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
319 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500320
321 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100322}
NeilBrown390ee602010-06-01 19:37:27 +1000323EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100324
NeilBrownfd01b882011-10-11 16:47:53 +1100325void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100326{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100327 if (--mddev->suspended)
328 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100329 wake_up(&mddev->sb_wait);
330 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500331
Jonathan Brassow47525e52012-05-22 13:55:29 +1000332 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500333 md_wakeup_thread(mddev->thread);
334 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100335}
NeilBrown390ee602010-06-01 19:37:27 +1000336EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100337
NeilBrownfd01b882011-10-11 16:47:53 +1100338int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000339{
NeilBrown5c675f82014-12-15 12:56:56 +1100340 struct md_personality *pers = mddev->pers;
341 int ret = 0;
342
343 rcu_read_lock();
344 if (mddev->suspended)
345 ret = 1;
346 else if (pers && pers->congested)
347 ret = pers->congested(mddev, bits);
348 rcu_read_unlock();
349 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000350}
NeilBrown5c675f82014-12-15 12:56:56 +1100351EXPORT_SYMBOL_GPL(mddev_congested);
352static int md_congested(void *data, int bits)
353{
354 struct mddev *mddev = data;
355 return mddev_congested(mddev, bits);
356}
NeilBrown3fa841d2009-09-23 18:10:29 +1000357
NeilBrowna2826aa2009-12-14 12:49:49 +1100358/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200359 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100360 */
361
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200362static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100363{
NeilBrown3cb03002011-10-11 16:45:26 +1100364 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100365 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100366
367 rdev_dec_pending(rdev, mddev);
368
369 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200370 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200371 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100372 }
373 bio_put(bio);
374}
375
NeilBrowna7a07e62010-12-09 16:04:25 +1100376static void md_submit_flush_data(struct work_struct *ws);
377
NeilBrowna035fc32010-12-09 16:17:51 +1100378static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100379{
NeilBrownfd01b882011-10-11 16:47:53 +1100380 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100381 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100382
NeilBrowna7a07e62010-12-09 16:04:25 +1100383 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
384 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100385 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100386 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100387 if (rdev->raid_disk >= 0 &&
388 !test_bit(Faulty, &rdev->flags)) {
389 /* Take two references, one is dropped
390 * when request finishes, one after
391 * we reclaim rcu_read_lock
392 */
393 struct bio *bi;
394 atomic_inc(&rdev->nr_pending);
395 atomic_inc(&rdev->nr_pending);
396 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000397 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200398 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100399 bi->bi_private = rdev;
400 bi->bi_bdev = rdev->bdev;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600401 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
NeilBrowna2826aa2009-12-14 12:49:49 +1100402 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500403 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100404 rcu_read_lock();
405 rdev_dec_pending(rdev, mddev);
406 }
407 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100408 if (atomic_dec_and_test(&mddev->flush_pending))
409 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100410}
411
Tejun Heoe9c74692010-09-03 11:56:18 +0200412static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100413{
NeilBrownfd01b882011-10-11 16:47:53 +1100414 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200415 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100416
Kent Overstreet4f024f32013-10-11 15:44:27 -0700417 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100418 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200419 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100420 else {
Jens Axboe1eff9d32016-08-05 15:35:16 -0600421 bio->bi_opf &= ~REQ_PREFLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200422 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100423 }
NeilBrown2b74e122010-12-09 15:59:01 +1100424
425 mddev->flush_bio = NULL;
426 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100427}
428
NeilBrownfd01b882011-10-11 16:47:53 +1100429void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100430{
NeilBrown85572d72014-12-15 12:56:56 +1100431 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100432 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200433 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100434 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200435 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100436 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100437
NeilBrowna035fc32010-12-09 16:17:51 +1100438 INIT_WORK(&mddev->flush_work, submit_flushes);
439 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100440}
Tejun Heoe9c74692010-09-03 11:56:18 +0200441EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100442
NeilBrownfd01b882011-10-11 16:47:53 +1100443static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 atomic_inc(&mddev->active);
446 return mddev;
447}
448
Dan Williams5fd3a172009-03-04 00:57:25 -0700449static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100450
NeilBrownfd01b882011-10-11 16:47:53 +1100451static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
NeilBrowna167f662010-10-26 18:31:13 +1100453 struct bio_set *bs = NULL;
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
456 return;
NeilBrownd3374822009-01-09 08:31:10 +1100457 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100458 mddev->ctime == 0 && !mddev->hold_active) {
459 /* Array is not configured at all, and not held active,
460 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100461 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100462 bs = mddev->bio_set;
463 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100464 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200465 /* We did a probe so need to clean up. Call
466 * queue_work inside the spinlock so that
467 * flush_workqueue() after mddev_find will
468 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100469 */
470 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200471 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100472 } else
473 kfree(mddev);
474 }
475 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100476 if (bs)
477 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Sasha Levin25b2edf2015-07-24 18:19:58 -0400480static void md_safemode_timeout(unsigned long data);
481
NeilBrownfd01b882011-10-11 16:47:53 +1100482void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100483{
484 mutex_init(&mddev->open_mutex);
485 mutex_init(&mddev->reconfig_mutex);
486 mutex_init(&mddev->bitmap_info.mutex);
487 INIT_LIST_HEAD(&mddev->disks);
488 INIT_LIST_HEAD(&mddev->all_mddevs);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400489 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
490 (unsigned long) mddev);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100491 atomic_set(&mddev->active, 1);
492 atomic_set(&mddev->openers, 0);
493 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100494 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100495 atomic_set(&mddev->flush_pending, 0);
496 init_waitqueue_head(&mddev->sb_wait);
497 init_waitqueue_head(&mddev->recovery_wait);
498 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000499 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500500 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100501 mddev->resync_min = 0;
502 mddev->resync_max = MaxSector;
503 mddev->level = LEVEL_NONE;
504}
NeilBrown390ee602010-06-01 19:37:27 +1000505EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100506
NeilBrownf72ffdd2014-09-30 14:23:59 +1000507static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
NeilBrownfd01b882011-10-11 16:47:53 +1100509 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
NeilBrown8f5f02c2011-02-16 13:58:51 +1100511 if (unit && MAJOR(unit) != MD_MAJOR)
512 unit &= ~((1<<MdpMinorShift)-1);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 retry:
515 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
NeilBrownefeb53c2009-01-09 08:31:10 +1100517 if (unit) {
518 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
519 if (mddev->unit == unit) {
520 mddev_get(mddev);
521 spin_unlock(&all_mddevs_lock);
522 kfree(new);
523 return mddev;
524 }
525
526 if (new) {
527 list_add(&new->all_mddevs, &all_mddevs);
528 spin_unlock(&all_mddevs_lock);
529 new->hold_active = UNTIL_IOCTL;
530 return new;
531 }
532 } else if (new) {
533 /* find an unused unit number */
534 static int next_minor = 512;
535 int start = next_minor;
536 int is_free = 0;
537 int dev = 0;
538 while (!is_free) {
539 dev = MKDEV(MD_MAJOR, next_minor);
540 next_minor++;
541 if (next_minor > MINORMASK)
542 next_minor = 0;
543 if (next_minor == start) {
544 /* Oh dear, all in use. */
545 spin_unlock(&all_mddevs_lock);
546 kfree(new);
547 return NULL;
548 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000549
NeilBrownefeb53c2009-01-09 08:31:10 +1100550 is_free = 1;
551 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
552 if (mddev->unit == dev) {
553 is_free = 0;
554 break;
555 }
556 }
557 new->unit = dev;
558 new->md_minor = MINOR(dev);
559 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 list_add(&new->all_mddevs, &all_mddevs);
561 spin_unlock(&all_mddevs_lock);
562 return new;
563 }
564 spin_unlock(&all_mddevs_lock);
565
NeilBrown9ffae0c2006-01-06 00:20:32 -0800566 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (!new)
568 return NULL;
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 new->unit = unit;
571 if (MAJOR(unit) == MD_MAJOR)
572 new->md_minor = MINOR(unit);
573 else
574 new->md_minor = MINOR(unit) >> MdpMinorShift;
575
NeilBrownfafd7fb2010-04-01 15:55:30 +1100576 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 goto retry;
579}
580
NeilBrownb6eb1272010-04-15 10:13:47 +1000581static struct attribute_group md_redundancy_group;
582
NeilBrown5c47daf2014-12-15 12:57:01 +1100583void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
NeilBrowna64c8762010-04-14 17:15:37 +1000585 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000586 /* These cannot be removed under reconfig_mutex as
587 * an access to the files will try to take reconfig_mutex
588 * while holding the file unremovable, which leads to
589 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000590 * So hold set sysfs_active while the remove in happeing,
591 * and anything else which might set ->to_remove or my
592 * otherwise change the sysfs namespace will fail with
593 * -EBUSY if sysfs_active is still set.
594 * We set sysfs_active under reconfig_mutex and elsewhere
595 * test it under the same mutex to ensure its correct value
596 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000597 */
NeilBrowna64c8762010-04-14 17:15:37 +1000598 struct attribute_group *to_remove = mddev->to_remove;
599 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000600 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000601 mutex_unlock(&mddev->reconfig_mutex);
602
NeilBrown00bcb4a2010-06-01 19:37:23 +1000603 if (mddev->kobj.sd) {
604 if (to_remove != &md_redundancy_group)
605 sysfs_remove_group(&mddev->kobj, to_remove);
606 if (mddev->pers == NULL ||
607 mddev->pers->sync_request == NULL) {
608 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
609 if (mddev->sysfs_action)
610 sysfs_put(mddev->sysfs_action);
611 mddev->sysfs_action = NULL;
612 }
NeilBrowna64c8762010-04-14 17:15:37 +1000613 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000614 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000615 } else
616 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Chris Dunlop751e67c2011-10-19 16:48:26 +1100618 /* As we've dropped the mutex we need a spinlock to
619 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000620 */
621 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700622 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000623 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
NeilBrown5c47daf2014-12-15 12:57:01 +1100625EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500627struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100628{
629 struct md_rdev *rdev;
630
631 rdev_for_each_rcu(rdev, mddev)
632 if (rdev->desc_nr == nr)
633 return rdev;
634
635 return NULL;
636}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500637EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100638
639static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
NeilBrown3cb03002011-10-11 16:45:26 +1100641 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
NeilBrowndafb20f2012-03-19 12:46:39 +1100643 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (rdev->bdev->bd_dev == dev)
645 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return NULL;
648}
649
NeilBrown1ca69c42012-10-11 13:37:33 +1100650static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
651{
652 struct md_rdev *rdev;
653
654 rdev_for_each_rcu(rdev, mddev)
655 if (rdev->bdev->bd_dev == dev)
656 return rdev;
657
658 return NULL;
659}
660
NeilBrown84fc4b52011-10-11 16:49:58 +1100661static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800662{
NeilBrown84fc4b52011-10-11 16:49:58 +1100663 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800664 list_for_each_entry(pers, &pers_list, list) {
665 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800666 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800667 if (strcmp(pers->name, clevel)==0)
668 return pers;
669 }
NeilBrown2604b702006-01-06 00:20:36 -0800670 return NULL;
671}
672
Andre Nollb73df2d2008-07-11 22:02:23 +1000673/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100674static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100676 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000677 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
NeilBrownf72ffdd2014-09-30 14:23:59 +1000680static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100683 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000684 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return 0;
686}
687
NeilBrown545c8792012-05-22 13:54:30 +1000688void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800691 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 rdev->sb_loaded = 0;
693 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000694 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100695 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
NeilBrown2699b672011-07-28 11:31:47 +1000697 if (rdev->bb_page) {
698 put_page(rdev->bb_page);
699 rdev->bb_page = NULL;
700 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800701 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
NeilBrown545c8792012-05-22 13:54:30 +1000703EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200705static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700706{
NeilBrown3cb03002011-10-11 16:45:26 +1100707 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100708 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700709
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200710 if (bio->bi_error) {
NeilBrown9d487392016-11-02 14:16:49 +1100711 pr_err("md: super_written gets error=%d\n", bio->bi_error);
NeilBrowna9701a32005-11-08 21:39:34 -0800712 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100713 if (!test_bit(Faulty, &rdev->flags)
714 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800715 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100716 set_bit(LastDev, &rdev->flags);
717 }
718 } else
719 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700720
NeilBrowna9701a32005-11-08 21:39:34 -0800721 if (atomic_dec_and_test(&mddev->pending_writes))
722 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700723 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700724 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700725}
726
NeilBrownfd01b882011-10-11 16:47:53 +1100727void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700728 sector_t sector, int size, struct page *page)
729{
730 /* write first size bytes of page to sector of rdev
731 * Increment mddev->pending_writes before returning
732 * and decrement it on completion, waking up sb_wait
733 * if zero is reached.
734 * If an error occurred, call md_error
735 */
NeilBrown46533ff2016-11-18 16:16:11 +1100736 struct bio *bio;
737 int ff = 0;
738
739 if (test_bit(Faulty, &rdev->flags))
740 return;
741
742 bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700743
Shaohua Lied3b98c2016-03-29 14:00:19 -0700744 atomic_inc(&rdev->nr_pending);
745
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100746 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700747 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700748 bio_add_page(bio, page, size, 0);
749 bio->bi_private = rdev;
750 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100751
752 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
753 test_bit(FailFast, &rdev->flags) &&
754 !test_bit(LastDev, &rdev->flags))
755 ff = MD_FAILFAST;
Shaohua Li20737732016-12-13 12:40:15 -0800756 bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -0800757
NeilBrown7bfa19f2005-06-21 17:17:28 -0700758 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500759 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800760}
761
NeilBrown46533ff2016-11-18 16:16:11 +1100762int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800763{
Tejun Heoe9c74692010-09-03 11:56:18 +0200764 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000765 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -0800766 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +1100767 return -EAGAIN;
768 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700769}
770
NeilBrown3cb03002011-10-11 16:45:26 +1100771int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -0500772 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
NeilBrowna167f662010-10-26 18:31:13 +1100774 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 int ret;
776
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100777 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
778 rdev->meta_bdev : rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -0500779 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100780 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700781 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000782 else if (rdev->mddev->reshape_position != MaxSector &&
783 (rdev->mddev->reshape_backwards ==
784 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700785 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100786 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700787 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500789
790 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200792 ret = !bio->bi_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 bio_put(bio);
794 return ret;
795}
NeilBrowna8745db2006-01-06 00:20:34 -0800796EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
NeilBrownf72ffdd2014-09-30 14:23:59 +1000798static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (rdev->sb_loaded)
803 return 0;
804
Mike Christie796a5cf2016-06-05 14:32:07 -0500805 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 goto fail;
807 rdev->sb_loaded = 1;
808 return 0;
809
810fail:
NeilBrown9d487392016-11-02 14:16:49 +1100811 pr_err("md: disabled device %s, could not read superblock.\n",
812 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return -EINVAL;
814}
815
816static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
817{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000818 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000819 sb1->set_uuid1 == sb2->set_uuid1 &&
820 sb1->set_uuid2 == sb2->set_uuid2 &&
821 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
825{
826 int ret;
827 mdp_super_t *tmp1, *tmp2;
828
829 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
830 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
831
832 if (!tmp1 || !tmp2) {
833 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 goto abort;
835 }
836
837 *tmp1 = *sb1;
838 *tmp2 = *sb2;
839
840 /*
841 * nr_disks is not constant
842 */
843 tmp1->nr_disks = 0;
844 tmp2->nr_disks = 0;
845
Andre Nollce0c8e02008-07-11 22:02:20 +1000846 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700848 kfree(tmp1);
849 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return ret;
851}
852
NeilBrown4d167f02007-05-09 02:35:37 -0700853static u32 md_csum_fold(u32 csum)
854{
855 csum = (csum & 0xffff) + (csum >> 16);
856 return (csum & 0xffff) + (csum >> 16);
857}
858
NeilBrownf72ffdd2014-09-30 14:23:59 +1000859static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
NeilBrown4d167f02007-05-09 02:35:37 -0700861 u64 newcsum = 0;
862 u32 *sb32 = (u32*)sb;
863 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 unsigned int disk_csum, csum;
865
866 disk_csum = sb->sb_csum;
867 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700868
869 for (i = 0; i < MD_SB_BYTES/4 ; i++)
870 newcsum += sb32[i];
871 csum = (newcsum & 0xffffffff) + (newcsum>>32);
872
NeilBrown4d167f02007-05-09 02:35:37 -0700873#ifdef CONFIG_ALPHA
874 /* This used to use csum_partial, which was wrong for several
875 * reasons including that different results are returned on
876 * different architectures. It isn't critical that we get exactly
877 * the same return value as before (we always csum_fold before
878 * testing, and that removes any differences). However as we
879 * know that csum_partial always returned a 16bit value on
880 * alphas, do a fold to maximise conformity to previous behaviour.
881 */
882 sb->sb_csum = md_csum_fold(disk_csum);
883#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700885#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return csum;
887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889/*
890 * Handle superblock details.
891 * We want to be able to handle multiple superblock formats
892 * so we have a common interface to them all, and an array of
893 * different handlers.
894 * We rely on user-space to write the initial superblock, and support
895 * reading and updating of superblocks.
896 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100897 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 * loads and validates a superblock on dev.
899 * if refdev != NULL, compare superblocks on both devices
900 * Return:
901 * 0 - dev has a superblock that is compatible with refdev
902 * 1 - dev has a superblock that is compatible and newer than refdev
903 * so dev should be used as the refdev in future
904 * -EINVAL superblock incompatible or invalid
905 * -othererror e.g. -EIO
906 *
NeilBrownfd01b882011-10-11 16:47:53 +1100907 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 * Verify that dev is acceptable into mddev.
909 * The first time, mddev->raid_disks will be 0, and data from
910 * dev should be merged in. Subsequent calls check that dev
911 * is new enough. Return 0 or -EINVAL
912 *
NeilBrownfd01b882011-10-11 16:47:53 +1100913 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 * Update the superblock for rdev with data in mddev
915 * This does not write to disc.
916 *
917 */
918
919struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000920 char *name;
921 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000922 int (*load_super)(struct md_rdev *rdev,
923 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000924 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000925 int (*validate_super)(struct mddev *mddev,
926 struct md_rdev *rdev);
927 void (*sync_super)(struct mddev *mddev,
928 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100929 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000930 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000931 int (*allow_new_offset)(struct md_rdev *rdev,
932 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933};
934
935/*
Andre Noll0894cc32009-06-18 08:49:23 +1000936 * Check that the given mddev has no bitmap.
937 *
938 * This function is called from the run method of all personalities that do not
939 * support bitmaps. It prints an error message and returns non-zero if mddev
940 * has a bitmap. Otherwise, it returns 0.
941 *
942 */
NeilBrownfd01b882011-10-11 16:47:53 +1100943int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000944{
NeilBrownc3d97142009-12-14 12:49:52 +1100945 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000946 return 0;
NeilBrown9d487392016-11-02 14:16:49 +1100947 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +1000948 mdname(mddev), mddev->pers->name);
949 return 1;
950}
951EXPORT_SYMBOL(md_check_no_bitmap);
952
953/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000954 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 */
NeilBrown3cb03002011-10-11 16:45:26 +1100956static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
959 mdp_super_t *sb;
960 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 /*
Andre Noll0f420352008-07-11 22:02:23 +1000963 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 * it's at the end of the disk.
965 *
966 * It also happens to be a multiple of 4Kb.
967 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100968 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
NeilBrown0002b272005-09-09 16:23:53 -0700970 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +1100971 if (ret)
972 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 ret = -EINVAL;
975
976 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000977 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +1100980 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 goto abort;
982 }
983
984 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800985 sb->minor_version < 90 ||
986 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +1100987 pr_warn("Bad version number %d.%d on %s\n",
988 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 goto abort;
990 }
991
992 if (sb->raid_disks <= 0)
993 goto abort;
994
NeilBrown4d167f02007-05-09 02:35:37 -0700995 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +1100996 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 goto abort;
998 }
999
1000 rdev->preferred_minor = sb->md_minor;
1001 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001002 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001003 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001004 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 if (sb->level == LEVEL_MULTIPATH)
1007 rdev->desc_nr = -1;
1008 else
1009 rdev->desc_nr = sb->this_disk.number;
1010
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001011 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001013 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001015 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (!uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001017 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 b, bdevname(refdev->bdev,b2));
1019 goto abort;
1020 }
1021 if (!sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001022 pr_warn("md: %s has same UUID but different superblock to %s\n",
1023 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 goto abort;
1025 }
1026 ev1 = md_event(sb);
1027 ev2 = md_event(refsb);
1028 if (ev1 > ev2)
1029 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001030 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 ret = 0;
1032 }
NeilBrown8190e752009-06-18 08:48:58 +10001033 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001034 /* Limit to 4TB as metadata cannot record more than that.
1035 * (not needed for Linear and RAID0 as metadata doesn't
1036 * record this size)
1037 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001038 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1039 sb->level >= 1)
1040 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
NeilBrown27a7b262011-09-10 17:21:28 +10001042 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001043 /* "this cannot possibly happen" ... */
1044 ret = -EINVAL;
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 abort:
1047 return ret;
1048}
1049
1050/*
1051 * validate_super for 0.90.0
1052 */
NeilBrownfd01b882011-10-11 16:47:53 +11001053static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001056 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001057 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
NeilBrown41158c72005-06-21 17:17:25 -07001059 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001060 clear_bit(Faulty, &rdev->flags);
1061 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001062 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001063 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (mddev->raid_disks == 0) {
1066 mddev->major_version = 0;
1067 mddev->minor_version = sb->minor_version;
1068 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001069 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001070 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 mddev->ctime = sb->ctime;
1072 mddev->utime = sb->utime;
1073 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001074 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 mddev->layout = sb->layout;
1076 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001077 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001078 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001079 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001080 mddev->bitmap_info.space = 0;
1081 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001082 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001083 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001084 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
NeilBrownf6705572006-03-27 01:18:11 -08001086 if (mddev->minor_version >= 91) {
1087 mddev->reshape_position = sb->reshape_position;
1088 mddev->delta_disks = sb->delta_disks;
1089 mddev->new_level = sb->new_level;
1090 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001091 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001092 if (mddev->delta_disks < 0)
1093 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001094 } else {
1095 mddev->reshape_position = MaxSector;
1096 mddev->delta_disks = 0;
1097 mddev->new_level = mddev->level;
1098 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001099 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001100 }
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (sb->state & (1<<MD_SB_CLEAN))
1103 mddev->recovery_cp = MaxSector;
1104 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001105 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 sb->events_lo == sb->cp_events_lo) {
1107 mddev->recovery_cp = sb->recovery_cp;
1108 } else
1109 mddev->recovery_cp = 0;
1110 }
1111
1112 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1113 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1114 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1115 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1116
1117 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001118
1119 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001120 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001121 mddev->bitmap_info.offset =
1122 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001123 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001124 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001125 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001126
NeilBrown41158c72005-06-21 17:17:25 -07001127 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001128 /* Insist on good event counter while assembling, except
1129 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001131 if (sb->disks[rdev->desc_nr].state & (
1132 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001133 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001134 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001135 } else if (mddev->bitmap) {
1136 /* if adding to array with a bitmap, then we can accept an
1137 * older device ... but not too old.
1138 */
NeilBrown41158c72005-06-21 17:17:25 -07001139 if (ev1 < mddev->bitmap->events_cleared)
1140 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001141 if (ev1 < mddev->events)
1142 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001143 } else {
1144 if (ev1 < mddev->events)
1145 /* just a hot-add of a new device, leave raid_disk at -1 */
1146 return 0;
1147 }
NeilBrown41158c72005-06-21 17:17:25 -07001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 desc = sb->disks + rdev->desc_nr;
1151
1152 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001153 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001154 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1155 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001156 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001158 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001159 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1160 /* active but not in sync implies recovery up to
1161 * reshape position. We don't know exactly where
1162 * that is, so set to zero for now */
1163 if (mddev->minor_version >= 91) {
1164 rdev->recovery_offset = 0;
1165 rdev->raid_disk = desc->raid_disk;
1166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001168 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1169 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001170 if (desc->state & (1<<MD_DISK_FAILFAST))
1171 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001172 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001173 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return 0;
1175}
1176
1177/*
1178 * sync_super for 0.90.0
1179 */
NeilBrownfd01b882011-10-11 16:47:53 +11001180static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001183 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /* make rdev->sb match mddev data..
1187 *
1188 * 1/ zero out disks
1189 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1190 * 3/ any empty disks < next_spare become removed
1191 *
1192 * disks[0] gets initialised to REMOVED because
1193 * we cannot be sure from other fields if it has
1194 * been initialised or not.
1195 */
1196 int i;
1197 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1198
NeilBrown61181562005-09-09 16:24:02 -07001199 rdev->sb_size = MD_SB_BYTES;
1200
Namhyung Kim65a06f062011-07-27 11:00:36 +10001201 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 memset(sb, 0, sizeof(*sb));
1204
1205 sb->md_magic = MD_SB_MAGIC;
1206 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 sb->patch_version = mddev->patch_version;
1208 sb->gvalid_words = 0; /* ignored */
1209 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1210 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1211 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1212 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1213
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001214 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001216 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 sb->raid_disks = mddev->raid_disks;
1218 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001219 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001220 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 sb->state = 0;
1222 sb->events_hi = (mddev->events>>32);
1223 sb->events_lo = (u32)mddev->events;
1224
NeilBrownf6705572006-03-27 01:18:11 -08001225 if (mddev->reshape_position == MaxSector)
1226 sb->minor_version = 90;
1227 else {
1228 sb->minor_version = 91;
1229 sb->reshape_position = mddev->reshape_position;
1230 sb->new_level = mddev->new_level;
1231 sb->delta_disks = mddev->delta_disks;
1232 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001233 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001234 }
1235 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (mddev->in_sync)
1237 {
1238 sb->recovery_cp = mddev->recovery_cp;
1239 sb->cp_events_hi = (mddev->events>>32);
1240 sb->cp_events_lo = (u32)mddev->events;
1241 if (mddev->recovery_cp == MaxSector)
1242 sb->state = (1<< MD_SB_CLEAN);
1243 } else
1244 sb->recovery_cp = 0;
1245
1246 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001247 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
NeilBrownc3d97142009-12-14 12:49:52 +11001249 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001250 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001253 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001255 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001256 int is_active = test_bit(In_sync, &rdev2->flags);
1257
1258 if (rdev2->raid_disk >= 0 &&
1259 sb->minor_version >= 91)
1260 /* we have nowhere to store the recovery_offset,
1261 * but if it is not below the reshape_position,
1262 * we can piggy-back on that.
1263 */
1264 is_active = 1;
1265 if (rdev2->raid_disk < 0 ||
1266 test_bit(Faulty, &rdev2->flags))
1267 is_active = 0;
1268 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001269 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001271 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001272 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 d = &sb->disks[rdev2->desc_nr];
1274 nr_disks++;
1275 d->number = rdev2->desc_nr;
1276 d->major = MAJOR(rdev2->bdev->bd_dev);
1277 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001278 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 d->raid_disk = rdev2->raid_disk;
1280 else
1281 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001282 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001284 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001286 if (test_bit(In_sync, &rdev2->flags))
1287 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 active++;
1289 working++;
1290 } else {
1291 d->state = 0;
1292 spare++;
1293 working++;
1294 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001295 if (test_bit(WriteMostly, &rdev2->flags))
1296 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001297 if (test_bit(FailFast, &rdev2->flags))
1298 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 /* now set the "removed" and "faulty" bits on any missing devices */
1301 for (i=0 ; i < mddev->raid_disks ; i++) {
1302 mdp_disk_t *d = &sb->disks[i];
1303 if (d->state == 0 && d->number == 0) {
1304 d->number = i;
1305 d->raid_disk = i;
1306 d->state = (1<<MD_DISK_REMOVED);
1307 d->state |= (1<<MD_DISK_FAULTY);
1308 failed++;
1309 }
1310 }
1311 sb->nr_disks = nr_disks;
1312 sb->active_disks = active;
1313 sb->working_disks = working;
1314 sb->failed_disks = failed;
1315 sb->spare_disks = spare;
1316
1317 sb->this_disk = sb->disks[rdev->desc_nr];
1318 sb->sb_csum = calc_sb_csum(sb);
1319}
1320
1321/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001322 * rdev_size_change for 0.90.0
1323 */
1324static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001325super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001326{
Andre Noll58c0fed2009-03-31 14:33:13 +11001327 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001328 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001329 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001330 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001331 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001332 if (!num_sectors || num_sectors > rdev->sb_start)
1333 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001334 /* Limit to 4TB as metadata cannot record more than that.
1335 * 4TB == 2^32 KB, or 2*2^32 sectors.
1336 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001337 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1338 rdev->mddev->level >= 1)
1339 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001340 do {
1341 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001342 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001343 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001344 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001345}
1346
NeilBrownc6563a82012-05-21 09:27:00 +10001347static int
1348super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1349{
1350 /* non-zero offset changes not possible with v0.90 */
1351 return new_offset == 0;
1352}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001353
1354/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 * version 1 superblock
1356 */
1357
NeilBrownf72ffdd2014-09-30 14:23:59 +10001358static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001360 __le32 disk_csum;
1361 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 unsigned long long newcsum;
1363 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001364 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 disk_csum = sb->sb_csum;
1367 sb->sb_csum = 0;
1368 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001369 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 newcsum += le32_to_cpu(*isuper++);
1371
1372 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001373 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1376 sb->sb_csum = disk_csum;
1377 return cpu_to_le32(csum);
1378}
1379
NeilBrown3cb03002011-10-11 16:45:26 +11001380static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 struct mdp_superblock_1 *sb;
1383 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001384 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001385 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001387 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 /*
Andre Noll0f420352008-07-11 22:02:23 +10001390 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 * It is always aligned to a 4K boundary and
1392 * depeding on minor_version, it can be:
1393 * 0: At least 8K, but less than 12K, from end of device
1394 * 1: At start of device
1395 * 2: 4K from start of device.
1396 */
1397 switch(minor_version) {
1398 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001399 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001400 sb_start -= 8*2;
1401 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 break;
1403 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001404 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 break;
1406 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001407 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 break;
1409 default:
1410 return -EINVAL;
1411 }
Andre Noll0f420352008-07-11 22:02:23 +10001412 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
NeilBrown0002b272005-09-09 16:23:53 -07001414 /* superblock is rarely larger than 1K, but it can be larger,
1415 * and it is safe to read 4k, so we do that
1416 */
1417 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (ret) return ret;
1419
Namhyung Kim65a06f062011-07-27 11:00:36 +10001420 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1423 sb->major_version != cpu_to_le32(1) ||
1424 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001425 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001426 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return -EINVAL;
1428
1429 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001430 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 bdevname(rdev->bdev,b));
1432 return -EINVAL;
1433 }
1434 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001435 pr_warn("md: data_size too small on %s\n",
1436 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return -EINVAL;
1438 }
NeilBrownc6563a82012-05-21 09:27:00 +10001439 if (sb->pad0 ||
1440 sb->pad3[0] ||
1441 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1442 /* Some padding is non-zero, might be a new feature */
1443 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 rdev->preferred_minor = 0xffff;
1446 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001447 rdev->new_data_offset = rdev->data_offset;
1448 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1449 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1450 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001451 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
NeilBrown0002b272005-09-09 16:23:53 -07001453 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001454 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001455 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001456 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1457
1458 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001459 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001460 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001461 if (minor_version
1462 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1463 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001464
NeilBrown31b65a02006-07-10 04:44:14 -07001465 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1466 rdev->desc_nr = -1;
1467 else
1468 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1469
NeilBrown2699b672011-07-28 11:31:47 +10001470 if (!rdev->bb_page) {
1471 rdev->bb_page = alloc_page(GFP_KERNEL);
1472 if (!rdev->bb_page)
1473 return -ENOMEM;
1474 }
1475 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1476 rdev->badblocks.count == 0) {
1477 /* need to load the bad block list.
1478 * Currently we limit it to one page.
1479 */
1480 s32 offset;
1481 sector_t bb_sector;
1482 u64 *bbp;
1483 int i;
1484 int sectors = le16_to_cpu(sb->bblog_size);
1485 if (sectors > (PAGE_SIZE / 512))
1486 return -EINVAL;
1487 offset = le32_to_cpu(sb->bblog_offset);
1488 if (offset == 0)
1489 return -EINVAL;
1490 bb_sector = (long long)offset;
1491 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001492 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001493 return -EIO;
1494 bbp = (u64 *)page_address(rdev->bb_page);
1495 rdev->badblocks.shift = sb->bblog_shift;
1496 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1497 u64 bb = le64_to_cpu(*bbp);
1498 int count = bb & (0x3ff);
1499 u64 sector = bb >> 10;
1500 sector <<= sb->bblog_shift;
1501 count <<= sb->bblog_shift;
1502 if (bb + 1 == 0)
1503 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001504 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001505 return -EINVAL;
1506 }
NeilBrown486adf72013-04-24 11:42:44 +10001507 } else if (sb->bblog_offset != 0)
1508 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001509
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001510 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) {
1511 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1512 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1513 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1514 }
1515
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001516 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001517 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001518 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001520 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1523 sb->level != refsb->level ||
1524 sb->layout != refsb->layout ||
1525 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001526 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 bdevname(rdev->bdev,b),
1528 bdevname(refdev->bdev,b2));
1529 return -EINVAL;
1530 }
1531 ev1 = le64_to_cpu(sb->events);
1532 ev2 = le64_to_cpu(refsb->events);
1533
1534 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001535 ret = 1;
1536 else
1537 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
NeilBrownc6563a82012-05-21 09:27:00 +10001539 if (minor_version) {
1540 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1541 sectors -= rdev->data_offset;
1542 } else
1543 sectors = rdev->sb_start;
1544 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001546 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001547 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
NeilBrownfd01b882011-10-11 16:47:53 +11001550static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001552 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001553 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
NeilBrown41158c72005-06-21 17:17:25 -07001555 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001556 clear_bit(Faulty, &rdev->flags);
1557 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001558 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001559 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (mddev->raid_disks == 0) {
1562 mddev->major_version = 1;
1563 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001564 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001565 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001566 mddev->ctime = le64_to_cpu(sb->ctime);
1567 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001569 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 mddev->layout = le32_to_cpu(sb->layout);
1571 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001572 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001573 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001574 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001575 mddev->bitmap_info.space = 0;
1576 /* Default location for bitmap is 1K after superblock
1577 * using 3K - total of 4K
1578 */
NeilBrownc3d97142009-12-14 12:49:52 +11001579 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001580 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001581 mddev->reshape_backwards = 0;
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1584 memcpy(mddev->uuid, sb->set_uuid, 16);
1585
1586 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001587
NeilBrown71c08052005-09-09 16:23:51 -07001588 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001589 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001590 mddev->bitmap_info.offset =
1591 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001592 /* Metadata doesn't record how much space is available.
1593 * For 1.0, we assume we can use up to the superblock
1594 * if before, else to 4K beyond superblock.
1595 * For others, assume no change is possible.
1596 */
1597 if (mddev->minor_version > 0)
1598 mddev->bitmap_info.space = 0;
1599 else if (mddev->bitmap_info.offset > 0)
1600 mddev->bitmap_info.space =
1601 8 - mddev->bitmap_info.offset;
1602 else
1603 mddev->bitmap_info.space =
1604 -mddev->bitmap_info.offset;
1605 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001606
NeilBrownf6705572006-03-27 01:18:11 -08001607 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1608 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1609 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1610 mddev->new_level = le32_to_cpu(sb->new_level);
1611 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001612 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001613 if (mddev->delta_disks < 0 ||
1614 (mddev->delta_disks == 0 &&
1615 (le32_to_cpu(sb->feature_map)
1616 & MD_FEATURE_RESHAPE_BACKWARDS)))
1617 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001618 } else {
1619 mddev->reshape_position = MaxSector;
1620 mddev->delta_disks = 0;
1621 mddev->new_level = mddev->level;
1622 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001623 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001624 }
1625
Song Liu486b0f72016-08-19 15:34:01 -07001626 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001627 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001628
1629 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) {
1630 if (le32_to_cpu(sb->feature_map) &
1631 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1632 return -EINVAL;
1633 set_bit(MD_HAS_PPL, &mddev->flags);
1634 }
NeilBrown41158c72005-06-21 17:17:25 -07001635 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001636 /* Insist of good event counter while assembling, except for
1637 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001639 if (rdev->desc_nr >= 0 &&
1640 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001641 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1642 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001643 if (ev1 < mddev->events)
1644 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001645 } else if (mddev->bitmap) {
1646 /* If adding to array with a bitmap, then we can accept an
1647 * older device, but not too old.
1648 */
NeilBrown41158c72005-06-21 17:17:25 -07001649 if (ev1 < mddev->bitmap->events_cleared)
1650 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001651 if (ev1 < mddev->events)
1652 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001653 } else {
1654 if (ev1 < mddev->events)
1655 /* just a hot-add of a new device, leave raid_disk at -1 */
1656 return 0;
1657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if (mddev->level != LEVEL_MULTIPATH) {
1659 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001660 if (rdev->desc_nr < 0 ||
1661 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001662 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001663 rdev->desc_nr = -1;
1664 } else
1665 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001667 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001669 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001670 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 break;
Song Liubac624f2015-08-13 14:31:55 -07001672 case MD_DISK_ROLE_JOURNAL: /* journal device */
1673 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1674 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001675 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001676 return -EINVAL;
1677 }
1678 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001679 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001680 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001681 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 default:
NeilBrownf4667222013-12-09 12:04:56 +11001683 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001684 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001685 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001686 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001687 if (!(le32_to_cpu(sb->feature_map) &
1688 MD_FEATURE_RECOVERY_BITMAP))
1689 rdev->saved_raid_disk = -1;
1690 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001691 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 rdev->raid_disk = role;
1693 break;
1694 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001695 if (sb->devflags & WriteMostly1)
1696 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001697 if (sb->devflags & FailFast1)
1698 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001699 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1700 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001701 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001702 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return 0;
1705}
1706
NeilBrownfd01b882011-10-11 16:47:53 +11001707static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
1709 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001710 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 int max_dev, i;
1712 /* make rdev->sb match mddev and rdev data. */
1713
Namhyung Kim65a06f062011-07-27 11:00:36 +10001714 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
1716 sb->feature_map = 0;
1717 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001718 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 memset(sb->pad3, 0, sizeof(sb->pad3));
1720
1721 sb->utime = cpu_to_le64((__u64)mddev->utime);
1722 sb->events = cpu_to_le64(mddev->events);
1723 if (mddev->in_sync)
1724 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001725 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1726 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 else
1728 sb->resync_offset = cpu_to_le64(0);
1729
NeilBrown1c05b4b2006-10-21 10:24:08 -07001730 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001731
NeilBrownf0ca3402006-02-02 14:28:04 -08001732 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001733 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001734 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001735 sb->level = cpu_to_le32(mddev->level);
1736 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11001737 if (test_bit(FailFast, &rdev->flags))
1738 sb->devflags |= FailFast1;
1739 else
1740 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08001741
NeilBrownaeb9b2112011-08-25 14:43:08 +10001742 if (test_bit(WriteMostly, &rdev->flags))
1743 sb->devflags |= WriteMostly1;
1744 else
1745 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001746 sb->data_offset = cpu_to_le64(rdev->data_offset);
1747 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001748
NeilBrownc3d97142009-12-14 12:49:52 +11001749 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1750 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001751 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001752 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001753
Shaohua Lif2076e72015-10-08 21:54:12 -07001754 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001755 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001756 sb->feature_map |=
1757 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1758 sb->recovery_offset =
1759 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001760 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1761 sb->feature_map |=
1762 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001763 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001764 /* Note: recovery_offset and journal_tail share space */
1765 if (test_bit(Journal, &rdev->flags))
1766 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001767 if (test_bit(Replacement, &rdev->flags))
1768 sb->feature_map |=
1769 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001770
NeilBrownf6705572006-03-27 01:18:11 -08001771 if (mddev->reshape_position != MaxSector) {
1772 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1773 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1774 sb->new_layout = cpu_to_le32(mddev->new_layout);
1775 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1776 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001777 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001778 if (mddev->delta_disks == 0 &&
1779 mddev->reshape_backwards)
1780 sb->feature_map
1781 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001782 if (rdev->new_data_offset != rdev->data_offset) {
1783 sb->feature_map
1784 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1785 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1786 - rdev->data_offset));
1787 }
NeilBrownf6705572006-03-27 01:18:11 -08001788 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001789
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001790 if (mddev_is_clustered(mddev))
1791 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1792
NeilBrown2699b672011-07-28 11:31:47 +10001793 if (rdev->badblocks.count == 0)
1794 /* Nothing to do for bad blocks*/ ;
1795 else if (sb->bblog_offset == 0)
1796 /* Cannot record bad blocks on this device */
1797 md_error(mddev, rdev);
1798 else {
1799 struct badblocks *bb = &rdev->badblocks;
1800 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1801 u64 *p = bb->page;
1802 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1803 if (bb->changed) {
1804 unsigned seq;
1805
1806retry:
1807 seq = read_seqbegin(&bb->lock);
1808
1809 memset(bbp, 0xff, PAGE_SIZE);
1810
1811 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001812 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001813 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1814 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001815 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001816 }
NeilBrownd0962932012-03-19 12:46:41 +11001817 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001818 if (read_seqretry(&bb->lock, seq))
1819 goto retry;
1820
1821 bb->sector = (rdev->sb_start +
1822 (int)le32_to_cpu(sb->bblog_offset));
1823 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001824 }
1825 }
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001828 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (rdev2->desc_nr+1 > max_dev)
1830 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001831
NeilBrown70471da2009-08-03 10:59:57 +10001832 if (max_dev > le32_to_cpu(sb->max_dev)) {
1833 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001834 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001835 rdev->sb_size = max_dev * 2 + 256;
1836 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1837 if (rdev->sb_size & bmask)
1838 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001839 } else
1840 max_dev = le32_to_cpu(sb->max_dev);
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 for (i=0; i<max_dev;i++)
Song Liuc4d4c912015-08-13 14:31:54 -07001843 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001844
Song Liua97b7892015-10-08 21:54:09 -07001845 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1846 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001848 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
1849 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
1850 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
1851 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
1852 }
1853
NeilBrowndafb20f2012-03-19 12:46:39 +11001854 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001856 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001857 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001858 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001860 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001861 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001862 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001863 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 else
Song Liuc4d4c912015-08-13 14:31:54 -07001865 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 sb->sb_csum = calc_sb_1_csum(sb);
1869}
1870
Chris Webb0cd17fe2008-06-28 08:31:46 +10001871static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001872super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001873{
1874 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001875 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001876 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001877 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001878 if (rdev->data_offset != rdev->new_data_offset)
1879 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001880 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001881 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001882 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001883 max_sectors -= rdev->data_offset;
1884 if (!num_sectors || num_sectors > max_sectors)
1885 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001886 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001887 /* minor version 0 with bitmap we can't move */
1888 return 0;
1889 } else {
1890 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001891 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001892 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001893 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001894 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001895 if (!num_sectors || num_sectors > max_sectors)
1896 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001897 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001898 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001899 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001900 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08001901 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10001902 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11001903 do {
1904 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1905 rdev->sb_page);
1906 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001907 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001908
1909}
1910
1911static int
1912super_1_allow_new_offset(struct md_rdev *rdev,
1913 unsigned long long new_offset)
1914{
1915 /* All necessary checks on new >= old have been done */
1916 struct bitmap *bitmap;
1917 if (new_offset >= rdev->data_offset)
1918 return 1;
1919
1920 /* with 1.0 metadata, there is no metadata to tread on
1921 * so we can always move back */
1922 if (rdev->mddev->minor_version == 0)
1923 return 1;
1924
1925 /* otherwise we must be sure not to step on
1926 * any metadata, so stay:
1927 * 36K beyond start of superblock
1928 * beyond end of badblocks
1929 * beyond write-intent bitmap
1930 */
1931 if (rdev->sb_start + (32+4)*2 > new_offset)
1932 return 0;
1933 bitmap = rdev->mddev->bitmap;
1934 if (bitmap && !rdev->mddev->bitmap_info.file &&
1935 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001936 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001937 return 0;
1938 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1939 return 0;
1940
1941 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001942}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Adrian Bunk75c96f82005-05-05 16:16:09 -07001944static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 [0] = {
1946 .name = "0.90.0",
1947 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001948 .load_super = super_90_load,
1949 .validate_super = super_90_validate,
1950 .sync_super = super_90_sync,
1951 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001952 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 },
1954 [1] = {
1955 .name = "md-1",
1956 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001957 .load_super = super_1_load,
1958 .validate_super = super_1_validate,
1959 .sync_super = super_1_sync,
1960 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001961 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 },
1963};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
NeilBrownfd01b882011-10-11 16:47:53 +11001965static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001966{
1967 if (mddev->sync_super) {
1968 mddev->sync_super(mddev, rdev);
1969 return;
1970 }
1971
1972 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1973
1974 super_types[mddev->major_version].sync_super(mddev, rdev);
1975}
1976
NeilBrownfd01b882011-10-11 16:47:53 +11001977static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
NeilBrown3cb03002011-10-11 16:45:26 +11001979 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
NeilBrown4b809912008-07-21 17:05:25 +10001981 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07001982 rdev_for_each_rcu(rdev, mddev1) {
1983 if (test_bit(Faulty, &rdev->flags) ||
1984 test_bit(Journal, &rdev->flags) ||
1985 rdev->raid_disk == -1)
1986 continue;
1987 rdev_for_each_rcu(rdev2, mddev2) {
1988 if (test_bit(Faulty, &rdev2->flags) ||
1989 test_bit(Journal, &rdev2->flags) ||
1990 rdev2->raid_disk == -1)
1991 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001992 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001993 rdev2->bdev->bd_contains) {
1994 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001995 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001996 }
Song Liu0b020e82015-09-03 23:00:35 -07001997 }
1998 }
NeilBrown4b809912008-07-21 17:05:25 +10001999 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return 0;
2001}
2002
2003static LIST_HEAD(pending_raid_disks);
2004
Andre Nollac5e7112009-08-03 10:59:47 +10002005/*
2006 * Try to register data integrity profile for an mddev
2007 *
2008 * This is called when an array is started and after a disk has been kicked
2009 * from the array. It only succeeds if all working and active component devices
2010 * are integrity capable with matching profiles.
2011 */
NeilBrownfd01b882011-10-11 16:47:53 +11002012int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002013{
NeilBrown3cb03002011-10-11 16:45:26 +11002014 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002015
Andre Nollac5e7112009-08-03 10:59:47 +10002016 if (list_empty(&mddev->disks))
2017 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002018 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2019 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002020 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002021 /* skip spares and non-functional disks */
2022 if (test_bit(Faulty, &rdev->flags))
2023 continue;
2024 if (rdev->raid_disk < 0)
2025 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002026 if (!reference) {
2027 /* Use the first rdev as the reference */
2028 reference = rdev;
2029 continue;
2030 }
2031 /* does this rdev's profile match the reference profile? */
2032 if (blk_integrity_compare(reference->bdev->bd_disk,
2033 rdev->bdev->bd_disk) < 0)
2034 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002035 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002036 if (!reference || !bdev_get_integrity(reference->bdev))
2037 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002038 /*
2039 * All component devices are integrity capable and have matching
2040 * profiles, register the common profile for the md device.
2041 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002042 blk_integrity_register(mddev->gendisk,
2043 bdev_get_integrity(reference->bdev));
2044
NeilBrown9d487392016-11-02 14:16:49 +11002045 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Martin K. Petersena91a2782011-03-17 11:11:05 +01002046 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002047 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002048 mdname(mddev));
2049 return -EINVAL;
2050 }
Andre Nollac5e7112009-08-03 10:59:47 +10002051 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002052}
Andre Nollac5e7112009-08-03 10:59:47 +10002053EXPORT_SYMBOL(md_integrity_register);
2054
Dan Williams1501efa2016-01-13 16:00:07 -08002055/*
2056 * Attempt to add an rdev, but only if it is consistent with the current
2057 * integrity profile
2058 */
2059int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002060{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002061 struct blk_integrity *bi_rdev;
2062 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002063 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002064
2065 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002066 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002067
2068 bi_rdev = bdev_get_integrity(rdev->bdev);
2069 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002070
2071 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002072 return 0;
2073
2074 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002075 pr_err("%s: incompatible integrity profile for %s\n",
2076 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002077 return -ENXIO;
2078 }
2079
2080 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002081}
2082EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002083
NeilBrownf72ffdd2014-09-30 14:23:59 +10002084static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002086 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002087 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002088 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Dan Williams11e2ede2008-04-30 00:52:32 -07002090 /* prevent duplicates */
2091 if (find_rdev(mddev, rdev->bdev->bd_dev))
2092 return -EEXIST;
2093
Andre Nolldd8ac332009-03-31 14:33:13 +11002094 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002095 if (!test_bit(Journal, &rdev->flags) &&
2096 rdev->sectors &&
2097 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002098 if (mddev->pers) {
2099 /* Cannot change size, so fail
2100 * If mddev->level <= 0, then we don't care
2101 * about aligning sizes (e.g. linear)
2102 */
2103 if (mddev->level > 0)
2104 return -ENOSPC;
2105 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002106 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 /* Verify rdev->desc_nr is unique.
2110 * If it is -1, assign a free number, else
2111 * check number is not in use
2112 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002113 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 if (rdev->desc_nr < 0) {
2115 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002116 if (mddev->pers)
2117 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002118 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 choice++;
2120 rdev->desc_nr = choice;
2121 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002122 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002123 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002127 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002128 if (!test_bit(Journal, &rdev->flags) &&
2129 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002130 pr_warn("md: %s: array is limited to %d devices\n",
2131 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002132 return -EBUSY;
2133 }
NeilBrown19133a42005-11-08 21:39:35 -08002134 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002135 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002138 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002139
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002140 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002141 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002142
Tejun Heo0762b8b2008-08-25 19:56:12 +09002143 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002144 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2145 /* failure here is OK */;
2146 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002147
NeilBrown4b809912008-07-21 17:05:25 +10002148 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002149 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002150
2151 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002152 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002155
2156 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002157 pr_warn("md: failed to register dev-%s for %s\n",
2158 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002159 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
NeilBrown177a99b2008-02-06 01:39:56 -08002162static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002163{
NeilBrown3cb03002011-10-11 16:45:26 +11002164 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002165 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002166 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002167}
2168
NeilBrownf72ffdd2014-09-30 14:23:59 +10002169static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
2171 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002172
Tejun Heo49731ba2011-01-14 18:43:57 +01002173 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002174 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002175 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002177 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002178 sysfs_put(rdev->sysfs_state);
2179 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002180 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002181 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002182 * writing to 'remove' to "dev/state". We also need
2183 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002184 */
NeilBrown4b809912008-07-21 17:05:25 +10002185 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002186 INIT_WORK(&rdev->del_work, md_delayed_delete);
2187 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002188 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
2191/*
2192 * prevent the device from being mounted, repartitioned or
2193 * otherwise reused by a RAID array (or any other kernel
2194 * subsystem), by bd_claiming the device.
2195 */
NeilBrown3cb03002011-10-11 16:45:26 +11002196static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
2198 int err = 0;
2199 struct block_device *bdev;
2200 char b[BDEVNAME_SIZE];
2201
Tejun Heod4d77622010-11-13 11:55:18 +01002202 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002203 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002205 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 return PTR_ERR(bdev);
2207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 rdev->bdev = bdev;
2209 return err;
2210}
2211
NeilBrown3cb03002011-10-11 16:45:26 +11002212static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
2214 struct block_device *bdev = rdev->bdev;
2215 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002216 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
2219void md_autodetect_dev(dev_t dev);
2220
NeilBrownf72ffdd2014-09-30 14:23:59 +10002221static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
2223 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002224
NeilBrown9d487392016-11-02 14:16:49 +11002225 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002226 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002228 if (test_bit(AutoDetected, &rdev->flags))
2229 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230#endif
2231 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002232 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002235void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
2237 unbind_rdev_from_array(rdev);
2238 export_rdev(rdev);
2239}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002240EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
NeilBrownfd01b882011-10-11 16:47:53 +11002242static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
NeilBrown0638bb02014-09-25 17:43:47 +10002244 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
NeilBrown0638bb02014-09-25 17:43:47 +10002246 while (!list_empty(&mddev->disks)) {
2247 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2248 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002249 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 mddev->raid_disks = 0;
2252 mddev->major_version = 0;
2253}
2254
NeilBrownf72ffdd2014-09-30 14:23:59 +10002255static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
NeilBrown42543762006-06-26 00:27:57 -07002257 /* Update each superblock (in-memory image), but
2258 * if we are allowed to, skip spares which already
2259 * have the right event counter, or have one earlier
2260 * (which would mean they aren't being marked as dirty
2261 * with the rest of the array)
2262 */
NeilBrown3cb03002011-10-11 16:45:26 +11002263 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002264 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002265 if (rdev->sb_events == mddev->events ||
2266 (nospares &&
2267 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002268 rdev->sb_events+1 == mddev->events)) {
2269 /* Don't update this superblock */
2270 rdev->sb_loaded = 2;
2271 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002272 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002273 rdev->sb_loaded = 1;
2274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
2276}
2277
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002278static bool does_sb_need_changing(struct mddev *mddev)
2279{
2280 struct md_rdev *rdev;
2281 struct mdp_superblock_1 *sb;
2282 int role;
2283
2284 /* Find a good rdev */
2285 rdev_for_each(rdev, mddev)
2286 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2287 break;
2288
2289 /* No good device found. */
2290 if (!rdev)
2291 return false;
2292
2293 sb = page_address(rdev->sb_page);
2294 /* Check if a device has become faulty or a spare become active */
2295 rdev_for_each(rdev, mddev) {
2296 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2297 /* Device activated? */
2298 if (role == 0xffff && rdev->raid_disk >=0 &&
2299 !test_bit(Faulty, &rdev->flags))
2300 return true;
2301 /* Device turned faulty? */
2302 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2303 return true;
2304 }
2305
2306 /* Check if any mddev parameters have changed */
2307 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2308 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002309 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002310 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2311 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2312 return true;
2313
2314 return false;
2315}
2316
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002317void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
NeilBrown3cb03002011-10-11 16:45:26 +11002319 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002320 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002321 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002322 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002323 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
NeilBrownd87f0642013-04-24 11:42:40 +10002325 if (mddev->ro) {
2326 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002327 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002328 return;
2329 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002330
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002331repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002332 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002333 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002334 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002335 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002336 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002337 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002338 /* Has someone else has updated the sb */
2339 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002340 if (ret == 0)
2341 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002342 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2343 BIT(MD_SB_CHANGE_DEVS) |
2344 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002345 return;
2346 }
2347 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002348
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002349 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002350 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002351 if (rdev->raid_disk >= 0 &&
2352 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002353 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002354 !test_bit(In_sync, &rdev->flags) &&
2355 mddev->curr_resync_completed > rdev->recovery_offset)
2356 rdev->recovery_offset = mddev->curr_resync_completed;
2357
NeilBrownf72ffdd2014-09-30 14:23:59 +10002358 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002359 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002360 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2361 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002362 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002363 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002364 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002365 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002366 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002367 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002368 md_error(mddev, rdev);
2369 }
2370 clear_bit(Blocked, &rdev->flags);
2371 clear_bit(BlockedBadBlocks, &rdev->flags);
2372 wake_up(&rdev->blocked_wait);
2373 }
2374 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002375 wake_up(&mddev->sb_wait);
2376 return;
2377 }
2378
NeilBrown85572d72014-12-15 12:56:56 +11002379 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002380
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002381 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002382
Shaohua Li29530792016-12-08 15:48:19 -08002383 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002384 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002385 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002386 /* just a clean<-> dirty transition, possibly leave spares alone,
2387 * though if events isn't the right even/odd, we will have to do
2388 * spares after all
2389 */
2390 nospares = 1;
2391 if (force_change)
2392 nospares = 0;
2393 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002394 /* If the array is degraded, then skipping spares is both
2395 * dangerous and fairly pointless.
2396 * Dangerous because a device that was removed from the array
2397 * might have a event_count that still looks up-to-date,
2398 * so it can be re-added without a resync.
2399 * Pointless because if there are any spares to skip,
2400 * then a recovery will happen and soon that array won't
2401 * be degraded any more and the spare can go back to sleep then.
2402 */
NeilBrown850b2b422006-10-03 01:15:46 -07002403 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002404
NeilBrown06d91a52005-06-21 17:17:12 -07002405 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002406
2407 /* If this is just a dirty<->clean transition, and the array is clean
2408 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002409 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002410 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002411 && mddev->can_decrease_events
2412 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002413 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002414 mddev->can_decrease_events = 0;
2415 } else {
NeilBrown42543762006-06-26 00:27:57 -07002416 /* otherwise we have to go forward and ... */
2417 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002418 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
NeilBrown403df472014-09-30 15:52:29 +10002421 /*
2422 * This 64-bit counter should never wrap.
2423 * Either we are in around ~1 trillion A.C., assuming
2424 * 1 reboot per second, or we have a bug...
2425 */
2426 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002427
NeilBrowndafb20f2012-03-19 12:46:39 +11002428 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002429 if (rdev->badblocks.changed)
2430 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002431 if (test_bit(Faulty, &rdev->flags))
2432 set_bit(FaultRecorded, &rdev->flags);
2433 }
NeilBrown2699b672011-07-28 11:31:47 +10002434
NeilBrowne6910632008-02-06 01:39:51 -08002435 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002436 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
NeilBrown36a4e1f2011-10-07 14:23:17 +11002438 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2439 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Shaohua Li504634f2016-11-18 09:44:08 -08002441 if (mddev->queue)
2442 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002443rewrite:
NeilBrown4ad13662007-07-17 04:06:13 -07002444 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002445 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002447
NeilBrown42543762006-06-26 00:27:57 -07002448 if (rdev->sb_loaded != 1)
2449 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
NeilBrownf4667222013-12-09 12:04:56 +11002451 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002452 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002453 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002454 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002455 pr_debug("md: (write) %s's sb offset: %llu\n",
2456 bdevname(rdev->bdev, b),
2457 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002458 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002459 if (rdev->badblocks.size) {
2460 md_super_write(mddev, rdev,
2461 rdev->badblocks.sector,
2462 rdev->badblocks.size << 9,
2463 rdev->bb_page);
2464 rdev->badblocks.size = 0;
2465 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002466
NeilBrownf4667222013-12-09 12:04:56 +11002467 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002468 pr_debug("md: %s (skipping faulty)\n",
2469 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002470
NeilBrown7bfa19f2005-06-21 17:17:28 -07002471 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /* only need to write one superblock... */
2473 break;
2474 }
NeilBrown46533ff2016-11-18 16:16:11 +11002475 if (md_super_wait(mddev) < 0)
2476 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002477 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002478
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002479 if (mddev_is_clustered(mddev) && ret == 0)
2480 md_cluster_ops->metadata_update_finish(mddev);
2481
NeilBrown850b2b422006-10-03 01:15:46 -07002482 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002483 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2484 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002485 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002486 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002487 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002488 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2489 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002490
NeilBrowndafb20f2012-03-19 12:46:39 +11002491 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002492 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2493 clear_bit(Blocked, &rdev->flags);
2494
2495 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002496 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002497 clear_bit(BlockedBadBlocks, &rdev->flags);
2498 wake_up(&rdev->blocked_wait);
2499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002501EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002503static int add_bound_rdev(struct md_rdev *rdev)
2504{
2505 struct mddev *mddev = rdev->mddev;
2506 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002507 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002508
Shaohua Li87d4d912016-01-06 14:37:14 -08002509 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002510 /* If there is hot_add_disk but no hot_remove_disk
2511 * then added disks for geometry changes,
2512 * and should be added immediately.
2513 */
2514 super_types[mddev->major_version].
2515 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002516 if (add_journal)
2517 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002518 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002519 if (add_journal)
2520 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002521 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002522 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002523 return err;
2524 }
2525 }
2526 sysfs_notify_dirent_safe(rdev->sysfs_state);
2527
Shaohua Li29530792016-12-08 15:48:19 -08002528 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002529 if (mddev->degraded)
2530 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2531 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2532 md_new_event(mddev);
2533 md_wakeup_thread(mddev->thread);
2534 return 0;
2535}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Andre Noll7f6ce762008-03-23 18:34:54 +01002537/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002538 * We want to accept with case. For this we use cmd_match.
2539 */
2540static int cmd_match(const char *cmd, const char *str)
2541{
2542 /* See if cmd, written into a sysfs file, matches
2543 * str. They must either be the same, or cmd can
2544 * have a trailing newline
2545 */
2546 while (*cmd && *str && *cmd == *str) {
2547 cmd++;
2548 str++;
2549 }
2550 if (*cmd == '\n')
2551 cmd++;
2552 if (*str || *cmd)
2553 return 0;
2554 return 1;
2555}
2556
NeilBrown86e6ffd2005-11-08 21:39:24 -08002557struct rdev_sysfs_entry {
2558 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002559 ssize_t (*show)(struct md_rdev *, char *);
2560 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002561};
2562
2563static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002564state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002565{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002566 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002567 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002568 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002569
NeilBrown758bfc82014-12-15 12:56:59 +11002570 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002571 (!test_bit(ExternalBbl, &flags) &&
2572 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002573 len += sprintf(page+len, "faulty%s", sep);
2574 if (test_bit(In_sync, &flags))
2575 len += sprintf(page+len, "in_sync%s", sep);
2576 if (test_bit(Journal, &flags))
2577 len += sprintf(page+len, "journal%s", sep);
2578 if (test_bit(WriteMostly, &flags))
2579 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002580 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002581 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002582 && !test_bit(Faulty, &flags)))
2583 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002584 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002585 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002586 !test_bit(In_sync, &flags))
2587 len += sprintf(page+len, "spare%s", sep);
2588 if (test_bit(WriteErrorSeen, &flags))
2589 len += sprintf(page+len, "write_error%s", sep);
2590 if (test_bit(WantReplacement, &flags))
2591 len += sprintf(page+len, "want_replacement%s", sep);
2592 if (test_bit(Replacement, &flags))
2593 len += sprintf(page+len, "replacement%s", sep);
2594 if (test_bit(ExternalBbl, &flags))
2595 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002596 if (test_bit(FailFast, &flags))
2597 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002598
2599 if (len)
2600 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002601
NeilBrown86e6ffd2005-11-08 21:39:24 -08002602 return len+sprintf(page+len, "\n");
2603}
2604
NeilBrown45dc2de2006-06-26 00:27:58 -07002605static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002606state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002607{
2608 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002609 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002610 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002611 * writemostly - sets write_mostly
2612 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002613 * blocked - sets the Blocked flags
2614 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002615 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002616 * -insync - clear Insync for a device with a slot assigned,
2617 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002618 * write_error - sets WriteErrorSeen
2619 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002620 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002621 */
2622 int err = -EINVAL;
2623 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2624 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002625 if (test_bit(Faulty, &rdev->flags))
2626 err = 0;
2627 else
2628 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002629 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002630 if (rdev->mddev->pers) {
2631 clear_bit(Blocked, &rdev->flags);
2632 remove_and_add_spares(rdev->mddev, rdev);
2633 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002634 if (rdev->raid_disk >= 0)
2635 err = -EBUSY;
2636 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002637 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002638 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002639 if (mddev_is_clustered(mddev))
2640 err = md_cluster_ops->remove_disk(mddev, rdev);
2641
2642 if (err == 0) {
2643 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002644 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002645 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002646 md_wakeup_thread(mddev->thread);
2647 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002648 md_new_event(mddev);
2649 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002650 }
NeilBrownf6556752006-06-26 00:28:01 -07002651 } else if (cmd_match(buf, "writemostly")) {
2652 set_bit(WriteMostly, &rdev->flags);
2653 err = 0;
2654 } else if (cmd_match(buf, "-writemostly")) {
2655 clear_bit(WriteMostly, &rdev->flags);
2656 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002657 } else if (cmd_match(buf, "blocked")) {
2658 set_bit(Blocked, &rdev->flags);
2659 err = 0;
2660 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002661 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002662 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002663 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002664 /* metadata handler doesn't understand badblocks,
2665 * so we need to fail the device
2666 */
2667 md_error(rdev->mddev, rdev);
2668 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002669 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002670 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002671 wake_up(&rdev->blocked_wait);
2672 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2673 md_wakeup_thread(rdev->mddev->thread);
2674
2675 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002676 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2677 set_bit(In_sync, &rdev->flags);
2678 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11002679 } else if (cmd_match(buf, "failfast")) {
2680 set_bit(FailFast, &rdev->flags);
2681 err = 0;
2682 } else if (cmd_match(buf, "-failfast")) {
2683 clear_bit(FailFast, &rdev->flags);
2684 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002685 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2686 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002687 if (rdev->mddev->pers == NULL) {
2688 clear_bit(In_sync, &rdev->flags);
2689 rdev->saved_raid_disk = rdev->raid_disk;
2690 rdev->raid_disk = -1;
2691 err = 0;
2692 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002693 } else if (cmd_match(buf, "write_error")) {
2694 set_bit(WriteErrorSeen, &rdev->flags);
2695 err = 0;
2696 } else if (cmd_match(buf, "-write_error")) {
2697 clear_bit(WriteErrorSeen, &rdev->flags);
2698 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002699 } else if (cmd_match(buf, "want_replacement")) {
2700 /* Any non-spare device that is not a replacement can
2701 * become want_replacement at any time, but we then need to
2702 * check if recovery is needed.
2703 */
2704 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002705 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002706 !test_bit(Replacement, &rdev->flags))
2707 set_bit(WantReplacement, &rdev->flags);
2708 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2709 md_wakeup_thread(rdev->mddev->thread);
2710 err = 0;
2711 } else if (cmd_match(buf, "-want_replacement")) {
2712 /* Clearing 'want_replacement' is always allowed.
2713 * Once replacements starts it is too late though.
2714 */
2715 err = 0;
2716 clear_bit(WantReplacement, &rdev->flags);
2717 } else if (cmd_match(buf, "replacement")) {
2718 /* Can only set a device as a replacement when array has not
2719 * yet been started. Once running, replacement is automatic
2720 * from spares, or by assigning 'slot'.
2721 */
2722 if (rdev->mddev->pers)
2723 err = -EBUSY;
2724 else {
2725 set_bit(Replacement, &rdev->flags);
2726 err = 0;
2727 }
2728 } else if (cmd_match(buf, "-replacement")) {
2729 /* Similarly, can only clear Replacement before start */
2730 if (rdev->mddev->pers)
2731 err = -EBUSY;
2732 else {
2733 clear_bit(Replacement, &rdev->flags);
2734 err = 0;
2735 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002736 } else if (cmd_match(buf, "re-add")) {
2737 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002738 /* clear_bit is performed _after_ all the devices
2739 * have their local Faulty bit cleared. If any writes
2740 * happen in the meantime in the local node, they
2741 * will land in the local bitmap, which will be synced
2742 * by this node eventually
2743 */
2744 if (!mddev_is_clustered(rdev->mddev) ||
2745 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2746 clear_bit(Faulty, &rdev->flags);
2747 err = add_bound_rdev(rdev);
2748 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002749 } else
2750 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002751 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2752 set_bit(ExternalBbl, &rdev->flags);
2753 rdev->badblocks.shift = 0;
2754 err = 0;
2755 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2756 clear_bit(ExternalBbl, &rdev->flags);
2757 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002758 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002759 if (!err)
2760 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002761 return err ? err : len;
2762}
NeilBrown80ca3a42006-07-10 04:44:18 -07002763static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002764__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002765
2766static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002767errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002768{
2769 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2770}
2771
2772static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002773errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002774{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002775 unsigned int n;
2776 int rv;
2777
2778 rv = kstrtouint(buf, 10, &n);
2779 if (rv < 0)
2780 return rv;
2781 atomic_set(&rdev->corrected_errors, n);
2782 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002783}
2784static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002785__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002786
NeilBrown014236d2006-01-06 00:20:55 -08002787static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002788slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002789{
Shaohua Lif2076e72015-10-08 21:54:12 -07002790 if (test_bit(Journal, &rdev->flags))
2791 return sprintf(page, "journal\n");
2792 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002793 return sprintf(page, "none\n");
2794 else
2795 return sprintf(page, "%d\n", rdev->raid_disk);
2796}
2797
2798static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002799slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002800{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002801 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002802 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002803
Shaohua Lif2076e72015-10-08 21:54:12 -07002804 if (test_bit(Journal, &rdev->flags))
2805 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002806 if (strncmp(buf, "none", 4)==0)
2807 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002808 else {
2809 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2810 if (err < 0)
2811 return err;
2812 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002813 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002814 /* Setting 'slot' on an active array requires also
2815 * updating the 'rd%d' link, and communicating
2816 * with the personality with ->hot_*_disk.
2817 * For now we only support removing
2818 * failed/spare devices. This normally happens automatically,
2819 * but not when the metadata is externally managed.
2820 */
NeilBrownc303da62008-02-06 01:39:51 -08002821 if (rdev->raid_disk == -1)
2822 return -EEXIST;
2823 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002824 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002825 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002826 clear_bit(Blocked, &rdev->flags);
2827 remove_and_add_spares(rdev->mddev, rdev);
2828 if (rdev->raid_disk >= 0)
2829 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002830 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2831 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002832 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002833 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002834 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002835 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002836 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002837
2838 if (rdev->raid_disk != -1)
2839 return -EBUSY;
2840
NeilBrownc6751b22011-02-02 11:57:13 +11002841 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2842 return -EBUSY;
2843
Neil Brown6c2fce22008-06-28 08:31:31 +10002844 if (rdev->mddev->pers->hot_add_disk == NULL)
2845 return -EINVAL;
2846
NeilBrownba1b41b2011-01-14 09:14:34 +11002847 if (slot >= rdev->mddev->raid_disks &&
2848 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2849 return -ENOSPC;
2850
Neil Brown6c2fce22008-06-28 08:31:31 +10002851 rdev->raid_disk = slot;
2852 if (test_bit(In_sync, &rdev->flags))
2853 rdev->saved_raid_disk = slot;
2854 else
2855 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002856 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002857 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002858 err = rdev->mddev->pers->
2859 hot_add_disk(rdev->mddev, rdev);
2860 if (err) {
2861 rdev->raid_disk = -1;
2862 return err;
2863 } else
2864 sysfs_notify_dirent_safe(rdev->sysfs_state);
2865 if (sysfs_link_rdev(rdev->mddev, rdev))
2866 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002867 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002868 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002869 if (slot >= rdev->mddev->raid_disks &&
2870 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002871 return -ENOSPC;
2872 rdev->raid_disk = slot;
2873 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002874 clear_bit(Faulty, &rdev->flags);
2875 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002876 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002877 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002878 }
NeilBrown014236d2006-01-06 00:20:55 -08002879 return len;
2880}
2881
NeilBrown014236d2006-01-06 00:20:55 -08002882static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002883__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002884
NeilBrown93c8cad2006-01-06 00:20:56 -08002885static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002886offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002887{
Andrew Morton6961ece2006-01-06 00:20:59 -08002888 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002889}
2890
2891static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002892offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002893{
NeilBrownc6563a82012-05-21 09:27:00 +10002894 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002895 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002896 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002897 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002898 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002899 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002900 /* Must set offset before size, so overlap checks
2901 * can be sane */
2902 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002903 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002904 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002905 return len;
2906}
2907
2908static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002909__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002910
NeilBrownc6563a82012-05-21 09:27:00 +10002911static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2912{
2913 return sprintf(page, "%llu\n",
2914 (unsigned long long)rdev->new_data_offset);
2915}
2916
2917static ssize_t new_offset_store(struct md_rdev *rdev,
2918 const char *buf, size_t len)
2919{
2920 unsigned long long new_offset;
2921 struct mddev *mddev = rdev->mddev;
2922
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002923 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002924 return -EINVAL;
2925
NeilBrownf851b602014-12-11 10:02:10 +11002926 if (mddev->sync_thread ||
2927 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002928 return -EBUSY;
2929 if (new_offset == rdev->data_offset)
2930 /* reset is always permitted */
2931 ;
2932 else if (new_offset > rdev->data_offset) {
2933 /* must not push array size beyond rdev_sectors */
2934 if (new_offset - rdev->data_offset
2935 + mddev->dev_sectors > rdev->sectors)
2936 return -E2BIG;
2937 }
2938 /* Metadata worries about other space details. */
2939
2940 /* decreasing the offset is inconsistent with a backwards
2941 * reshape.
2942 */
2943 if (new_offset < rdev->data_offset &&
2944 mddev->reshape_backwards)
2945 return -EINVAL;
2946 /* Increasing offset is inconsistent with forwards
2947 * reshape. reshape_direction should be set to
2948 * 'backwards' first.
2949 */
2950 if (new_offset > rdev->data_offset &&
2951 !mddev->reshape_backwards)
2952 return -EINVAL;
2953
2954 if (mddev->pers && mddev->persistent &&
2955 !super_types[mddev->major_version]
2956 .allow_new_offset(rdev, new_offset))
2957 return -E2BIG;
2958 rdev->new_data_offset = new_offset;
2959 if (new_offset > rdev->data_offset)
2960 mddev->reshape_backwards = 1;
2961 else if (new_offset < rdev->data_offset)
2962 mddev->reshape_backwards = 0;
2963
2964 return len;
2965}
2966static struct rdev_sysfs_entry rdev_new_offset =
2967__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2968
NeilBrown83303b62006-01-06 00:21:06 -08002969static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002970rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002971{
Andre Nolldd8ac332009-03-31 14:33:13 +11002972 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002973}
2974
NeilBrownc5d79ad2008-02-06 01:39:54 -08002975static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2976{
2977 /* check if two start/length pairs overlap */
2978 if (s1+l1 <= s2)
2979 return 0;
2980 if (s2+l2 <= s1)
2981 return 0;
2982 return 1;
2983}
2984
Dan Williamsb522adc2009-03-31 15:00:31 +11002985static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2986{
2987 unsigned long long blocks;
2988 sector_t new;
2989
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002990 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002991 return -EINVAL;
2992
2993 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2994 return -EINVAL; /* sector conversion overflow */
2995
2996 new = blocks * 2;
2997 if (new != blocks * 2)
2998 return -EINVAL; /* unsigned long long to sector_t overflow */
2999
3000 *sectors = new;
3001 return 0;
3002}
3003
NeilBrown83303b62006-01-06 00:21:06 -08003004static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003005rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003006{
NeilBrownfd01b882011-10-11 16:47:53 +11003007 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003008 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003009 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003010
Shaohua Lif2076e72015-10-08 21:54:12 -07003011 if (test_bit(Journal, &rdev->flags))
3012 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003013 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003014 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003015 if (rdev->data_offset != rdev->new_data_offset)
3016 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003017 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003018 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003019 sectors = super_types[my_mddev->major_version].
3020 rdev_size_change(rdev, sectors);
3021 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003022 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003023 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003024 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003025 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003026 if (!my_mddev->pers->resize)
3027 /* Cannot change size for RAID0 or Linear etc */
3028 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003029 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003030 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003031 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003032
Andre Nolldd8ac332009-03-31 14:33:13 +11003033 rdev->sectors = sectors;
3034 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003035 /* Need to check that all other rdevs with the same
3036 * ->bdev do not overlap. 'rcu' is sufficient to walk
3037 * the rdev lists safely.
3038 * This check does not provide a hard guarantee, it
3039 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003040 */
NeilBrownfd01b882011-10-11 16:47:53 +11003041 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003042 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003043 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003044
NeilBrown8b1afc32014-09-29 15:33:20 +10003045 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003046 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003047 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003048
NeilBrowndafb20f2012-03-19 12:46:39 +11003049 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003050 if (rdev->bdev == rdev2->bdev &&
3051 rdev != rdev2 &&
3052 overlaps(rdev->data_offset, rdev->sectors,
3053 rdev2->data_offset,
3054 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003055 overlap = 1;
3056 break;
3057 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003058 if (overlap) {
3059 mddev_put(mddev);
3060 break;
3061 }
3062 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003063 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003064 if (overlap) {
3065 /* Someone else could have slipped in a size
3066 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003067 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003068 * safe, and trust userspace not to race with
3069 * itself
3070 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003071 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003072 return -EBUSY;
3073 }
3074 }
NeilBrown83303b62006-01-06 00:21:06 -08003075 return len;
3076}
3077
3078static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003079__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003080
NeilBrown3cb03002011-10-11 16:45:26 +11003081static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003082{
3083 unsigned long long recovery_start = rdev->recovery_offset;
3084
3085 if (test_bit(In_sync, &rdev->flags) ||
3086 recovery_start == MaxSector)
3087 return sprintf(page, "none\n");
3088
3089 return sprintf(page, "%llu\n", recovery_start);
3090}
3091
NeilBrown3cb03002011-10-11 16:45:26 +11003092static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003093{
3094 unsigned long long recovery_start;
3095
3096 if (cmd_match(buf, "none"))
3097 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003098 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003099 return -EINVAL;
3100
3101 if (rdev->mddev->pers &&
3102 rdev->raid_disk >= 0)
3103 return -EBUSY;
3104
3105 rdev->recovery_offset = recovery_start;
3106 if (recovery_start == MaxSector)
3107 set_bit(In_sync, &rdev->flags);
3108 else
3109 clear_bit(In_sync, &rdev->flags);
3110 return len;
3111}
3112
3113static struct rdev_sysfs_entry rdev_recovery_start =
3114__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3115
Vishal Vermafc974ee2015-12-24 19:20:34 -07003116/* sysfs access to bad-blocks list.
3117 * We present two files.
3118 * 'bad-blocks' lists sector numbers and lengths of ranges that
3119 * are recorded as bad. The list is truncated to fit within
3120 * the one-page limit of sysfs.
3121 * Writing "sector length" to this file adds an acknowledged
3122 * bad block list.
3123 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3124 * been acknowledged. Writing to this file adds bad blocks
3125 * without acknowledging them. This is largely for testing.
3126 */
NeilBrown3cb03002011-10-11 16:45:26 +11003127static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003128{
3129 return badblocks_show(&rdev->badblocks, page, 0);
3130}
NeilBrown3cb03002011-10-11 16:45:26 +11003131static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003132{
NeilBrownde393cd2011-07-28 11:31:48 +10003133 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3134 /* Maybe that ack was all we needed */
3135 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3136 wake_up(&rdev->blocked_wait);
3137 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003138}
3139static struct rdev_sysfs_entry rdev_bad_blocks =
3140__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3141
NeilBrown3cb03002011-10-11 16:45:26 +11003142static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003143{
3144 return badblocks_show(&rdev->badblocks, page, 1);
3145}
NeilBrown3cb03002011-10-11 16:45:26 +11003146static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003147{
3148 return badblocks_store(&rdev->badblocks, page, len, 1);
3149}
3150static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3151__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3152
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003153static ssize_t
3154ppl_sector_show(struct md_rdev *rdev, char *page)
3155{
3156 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3157}
3158
3159static ssize_t
3160ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3161{
3162 unsigned long long sector;
3163
3164 if (kstrtoull(buf, 10, &sector) < 0)
3165 return -EINVAL;
3166 if (sector != (sector_t)sector)
3167 return -EINVAL;
3168
3169 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3170 rdev->raid_disk >= 0)
3171 return -EBUSY;
3172
3173 if (rdev->mddev->persistent) {
3174 if (rdev->mddev->major_version == 0)
3175 return -EINVAL;
3176 if ((sector > rdev->sb_start &&
3177 sector - rdev->sb_start > S16_MAX) ||
3178 (sector < rdev->sb_start &&
3179 rdev->sb_start - sector > -S16_MIN))
3180 return -EINVAL;
3181 rdev->ppl.offset = sector - rdev->sb_start;
3182 } else if (!rdev->mddev->external) {
3183 return -EBUSY;
3184 }
3185 rdev->ppl.sector = sector;
3186 return len;
3187}
3188
3189static struct rdev_sysfs_entry rdev_ppl_sector =
3190__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3191
3192static ssize_t
3193ppl_size_show(struct md_rdev *rdev, char *page)
3194{
3195 return sprintf(page, "%u\n", rdev->ppl.size);
3196}
3197
3198static ssize_t
3199ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3200{
3201 unsigned int size;
3202
3203 if (kstrtouint(buf, 10, &size) < 0)
3204 return -EINVAL;
3205
3206 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3207 rdev->raid_disk >= 0)
3208 return -EBUSY;
3209
3210 if (rdev->mddev->persistent) {
3211 if (rdev->mddev->major_version == 0)
3212 return -EINVAL;
3213 if (size > U16_MAX)
3214 return -EINVAL;
3215 } else if (!rdev->mddev->external) {
3216 return -EBUSY;
3217 }
3218 rdev->ppl.size = size;
3219 return len;
3220}
3221
3222static struct rdev_sysfs_entry rdev_ppl_size =
3223__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3224
NeilBrown86e6ffd2005-11-08 21:39:24 -08003225static struct attribute *rdev_default_attrs[] = {
3226 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003227 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003228 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003229 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003230 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003231 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003232 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003233 &rdev_bad_blocks.attr,
3234 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003235 &rdev_ppl_sector.attr,
3236 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003237 NULL,
3238};
3239static ssize_t
3240rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3241{
3242 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003243 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003244
3245 if (!entry->show)
3246 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003247 if (!rdev->mddev)
3248 return -EBUSY;
3249 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003250}
3251
3252static ssize_t
3253rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3254 const char *page, size_t length)
3255{
3256 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003257 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003258 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003259 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003260
3261 if (!entry->store)
3262 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003263 if (!capable(CAP_SYS_ADMIN))
3264 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003265 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003266 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003267 if (rdev->mddev == NULL)
3268 rv = -EBUSY;
3269 else
3270 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003271 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003272 }
3273 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003274}
3275
3276static void rdev_free(struct kobject *ko)
3277{
NeilBrown3cb03002011-10-11 16:45:26 +11003278 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003279 kfree(rdev);
3280}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003281static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003282 .show = rdev_attr_show,
3283 .store = rdev_attr_store,
3284};
3285static struct kobj_type rdev_ktype = {
3286 .release = rdev_free,
3287 .sysfs_ops = &rdev_sysfs_ops,
3288 .default_attrs = rdev_default_attrs,
3289};
3290
NeilBrown3cb03002011-10-11 16:45:26 +11003291int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003292{
3293 rdev->desc_nr = -1;
3294 rdev->saved_raid_disk = -1;
3295 rdev->raid_disk = -1;
3296 rdev->flags = 0;
3297 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003298 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003299 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003300 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003301 rdev->sb_loaded = 0;
3302 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003303 atomic_set(&rdev->nr_pending, 0);
3304 atomic_set(&rdev->read_errors, 0);
3305 atomic_set(&rdev->corrected_errors, 0);
3306
3307 INIT_LIST_HEAD(&rdev->same_set);
3308 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003309
3310 /* Add space to store bad block list.
3311 * This reserves the space even on arrays where it cannot
3312 * be used - I wonder if that matters
3313 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003314 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003315}
3316EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317/*
3318 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3319 *
3320 * mark the device faulty if:
3321 *
3322 * - the device is nonexistent (zero size)
3323 * - the device has no valid superblock
3324 *
3325 * a faulty rdev _never_ has rdev->sb set.
3326 */
NeilBrown3cb03002011-10-11 16:45:26 +11003327static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328{
3329 char b[BDEVNAME_SIZE];
3330 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003331 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 sector_t size;
3333
NeilBrown9ffae0c2006-01-06 00:20:32 -08003334 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003335 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
NeilBrown2230dfe2011-07-28 11:31:46 +10003338 err = md_rdev_init(rdev);
3339 if (err)
3340 goto abort_free;
3341 err = alloc_disk_sb(rdev);
3342 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 goto abort_free;
3344
NeilBrownc5d79ad2008-02-06 01:39:54 -08003345 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 if (err)
3347 goto abort_free;
3348
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003349 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003350
Mike Snitzer77304d22010-11-08 14:39:12 +01003351 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003353 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 bdevname(rdev->bdev,b));
3355 err = -EINVAL;
3356 goto abort_free;
3357 }
3358
3359 if (super_format >= 0) {
3360 err = super_types[super_format].
3361 load_super(rdev, NULL, super_minor);
3362 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003363 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003364 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003365 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 goto abort_free;
3367 }
3368 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003369 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 bdevname(rdev->bdev,b));
3371 goto abort_free;
3372 }
3373 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003374
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 return rdev;
3376
3377abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003378 if (rdev->bdev)
3379 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003380 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 kfree(rdev);
3382 return ERR_PTR(err);
3383}
3384
3385/*
3386 * Check a full RAID array for plausibility
3387 */
3388
NeilBrownf72ffdd2014-09-30 14:23:59 +10003389static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
3391 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003392 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 char b[BDEVNAME_SIZE];
3394
3395 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003396 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 switch (super_types[mddev->major_version].
3398 load_super(rdev, freshest, mddev->minor_version)) {
3399 case 1:
3400 freshest = rdev;
3401 break;
3402 case 0:
3403 break;
3404 default:
NeilBrown9d487392016-11-02 14:16:49 +11003405 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003407 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
3409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 super_types[mddev->major_version].
3411 validate_super(mddev, freshest);
3412
3413 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003414 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003415 if (mddev->max_disks &&
3416 (rdev->desc_nr >= mddev->max_disks ||
3417 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003418 pr_warn("md: %s: %s: only %d devices permitted\n",
3419 mdname(mddev), bdevname(rdev->bdev, b),
3420 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003421 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003422 continue;
3423 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003424 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 if (super_types[mddev->major_version].
3426 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003427 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003429 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 continue;
3431 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 if (mddev->level == LEVEL_MULTIPATH) {
3434 rdev->desc_nr = i++;
3435 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003436 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003437 } else if (rdev->raid_disk >=
3438 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3439 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003440 rdev->raid_disk = -1;
3441 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 }
3443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444}
3445
NeilBrown72e02072009-12-14 12:49:55 +11003446/* Read a fixed-point number.
3447 * Numbers in sysfs attributes should be in "standard" units where
3448 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003449 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003450 * milliseconds or jiffies.
3451 * This function takes a decimal number with a possible fractional
3452 * component, and produces an integer which is the result of
3453 * multiplying that number by 10^'scale'.
3454 * all without any floating-point arithmetic.
3455 */
3456int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3457{
3458 unsigned long result = 0;
3459 long decimals = -1;
3460 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3461 if (*cp == '.')
3462 decimals = 0;
3463 else if (decimals < scale) {
3464 unsigned int value;
3465 value = *cp - '0';
3466 result = result * 10 + value;
3467 if (decimals >= 0)
3468 decimals++;
3469 }
3470 cp++;
3471 }
3472 if (*cp == '\n')
3473 cp++;
3474 if (*cp)
3475 return -EINVAL;
3476 if (decimals < 0)
3477 decimals = 0;
3478 while (decimals < scale) {
3479 result *= 10;
3480 decimals ++;
3481 }
3482 *res = result;
3483 return 0;
3484}
3485
NeilBrowneae17012005-11-08 21:39:23 -08003486static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003487safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003488{
3489 int msec = (mddev->safemode_delay*1000)/HZ;
3490 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3491}
3492static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003493safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003494{
NeilBrown16f17b32006-06-26 00:27:37 -07003495 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003496
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003497 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003498 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003499 return -EINVAL;
3500 }
3501
NeilBrown72e02072009-12-14 12:49:55 +11003502 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003503 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003504 if (msec == 0)
3505 mddev->safemode_delay = 0;
3506 else {
NeilBrown19052c02008-08-05 15:54:13 +10003507 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003508 unsigned long new_delay = (msec*HZ)/1000;
3509
3510 if (new_delay == 0)
3511 new_delay = 1;
3512 mddev->safemode_delay = new_delay;
3513 if (new_delay < old_delay || old_delay == 0)
3514 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003515 }
3516 return len;
3517}
3518static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003519__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003520
3521static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003522level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003523{
NeilBrown36d091f2014-12-15 12:56:58 +11003524 struct md_personality *p;
3525 int ret;
3526 spin_lock(&mddev->lock);
3527 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003528 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003529 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003530 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003531 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003532 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003533 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003534 else
NeilBrown36d091f2014-12-15 12:56:58 +11003535 ret = 0;
3536 spin_unlock(&mddev->lock);
3537 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003538}
3539
NeilBrownd9d166c2006-01-06 00:20:51 -08003540static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003541level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003542{
Dan Williamsf2859af2010-05-02 10:04:16 -07003543 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003544 ssize_t rv;
3545 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003546 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003547 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003548 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003549 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003550
NeilBrown67918752014-12-15 12:57:01 +11003551 if (slen == 0 || slen >= sizeof(clevel))
3552 return -EINVAL;
3553
3554 rv = mddev_lock(mddev);
3555 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003556 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003557
3558 if (mddev->pers == NULL) {
3559 strncpy(mddev->clevel, buf, slen);
3560 if (mddev->clevel[slen-1] == '\n')
3561 slen--;
3562 mddev->clevel[slen] = 0;
3563 mddev->level = LEVEL_NONE;
3564 rv = len;
3565 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003566 }
NeilBrown67918752014-12-15 12:57:01 +11003567 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003568 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003569 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003570
3571 /* request to change the personality. Need to ensure:
3572 * - array is not engaged in resync/recovery/reshape
3573 * - old personality can be suspended
3574 * - new personality will access other array.
3575 */
3576
NeilBrown67918752014-12-15 12:57:01 +11003577 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003578 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003579 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003580 mddev->reshape_position != MaxSector ||
3581 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003582 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003583
NeilBrown67918752014-12-15 12:57:01 +11003584 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003585 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003586 pr_warn("md: %s: %s does not support online personality change\n",
3587 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003588 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003589 }
3590
3591 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003592 strncpy(clevel, buf, slen);
3593 if (clevel[slen-1] == '\n')
3594 slen--;
3595 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003596 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003597 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003598
Dan Williamsf2859af2010-05-02 10:04:16 -07003599 if (request_module("md-%s", clevel) != 0)
3600 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003601 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003602 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003603 if (!pers || !try_module_get(pers->owner)) {
3604 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003605 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003606 rv = -EINVAL;
3607 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003608 }
3609 spin_unlock(&pers_lock);
3610
3611 if (pers == mddev->pers) {
3612 /* Nothing to do! */
3613 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003614 rv = len;
3615 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003616 }
3617 if (!pers->takeover) {
3618 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003619 pr_warn("md: %s: %s does not support personality takeover\n",
3620 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003621 rv = -EINVAL;
3622 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003623 }
3624
NeilBrowndafb20f2012-03-19 12:46:39 +11003625 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003626 rdev->new_raid_disk = rdev->raid_disk;
3627
NeilBrown245f46c2009-03-31 14:39:39 +11003628 /* ->takeover must set new_* and/or delta_disks
3629 * if it succeeds, and may set them when it fails.
3630 */
3631 priv = pers->takeover(mddev);
3632 if (IS_ERR(priv)) {
3633 mddev->new_level = mddev->level;
3634 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003635 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003636 mddev->raid_disks -= mddev->delta_disks;
3637 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003638 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003639 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003640 pr_warn("md: %s: %s would not accept array\n",
3641 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003642 rv = PTR_ERR(priv);
3643 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003644 }
3645
3646 /* Looks like we have a winner */
3647 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003648 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003649
3650 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003651 oldpers = mddev->pers;
3652 oldpriv = mddev->private;
3653 mddev->pers = pers;
3654 mddev->private = priv;
3655 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3656 mddev->level = mddev->new_level;
3657 mddev->layout = mddev->new_layout;
3658 mddev->chunk_sectors = mddev->new_chunk_sectors;
3659 mddev->delta_disks = 0;
3660 mddev->reshape_backwards = 0;
3661 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003662 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003663
NeilBrowndb721d32014-12-15 12:56:58 +11003664 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003665 mddev->external) {
3666 /* We are converting from a no-redundancy array
3667 * to a redundancy array and metadata is managed
3668 * externally so we need to be sure that writes
3669 * won't block due to a need to transition
3670 * clean->dirty
3671 * until external management is started.
3672 */
3673 mddev->in_sync = 0;
3674 mddev->safemode_delay = 0;
3675 mddev->safemode = 0;
3676 }
3677
NeilBrowndb721d32014-12-15 12:56:58 +11003678 oldpers->free(mddev, oldpriv);
3679
3680 if (oldpers->sync_request == NULL &&
3681 pers->sync_request != NULL) {
3682 /* need to add the md_redundancy_group */
3683 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11003684 pr_warn("md: cannot register extra attributes for %s\n",
3685 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11003686 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3687 }
3688 if (oldpers->sync_request != NULL &&
3689 pers->sync_request == NULL) {
3690 /* need to remove the md_redundancy_group */
3691 if (mddev->to_remove == NULL)
3692 mddev->to_remove = &md_redundancy_group;
3693 }
3694
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003695 module_put(oldpers->owner);
3696
NeilBrowndafb20f2012-03-19 12:46:39 +11003697 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003698 if (rdev->raid_disk < 0)
3699 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003700 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003701 rdev->new_raid_disk = -1;
3702 if (rdev->new_raid_disk == rdev->raid_disk)
3703 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003704 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003705 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003706 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003707 if (rdev->raid_disk < 0)
3708 continue;
3709 if (rdev->new_raid_disk == rdev->raid_disk)
3710 continue;
3711 rdev->raid_disk = rdev->new_raid_disk;
3712 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003713 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003714 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003715 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11003716 pr_warn("md: cannot register rd%d for %s after level change\n",
3717 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003718 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003719 }
3720
NeilBrowndb721d32014-12-15 12:56:58 +11003721 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003722 /* this is now an array without redundancy, so
3723 * it must always be in_sync
3724 */
3725 mddev->in_sync = 1;
3726 del_timer_sync(&mddev->safemode_timer);
3727 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003728 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003729 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08003730 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003731 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003732 if (!mddev->thread)
3733 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003734 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003735 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003736 rv = len;
3737out_unlock:
3738 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003739 return rv;
3740}
3741
3742static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003743__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003744
NeilBrownd4dbd022006-06-26 00:27:59 -07003745static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003746layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003747{
3748 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003749 if (mddev->reshape_position != MaxSector &&
3750 mddev->layout != mddev->new_layout)
3751 return sprintf(page, "%d (%d)\n",
3752 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003753 return sprintf(page, "%d\n", mddev->layout);
3754}
3755
3756static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003757layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003758{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003759 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003760 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003761
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003762 err = kstrtouint(buf, 10, &n);
3763 if (err < 0)
3764 return err;
NeilBrown67918752014-12-15 12:57:01 +11003765 err = mddev_lock(mddev);
3766 if (err)
3767 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003768
NeilBrownb3546032009-03-31 14:56:41 +11003769 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003770 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003771 err = -EBUSY;
3772 else if (mddev->ro)
3773 err = -EROFS;
3774 else {
3775 mddev->new_layout = n;
3776 err = mddev->pers->check_reshape(mddev);
3777 if (err)
3778 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003779 }
NeilBrownb3546032009-03-31 14:56:41 +11003780 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003781 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003782 if (mddev->reshape_position == MaxSector)
3783 mddev->layout = n;
3784 }
NeilBrown67918752014-12-15 12:57:01 +11003785 mddev_unlock(mddev);
3786 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003787}
3788static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003789__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003790
NeilBrowneae17012005-11-08 21:39:23 -08003791static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003792raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003793{
NeilBrownbb636542005-11-08 21:39:45 -08003794 if (mddev->raid_disks == 0)
3795 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003796 if (mddev->reshape_position != MaxSector &&
3797 mddev->delta_disks != 0)
3798 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3799 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003800 return sprintf(page, "%d\n", mddev->raid_disks);
3801}
3802
NeilBrownfd01b882011-10-11 16:47:53 +11003803static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003804
3805static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003806raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003807{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003808 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003809 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003810
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003811 err = kstrtouint(buf, 10, &n);
3812 if (err < 0)
3813 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003814
NeilBrown67918752014-12-15 12:57:01 +11003815 err = mddev_lock(mddev);
3816 if (err)
3817 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003818 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003819 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003820 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003821 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003822 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003823
NeilBrown67918752014-12-15 12:57:01 +11003824 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003825 rdev_for_each(rdev, mddev) {
3826 if (olddisks < n &&
3827 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003828 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003829 if (olddisks > n &&
3830 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003831 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003832 }
NeilBrown67918752014-12-15 12:57:01 +11003833 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003834 mddev->delta_disks = n - olddisks;
3835 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003836 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003837 } else
NeilBrownda943b992006-01-06 00:20:54 -08003838 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003839out_unlock:
3840 mddev_unlock(mddev);
3841 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003842}
3843static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003844__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003845
NeilBrown24dd4692005-11-08 21:39:26 -08003846static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003847chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003848{
NeilBrown08a02ec2007-05-09 02:35:38 -07003849 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003850 mddev->chunk_sectors != mddev->new_chunk_sectors)
3851 return sprintf(page, "%d (%d)\n",
3852 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003853 mddev->chunk_sectors << 9);
3854 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003855}
3856
3857static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003858chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003859{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003860 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003861 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003862
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003863 err = kstrtoul(buf, 10, &n);
3864 if (err < 0)
3865 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003866
NeilBrown67918752014-12-15 12:57:01 +11003867 err = mddev_lock(mddev);
3868 if (err)
3869 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003870 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003871 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003872 err = -EBUSY;
3873 else if (mddev->ro)
3874 err = -EROFS;
3875 else {
3876 mddev->new_chunk_sectors = n >> 9;
3877 err = mddev->pers->check_reshape(mddev);
3878 if (err)
3879 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003880 }
NeilBrownb3546032009-03-31 14:56:41 +11003881 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003882 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003883 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003884 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003885 }
NeilBrown67918752014-12-15 12:57:01 +11003886 mddev_unlock(mddev);
3887 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003888}
3889static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003890__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003891
NeilBrowna94213b2006-06-26 00:28:00 -07003892static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003893resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003894{
NeilBrownd1a7c502009-03-31 15:24:32 +11003895 if (mddev->recovery_cp == MaxSector)
3896 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003897 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3898}
3899
3900static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003901resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003902{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003903 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003904 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003905
3906 if (cmd_match(buf, "none"))
3907 n = MaxSector;
3908 else {
3909 err = kstrtoull(buf, 10, &n);
3910 if (err < 0)
3911 return err;
3912 if (n != (sector_t)n)
3913 return -EINVAL;
3914 }
NeilBrowna94213b2006-06-26 00:28:00 -07003915
NeilBrown67918752014-12-15 12:57:01 +11003916 err = mddev_lock(mddev);
3917 if (err)
3918 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003919 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003920 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003921
NeilBrown67918752014-12-15 12:57:01 +11003922 if (!err) {
3923 mddev->recovery_cp = n;
3924 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08003925 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11003926 }
3927 mddev_unlock(mddev);
3928 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003929}
3930static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003931__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3932 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003933
NeilBrown9e653b62006-06-26 00:27:58 -07003934/*
3935 * The array state can be:
3936 *
3937 * clear
3938 * No devices, no size, no level
3939 * Equivalent to STOP_ARRAY ioctl
3940 * inactive
3941 * May have some settings, but array is not active
3942 * all IO results in error
3943 * When written, doesn't tear down array, but just stops it
3944 * suspended (not supported yet)
3945 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003946 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003947 * readonly
3948 * no resync can happen. no superblocks get written.
3949 * write requests fail
3950 * read-auto
3951 * like readonly, but behaves like 'clean' on a write request.
3952 *
3953 * clean - no pending writes, but otherwise active.
3954 * When written to inactive array, starts without resync
3955 * If a write request arrives then
3956 * if metadata is known, mark 'dirty' and switch to 'active'.
3957 * if not known, block and switch to write-pending
3958 * If written to an active array that has pending writes, then fails.
3959 * active
3960 * fully active: IO and resync can be happening.
3961 * When written to inactive array, starts with resync
3962 *
3963 * write-pending
3964 * clean, but writes are blocked waiting for 'active' to be written.
3965 *
3966 * active-idle
3967 * like active, but no writes have been seen for a while (100msec).
3968 *
3969 */
3970enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3971 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003972static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003973 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3974 "write-pending", "active-idle", NULL };
3975
3976static int match_word(const char *word, char **list)
3977{
3978 int n;
3979 for (n=0; list[n]; n++)
3980 if (cmd_match(word, list[n]))
3981 break;
3982 return n;
3983}
3984
3985static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003986array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003987{
3988 enum array_state st = inactive;
3989
3990 if (mddev->pers)
3991 switch(mddev->ro) {
3992 case 1:
3993 st = readonly;
3994 break;
3995 case 2:
3996 st = read_auto;
3997 break;
3998 case 0:
Shaohua Li29530792016-12-08 15:48:19 -08003999 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004000 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004001 else if (mddev->in_sync)
4002 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004003 else if (mddev->safemode)
4004 st = active_idle;
4005 else
4006 st = active;
4007 }
4008 else {
4009 if (list_empty(&mddev->disks) &&
4010 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004011 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004012 st = clear;
4013 else
4014 st = inactive;
4015 }
4016 return sprintf(page, "%s\n", array_states[st]);
4017}
4018
NeilBrownf72ffdd2014-09-30 14:23:59 +10004019static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4020static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4021static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004022static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004023
4024static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004025array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004026{
NeilBrown67918752014-12-15 12:57:01 +11004027 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07004028 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004029
4030 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4031 /* don't take reconfig_mutex when toggling between
4032 * clean and active
4033 */
4034 spin_lock(&mddev->lock);
4035 if (st == active) {
4036 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004037 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004038 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004039 wake_up(&mddev->sb_wait);
4040 err = 0;
4041 } else /* st == clean */ {
4042 restart_array(mddev);
4043 if (atomic_read(&mddev->writes_pending) == 0) {
4044 if (mddev->in_sync == 0) {
4045 mddev->in_sync = 1;
4046 if (mddev->safemode == 1)
4047 mddev->safemode = 0;
Shaohua Li29530792016-12-08 15:48:19 -08004048 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004049 }
4050 err = 0;
4051 } else
4052 err = -EBUSY;
4053 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004054 if (!err)
4055 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004056 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004057 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004058 }
4059 err = mddev_lock(mddev);
4060 if (err)
4061 return err;
4062 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004063 switch(st) {
4064 case bad_word:
4065 break;
4066 case clear:
4067 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004068 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004069 break;
4070 case inactive:
4071 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004072 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004073 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004074 else
NeilBrowne6910632008-02-06 01:39:51 -08004075 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004076 break;
4077 case suspended:
4078 break; /* not supported yet */
4079 case readonly:
4080 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004081 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004082 else {
4083 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004084 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004085 err = do_md_run(mddev);
4086 }
4087 break;
4088 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004089 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004090 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004091 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004092 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004093 err = restart_array(mddev);
4094 if (err == 0) {
4095 mddev->ro = 2;
4096 set_disk_ro(mddev->gendisk, 0);
4097 }
NeilBrown9e653b62006-06-26 00:27:58 -07004098 } else {
4099 mddev->ro = 2;
4100 err = do_md_run(mddev);
4101 }
4102 break;
4103 case clean:
4104 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004105 err = restart_array(mddev);
4106 if (err)
4107 break;
NeilBrown85572d72014-12-15 12:56:56 +11004108 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004109 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08004110 if (mddev->in_sync == 0) {
4111 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07004112 if (mddev->safemode == 1)
4113 mddev->safemode = 0;
Shaohua Li29530792016-12-08 15:48:19 -08004114 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrowne6910632008-02-06 01:39:51 -08004115 }
4116 err = 0;
4117 } else
4118 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004119 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004120 } else
4121 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004122 break;
4123 case active:
4124 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004125 err = restart_array(mddev);
4126 if (err)
4127 break;
Shaohua Li29530792016-12-08 15:48:19 -08004128 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004129 wake_up(&mddev->sb_wait);
4130 err = 0;
4131 } else {
4132 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004133 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004134 err = do_md_run(mddev);
4135 }
4136 break;
4137 case write_pending:
4138 case active_idle:
4139 /* these cannot be set */
4140 break;
4141 }
NeilBrown67918752014-12-15 12:57:01 +11004142
4143 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004144 if (mddev->hold_active == UNTIL_IOCTL)
4145 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004146 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004147 }
NeilBrown67918752014-12-15 12:57:01 +11004148 mddev_unlock(mddev);
4149 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004150}
NeilBrown80ca3a42006-07-10 04:44:18 -07004151static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004152__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004153
NeilBrown6d7ff7382006-01-06 00:21:16 -08004154static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004155max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004156 return sprintf(page, "%d\n",
4157 atomic_read(&mddev->max_corr_read_errors));
4158}
4159
4160static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004161max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004162{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004163 unsigned int n;
4164 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004165
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004166 rv = kstrtouint(buf, 10, &n);
4167 if (rv < 0)
4168 return rv;
4169 atomic_set(&mddev->max_corr_read_errors, n);
4170 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004171}
4172
4173static struct md_sysfs_entry max_corr_read_errors =
4174__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4175 max_corrected_read_errors_store);
4176
4177static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004178null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004179{
4180 return -EINVAL;
4181}
4182
4183static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004184new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004185{
4186 /* buf must be %d:%d\n? giving major and minor numbers */
4187 /* The new device is added to the array.
4188 * If the array has a persistent superblock, we read the
4189 * superblock to initialise info and check validity.
4190 * Otherwise, only checking done is that in bind_rdev_to_array,
4191 * which mainly checks size.
4192 */
4193 char *e;
4194 int major = simple_strtoul(buf, &e, 10);
4195 int minor;
4196 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004197 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004198 int err;
4199
4200 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4201 return -EINVAL;
4202 minor = simple_strtoul(e+1, &e, 10);
4203 if (*e && *e != '\n')
4204 return -EINVAL;
4205 dev = MKDEV(major, minor);
4206 if (major != MAJOR(dev) ||
4207 minor != MINOR(dev))
4208 return -EOVERFLOW;
4209
NeilBrown67918752014-12-15 12:57:01 +11004210 flush_workqueue(md_misc_wq);
4211
4212 err = mddev_lock(mddev);
4213 if (err)
4214 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004215 if (mddev->persistent) {
4216 rdev = md_import_device(dev, mddev->major_version,
4217 mddev->minor_version);
4218 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004219 struct md_rdev *rdev0
4220 = list_entry(mddev->disks.next,
4221 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004222 err = super_types[mddev->major_version]
4223 .load_super(rdev, rdev0, mddev->minor_version);
4224 if (err < 0)
4225 goto out;
4226 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004227 } else if (mddev->external)
4228 rdev = md_import_device(dev, -2, -1);
4229 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004230 rdev = md_import_device(dev, -1, -1);
4231
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004232 if (IS_ERR(rdev)) {
4233 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004234 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004235 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004236 err = bind_rdev_to_array(rdev, mddev);
4237 out:
4238 if (err)
4239 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004240 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004241 return err ? err : len;
4242}
4243
4244static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004245__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004246
4247static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004248bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004249{
4250 char *end;
4251 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004252 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004253
NeilBrown67918752014-12-15 12:57:01 +11004254 err = mddev_lock(mddev);
4255 if (err)
4256 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004257 if (!mddev->bitmap)
4258 goto out;
4259 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4260 while (*buf) {
4261 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4262 if (buf == end) break;
4263 if (*end == '-') { /* range */
4264 buf = end + 1;
4265 end_chunk = simple_strtoul(buf, &end, 0);
4266 if (buf == end) break;
4267 }
4268 if (*end && !isspace(*end)) break;
4269 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004270 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004271 }
4272 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4273out:
NeilBrown67918752014-12-15 12:57:01 +11004274 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004275 return len;
4276}
4277
4278static struct md_sysfs_entry md_bitmap =
4279__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4280
4281static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004282size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004283{
Andre Noll58c0fed2009-03-31 14:33:13 +11004284 return sprintf(page, "%llu\n",
4285 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004286}
4287
NeilBrownfd01b882011-10-11 16:47:53 +11004288static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004289
4290static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004291size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004292{
4293 /* If array is inactive, we can reduce the component size, but
4294 * not increase it (except from 0).
4295 * If array is active, we can try an on-line resize
4296 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004297 sector_t sectors;
4298 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004299
Andre Noll58c0fed2009-03-31 14:33:13 +11004300 if (err < 0)
4301 return err;
NeilBrown67918752014-12-15 12:57:01 +11004302 err = mddev_lock(mddev);
4303 if (err)
4304 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004305 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004306 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004307 if (err == 0)
4308 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004309 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004310 if (mddev->dev_sectors == 0 ||
4311 mddev->dev_sectors > sectors)
4312 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004313 else
4314 err = -ENOSPC;
4315 }
NeilBrown67918752014-12-15 12:57:01 +11004316 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004317 return err ? err : len;
4318}
4319
4320static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004321__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004322
Masanari Iida83f0d772012-10-30 00:18:08 +09004323/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004324 * This is one of
4325 * 'none' for arrays with no metadata (good luck...)
4326 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004327 * or N.M for internally known formats
4328 */
4329static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004330metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004331{
4332 if (mddev->persistent)
4333 return sprintf(page, "%d.%d\n",
4334 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004335 else if (mddev->external)
4336 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004337 else
4338 return sprintf(page, "none\n");
4339}
4340
4341static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004342metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004343{
4344 int major, minor;
4345 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004346 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004347 /* Changing the details of 'external' metadata is
4348 * always permitted. Otherwise there must be
4349 * no devices attached to the array.
4350 */
NeilBrown67918752014-12-15 12:57:01 +11004351
4352 err = mddev_lock(mddev);
4353 if (err)
4354 return err;
4355 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004356 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4357 ;
4358 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004359 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004360
NeilBrown67918752014-12-15 12:57:01 +11004361 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004362 if (cmd_match(buf, "none")) {
4363 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004364 mddev->external = 0;
4365 mddev->major_version = 0;
4366 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004367 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004368 }
4369 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004370 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004371 if (namelen >= sizeof(mddev->metadata_type))
4372 namelen = sizeof(mddev->metadata_type)-1;
4373 strncpy(mddev->metadata_type, buf+9, namelen);
4374 mddev->metadata_type[namelen] = 0;
4375 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4376 mddev->metadata_type[--namelen] = 0;
4377 mddev->persistent = 0;
4378 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004379 mddev->major_version = 0;
4380 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004381 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004382 }
4383 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004384 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004385 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004386 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004387 buf = e+1;
4388 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004389 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004390 goto out_unlock;
4391 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004392 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004393 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004394 mddev->major_version = major;
4395 mddev->minor_version = minor;
4396 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004397 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004398 err = 0;
4399out_unlock:
4400 mddev_unlock(mddev);
4401 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004402}
4403
4404static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004405__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004406
NeilBrowna35b0d62006-01-06 00:20:49 -08004407static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004408action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004409{
NeilBrown7eec3142005-11-08 21:39:44 -08004410 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004411 unsigned long recovery = mddev->recovery;
4412 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004413 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004414 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4415 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4416 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004417 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004418 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4419 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004420 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004421 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004422 type = "check";
4423 else
4424 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004425 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004426 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004427 else if (mddev->reshape_position != MaxSector)
4428 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004429 }
4430 return sprintf(page, "%s\n", type);
4431}
4432
4433static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004434action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004435{
NeilBrown7eec3142005-11-08 21:39:44 -08004436 if (!mddev->pers || !mddev->pers->sync_request)
4437 return -EINVAL;
4438
NeilBrownb6a9ce62009-05-26 09:41:17 +10004439
4440 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004441 if (cmd_match(page, "frozen"))
4442 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4443 else
4444 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004445 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4446 mddev_lock(mddev) == 0) {
4447 flush_workqueue(md_misc_wq);
4448 if (mddev->sync_thread) {
4449 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004450 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004451 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004452 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004453 }
NeilBrown312045e2015-12-21 11:01:21 +11004454 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004455 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004456 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004457 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004458 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004459 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004460 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004461 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004462 int err;
4463 if (mddev->pers->start_reshape == NULL)
4464 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004465 err = mddev_lock(mddev);
4466 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004467 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4468 err = -EBUSY;
4469 else {
4470 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4471 err = mddev->pers->start_reshape(mddev);
4472 }
NeilBrown67918752014-12-15 12:57:01 +11004473 mddev_unlock(mddev);
4474 }
NeilBrown16484bf2006-03-27 01:18:13 -08004475 if (err)
4476 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004477 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004478 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004479 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004480 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004481 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004482 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004483 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004484 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4485 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004486 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004487 if (mddev->ro == 2) {
4488 /* A write to sync_action is enough to justify
4489 * canceling read-auto mode
4490 */
4491 mddev->ro = 0;
4492 md_wakeup_thread(mddev->sync_thread);
4493 }
NeilBrown03c902e2006-01-06 00:20:46 -08004494 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004495 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004496 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004497 return len;
4498}
4499
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004500static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004501__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004502
4503static ssize_t
4504last_sync_action_show(struct mddev *mddev, char *page)
4505{
4506 return sprintf(page, "%s\n", mddev->last_sync_action);
4507}
4508
4509static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4510
NeilBrown9d888832005-11-08 21:39:26 -08004511static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004512mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004513{
4514 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004515 (unsigned long long)
4516 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004517}
4518
NeilBrown80ca3a42006-07-10 04:44:18 -07004519static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004520
NeilBrown88202a02006-01-06 00:21:36 -08004521static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004522sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004523{
4524 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4525 mddev->sync_speed_min ? "local": "system");
4526}
4527
4528static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004529sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004530{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004531 unsigned int min;
4532 int rv;
4533
NeilBrown88202a02006-01-06 00:21:36 -08004534 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004535 min = 0;
4536 } else {
4537 rv = kstrtouint(buf, 10, &min);
4538 if (rv < 0)
4539 return rv;
4540 if (min == 0)
4541 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004542 }
NeilBrown88202a02006-01-06 00:21:36 -08004543 mddev->sync_speed_min = min;
4544 return len;
4545}
4546
4547static struct md_sysfs_entry md_sync_min =
4548__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4549
4550static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004551sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004552{
4553 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4554 mddev->sync_speed_max ? "local": "system");
4555}
4556
4557static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004558sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004559{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004560 unsigned int max;
4561 int rv;
4562
NeilBrown88202a02006-01-06 00:21:36 -08004563 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004564 max = 0;
4565 } else {
4566 rv = kstrtouint(buf, 10, &max);
4567 if (rv < 0)
4568 return rv;
4569 if (max == 0)
4570 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004571 }
NeilBrown88202a02006-01-06 00:21:36 -08004572 mddev->sync_speed_max = max;
4573 return len;
4574}
4575
4576static struct md_sysfs_entry md_sync_max =
4577__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4578
Iustin Popd7f3d292007-10-16 23:30:54 -07004579static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004580degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004581{
4582 return sprintf(page, "%d\n", mddev->degraded);
4583}
4584static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004585
4586static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004587sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004588{
4589 return sprintf(page, "%d\n", mddev->parallel_resync);
4590}
4591
4592static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004593sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004594{
4595 long n;
4596
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004597 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004598 return -EINVAL;
4599
4600 if (n != 0 && n != 1)
4601 return -EINVAL;
4602
4603 mddev->parallel_resync = n;
4604
4605 if (mddev->sync_thread)
4606 wake_up(&resync_wait);
4607
4608 return len;
4609}
4610
4611/* force parallel resync, even with shared block devices */
4612static struct md_sysfs_entry md_sync_force_parallel =
4613__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4614 sync_force_parallel_show, sync_force_parallel_store);
4615
4616static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004617sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004618{
4619 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004620 if (mddev->curr_resync == 0)
4621 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004622 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4623 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004624 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004625 db = resync - mddev->resync_mark_cnt;
4626 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004627}
4628
NeilBrown80ca3a42006-07-10 04:44:18 -07004629static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004630
4631static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004632sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004633{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004634 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004635
NeilBrownacb180b2009-04-14 16:28:34 +10004636 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4637 return sprintf(page, "none\n");
4638
NeilBrown72f36d52012-10-11 14:25:57 +11004639 if (mddev->curr_resync == 1 ||
4640 mddev->curr_resync == 2)
4641 return sprintf(page, "delayed\n");
4642
NeilBrownc804cde2012-05-21 09:28:33 +10004643 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4644 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004645 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004646 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004647 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004648
NeilBrownacb180b2009-04-14 16:28:34 +10004649 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004650 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004651}
4652
NeilBrown750f1992014-09-30 08:53:05 +10004653static struct md_sysfs_entry md_sync_completed =
4654 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004655
NeilBrowne464eaf2006-03-27 01:18:14 -08004656static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004657min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004658{
4659 return sprintf(page, "%llu\n",
4660 (unsigned long long)mddev->resync_min);
4661}
4662static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004663min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004664{
4665 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004666 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004667
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004668 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004669 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004670
4671 spin_lock(&mddev->lock);
4672 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004673 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004674 goto out_unlock;
4675
4676 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004677 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004678 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004679
NeilBrown50c37b12015-03-23 17:36:38 +11004680 /* Round down to multiple of 4K for safety */
4681 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004682 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004683
NeilBrown23da4222014-12-15 12:57:01 +11004684out_unlock:
4685 spin_unlock(&mddev->lock);
4686 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004687}
4688
4689static struct md_sysfs_entry md_min_sync =
4690__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4691
4692static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004693max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004694{
4695 if (mddev->resync_max == MaxSector)
4696 return sprintf(page, "max\n");
4697 else
4698 return sprintf(page, "%llu\n",
4699 (unsigned long long)mddev->resync_max);
4700}
4701static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004702max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004703{
NeilBrown23da4222014-12-15 12:57:01 +11004704 int err;
4705 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004706 if (strncmp(buf, "max", 3) == 0)
4707 mddev->resync_max = MaxSector;
4708 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004709 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004710 int chunk;
4711
4712 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004713 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004714 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004715 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004716 goto out_unlock;
4717
4718 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004719 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004720 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004721 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004722 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004723
4724 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004725 chunk = mddev->chunk_sectors;
4726 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004727 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004728
4729 err = -EINVAL;
4730 if (sector_div(temp, chunk))
4731 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004732 }
4733 mddev->resync_max = max;
4734 }
4735 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004736 err = 0;
4737out_unlock:
4738 spin_unlock(&mddev->lock);
4739 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004740}
4741
4742static struct md_sysfs_entry md_max_sync =
4743__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4744
4745static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004746suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004747{
4748 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4749}
4750
4751static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004752suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004753{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004754 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004755 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004756
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004757 err = kstrtoull(buf, 10, &new);
4758 if (err < 0)
4759 return err;
4760 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004761 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004762
NeilBrown67918752014-12-15 12:57:01 +11004763 err = mddev_lock(mddev);
4764 if (err)
4765 return err;
4766 err = -EINVAL;
4767 if (mddev->pers == NULL ||
4768 mddev->pers->quiesce == NULL)
4769 goto unlock;
4770 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004771 mddev->suspend_lo = new;
4772 if (new >= old)
4773 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004774 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004775 else {
4776 /* Expanding suspended region - need to wait */
4777 mddev->pers->quiesce(mddev, 1);
4778 mddev->pers->quiesce(mddev, 0);
4779 }
NeilBrown67918752014-12-15 12:57:01 +11004780 err = 0;
4781unlock:
4782 mddev_unlock(mddev);
4783 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004784}
4785static struct md_sysfs_entry md_suspend_lo =
4786__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4787
NeilBrowne464eaf2006-03-27 01:18:14 -08004788static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004789suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004790{
4791 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4792}
4793
4794static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004795suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004796{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004797 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004798 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004799
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004800 err = kstrtoull(buf, 10, &new);
4801 if (err < 0)
4802 return err;
4803 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004804 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004805
NeilBrown67918752014-12-15 12:57:01 +11004806 err = mddev_lock(mddev);
4807 if (err)
4808 return err;
4809 err = -EINVAL;
4810 if (mddev->pers == NULL ||
4811 mddev->pers->quiesce == NULL)
4812 goto unlock;
4813 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004814 mddev->suspend_hi = new;
4815 if (new <= old)
4816 /* Shrinking suspended region */
4817 mddev->pers->quiesce(mddev, 2);
4818 else {
4819 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004820 mddev->pers->quiesce(mddev, 1);
4821 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004822 }
NeilBrown67918752014-12-15 12:57:01 +11004823 err = 0;
4824unlock:
4825 mddev_unlock(mddev);
4826 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004827}
4828static struct md_sysfs_entry md_suspend_hi =
4829__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4830
NeilBrown08a02ec2007-05-09 02:35:38 -07004831static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004832reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004833{
4834 if (mddev->reshape_position != MaxSector)
4835 return sprintf(page, "%llu\n",
4836 (unsigned long long)mddev->reshape_position);
4837 strcpy(page, "none\n");
4838 return 5;
4839}
4840
4841static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004842reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004843{
NeilBrownc6563a82012-05-21 09:27:00 +10004844 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004845 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004846 int err;
NeilBrown67918752014-12-15 12:57:01 +11004847
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004848 err = kstrtoull(buf, 10, &new);
4849 if (err < 0)
4850 return err;
4851 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004852 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004853 err = mddev_lock(mddev);
4854 if (err)
4855 return err;
4856 err = -EBUSY;
4857 if (mddev->pers)
4858 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004859 mddev->reshape_position = new;
4860 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004861 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004862 mddev->new_level = mddev->level;
4863 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004864 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004865 rdev_for_each(rdev, mddev)
4866 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004867 err = 0;
4868unlock:
4869 mddev_unlock(mddev);
4870 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004871}
4872
4873static struct md_sysfs_entry md_reshape_position =
4874__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4875 reshape_position_store);
4876
Dan Williamsb522adc2009-03-31 15:00:31 +11004877static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004878reshape_direction_show(struct mddev *mddev, char *page)
4879{
4880 return sprintf(page, "%s\n",
4881 mddev->reshape_backwards ? "backwards" : "forwards");
4882}
4883
4884static ssize_t
4885reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4886{
4887 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004888 int err;
4889
NeilBrown2c810cd2012-05-21 09:27:00 +10004890 if (cmd_match(buf, "forwards"))
4891 backwards = 0;
4892 else if (cmd_match(buf, "backwards"))
4893 backwards = 1;
4894 else
4895 return -EINVAL;
4896 if (mddev->reshape_backwards == backwards)
4897 return len;
4898
NeilBrown67918752014-12-15 12:57:01 +11004899 err = mddev_lock(mddev);
4900 if (err)
4901 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004902 /* check if we are allowed to change */
4903 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004904 err = -EBUSY;
4905 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004906 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004907 err = -EINVAL;
4908 else
4909 mddev->reshape_backwards = backwards;
4910 mddev_unlock(mddev);
4911 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004912}
4913
4914static struct md_sysfs_entry md_reshape_direction =
4915__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4916 reshape_direction_store);
4917
4918static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004919array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004920{
4921 if (mddev->external_size)
4922 return sprintf(page, "%llu\n",
4923 (unsigned long long)mddev->array_sectors/2);
4924 else
4925 return sprintf(page, "default\n");
4926}
4927
4928static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004929array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004930{
4931 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004932 int err;
4933
4934 err = mddev_lock(mddev);
4935 if (err)
4936 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004937
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004938 /* cluster raid doesn't support change array_sectors */
4939 if (mddev_is_clustered(mddev))
4940 return -EINVAL;
4941
Dan Williamsb522adc2009-03-31 15:00:31 +11004942 if (strncmp(buf, "default", 7) == 0) {
4943 if (mddev->pers)
4944 sectors = mddev->pers->size(mddev, 0, 0);
4945 else
4946 sectors = mddev->array_sectors;
4947
4948 mddev->external_size = 0;
4949 } else {
4950 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004951 err = -EINVAL;
4952 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4953 err = -E2BIG;
4954 else
4955 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004956 }
4957
NeilBrown67918752014-12-15 12:57:01 +11004958 if (!err) {
4959 mddev->array_sectors = sectors;
4960 if (mddev->pers) {
4961 set_capacity(mddev->gendisk, mddev->array_sectors);
4962 revalidate_disk(mddev->gendisk);
4963 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004964 }
NeilBrown67918752014-12-15 12:57:01 +11004965 mddev_unlock(mddev);
4966 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004967}
4968
4969static struct md_sysfs_entry md_array_size =
4970__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4971 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004972
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01004973static ssize_t
4974consistency_policy_show(struct mddev *mddev, char *page)
4975{
4976 int ret;
4977
4978 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
4979 ret = sprintf(page, "journal\n");
4980 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
4981 ret = sprintf(page, "ppl\n");
4982 } else if (mddev->bitmap) {
4983 ret = sprintf(page, "bitmap\n");
4984 } else if (mddev->pers) {
4985 if (mddev->pers->sync_request)
4986 ret = sprintf(page, "resync\n");
4987 else
4988 ret = sprintf(page, "none\n");
4989 } else {
4990 ret = sprintf(page, "unknown\n");
4991 }
4992
4993 return ret;
4994}
4995
4996static ssize_t
4997consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
4998{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01004999 int err = 0;
5000
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005001 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005002 if (mddev->pers->change_consistency_policy)
5003 err = mddev->pers->change_consistency_policy(mddev, buf);
5004 else
5005 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005006 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5007 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005008 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005009 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005010 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005011
5012 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005013}
5014
5015static struct md_sysfs_entry md_consistency_policy =
5016__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5017 consistency_policy_store);
5018
NeilBrowneae17012005-11-08 21:39:23 -08005019static struct attribute *md_default_attrs[] = {
5020 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005021 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005022 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005023 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005024 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005025 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005026 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005027 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005028 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005029 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005030 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005031 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005032 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005033 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005034 &md_consistency_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005035 NULL,
5036};
5037
5038static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005039 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005040 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005041 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005042 &md_sync_min.attr,
5043 &md_sync_max.attr,
5044 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005045 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005046 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005047 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005048 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005049 &md_suspend_lo.attr,
5050 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005051 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005052 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005053 NULL,
5054};
NeilBrown411036f2005-11-08 21:39:40 -08005055static struct attribute_group md_redundancy_group = {
5056 .name = NULL,
5057 .attrs = md_redundancy_attrs,
5058};
5059
NeilBrowneae17012005-11-08 21:39:23 -08005060static ssize_t
5061md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5062{
5063 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005064 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005065 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005066
5067 if (!entry->show)
5068 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005069 spin_lock(&all_mddevs_lock);
5070 if (list_empty(&mddev->all_mddevs)) {
5071 spin_unlock(&all_mddevs_lock);
5072 return -EBUSY;
5073 }
5074 mddev_get(mddev);
5075 spin_unlock(&all_mddevs_lock);
5076
NeilBrownb7b17c92014-12-15 12:56:59 +11005077 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005078 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005079 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005080}
5081
5082static ssize_t
5083md_attr_store(struct kobject *kobj, struct attribute *attr,
5084 const char *page, size_t length)
5085{
5086 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005087 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005088 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005089
5090 if (!entry->store)
5091 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005092 if (!capable(CAP_SYS_ADMIN))
5093 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005094 spin_lock(&all_mddevs_lock);
5095 if (list_empty(&mddev->all_mddevs)) {
5096 spin_unlock(&all_mddevs_lock);
5097 return -EBUSY;
5098 }
5099 mddev_get(mddev);
5100 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005101 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005102 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005103 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005104}
5105
5106static void md_free(struct kobject *ko)
5107{
NeilBrownfd01b882011-10-11 16:47:53 +11005108 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005109
5110 if (mddev->sysfs_state)
5111 sysfs_put(mddev->sysfs_state);
5112
NeilBrown6cd18e72015-04-27 14:12:22 +10005113 if (mddev->queue)
5114 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11005115 if (mddev->gendisk) {
5116 del_gendisk(mddev->gendisk);
5117 put_disk(mddev->gendisk);
5118 }
NeilBrowna21d1502009-01-09 08:31:09 +11005119
NeilBrowneae17012005-11-08 21:39:23 -08005120 kfree(mddev);
5121}
5122
Emese Revfy52cf25d2010-01-19 02:58:23 +01005123static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005124 .show = md_attr_show,
5125 .store = md_attr_store,
5126};
5127static struct kobj_type md_ktype = {
5128 .release = md_free,
5129 .sysfs_ops = &md_sysfs_ops,
5130 .default_attrs = md_default_attrs,
5131};
5132
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133int mdp_major = 0;
5134
Dan Williams5fd3a172009-03-04 00:57:25 -07005135static void mddev_delayed_delete(struct work_struct *ws)
5136{
NeilBrownfd01b882011-10-11 16:47:53 +11005137 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005138
NeilBrown43a70502009-12-14 12:49:55 +11005139 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005140 kobject_del(&mddev->kobj);
5141 kobject_put(&mddev->kobj);
5142}
5143
NeilBrownefeb53c2009-01-09 08:31:10 +11005144static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005146 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005147 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005149 int partitioned;
5150 int shift;
5151 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005152 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153
5154 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005155 return -ENODEV;
5156
5157 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5158 shift = partitioned ? MdpMinorShift : 0;
5159 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
Tejun Heoe804ac72010-10-15 15:36:08 +02005161 /* wait for any previous instance of this device to be
5162 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005163 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005164 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005165
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005166 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005167 error = -EEXIST;
5168 if (mddev->gendisk)
5169 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005170
5171 if (name) {
5172 /* Need to ensure that 'name' is not a duplicate.
5173 */
NeilBrownfd01b882011-10-11 16:47:53 +11005174 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005175 spin_lock(&all_mddevs_lock);
5176
5177 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5178 if (mddev2->gendisk &&
5179 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5180 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005181 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005182 }
5183 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 }
NeilBrown8b765392009-01-09 08:31:08 +11005185
NeilBrown0909dc42009-07-01 12:27:21 +10005186 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005187 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005188 if (!mddev->queue)
5189 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005190 mddev->queue->queuedata = mddev;
5191
NeilBrown409c57f2009-03-31 14:39:39 +11005192 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005193 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005194
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 disk = alloc_disk(1 << shift);
5196 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005197 blk_cleanup_queue(mddev->queue);
5198 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005199 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005201 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005203 if (name)
5204 strcpy(disk->disk_name, name);
5205 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005207 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 disk->fops = &md_fops;
5210 disk->private_data = mddev;
5211 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005212 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005213 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005214 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005215 * remove it now.
5216 */
5217 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005219 /* As soon as we call add_disk(), another thread could get
5220 * through to md_open, so make sure it doesn't get too far
5221 */
5222 mutex_lock(&mddev->open_mutex);
5223 add_disk(disk);
5224
Tejun Heoed9e1982008-08-25 19:56:05 +09005225 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5226 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005227 if (error) {
5228 /* This isn't possible, but as kobject_init_and_add is marked
5229 * __must_check, we must do something with the result
5230 */
NeilBrown9d487392016-11-02 14:16:49 +11005231 pr_debug("md: cannot register %s/md - name in use\n",
5232 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005233 error = 0;
5234 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005235 if (mddev->kobj.sd &&
5236 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005237 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005238 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005239 abort:
5240 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005241 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005242 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005243 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005244 }
NeilBrownd3374822009-01-09 08:31:10 +11005245 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005246 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005247}
5248
5249static struct kobject *md_probe(dev_t dev, int *part, void *data)
5250{
5251 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 return NULL;
5253}
5254
NeilBrownefeb53c2009-01-09 08:31:10 +11005255static int add_named_array(const char *val, struct kernel_param *kp)
5256{
5257 /* val must be "md_*" where * is not all digits.
5258 * We allocate an array with a large free minor number, and
5259 * set the name to val. val must not already be an active name.
5260 */
5261 int len = strlen(val);
5262 char buf[DISK_NAME_LEN];
5263
5264 while (len && val[len-1] == '\n')
5265 len--;
5266 if (len >= DISK_NAME_LEN)
5267 return -E2BIG;
5268 strlcpy(buf, val, len+1);
5269 if (strncmp(buf, "md_", 3) != 0)
5270 return -EINVAL;
5271 return md_alloc(0, buf);
5272}
5273
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274static void md_safemode_timeout(unsigned long data)
5275{
NeilBrownfd01b882011-10-11 16:47:53 +11005276 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277
Neil Brown0fd62b82008-06-28 08:31:36 +10005278 if (!atomic_read(&mddev->writes_pending)) {
5279 mddev->safemode = 1;
5280 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10005281 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 md_wakeup_thread(mddev->thread);
5284}
5285
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005286static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287
NeilBrownfd01b882011-10-11 16:47:53 +11005288int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289{
NeilBrown2604b702006-01-06 00:20:36 -08005290 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005291 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005292 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293
NeilBrowna757e642005-04-16 15:26:42 -07005294 if (list_empty(&mddev->disks))
5295 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297
5298 if (mddev->pers)
5299 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005300 /* Cannot run until previous stop completes properly */
5301 if (mddev->sysfs_active)
5302 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005303
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 /*
5305 * Analyze all RAID superblock(s)
5306 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005307 if (!mddev->raid_disks) {
5308 if (!mddev->persistent)
5309 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005310 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
NeilBrownd9d166c2006-01-06 00:20:51 -08005313 if (mddev->level != LEVEL_NONE)
5314 request_module("md-level-%d", mddev->level);
5315 else if (mddev->clevel[0])
5316 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317
5318 /*
5319 * Drop all container device buffers, from now on
5320 * the only valid external interface is through the md
5321 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005323 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005324 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 continue;
5326 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005327 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005328
5329 /* perform some consistency tests on the device.
5330 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005331 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005332 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005333 if (rdev->meta_bdev) {
5334 /* Nothing to check */;
5335 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005336 if (mddev->dev_sectors &&
5337 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005338 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005339 pr_warn("md: %s: data overlaps metadata\n",
5340 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005341 return -EINVAL;
5342 }
5343 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005344 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005345 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005346 pr_warn("md: %s: metadata overlaps data\n",
5347 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005348 return -EINVAL;
5349 }
5350 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005351 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 }
5353
Ming Lei10273172017-02-14 23:29:00 +08005354 if (mddev->bio_set == NULL) {
Kent Overstreet395c72a2012-09-06 15:34:55 -07005355 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
Ming Lei10273172017-02-14 23:29:00 +08005356 if (!mddev->bio_set)
5357 return -ENOMEM;
5358 }
NeilBrowna167f662010-10-26 18:31:13 +11005359
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005361 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005362 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005364 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005365 pr_warn("md: personality for level %d is not loaded!\n",
5366 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005367 else
NeilBrown9d487392016-11-02 14:16:49 +11005368 pr_warn("md: personality for level %s is not loaded!\n",
5369 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 return -EINVAL;
5371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005373 if (mddev->level != pers->level) {
5374 mddev->level = pers->level;
5375 mddev->new_level = pers->level;
5376 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005377 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
NeilBrownf6705572006-03-27 01:18:11 -08005379 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005380 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005381 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005382 module_put(pers->owner);
5383 return -EINVAL;
5384 }
5385
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005386 if (pers->sync_request) {
5387 /* Warn if this is a potentially silly
5388 * configuration.
5389 */
5390 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005391 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005392 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005393
NeilBrowndafb20f2012-03-19 12:46:39 +11005394 rdev_for_each(rdev, mddev)
5395 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005396 if (rdev < rdev2 &&
5397 rdev->bdev->bd_contains ==
5398 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005399 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5400 mdname(mddev),
5401 bdevname(rdev->bdev,b),
5402 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005403 warned = 1;
5404 }
5405 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005406
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005407 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005408 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005409 }
5410
NeilBrown657390d2005-08-26 18:34:16 -07005411 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005412 /* may be over-ridden by personality */
5413 mddev->resync_max_sectors = mddev->dev_sectors;
5414
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005415 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
NeilBrown0f9552b52009-12-30 12:08:50 +11005417 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005418 mddev->ro = 2; /* read-only, but switch on first write */
5419
Song Liua85dd7b2017-01-23 17:12:57 -08005420 /*
5421 * NOTE: some pers->run(), for example r5l_recovery_log(), wakes
5422 * up mddev->thread. It is important to initialize critical
5423 * resources for mddev->thread BEFORE calling pers->run().
5424 */
NeilBrown36d091f2014-12-15 12:56:58 +11005425 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005426 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005427 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005428 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005429 WARN_ONCE(!mddev->external_size,
5430 "%s: default size too small, but 'external_size' not in effect?\n",
5431 __func__);
5432 pr_warn("md: invalid array_size %llu > default size %llu\n",
5433 (unsigned long long)mddev->array_sectors / 2,
5434 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005435 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005436 }
NeilBrown36d091f2014-12-15 12:56:58 +11005437 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005438 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005439 struct bitmap *bitmap;
5440
5441 bitmap = bitmap_create(mddev, -1);
5442 if (IS_ERR(bitmap)) {
5443 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005444 pr_warn("%s: failed to create bitmap (%d)\n",
5445 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005446 } else
5447 mddev->bitmap = bitmap;
5448
NeilBrownb15c2e52006-01-06 00:20:16 -08005449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005451 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005452 if (mddev->private)
5453 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005454 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005455 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005456 bitmap_destroy(mddev);
5457 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 }
NeilBrown5c675f82014-12-15 12:56:56 +11005459 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005460 bool nonrot = true;
5461
5462 rdev_for_each(rdev, mddev) {
5463 if (rdev->raid_disk >= 0 &&
5464 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5465 nonrot = false;
5466 break;
5467 }
5468 }
5469 if (mddev->degraded)
5470 nonrot = false;
5471 if (nonrot)
5472 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5473 else
5474 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005475 mddev->queue->backing_dev_info->congested_data = mddev;
5476 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005477 }
NeilBrown36d091f2014-12-15 12:56:58 +11005478 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005479 if (mddev->kobj.sd &&
5480 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005481 pr_warn("md: cannot register extra attributes for %s\n",
5482 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005483 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005484 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005485 mddev->ro = 0;
5486
NeilBrownf72ffdd2014-09-30 14:23:59 +10005487 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005488 atomic_set(&mddev->max_corr_read_errors,
5489 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005491 if (mddev_is_clustered(mddev))
5492 mddev->safemode_delay = 0;
5493 else
5494 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005496 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005497 spin_lock(&mddev->lock);
5498 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005499 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005500 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005501 if (rdev->raid_disk >= 0)
5502 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005503 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005504
NeilBrowna4a3d262015-07-17 11:57:30 +10005505 if (mddev->degraded && !mddev->ro)
5506 /* This ensures that recovering status is reported immediately
5507 * via sysfs - until a lack of spares is confirmed.
5508 */
5509 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005511
Shaohua Li29530792016-12-08 15:48:19 -08005512 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07005513 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514
NeilBrownd7603b72006-01-06 00:20:30 -08005515 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005516 sysfs_notify_dirent_safe(mddev->sysfs_state);
5517 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005518 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 return 0;
5520}
NeilBrown390ee602010-06-01 19:37:27 +10005521EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522
NeilBrownfd01b882011-10-11 16:47:53 +11005523static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005524{
5525 int err;
5526
5527 err = md_run(mddev);
5528 if (err)
5529 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005530 err = bitmap_load(mddev);
5531 if (err) {
5532 bitmap_destroy(mddev);
5533 goto out;
5534 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005535
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005536 if (mddev_is_clustered(mddev))
5537 md_allow_write(mddev);
5538
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005539 md_wakeup_thread(mddev->thread);
5540 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5541
NeilBrownfe60b012010-03-29 11:10:42 +11005542 set_capacity(mddev->gendisk, mddev->array_sectors);
5543 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005544 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005545 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5546out:
5547 return err;
5548}
5549
NeilBrownfd01b882011-10-11 16:47:53 +11005550static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551{
5552 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
Andre Noll80fab1d2008-07-11 22:02:21 +10005554 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005556 return -ENXIO;
5557 if (!mddev->pers)
5558 return -EINVAL;
5559 if (!mddev->ro)
5560 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005561 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5562 struct md_rdev *rdev;
5563 bool has_journal = false;
5564
5565 rcu_read_lock();
5566 rdev_for_each_rcu(rdev, mddev) {
5567 if (test_bit(Journal, &rdev->flags) &&
5568 !test_bit(Faulty, &rdev->flags)) {
5569 has_journal = true;
5570 break;
5571 }
5572 }
5573 rcu_read_unlock();
5574
5575 /* Don't restart rw with journal missing/faulty */
5576 if (!has_journal)
5577 return -EINVAL;
5578 }
5579
Andre Noll80fab1d2008-07-11 22:02:21 +10005580 mddev->safemode = 0;
5581 mddev->ro = 0;
5582 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11005583 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10005584 /* Kick recovery or resync if necessary */
5585 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5586 md_wakeup_thread(mddev->thread);
5587 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005588 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590}
5591
NeilBrownfd01b882011-10-11 16:47:53 +11005592static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005593{
5594 mddev->array_sectors = 0;
5595 mddev->external_size = 0;
5596 mddev->dev_sectors = 0;
5597 mddev->raid_disks = 0;
5598 mddev->recovery_cp = 0;
5599 mddev->resync_min = 0;
5600 mddev->resync_max = MaxSector;
5601 mddev->reshape_position = MaxSector;
5602 mddev->external = 0;
5603 mddev->persistent = 0;
5604 mddev->level = LEVEL_NONE;
5605 mddev->clevel[0] = 0;
5606 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08005607 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005608 mddev->ro = 0;
5609 mddev->metadata_type[0] = 0;
5610 mddev->chunk_sectors = 0;
5611 mddev->ctime = mddev->utime = 0;
5612 mddev->layout = 0;
5613 mddev->max_disks = 0;
5614 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005615 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005616 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005617 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005618 mddev->new_level = LEVEL_NONE;
5619 mddev->new_layout = 0;
5620 mddev->new_chunk_sectors = 0;
5621 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005622 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005623 mddev->suspend_lo = mddev->suspend_hi = 0;
5624 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5625 mddev->recovery = 0;
5626 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005627 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005628 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005629 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005630 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005631 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005632 mddev->bitmap_info.offset = 0;
5633 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005634 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005635 mddev->bitmap_info.chunksize = 0;
5636 mddev->bitmap_info.daemon_sleep = 0;
5637 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005638 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005639}
5640
NeilBrownfd01b882011-10-11 16:47:53 +11005641static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005642{
NeilBrown6b6204e2013-05-09 09:48:30 +10005643 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005644 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005645 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005646 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005647 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005648 }
5649
5650 del_timer_sync(&mddev->safemode_timer);
5651
Shaohua Li034e33f2016-11-21 10:29:19 -08005652 if (mddev->pers && mddev->pers->quiesce) {
5653 mddev->pers->quiesce(mddev, 1);
5654 mddev->pers->quiesce(mddev, 0);
5655 }
NeilBrowna047e122010-03-29 12:07:53 +11005656 bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005657
NeilBrownb6d428c2013-04-24 11:42:42 +10005658 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005659 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08005660 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11005661 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005662 if (!mddev_is_clustered(mddev))
5663 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005664 md_update_sb(mddev, 1);
5665 }
5666}
NeilBrowndefad612011-01-14 09:14:33 +11005667
NeilBrownfd01b882011-10-11 16:47:53 +11005668void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005669{
NeilBrown29f097c2013-11-14 17:54:51 +11005670 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005671 __md_stop_writes(mddev);
5672 mddev_unlock(mddev);
5673}
NeilBrown390ee602010-06-01 19:37:27 +10005674EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005675
NeilBrown5aa61f42014-12-15 12:56:57 +11005676static void mddev_detach(struct mddev *mddev)
5677{
5678 struct bitmap *bitmap = mddev->bitmap;
5679 /* wait for behind writes to complete */
5680 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005681 pr_debug("md:%s: behind writes in progress - waiting to stop.\n",
5682 mdname(mddev));
NeilBrown5aa61f42014-12-15 12:56:57 +11005683 /* need to kick something here to make sure I/O goes? */
5684 wait_event(bitmap->behind_wait,
5685 atomic_read(&bitmap->behind_writes) == 0);
5686 }
NeilBrown36d091f2014-12-15 12:56:58 +11005687 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005688 mddev->pers->quiesce(mddev, 1);
5689 mddev->pers->quiesce(mddev, 0);
5690 }
5691 md_unregister_thread(&mddev->thread);
5692 if (mddev->queue)
5693 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5694}
5695
NeilBrown5eff3c42012-11-19 10:47:48 +11005696static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005697{
NeilBrown36d091f2014-12-15 12:56:58 +11005698 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005699 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005700 /* Ensure ->event_work is done */
5701 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005702 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005703 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005704 spin_unlock(&mddev->lock);
5705 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005706 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005707 if (pers->sync_request && mddev->to_remove == NULL)
5708 mddev->to_remove = &md_redundancy_group;
5709 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005710 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005711}
NeilBrown5eff3c42012-11-19 10:47:48 +11005712
5713void md_stop(struct mddev *mddev)
5714{
5715 /* stop the array and free an attached data structures.
5716 * This is called from dm-raid
5717 */
5718 __md_stop(mddev);
5719 bitmap_destroy(mddev);
5720 if (mddev->bio_set)
5721 bioset_free(mddev->bio_set);
5722}
5723
NeilBrown390ee602010-06-01 19:37:27 +10005724EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005725
NeilBrowna05b7ea2012-07-19 15:59:18 +10005726static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005727{
5728 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005729 int did_freeze = 0;
5730
5731 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5732 did_freeze = 1;
5733 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5734 md_wakeup_thread(mddev->thread);
5735 }
NeilBrownf851b602014-12-11 10:02:10 +11005736 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005737 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005738 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005739 /* Thread might be blocked waiting for metadata update
5740 * which will now never happen */
5741 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005742
Shaohua Li29530792016-12-08 15:48:19 -08005743 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10005744 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005745 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005746 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5747 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005748 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005749 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005750 mddev_lock_nointr(mddev);
5751
NeilBrowna4bd82d2010-03-29 13:23:10 +11005752 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005753 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005754 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005755 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005756 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005757 if (did_freeze) {
5758 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005759 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005760 md_wakeup_thread(mddev->thread);
5761 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005762 err = -EBUSY;
5763 goto out;
5764 }
5765 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005766 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005767
5768 err = -ENXIO;
5769 if (mddev->ro==1)
5770 goto out;
5771 mddev->ro = 1;
5772 set_disk_ro(mddev->gendisk, 1);
5773 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005774 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5775 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005776 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005777 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005778 }
5779out:
5780 mutex_unlock(&mddev->open_mutex);
5781 return err;
5782}
5783
NeilBrown9e653b62006-06-26 00:27:58 -07005784/* mode:
5785 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005786 * 2 - stop but do not disassemble array
5787 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005788static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005789 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005792 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005793 int did_freeze = 0;
5794
5795 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5796 did_freeze = 1;
5797 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5798 md_wakeup_thread(mddev->thread);
5799 }
NeilBrownf851b602014-12-11 10:02:10 +11005800 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005801 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005802 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005803 /* Thread might be blocked waiting for metadata update
5804 * which will now never happen */
5805 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005806
NeilBrown30b8feb2013-11-14 15:16:17 +11005807 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005808 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5809 !test_bit(MD_RECOVERY_RUNNING,
5810 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005811 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812
NeilBrownc8c00a62009-08-10 12:50:52 +10005813 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005814 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005815 mddev->sysfs_active ||
5816 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005817 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005818 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005819 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005820 if (did_freeze) {
5821 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005822 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005823 md_wakeup_thread(mddev->thread);
5824 }
NeilBrown260fa032013-08-27 16:44:13 +10005825 return -EBUSY;
5826 }
NeilBrown6e17b022010-08-07 21:41:19 +10005827 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005828 if (mddev->ro)
5829 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005830
NeilBrowndefad612011-01-14 09:14:33 +11005831 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005832 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01005833 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005834
NeilBrowna4bd82d2010-03-29 13:23:10 +11005835 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005836 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005837
NeilBrowndafb20f2012-03-19 12:46:39 +11005838 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005839 if (rdev->raid_disk >= 0)
5840 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005841
NeilBrowna4bd82d2010-03-29 13:23:10 +11005842 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005843 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005844 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005845 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005846
NeilBrowna4bd82d2010-03-29 13:23:10 +11005847 if (mddev->ro)
5848 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005849 } else
5850 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 /*
5852 * Free resources if final stop
5853 */
NeilBrown9e653b62006-06-26 00:27:58 -07005854 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005855 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
NeilBrown978f9462006-02-02 14:28:05 -08005857 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005858 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005859 struct file *f = mddev->bitmap_info.file;
5860 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005861 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005862 spin_unlock(&mddev->lock);
5863 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005864 }
NeilBrownc3d97142009-12-14 12:49:52 +11005865 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005866
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 export_array(mddev);
5868
NeilBrown6177b472010-03-29 11:37:13 +11005869 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11005870 if (mddev->hold_active == UNTIL_STOP)
5871 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005872 }
NeilBrownd7603b72006-01-06 00:20:30 -08005873 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005874 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005875 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876}
5877
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005878#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005879static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880{
NeilBrown3cb03002011-10-11 16:45:26 +11005881 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 int err;
5883
NeilBrowna757e642005-04-16 15:26:42 -07005884 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
NeilBrown9d487392016-11-02 14:16:49 +11005887 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888
NeilBrowndafb20f2012-03-19 12:46:39 +11005889 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11005891 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 }
NeilBrown9d487392016-11-02 14:16:49 +11005893 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894
NeilBrownd710e132008-10-13 11:55:12 +11005895 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11005897 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005898 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 }
5900}
5901
5902/*
5903 * lets try to run arrays based on all disks that have arrived
5904 * until now. (those are in pending_raid_disks)
5905 *
5906 * the method: pick the first pending disk, collect all disks with
5907 * the same UUID, remove all from the pending list and put them into
5908 * the 'same_array' list. Then order this list based on superblock
5909 * update time (freshest comes first), kick out 'old' disks and
5910 * compare superblocks. If everything's fine then run it.
5911 *
5912 * If "unit" is allocated, then bump its reference count
5913 */
5914static void autorun_devices(int part)
5915{
NeilBrown3cb03002011-10-11 16:45:26 +11005916 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005917 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 char b[BDEVNAME_SIZE];
5919
NeilBrown9d487392016-11-02 14:16:49 +11005920 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005922 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005924 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005926 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927
NeilBrown9d487392016-11-02 14:16:49 +11005928 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005930 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005932 pr_debug("md: adding %s ...\n",
5933 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 list_move(&rdev->same_set, &candidates);
5935 }
5936 /*
5937 * now we have a set of devices, with all of them having
5938 * mostly sane superblocks. It's time to allocate the
5939 * mddev.
5940 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005941 if (part) {
5942 dev = MKDEV(mdp_major,
5943 rdev0->preferred_minor << MdpMinorShift);
5944 unit = MINOR(dev) >> MdpMinorShift;
5945 } else {
5946 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5947 unit = MINOR(dev);
5948 }
5949 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11005950 pr_warn("md: unit number in %s is bad: %d\n",
5951 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 break;
5953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954
5955 md_probe(dev, NULL, NULL);
5956 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005957 if (!mddev || !mddev->gendisk) {
5958 if (mddev)
5959 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 break;
5961 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005962 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11005963 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 else if (mddev->raid_disks || mddev->major_version
5965 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11005966 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 mdname(mddev), bdevname(rdev0->bdev,b));
5968 mddev_unlock(mddev);
5969 } else {
NeilBrown9d487392016-11-02 14:16:49 +11005970 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005971 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005972 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 list_del_init(&rdev->same_set);
5974 if (bind_rdev_to_array(rdev, mddev))
5975 export_rdev(rdev);
5976 }
5977 autorun_array(mddev);
5978 mddev_unlock(mddev);
5979 }
5980 /* on success, candidates will be empty, on error
5981 * it won't...
5982 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005983 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005984 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 mddev_put(mddev);
5988 }
NeilBrown9d487392016-11-02 14:16:49 +11005989 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005991#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992
NeilBrownf72ffdd2014-09-30 14:23:59 +10005993static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994{
5995 mdu_version_t ver;
5996
5997 ver.major = MD_MAJOR_VERSION;
5998 ver.minor = MD_MINOR_VERSION;
5999 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6000
6001 if (copy_to_user(arg, &ver, sizeof(ver)))
6002 return -EFAULT;
6003
6004 return 0;
6005}
6006
NeilBrownf72ffdd2014-09-30 14:23:59 +10006007static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008{
6009 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006010 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006011 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012
NeilBrown1ca69c42012-10-11 13:37:33 +11006013 nr = working = insync = failed = spare = 0;
6014 rcu_read_lock();
6015 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006017 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 failed++;
6019 else {
6020 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006021 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006022 insync++;
Song Liub347af82016-08-11 17:14:45 -07006023 else if (test_bit(Journal, &rdev->flags))
6024 /* TODO: add journal count to md_u.h */
6025 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 else
6027 spare++;
6028 }
6029 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006030 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031
6032 info.major_version = mddev->major_version;
6033 info.minor_version = mddev->minor_version;
6034 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006035 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006037 info.size = mddev->dev_sectors / 2;
6038 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006039 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 info.nr_disks = nr;
6041 info.raid_disks = mddev->raid_disks;
6042 info.md_minor = mddev->md_minor;
6043 info.not_persistent= !mddev->persistent;
6044
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006045 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 info.state = 0;
6047 if (mddev->in_sync)
6048 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006049 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006050 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006051 if (mddev_is_clustered(mddev))
6052 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006053 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 info.working_disks = working;
6055 info.failed_disks = failed;
6056 info.spare_disks = spare;
6057
6058 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006059 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060
6061 if (copy_to_user(arg, &info, sizeof(info)))
6062 return -EFAULT;
6063
6064 return 0;
6065}
6066
NeilBrownf72ffdd2014-09-30 14:23:59 +10006067static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006068{
6069 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006070 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006071 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006072
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006073 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006074 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006075 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006076
NeilBrown32a76272005-06-21 17:17:14 -07006077 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006078 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006079 /* bitmap enabled */
6080 if (mddev->bitmap_info.file) {
6081 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6082 sizeof(file->pathname));
6083 if (IS_ERR(ptr))
6084 err = PTR_ERR(ptr);
6085 else
6086 memmove(file->pathname, ptr,
6087 sizeof(file->pathname)-(ptr-file->pathname));
6088 }
NeilBrown4af1a042014-12-15 12:57:00 +11006089 spin_unlock(&mddev->lock);
6090
6091 if (err == 0 &&
6092 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006093 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006094
NeilBrown32a76272005-06-21 17:17:14 -07006095 kfree(file);
6096 return err;
6097}
6098
NeilBrownf72ffdd2014-09-30 14:23:59 +10006099static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100{
6101 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006102 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103
6104 if (copy_from_user(&info, arg, sizeof(info)))
6105 return -EFAULT;
6106
NeilBrown1ca69c42012-10-11 13:37:33 +11006107 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006108 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 if (rdev) {
6110 info.major = MAJOR(rdev->bdev->bd_dev);
6111 info.minor = MINOR(rdev->bdev->bd_dev);
6112 info.raid_disk = rdev->raid_disk;
6113 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006114 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006116 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 info.state |= (1<<MD_DISK_ACTIVE);
6118 info.state |= (1<<MD_DISK_SYNC);
6119 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006120 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006121 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006122 if (test_bit(WriteMostly, &rdev->flags))
6123 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006124 if (test_bit(FailFast, &rdev->flags))
6125 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 } else {
6127 info.major = info.minor = 0;
6128 info.raid_disk = -1;
6129 info.state = (1<<MD_DISK_REMOVED);
6130 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006131 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132
6133 if (copy_to_user(arg, &info, sizeof(info)))
6134 return -EFAULT;
6135
6136 return 0;
6137}
6138
NeilBrownf72ffdd2014-09-30 14:23:59 +10006139static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140{
6141 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006142 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 dev_t dev = MKDEV(info->major,info->minor);
6144
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006145 if (mddev_is_clustered(mddev) &&
6146 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006147 pr_warn("%s: Cannot add to clustered mddev.\n",
6148 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006149 return -EINVAL;
6150 }
6151
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6153 return -EOVERFLOW;
6154
6155 if (!mddev->raid_disks) {
6156 int err;
6157 /* expecting a device which has a superblock */
6158 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6159 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006160 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 PTR_ERR(rdev));
6162 return PTR_ERR(rdev);
6163 }
6164 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006165 struct md_rdev *rdev0
6166 = list_entry(mddev->disks.next,
6167 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006168 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 .load_super(rdev, rdev0, mddev->minor_version);
6170 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006171 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006172 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 bdevname(rdev0->bdev,b2));
6174 export_rdev(rdev);
6175 return -EINVAL;
6176 }
6177 }
6178 err = bind_rdev_to_array(rdev, mddev);
6179 if (err)
6180 export_rdev(rdev);
6181 return err;
6182 }
6183
6184 /*
6185 * add_new_disk can be used once the array is assembled
6186 * to add "hot spares". They must already have a superblock
6187 * written
6188 */
6189 if (mddev->pers) {
6190 int err;
6191 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006192 pr_warn("%s: personality does not support diskops!\n",
6193 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 return -EINVAL;
6195 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006196 if (mddev->persistent)
6197 rdev = md_import_device(dev, mddev->major_version,
6198 mddev->minor_version);
6199 else
6200 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006202 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 PTR_ERR(rdev));
6204 return PTR_ERR(rdev);
6205 }
NeilBrown1a855a02010-12-09 16:36:28 +11006206 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006207 if (!mddev->persistent) {
6208 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006209 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006210 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006211 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006212 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006213 } else
NeilBrown41158c72005-06-21 17:17:25 -07006214 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006215 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006216 } else
6217 super_types[mddev->major_version].
6218 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006219 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006220 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006221 /* This was a hot-add request, but events doesn't
6222 * match, so reject it.
6223 */
6224 export_rdev(rdev);
6225 return -EINVAL;
6226 }
6227
NeilBrownb2d444d2005-11-08 21:39:31 -08006228 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006229 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6230 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006231 else
6232 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006233 if (info->state & (1<<MD_DISK_FAILFAST))
6234 set_bit(FailFast, &rdev->flags);
6235 else
6236 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006237
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006238 if (info->state & (1<<MD_DISK_JOURNAL)) {
6239 struct md_rdev *rdev2;
6240 bool has_journal = false;
6241
6242 /* make sure no existing journal disk */
6243 rdev_for_each(rdev2, mddev) {
6244 if (test_bit(Journal, &rdev2->flags)) {
6245 has_journal = true;
6246 break;
6247 }
6248 }
6249 if (has_journal) {
6250 export_rdev(rdev);
6251 return -EBUSY;
6252 }
Song Liubac624f2015-08-13 14:31:55 -07006253 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006254 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006255 /*
6256 * check whether the device shows up in other nodes
6257 */
6258 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006259 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006260 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006261 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006262 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006263 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006264 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006265 export_rdev(rdev);
6266 return err;
6267 }
6268 }
6269 }
6270
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 rdev->raid_disk = -1;
6272 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006273
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 if (err)
6275 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006276
6277 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006278 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6279 if (!err) {
6280 err = md_cluster_ops->new_disk_ack(mddev,
6281 err == 0);
6282 if (err)
6283 md_kick_rdev_from_array(rdev);
6284 }
6285 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006286 if (err)
6287 md_cluster_ops->add_new_disk_cancel(mddev);
6288 else
6289 err = add_bound_rdev(rdev);
6290 }
6291
6292 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006293 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006294
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 return err;
6296 }
6297
6298 /* otherwise, add_new_disk is only allowed
6299 * for major_version==0 superblocks
6300 */
6301 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006302 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303 return -EINVAL;
6304 }
6305
6306 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6307 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006308 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006310 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 PTR_ERR(rdev));
6312 return PTR_ERR(rdev);
6313 }
6314 rdev->desc_nr = info->number;
6315 if (info->raid_disk < mddev->raid_disks)
6316 rdev->raid_disk = info->raid_disk;
6317 else
6318 rdev->raid_disk = -1;
6319
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006321 if (info->state & (1<<MD_DISK_SYNC))
6322 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006324 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6325 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006326 if (info->state & (1<<MD_DISK_FAILFAST))
6327 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006328
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006330 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006331 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6332 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006333 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006334 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335
NeilBrown2bf071b2006-01-06 00:20:55 -08006336 err = bind_rdev_to_array(rdev, mddev);
6337 if (err) {
6338 export_rdev(rdev);
6339 return err;
6340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 }
6342
6343 return 0;
6344}
6345
NeilBrownf72ffdd2014-09-30 14:23:59 +10006346static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347{
6348 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006349 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 rdev = find_rdev(mddev, dev);
6352 if (!rdev)
6353 return -ENXIO;
6354
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006355 if (rdev->raid_disk < 0)
6356 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006357
NeilBrown3ea8929d2013-04-24 11:42:41 +10006358 clear_bit(Blocked, &rdev->flags);
6359 remove_and_add_spares(mddev, rdev);
6360
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 if (rdev->raid_disk >= 0)
6362 goto busy;
6363
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006364kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006365 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006366 md_cluster_ops->remove_disk(mddev, rdev);
6367
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006368 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006369 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006370 if (mddev->thread)
6371 md_wakeup_thread(mddev->thread);
6372 else
6373 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006374 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375
6376 return 0;
6377busy:
NeilBrown9d487392016-11-02 14:16:49 +11006378 pr_debug("md: cannot remove active disk %s from %s ...\n",
6379 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380 return -EBUSY;
6381}
6382
NeilBrownf72ffdd2014-09-30 14:23:59 +10006383static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384{
6385 char b[BDEVNAME_SIZE];
6386 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006387 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388
6389 if (!mddev->pers)
6390 return -ENODEV;
6391
6392 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006393 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 mdname(mddev));
6395 return -EINVAL;
6396 }
6397 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006398 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 mdname(mddev));
6400 return -EINVAL;
6401 }
6402
NeilBrownd710e132008-10-13 11:55:12 +11006403 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006405 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 PTR_ERR(rdev));
6407 return -EINVAL;
6408 }
6409
6410 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006411 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006413 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414
NeilBrown8190e752009-06-18 08:48:58 +10006415 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416
NeilBrownb2d444d2005-11-08 21:39:31 -08006417 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006418 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 bdevname(rdev->bdev,b), mdname(mddev));
6420 err = -EINVAL;
6421 goto abort_export;
6422 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006423
NeilBrownb2d444d2005-11-08 21:39:31 -08006424 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006426 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006427 err = bind_rdev_to_array(rdev, mddev);
6428 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006429 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430
6431 /*
6432 * The rest should better be atomic, we can have disk failures
6433 * noticed in interrupt contexts ...
6434 */
6435
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 rdev->raid_disk = -1;
6437
Shaohua Li29530792016-12-08 15:48:19 -08006438 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006439 if (!mddev->thread)
6440 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 /*
6442 * Kick recovery, maybe this spare has to be added to the
6443 * array immediately.
6444 */
6445 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6446 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006447 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 return 0;
6449
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450abort_export:
6451 export_rdev(rdev);
6452 return err;
6453}
6454
NeilBrownfd01b882011-10-11 16:47:53 +11006455static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006456{
NeilBrown035328c2014-04-09 12:25:40 +10006457 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006458
NeilBrown36fa3062005-09-09 16:23:45 -07006459 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006460 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006461 return -EBUSY;
6462 if (mddev->recovery || mddev->sync_thread)
6463 return -EBUSY;
6464 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006465 }
6466
NeilBrown36fa3062005-09-09 16:23:45 -07006467 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006468 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006469 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006470
NeilBrown1e594bb2014-12-15 12:57:00 +11006471 if (mddev->bitmap || mddev->bitmap_info.file)
6472 return -EEXIST; /* cannot add when bitmap is present */
6473 f = fget(fd);
6474
6475 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006476 pr_warn("%s: error: failed to get bitmap file\n",
6477 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006478 return -EBADF;
6479 }
6480
NeilBrown1e594bb2014-12-15 12:57:00 +11006481 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006482 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006483 pr_warn("%s: error: bitmap file must be a regular file\n",
6484 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006485 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006486 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006487 pr_warn("%s: error: bitmap file must open for write\n",
6488 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006489 err = -EBADF;
6490 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006491 pr_warn("%s: error: bitmap file is already in use\n",
6492 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006493 err = -EBUSY;
6494 }
6495 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006496 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006497 return err;
6498 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006499 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006500 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006501 } else if (mddev->bitmap == NULL)
6502 return -ENOENT; /* cannot remove what isn't there */
6503 err = 0;
6504 if (mddev->pers) {
6505 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006506 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006507 struct bitmap *bitmap;
6508
6509 bitmap = bitmap_create(mddev, -1);
6510 if (!IS_ERR(bitmap)) {
6511 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006512 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006513 } else
6514 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006515 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006516 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006517 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006518 fd = -1; /* make sure to put the file */
6519 }
NeilBrown36fa3062005-09-09 16:23:45 -07006520 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006521 }
6522 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006523 struct file *f = mddev->bitmap_info.file;
6524 if (f) {
6525 spin_lock(&mddev->lock);
6526 mddev->bitmap_info.file = NULL;
6527 spin_unlock(&mddev->lock);
6528 fput(f);
6529 }
NeilBrown36fa3062005-09-09 16:23:45 -07006530 }
6531
NeilBrown32a76272005-06-21 17:17:14 -07006532 return err;
6533}
6534
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535/*
6536 * set_array_info is used two different ways
6537 * The original usage is when creating a new array.
6538 * In this usage, raid_disks is > 0 and it together with
6539 * level, size, not_persistent,layout,chunksize determine the
6540 * shape of the array.
6541 * This will always create an array with a type-0.90.0 superblock.
6542 * The newer usage is when assembling an array.
6543 * In this case raid_disks will be 0, and the major_version field is
6544 * use to determine which style super-blocks are to be found on the devices.
6545 * The minor and patch _version numbers are also kept incase the
6546 * super_block handler wishes to interpret them.
6547 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006548static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549{
6550
6551 if (info->raid_disks == 0) {
6552 /* just setting version number for superblock loading */
6553 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006554 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555 super_types[info->major_version].name == NULL) {
6556 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11006557 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 info->major_version);
6559 return -EINVAL;
6560 }
6561 mddev->major_version = info->major_version;
6562 mddev->minor_version = info->minor_version;
6563 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006564 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006565 /* ensure mddev_put doesn't delete this now that there
6566 * is some minimal configuration.
6567 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006568 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 return 0;
6570 }
6571 mddev->major_version = MD_MAJOR_VERSION;
6572 mddev->minor_version = MD_MINOR_VERSION;
6573 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006574 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575
6576 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006577 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006578 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 mddev->raid_disks = info->raid_disks;
6580 /* don't set md_minor, it is determined by which /dev/md* was
6581 * openned
6582 */
6583 if (info->state & (1<<MD_SB_CLEAN))
6584 mddev->recovery_cp = MaxSector;
6585 else
6586 mddev->recovery_cp = 0;
6587 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006588 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589
6590 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006591 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592
Shaohua Li29530792016-12-08 15:48:19 -08006593 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11006594 mddev->max_disks = MD_SB_DISKS;
6595 mddev->flags = 0;
6596 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006597 }
6598 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599
NeilBrownc3d97142009-12-14 12:49:52 +11006600 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006601 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006602 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006603
NeilBrownf6705572006-03-27 01:18:11 -08006604 mddev->reshape_position = MaxSector;
6605
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 /*
6607 * Generate a 128 bit UUID
6608 */
6609 get_random_bytes(mddev->uuid, 16);
6610
NeilBrownf6705572006-03-27 01:18:11 -08006611 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006612 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006613 mddev->new_layout = mddev->layout;
6614 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006615 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006616
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617 return 0;
6618}
6619
NeilBrownfd01b882011-10-11 16:47:53 +11006620void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006621{
Dan Williamsb522adc2009-03-31 15:00:31 +11006622 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6623
6624 if (mddev->external_size)
6625 return;
6626
Dan Williams1f403622009-03-31 14:59:03 +11006627 mddev->array_sectors = array_sectors;
6628}
6629EXPORT_SYMBOL(md_set_array_sectors);
6630
NeilBrownfd01b882011-10-11 16:47:53 +11006631static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006632{
NeilBrown3cb03002011-10-11 16:45:26 +11006633 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006634 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006635 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08006636 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006637
NeilBrowna35b0d62006-01-06 00:20:49 -08006638 if (mddev->pers->resize == NULL)
6639 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006640 /* The "num_sectors" is the number of sectors of each device that
6641 * is used. This can only make sense for arrays with redundancy.
6642 * linear and raid0 always use whatever space is available. We can only
6643 * consider changing this number if no resync or reconstruction is
6644 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006645 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006646 * of each device. If num_sectors is zero, we find the largest size
6647 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006648 */
NeilBrownf851b602014-12-11 10:02:10 +11006649 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6650 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006651 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006652 if (mddev->ro)
6653 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006654
NeilBrowndafb20f2012-03-19 12:46:39 +11006655 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006656 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006657
Andre Nolld71f9f82008-07-11 22:02:22 +10006658 if (fit && (num_sectors == 0 || num_sectors > avail))
6659 num_sectors = avail;
6660 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006661 return -ENOSPC;
6662 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006663 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08006664 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08006665 if (mddev_is_clustered(mddev))
6666 md_cluster_ops->update_size(mddev, old_dev_sectors);
6667 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08006668 set_capacity(mddev->gendisk, mddev->array_sectors);
6669 revalidate_disk(mddev->gendisk);
6670 }
6671 }
NeilBrowna35b0d62006-01-06 00:20:49 -08006672 return rv;
6673}
6674
NeilBrownfd01b882011-10-11 16:47:53 +11006675static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006676{
6677 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006678 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006679 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006680 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006681 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006682 if (mddev->ro)
6683 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006684 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006685 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006686 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006687 if (mddev->sync_thread ||
6688 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6689 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006690 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006691
6692 rdev_for_each(rdev, mddev) {
6693 if (mddev->raid_disks < raid_disks &&
6694 rdev->data_offset < rdev->new_data_offset)
6695 return -EINVAL;
6696 if (mddev->raid_disks > raid_disks &&
6697 rdev->data_offset > rdev->new_data_offset)
6698 return -EINVAL;
6699 }
6700
NeilBrown63c70c42006-03-27 01:18:13 -08006701 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006702 if (mddev->delta_disks < 0)
6703 mddev->reshape_backwards = 1;
6704 else if (mddev->delta_disks > 0)
6705 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006706
6707 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006708 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006709 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006710 mddev->reshape_backwards = 0;
6711 }
NeilBrownda943b992006-01-06 00:20:54 -08006712 return rv;
6713}
6714
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715/*
6716 * update_array_info is used to change the configuration of an
6717 * on-line array.
6718 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6719 * fields in the info are checked against the array.
6720 * Any differences that cannot be handled will cause an error.
6721 * Normally, only one change can be managed at a time.
6722 */
NeilBrownfd01b882011-10-11 16:47:53 +11006723static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724{
6725 int rv = 0;
6726 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006727 int state = 0;
6728
6729 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006730 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006731 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732
6733 if (mddev->major_version != info->major_version ||
6734 mddev->minor_version != info->minor_version ||
6735/* mddev->patch_version != info->patch_version || */
6736 mddev->ctime != info->ctime ||
6737 mddev->level != info->level ||
6738/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006739 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006740 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006741 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6742 ((state^info->state) & 0xfffffe00)
6743 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 return -EINVAL;
6745 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006746 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6747 cnt++;
6748 if (mddev->raid_disks != info->raid_disks)
6749 cnt++;
6750 if (mddev->layout != info->layout)
6751 cnt++;
6752 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6753 cnt++;
6754 if (cnt == 0)
6755 return 0;
6756 if (cnt > 1)
6757 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758
6759 if (mddev->layout != info->layout) {
6760 /* Change layout
6761 * we don't need to do anything at the md level, the
6762 * personality will take care of it all.
6763 */
NeilBrown50ac1682009-06-18 08:47:55 +10006764 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006766 else {
6767 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006768 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006769 if (rv)
6770 mddev->new_layout = mddev->layout;
6771 return rv;
6772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006774 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006775 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776
NeilBrownda943b992006-01-06 00:20:54 -08006777 if (mddev->raid_disks != info->raid_disks)
6778 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779
NeilBrown36fa3062005-09-09 16:23:45 -07006780 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006781 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6782 rv = -EINVAL;
6783 goto err;
6784 }
6785 if (mddev->recovery || mddev->sync_thread) {
6786 rv = -EBUSY;
6787 goto err;
6788 }
NeilBrown36fa3062005-09-09 16:23:45 -07006789 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006790 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006791 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006792 if (mddev->bitmap) {
6793 rv = -EEXIST;
6794 goto err;
6795 }
6796 if (mddev->bitmap_info.default_offset == 0) {
6797 rv = -EINVAL;
6798 goto err;
6799 }
NeilBrownc3d97142009-12-14 12:49:52 +11006800 mddev->bitmap_info.offset =
6801 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006802 mddev->bitmap_info.space =
6803 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006804 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006805 bitmap = bitmap_create(mddev, -1);
6806 if (!IS_ERR(bitmap)) {
6807 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006808 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006809 } else
6810 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006811 if (rv)
6812 bitmap_destroy(mddev);
6813 mddev->pers->quiesce(mddev, 0);
6814 } else {
6815 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006816 if (!mddev->bitmap) {
6817 rv = -ENOENT;
6818 goto err;
6819 }
6820 if (mddev->bitmap->storage.file) {
6821 rv = -EINVAL;
6822 goto err;
6823 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006824 if (mddev->bitmap_info.nodes) {
6825 /* hold PW on all the bitmap lock */
6826 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006827 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 +11006828 rv = -EPERM;
6829 md_cluster_ops->unlock_all_bitmaps(mddev);
6830 goto err;
6831 }
6832
6833 mddev->bitmap_info.nodes = 0;
6834 md_cluster_ops->leave(mddev);
6835 }
NeilBrown36fa3062005-09-09 16:23:45 -07006836 mddev->pers->quiesce(mddev, 1);
6837 bitmap_destroy(mddev);
6838 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006839 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006840 }
6841 }
NeilBrown850b2b422006-10-03 01:15:46 -07006842 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006843 return rv;
6844err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 return rv;
6846}
6847
NeilBrownfd01b882011-10-11 16:47:53 +11006848static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849{
NeilBrown3cb03002011-10-11 16:45:26 +11006850 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006851 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852
6853 if (mddev->pers == NULL)
6854 return -ENODEV;
6855
NeilBrown1ca69c42012-10-11 13:37:33 +11006856 rcu_read_lock();
6857 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006859 err = -ENODEV;
6860 else {
6861 md_error(mddev, rdev);
6862 if (!test_bit(Faulty, &rdev->flags))
6863 err = -EBUSY;
6864 }
6865 rcu_read_unlock();
6866 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867}
6868
Andre Noll2f9618c2008-04-25 18:57:58 +02006869/*
6870 * We have a problem here : there is no easy way to give a CHS
6871 * virtual geometry. We currently pretend that we have a 2 heads
6872 * 4 sectors (with a BIG number of cylinders...). This drives
6873 * dosfs just mad... ;-)
6874 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006875static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6876{
NeilBrownfd01b882011-10-11 16:47:53 +11006877 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006878
6879 geo->heads = 2;
6880 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006881 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006882 return 0;
6883}
6884
Nicolas Schichancb335f82014-01-15 16:58:52 +01006885static inline bool md_ioctl_valid(unsigned int cmd)
6886{
6887 switch (cmd) {
6888 case ADD_NEW_DISK:
6889 case BLKROSET:
6890 case GET_ARRAY_INFO:
6891 case GET_BITMAP_FILE:
6892 case GET_DISK_INFO:
6893 case HOT_ADD_DISK:
6894 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006895 case RAID_AUTORUN:
6896 case RAID_VERSION:
6897 case RESTART_ARRAY_RW:
6898 case RUN_ARRAY:
6899 case SET_ARRAY_INFO:
6900 case SET_BITMAP_FILE:
6901 case SET_DISK_FAULTY:
6902 case STOP_ARRAY:
6903 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006904 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006905 return true;
6906 default:
6907 return false;
6908 }
6909}
6910
Al Viroa39907f2008-03-02 10:31:15 -05006911static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912 unsigned int cmd, unsigned long arg)
6913{
6914 int err = 0;
6915 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006916 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006917 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918
Nicolas Schichancb335f82014-01-15 16:58:52 +01006919 if (!md_ioctl_valid(cmd))
6920 return -ENOTTY;
6921
NeilBrown506c9e42011-12-23 10:17:26 +11006922 switch (cmd) {
6923 case RAID_VERSION:
6924 case GET_ARRAY_INFO:
6925 case GET_DISK_INFO:
6926 break;
6927 default:
6928 if (!capable(CAP_SYS_ADMIN))
6929 return -EACCES;
6930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931
6932 /*
6933 * Commands dealing with the RAID driver but not any
6934 * particular array:
6935 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006936 switch (cmd) {
6937 case RAID_VERSION:
6938 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006939 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006942 case RAID_AUTORUN:
6943 err = 0;
6944 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006945 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006947 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 }
6949
6950 /*
6951 * Commands creating/starting a new array:
6952 */
6953
Al Viroa39907f2008-03-02 10:31:15 -05006954 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955
6956 if (!mddev) {
6957 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006958 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 }
6960
NeilBrown1ca69c42012-10-11 13:37:33 +11006961 /* Some actions do not requires the mutex */
6962 switch (cmd) {
6963 case GET_ARRAY_INFO:
6964 if (!mddev->raid_disks && !mddev->external)
6965 err = -ENODEV;
6966 else
6967 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006968 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006969
6970 case GET_DISK_INFO:
6971 if (!mddev->raid_disks && !mddev->external)
6972 err = -ENODEV;
6973 else
6974 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006975 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006976
6977 case SET_DISK_FAULTY:
6978 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006979 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006980
6981 case GET_BITMAP_FILE:
6982 err = get_bitmap_file(mddev, argp);
6983 goto out;
6984
NeilBrown1ca69c42012-10-11 13:37:33 +11006985 }
6986
NeilBrowna7a3f082012-12-11 13:35:54 +11006987 if (cmd == ADD_NEW_DISK)
6988 /* need to ensure md_delayed_delete() has completed */
6989 flush_workqueue(md_misc_wq);
6990
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006991 if (cmd == HOT_REMOVE_DISK)
6992 /* need to ensure recovery thread has run */
6993 wait_event_interruptible_timeout(mddev->sb_wait,
6994 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08006995 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006996 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006997 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6998 /* Need to flush page cache, and ensure no-one else opens
6999 * and writes
7000 */
7001 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007002 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007003 mutex_unlock(&mddev->open_mutex);
7004 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007005 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007006 }
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007007 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown260fa032013-08-27 16:44:13 +10007008 mutex_unlock(&mddev->open_mutex);
7009 sync_blockdev(bdev);
7010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011 err = mddev_lock(mddev);
7012 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007013 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7014 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007015 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 }
7017
NeilBrownc02c0ae2012-12-11 13:39:21 +11007018 if (cmd == SET_ARRAY_INFO) {
7019 mdu_array_info_t info;
7020 if (!arg)
7021 memset(&info, 0, sizeof(info));
7022 else if (copy_from_user(&info, argp, sizeof(info))) {
7023 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007024 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007025 }
7026 if (mddev->pers) {
7027 err = update_array_info(mddev, &info);
7028 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007029 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007030 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007032 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007033 }
7034 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007035 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007036 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007037 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007038 }
7039 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007040 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007041 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007042 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007043 }
7044 err = set_array_info(mddev, &info);
7045 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007046 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007047 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007048 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007049 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050 }
7051
7052 /*
7053 * Commands querying/configuring an existing array:
7054 */
NeilBrown32a76272005-06-21 17:17:14 -07007055 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007056 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007057 if ((!mddev->raid_disks && !mddev->external)
7058 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7059 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7060 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007062 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 }
7064
7065 /*
7066 * Commands even a read-only array can execute:
7067 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007068 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007069 case RESTART_ARRAY_RW:
7070 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007071 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007072
7073 case STOP_ARRAY:
7074 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007075 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007076
7077 case STOP_ARRAY_RO:
7078 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007079 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007080
NeilBrown3ea8929d2013-04-24 11:42:41 +10007081 case HOT_REMOVE_DISK:
7082 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007083 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007084
NeilBrown7ceb17e2013-04-24 11:42:42 +10007085 case ADD_NEW_DISK:
7086 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007087 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007088 * So require mddev->pers and MD_DISK_SYNC.
7089 */
7090 if (mddev->pers) {
7091 mdu_disk_info_t info;
7092 if (copy_from_user(&info, argp, sizeof(info)))
7093 err = -EFAULT;
7094 else if (!(info.state & (1<<MD_DISK_SYNC)))
7095 /* Need to clear read-only for this */
7096 break;
7097 else
7098 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007099 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007100 }
7101 break;
7102
NeilBrownc02c0ae2012-12-11 13:39:21 +11007103 case BLKROSET:
7104 if (get_user(ro, (int __user *)(arg))) {
7105 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007106 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007107 }
7108 err = -EINVAL;
7109
7110 /* if the bdev is going readonly the value of mddev->ro
7111 * does not matter, no writes are coming
7112 */
7113 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007114 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007115
NeilBrownc02c0ae2012-12-11 13:39:21 +11007116 /* are we are already prepared for writes? */
7117 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007118 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007119
7120 /* transitioning to readauto need only happen for
7121 * arrays that call md_write_start
7122 */
7123 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007125 if (err == 0) {
7126 mddev->ro = 2;
7127 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007128 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007129 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007130 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 }
7132
7133 /*
7134 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007135 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 */
NeilBrown326eb172014-09-30 15:36:28 +10007137 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007138 if (mddev->ro == 2) {
7139 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007140 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007141 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007142 /* mddev_unlock will wake thread */
7143 /* If a device failed while we were read-only, we
7144 * need to make sure the metadata is updated now.
7145 */
Shaohua Li29530792016-12-08 15:48:19 -08007146 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007147 mddev_unlock(mddev);
7148 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007149 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7150 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007151 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007152 }
NeilBrownf91de922005-11-08 21:39:36 -08007153 } else {
7154 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007155 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157 }
7158
NeilBrownc02c0ae2012-12-11 13:39:21 +11007159 switch (cmd) {
7160 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007162 mdu_disk_info_t info;
7163 if (copy_from_user(&info, argp, sizeof(info)))
7164 err = -EFAULT;
7165 else
7166 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007167 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007170 case CLUSTERED_DISK_NACK:
7171 if (mddev_is_clustered(mddev))
7172 md_cluster_ops->new_disk_ack(mddev, false);
7173 else
7174 err = -EINVAL;
7175 goto unlock;
7176
NeilBrownc02c0ae2012-12-11 13:39:21 +11007177 case HOT_ADD_DISK:
7178 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007179 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180
NeilBrownc02c0ae2012-12-11 13:39:21 +11007181 case RUN_ARRAY:
7182 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007183 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184
NeilBrownc02c0ae2012-12-11 13:39:21 +11007185 case SET_BITMAP_FILE:
7186 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007187 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007188
NeilBrownc02c0ae2012-12-11 13:39:21 +11007189 default:
7190 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007191 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192 }
7193
NeilBrown3adc28d2014-09-30 15:46:41 +10007194unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007195 if (mddev->hold_active == UNTIL_IOCTL &&
7196 err != -EINVAL)
7197 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007199out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200 return err;
7201}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007202#ifdef CONFIG_COMPAT
7203static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7204 unsigned int cmd, unsigned long arg)
7205{
7206 switch (cmd) {
7207 case HOT_REMOVE_DISK:
7208 case HOT_ADD_DISK:
7209 case SET_DISK_FAULTY:
7210 case SET_BITMAP_FILE:
7211 /* These take in integer arg, do not convert */
7212 break;
7213 default:
7214 arg = (unsigned long)compat_ptr(arg);
7215 break;
7216 }
7217
7218 return md_ioctl(bdev, mode, cmd, arg);
7219}
7220#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221
Al Viroa39907f2008-03-02 10:31:15 -05007222static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223{
7224 /*
7225 * Succeed if we can lock the mddev, which confirms that
7226 * it isn't being stopped right now.
7227 */
NeilBrownfd01b882011-10-11 16:47:53 +11007228 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229 int err;
7230
Yuanhan Liu0c098222012-05-22 13:55:32 +10007231 if (!mddev)
7232 return -ENODEV;
7233
NeilBrownd3374822009-01-09 08:31:10 +11007234 if (mddev->gendisk != bdev->bd_disk) {
7235 /* we are racing with mddev_put which is discarding this
7236 * bd_disk.
7237 */
7238 mddev_put(mddev);
7239 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007240 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007241 /* Then retry the open from the top */
7242 return -ERESTARTSYS;
7243 }
7244 BUG_ON(mddev != bdev->bd_disk->private_data);
7245
NeilBrownc8c00a62009-08-10 12:50:52 +10007246 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247 goto out;
7248
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007249 if (test_bit(MD_CLOSING, &mddev->flags)) {
7250 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007251 err = -ENODEV;
7252 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007253 }
7254
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007256 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007257 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007259 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007261 if (err)
7262 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263 return err;
7264}
7265
Al Virodb2a1442013-05-05 21:52:57 -04007266static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007268 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007270 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007271 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007274
7275static int md_media_changed(struct gendisk *disk)
7276{
NeilBrownfd01b882011-10-11 16:47:53 +11007277 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007278
7279 return mddev->changed;
7280}
7281
7282static int md_revalidate(struct gendisk *disk)
7283{
NeilBrownfd01b882011-10-11 16:47:53 +11007284 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007285
7286 mddev->changed = 0;
7287 return 0;
7288}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007289static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290{
7291 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007292 .open = md_open,
7293 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007294 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007295#ifdef CONFIG_COMPAT
7296 .compat_ioctl = md_compat_ioctl,
7297#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007298 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007299 .media_changed = md_media_changed,
7300 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301};
7302
NeilBrownf72ffdd2014-09-30 14:23:59 +10007303static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304{
NeilBrown2b8bf342011-10-11 16:48:23 +11007305 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 /*
7308 * md_thread is a 'system-thread', it's priority should be very
7309 * high. We avoid resource deadlocks individually in each
7310 * raid personality. (RAID5 does preallocation) We also use RR and
7311 * the very same RT priority as kswapd, thus we will never get
7312 * into a priority inversion deadlock.
7313 *
7314 * we definitely have to have equal or higher priority than
7315 * bdflush, otherwise bdflush will deadlock if there are too
7316 * many dirty RAID5 blocks.
7317 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318
NeilBrown6985c432005-10-19 21:23:47 -07007319 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007320 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007321
NeilBrown93588e22005-11-15 00:09:12 -08007322 /* We need to wait INTERRUPTIBLE so that
7323 * we don't add to the load-average.
7324 * That means we need to be sure no signals are
7325 * pending
7326 */
7327 if (signal_pending(current))
7328 flush_signals(current);
7329
7330 wait_event_interruptible_timeout
7331 (thread->wqueue,
7332 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007333 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007334 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335
NeilBrown6c987912011-01-14 09:13:53 +11007336 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007337 if (kthread_should_park())
7338 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007339 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007340 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007342
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343 return 0;
7344}
7345
NeilBrown2b8bf342011-10-11 16:48:23 +11007346void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347{
7348 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007349 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 set_bit(THREAD_WAKEUP, &thread->flags);
7351 wake_up(&thread->wqueue);
7352 }
7353}
NeilBrown6c144d32014-09-30 16:15:38 +10007354EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355
Shaohua Li4ed87312012-10-11 13:34:00 +11007356struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7357 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358{
NeilBrown2b8bf342011-10-11 16:48:23 +11007359 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360
NeilBrown2b8bf342011-10-11 16:48:23 +11007361 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 if (!thread)
7363 return NULL;
7364
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 init_waitqueue_head(&thread->wqueue);
7366
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367 thread->run = run;
7368 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007369 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007370 thread->tsk = kthread_run(md_thread, thread,
7371 "%s_%s",
7372 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007373 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007374 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 kfree(thread);
7376 return NULL;
7377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378 return thread;
7379}
NeilBrown6c144d32014-09-30 16:15:38 +10007380EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381
NeilBrown2b8bf342011-10-11 16:48:23 +11007382void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383{
NeilBrown2b8bf342011-10-11 16:48:23 +11007384 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007385 if (!thread)
7386 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007387 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007388 /* Locking ensures that mddev_unlock does not wake_up a
7389 * non-existent thread
7390 */
7391 spin_lock(&pers_lock);
7392 *threadp = NULL;
7393 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007394
7395 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 kfree(thread);
7397}
NeilBrown6c144d32014-09-30 16:15:38 +10007398EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399
NeilBrownfd01b882011-10-11 16:47:53 +11007400void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401{
NeilBrownb2d444d2005-11-08 21:39:31 -08007402 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007404
NeilBrownde393cd2011-07-28 11:31:48 +10007405 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406 return;
7407 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007408 if (mddev->degraded)
7409 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007410 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7412 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7413 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007414 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007415 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007416 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417}
NeilBrown6c144d32014-09-30 16:15:38 +10007418EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419
7420/* seq_file implementation /proc/mdstat */
7421
7422static void status_unused(struct seq_file *seq)
7423{
7424 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007425 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426
7427 seq_printf(seq, "unused devices: ");
7428
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007429 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430 char b[BDEVNAME_SIZE];
7431 i++;
7432 seq_printf(seq, "%s ",
7433 bdevname(rdev->bdev,b));
7434 }
7435 if (!i)
7436 seq_printf(seq, "<none>");
7437
7438 seq_printf(seq, "\n");
7439}
7440
NeilBrownf7851be2015-07-02 17:12:58 +10007441static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007442{
NeilBrowndd71cf62009-05-07 12:49:35 +10007443 sector_t max_sectors, resync, res;
7444 unsigned long dt, db;
7445 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007446 int scale;
7447 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448
NeilBrownc804cde2012-05-21 09:28:33 +10007449 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7450 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007451 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007453 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454
NeilBrownf7851be2015-07-02 17:12:58 +10007455 resync = mddev->curr_resync;
7456 if (resync <= 3) {
7457 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7458 /* Still cleaning up */
7459 resync = max_sectors;
7460 } else
7461 resync -= atomic_read(&mddev->recovery_active);
7462
7463 if (resync == 0) {
7464 if (mddev->recovery_cp < MaxSector) {
7465 seq_printf(seq, "\tresync=PENDING");
7466 return 1;
7467 }
7468 return 0;
7469 }
7470 if (resync < 3) {
7471 seq_printf(seq, "\tresync=DELAYED");
7472 return 1;
7473 }
7474
NeilBrown403df472014-09-30 15:52:29 +10007475 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007476 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007477 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007478 * u32, as those are the requirements for sector_div.
7479 * Thus 'scale' must be at least 10
7480 */
7481 scale = 10;
7482 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007483 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007484 scale++;
7485 }
7486 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007487 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007488
7489 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490 {
NeilBrown4588b422006-03-27 01:18:04 -08007491 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007492 seq_printf(seq, "[");
7493 for (i = 0; i < x; i++)
7494 seq_printf(seq, "=");
7495 seq_printf(seq, ">");
7496 for (i = 0; i < y; i++)
7497 seq_printf(seq, ".");
7498 seq_printf(seq, "] ");
7499 }
NeilBrown4588b422006-03-27 01:18:04 -08007500 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007501 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7502 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007503 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7504 "check" :
7505 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7506 "resync" : "recovery"))),
7507 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007508 (unsigned long long) resync/2,
7509 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510
7511 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 * dt: time from mark until now
7513 * db: blocks written from mark until now
7514 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007515 *
7516 * rt is a sector_t, so could be 32bit or 64bit.
7517 * So we divide before multiply in case it is 32bit and close
7518 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007519 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007520 * near the end of resync when the number of remaining sectors
7521 * is close to 'db'.
7522 * We then divide rt by 32 after multiplying by db to compensate.
7523 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524 */
7525 dt = ((jiffies - mddev->resync_mark) / HZ);
7526 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007527 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7528 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007529
NeilBrowndd71cf62009-05-07 12:49:35 +10007530 rt = max_sectors - resync; /* number of remaining sectors */
7531 sector_div(rt, db/32+1);
7532 rt *= dt;
7533 rt >>= 5;
7534
7535 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7536 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537
NeilBrownff4e8d92006-07-10 04:44:16 -07007538 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007539 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007540}
7541
7542static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7543{
7544 struct list_head *tmp;
7545 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007546 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547
7548 if (l >= 0x10000)
7549 return NULL;
7550 if (!l--)
7551 /* header */
7552 return (void*)1;
7553
7554 spin_lock(&all_mddevs_lock);
7555 list_for_each(tmp,&all_mddevs)
7556 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007557 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007558 mddev_get(mddev);
7559 spin_unlock(&all_mddevs_lock);
7560 return mddev;
7561 }
7562 spin_unlock(&all_mddevs_lock);
7563 if (!l--)
7564 return (void*)2;/* tail */
7565 return NULL;
7566}
7567
7568static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7569{
7570 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007571 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007572
Linus Torvalds1da177e2005-04-16 15:20:36 -07007573 ++*pos;
7574 if (v == (void*)2)
7575 return NULL;
7576
7577 spin_lock(&all_mddevs_lock);
7578 if (v == (void*)1)
7579 tmp = all_mddevs.next;
7580 else
7581 tmp = mddev->all_mddevs.next;
7582 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007583 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584 else {
7585 next_mddev = (void*)2;
7586 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588 spin_unlock(&all_mddevs_lock);
7589
7590 if (v != (void*)1)
7591 mddev_put(mddev);
7592 return next_mddev;
7593
7594}
7595
7596static void md_seq_stop(struct seq_file *seq, void *v)
7597{
NeilBrownfd01b882011-10-11 16:47:53 +11007598 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599
7600 if (mddev && v != (void*)1 && v != (void*)2)
7601 mddev_put(mddev);
7602}
7603
7604static int md_seq_show(struct seq_file *seq, void *v)
7605{
NeilBrownfd01b882011-10-11 16:47:53 +11007606 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007607 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007608 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609
7610 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007611 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612 seq_printf(seq, "Personalities : ");
7613 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007614 list_for_each_entry(pers, &pers_list, list)
7615 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616
7617 spin_unlock(&pers_lock);
7618 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007619 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620 return 0;
7621 }
7622 if (v == (void*)2) {
7623 status_unused(seq);
7624 return 0;
7625 }
7626
NeilBrown36d091f2014-12-15 12:56:58 +11007627 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7629 seq_printf(seq, "%s : %sactive", mdname(mddev),
7630 mddev->pers ? "" : "in");
7631 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007632 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007634 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007635 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636 seq_printf(seq, " %s", mddev->pers->name);
7637 }
7638
Andre Nolldd8ac332009-03-31 14:33:13 +11007639 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007640 rcu_read_lock();
7641 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642 char b[BDEVNAME_SIZE];
7643 seq_printf(seq, " %s[%d]",
7644 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007645 if (test_bit(WriteMostly, &rdev->flags))
7646 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007647 if (test_bit(Journal, &rdev->flags))
7648 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007649 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007650 seq_printf(seq, "(F)");
7651 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007652 }
7653 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007654 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007655 if (test_bit(Replacement, &rdev->flags))
7656 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007657 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007659 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660
7661 if (!list_empty(&mddev->disks)) {
7662 if (mddev->pers)
7663 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007664 (unsigned long long)
7665 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666 else
7667 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007668 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007670 if (mddev->persistent) {
7671 if (mddev->major_version != 0 ||
7672 mddev->minor_version != 90) {
7673 seq_printf(seq," super %d.%d",
7674 mddev->major_version,
7675 mddev->minor_version);
7676 }
NeilBrowne6910632008-02-06 01:39:51 -08007677 } else if (mddev->external)
7678 seq_printf(seq, " super external:%s",
7679 mddev->metadata_type);
7680 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007681 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682
7683 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007684 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007685 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007686 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007687 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007688 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007689 }
NeilBrown32a76272005-06-21 17:17:14 -07007690 } else
7691 seq_printf(seq, "\n ");
7692
NeilBrown57148962012-03-19 12:46:40 +11007693 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694
7695 seq_printf(seq, "\n");
7696 }
NeilBrown36d091f2014-12-15 12:56:58 +11007697 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007698
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699 return 0;
7700}
7701
Jan Engelhardt110518b2009-05-07 12:49:37 +10007702static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007703 .start = md_seq_start,
7704 .next = md_seq_next,
7705 .stop = md_seq_stop,
7706 .show = md_seq_show,
7707};
7708
7709static int md_seq_open(struct inode *inode, struct file *file)
7710{
Kay Sieversf1514632011-07-12 20:48:39 +02007711 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712 int error;
7713
7714 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007715 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007716 return error;
7717
7718 seq = file->private_data;
7719 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720 return error;
7721}
7722
NeilBrowne2f23b62014-04-09 14:33:51 +10007723static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007724static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7725{
Kay Sieversf1514632011-07-12 20:48:39 +02007726 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007727 int mask;
7728
NeilBrowne2f23b62014-04-09 14:33:51 +10007729 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007730 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007731 poll_wait(filp, &md_event_waiters, wait);
7732
7733 /* always allow read */
7734 mask = POLLIN | POLLRDNORM;
7735
Kay Sieversf1514632011-07-12 20:48:39 +02007736 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007737 mask |= POLLERR | POLLPRI;
7738 return mask;
7739}
7740
Arjan van de Venfa027c22007-02-12 00:55:33 -08007741static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007742 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743 .open = md_seq_open,
7744 .read = seq_read,
7745 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007746 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007747 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007748};
7749
NeilBrown84fc4b52011-10-11 16:49:58 +11007750int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751{
NeilBrown9d487392016-11-02 14:16:49 +11007752 pr_debug("md: %s personality registered for level %d\n",
7753 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007754 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007755 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007756 spin_unlock(&pers_lock);
7757 return 0;
7758}
NeilBrown6c144d32014-09-30 16:15:38 +10007759EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760
NeilBrown84fc4b52011-10-11 16:49:58 +11007761int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007762{
NeilBrown9d487392016-11-02 14:16:49 +11007763 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007765 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766 spin_unlock(&pers_lock);
7767 return 0;
7768}
NeilBrown6c144d32014-09-30 16:15:38 +10007769EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770
NeilBrown6022e752015-08-13 12:32:55 +10007771int register_md_cluster_operations(struct md_cluster_operations *ops,
7772 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007773{
NeilBrown6022e752015-08-13 12:32:55 +10007774 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007775 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007776 if (md_cluster_ops != NULL)
7777 ret = -EALREADY;
7778 else {
7779 md_cluster_ops = ops;
7780 md_cluster_mod = module;
7781 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007782 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007783 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007784}
7785EXPORT_SYMBOL(register_md_cluster_operations);
7786
7787int unregister_md_cluster_operations(void)
7788{
7789 spin_lock(&pers_lock);
7790 md_cluster_ops = NULL;
7791 spin_unlock(&pers_lock);
7792 return 0;
7793}
7794EXPORT_SYMBOL(unregister_md_cluster_operations);
7795
7796int md_setup_cluster(struct mddev *mddev, int nodes)
7797{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007798 if (!md_cluster_ops)
7799 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007800 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007801 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007802 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11007803 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007804 spin_unlock(&pers_lock);
7805 return -ENOENT;
7806 }
7807 spin_unlock(&pers_lock);
7808
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007809 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007810}
7811
7812void md_cluster_stop(struct mddev *mddev)
7813{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007814 if (!md_cluster_ops)
7815 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007816 md_cluster_ops->leave(mddev);
7817 module_put(md_cluster_mod);
7818}
7819
NeilBrownfd01b882011-10-11 16:47:53 +11007820static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007822 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007824 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825
7826 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007827 rcu_read_lock();
7828 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007830 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7831 (int)part_stat_read(&disk->part0, sectors[1]) -
7832 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007833 /* sync IO will cause sync_io to increase before the disk_stats
7834 * as sync_io is counted when a request starts, and
7835 * disk_stats is counted when it completes.
7836 * So resync activity will cause curr_events to be smaller than
7837 * when there was no such activity.
7838 * non-sync IO will cause disk_stat to increase without
7839 * increasing sync_io so curr_events will (eventually)
7840 * be larger than it was before. Once it becomes
7841 * substantially larger, the test below will cause
7842 * the array to appear non-idle, and resync will slow
7843 * down.
7844 * If there is a lot of outstanding resync activity when
7845 * we set last_event to curr_events, then all that activity
7846 * completing might cause the array to appear non-idle
7847 * and resync will be slowed down even though there might
7848 * not have been non-resync activity. This will only
7849 * happen once though. 'last_events' will soon reflect
7850 * the state where there is little or no outstanding
7851 * resync requests, and further resync activity will
7852 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007853 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007855 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856 rdev->last_events = curr_events;
7857 idle = 0;
7858 }
7859 }
NeilBrown4b809912008-07-21 17:05:25 +10007860 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861 return idle;
7862}
7863
NeilBrownfd01b882011-10-11 16:47:53 +11007864void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865{
7866 /* another "blocks" (512byte) blocks have been synced */
7867 atomic_sub(blocks, &mddev->recovery_active);
7868 wake_up(&mddev->recovery_wait);
7869 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007870 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007871 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872 md_wakeup_thread(mddev->thread);
7873 // stop recovery, signal do_sync ....
7874 }
7875}
NeilBrown6c144d32014-09-30 16:15:38 +10007876EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877
NeilBrown06d91a52005-06-21 17:17:12 -07007878/* md_write_start(mddev, bi)
7879 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007880 * in superblock) before writing, schedule a superblock update
7881 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007882 */
NeilBrownfd01b882011-10-11 16:47:53 +11007883void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007884{
Neil Brown0fd62b82008-06-28 08:31:36 +10007885 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007886 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007887 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007888
NeilBrownf91de922005-11-08 21:39:36 -08007889 BUG_ON(mddev->ro == 1);
7890 if (mddev->ro == 2) {
7891 /* need to switch to read/write */
7892 mddev->ro = 0;
7893 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7894 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007895 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007896 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007897 }
NeilBrown06d91a52005-06-21 17:17:12 -07007898 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007899 if (mddev->safemode == 1)
7900 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007901 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007902 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007903 if (mddev->in_sync) {
7904 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08007905 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
7906 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007907 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007908 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007909 }
NeilBrown85572d72014-12-15 12:56:56 +11007910 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007911 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007912 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007913 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007914 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007915 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916}
NeilBrown6c144d32014-09-30 16:15:38 +10007917EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007918
NeilBrownfd01b882011-10-11 16:47:53 +11007919void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007920{
7921 if (atomic_dec_and_test(&mddev->writes_pending)) {
7922 if (mddev->safemode == 2)
7923 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007924 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7926 }
7927}
NeilBrown6c144d32014-09-30 16:15:38 +10007928EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929
NeilBrown2a2275d2007-01-26 00:57:11 -08007930/* md_allow_write(mddev)
7931 * Calling this ensures that the array is marked 'active' so that writes
7932 * may proceed without blocking. It is important to call this before
7933 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7934 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007935 *
Shaohua Li29530792016-12-08 15:48:19 -08007936 * In the ->external case MD_SB_CHANGE_PENDING can not be cleared until mddev->lock
Dan Williamsb5470dc2008-06-27 21:44:04 -07007937 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007938 */
NeilBrownfd01b882011-10-11 16:47:53 +11007939int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007940{
7941 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007942 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007943 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007944 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007945 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007946 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007947
NeilBrown85572d72014-12-15 12:56:56 +11007948 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007949 if (mddev->in_sync) {
7950 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08007951 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
7952 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007953 if (mddev->safemode_delay &&
7954 mddev->safemode == 0)
7955 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007956 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007957 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007958 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007959 } else
NeilBrown85572d72014-12-15 12:56:56 +11007960 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007961
Shaohua Li29530792016-12-08 15:48:19 -08007962 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007963 return -EAGAIN;
7964 else
7965 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007966}
7967EXPORT_SYMBOL_GPL(md_allow_write);
7968
Linus Torvalds1da177e2005-04-16 15:20:36 -07007969#define SYNC_MARKS 10
7970#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007971#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007972void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973{
Shaohua Li4ed87312012-10-11 13:34:00 +11007974 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007975 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976 unsigned int currspeed = 0,
7977 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007978 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007980 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007981 sector_t mark_cnt[SYNC_MARKS];
7982 int last_mark,m;
7983 struct list_head *tmp;
7984 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007985 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007986 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007987 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007988 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007989 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007990
7991 /* just incase thread restarts... */
7992 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7993 return;
NeilBrown3991b312014-05-28 13:39:23 +10007994 if (mddev->ro) {/* never try to sync a read-only array */
7995 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007996 return;
NeilBrown3991b312014-05-28 13:39:23 +10007997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007998
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007999 if (mddev_is_clustered(mddev)) {
8000 ret = md_cluster_ops->resync_start(mddev);
8001 if (ret)
8002 goto skip;
8003
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008004 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008005 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8006 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8007 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8008 && ((unsigned long long)mddev->curr_resync_completed
8009 < (unsigned long long)mddev->resync_max_sectors))
8010 goto skip;
8011 }
8012
NeilBrown61df9d92006-10-03 01:15:57 -07008013 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008014 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008015 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008016 action = "check";
8017 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008018 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008019 action = "repair";
8020 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008021 desc = "resync";
8022 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8023 desc = "reshape";
8024 else
8025 desc = "recovery";
8026
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008027 mddev->last_sync_action = action ?: desc;
8028
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029 /* we overload curr_resync somewhat here.
8030 * 0 == not engaged in resync at all
8031 * 2 == checking that there is no conflict with another sync
8032 * 1 == like 2, but have yielded to allow conflicting resync to
8033 * commense
8034 * other == active in resync - this many blocks
8035 *
8036 * Before starting a resync we must have set curr_resync to
8037 * 2, and then checked that every "conflicting" array has curr_resync
8038 * less than ours. When we find one that is the same or higher
8039 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8040 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8041 * This will mean we have to start checking from the beginning again.
8042 *
8043 */
8044
8045 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008046 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008047 mddev->curr_resync = 2;
8048
8049 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008050 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008051 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008052 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008053 if (mddev2 == mddev)
8054 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008055 if (!mddev->parallel_resync
8056 && mddev2->curr_resync
8057 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008058 DEFINE_WAIT(wq);
8059 if (mddev < mddev2 && mddev->curr_resync == 2) {
8060 /* arbitrarily yield */
8061 mddev->curr_resync = 1;
8062 wake_up(&resync_wait);
8063 }
8064 if (mddev > mddev2 && mddev->curr_resync == 1)
8065 /* no need to wait here, we can wait the next
8066 * time 'round when curr_resync == 2
8067 */
8068 continue;
NeilBrown97441972008-09-19 11:49:54 +10008069 /* We need to wait 'interruptible' so as not to
8070 * contribute to the load average, and not to
8071 * be caught by 'softlockup'
8072 */
8073 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008074 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008075 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008076 if (mddev2_minor != mddev2->md_minor) {
8077 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008078 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8079 desc, mdname(mddev),
8080 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008082 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008083 if (signal_pending(current))
8084 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008085 schedule();
8086 finish_wait(&resync_wait, &wq);
8087 goto try_again;
8088 }
8089 finish_wait(&resync_wait, &wq);
8090 }
8091 }
8092 } while (mddev->curr_resync < 2);
8093
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008094 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008095 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008096 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008097 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008098 */
8099 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008100 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008101 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008102 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8103 j = mddev->resync_min;
8104 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008105 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008106
NeilBrownccfcc3c2006-03-27 01:18:09 -08008107 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10008108 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008109 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008110 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008111 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008112 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008113 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008114 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008115 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008116 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008117 !test_bit(Faulty, &rdev->flags) &&
8118 !test_bit(In_sync, &rdev->flags) &&
8119 rdev->recovery_offset < j)
8120 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008121 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008122
8123 /* If there is a bitmap, we need to make sure all
8124 * writes that started before we added a spare
8125 * complete before we start doing a recovery.
8126 * Otherwise the write might complete and (via
8127 * bitmap_endwrite) set a bit in the bitmap after the
8128 * recovery has checked that bit and skipped that
8129 * region.
8130 */
8131 if (mddev->bitmap) {
8132 mddev->pers->quiesce(mddev, 1);
8133 mddev->pers->quiesce(mddev, 0);
8134 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008136
NeilBrown9d487392016-11-02 14:16:49 +11008137 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8138 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8139 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8140 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008141
NeilBrowneea1bf32009-03-31 14:27:02 +11008142 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008143
NeilBrown57afd892005-06-21 17:17:13 -07008144 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008145 for (m = 0; m < SYNC_MARKS; m++) {
8146 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008147 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008148 }
8149 last_mark = 0;
8150 mddev->resync_mark = mark[last_mark];
8151 mddev->resync_mark_cnt = mark_cnt[last_mark];
8152
8153 /*
8154 * Tune reconstruction:
8155 */
8156 window = 32*(PAGE_SIZE/512);
NeilBrown9d487392016-11-02 14:16:49 +11008157 pr_debug("md: using %dk window, over a total of %lluk.\n",
8158 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008159
8160 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161 last_check = 0;
8162
8163 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008164 pr_debug("md: resuming %s of %s from checkpoint.\n",
8165 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008167 } else
8168 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008169 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008170 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8171 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008172 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008173
majianpeng7c2c57c2012-07-03 12:12:26 +10008174 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008176 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008177
NeilBrown57afd892005-06-21 17:17:13 -07008178 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008179
NeilBrown7a91ee12009-05-26 12:57:21 +10008180 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8181 ((mddev->curr_resync > mddev->curr_resync_completed &&
8182 (mddev->curr_resync - mddev->curr_resync_completed)
8183 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008184 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008185 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008186 >= mddev->resync_max - mddev->curr_resync_completed ||
8187 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008188 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008189 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008190 wait_event(mddev->recovery_wait,
8191 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008192 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008193 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8194 j > mddev->recovery_cp)
8195 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008196 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008197 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008198 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008199 }
NeilBrownacb180b2009-04-14 16:28:34 +10008200
NeilBrownc91abf52013-11-19 12:02:01 +11008201 while (j >= mddev->resync_max &&
8202 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008203 /* As this condition is controlled by user-space,
8204 * we can block indefinitely, so use '_interruptible'
8205 * to avoid triggering warnings.
8206 */
8207 flush_signals(current); /* just in case */
8208 wait_event_interruptible(mddev->recovery_wait,
8209 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008210 || test_bit(MD_RECOVERY_INTR,
8211 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008212 }
NeilBrownacb180b2009-04-14 16:28:34 +10008213
NeilBrownc91abf52013-11-19 12:02:01 +11008214 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8215 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008216
NeilBrown09314792015-02-19 16:04:40 +11008217 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008218 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008219 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008221 }
NeilBrown57afd892005-06-21 17:17:13 -07008222
8223 if (!skipped) { /* actual IO requested */
8224 io_sectors += sectors;
8225 atomic_add(sectors, &mddev->recovery_active);
8226 }
8227
NeilBrowne875ece2011-07-28 11:39:24 +10008228 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8229 break;
8230
Linus Torvalds1da177e2005-04-16 15:20:36 -07008231 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008232 if (j > max_sectors)
8233 /* when skipping, extra large numbers can be returned. */
8234 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008235 if (j > 2)
8236 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008237 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008238 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008239 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008240 * visible in /proc/mdstat
8241 */
8242 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008243
8244 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008245 continue;
8246
NeilBrown57afd892005-06-21 17:17:13 -07008247 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248 repeat:
8249 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8250 /* step marks */
8251 int next = (last_mark+1) % SYNC_MARKS;
8252
8253 mddev->resync_mark = mark[next];
8254 mddev->resync_mark_cnt = mark_cnt[next];
8255 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008256 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008257 last_mark = next;
8258 }
8259
NeilBrownc91abf52013-11-19 12:02:01 +11008260 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8261 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008262
8263 /*
8264 * this loop exits only if either when we are slower than
8265 * the 'hard' speed limit, or the system was IO-idle for
8266 * a jiffy.
8267 * the system might be non-idle CPU-wise, but we only care
8268 * about not overloading the IO subsystem. (things like an
8269 * e2fsck being done on the RAID array should execute fast)
8270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008271 cond_resched();
8272
Xiao Niac7e50a2014-08-07 09:37:41 -04008273 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8274 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008275 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008276
NeilBrown88202a02006-01-06 00:21:36 -08008277 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008278 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008279 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008280 goto repeat;
8281 }
NeilBrownac8fa412015-02-19 16:55:00 +11008282 if (!is_mddev_idle(mddev, 0)) {
8283 /*
8284 * Give other IO more of a chance.
8285 * The faster the devices, the less we wait.
8286 */
8287 wait_event(mddev->recovery_wait,
8288 !atomic_read(&mddev->recovery_active));
8289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008290 }
8291 }
NeilBrown9d487392016-11-02 14:16:49 +11008292 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8293 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8294 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008295 /*
8296 * this also signals 'finished resyncing' to md_stop
8297 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008298 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008299 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8300
NeilBrown5ed1df22015-07-24 13:27:08 +10008301 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8302 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008303 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008304 mddev->curr_resync_completed = mddev->curr_resync;
8305 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8306 }
NeilBrown09314792015-02-19 16:04:40 +11008307 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008308
NeilBrowndfc70642008-05-23 13:04:39 -07008309 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008310 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008311 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8312 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8313 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008314 pr_debug("md: checkpointing %s of %s.\n",
8315 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008316 if (test_bit(MD_RECOVERY_ERROR,
8317 &mddev->recovery))
8318 mddev->recovery_cp =
8319 mddev->curr_resync_completed;
8320 else
8321 mddev->recovery_cp =
8322 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008323 }
8324 } else
8325 mddev->recovery_cp = MaxSector;
8326 } else {
8327 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8328 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008329 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008330 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008331 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008332 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008333 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008334 !test_bit(Faulty, &rdev->flags) &&
8335 !test_bit(In_sync, &rdev->flags) &&
8336 rdev->recovery_offset < mddev->curr_resync)
8337 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008338 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008340 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008341 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008342 /* set CHANGE_PENDING here since maybe another update is needed,
8343 * so other nodes are informed. It should be harmless for normal
8344 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008345 set_mask_bits(&mddev->sb_flags, 0,
8346 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008347
NeilBrown23da4222014-12-15 12:57:01 +11008348 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008349 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8350 /* We completed so min/max setting can be forgotten if used. */
8351 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8352 mddev->resync_min = 0;
8353 mddev->resync_max = MaxSector;
8354 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8355 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008356 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008357 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008358 spin_unlock(&mddev->lock);
8359
Linus Torvalds1da177e2005-04-16 15:20:36 -07008360 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008361 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008362 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008363}
NeilBrown29269552006-03-27 01:18:10 -08008364EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365
NeilBrown746d3202013-04-24 11:42:41 +10008366static int remove_and_add_spares(struct mddev *mddev,
8367 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008368{
NeilBrown3cb03002011-10-11 16:45:26 +11008369 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008370 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008371 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008372 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008373
NeilBrownd787be42016-06-02 16:19:53 +10008374 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008375 if ((this == NULL || rdev == this) &&
8376 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008377 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008378 test_bit(Faulty, &rdev->flags) &&
8379 atomic_read(&rdev->nr_pending)==0) {
8380 /* Faulty non-Blocked devices with nr_pending == 0
8381 * never get nr_pending incremented,
8382 * never get Faulty cleared, and never get Blocked set.
8383 * So we can synchronize_rcu now rather than once per device
8384 */
8385 remove_some = true;
8386 set_bit(RemoveSynchronized, &rdev->flags);
8387 }
8388 }
8389
8390 if (remove_some)
8391 synchronize_rcu();
8392 rdev_for_each(rdev, mddev) {
8393 if ((this == NULL || rdev == this) &&
8394 rdev->raid_disk >= 0 &&
8395 !test_bit(Blocked, &rdev->flags) &&
8396 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008397 (!test_bit(In_sync, &rdev->flags) &&
8398 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008399 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008400 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008401 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008402 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008403 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008404 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008405 }
8406 }
NeilBrownd787be42016-06-02 16:19:53 +10008407 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8408 clear_bit(RemoveSynchronized, &rdev->flags);
8409 }
8410
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008411 if (removed && mddev->kobj.sd)
8412 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008413
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008414 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008415 goto no_add;
8416
NeilBrowndafb20f2012-03-19 12:46:39 +11008417 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008418 if (this && this != rdev)
8419 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008420 if (test_bit(Candidate, &rdev->flags))
8421 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008422 if (rdev->raid_disk >= 0 &&
8423 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008424 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008425 !test_bit(Faulty, &rdev->flags))
8426 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008427 if (rdev->raid_disk >= 0)
8428 continue;
8429 if (test_bit(Faulty, &rdev->flags))
8430 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008431 if (!test_bit(Journal, &rdev->flags)) {
8432 if (mddev->ro &&
8433 ! (rdev->saved_raid_disk >= 0 &&
8434 !test_bit(Bitmap_sync, &rdev->flags)))
8435 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008436
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008437 rdev->recovery_offset = 0;
8438 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008439 if (mddev->pers->
8440 hot_add_disk(mddev, rdev) == 0) {
8441 if (sysfs_link_rdev(mddev, rdev))
8442 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008443 if (!test_bit(Journal, &rdev->flags))
8444 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008445 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08008446 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008447 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008448 }
NeilBrown746d3202013-04-24 11:42:41 +10008449no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008450 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08008451 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008452 return spares;
8453}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008454
NeilBrownac05f252014-09-30 08:10:42 +10008455static void md_start_sync(struct work_struct *ws)
8456{
8457 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008458
NeilBrownac05f252014-09-30 08:10:42 +10008459 mddev->sync_thread = md_register_thread(md_do_sync,
8460 mddev,
8461 "resync");
8462 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11008463 pr_warn("%s: could not start resync thread...\n",
8464 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008465 /* leave the spares where they are, it shouldn't hurt */
8466 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8467 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8468 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8469 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8470 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008471 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008472 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8473 &mddev->recovery))
8474 if (mddev->sysfs_action)
8475 sysfs_notify_dirent_safe(mddev->sysfs_action);
8476 } else
8477 md_wakeup_thread(mddev->sync_thread);
8478 sysfs_notify_dirent_safe(mddev->sysfs_action);
8479 md_new_event(mddev);
8480}
8481
Linus Torvalds1da177e2005-04-16 15:20:36 -07008482/*
8483 * This routine is regularly called by all per-raid-array threads to
8484 * deal with generic issues like resync and super-block update.
8485 * Raid personalities that don't have a thread (linear/raid0) do not
8486 * need this as they never do any recovery or update the superblock.
8487 *
8488 * It does not do any resync itself, but rather "forks" off other threads
8489 * to do that as needed.
8490 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8491 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008492 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008493 * and wakeups up this thread which will reap the thread and finish up.
8494 * This thread also removes any faulty devices (with nr_pending == 0).
8495 *
8496 * The overall approach is:
8497 * 1/ if the superblock needs updating, update it.
8498 * 2/ If a recovery thread is running, don't do anything else.
8499 * 3/ If recovery has finished, clean up, possibly marking spares active.
8500 * 4/ If there are any faulty devices, remove them.
8501 * 5/ If array is degraded, try to add spares devices
8502 * 6/ If array has spares or is not in-sync, start a resync thread.
8503 */
NeilBrownfd01b882011-10-11 16:47:53 +11008504void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008505{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008506 if (mddev->suspended)
8507 return;
8508
NeilBrown5f404022005-06-21 17:17:16 -07008509 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008510 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008511
NeilBrownfca4d842005-06-21 17:17:11 -07008512 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008513 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11008514 pr_debug("md: %s in immediate safe mode\n",
8515 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07008516 mddev->safemode = 2;
8517 }
8518 flush_signals(current);
8519 }
8520
NeilBrownc89a8ee2008-08-05 15:54:13 +10008521 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8522 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08008524 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008525 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008526 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008527 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008528 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8529 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530 ))
8531 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008532
NeilBrowndf5b89b2006-03-27 01:18:20 -08008533 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008534 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008535
NeilBrownc89a8ee2008-08-05 15:54:13 +10008536 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008537 struct md_rdev *rdev;
8538 if (!mddev->external && mddev->in_sync)
8539 /* 'Blocked' flag not needed as failed devices
8540 * will be recorded if array switched to read/write.
8541 * Leaving it set will prevent the device
8542 * from being removed.
8543 */
8544 rdev_for_each(rdev, mddev)
8545 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008546 /* On a read-only array we can:
8547 * - remove failed devices
8548 * - add already-in_sync devices if the array itself
8549 * is in-sync.
8550 * As we only add devices that are already in-sync,
8551 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008552 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008553 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008554 /* There is no thread, but we need to call
8555 * ->spare_active and clear saved_raid_disk
8556 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008557 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008558 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008559 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008560 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08008561 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008562 goto unlock;
8563 }
8564
Guoqing Jiang659b2542015-12-21 10:50:59 +11008565 if (mddev_is_clustered(mddev)) {
8566 struct md_rdev *rdev;
8567 /* kick the device if another node issued a
8568 * remove disk.
8569 */
8570 rdev_for_each(rdev, mddev) {
8571 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8572 rdev->raid_disk < 0)
8573 md_kick_rdev_from_array(rdev);
8574 }
8575 }
8576
NeilBrown31a59e32008-04-30 00:52:30 -07008577 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008578 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008579 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008580 if (mddev->safemode &&
8581 !atomic_read(&mddev->writes_pending) &&
8582 !mddev->in_sync &&
8583 mddev->recovery_cp == MaxSector) {
8584 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008585 did_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08008586 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008587 }
8588 if (mddev->safemode == 1)
8589 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008590 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008591 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008592 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008593 }
NeilBrownfca4d842005-06-21 17:17:11 -07008594
Shaohua Li29530792016-12-08 15:48:19 -08008595 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07008596 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008597
Linus Torvalds1da177e2005-04-16 15:20:36 -07008598 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8599 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8600 /* resync/recovery still happening */
8601 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8602 goto unlock;
8603 }
8604 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008605 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008606 goto unlock;
8607 }
Neil Brown72a23c22008-06-28 08:31:41 +10008608 /* Set RUNNING before clearing NEEDED to avoid
8609 * any transients in the value of "sync_action".
8610 */
NeilBrown72f36d52012-10-11 14:25:57 +11008611 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008612 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008613 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008614 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008615 /* Clear some bits that don't mean anything, but
8616 * might be left set
8617 */
NeilBrown24dd4692005-11-08 21:39:26 -08008618 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8619 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008620
NeilBrowned209582012-04-24 10:23:14 +10008621 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8622 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008623 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008624 /* no recovery is running.
8625 * remove any failed drives, then
8626 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008627 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008628 * the personality to fail the re-add.
8629 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008630
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008631 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008632 if (mddev->pers->check_reshape == NULL ||
8633 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008634 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008635 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008636 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008637 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008638 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008639 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8640 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008641 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008642 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008643 } else if (mddev->recovery_cp < MaxSector) {
8644 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008645 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008646 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8647 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008648 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008649
Linus Torvalds1da177e2005-04-16 15:20:36 -07008650 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008651 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008652 /* We are adding a device or devices to an array
8653 * which has the bitmap stored on all devices.
8654 * So make sure all bitmap pages get written
8655 */
8656 bitmap_write_all(mddev->bitmap);
8657 }
NeilBrownac05f252014-09-30 08:10:42 +10008658 INIT_WORK(&mddev->del_work, md_start_sync);
8659 queue_work(md_misc_wq, &mddev->del_work);
8660 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008661 }
NeilBrownac05f252014-09-30 08:10:42 +10008662 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008663 if (!mddev->sync_thread) {
8664 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008665 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008666 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8667 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008668 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008669 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008670 }
NeilBrownac05f252014-09-30 08:10:42 +10008671 unlock:
8672 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008673 mddev_unlock(mddev);
8674 }
8675}
NeilBrown6c144d32014-09-30 16:15:38 +10008676EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008678void md_reap_sync_thread(struct mddev *mddev)
8679{
8680 struct md_rdev *rdev;
8681
8682 /* resync has finished, collect result */
8683 md_unregister_thread(&mddev->sync_thread);
8684 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8685 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8686 /* success...*/
8687 /* activate any spares */
8688 if (mddev->pers->spare_active(mddev)) {
8689 sysfs_notify(&mddev->kobj, NULL,
8690 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08008691 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008692 }
8693 }
8694 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8695 mddev->pers->finish_reshape)
8696 mddev->pers->finish_reshape(mddev);
8697
8698 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008699 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008700 */
NeilBrownf4667222013-12-09 12:04:56 +11008701 if (!mddev->degraded)
8702 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008703 rdev->saved_raid_disk = -1;
8704
8705 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08008706 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008707 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8708 * clustered raid */
8709 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8710 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008711 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008712 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008713 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8714 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8715 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8716 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008717 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008718 /* flag recovery needed just to double check */
8719 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8720 sysfs_notify_dirent_safe(mddev->sysfs_action);
8721 md_new_event(mddev);
8722 if (mddev->event_work.func)
8723 queue_work(md_misc_wq, &mddev->event_work);
8724}
NeilBrown6c144d32014-09-30 16:15:38 +10008725EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008726
NeilBrownfd01b882011-10-11 16:47:53 +11008727void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008728{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008729 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008730 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008731 !test_bit(Blocked, &rdev->flags) &&
8732 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008733 msecs_to_jiffies(5000));
8734 rdev_dec_pending(rdev, mddev);
8735}
8736EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8737
NeilBrownc6563a82012-05-21 09:27:00 +10008738void md_finish_reshape(struct mddev *mddev)
8739{
8740 /* called be personality module when reshape completes. */
8741 struct md_rdev *rdev;
8742
8743 rdev_for_each(rdev, mddev) {
8744 if (rdev->data_offset > rdev->new_data_offset)
8745 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8746 else
8747 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8748 rdev->data_offset = rdev->new_data_offset;
8749 }
8750}
8751EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008752
Vishal Vermafc974ee2015-12-24 19:20:34 -07008753/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10008754
Vishal Vermafc974ee2015-12-24 19:20:34 -07008755/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11008756int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008757 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008758{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008759 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10008760 int rv;
8761 if (is_new)
8762 s += rdev->new_data_offset;
8763 else
8764 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008765 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8766 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008767 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008768 if (test_bit(ExternalBbl, &rdev->flags))
8769 sysfs_notify(&rdev->kobj, NULL,
8770 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008771 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08008772 set_mask_bits(&mddev->sb_flags, 0,
8773 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10008774 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07008775 return 1;
8776 } else
8777 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008778}
8779EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8780
NeilBrownc6563a82012-05-21 09:27:00 +10008781int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8782 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008783{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008784 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10008785 if (is_new)
8786 s += rdev->new_data_offset;
8787 else
8788 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008789 rv = badblocks_clear(&rdev->badblocks, s, sectors);
8790 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
8791 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
8792 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008793}
8794EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8795
Adrian Bunk75c96f82005-05-05 16:16:09 -07008796static int md_notify_reboot(struct notifier_block *this,
8797 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008798{
8799 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008800 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008801 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008802
NeilBrownc744a652012-03-19 12:46:37 +11008803 for_each_mddev(mddev, tmp) {
8804 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008805 if (mddev->pers)
8806 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008807 if (mddev->persistent)
8808 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008809 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008810 }
NeilBrownc744a652012-03-19 12:46:37 +11008811 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008812 }
NeilBrownc744a652012-03-19 12:46:37 +11008813 /*
8814 * certain more exotic SCSI devices are known to be
8815 * volatile wrt too early system reboots. While the
8816 * right place to handle this issue is the given
8817 * driver, we do want to have a safe RAID driver ...
8818 */
8819 if (need_delay)
8820 mdelay(1000*1);
8821
Linus Torvalds1da177e2005-04-16 15:20:36 -07008822 return NOTIFY_DONE;
8823}
8824
Adrian Bunk75c96f82005-05-05 16:16:09 -07008825static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008826 .notifier_call = md_notify_reboot,
8827 .next = NULL,
8828 .priority = INT_MAX, /* before any real devices */
8829};
8830
8831static void md_geninit(void)
8832{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008833 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008834
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008835 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008836}
8837
Adrian Bunk75c96f82005-05-05 16:16:09 -07008838static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008839{
Tejun Heoe804ac72010-10-15 15:36:08 +02008840 int ret = -ENOMEM;
8841
Tejun Heoada609e2011-01-25 14:35:54 +01008842 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008843 if (!md_wq)
8844 goto err_wq;
8845
8846 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8847 if (!md_misc_wq)
8848 goto err_misc_wq;
8849
8850 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8851 goto err_md;
8852
8853 if ((ret = register_blkdev(0, "mdp")) < 0)
8854 goto err_mdp;
8855 mdp_major = ret;
8856
NeilBrownaf5628f2014-07-31 13:54:54 +10008857 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008858 md_probe, NULL, NULL);
8859 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860 md_probe, NULL, NULL);
8861
Linus Torvalds1da177e2005-04-16 15:20:36 -07008862 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008863 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008864
8865 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008867
Tejun Heoe804ac72010-10-15 15:36:08 +02008868err_mdp:
8869 unregister_blkdev(MD_MAJOR, "md");
8870err_md:
8871 destroy_workqueue(md_misc_wq);
8872err_misc_wq:
8873 destroy_workqueue(md_wq);
8874err_wq:
8875 return ret;
8876}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008877
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008878static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008879{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008880 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8881 struct md_rdev *rdev2;
8882 int role, ret;
8883 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008884
Guoqing Jiang818da592017-03-01 16:42:40 +08008885 /*
8886 * If size is changed in another node then we need to
8887 * do resize as well.
8888 */
8889 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
8890 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
8891 if (ret)
8892 pr_info("md-cluster: resize failed\n");
8893 else
8894 bitmap_update_sb(mddev->bitmap);
8895 }
8896
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008897 /* Check for change of roles in the active devices */
8898 rdev_for_each(rdev2, mddev) {
8899 if (test_bit(Faulty, &rdev2->flags))
8900 continue;
8901
8902 /* Check if the roles changed */
8903 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008904
8905 if (test_bit(Candidate, &rdev2->flags)) {
8906 if (role == 0xfffe) {
8907 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
8908 md_kick_rdev_from_array(rdev2);
8909 continue;
8910 }
8911 else
8912 clear_bit(Candidate, &rdev2->flags);
8913 }
8914
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008915 if (role != rdev2->raid_disk) {
8916 /* got activated */
8917 if (rdev2->raid_disk == -1 && role != 0xffff) {
8918 rdev2->saved_raid_disk = role;
8919 ret = remove_and_add_spares(mddev, rdev2);
8920 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11008921 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04008922 /* wakeup mddev->thread here, so array could
8923 * perform resync with the new activated disk */
8924 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8925 md_wakeup_thread(mddev->thread);
8926
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008927 }
8928 /* device faulty
8929 * We just want to do the minimum to mark the disk
8930 * as faulty. The recovery is performed by the
8931 * one who initiated the error.
8932 */
8933 if ((role == 0xfffe) || (role == 0xfffd)) {
8934 md_error(mddev, rdev2);
8935 clear_bit(Blocked, &rdev2->flags);
8936 }
8937 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008938 }
8939
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11008940 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
8941 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008942
8943 /* Finally set the event to be up to date */
8944 mddev->events = le64_to_cpu(sb->events);
8945}
8946
8947static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
8948{
8949 int err;
8950 struct page *swapout = rdev->sb_page;
8951 struct mdp_superblock_1 *sb;
8952
8953 /* Store the sb page of the rdev in the swapout temporary
8954 * variable in case we err in the future
8955 */
8956 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11008957 err = alloc_disk_sb(rdev);
8958 if (err == 0) {
8959 ClearPageUptodate(rdev->sb_page);
8960 rdev->sb_loaded = 0;
8961 err = super_types[mddev->major_version].
8962 load_super(rdev, NULL, mddev->minor_version);
8963 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008964 if (err < 0) {
8965 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
8966 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11008967 if (rdev->sb_page)
8968 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008969 rdev->sb_page = swapout;
8970 rdev->sb_loaded = 1;
8971 return err;
8972 }
8973
8974 sb = page_address(rdev->sb_page);
8975 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
8976 * is not set
8977 */
8978
8979 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
8980 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
8981
8982 /* The other node finished recovery, call spare_active to set
8983 * device In_sync and mddev->degraded
8984 */
8985 if (rdev->recovery_offset == MaxSector &&
8986 !test_bit(In_sync, &rdev->flags) &&
8987 mddev->pers->spare_active(mddev))
8988 sysfs_notify(&mddev->kobj, NULL, "degraded");
8989
8990 put_page(swapout);
8991 return 0;
8992}
8993
8994void md_reload_sb(struct mddev *mddev, int nr)
8995{
8996 struct md_rdev *rdev;
8997 int err;
8998
8999 /* Find the rdev */
9000 rdev_for_each_rcu(rdev, mddev) {
9001 if (rdev->desc_nr == nr)
9002 break;
9003 }
9004
9005 if (!rdev || rdev->desc_nr != nr) {
9006 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9007 return;
9008 }
9009
9010 err = read_rdev(mddev, rdev);
9011 if (err < 0)
9012 return;
9013
9014 check_sb_changes(mddev, rdev);
9015
9016 /* Read all rdev's to update recovery_offset */
9017 rdev_for_each_rcu(rdev, mddev)
9018 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009019}
9020EXPORT_SYMBOL(md_reload_sb);
9021
Linus Torvalds1da177e2005-04-16 15:20:36 -07009022#ifndef MODULE
9023
9024/*
9025 * Searches all registered partitions for autorun RAID arrays
9026 * at boot time.
9027 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009028
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009029static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009030static LIST_HEAD(all_detected_devices);
9031struct detected_devices_node {
9032 struct list_head list;
9033 dev_t dev;
9034};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009035
9036void md_autodetect_dev(dev_t dev)
9037{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009038 struct detected_devices_node *node_detected_dev;
9039
9040 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9041 if (node_detected_dev) {
9042 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009043 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009044 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009045 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009047}
9048
Linus Torvalds1da177e2005-04-16 15:20:36 -07009049static void autostart_arrays(int part)
9050{
NeilBrown3cb03002011-10-11 16:45:26 +11009051 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009052 struct detected_devices_node *node_detected_dev;
9053 dev_t dev;
9054 int i_scanned, i_passed;
9055
9056 i_scanned = 0;
9057 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009058
NeilBrown9d487392016-11-02 14:16:49 +11009059 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009060
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009061 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009062 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9063 i_scanned++;
9064 node_detected_dev = list_entry(all_detected_devices.next,
9065 struct detected_devices_node, list);
9066 list_del(&node_detected_dev->list);
9067 dev = node_detected_dev->dev;
9068 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009069 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009070 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009071 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009072 if (IS_ERR(rdev))
9073 continue;
9074
NeilBrown403df472014-09-30 15:52:29 +10009075 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009076 continue;
NeilBrown403df472014-09-30 15:52:29 +10009077
NeilBrownd0fae182008-03-04 14:29:31 -08009078 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009079 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009080 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009081 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009082 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009083
NeilBrown9d487392016-11-02 14:16:49 +11009084 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009085
9086 autorun_devices(part);
9087}
9088
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009089#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009090
9091static __exit void md_exit(void)
9092{
NeilBrownfd01b882011-10-11 16:47:53 +11009093 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009094 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009095 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009096
NeilBrownaf5628f2014-07-31 13:54:54 +10009097 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009098 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009099
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009100 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009101 unregister_blkdev(mdp_major, "mdp");
9102 unregister_reboot_notifier(&md_notifier);
9103 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009104
9105 /* We cannot unload the modules while some process is
9106 * waiting for us in select() or poll() - wake them up
9107 */
9108 md_unloading = 1;
9109 while (waitqueue_active(&md_event_waiters)) {
9110 /* not safe to leave yet */
9111 wake_up(&md_event_waiters);
9112 msleep(delay);
9113 delay += delay;
9114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009115 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009116
NeilBrown29ac4aa2008-02-06 01:39:58 -08009117 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009118 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009119 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009120 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009121 /*
9122 * for_each_mddev() will call mddev_put() at the end of each
9123 * iteration. As the mddev is now fully clear, this will
9124 * schedule the mddev for destruction by a workqueue, and the
9125 * destroy_workqueue() below will wait for that to complete.
9126 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009127 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009128 destroy_workqueue(md_misc_wq);
9129 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009130}
9131
Dan Williams685784a2007-07-09 11:56:42 -07009132subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009133module_exit(md_exit)
9134
NeilBrownf91de922005-11-08 21:39:36 -08009135static int get_ro(char *buffer, struct kernel_param *kp)
9136{
9137 return sprintf(buffer, "%d", start_readonly);
9138}
9139static int set_ro(const char *val, struct kernel_param *kp)
9140{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009141 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009142}
9143
NeilBrown80ca3a42006-07-10 04:44:18 -07009144module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9145module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009146module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009147
Linus Torvalds1da177e2005-04-16 15:20:36 -07009148MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009149MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009150MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009151MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);