blob: 97afb28c6f511968c69a0b507c2437e836561d0a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
NeilBrownf72ffdd2014-09-30 14:23:59 +10003 Copyright (C) 1998, 1999, 2000 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
NeilBrown9d487392016-11-02 14:16:49 +110033
34 Errors, Warnings, etc.
35 Please use:
36 pr_crit() for error conditions that risk data loss
37 pr_err() for error conditions that are unexpected, like an IO error
38 or internal inconsistency
39 pr_warn() for error conditions that could have been predicated, like
40 adding a device to an array when it has incompatible metadata
41 pr_info() for every interesting, very rare events, like an array starting
42 or stopping, or resync starting or stopping
43 pr_debug() for everything else.
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045*/
46
Ingo Molnar3f07c012017-02-08 18:51:30 +010047#include <linux/sched/signal.h>
NeilBrowna6fb0932005-09-09 16:23:56 -070048#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110049#include <linux/blkdev.h>
Vishal Vermafc974ee2015-12-24 19:20:34 -070050#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040053#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080054#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070055#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080056#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110057#include <linux/hdreg.h>
58#include <linux/proc_fs.h>
59#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040060#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110061#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070062#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110063#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110064#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110065#include <linux/raid/md_p.h>
66#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090067#include <linux/slab.h>
NeilBrown4ad23a972017-03-15 14:05:14 +110068#include <linux/percpu-refcount.h>
69
Shaohua Li504634f2016-11-18 09:44:08 -080070#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110071#include "md.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040072#include "md-bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050073#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110076static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#endif
78
NeilBrown01f96c02011-09-21 15:30:20 +100079/* pers_list is a list of registered personalities protected
80 * by pers_lock.
81 * pers_lock does extra service to protect accesses to
82 * mddev->thread when the mutex cannot be held.
83 */
NeilBrown2604b702006-01-06 00:20:36 -080084static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static DEFINE_SPINLOCK(pers_lock);
86
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050087struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050088EXPORT_SYMBOL(md_cluster_ops);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050089struct module *md_cluster_mod;
90EXPORT_SYMBOL(md_cluster_mod);
91
Bernd Schubert90b08712008-05-23 13:04:38 -070092static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020093static struct workqueue_struct *md_wq;
94static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070095
NeilBrown746d3202013-04-24 11:42:41 +100096static int remove_and_add_spares(struct mddev *mddev,
97 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110098static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
Robert Becker1e509152009-12-14 12:49:58 +1100101 * Default number of read corrections we'll attempt on an rdev
102 * before ejecting it from the array. We divide the read error
103 * count by 2 for every hour elapsed between read errors.
104 */
105#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
106/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
108 * is 1000 KB/sec, so the extra system load does not show up that much.
109 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -0700110 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * subsystem is idle. There is also an 'absolute maximum' reconstruction
112 * speed limit - in case reconstruction slows down your system despite
113 * idle IO detection.
114 *
115 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800116 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118
119static int sysctl_speed_limit_min = 1000;
120static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100121static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800122{
123 return mddev->sync_speed_min ?
124 mddev->sync_speed_min : sysctl_speed_limit_min;
125}
126
NeilBrownfd01b882011-10-11 16:47:53 +1100127static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800128{
129 return mddev->sync_speed_max ?
130 mddev->sync_speed_max : sysctl_speed_limit_max;
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133static struct ctl_table_header *raid_table_header;
134
Joe Perches82592c32013-11-14 15:16:18 +1100135static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .procname = "speed_limit_min",
138 .data = &sysctl_speed_limit_min,
139 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700140 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800141 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 },
143 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .procname = "speed_limit_max",
145 .data = &sysctl_speed_limit_max,
146 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700147 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800148 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800150 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Joe Perches82592c32013-11-14 15:16:18 +1100153static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .procname = "raid",
156 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700157 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .child = raid_table,
159 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800160 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
Joe Perches82592c32013-11-14 15:16:18 +1100163static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 .procname = "dev",
166 .maxlen = 0,
167 .mode = 0555,
168 .child = raid_dir_table,
169 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800170 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700173static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
NeilBrownf91de922005-11-08 21:39:36 -0800175static int start_readonly;
176
NeilBrown78b63502017-04-12 16:26:13 +1000177/*
178 * The original mechanism for creating an md device is to create
179 * a device node in /dev and to open it. This causes races with device-close.
180 * The preferred method is to write to the "new_array" module parameter.
181 * This can avoid races.
182 * Setting create_on_open to false disables the original mechanism
183 * so all the races disappear.
184 */
185static bool create_on_open = true;
186
NeilBrowna167f662010-10-26 18:31:13 +1100187/* bio_clone_mddev
NeilBrown9b10f6a2017-06-18 14:38:59 +1000188 * like bio_clone_bioset, but with a local bio set
NeilBrowna167f662010-10-26 18:31:13 +1100189 */
190
NeilBrowna167f662010-10-26 18:31:13 +1100191struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100192 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100193{
194 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100195
196 if (!mddev || !mddev->bio_set)
197 return bio_alloc(gfp_mask, nr_iovecs);
198
Kent Overstreet395c72a2012-09-06 15:34:55 -0700199 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100200 if (!b)
201 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100202 return b;
203}
204EXPORT_SYMBOL_GPL(bio_alloc_mddev);
205
NeilBrown5a850712017-06-21 09:12:21 +1000206static struct bio *md_bio_alloc_sync(struct mddev *mddev)
207{
Shaohua Li7f053a6a2017-06-23 09:19:49 -0700208 if (!mddev || !mddev->sync_set)
NeilBrown5a850712017-06-21 09:12:21 +1000209 return bio_alloc(GFP_NOIO, 1);
210
211 return bio_alloc_bioset(GFP_NOIO, 1, mddev->sync_set);
212}
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
NeilBrownd7603b72006-01-06 00:20:30 -0800215 * We have a system wide 'event count' that is incremented
216 * on any 'interesting' event, and readers of /proc/mdstat
217 * can use 'poll' or 'select' to find out when the event
218 * count increases.
219 *
220 * Events are:
221 * start array, stop array, error, add device, remove device,
222 * start build, activate spare
223 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800224static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800225static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100226void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800227{
228 atomic_inc(&md_event_count);
229 wake_up(&md_event_waiters);
230}
NeilBrown29269552006-03-27 01:18:10 -0800231EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800232
233/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * Enables to iterate over all existing md arrays
235 * all_mddevs_lock protects this list.
236 */
237static LIST_HEAD(all_mddevs);
238static DEFINE_SPINLOCK(all_mddevs_lock);
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
241 * iterates through all used mddevs in the system.
242 * We take care to grab the all_mddevs_lock whenever navigating
243 * the list, and to always hold a refcount when unlocked.
244 * Any code which breaks out of this loop while own
245 * a reference to the current mddev and must mddev_put it.
246 */
NeilBrownfd01b882011-10-11 16:47:53 +1100247#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000249 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100250 _tmp = all_mddevs.next; \
251 _mddev = NULL;}); \
252 ({ if (_tmp != &all_mddevs) \
253 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100255 if (_mddev) mddev_put(_mddev); \
256 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
257 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100259 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 )
261
NeilBrown409c57f2009-03-31 14:39:39 +1100262/* Rather than calling directly into the personality make_request function,
263 * IO requests come here first so that we can check if the device is
264 * being suspended pending a reconfiguration.
265 * We hold a refcount over the call to ->make_request. By the time that
266 * call has finished, the bio has been linked into some internal structure
267 * and so is visible to ->quiesce(), so we don't need the refcount any more.
268 */
Shaohua Li393debc2017-09-21 10:23:35 -0700269void md_handle_request(struct mddev *mddev, struct bio *bio)
270{
271check_suspended:
272 rcu_read_lock();
273 if (mddev->suspended) {
274 DEFINE_WAIT(__wait);
275 for (;;) {
276 prepare_to_wait(&mddev->sb_wait, &__wait,
277 TASK_UNINTERRUPTIBLE);
278 if (!mddev->suspended)
279 break;
280 rcu_read_unlock();
281 schedule();
282 rcu_read_lock();
283 }
284 finish_wait(&mddev->sb_wait, &__wait);
285 }
286 atomic_inc(&mddev->active_io);
287 rcu_read_unlock();
288
289 if (!mddev->pers->make_request(mddev, bio)) {
290 atomic_dec(&mddev->active_io);
291 wake_up(&mddev->sb_wait);
292 goto check_suspended;
293 }
294
295 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
296 wake_up(&mddev->sb_wait);
297}
298EXPORT_SYMBOL(md_handle_request);
299
Jens Axboedece1632015-11-05 10:41:16 -0700300static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
NeilBrown49077322010-03-25 16:20:56 +1100302 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100303 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500304 unsigned int sectors;
Gu Zheng74672d02015-04-03 08:44:47 +0800305 int cpu;
NeilBrown49077322010-03-25 16:20:56 +1100306
NeilBrownaf67c312017-06-18 14:38:57 +1000307 blk_queue_split(q, &bio);
Kent Overstreet54efd502015-04-23 22:37:18 -0700308
NeilBrown274d8cb2016-01-04 16:16:58 +1100309 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100310 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700311 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100312 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100313 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200314 if (bio_sectors(bio) != 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200315 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200316 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700317 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100318 }
NeilBrown49077322010-03-25 16:20:56 +1100319
Chris Masone91ece52011-02-07 19:21:48 -0500320 /*
321 * save the sectors now since our bio can
322 * go away inside make_request
323 */
324 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700325 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600326 bio->bi_opf &= ~REQ_NOMERGE;
Shaohua Li393debc2017-09-21 10:23:35 -0700327
328 md_handle_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100329
Gu Zheng74672d02015-04-03 08:44:47 +0800330 cpu = part_stat_lock();
331 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
332 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
333 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100334
Jens Axboedece1632015-11-05 10:41:16 -0700335 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
NeilBrown9e35b992010-04-06 14:23:02 +1000338/* mddev_suspend makes sure no new requests are submitted
339 * to the device, and that any requests that have been submitted
340 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100341 * Once mddev_detach() is called and completes, the module will be
342 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000343 */
NeilBrownfd01b882011-10-11 16:47:53 +1100344void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100345{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200346 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100347 if (mddev->suspended++)
348 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100349 synchronize_rcu();
NeilBrowncc27b0c2017-06-05 16:49:39 +1000350 wake_up(&mddev->sb_wait);
NeilBrown409c57f2009-03-31 14:39:39 +1100351 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
352 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500353
354 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100355}
NeilBrown390ee602010-06-01 19:37:27 +1000356EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100357
NeilBrownfd01b882011-10-11 16:47:53 +1100358void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100359{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100360 if (--mddev->suspended)
361 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100362 wake_up(&mddev->sb_wait);
363 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500364
Jonathan Brassow47525e52012-05-22 13:55:29 +1000365 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500366 md_wakeup_thread(mddev->thread);
367 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100368}
NeilBrown390ee602010-06-01 19:37:27 +1000369EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100370
NeilBrownfd01b882011-10-11 16:47:53 +1100371int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000372{
NeilBrown5c675f82014-12-15 12:56:56 +1100373 struct md_personality *pers = mddev->pers;
374 int ret = 0;
375
376 rcu_read_lock();
377 if (mddev->suspended)
378 ret = 1;
379 else if (pers && pers->congested)
380 ret = pers->congested(mddev, bits);
381 rcu_read_unlock();
382 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000383}
NeilBrown5c675f82014-12-15 12:56:56 +1100384EXPORT_SYMBOL_GPL(mddev_congested);
385static int md_congested(void *data, int bits)
386{
387 struct mddev *mddev = data;
388 return mddev_congested(mddev, bits);
389}
NeilBrown3fa841d2009-09-23 18:10:29 +1000390
NeilBrowna2826aa2009-12-14 12:49:49 +1100391/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200392 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100393 */
394
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200395static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100396{
NeilBrown3cb03002011-10-11 16:45:26 +1100397 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100398 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100399
400 rdev_dec_pending(rdev, mddev);
401
402 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200403 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200404 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100405 }
406 bio_put(bio);
407}
408
NeilBrowna7a07e62010-12-09 16:04:25 +1100409static void md_submit_flush_data(struct work_struct *ws);
410
NeilBrowna035fc32010-12-09 16:17:51 +1100411static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100412{
NeilBrownfd01b882011-10-11 16:47:53 +1100413 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100414 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100415
NeilBrowna7a07e62010-12-09 16:04:25 +1100416 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
417 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100418 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100419 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100420 if (rdev->raid_disk >= 0 &&
421 !test_bit(Faulty, &rdev->flags)) {
422 /* Take two references, one is dropped
423 * when request finishes, one after
424 * we reclaim rcu_read_lock
425 */
426 struct bio *bi;
427 atomic_inc(&rdev->nr_pending);
428 atomic_inc(&rdev->nr_pending);
429 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000430 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200431 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100432 bi->bi_private = rdev;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200433 bio_set_dev(bi, rdev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600434 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
NeilBrowna2826aa2009-12-14 12:49:49 +1100435 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500436 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100437 rcu_read_lock();
438 rdev_dec_pending(rdev, mddev);
439 }
440 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100441 if (atomic_dec_and_test(&mddev->flush_pending))
442 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100443}
444
Tejun Heoe9c74692010-09-03 11:56:18 +0200445static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100446{
NeilBrownfd01b882011-10-11 16:47:53 +1100447 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200448 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100449
Shaohua Li79bf31a2017-09-21 09:55:28 -0700450 /*
451 * must reset flush_bio before calling into md_handle_request to avoid a
452 * deadlock, because other bios passed md_handle_request suspend check
453 * could wait for this and below md_handle_request could wait for those
454 * bios because of suspend check
455 */
456 mddev->flush_bio = NULL;
457 wake_up(&mddev->sb_wait);
458
Kent Overstreet4f024f32013-10-11 15:44:27 -0700459 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100460 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200461 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100462 else {
Jens Axboe1eff9d32016-08-05 15:35:16 -0600463 bio->bi_opf &= ~REQ_PREFLUSH;
Shaohua Li79bf31a2017-09-21 09:55:28 -0700464 md_handle_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100465 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100466}
467
NeilBrownfd01b882011-10-11 16:47:53 +1100468void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100469{
NeilBrown85572d72014-12-15 12:56:56 +1100470 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100471 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200472 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100473 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200474 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100475 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100476
NeilBrowna035fc32010-12-09 16:17:51 +1100477 INIT_WORK(&mddev->flush_work, submit_flushes);
478 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100479}
Tejun Heoe9c74692010-09-03 11:56:18 +0200480EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100481
NeilBrownfd01b882011-10-11 16:47:53 +1100482static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 atomic_inc(&mddev->active);
485 return mddev;
486}
487
Dan Williams5fd3a172009-03-04 00:57:25 -0700488static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100489
NeilBrownfd01b882011-10-11 16:47:53 +1100490static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Shaohua Li7184ef82017-07-03 14:34:23 -0700492 struct bio_set *bs = NULL, *sync_bs = NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
495 return;
NeilBrownd3374822009-01-09 08:31:10 +1100496 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100497 mddev->ctime == 0 && !mddev->hold_active) {
498 /* Array is not configured at all, and not held active,
499 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100500 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100501 bs = mddev->bio_set;
Shaohua Li7184ef82017-07-03 14:34:23 -0700502 sync_bs = mddev->sync_set;
NeilBrowna167f662010-10-26 18:31:13 +1100503 mddev->bio_set = NULL;
NeilBrown5a850712017-06-21 09:12:21 +1000504 mddev->sync_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100505 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200506 /* We did a probe so need to clean up. Call
507 * queue_work inside the spinlock so that
508 * flush_workqueue() after mddev_find will
509 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100510 */
511 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200512 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100513 } else
514 kfree(mddev);
515 }
516 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100517 if (bs)
518 bioset_free(bs);
Shaohua Li7184ef82017-07-03 14:34:23 -0700519 if (sync_bs)
520 bioset_free(sync_bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Sasha Levin25b2edf2015-07-24 18:19:58 -0400523static void md_safemode_timeout(unsigned long data);
524
NeilBrownfd01b882011-10-11 16:47:53 +1100525void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100526{
527 mutex_init(&mddev->open_mutex);
528 mutex_init(&mddev->reconfig_mutex);
529 mutex_init(&mddev->bitmap_info.mutex);
530 INIT_LIST_HEAD(&mddev->disks);
531 INIT_LIST_HEAD(&mddev->all_mddevs);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400532 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
533 (unsigned long) mddev);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100534 atomic_set(&mddev->active, 1);
535 atomic_set(&mddev->openers, 0);
536 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100537 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100538 atomic_set(&mddev->flush_pending, 0);
539 init_waitqueue_head(&mddev->sb_wait);
540 init_waitqueue_head(&mddev->recovery_wait);
541 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000542 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500543 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100544 mddev->resync_min = 0;
545 mddev->resync_max = MaxSector;
546 mddev->level = LEVEL_NONE;
547}
NeilBrown390ee602010-06-01 19:37:27 +1000548EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100549
NeilBrownf72ffdd2014-09-30 14:23:59 +1000550static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
NeilBrownfd01b882011-10-11 16:47:53 +1100552 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
NeilBrown8f5f02c2011-02-16 13:58:51 +1100554 if (unit && MAJOR(unit) != MD_MAJOR)
555 unit &= ~((1<<MdpMinorShift)-1);
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 retry:
558 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
NeilBrownefeb53c2009-01-09 08:31:10 +1100560 if (unit) {
561 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
562 if (mddev->unit == unit) {
563 mddev_get(mddev);
564 spin_unlock(&all_mddevs_lock);
565 kfree(new);
566 return mddev;
567 }
568
569 if (new) {
570 list_add(&new->all_mddevs, &all_mddevs);
571 spin_unlock(&all_mddevs_lock);
572 new->hold_active = UNTIL_IOCTL;
573 return new;
574 }
575 } else if (new) {
576 /* find an unused unit number */
577 static int next_minor = 512;
578 int start = next_minor;
579 int is_free = 0;
580 int dev = 0;
581 while (!is_free) {
582 dev = MKDEV(MD_MAJOR, next_minor);
583 next_minor++;
584 if (next_minor > MINORMASK)
585 next_minor = 0;
586 if (next_minor == start) {
587 /* Oh dear, all in use. */
588 spin_unlock(&all_mddevs_lock);
589 kfree(new);
590 return NULL;
591 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000592
NeilBrownefeb53c2009-01-09 08:31:10 +1100593 is_free = 1;
594 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
595 if (mddev->unit == dev) {
596 is_free = 0;
597 break;
598 }
599 }
600 new->unit = dev;
601 new->md_minor = MINOR(dev);
602 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 list_add(&new->all_mddevs, &all_mddevs);
604 spin_unlock(&all_mddevs_lock);
605 return new;
606 }
607 spin_unlock(&all_mddevs_lock);
608
NeilBrown9ffae0c2006-01-06 00:20:32 -0800609 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (!new)
611 return NULL;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 new->unit = unit;
614 if (MAJOR(unit) == MD_MAJOR)
615 new->md_minor = MINOR(unit);
616 else
617 new->md_minor = MINOR(unit) >> MdpMinorShift;
618
NeilBrownfafd7fb2010-04-01 15:55:30 +1100619 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 goto retry;
622}
623
NeilBrownb6eb1272010-04-15 10:13:47 +1000624static struct attribute_group md_redundancy_group;
625
NeilBrown5c47daf2014-12-15 12:57:01 +1100626void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
NeilBrowna64c8762010-04-14 17:15:37 +1000628 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000629 /* These cannot be removed under reconfig_mutex as
630 * an access to the files will try to take reconfig_mutex
631 * while holding the file unremovable, which leads to
632 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000633 * So hold set sysfs_active while the remove in happeing,
634 * and anything else which might set ->to_remove or my
635 * otherwise change the sysfs namespace will fail with
636 * -EBUSY if sysfs_active is still set.
637 * We set sysfs_active under reconfig_mutex and elsewhere
638 * test it under the same mutex to ensure its correct value
639 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000640 */
NeilBrowna64c8762010-04-14 17:15:37 +1000641 struct attribute_group *to_remove = mddev->to_remove;
642 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000643 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000644 mutex_unlock(&mddev->reconfig_mutex);
645
NeilBrown00bcb4a2010-06-01 19:37:23 +1000646 if (mddev->kobj.sd) {
647 if (to_remove != &md_redundancy_group)
648 sysfs_remove_group(&mddev->kobj, to_remove);
649 if (mddev->pers == NULL ||
650 mddev->pers->sync_request == NULL) {
651 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
652 if (mddev->sysfs_action)
653 sysfs_put(mddev->sysfs_action);
654 mddev->sysfs_action = NULL;
655 }
NeilBrowna64c8762010-04-14 17:15:37 +1000656 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000657 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000658 } else
659 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Chris Dunlop751e67c2011-10-19 16:48:26 +1100661 /* As we've dropped the mutex we need a spinlock to
662 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000663 */
664 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700665 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000666 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
NeilBrown5c47daf2014-12-15 12:57:01 +1100668EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500670struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100671{
672 struct md_rdev *rdev;
673
674 rdev_for_each_rcu(rdev, mddev)
675 if (rdev->desc_nr == nr)
676 return rdev;
677
678 return NULL;
679}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500680EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100681
682static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
NeilBrown3cb03002011-10-11 16:45:26 +1100684 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
NeilBrowndafb20f2012-03-19 12:46:39 +1100686 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (rdev->bdev->bd_dev == dev)
688 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return NULL;
691}
692
NeilBrown1ca69c42012-10-11 13:37:33 +1100693static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
694{
695 struct md_rdev *rdev;
696
697 rdev_for_each_rcu(rdev, mddev)
698 if (rdev->bdev->bd_dev == dev)
699 return rdev;
700
701 return NULL;
702}
703
NeilBrown84fc4b52011-10-11 16:49:58 +1100704static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800705{
NeilBrown84fc4b52011-10-11 16:49:58 +1100706 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800707 list_for_each_entry(pers, &pers_list, list) {
708 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800709 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800710 if (strcmp(pers->name, clevel)==0)
711 return pers;
712 }
NeilBrown2604b702006-01-06 00:20:36 -0800713 return NULL;
714}
715
Andre Nollb73df2d2008-07-11 22:02:23 +1000716/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100717static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100719 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000720 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
NeilBrownf72ffdd2014-09-30 14:23:59 +1000723static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100726 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000727 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return 0;
729}
730
NeilBrown545c8792012-05-22 13:54:30 +1000731void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800734 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 rdev->sb_loaded = 0;
736 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000737 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100738 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
NeilBrown2699b672011-07-28 11:31:47 +1000740 if (rdev->bb_page) {
741 put_page(rdev->bb_page);
742 rdev->bb_page = NULL;
743 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800744 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
NeilBrown545c8792012-05-22 13:54:30 +1000746EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200748static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700749{
NeilBrown3cb03002011-10-11 16:45:26 +1100750 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100751 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700752
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200753 if (bio->bi_status) {
754 pr_err("md: super_written gets error=%d\n", bio->bi_status);
NeilBrowna9701a32005-11-08 21:39:34 -0800755 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100756 if (!test_bit(Faulty, &rdev->flags)
757 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800758 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100759 set_bit(LastDev, &rdev->flags);
760 }
761 } else
762 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700763
NeilBrowna9701a32005-11-08 21:39:34 -0800764 if (atomic_dec_and_test(&mddev->pending_writes))
765 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700766 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700767 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700768}
769
NeilBrownfd01b882011-10-11 16:47:53 +1100770void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700771 sector_t sector, int size, struct page *page)
772{
773 /* write first size bytes of page to sector of rdev
774 * Increment mddev->pending_writes before returning
775 * and decrement it on completion, waking up sb_wait
776 * if zero is reached.
777 * If an error occurred, call md_error
778 */
NeilBrown46533ff2016-11-18 16:16:11 +1100779 struct bio *bio;
780 int ff = 0;
781
782 if (test_bit(Faulty, &rdev->flags))
783 return;
784
NeilBrown5a850712017-06-21 09:12:21 +1000785 bio = md_bio_alloc_sync(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700786
Shaohua Lied3b98c2016-03-29 14:00:19 -0700787 atomic_inc(&rdev->nr_pending);
788
Christoph Hellwig74d46992017-08-23 19:10:32 +0200789 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700790 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700791 bio_add_page(bio, page, size, 0);
792 bio->bi_private = rdev;
793 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100794
795 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
796 test_bit(FailFast, &rdev->flags) &&
797 !test_bit(LastDev, &rdev->flags))
798 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +0200799 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -0800800
NeilBrown7bfa19f2005-06-21 17:17:28 -0700801 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500802 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800803}
804
NeilBrown46533ff2016-11-18 16:16:11 +1100805int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800806{
Tejun Heoe9c74692010-09-03 11:56:18 +0200807 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000808 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -0800809 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +1100810 return -EAGAIN;
811 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700812}
813
NeilBrown3cb03002011-10-11 16:45:26 +1100814int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -0500815 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
NeilBrown5a850712017-06-21 09:12:21 +1000817 struct bio *bio = md_bio_alloc_sync(rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 int ret;
819
Christoph Hellwig74d46992017-08-23 19:10:32 +0200820 if (metadata_op && rdev->meta_bdev)
821 bio_set_dev(bio, rdev->meta_bdev);
822 else
823 bio_set_dev(bio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -0500824 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100825 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700826 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000827 else if (rdev->mddev->reshape_position != MaxSector &&
828 (rdev->mddev->reshape_backwards ==
829 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700830 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100831 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700832 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500834
835 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200837 ret = !bio->bi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 bio_put(bio);
839 return ret;
840}
NeilBrowna8745db2006-01-06 00:20:34 -0800841EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
NeilBrownf72ffdd2014-09-30 14:23:59 +1000843static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (rdev->sb_loaded)
848 return 0;
849
Mike Christie796a5cf2016-06-05 14:32:07 -0500850 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 goto fail;
852 rdev->sb_loaded = 1;
853 return 0;
854
855fail:
NeilBrown9d487392016-11-02 14:16:49 +1100856 pr_err("md: disabled device %s, could not read superblock.\n",
857 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -EINVAL;
859}
860
Amir Goldsteine6fd2092017-05-04 16:26:20 +0300861static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000863 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000864 sb1->set_uuid1 == sb2->set_uuid1 &&
865 sb1->set_uuid2 == sb2->set_uuid2 &&
866 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Amir Goldsteine6fd2092017-05-04 16:26:20 +0300869static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 int ret;
872 mdp_super_t *tmp1, *tmp2;
873
874 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
875 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
876
877 if (!tmp1 || !tmp2) {
878 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 goto abort;
880 }
881
882 *tmp1 = *sb1;
883 *tmp2 = *sb2;
884
885 /*
886 * nr_disks is not constant
887 */
888 tmp1->nr_disks = 0;
889 tmp2->nr_disks = 0;
890
Andre Nollce0c8e02008-07-11 22:02:20 +1000891 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700893 kfree(tmp1);
894 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return ret;
896}
897
NeilBrown4d167f02007-05-09 02:35:37 -0700898static u32 md_csum_fold(u32 csum)
899{
900 csum = (csum & 0xffff) + (csum >> 16);
901 return (csum & 0xffff) + (csum >> 16);
902}
903
NeilBrownf72ffdd2014-09-30 14:23:59 +1000904static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
NeilBrown4d167f02007-05-09 02:35:37 -0700906 u64 newcsum = 0;
907 u32 *sb32 = (u32*)sb;
908 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 unsigned int disk_csum, csum;
910
911 disk_csum = sb->sb_csum;
912 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700913
914 for (i = 0; i < MD_SB_BYTES/4 ; i++)
915 newcsum += sb32[i];
916 csum = (newcsum & 0xffffffff) + (newcsum>>32);
917
NeilBrown4d167f02007-05-09 02:35:37 -0700918#ifdef CONFIG_ALPHA
919 /* This used to use csum_partial, which was wrong for several
920 * reasons including that different results are returned on
921 * different architectures. It isn't critical that we get exactly
922 * the same return value as before (we always csum_fold before
923 * testing, and that removes any differences). However as we
924 * know that csum_partial always returned a 16bit value on
925 * alphas, do a fold to maximise conformity to previous behaviour.
926 */
927 sb->sb_csum = md_csum_fold(disk_csum);
928#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700930#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return csum;
932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934/*
935 * Handle superblock details.
936 * We want to be able to handle multiple superblock formats
937 * so we have a common interface to them all, and an array of
938 * different handlers.
939 * We rely on user-space to write the initial superblock, and support
940 * reading and updating of superblocks.
941 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100942 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * loads and validates a superblock on dev.
944 * if refdev != NULL, compare superblocks on both devices
945 * Return:
946 * 0 - dev has a superblock that is compatible with refdev
947 * 1 - dev has a superblock that is compatible and newer than refdev
948 * so dev should be used as the refdev in future
949 * -EINVAL superblock incompatible or invalid
950 * -othererror e.g. -EIO
951 *
NeilBrownfd01b882011-10-11 16:47:53 +1100952 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 * Verify that dev is acceptable into mddev.
954 * The first time, mddev->raid_disks will be 0, and data from
955 * dev should be merged in. Subsequent calls check that dev
956 * is new enough. Return 0 or -EINVAL
957 *
NeilBrownfd01b882011-10-11 16:47:53 +1100958 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * Update the superblock for rdev with data in mddev
960 * This does not write to disc.
961 *
962 */
963
964struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000965 char *name;
966 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000967 int (*load_super)(struct md_rdev *rdev,
968 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000969 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000970 int (*validate_super)(struct mddev *mddev,
971 struct md_rdev *rdev);
972 void (*sync_super)(struct mddev *mddev,
973 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100974 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000975 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000976 int (*allow_new_offset)(struct md_rdev *rdev,
977 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978};
979
980/*
Andre Noll0894cc32009-06-18 08:49:23 +1000981 * Check that the given mddev has no bitmap.
982 *
983 * This function is called from the run method of all personalities that do not
984 * support bitmaps. It prints an error message and returns non-zero if mddev
985 * has a bitmap. Otherwise, it returns 0.
986 *
987 */
NeilBrownfd01b882011-10-11 16:47:53 +1100988int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000989{
NeilBrownc3d97142009-12-14 12:49:52 +1100990 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000991 return 0;
NeilBrown9d487392016-11-02 14:16:49 +1100992 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +1000993 mdname(mddev), mddev->pers->name);
994 return 1;
995}
996EXPORT_SYMBOL(md_check_no_bitmap);
997
998/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000999 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
NeilBrown3cb03002011-10-11 16:45:26 +11001001static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1004 mdp_super_t *sb;
1005 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 /*
Andre Noll0f420352008-07-11 22:02:23 +10001008 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 * it's at the end of the disk.
1010 *
1011 * It also happens to be a multiple of 4Kb.
1012 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001013 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
NeilBrown0002b272005-09-09 16:23:53 -07001015 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +11001016 if (ret)
1017 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 ret = -EINVAL;
1020
1021 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001022 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +11001025 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 goto abort;
1027 }
1028
1029 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001030 sb->minor_version < 90 ||
1031 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +11001032 pr_warn("Bad version number %d.%d on %s\n",
1033 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 goto abort;
1035 }
1036
1037 if (sb->raid_disks <= 0)
1038 goto abort;
1039
NeilBrown4d167f02007-05-09 02:35:37 -07001040 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001041 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 goto abort;
1043 }
1044
1045 rdev->preferred_minor = sb->md_minor;
1046 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001047 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001048 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001049 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 if (sb->level == LEVEL_MULTIPATH)
1052 rdev->desc_nr = -1;
1053 else
1054 rdev->desc_nr = sb->this_disk.number;
1055
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001056 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001058 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001060 mdp_super_t *refsb = page_address(refdev->sb_page);
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001061 if (!md_uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001062 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 b, bdevname(refdev->bdev,b2));
1064 goto abort;
1065 }
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001066 if (!md_sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001067 pr_warn("md: %s has same UUID but different superblock to %s\n",
1068 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 goto abort;
1070 }
1071 ev1 = md_event(sb);
1072 ev2 = md_event(refsb);
1073 if (ev1 > ev2)
1074 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001075 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 ret = 0;
1077 }
NeilBrown8190e752009-06-18 08:48:58 +10001078 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001079 /* Limit to 4TB as metadata cannot record more than that.
1080 * (not needed for Linear and RAID0 as metadata doesn't
1081 * record this size)
1082 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001083 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1084 sb->level >= 1)
1085 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
NeilBrown27a7b262011-09-10 17:21:28 +10001087 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001088 /* "this cannot possibly happen" ... */
1089 ret = -EINVAL;
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 abort:
1092 return ret;
1093}
1094
1095/*
1096 * validate_super for 0.90.0
1097 */
NeilBrownfd01b882011-10-11 16:47:53 +11001098static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001101 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001102 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
NeilBrown41158c72005-06-21 17:17:25 -07001104 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001105 clear_bit(Faulty, &rdev->flags);
1106 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001107 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001108 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (mddev->raid_disks == 0) {
1111 mddev->major_version = 0;
1112 mddev->minor_version = sb->minor_version;
1113 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001114 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001115 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 mddev->ctime = sb->ctime;
1117 mddev->utime = sb->utime;
1118 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001119 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 mddev->layout = sb->layout;
1121 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001122 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001123 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001124 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001125 mddev->bitmap_info.space = 0;
1126 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001127 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001128 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001129 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
NeilBrownf6705572006-03-27 01:18:11 -08001131 if (mddev->minor_version >= 91) {
1132 mddev->reshape_position = sb->reshape_position;
1133 mddev->delta_disks = sb->delta_disks;
1134 mddev->new_level = sb->new_level;
1135 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001136 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001137 if (mddev->delta_disks < 0)
1138 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001139 } else {
1140 mddev->reshape_position = MaxSector;
1141 mddev->delta_disks = 0;
1142 mddev->new_level = mddev->level;
1143 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001144 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001145 }
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (sb->state & (1<<MD_SB_CLEAN))
1148 mddev->recovery_cp = MaxSector;
1149 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001150 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 sb->events_lo == sb->cp_events_lo) {
1152 mddev->recovery_cp = sb->recovery_cp;
1153 } else
1154 mddev->recovery_cp = 0;
1155 }
1156
1157 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1158 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1159 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1160 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1161
1162 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001163
1164 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001165 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001166 mddev->bitmap_info.offset =
1167 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001168 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001169 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001170 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001171
NeilBrown41158c72005-06-21 17:17:25 -07001172 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001173 /* Insist on good event counter while assembling, except
1174 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001176 if (sb->disks[rdev->desc_nr].state & (
1177 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001178 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001179 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001180 } else if (mddev->bitmap) {
1181 /* if adding to array with a bitmap, then we can accept an
1182 * older device ... but not too old.
1183 */
NeilBrown41158c72005-06-21 17:17:25 -07001184 if (ev1 < mddev->bitmap->events_cleared)
1185 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001186 if (ev1 < mddev->events)
1187 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001188 } else {
1189 if (ev1 < mddev->events)
1190 /* just a hot-add of a new device, leave raid_disk at -1 */
1191 return 0;
1192 }
NeilBrown41158c72005-06-21 17:17:25 -07001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 desc = sb->disks + rdev->desc_nr;
1196
1197 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001198 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001199 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1200 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001201 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001203 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001204 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1205 /* active but not in sync implies recovery up to
1206 * reshape position. We don't know exactly where
1207 * that is, so set to zero for now */
1208 if (mddev->minor_version >= 91) {
1209 rdev->recovery_offset = 0;
1210 rdev->raid_disk = desc->raid_disk;
1211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001213 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1214 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001215 if (desc->state & (1<<MD_DISK_FAILFAST))
1216 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001217 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001218 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return 0;
1220}
1221
1222/*
1223 * sync_super for 0.90.0
1224 */
NeilBrownfd01b882011-10-11 16:47:53 +11001225static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001228 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* make rdev->sb match mddev data..
1232 *
1233 * 1/ zero out disks
1234 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1235 * 3/ any empty disks < next_spare become removed
1236 *
1237 * disks[0] gets initialised to REMOVED because
1238 * we cannot be sure from other fields if it has
1239 * been initialised or not.
1240 */
1241 int i;
1242 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1243
NeilBrown61181562005-09-09 16:24:02 -07001244 rdev->sb_size = MD_SB_BYTES;
1245
Namhyung Kim65a06f062011-07-27 11:00:36 +10001246 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 memset(sb, 0, sizeof(*sb));
1249
1250 sb->md_magic = MD_SB_MAGIC;
1251 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 sb->patch_version = mddev->patch_version;
1253 sb->gvalid_words = 0; /* ignored */
1254 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1255 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1256 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1257 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1258
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001259 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001261 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 sb->raid_disks = mddev->raid_disks;
1263 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001264 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001265 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 sb->state = 0;
1267 sb->events_hi = (mddev->events>>32);
1268 sb->events_lo = (u32)mddev->events;
1269
NeilBrownf6705572006-03-27 01:18:11 -08001270 if (mddev->reshape_position == MaxSector)
1271 sb->minor_version = 90;
1272 else {
1273 sb->minor_version = 91;
1274 sb->reshape_position = mddev->reshape_position;
1275 sb->new_level = mddev->new_level;
1276 sb->delta_disks = mddev->delta_disks;
1277 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001278 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001279 }
1280 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (mddev->in_sync)
1282 {
1283 sb->recovery_cp = mddev->recovery_cp;
1284 sb->cp_events_hi = (mddev->events>>32);
1285 sb->cp_events_lo = (u32)mddev->events;
1286 if (mddev->recovery_cp == MaxSector)
1287 sb->state = (1<< MD_SB_CLEAN);
1288 } else
1289 sb->recovery_cp = 0;
1290
1291 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001292 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
NeilBrownc3d97142009-12-14 12:49:52 +11001294 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001295 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001298 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001300 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001301 int is_active = test_bit(In_sync, &rdev2->flags);
1302
1303 if (rdev2->raid_disk >= 0 &&
1304 sb->minor_version >= 91)
1305 /* we have nowhere to store the recovery_offset,
1306 * but if it is not below the reshape_position,
1307 * we can piggy-back on that.
1308 */
1309 is_active = 1;
1310 if (rdev2->raid_disk < 0 ||
1311 test_bit(Faulty, &rdev2->flags))
1312 is_active = 0;
1313 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001314 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001316 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001317 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 d = &sb->disks[rdev2->desc_nr];
1319 nr_disks++;
1320 d->number = rdev2->desc_nr;
1321 d->major = MAJOR(rdev2->bdev->bd_dev);
1322 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001323 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 d->raid_disk = rdev2->raid_disk;
1325 else
1326 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001327 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001329 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001331 if (test_bit(In_sync, &rdev2->flags))
1332 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 active++;
1334 working++;
1335 } else {
1336 d->state = 0;
1337 spare++;
1338 working++;
1339 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001340 if (test_bit(WriteMostly, &rdev2->flags))
1341 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001342 if (test_bit(FailFast, &rdev2->flags))
1343 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /* now set the "removed" and "faulty" bits on any missing devices */
1346 for (i=0 ; i < mddev->raid_disks ; i++) {
1347 mdp_disk_t *d = &sb->disks[i];
1348 if (d->state == 0 && d->number == 0) {
1349 d->number = i;
1350 d->raid_disk = i;
1351 d->state = (1<<MD_DISK_REMOVED);
1352 d->state |= (1<<MD_DISK_FAULTY);
1353 failed++;
1354 }
1355 }
1356 sb->nr_disks = nr_disks;
1357 sb->active_disks = active;
1358 sb->working_disks = working;
1359 sb->failed_disks = failed;
1360 sb->spare_disks = spare;
1361
1362 sb->this_disk = sb->disks[rdev->desc_nr];
1363 sb->sb_csum = calc_sb_csum(sb);
1364}
1365
1366/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001367 * rdev_size_change for 0.90.0
1368 */
1369static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001370super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001371{
Andre Noll58c0fed2009-03-31 14:33:13 +11001372 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001373 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001374 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001375 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001376 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001377 if (!num_sectors || num_sectors > rdev->sb_start)
1378 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001379 /* Limit to 4TB as metadata cannot record more than that.
1380 * 4TB == 2^32 KB, or 2*2^32 sectors.
1381 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001382 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1383 rdev->mddev->level >= 1)
1384 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001385 do {
1386 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001387 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001388 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001389 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001390}
1391
NeilBrownc6563a82012-05-21 09:27:00 +10001392static int
1393super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1394{
1395 /* non-zero offset changes not possible with v0.90 */
1396 return new_offset == 0;
1397}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001398
1399/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 * version 1 superblock
1401 */
1402
NeilBrownf72ffdd2014-09-30 14:23:59 +10001403static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001405 __le32 disk_csum;
1406 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 unsigned long long newcsum;
1408 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001409 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 disk_csum = sb->sb_csum;
1412 sb->sb_csum = 0;
1413 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001414 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 newcsum += le32_to_cpu(*isuper++);
1416
1417 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001418 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1421 sb->sb_csum = disk_csum;
1422 return cpu_to_le32(csum);
1423}
1424
NeilBrown3cb03002011-10-11 16:45:26 +11001425static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 struct mdp_superblock_1 *sb;
1428 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001429 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001430 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001432 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 /*
Andre Noll0f420352008-07-11 22:02:23 +10001435 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 * It is always aligned to a 4K boundary and
1437 * depeding on minor_version, it can be:
1438 * 0: At least 8K, but less than 12K, from end of device
1439 * 1: At start of device
1440 * 2: 4K from start of device.
1441 */
1442 switch(minor_version) {
1443 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001444 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001445 sb_start -= 8*2;
1446 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 break;
1448 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001449 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 break;
1451 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001452 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 break;
1454 default:
1455 return -EINVAL;
1456 }
Andre Noll0f420352008-07-11 22:02:23 +10001457 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
NeilBrown0002b272005-09-09 16:23:53 -07001459 /* superblock is rarely larger than 1K, but it can be larger,
1460 * and it is safe to read 4k, so we do that
1461 */
1462 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (ret) return ret;
1464
Namhyung Kim65a06f062011-07-27 11:00:36 +10001465 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1468 sb->major_version != cpu_to_le32(1) ||
1469 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001470 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001471 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return -EINVAL;
1473
1474 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001475 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 bdevname(rdev->bdev,b));
1477 return -EINVAL;
1478 }
1479 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001480 pr_warn("md: data_size too small on %s\n",
1481 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return -EINVAL;
1483 }
NeilBrownc6563a82012-05-21 09:27:00 +10001484 if (sb->pad0 ||
1485 sb->pad3[0] ||
1486 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1487 /* Some padding is non-zero, might be a new feature */
1488 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 rdev->preferred_minor = 0xffff;
1491 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001492 rdev->new_data_offset = rdev->data_offset;
1493 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1494 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1495 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001496 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
NeilBrown0002b272005-09-09 16:23:53 -07001498 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001499 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001500 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001501 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1502
1503 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001504 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001505 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001506 if (minor_version
1507 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1508 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001509
NeilBrown31b65a02006-07-10 04:44:14 -07001510 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1511 rdev->desc_nr = -1;
1512 else
1513 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1514
NeilBrown2699b672011-07-28 11:31:47 +10001515 if (!rdev->bb_page) {
1516 rdev->bb_page = alloc_page(GFP_KERNEL);
1517 if (!rdev->bb_page)
1518 return -ENOMEM;
1519 }
1520 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1521 rdev->badblocks.count == 0) {
1522 /* need to load the bad block list.
1523 * Currently we limit it to one page.
1524 */
1525 s32 offset;
1526 sector_t bb_sector;
1527 u64 *bbp;
1528 int i;
1529 int sectors = le16_to_cpu(sb->bblog_size);
1530 if (sectors > (PAGE_SIZE / 512))
1531 return -EINVAL;
1532 offset = le32_to_cpu(sb->bblog_offset);
1533 if (offset == 0)
1534 return -EINVAL;
1535 bb_sector = (long long)offset;
1536 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001537 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001538 return -EIO;
1539 bbp = (u64 *)page_address(rdev->bb_page);
1540 rdev->badblocks.shift = sb->bblog_shift;
1541 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1542 u64 bb = le64_to_cpu(*bbp);
1543 int count = bb & (0x3ff);
1544 u64 sector = bb >> 10;
1545 sector <<= sb->bblog_shift;
1546 count <<= sb->bblog_shift;
1547 if (bb + 1 == 0)
1548 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001549 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001550 return -EINVAL;
1551 }
NeilBrown486adf72013-04-24 11:42:44 +10001552 } else if (sb->bblog_offset != 0)
1553 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001554
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001555 if ((le32_to_cpu(sb->feature_map) &
1556 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001557 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1558 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1559 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1560 }
1561
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001562 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001563 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001564 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001566 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1569 sb->level != refsb->level ||
1570 sb->layout != refsb->layout ||
1571 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001572 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 bdevname(rdev->bdev,b),
1574 bdevname(refdev->bdev,b2));
1575 return -EINVAL;
1576 }
1577 ev1 = le64_to_cpu(sb->events);
1578 ev2 = le64_to_cpu(refsb->events);
1579
1580 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001581 ret = 1;
1582 else
1583 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
NeilBrownc6563a82012-05-21 09:27:00 +10001585 if (minor_version) {
1586 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1587 sectors -= rdev->data_offset;
1588 } else
1589 sectors = rdev->sb_start;
1590 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001592 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001593 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
NeilBrownfd01b882011-10-11 16:47:53 +11001596static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001598 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001599 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
NeilBrown41158c72005-06-21 17:17:25 -07001601 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001602 clear_bit(Faulty, &rdev->flags);
1603 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001604 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001605 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (mddev->raid_disks == 0) {
1608 mddev->major_version = 1;
1609 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001610 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001611 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001612 mddev->ctime = le64_to_cpu(sb->ctime);
1613 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001615 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 mddev->layout = le32_to_cpu(sb->layout);
1617 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001618 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001619 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001620 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001621 mddev->bitmap_info.space = 0;
1622 /* Default location for bitmap is 1K after superblock
1623 * using 3K - total of 4K
1624 */
NeilBrownc3d97142009-12-14 12:49:52 +11001625 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001626 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001627 mddev->reshape_backwards = 0;
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1630 memcpy(mddev->uuid, sb->set_uuid, 16);
1631
1632 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001633
NeilBrown71c08052005-09-09 16:23:51 -07001634 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001635 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001636 mddev->bitmap_info.offset =
1637 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001638 /* Metadata doesn't record how much space is available.
1639 * For 1.0, we assume we can use up to the superblock
1640 * if before, else to 4K beyond superblock.
1641 * For others, assume no change is possible.
1642 */
1643 if (mddev->minor_version > 0)
1644 mddev->bitmap_info.space = 0;
1645 else if (mddev->bitmap_info.offset > 0)
1646 mddev->bitmap_info.space =
1647 8 - mddev->bitmap_info.offset;
1648 else
1649 mddev->bitmap_info.space =
1650 -mddev->bitmap_info.offset;
1651 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001652
NeilBrownf6705572006-03-27 01:18:11 -08001653 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1654 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1655 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1656 mddev->new_level = le32_to_cpu(sb->new_level);
1657 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001658 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001659 if (mddev->delta_disks < 0 ||
1660 (mddev->delta_disks == 0 &&
1661 (le32_to_cpu(sb->feature_map)
1662 & MD_FEATURE_RESHAPE_BACKWARDS)))
1663 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001664 } else {
1665 mddev->reshape_position = MaxSector;
1666 mddev->delta_disks = 0;
1667 mddev->new_level = mddev->level;
1668 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001669 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001670 }
1671
Song Liu486b0f72016-08-19 15:34:01 -07001672 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001673 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001674
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001675 if (le32_to_cpu(sb->feature_map) &
1676 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001677 if (le32_to_cpu(sb->feature_map) &
1678 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1679 return -EINVAL;
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001680 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1681 (le32_to_cpu(sb->feature_map) &
1682 MD_FEATURE_MULTIPLE_PPLS))
1683 return -EINVAL;
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001684 set_bit(MD_HAS_PPL, &mddev->flags);
1685 }
NeilBrown41158c72005-06-21 17:17:25 -07001686 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001687 /* Insist of good event counter while assembling, except for
1688 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001690 if (rdev->desc_nr >= 0 &&
1691 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001692 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1693 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001694 if (ev1 < mddev->events)
1695 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001696 } else if (mddev->bitmap) {
1697 /* If adding to array with a bitmap, then we can accept an
1698 * older device, but not too old.
1699 */
NeilBrown41158c72005-06-21 17:17:25 -07001700 if (ev1 < mddev->bitmap->events_cleared)
1701 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001702 if (ev1 < mddev->events)
1703 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001704 } else {
1705 if (ev1 < mddev->events)
1706 /* just a hot-add of a new device, leave raid_disk at -1 */
1707 return 0;
1708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if (mddev->level != LEVEL_MULTIPATH) {
1710 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001711 if (rdev->desc_nr < 0 ||
1712 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001713 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001714 rdev->desc_nr = -1;
1715 } else
1716 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001718 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001720 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001721 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 break;
Song Liubac624f2015-08-13 14:31:55 -07001723 case MD_DISK_ROLE_JOURNAL: /* journal device */
1724 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1725 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001726 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001727 return -EINVAL;
1728 }
1729 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001730 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001731 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001732 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 default:
NeilBrownf4667222013-12-09 12:04:56 +11001734 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001735 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001736 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001737 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001738 if (!(le32_to_cpu(sb->feature_map) &
1739 MD_FEATURE_RECOVERY_BITMAP))
1740 rdev->saved_raid_disk = -1;
1741 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001742 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 rdev->raid_disk = role;
1744 break;
1745 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001746 if (sb->devflags & WriteMostly1)
1747 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001748 if (sb->devflags & FailFast1)
1749 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001750 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1751 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001752 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001753 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return 0;
1756}
1757
NeilBrownfd01b882011-10-11 16:47:53 +11001758static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
1760 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001761 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 int max_dev, i;
1763 /* make rdev->sb match mddev and rdev data. */
1764
Namhyung Kim65a06f062011-07-27 11:00:36 +10001765 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 sb->feature_map = 0;
1768 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001769 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 memset(sb->pad3, 0, sizeof(sb->pad3));
1771
1772 sb->utime = cpu_to_le64((__u64)mddev->utime);
1773 sb->events = cpu_to_le64(mddev->events);
1774 if (mddev->in_sync)
1775 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001776 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1777 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 else
1779 sb->resync_offset = cpu_to_le64(0);
1780
NeilBrown1c05b4b2006-10-21 10:24:08 -07001781 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001782
NeilBrownf0ca3402006-02-02 14:28:04 -08001783 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001784 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001785 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001786 sb->level = cpu_to_le32(mddev->level);
1787 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11001788 if (test_bit(FailFast, &rdev->flags))
1789 sb->devflags |= FailFast1;
1790 else
1791 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08001792
NeilBrownaeb9b2112011-08-25 14:43:08 +10001793 if (test_bit(WriteMostly, &rdev->flags))
1794 sb->devflags |= WriteMostly1;
1795 else
1796 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001797 sb->data_offset = cpu_to_le64(rdev->data_offset);
1798 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001799
NeilBrownc3d97142009-12-14 12:49:52 +11001800 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1801 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001802 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001803 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001804
Shaohua Lif2076e72015-10-08 21:54:12 -07001805 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001806 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001807 sb->feature_map |=
1808 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1809 sb->recovery_offset =
1810 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001811 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1812 sb->feature_map |=
1813 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001814 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001815 /* Note: recovery_offset and journal_tail share space */
1816 if (test_bit(Journal, &rdev->flags))
1817 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001818 if (test_bit(Replacement, &rdev->flags))
1819 sb->feature_map |=
1820 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001821
NeilBrownf6705572006-03-27 01:18:11 -08001822 if (mddev->reshape_position != MaxSector) {
1823 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1824 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1825 sb->new_layout = cpu_to_le32(mddev->new_layout);
1826 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1827 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001828 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001829 if (mddev->delta_disks == 0 &&
1830 mddev->reshape_backwards)
1831 sb->feature_map
1832 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001833 if (rdev->new_data_offset != rdev->data_offset) {
1834 sb->feature_map
1835 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1836 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1837 - rdev->data_offset));
1838 }
NeilBrownf6705572006-03-27 01:18:11 -08001839 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001840
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001841 if (mddev_is_clustered(mddev))
1842 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1843
NeilBrown2699b672011-07-28 11:31:47 +10001844 if (rdev->badblocks.count == 0)
1845 /* Nothing to do for bad blocks*/ ;
1846 else if (sb->bblog_offset == 0)
1847 /* Cannot record bad blocks on this device */
1848 md_error(mddev, rdev);
1849 else {
1850 struct badblocks *bb = &rdev->badblocks;
1851 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1852 u64 *p = bb->page;
1853 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1854 if (bb->changed) {
1855 unsigned seq;
1856
1857retry:
1858 seq = read_seqbegin(&bb->lock);
1859
1860 memset(bbp, 0xff, PAGE_SIZE);
1861
1862 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001863 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001864 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1865 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001866 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001867 }
NeilBrownd0962932012-03-19 12:46:41 +11001868 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001869 if (read_seqretry(&bb->lock, seq))
1870 goto retry;
1871
1872 bb->sector = (rdev->sb_start +
1873 (int)le32_to_cpu(sb->bblog_offset));
1874 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001875 }
1876 }
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001879 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (rdev2->desc_nr+1 > max_dev)
1881 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001882
NeilBrown70471da2009-08-03 10:59:57 +10001883 if (max_dev > le32_to_cpu(sb->max_dev)) {
1884 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001885 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001886 rdev->sb_size = max_dev * 2 + 256;
1887 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1888 if (rdev->sb_size & bmask)
1889 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001890 } else
1891 max_dev = le32_to_cpu(sb->max_dev);
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 for (i=0; i<max_dev;i++)
Lidong Zhong8df72022017-06-12 10:45:55 +08001894 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001895
Song Liua97b7892015-10-08 21:54:09 -07001896 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1897 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001899 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001900 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
1901 sb->feature_map |=
1902 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
1903 else
1904 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001905 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
1906 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
1907 }
1908
NeilBrowndafb20f2012-03-19 12:46:39 +11001909 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001911 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001912 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001913 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001915 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001916 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001917 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001918 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 else
Song Liuc4d4c912015-08-13 14:31:54 -07001920 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 sb->sb_csum = calc_sb_1_csum(sb);
1924}
1925
Chris Webb0cd17fe2008-06-28 08:31:46 +10001926static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001927super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001928{
1929 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001930 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001931 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001932 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001933 if (rdev->data_offset != rdev->new_data_offset)
1934 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001935 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001936 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001937 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001938 max_sectors -= rdev->data_offset;
1939 if (!num_sectors || num_sectors > max_sectors)
1940 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001941 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001942 /* minor version 0 with bitmap we can't move */
1943 return 0;
1944 } else {
1945 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001946 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001947 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001948 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001949 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001950 if (!num_sectors || num_sectors > max_sectors)
1951 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001952 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001953 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001954 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001955 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08001956 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10001957 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11001958 do {
1959 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1960 rdev->sb_page);
1961 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001962 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001963
1964}
1965
1966static int
1967super_1_allow_new_offset(struct md_rdev *rdev,
1968 unsigned long long new_offset)
1969{
1970 /* All necessary checks on new >= old have been done */
1971 struct bitmap *bitmap;
1972 if (new_offset >= rdev->data_offset)
1973 return 1;
1974
1975 /* with 1.0 metadata, there is no metadata to tread on
1976 * so we can always move back */
1977 if (rdev->mddev->minor_version == 0)
1978 return 1;
1979
1980 /* otherwise we must be sure not to step on
1981 * any metadata, so stay:
1982 * 36K beyond start of superblock
1983 * beyond end of badblocks
1984 * beyond write-intent bitmap
1985 */
1986 if (rdev->sb_start + (32+4)*2 > new_offset)
1987 return 0;
1988 bitmap = rdev->mddev->bitmap;
1989 if (bitmap && !rdev->mddev->bitmap_info.file &&
1990 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001991 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001992 return 0;
1993 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1994 return 0;
1995
1996 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001997}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Adrian Bunk75c96f82005-05-05 16:16:09 -07001999static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 [0] = {
2001 .name = "0.90.0",
2002 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002003 .load_super = super_90_load,
2004 .validate_super = super_90_validate,
2005 .sync_super = super_90_sync,
2006 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002007 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 },
2009 [1] = {
2010 .name = "md-1",
2011 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002012 .load_super = super_1_load,
2013 .validate_super = super_1_validate,
2014 .sync_super = super_1_sync,
2015 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002016 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 },
2018};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
NeilBrownfd01b882011-10-11 16:47:53 +11002020static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05002021{
2022 if (mddev->sync_super) {
2023 mddev->sync_super(mddev, rdev);
2024 return;
2025 }
2026
2027 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2028
2029 super_types[mddev->major_version].sync_super(mddev, rdev);
2030}
2031
NeilBrownfd01b882011-10-11 16:47:53 +11002032static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
NeilBrown3cb03002011-10-11 16:45:26 +11002034 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
NeilBrown4b809912008-07-21 17:05:25 +10002036 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07002037 rdev_for_each_rcu(rdev, mddev1) {
2038 if (test_bit(Faulty, &rdev->flags) ||
2039 test_bit(Journal, &rdev->flags) ||
2040 rdev->raid_disk == -1)
2041 continue;
2042 rdev_for_each_rcu(rdev2, mddev2) {
2043 if (test_bit(Faulty, &rdev2->flags) ||
2044 test_bit(Journal, &rdev2->flags) ||
2045 rdev2->raid_disk == -1)
2046 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002047 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002048 rdev2->bdev->bd_contains) {
2049 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002050 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002051 }
Song Liu0b020e82015-09-03 23:00:35 -07002052 }
2053 }
NeilBrown4b809912008-07-21 17:05:25 +10002054 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return 0;
2056}
2057
2058static LIST_HEAD(pending_raid_disks);
2059
Andre Nollac5e7112009-08-03 10:59:47 +10002060/*
2061 * Try to register data integrity profile for an mddev
2062 *
2063 * This is called when an array is started and after a disk has been kicked
2064 * from the array. It only succeeds if all working and active component devices
2065 * are integrity capable with matching profiles.
2066 */
NeilBrownfd01b882011-10-11 16:47:53 +11002067int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002068{
NeilBrown3cb03002011-10-11 16:45:26 +11002069 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002070
Andre Nollac5e7112009-08-03 10:59:47 +10002071 if (list_empty(&mddev->disks))
2072 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002073 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2074 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002075 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002076 /* skip spares and non-functional disks */
2077 if (test_bit(Faulty, &rdev->flags))
2078 continue;
2079 if (rdev->raid_disk < 0)
2080 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002081 if (!reference) {
2082 /* Use the first rdev as the reference */
2083 reference = rdev;
2084 continue;
2085 }
2086 /* does this rdev's profile match the reference profile? */
2087 if (blk_integrity_compare(reference->bdev->bd_disk,
2088 rdev->bdev->bd_disk) < 0)
2089 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002090 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002091 if (!reference || !bdev_get_integrity(reference->bdev))
2092 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002093 /*
2094 * All component devices are integrity capable and have matching
2095 * profiles, register the common profile for the md device.
2096 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002097 blk_integrity_register(mddev->gendisk,
2098 bdev_get_integrity(reference->bdev));
2099
NeilBrown9d487392016-11-02 14:16:49 +11002100 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Martin K. Petersena91a2782011-03-17 11:11:05 +01002101 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002102 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002103 mdname(mddev));
2104 return -EINVAL;
2105 }
Andre Nollac5e7112009-08-03 10:59:47 +10002106 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002107}
Andre Nollac5e7112009-08-03 10:59:47 +10002108EXPORT_SYMBOL(md_integrity_register);
2109
Dan Williams1501efa2016-01-13 16:00:07 -08002110/*
2111 * Attempt to add an rdev, but only if it is consistent with the current
2112 * integrity profile
2113 */
2114int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002115{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002116 struct blk_integrity *bi_rdev;
2117 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002118 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002119
2120 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002121 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002122
2123 bi_rdev = bdev_get_integrity(rdev->bdev);
2124 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002125
2126 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002127 return 0;
2128
2129 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002130 pr_err("%s: incompatible integrity profile for %s\n",
2131 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002132 return -ENXIO;
2133 }
2134
2135 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002136}
2137EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002138
NeilBrownf72ffdd2014-09-30 14:23:59 +10002139static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002141 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002142 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002143 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Dan Williams11e2ede2008-04-30 00:52:32 -07002145 /* prevent duplicates */
2146 if (find_rdev(mddev, rdev->bdev->bd_dev))
2147 return -EEXIST;
2148
NeilBrown97b20ef2017-04-13 08:53:48 +10002149 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2150 mddev->pers)
2151 return -EROFS;
2152
Andre Nolldd8ac332009-03-31 14:33:13 +11002153 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002154 if (!test_bit(Journal, &rdev->flags) &&
2155 rdev->sectors &&
2156 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002157 if (mddev->pers) {
2158 /* Cannot change size, so fail
2159 * If mddev->level <= 0, then we don't care
2160 * about aligning sizes (e.g. linear)
2161 */
2162 if (mddev->level > 0)
2163 return -ENOSPC;
2164 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002165 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 /* Verify rdev->desc_nr is unique.
2169 * If it is -1, assign a free number, else
2170 * check number is not in use
2171 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002172 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 if (rdev->desc_nr < 0) {
2174 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002175 if (mddev->pers)
2176 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002177 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 choice++;
2179 rdev->desc_nr = choice;
2180 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002181 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002182 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002186 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002187 if (!test_bit(Journal, &rdev->flags) &&
2188 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002189 pr_warn("md: %s: array is limited to %d devices\n",
2190 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002191 return -EBUSY;
2192 }
NeilBrown19133a42005-11-08 21:39:35 -08002193 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002194 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002197 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002198
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002199 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002200 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002201
Tejun Heo0762b8b2008-08-25 19:56:12 +09002202 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002203 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2204 /* failure here is OK */;
2205 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002206
NeilBrown4b809912008-07-21 17:05:25 +10002207 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002208 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002209
2210 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002211 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002214
2215 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002216 pr_warn("md: failed to register dev-%s for %s\n",
2217 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002218 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219}
2220
NeilBrown177a99b2008-02-06 01:39:56 -08002221static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002222{
NeilBrown3cb03002011-10-11 16:45:26 +11002223 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002224 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002225 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002226}
2227
NeilBrownf72ffdd2014-09-30 14:23:59 +10002228static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
2230 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002231
Tejun Heo49731ba2011-01-14 18:43:57 +01002232 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002233 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002234 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002236 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002237 sysfs_put(rdev->sysfs_state);
2238 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002239 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002240 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002241 * writing to 'remove' to "dev/state". We also need
2242 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002243 */
NeilBrown4b809912008-07-21 17:05:25 +10002244 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002245 INIT_WORK(&rdev->del_work, md_delayed_delete);
2246 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002247 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
2250/*
2251 * prevent the device from being mounted, repartitioned or
2252 * otherwise reused by a RAID array (or any other kernel
2253 * subsystem), by bd_claiming the device.
2254 */
NeilBrown3cb03002011-10-11 16:45:26 +11002255static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 int err = 0;
2258 struct block_device *bdev;
2259 char b[BDEVNAME_SIZE];
2260
Tejun Heod4d77622010-11-13 11:55:18 +01002261 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002262 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002264 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return PTR_ERR(bdev);
2266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 rdev->bdev = bdev;
2268 return err;
2269}
2270
NeilBrown3cb03002011-10-11 16:45:26 +11002271static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
2273 struct block_device *bdev = rdev->bdev;
2274 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002275 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
2278void md_autodetect_dev(dev_t dev);
2279
NeilBrownf72ffdd2014-09-30 14:23:59 +10002280static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
2282 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002283
NeilBrown9d487392016-11-02 14:16:49 +11002284 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002285 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002287 if (test_bit(AutoDetected, &rdev->flags))
2288 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289#endif
2290 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002291 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002294void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
2296 unbind_rdev_from_array(rdev);
2297 export_rdev(rdev);
2298}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002299EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
NeilBrownfd01b882011-10-11 16:47:53 +11002301static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
NeilBrown0638bb02014-09-25 17:43:47 +10002303 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
NeilBrown0638bb02014-09-25 17:43:47 +10002305 while (!list_empty(&mddev->disks)) {
2306 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2307 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002308 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 mddev->raid_disks = 0;
2311 mddev->major_version = 0;
2312}
2313
NeilBrown6497709b2017-03-15 14:05:14 +11002314static bool set_in_sync(struct mddev *mddev)
2315{
Shaohua Lied9b66d2017-07-25 15:18:13 -07002316 WARN_ON_ONCE(NR_CPUS != 1 && !spin_is_locked(&mddev->lock));
NeilBrown4ad23a972017-03-15 14:05:14 +11002317 if (!mddev->in_sync) {
2318 mddev->sync_checkers++;
2319 spin_unlock(&mddev->lock);
2320 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2321 spin_lock(&mddev->lock);
2322 if (!mddev->in_sync &&
2323 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002324 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002325 /*
2326 * Ensure ->in_sync is visible before we clear
2327 * ->sync_checkers.
2328 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002329 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002330 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2331 sysfs_notify_dirent_safe(mddev->sysfs_state);
2332 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002333 if (--mddev->sync_checkers == 0)
2334 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002335 }
2336 if (mddev->safemode == 1)
2337 mddev->safemode = 0;
2338 return mddev->in_sync;
2339}
2340
NeilBrownf72ffdd2014-09-30 14:23:59 +10002341static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
NeilBrown42543762006-06-26 00:27:57 -07002343 /* Update each superblock (in-memory image), but
2344 * if we are allowed to, skip spares which already
2345 * have the right event counter, or have one earlier
2346 * (which would mean they aren't being marked as dirty
2347 * with the rest of the array)
2348 */
NeilBrown3cb03002011-10-11 16:45:26 +11002349 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002350 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002351 if (rdev->sb_events == mddev->events ||
2352 (nospares &&
2353 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002354 rdev->sb_events+1 == mddev->events)) {
2355 /* Don't update this superblock */
2356 rdev->sb_loaded = 2;
2357 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002358 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002359 rdev->sb_loaded = 1;
2360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 }
2362}
2363
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002364static bool does_sb_need_changing(struct mddev *mddev)
2365{
2366 struct md_rdev *rdev;
2367 struct mdp_superblock_1 *sb;
2368 int role;
2369
2370 /* Find a good rdev */
2371 rdev_for_each(rdev, mddev)
2372 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2373 break;
2374
2375 /* No good device found. */
2376 if (!rdev)
2377 return false;
2378
2379 sb = page_address(rdev->sb_page);
2380 /* Check if a device has become faulty or a spare become active */
2381 rdev_for_each(rdev, mddev) {
2382 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2383 /* Device activated? */
2384 if (role == 0xffff && rdev->raid_disk >=0 &&
2385 !test_bit(Faulty, &rdev->flags))
2386 return true;
2387 /* Device turned faulty? */
2388 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2389 return true;
2390 }
2391
2392 /* Check if any mddev parameters have changed */
2393 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2394 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002395 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002396 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2397 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2398 return true;
2399
2400 return false;
2401}
2402
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002403void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
NeilBrown3cb03002011-10-11 16:45:26 +11002405 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002406 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002407 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002408 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002409 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
NeilBrownd87f0642013-04-24 11:42:40 +10002411 if (mddev->ro) {
2412 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002413 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002414 return;
2415 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002416
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002417repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002418 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002419 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002420 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002421 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002422 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002423 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002424 /* Has someone else has updated the sb */
2425 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002426 if (ret == 0)
2427 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002428 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2429 BIT(MD_SB_CHANGE_DEVS) |
2430 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002431 return;
2432 }
2433 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002434
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002435 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002436 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002437 if (rdev->raid_disk >= 0 &&
2438 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002439 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002440 !test_bit(In_sync, &rdev->flags) &&
2441 mddev->curr_resync_completed > rdev->recovery_offset)
2442 rdev->recovery_offset = mddev->curr_resync_completed;
2443
NeilBrownf72ffdd2014-09-30 14:23:59 +10002444 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002445 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002446 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2447 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002448 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002449 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002450 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002451 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002452 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002453 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002454 md_error(mddev, rdev);
2455 }
2456 clear_bit(Blocked, &rdev->flags);
2457 clear_bit(BlockedBadBlocks, &rdev->flags);
2458 wake_up(&rdev->blocked_wait);
2459 }
2460 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002461 wake_up(&mddev->sb_wait);
2462 return;
2463 }
2464
NeilBrown85572d72014-12-15 12:56:56 +11002465 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002466
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002467 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002468
Shaohua Li29530792016-12-08 15:48:19 -08002469 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002470 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002471 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002472 /* just a clean<-> dirty transition, possibly leave spares alone,
2473 * though if events isn't the right even/odd, we will have to do
2474 * spares after all
2475 */
2476 nospares = 1;
2477 if (force_change)
2478 nospares = 0;
2479 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002480 /* If the array is degraded, then skipping spares is both
2481 * dangerous and fairly pointless.
2482 * Dangerous because a device that was removed from the array
2483 * might have a event_count that still looks up-to-date,
2484 * so it can be re-added without a resync.
2485 * Pointless because if there are any spares to skip,
2486 * then a recovery will happen and soon that array won't
2487 * be degraded any more and the spare can go back to sleep then.
2488 */
NeilBrown850b2b422006-10-03 01:15:46 -07002489 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002490
NeilBrown06d91a52005-06-21 17:17:12 -07002491 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002492
2493 /* If this is just a dirty<->clean transition, and the array is clean
2494 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002495 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002496 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002497 && mddev->can_decrease_events
2498 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002499 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002500 mddev->can_decrease_events = 0;
2501 } else {
NeilBrown42543762006-06-26 00:27:57 -07002502 /* otherwise we have to go forward and ... */
2503 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002504 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
NeilBrown403df472014-09-30 15:52:29 +10002507 /*
2508 * This 64-bit counter should never wrap.
2509 * Either we are in around ~1 trillion A.C., assuming
2510 * 1 reboot per second, or we have a bug...
2511 */
2512 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002513
NeilBrowndafb20f2012-03-19 12:46:39 +11002514 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002515 if (rdev->badblocks.changed)
2516 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002517 if (test_bit(Faulty, &rdev->flags))
2518 set_bit(FaultRecorded, &rdev->flags);
2519 }
NeilBrown2699b672011-07-28 11:31:47 +10002520
NeilBrowne6910632008-02-06 01:39:51 -08002521 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002522 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
NeilBrown36a4e1f2011-10-07 14:23:17 +11002524 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2525 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Shaohua Li504634f2016-11-18 09:44:08 -08002527 if (mddev->queue)
2528 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002529rewrite:
NeilBrown4ad13662007-07-17 04:06:13 -07002530 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002531 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002533
NeilBrown42543762006-06-26 00:27:57 -07002534 if (rdev->sb_loaded != 1)
2535 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
NeilBrownf4667222013-12-09 12:04:56 +11002537 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002538 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002539 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002540 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002541 pr_debug("md: (write) %s's sb offset: %llu\n",
2542 bdevname(rdev->bdev, b),
2543 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002544 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002545 if (rdev->badblocks.size) {
2546 md_super_write(mddev, rdev,
2547 rdev->badblocks.sector,
2548 rdev->badblocks.size << 9,
2549 rdev->bb_page);
2550 rdev->badblocks.size = 0;
2551 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002552
NeilBrownf4667222013-12-09 12:04:56 +11002553 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002554 pr_debug("md: %s (skipping faulty)\n",
2555 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002556
NeilBrown7bfa19f2005-06-21 17:17:28 -07002557 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 /* only need to write one superblock... */
2559 break;
2560 }
NeilBrown46533ff2016-11-18 16:16:11 +11002561 if (md_super_wait(mddev) < 0)
2562 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002563 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002564
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002565 if (mddev_is_clustered(mddev) && ret == 0)
2566 md_cluster_ops->metadata_update_finish(mddev);
2567
NeilBrown850b2b422006-10-03 01:15:46 -07002568 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002569 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2570 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002571 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002572 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002573 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002574 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2575 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002576
NeilBrowndafb20f2012-03-19 12:46:39 +11002577 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002578 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2579 clear_bit(Blocked, &rdev->flags);
2580
2581 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002582 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002583 clear_bit(BlockedBadBlocks, &rdev->flags);
2584 wake_up(&rdev->blocked_wait);
2585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002587EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002589static int add_bound_rdev(struct md_rdev *rdev)
2590{
2591 struct mddev *mddev = rdev->mddev;
2592 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002593 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002594
Shaohua Li87d4d912016-01-06 14:37:14 -08002595 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002596 /* If there is hot_add_disk but no hot_remove_disk
2597 * then added disks for geometry changes,
2598 * and should be added immediately.
2599 */
2600 super_types[mddev->major_version].
2601 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002602 if (add_journal)
2603 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002604 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002605 if (add_journal)
2606 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002607 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002608 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002609 return err;
2610 }
2611 }
2612 sysfs_notify_dirent_safe(rdev->sysfs_state);
2613
Shaohua Li29530792016-12-08 15:48:19 -08002614 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002615 if (mddev->degraded)
2616 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2617 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2618 md_new_event(mddev);
2619 md_wakeup_thread(mddev->thread);
2620 return 0;
2621}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Andre Noll7f6ce762008-03-23 18:34:54 +01002623/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002624 * We want to accept with case. For this we use cmd_match.
2625 */
2626static int cmd_match(const char *cmd, const char *str)
2627{
2628 /* See if cmd, written into a sysfs file, matches
2629 * str. They must either be the same, or cmd can
2630 * have a trailing newline
2631 */
2632 while (*cmd && *str && *cmd == *str) {
2633 cmd++;
2634 str++;
2635 }
2636 if (*cmd == '\n')
2637 cmd++;
2638 if (*str || *cmd)
2639 return 0;
2640 return 1;
2641}
2642
NeilBrown86e6ffd2005-11-08 21:39:24 -08002643struct rdev_sysfs_entry {
2644 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002645 ssize_t (*show)(struct md_rdev *, char *);
2646 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002647};
2648
2649static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002650state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002651{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002652 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002653 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002654 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002655
NeilBrown758bfc82014-12-15 12:56:59 +11002656 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002657 (!test_bit(ExternalBbl, &flags) &&
2658 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002659 len += sprintf(page+len, "faulty%s", sep);
2660 if (test_bit(In_sync, &flags))
2661 len += sprintf(page+len, "in_sync%s", sep);
2662 if (test_bit(Journal, &flags))
2663 len += sprintf(page+len, "journal%s", sep);
2664 if (test_bit(WriteMostly, &flags))
2665 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002666 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002667 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002668 && !test_bit(Faulty, &flags)))
2669 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002670 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002671 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002672 !test_bit(In_sync, &flags))
2673 len += sprintf(page+len, "spare%s", sep);
2674 if (test_bit(WriteErrorSeen, &flags))
2675 len += sprintf(page+len, "write_error%s", sep);
2676 if (test_bit(WantReplacement, &flags))
2677 len += sprintf(page+len, "want_replacement%s", sep);
2678 if (test_bit(Replacement, &flags))
2679 len += sprintf(page+len, "replacement%s", sep);
2680 if (test_bit(ExternalBbl, &flags))
2681 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002682 if (test_bit(FailFast, &flags))
2683 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002684
2685 if (len)
2686 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002687
NeilBrown86e6ffd2005-11-08 21:39:24 -08002688 return len+sprintf(page+len, "\n");
2689}
2690
NeilBrown45dc2de2006-06-26 00:27:58 -07002691static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002692state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002693{
2694 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002695 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002696 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002697 * writemostly - sets write_mostly
2698 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002699 * blocked - sets the Blocked flags
2700 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002701 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002702 * -insync - clear Insync for a device with a slot assigned,
2703 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002704 * write_error - sets WriteErrorSeen
2705 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002706 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002707 */
2708 int err = -EINVAL;
2709 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2710 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002711 if (test_bit(Faulty, &rdev->flags))
2712 err = 0;
2713 else
2714 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002715 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002716 if (rdev->mddev->pers) {
2717 clear_bit(Blocked, &rdev->flags);
2718 remove_and_add_spares(rdev->mddev, rdev);
2719 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002720 if (rdev->raid_disk >= 0)
2721 err = -EBUSY;
2722 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002723 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002724 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002725 if (mddev_is_clustered(mddev))
2726 err = md_cluster_ops->remove_disk(mddev, rdev);
2727
2728 if (err == 0) {
2729 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002730 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002731 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002732 md_wakeup_thread(mddev->thread);
2733 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002734 md_new_event(mddev);
2735 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002736 }
NeilBrownf6556752006-06-26 00:28:01 -07002737 } else if (cmd_match(buf, "writemostly")) {
2738 set_bit(WriteMostly, &rdev->flags);
2739 err = 0;
2740 } else if (cmd_match(buf, "-writemostly")) {
2741 clear_bit(WriteMostly, &rdev->flags);
2742 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002743 } else if (cmd_match(buf, "blocked")) {
2744 set_bit(Blocked, &rdev->flags);
2745 err = 0;
2746 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002747 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002748 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002749 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002750 /* metadata handler doesn't understand badblocks,
2751 * so we need to fail the device
2752 */
2753 md_error(rdev->mddev, rdev);
2754 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002755 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002756 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002757 wake_up(&rdev->blocked_wait);
2758 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2759 md_wakeup_thread(rdev->mddev->thread);
2760
2761 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002762 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2763 set_bit(In_sync, &rdev->flags);
2764 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11002765 } else if (cmd_match(buf, "failfast")) {
2766 set_bit(FailFast, &rdev->flags);
2767 err = 0;
2768 } else if (cmd_match(buf, "-failfast")) {
2769 clear_bit(FailFast, &rdev->flags);
2770 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002771 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2772 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002773 if (rdev->mddev->pers == NULL) {
2774 clear_bit(In_sync, &rdev->flags);
2775 rdev->saved_raid_disk = rdev->raid_disk;
2776 rdev->raid_disk = -1;
2777 err = 0;
2778 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002779 } else if (cmd_match(buf, "write_error")) {
2780 set_bit(WriteErrorSeen, &rdev->flags);
2781 err = 0;
2782 } else if (cmd_match(buf, "-write_error")) {
2783 clear_bit(WriteErrorSeen, &rdev->flags);
2784 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002785 } else if (cmd_match(buf, "want_replacement")) {
2786 /* Any non-spare device that is not a replacement can
2787 * become want_replacement at any time, but we then need to
2788 * check if recovery is needed.
2789 */
2790 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002791 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002792 !test_bit(Replacement, &rdev->flags))
2793 set_bit(WantReplacement, &rdev->flags);
2794 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2795 md_wakeup_thread(rdev->mddev->thread);
2796 err = 0;
2797 } else if (cmd_match(buf, "-want_replacement")) {
2798 /* Clearing 'want_replacement' is always allowed.
2799 * Once replacements starts it is too late though.
2800 */
2801 err = 0;
2802 clear_bit(WantReplacement, &rdev->flags);
2803 } else if (cmd_match(buf, "replacement")) {
2804 /* Can only set a device as a replacement when array has not
2805 * yet been started. Once running, replacement is automatic
2806 * from spares, or by assigning 'slot'.
2807 */
2808 if (rdev->mddev->pers)
2809 err = -EBUSY;
2810 else {
2811 set_bit(Replacement, &rdev->flags);
2812 err = 0;
2813 }
2814 } else if (cmd_match(buf, "-replacement")) {
2815 /* Similarly, can only clear Replacement before start */
2816 if (rdev->mddev->pers)
2817 err = -EBUSY;
2818 else {
2819 clear_bit(Replacement, &rdev->flags);
2820 err = 0;
2821 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002822 } else if (cmd_match(buf, "re-add")) {
2823 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002824 /* clear_bit is performed _after_ all the devices
2825 * have their local Faulty bit cleared. If any writes
2826 * happen in the meantime in the local node, they
2827 * will land in the local bitmap, which will be synced
2828 * by this node eventually
2829 */
2830 if (!mddev_is_clustered(rdev->mddev) ||
2831 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2832 clear_bit(Faulty, &rdev->flags);
2833 err = add_bound_rdev(rdev);
2834 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002835 } else
2836 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002837 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2838 set_bit(ExternalBbl, &rdev->flags);
2839 rdev->badblocks.shift = 0;
2840 err = 0;
2841 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2842 clear_bit(ExternalBbl, &rdev->flags);
2843 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002844 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002845 if (!err)
2846 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002847 return err ? err : len;
2848}
NeilBrown80ca3a42006-07-10 04:44:18 -07002849static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002850__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002851
2852static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002853errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002854{
2855 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2856}
2857
2858static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002859errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002860{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002861 unsigned int n;
2862 int rv;
2863
2864 rv = kstrtouint(buf, 10, &n);
2865 if (rv < 0)
2866 return rv;
2867 atomic_set(&rdev->corrected_errors, n);
2868 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002869}
2870static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002871__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002872
NeilBrown014236d2006-01-06 00:20:55 -08002873static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002874slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002875{
Shaohua Lif2076e72015-10-08 21:54:12 -07002876 if (test_bit(Journal, &rdev->flags))
2877 return sprintf(page, "journal\n");
2878 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002879 return sprintf(page, "none\n");
2880 else
2881 return sprintf(page, "%d\n", rdev->raid_disk);
2882}
2883
2884static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002885slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002886{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002887 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002888 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002889
Shaohua Lif2076e72015-10-08 21:54:12 -07002890 if (test_bit(Journal, &rdev->flags))
2891 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002892 if (strncmp(buf, "none", 4)==0)
2893 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002894 else {
2895 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2896 if (err < 0)
2897 return err;
2898 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002899 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002900 /* Setting 'slot' on an active array requires also
2901 * updating the 'rd%d' link, and communicating
2902 * with the personality with ->hot_*_disk.
2903 * For now we only support removing
2904 * failed/spare devices. This normally happens automatically,
2905 * but not when the metadata is externally managed.
2906 */
NeilBrownc303da62008-02-06 01:39:51 -08002907 if (rdev->raid_disk == -1)
2908 return -EEXIST;
2909 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002910 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002911 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002912 clear_bit(Blocked, &rdev->flags);
2913 remove_and_add_spares(rdev->mddev, rdev);
2914 if (rdev->raid_disk >= 0)
2915 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002916 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2917 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002918 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002919 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002920 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002921 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002922 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002923
2924 if (rdev->raid_disk != -1)
2925 return -EBUSY;
2926
NeilBrownc6751b22011-02-02 11:57:13 +11002927 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2928 return -EBUSY;
2929
Neil Brown6c2fce22008-06-28 08:31:31 +10002930 if (rdev->mddev->pers->hot_add_disk == NULL)
2931 return -EINVAL;
2932
NeilBrownba1b41b2011-01-14 09:14:34 +11002933 if (slot >= rdev->mddev->raid_disks &&
2934 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2935 return -ENOSPC;
2936
Neil Brown6c2fce22008-06-28 08:31:31 +10002937 rdev->raid_disk = slot;
2938 if (test_bit(In_sync, &rdev->flags))
2939 rdev->saved_raid_disk = slot;
2940 else
2941 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002942 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002943 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002944 err = rdev->mddev->pers->
2945 hot_add_disk(rdev->mddev, rdev);
2946 if (err) {
2947 rdev->raid_disk = -1;
2948 return err;
2949 } else
2950 sysfs_notify_dirent_safe(rdev->sysfs_state);
2951 if (sysfs_link_rdev(rdev->mddev, rdev))
2952 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002953 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002954 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002955 if (slot >= rdev->mddev->raid_disks &&
2956 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002957 return -ENOSPC;
2958 rdev->raid_disk = slot;
2959 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002960 clear_bit(Faulty, &rdev->flags);
2961 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002962 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002963 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002964 }
NeilBrown014236d2006-01-06 00:20:55 -08002965 return len;
2966}
2967
NeilBrown014236d2006-01-06 00:20:55 -08002968static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002969__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002970
NeilBrown93c8cad2006-01-06 00:20:56 -08002971static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002972offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002973{
Andrew Morton6961ece2006-01-06 00:20:59 -08002974 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002975}
2976
2977static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002978offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002979{
NeilBrownc6563a82012-05-21 09:27:00 +10002980 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002981 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002982 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002983 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002984 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002985 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002986 /* Must set offset before size, so overlap checks
2987 * can be sane */
2988 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002989 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002990 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002991 return len;
2992}
2993
2994static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002995__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002996
NeilBrownc6563a82012-05-21 09:27:00 +10002997static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2998{
2999 return sprintf(page, "%llu\n",
3000 (unsigned long long)rdev->new_data_offset);
3001}
3002
3003static ssize_t new_offset_store(struct md_rdev *rdev,
3004 const char *buf, size_t len)
3005{
3006 unsigned long long new_offset;
3007 struct mddev *mddev = rdev->mddev;
3008
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003009 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10003010 return -EINVAL;
3011
NeilBrownf851b602014-12-11 10:02:10 +11003012 if (mddev->sync_thread ||
3013 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10003014 return -EBUSY;
3015 if (new_offset == rdev->data_offset)
3016 /* reset is always permitted */
3017 ;
3018 else if (new_offset > rdev->data_offset) {
3019 /* must not push array size beyond rdev_sectors */
3020 if (new_offset - rdev->data_offset
3021 + mddev->dev_sectors > rdev->sectors)
3022 return -E2BIG;
3023 }
3024 /* Metadata worries about other space details. */
3025
3026 /* decreasing the offset is inconsistent with a backwards
3027 * reshape.
3028 */
3029 if (new_offset < rdev->data_offset &&
3030 mddev->reshape_backwards)
3031 return -EINVAL;
3032 /* Increasing offset is inconsistent with forwards
3033 * reshape. reshape_direction should be set to
3034 * 'backwards' first.
3035 */
3036 if (new_offset > rdev->data_offset &&
3037 !mddev->reshape_backwards)
3038 return -EINVAL;
3039
3040 if (mddev->pers && mddev->persistent &&
3041 !super_types[mddev->major_version]
3042 .allow_new_offset(rdev, new_offset))
3043 return -E2BIG;
3044 rdev->new_data_offset = new_offset;
3045 if (new_offset > rdev->data_offset)
3046 mddev->reshape_backwards = 1;
3047 else if (new_offset < rdev->data_offset)
3048 mddev->reshape_backwards = 0;
3049
3050 return len;
3051}
3052static struct rdev_sysfs_entry rdev_new_offset =
3053__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3054
NeilBrown83303b62006-01-06 00:21:06 -08003055static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003056rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003057{
Andre Nolldd8ac332009-03-31 14:33:13 +11003058 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003059}
3060
NeilBrownc5d79ad2008-02-06 01:39:54 -08003061static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3062{
3063 /* check if two start/length pairs overlap */
3064 if (s1+l1 <= s2)
3065 return 0;
3066 if (s2+l2 <= s1)
3067 return 0;
3068 return 1;
3069}
3070
Dan Williamsb522adc2009-03-31 15:00:31 +11003071static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3072{
3073 unsigned long long blocks;
3074 sector_t new;
3075
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003076 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003077 return -EINVAL;
3078
3079 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3080 return -EINVAL; /* sector conversion overflow */
3081
3082 new = blocks * 2;
3083 if (new != blocks * 2)
3084 return -EINVAL; /* unsigned long long to sector_t overflow */
3085
3086 *sectors = new;
3087 return 0;
3088}
3089
NeilBrown83303b62006-01-06 00:21:06 -08003090static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003091rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003092{
NeilBrownfd01b882011-10-11 16:47:53 +11003093 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003094 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003095 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003096
Shaohua Lif2076e72015-10-08 21:54:12 -07003097 if (test_bit(Journal, &rdev->flags))
3098 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003099 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003100 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003101 if (rdev->data_offset != rdev->new_data_offset)
3102 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003103 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003104 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003105 sectors = super_types[my_mddev->major_version].
3106 rdev_size_change(rdev, sectors);
3107 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003108 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003109 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003110 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003111 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003112 if (!my_mddev->pers->resize)
3113 /* Cannot change size for RAID0 or Linear etc */
3114 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003115 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003116 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003117 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003118
Andre Nolldd8ac332009-03-31 14:33:13 +11003119 rdev->sectors = sectors;
3120 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003121 /* Need to check that all other rdevs with the same
3122 * ->bdev do not overlap. 'rcu' is sufficient to walk
3123 * the rdev lists safely.
3124 * This check does not provide a hard guarantee, it
3125 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003126 */
NeilBrownfd01b882011-10-11 16:47:53 +11003127 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003128 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003129 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003130
NeilBrown8b1afc32014-09-29 15:33:20 +10003131 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003132 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003133 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003134
NeilBrowndafb20f2012-03-19 12:46:39 +11003135 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003136 if (rdev->bdev == rdev2->bdev &&
3137 rdev != rdev2 &&
3138 overlaps(rdev->data_offset, rdev->sectors,
3139 rdev2->data_offset,
3140 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003141 overlap = 1;
3142 break;
3143 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003144 if (overlap) {
3145 mddev_put(mddev);
3146 break;
3147 }
3148 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003149 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003150 if (overlap) {
3151 /* Someone else could have slipped in a size
3152 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003153 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003154 * safe, and trust userspace not to race with
3155 * itself
3156 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003157 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003158 return -EBUSY;
3159 }
3160 }
NeilBrown83303b62006-01-06 00:21:06 -08003161 return len;
3162}
3163
3164static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003165__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003166
NeilBrown3cb03002011-10-11 16:45:26 +11003167static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003168{
3169 unsigned long long recovery_start = rdev->recovery_offset;
3170
3171 if (test_bit(In_sync, &rdev->flags) ||
3172 recovery_start == MaxSector)
3173 return sprintf(page, "none\n");
3174
3175 return sprintf(page, "%llu\n", recovery_start);
3176}
3177
NeilBrown3cb03002011-10-11 16:45:26 +11003178static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003179{
3180 unsigned long long recovery_start;
3181
3182 if (cmd_match(buf, "none"))
3183 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003184 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003185 return -EINVAL;
3186
3187 if (rdev->mddev->pers &&
3188 rdev->raid_disk >= 0)
3189 return -EBUSY;
3190
3191 rdev->recovery_offset = recovery_start;
3192 if (recovery_start == MaxSector)
3193 set_bit(In_sync, &rdev->flags);
3194 else
3195 clear_bit(In_sync, &rdev->flags);
3196 return len;
3197}
3198
3199static struct rdev_sysfs_entry rdev_recovery_start =
3200__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3201
Vishal Vermafc974ee2015-12-24 19:20:34 -07003202/* sysfs access to bad-blocks list.
3203 * We present two files.
3204 * 'bad-blocks' lists sector numbers and lengths of ranges that
3205 * are recorded as bad. The list is truncated to fit within
3206 * the one-page limit of sysfs.
3207 * Writing "sector length" to this file adds an acknowledged
3208 * bad block list.
3209 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3210 * been acknowledged. Writing to this file adds bad blocks
3211 * without acknowledging them. This is largely for testing.
3212 */
NeilBrown3cb03002011-10-11 16:45:26 +11003213static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003214{
3215 return badblocks_show(&rdev->badblocks, page, 0);
3216}
NeilBrown3cb03002011-10-11 16:45:26 +11003217static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003218{
NeilBrownde393cd2011-07-28 11:31:48 +10003219 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3220 /* Maybe that ack was all we needed */
3221 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3222 wake_up(&rdev->blocked_wait);
3223 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003224}
3225static struct rdev_sysfs_entry rdev_bad_blocks =
3226__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3227
NeilBrown3cb03002011-10-11 16:45:26 +11003228static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003229{
3230 return badblocks_show(&rdev->badblocks, page, 1);
3231}
NeilBrown3cb03002011-10-11 16:45:26 +11003232static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003233{
3234 return badblocks_store(&rdev->badblocks, page, len, 1);
3235}
3236static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3237__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3238
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003239static ssize_t
3240ppl_sector_show(struct md_rdev *rdev, char *page)
3241{
3242 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3243}
3244
3245static ssize_t
3246ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3247{
3248 unsigned long long sector;
3249
3250 if (kstrtoull(buf, 10, &sector) < 0)
3251 return -EINVAL;
3252 if (sector != (sector_t)sector)
3253 return -EINVAL;
3254
3255 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3256 rdev->raid_disk >= 0)
3257 return -EBUSY;
3258
3259 if (rdev->mddev->persistent) {
3260 if (rdev->mddev->major_version == 0)
3261 return -EINVAL;
3262 if ((sector > rdev->sb_start &&
3263 sector - rdev->sb_start > S16_MAX) ||
3264 (sector < rdev->sb_start &&
3265 rdev->sb_start - sector > -S16_MIN))
3266 return -EINVAL;
3267 rdev->ppl.offset = sector - rdev->sb_start;
3268 } else if (!rdev->mddev->external) {
3269 return -EBUSY;
3270 }
3271 rdev->ppl.sector = sector;
3272 return len;
3273}
3274
3275static struct rdev_sysfs_entry rdev_ppl_sector =
3276__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3277
3278static ssize_t
3279ppl_size_show(struct md_rdev *rdev, char *page)
3280{
3281 return sprintf(page, "%u\n", rdev->ppl.size);
3282}
3283
3284static ssize_t
3285ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3286{
3287 unsigned int size;
3288
3289 if (kstrtouint(buf, 10, &size) < 0)
3290 return -EINVAL;
3291
3292 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3293 rdev->raid_disk >= 0)
3294 return -EBUSY;
3295
3296 if (rdev->mddev->persistent) {
3297 if (rdev->mddev->major_version == 0)
3298 return -EINVAL;
3299 if (size > U16_MAX)
3300 return -EINVAL;
3301 } else if (!rdev->mddev->external) {
3302 return -EBUSY;
3303 }
3304 rdev->ppl.size = size;
3305 return len;
3306}
3307
3308static struct rdev_sysfs_entry rdev_ppl_size =
3309__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3310
NeilBrown86e6ffd2005-11-08 21:39:24 -08003311static struct attribute *rdev_default_attrs[] = {
3312 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003313 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003314 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003315 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003316 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003317 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003318 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003319 &rdev_bad_blocks.attr,
3320 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003321 &rdev_ppl_sector.attr,
3322 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003323 NULL,
3324};
3325static ssize_t
3326rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3327{
3328 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003329 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003330
3331 if (!entry->show)
3332 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003333 if (!rdev->mddev)
3334 return -EBUSY;
3335 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003336}
3337
3338static ssize_t
3339rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3340 const char *page, size_t length)
3341{
3342 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003343 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003344 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003345 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003346
3347 if (!entry->store)
3348 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003349 if (!capable(CAP_SYS_ADMIN))
3350 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003351 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003352 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003353 if (rdev->mddev == NULL)
3354 rv = -EBUSY;
3355 else
3356 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003357 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003358 }
3359 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003360}
3361
3362static void rdev_free(struct kobject *ko)
3363{
NeilBrown3cb03002011-10-11 16:45:26 +11003364 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003365 kfree(rdev);
3366}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003367static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003368 .show = rdev_attr_show,
3369 .store = rdev_attr_store,
3370};
3371static struct kobj_type rdev_ktype = {
3372 .release = rdev_free,
3373 .sysfs_ops = &rdev_sysfs_ops,
3374 .default_attrs = rdev_default_attrs,
3375};
3376
NeilBrown3cb03002011-10-11 16:45:26 +11003377int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003378{
3379 rdev->desc_nr = -1;
3380 rdev->saved_raid_disk = -1;
3381 rdev->raid_disk = -1;
3382 rdev->flags = 0;
3383 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003384 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003385 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003386 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003387 rdev->sb_loaded = 0;
3388 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003389 atomic_set(&rdev->nr_pending, 0);
3390 atomic_set(&rdev->read_errors, 0);
3391 atomic_set(&rdev->corrected_errors, 0);
3392
3393 INIT_LIST_HEAD(&rdev->same_set);
3394 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003395
3396 /* Add space to store bad block list.
3397 * This reserves the space even on arrays where it cannot
3398 * be used - I wonder if that matters
3399 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003400 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003401}
3402EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403/*
3404 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3405 *
3406 * mark the device faulty if:
3407 *
3408 * - the device is nonexistent (zero size)
3409 * - the device has no valid superblock
3410 *
3411 * a faulty rdev _never_ has rdev->sb set.
3412 */
NeilBrown3cb03002011-10-11 16:45:26 +11003413static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
3415 char b[BDEVNAME_SIZE];
3416 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003417 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 sector_t size;
3419
NeilBrown9ffae0c2006-01-06 00:20:32 -08003420 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003421 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
NeilBrown2230dfe2011-07-28 11:31:46 +10003424 err = md_rdev_init(rdev);
3425 if (err)
3426 goto abort_free;
3427 err = alloc_disk_sb(rdev);
3428 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 goto abort_free;
3430
NeilBrownc5d79ad2008-02-06 01:39:54 -08003431 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 if (err)
3433 goto abort_free;
3434
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003435 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003436
Mike Snitzer77304d22010-11-08 14:39:12 +01003437 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003439 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 bdevname(rdev->bdev,b));
3441 err = -EINVAL;
3442 goto abort_free;
3443 }
3444
3445 if (super_format >= 0) {
3446 err = super_types[super_format].
3447 load_super(rdev, NULL, super_minor);
3448 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003449 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003450 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003451 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 goto abort_free;
3453 }
3454 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003455 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 bdevname(rdev->bdev,b));
3457 goto abort_free;
3458 }
3459 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003460
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 return rdev;
3462
3463abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003464 if (rdev->bdev)
3465 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003466 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 kfree(rdev);
3468 return ERR_PTR(err);
3469}
3470
3471/*
3472 * Check a full RAID array for plausibility
3473 */
3474
NeilBrownf72ffdd2014-09-30 14:23:59 +10003475static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476{
3477 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003478 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 char b[BDEVNAME_SIZE];
3480
3481 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003482 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 switch (super_types[mddev->major_version].
3484 load_super(rdev, freshest, mddev->minor_version)) {
3485 case 1:
3486 freshest = rdev;
3487 break;
3488 case 0:
3489 break;
3490 default:
NeilBrown9d487392016-11-02 14:16:49 +11003491 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003493 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 }
3495
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 super_types[mddev->major_version].
3497 validate_super(mddev, freshest);
3498
3499 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003500 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003501 if (mddev->max_disks &&
3502 (rdev->desc_nr >= mddev->max_disks ||
3503 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003504 pr_warn("md: %s: %s: only %d devices permitted\n",
3505 mdname(mddev), bdevname(rdev->bdev, b),
3506 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003507 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003508 continue;
3509 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003510 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 if (super_types[mddev->major_version].
3512 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003513 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003515 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 continue;
3517 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 if (mddev->level == LEVEL_MULTIPATH) {
3520 rdev->desc_nr = i++;
3521 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003522 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003523 } else if (rdev->raid_disk >=
3524 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3525 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003526 rdev->raid_disk = -1;
3527 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 }
3529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530}
3531
NeilBrown72e02072009-12-14 12:49:55 +11003532/* Read a fixed-point number.
3533 * Numbers in sysfs attributes should be in "standard" units where
3534 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003535 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003536 * milliseconds or jiffies.
3537 * This function takes a decimal number with a possible fractional
3538 * component, and produces an integer which is the result of
3539 * multiplying that number by 10^'scale'.
3540 * all without any floating-point arithmetic.
3541 */
3542int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3543{
3544 unsigned long result = 0;
3545 long decimals = -1;
3546 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3547 if (*cp == '.')
3548 decimals = 0;
3549 else if (decimals < scale) {
3550 unsigned int value;
3551 value = *cp - '0';
3552 result = result * 10 + value;
3553 if (decimals >= 0)
3554 decimals++;
3555 }
3556 cp++;
3557 }
3558 if (*cp == '\n')
3559 cp++;
3560 if (*cp)
3561 return -EINVAL;
3562 if (decimals < 0)
3563 decimals = 0;
3564 while (decimals < scale) {
3565 result *= 10;
3566 decimals ++;
3567 }
3568 *res = result;
3569 return 0;
3570}
3571
NeilBrowneae17012005-11-08 21:39:23 -08003572static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003573safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003574{
3575 int msec = (mddev->safemode_delay*1000)/HZ;
3576 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3577}
3578static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003579safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003580{
NeilBrown16f17b32006-06-26 00:27:37 -07003581 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003582
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003583 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003584 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003585 return -EINVAL;
3586 }
3587
NeilBrown72e02072009-12-14 12:49:55 +11003588 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003589 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003590 if (msec == 0)
3591 mddev->safemode_delay = 0;
3592 else {
NeilBrown19052c02008-08-05 15:54:13 +10003593 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003594 unsigned long new_delay = (msec*HZ)/1000;
3595
3596 if (new_delay == 0)
3597 new_delay = 1;
3598 mddev->safemode_delay = new_delay;
3599 if (new_delay < old_delay || old_delay == 0)
3600 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003601 }
3602 return len;
3603}
3604static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003605__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003606
3607static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003608level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003609{
NeilBrown36d091f2014-12-15 12:56:58 +11003610 struct md_personality *p;
3611 int ret;
3612 spin_lock(&mddev->lock);
3613 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003614 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003615 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003616 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003617 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003618 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003619 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003620 else
NeilBrown36d091f2014-12-15 12:56:58 +11003621 ret = 0;
3622 spin_unlock(&mddev->lock);
3623 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003624}
3625
NeilBrownd9d166c2006-01-06 00:20:51 -08003626static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003627level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003628{
Dan Williamsf2859af2010-05-02 10:04:16 -07003629 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003630 ssize_t rv;
3631 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003632 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003633 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003634 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003635 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003636
NeilBrown67918752014-12-15 12:57:01 +11003637 if (slen == 0 || slen >= sizeof(clevel))
3638 return -EINVAL;
3639
3640 rv = mddev_lock(mddev);
3641 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003642 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003643
3644 if (mddev->pers == NULL) {
3645 strncpy(mddev->clevel, buf, slen);
3646 if (mddev->clevel[slen-1] == '\n')
3647 slen--;
3648 mddev->clevel[slen] = 0;
3649 mddev->level = LEVEL_NONE;
3650 rv = len;
3651 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003652 }
NeilBrown67918752014-12-15 12:57:01 +11003653 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003654 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003655 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003656
3657 /* request to change the personality. Need to ensure:
3658 * - array is not engaged in resync/recovery/reshape
3659 * - old personality can be suspended
3660 * - new personality will access other array.
3661 */
3662
NeilBrown67918752014-12-15 12:57:01 +11003663 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003664 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003665 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003666 mddev->reshape_position != MaxSector ||
3667 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003668 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003669
NeilBrown67918752014-12-15 12:57:01 +11003670 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003671 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003672 pr_warn("md: %s: %s does not support online personality change\n",
3673 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003674 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003675 }
3676
3677 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003678 strncpy(clevel, buf, slen);
3679 if (clevel[slen-1] == '\n')
3680 slen--;
3681 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003682 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003683 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003684
Dan Williamsf2859af2010-05-02 10:04:16 -07003685 if (request_module("md-%s", clevel) != 0)
3686 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003687 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003688 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003689 if (!pers || !try_module_get(pers->owner)) {
3690 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003691 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003692 rv = -EINVAL;
3693 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003694 }
3695 spin_unlock(&pers_lock);
3696
3697 if (pers == mddev->pers) {
3698 /* Nothing to do! */
3699 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003700 rv = len;
3701 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003702 }
3703 if (!pers->takeover) {
3704 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003705 pr_warn("md: %s: %s does not support personality takeover\n",
3706 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003707 rv = -EINVAL;
3708 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003709 }
3710
NeilBrowndafb20f2012-03-19 12:46:39 +11003711 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003712 rdev->new_raid_disk = rdev->raid_disk;
3713
NeilBrown245f46c2009-03-31 14:39:39 +11003714 /* ->takeover must set new_* and/or delta_disks
3715 * if it succeeds, and may set them when it fails.
3716 */
3717 priv = pers->takeover(mddev);
3718 if (IS_ERR(priv)) {
3719 mddev->new_level = mddev->level;
3720 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003721 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003722 mddev->raid_disks -= mddev->delta_disks;
3723 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003724 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003725 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003726 pr_warn("md: %s: %s would not accept array\n",
3727 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003728 rv = PTR_ERR(priv);
3729 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003730 }
3731
3732 /* Looks like we have a winner */
3733 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003734 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003735
3736 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003737 oldpers = mddev->pers;
3738 oldpriv = mddev->private;
3739 mddev->pers = pers;
3740 mddev->private = priv;
3741 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3742 mddev->level = mddev->new_level;
3743 mddev->layout = mddev->new_layout;
3744 mddev->chunk_sectors = mddev->new_chunk_sectors;
3745 mddev->delta_disks = 0;
3746 mddev->reshape_backwards = 0;
3747 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003748 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003749
NeilBrowndb721d32014-12-15 12:56:58 +11003750 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003751 mddev->external) {
3752 /* We are converting from a no-redundancy array
3753 * to a redundancy array and metadata is managed
3754 * externally so we need to be sure that writes
3755 * won't block due to a need to transition
3756 * clean->dirty
3757 * until external management is started.
3758 */
3759 mddev->in_sync = 0;
3760 mddev->safemode_delay = 0;
3761 mddev->safemode = 0;
3762 }
3763
NeilBrowndb721d32014-12-15 12:56:58 +11003764 oldpers->free(mddev, oldpriv);
3765
3766 if (oldpers->sync_request == NULL &&
3767 pers->sync_request != NULL) {
3768 /* need to add the md_redundancy_group */
3769 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11003770 pr_warn("md: cannot register extra attributes for %s\n",
3771 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11003772 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3773 }
3774 if (oldpers->sync_request != NULL &&
3775 pers->sync_request == NULL) {
3776 /* need to remove the md_redundancy_group */
3777 if (mddev->to_remove == NULL)
3778 mddev->to_remove = &md_redundancy_group;
3779 }
3780
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003781 module_put(oldpers->owner);
3782
NeilBrowndafb20f2012-03-19 12:46:39 +11003783 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003784 if (rdev->raid_disk < 0)
3785 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003786 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003787 rdev->new_raid_disk = -1;
3788 if (rdev->new_raid_disk == rdev->raid_disk)
3789 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003790 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003791 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003792 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003793 if (rdev->raid_disk < 0)
3794 continue;
3795 if (rdev->new_raid_disk == rdev->raid_disk)
3796 continue;
3797 rdev->raid_disk = rdev->new_raid_disk;
3798 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003799 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003800 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003801 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11003802 pr_warn("md: cannot register rd%d for %s after level change\n",
3803 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003804 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003805 }
3806
NeilBrowndb721d32014-12-15 12:56:58 +11003807 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003808 /* this is now an array without redundancy, so
3809 * it must always be in_sync
3810 */
3811 mddev->in_sync = 1;
3812 del_timer_sync(&mddev->safemode_timer);
3813 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003814 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003815 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08003816 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003817 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003818 if (!mddev->thread)
3819 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003820 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003821 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003822 rv = len;
3823out_unlock:
3824 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003825 return rv;
3826}
3827
3828static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003829__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003830
NeilBrownd4dbd022006-06-26 00:27:59 -07003831static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003832layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003833{
3834 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003835 if (mddev->reshape_position != MaxSector &&
3836 mddev->layout != mddev->new_layout)
3837 return sprintf(page, "%d (%d)\n",
3838 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003839 return sprintf(page, "%d\n", mddev->layout);
3840}
3841
3842static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003843layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003844{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003845 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003846 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003847
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003848 err = kstrtouint(buf, 10, &n);
3849 if (err < 0)
3850 return err;
NeilBrown67918752014-12-15 12:57:01 +11003851 err = mddev_lock(mddev);
3852 if (err)
3853 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003854
NeilBrownb3546032009-03-31 14:56:41 +11003855 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003856 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003857 err = -EBUSY;
3858 else if (mddev->ro)
3859 err = -EROFS;
3860 else {
3861 mddev->new_layout = n;
3862 err = mddev->pers->check_reshape(mddev);
3863 if (err)
3864 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003865 }
NeilBrownb3546032009-03-31 14:56:41 +11003866 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003867 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003868 if (mddev->reshape_position == MaxSector)
3869 mddev->layout = n;
3870 }
NeilBrown67918752014-12-15 12:57:01 +11003871 mddev_unlock(mddev);
3872 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003873}
3874static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003875__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003876
NeilBrowneae17012005-11-08 21:39:23 -08003877static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003878raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003879{
NeilBrownbb636542005-11-08 21:39:45 -08003880 if (mddev->raid_disks == 0)
3881 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003882 if (mddev->reshape_position != MaxSector &&
3883 mddev->delta_disks != 0)
3884 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3885 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003886 return sprintf(page, "%d\n", mddev->raid_disks);
3887}
3888
NeilBrownfd01b882011-10-11 16:47:53 +11003889static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003890
3891static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003892raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003893{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003894 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003895 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003896
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003897 err = kstrtouint(buf, 10, &n);
3898 if (err < 0)
3899 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003900
NeilBrown67918752014-12-15 12:57:01 +11003901 err = mddev_lock(mddev);
3902 if (err)
3903 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003904 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003905 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003906 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003907 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003908 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003909
NeilBrown67918752014-12-15 12:57:01 +11003910 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003911 rdev_for_each(rdev, mddev) {
3912 if (olddisks < n &&
3913 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003914 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003915 if (olddisks > n &&
3916 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003917 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003918 }
NeilBrown67918752014-12-15 12:57:01 +11003919 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003920 mddev->delta_disks = n - olddisks;
3921 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003922 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003923 } else
NeilBrownda943b992006-01-06 00:20:54 -08003924 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003925out_unlock:
3926 mddev_unlock(mddev);
3927 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003928}
3929static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003930__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003931
NeilBrown24dd4692005-11-08 21:39:26 -08003932static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003933chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003934{
NeilBrown08a02ec2007-05-09 02:35:38 -07003935 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003936 mddev->chunk_sectors != mddev->new_chunk_sectors)
3937 return sprintf(page, "%d (%d)\n",
3938 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003939 mddev->chunk_sectors << 9);
3940 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003941}
3942
3943static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003944chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003945{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003946 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003947 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003948
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003949 err = kstrtoul(buf, 10, &n);
3950 if (err < 0)
3951 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003952
NeilBrown67918752014-12-15 12:57:01 +11003953 err = mddev_lock(mddev);
3954 if (err)
3955 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003956 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003957 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003958 err = -EBUSY;
3959 else if (mddev->ro)
3960 err = -EROFS;
3961 else {
3962 mddev->new_chunk_sectors = n >> 9;
3963 err = mddev->pers->check_reshape(mddev);
3964 if (err)
3965 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003966 }
NeilBrownb3546032009-03-31 14:56:41 +11003967 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003968 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003969 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003970 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003971 }
NeilBrown67918752014-12-15 12:57:01 +11003972 mddev_unlock(mddev);
3973 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003974}
3975static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003976__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003977
NeilBrowna94213b2006-06-26 00:28:00 -07003978static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003979resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003980{
NeilBrownd1a7c502009-03-31 15:24:32 +11003981 if (mddev->recovery_cp == MaxSector)
3982 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003983 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3984}
3985
3986static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003987resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003988{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003989 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003990 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003991
3992 if (cmd_match(buf, "none"))
3993 n = MaxSector;
3994 else {
3995 err = kstrtoull(buf, 10, &n);
3996 if (err < 0)
3997 return err;
3998 if (n != (sector_t)n)
3999 return -EINVAL;
4000 }
NeilBrowna94213b2006-06-26 00:28:00 -07004001
NeilBrown67918752014-12-15 12:57:01 +11004002 err = mddev_lock(mddev);
4003 if (err)
4004 return err;
NeilBrownb0986362011-05-11 15:52:21 +10004005 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11004006 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07004007
NeilBrown67918752014-12-15 12:57:01 +11004008 if (!err) {
4009 mddev->recovery_cp = n;
4010 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08004011 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004012 }
4013 mddev_unlock(mddev);
4014 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07004015}
4016static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10004017__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4018 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07004019
NeilBrown9e653b62006-06-26 00:27:58 -07004020/*
4021 * The array state can be:
4022 *
4023 * clear
4024 * No devices, no size, no level
4025 * Equivalent to STOP_ARRAY ioctl
4026 * inactive
4027 * May have some settings, but array is not active
4028 * all IO results in error
4029 * When written, doesn't tear down array, but just stops it
4030 * suspended (not supported yet)
4031 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01004032 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07004033 * readonly
4034 * no resync can happen. no superblocks get written.
4035 * write requests fail
4036 * read-auto
4037 * like readonly, but behaves like 'clean' on a write request.
4038 *
4039 * clean - no pending writes, but otherwise active.
4040 * When written to inactive array, starts without resync
4041 * If a write request arrives then
4042 * if metadata is known, mark 'dirty' and switch to 'active'.
4043 * if not known, block and switch to write-pending
4044 * If written to an active array that has pending writes, then fails.
4045 * active
4046 * fully active: IO and resync can be happening.
4047 * When written to inactive array, starts with resync
4048 *
4049 * write-pending
4050 * clean, but writes are blocked waiting for 'active' to be written.
4051 *
4052 * active-idle
4053 * like active, but no writes have been seen for a while (100msec).
4054 *
4055 */
4056enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4057 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004058static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004059 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4060 "write-pending", "active-idle", NULL };
4061
4062static int match_word(const char *word, char **list)
4063{
4064 int n;
4065 for (n=0; list[n]; n++)
4066 if (cmd_match(word, list[n]))
4067 break;
4068 return n;
4069}
4070
4071static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004072array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004073{
4074 enum array_state st = inactive;
4075
4076 if (mddev->pers)
4077 switch(mddev->ro) {
4078 case 1:
4079 st = readonly;
4080 break;
4081 case 2:
4082 st = read_auto;
4083 break;
4084 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004085 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004086 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004087 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004088 else if (mddev->in_sync)
4089 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004090 else if (mddev->safemode)
4091 st = active_idle;
4092 else
4093 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004094 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004095 }
4096 else {
4097 if (list_empty(&mddev->disks) &&
4098 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004099 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004100 st = clear;
4101 else
4102 st = inactive;
4103 }
4104 return sprintf(page, "%s\n", array_states[st]);
4105}
4106
NeilBrownf72ffdd2014-09-30 14:23:59 +10004107static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4108static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4109static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004110static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004111
4112static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004113array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004114{
NeilBrown6497709b2017-03-15 14:05:14 +11004115 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004116 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004117
4118 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4119 /* don't take reconfig_mutex when toggling between
4120 * clean and active
4121 */
4122 spin_lock(&mddev->lock);
4123 if (st == active) {
4124 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004125 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004126 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004127 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004128 } else /* st == clean */ {
4129 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004130 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004131 err = -EBUSY;
4132 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004133 if (!err)
4134 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004135 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004136 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004137 }
4138 err = mddev_lock(mddev);
4139 if (err)
4140 return err;
4141 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004142 switch(st) {
4143 case bad_word:
4144 break;
4145 case clear:
4146 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004147 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004148 break;
4149 case inactive:
4150 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004151 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004152 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004153 else
NeilBrowne6910632008-02-06 01:39:51 -08004154 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004155 break;
4156 case suspended:
4157 break; /* not supported yet */
4158 case readonly:
4159 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004160 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004161 else {
4162 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004163 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004164 err = do_md_run(mddev);
4165 }
4166 break;
4167 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004168 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004169 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004170 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004171 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004172 err = restart_array(mddev);
4173 if (err == 0) {
4174 mddev->ro = 2;
4175 set_disk_ro(mddev->gendisk, 0);
4176 }
NeilBrown9e653b62006-06-26 00:27:58 -07004177 } else {
4178 mddev->ro = 2;
4179 err = do_md_run(mddev);
4180 }
4181 break;
4182 case clean:
4183 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004184 err = restart_array(mddev);
4185 if (err)
4186 break;
NeilBrown85572d72014-12-15 12:56:56 +11004187 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004188 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004189 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004190 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004191 } else
4192 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004193 break;
4194 case active:
4195 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004196 err = restart_array(mddev);
4197 if (err)
4198 break;
Shaohua Li29530792016-12-08 15:48:19 -08004199 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004200 wake_up(&mddev->sb_wait);
4201 err = 0;
4202 } else {
4203 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004204 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004205 err = do_md_run(mddev);
4206 }
4207 break;
4208 case write_pending:
4209 case active_idle:
4210 /* these cannot be set */
4211 break;
4212 }
NeilBrown67918752014-12-15 12:57:01 +11004213
4214 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004215 if (mddev->hold_active == UNTIL_IOCTL)
4216 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004217 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004218 }
NeilBrown67918752014-12-15 12:57:01 +11004219 mddev_unlock(mddev);
4220 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004221}
NeilBrown80ca3a42006-07-10 04:44:18 -07004222static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004223__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004224
NeilBrown6d7ff7382006-01-06 00:21:16 -08004225static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004226max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004227 return sprintf(page, "%d\n",
4228 atomic_read(&mddev->max_corr_read_errors));
4229}
4230
4231static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004232max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004233{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004234 unsigned int n;
4235 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004236
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004237 rv = kstrtouint(buf, 10, &n);
4238 if (rv < 0)
4239 return rv;
4240 atomic_set(&mddev->max_corr_read_errors, n);
4241 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004242}
4243
4244static struct md_sysfs_entry max_corr_read_errors =
4245__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4246 max_corrected_read_errors_store);
4247
4248static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004249null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004250{
4251 return -EINVAL;
4252}
4253
4254static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004255new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004256{
4257 /* buf must be %d:%d\n? giving major and minor numbers */
4258 /* The new device is added to the array.
4259 * If the array has a persistent superblock, we read the
4260 * superblock to initialise info and check validity.
4261 * Otherwise, only checking done is that in bind_rdev_to_array,
4262 * which mainly checks size.
4263 */
4264 char *e;
4265 int major = simple_strtoul(buf, &e, 10);
4266 int minor;
4267 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004268 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004269 int err;
4270
4271 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4272 return -EINVAL;
4273 minor = simple_strtoul(e+1, &e, 10);
4274 if (*e && *e != '\n')
4275 return -EINVAL;
4276 dev = MKDEV(major, minor);
4277 if (major != MAJOR(dev) ||
4278 minor != MINOR(dev))
4279 return -EOVERFLOW;
4280
NeilBrown67918752014-12-15 12:57:01 +11004281 flush_workqueue(md_misc_wq);
4282
4283 err = mddev_lock(mddev);
4284 if (err)
4285 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004286 if (mddev->persistent) {
4287 rdev = md_import_device(dev, mddev->major_version,
4288 mddev->minor_version);
4289 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004290 struct md_rdev *rdev0
4291 = list_entry(mddev->disks.next,
4292 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004293 err = super_types[mddev->major_version]
4294 .load_super(rdev, rdev0, mddev->minor_version);
4295 if (err < 0)
4296 goto out;
4297 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004298 } else if (mddev->external)
4299 rdev = md_import_device(dev, -2, -1);
4300 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004301 rdev = md_import_device(dev, -1, -1);
4302
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004303 if (IS_ERR(rdev)) {
4304 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004305 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004306 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004307 err = bind_rdev_to_array(rdev, mddev);
4308 out:
4309 if (err)
4310 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004311 mddev_unlock(mddev);
Alexey Obitotskiy5492c462017-07-28 15:49:25 +02004312 if (!err)
4313 md_new_event(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004314 return err ? err : len;
4315}
4316
4317static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004318__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004319
4320static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004321bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004322{
4323 char *end;
4324 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004325 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004326
NeilBrown67918752014-12-15 12:57:01 +11004327 err = mddev_lock(mddev);
4328 if (err)
4329 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004330 if (!mddev->bitmap)
4331 goto out;
4332 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4333 while (*buf) {
4334 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4335 if (buf == end) break;
4336 if (*end == '-') { /* range */
4337 buf = end + 1;
4338 end_chunk = simple_strtoul(buf, &end, 0);
4339 if (buf == end) break;
4340 }
4341 if (*end && !isspace(*end)) break;
4342 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004343 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004344 }
4345 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4346out:
NeilBrown67918752014-12-15 12:57:01 +11004347 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004348 return len;
4349}
4350
4351static struct md_sysfs_entry md_bitmap =
4352__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4353
4354static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004355size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004356{
Andre Noll58c0fed2009-03-31 14:33:13 +11004357 return sprintf(page, "%llu\n",
4358 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004359}
4360
NeilBrownfd01b882011-10-11 16:47:53 +11004361static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004362
4363static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004364size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004365{
4366 /* If array is inactive, we can reduce the component size, but
4367 * not increase it (except from 0).
4368 * If array is active, we can try an on-line resize
4369 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004370 sector_t sectors;
4371 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004372
Andre Noll58c0fed2009-03-31 14:33:13 +11004373 if (err < 0)
4374 return err;
NeilBrown67918752014-12-15 12:57:01 +11004375 err = mddev_lock(mddev);
4376 if (err)
4377 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004378 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004379 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004380 if (err == 0)
4381 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004382 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004383 if (mddev->dev_sectors == 0 ||
4384 mddev->dev_sectors > sectors)
4385 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004386 else
4387 err = -ENOSPC;
4388 }
NeilBrown67918752014-12-15 12:57:01 +11004389 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004390 return err ? err : len;
4391}
4392
4393static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004394__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004395
Masanari Iida83f0d772012-10-30 00:18:08 +09004396/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004397 * This is one of
4398 * 'none' for arrays with no metadata (good luck...)
4399 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004400 * or N.M for internally known formats
4401 */
4402static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004403metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004404{
4405 if (mddev->persistent)
4406 return sprintf(page, "%d.%d\n",
4407 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004408 else if (mddev->external)
4409 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004410 else
4411 return sprintf(page, "none\n");
4412}
4413
4414static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004415metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004416{
4417 int major, minor;
4418 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004419 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004420 /* Changing the details of 'external' metadata is
4421 * always permitted. Otherwise there must be
4422 * no devices attached to the array.
4423 */
NeilBrown67918752014-12-15 12:57:01 +11004424
4425 err = mddev_lock(mddev);
4426 if (err)
4427 return err;
4428 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004429 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4430 ;
4431 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004432 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004433
NeilBrown67918752014-12-15 12:57:01 +11004434 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004435 if (cmd_match(buf, "none")) {
4436 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004437 mddev->external = 0;
4438 mddev->major_version = 0;
4439 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004440 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004441 }
4442 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004443 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004444 if (namelen >= sizeof(mddev->metadata_type))
4445 namelen = sizeof(mddev->metadata_type)-1;
4446 strncpy(mddev->metadata_type, buf+9, namelen);
4447 mddev->metadata_type[namelen] = 0;
4448 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4449 mddev->metadata_type[--namelen] = 0;
4450 mddev->persistent = 0;
4451 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004452 mddev->major_version = 0;
4453 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004454 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004455 }
4456 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004457 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004458 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004459 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004460 buf = e+1;
4461 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004462 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004463 goto out_unlock;
4464 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004465 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004466 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004467 mddev->major_version = major;
4468 mddev->minor_version = minor;
4469 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004470 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004471 err = 0;
4472out_unlock:
4473 mddev_unlock(mddev);
4474 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004475}
4476
4477static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004478__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004479
NeilBrowna35b0d62006-01-06 00:20:49 -08004480static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004481action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004482{
NeilBrown7eec3142005-11-08 21:39:44 -08004483 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004484 unsigned long recovery = mddev->recovery;
4485 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004486 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004487 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4488 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4489 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004490 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004491 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4492 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004493 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004494 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004495 type = "check";
4496 else
4497 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004498 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004499 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004500 else if (mddev->reshape_position != MaxSector)
4501 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004502 }
4503 return sprintf(page, "%s\n", type);
4504}
4505
4506static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004507action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004508{
NeilBrown7eec3142005-11-08 21:39:44 -08004509 if (!mddev->pers || !mddev->pers->sync_request)
4510 return -EINVAL;
4511
NeilBrownb6a9ce62009-05-26 09:41:17 +10004512
4513 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004514 if (cmd_match(page, "frozen"))
4515 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4516 else
4517 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004518 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4519 mddev_lock(mddev) == 0) {
4520 flush_workqueue(md_misc_wq);
4521 if (mddev->sync_thread) {
4522 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004523 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004524 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004525 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004526 }
NeilBrown312045e2015-12-21 11:01:21 +11004527 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004528 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004529 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004530 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004531 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004532 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004533 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004534 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004535 int err;
4536 if (mddev->pers->start_reshape == NULL)
4537 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004538 err = mddev_lock(mddev);
4539 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004540 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4541 err = -EBUSY;
4542 else {
4543 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4544 err = mddev->pers->start_reshape(mddev);
4545 }
NeilBrown67918752014-12-15 12:57:01 +11004546 mddev_unlock(mddev);
4547 }
NeilBrown16484bf2006-03-27 01:18:13 -08004548 if (err)
4549 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004550 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004551 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004552 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004553 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004554 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004555 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004556 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004557 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4558 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004559 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004560 if (mddev->ro == 2) {
4561 /* A write to sync_action is enough to justify
4562 * canceling read-auto mode
4563 */
4564 mddev->ro = 0;
4565 md_wakeup_thread(mddev->sync_thread);
4566 }
NeilBrown03c902e2006-01-06 00:20:46 -08004567 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004568 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004569 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004570 return len;
4571}
4572
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004573static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004574__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004575
4576static ssize_t
4577last_sync_action_show(struct mddev *mddev, char *page)
4578{
4579 return sprintf(page, "%s\n", mddev->last_sync_action);
4580}
4581
4582static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4583
NeilBrown9d888832005-11-08 21:39:26 -08004584static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004585mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004586{
4587 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004588 (unsigned long long)
4589 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004590}
4591
NeilBrown80ca3a42006-07-10 04:44:18 -07004592static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004593
NeilBrown88202a02006-01-06 00:21:36 -08004594static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004595sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004596{
4597 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4598 mddev->sync_speed_min ? "local": "system");
4599}
4600
4601static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004602sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004603{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004604 unsigned int min;
4605 int rv;
4606
NeilBrown88202a02006-01-06 00:21:36 -08004607 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004608 min = 0;
4609 } else {
4610 rv = kstrtouint(buf, 10, &min);
4611 if (rv < 0)
4612 return rv;
4613 if (min == 0)
4614 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004615 }
NeilBrown88202a02006-01-06 00:21:36 -08004616 mddev->sync_speed_min = min;
4617 return len;
4618}
4619
4620static struct md_sysfs_entry md_sync_min =
4621__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4622
4623static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004624sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004625{
4626 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4627 mddev->sync_speed_max ? "local": "system");
4628}
4629
4630static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004631sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004632{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004633 unsigned int max;
4634 int rv;
4635
NeilBrown88202a02006-01-06 00:21:36 -08004636 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004637 max = 0;
4638 } else {
4639 rv = kstrtouint(buf, 10, &max);
4640 if (rv < 0)
4641 return rv;
4642 if (max == 0)
4643 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004644 }
NeilBrown88202a02006-01-06 00:21:36 -08004645 mddev->sync_speed_max = max;
4646 return len;
4647}
4648
4649static struct md_sysfs_entry md_sync_max =
4650__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4651
Iustin Popd7f3d292007-10-16 23:30:54 -07004652static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004653degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004654{
4655 return sprintf(page, "%d\n", mddev->degraded);
4656}
4657static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004658
4659static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004660sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004661{
4662 return sprintf(page, "%d\n", mddev->parallel_resync);
4663}
4664
4665static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004666sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004667{
4668 long n;
4669
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004670 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004671 return -EINVAL;
4672
4673 if (n != 0 && n != 1)
4674 return -EINVAL;
4675
4676 mddev->parallel_resync = n;
4677
4678 if (mddev->sync_thread)
4679 wake_up(&resync_wait);
4680
4681 return len;
4682}
4683
4684/* force parallel resync, even with shared block devices */
4685static struct md_sysfs_entry md_sync_force_parallel =
4686__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4687 sync_force_parallel_show, sync_force_parallel_store);
4688
4689static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004690sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004691{
4692 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004693 if (mddev->curr_resync == 0)
4694 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004695 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4696 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004697 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004698 db = resync - mddev->resync_mark_cnt;
4699 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004700}
4701
NeilBrown80ca3a42006-07-10 04:44:18 -07004702static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004703
4704static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004705sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004706{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004707 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004708
NeilBrownacb180b2009-04-14 16:28:34 +10004709 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4710 return sprintf(page, "none\n");
4711
NeilBrown72f36d52012-10-11 14:25:57 +11004712 if (mddev->curr_resync == 1 ||
4713 mddev->curr_resync == 2)
4714 return sprintf(page, "delayed\n");
4715
NeilBrownc804cde2012-05-21 09:28:33 +10004716 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4717 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004718 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004719 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004720 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004721
NeilBrownacb180b2009-04-14 16:28:34 +10004722 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004723 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004724}
4725
NeilBrown750f1992014-09-30 08:53:05 +10004726static struct md_sysfs_entry md_sync_completed =
4727 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004728
NeilBrowne464eaf2006-03-27 01:18:14 -08004729static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004730min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004731{
4732 return sprintf(page, "%llu\n",
4733 (unsigned long long)mddev->resync_min);
4734}
4735static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004736min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004737{
4738 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004739 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004740
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004741 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004742 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004743
4744 spin_lock(&mddev->lock);
4745 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004746 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004747 goto out_unlock;
4748
4749 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004750 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004751 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004752
NeilBrown50c37b12015-03-23 17:36:38 +11004753 /* Round down to multiple of 4K for safety */
4754 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004755 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004756
NeilBrown23da4222014-12-15 12:57:01 +11004757out_unlock:
4758 spin_unlock(&mddev->lock);
4759 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004760}
4761
4762static struct md_sysfs_entry md_min_sync =
4763__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4764
4765static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004766max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004767{
4768 if (mddev->resync_max == MaxSector)
4769 return sprintf(page, "max\n");
4770 else
4771 return sprintf(page, "%llu\n",
4772 (unsigned long long)mddev->resync_max);
4773}
4774static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004775max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004776{
NeilBrown23da4222014-12-15 12:57:01 +11004777 int err;
4778 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004779 if (strncmp(buf, "max", 3) == 0)
4780 mddev->resync_max = MaxSector;
4781 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004782 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004783 int chunk;
4784
4785 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004786 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004787 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004788 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004789 goto out_unlock;
4790
4791 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004792 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004793 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004794 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004795 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004796
4797 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004798 chunk = mddev->chunk_sectors;
4799 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004800 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004801
4802 err = -EINVAL;
4803 if (sector_div(temp, chunk))
4804 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004805 }
4806 mddev->resync_max = max;
4807 }
4808 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004809 err = 0;
4810out_unlock:
4811 spin_unlock(&mddev->lock);
4812 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004813}
4814
4815static struct md_sysfs_entry md_max_sync =
4816__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4817
4818static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004819suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004820{
4821 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4822}
4823
4824static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004825suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004826{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004827 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004828 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004829
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004830 err = kstrtoull(buf, 10, &new);
4831 if (err < 0)
4832 return err;
4833 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004834 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004835
NeilBrown67918752014-12-15 12:57:01 +11004836 err = mddev_lock(mddev);
4837 if (err)
4838 return err;
4839 err = -EINVAL;
4840 if (mddev->pers == NULL ||
4841 mddev->pers->quiesce == NULL)
4842 goto unlock;
4843 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004844 mddev->suspend_lo = new;
4845 if (new >= old)
4846 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004847 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004848 else {
4849 /* Expanding suspended region - need to wait */
4850 mddev->pers->quiesce(mddev, 1);
4851 mddev->pers->quiesce(mddev, 0);
4852 }
NeilBrown67918752014-12-15 12:57:01 +11004853 err = 0;
4854unlock:
4855 mddev_unlock(mddev);
4856 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004857}
4858static struct md_sysfs_entry md_suspend_lo =
4859__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4860
NeilBrowne464eaf2006-03-27 01:18:14 -08004861static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004862suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004863{
4864 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4865}
4866
4867static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004868suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004869{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004870 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004871 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004872
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004873 err = kstrtoull(buf, 10, &new);
4874 if (err < 0)
4875 return err;
4876 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004877 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004878
NeilBrown67918752014-12-15 12:57:01 +11004879 err = mddev_lock(mddev);
4880 if (err)
4881 return err;
4882 err = -EINVAL;
4883 if (mddev->pers == NULL ||
4884 mddev->pers->quiesce == NULL)
4885 goto unlock;
4886 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004887 mddev->suspend_hi = new;
4888 if (new <= old)
4889 /* Shrinking suspended region */
4890 mddev->pers->quiesce(mddev, 2);
4891 else {
4892 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004893 mddev->pers->quiesce(mddev, 1);
4894 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004895 }
NeilBrown67918752014-12-15 12:57:01 +11004896 err = 0;
4897unlock:
4898 mddev_unlock(mddev);
4899 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004900}
4901static struct md_sysfs_entry md_suspend_hi =
4902__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4903
NeilBrown08a02ec2007-05-09 02:35:38 -07004904static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004905reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004906{
4907 if (mddev->reshape_position != MaxSector)
4908 return sprintf(page, "%llu\n",
4909 (unsigned long long)mddev->reshape_position);
4910 strcpy(page, "none\n");
4911 return 5;
4912}
4913
4914static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004915reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004916{
NeilBrownc6563a82012-05-21 09:27:00 +10004917 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004918 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004919 int err;
NeilBrown67918752014-12-15 12:57:01 +11004920
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004921 err = kstrtoull(buf, 10, &new);
4922 if (err < 0)
4923 return err;
4924 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004925 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004926 err = mddev_lock(mddev);
4927 if (err)
4928 return err;
4929 err = -EBUSY;
4930 if (mddev->pers)
4931 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004932 mddev->reshape_position = new;
4933 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004934 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004935 mddev->new_level = mddev->level;
4936 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004937 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004938 rdev_for_each(rdev, mddev)
4939 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004940 err = 0;
4941unlock:
4942 mddev_unlock(mddev);
4943 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004944}
4945
4946static struct md_sysfs_entry md_reshape_position =
4947__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4948 reshape_position_store);
4949
Dan Williamsb522adc2009-03-31 15:00:31 +11004950static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004951reshape_direction_show(struct mddev *mddev, char *page)
4952{
4953 return sprintf(page, "%s\n",
4954 mddev->reshape_backwards ? "backwards" : "forwards");
4955}
4956
4957static ssize_t
4958reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4959{
4960 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004961 int err;
4962
NeilBrown2c810cd2012-05-21 09:27:00 +10004963 if (cmd_match(buf, "forwards"))
4964 backwards = 0;
4965 else if (cmd_match(buf, "backwards"))
4966 backwards = 1;
4967 else
4968 return -EINVAL;
4969 if (mddev->reshape_backwards == backwards)
4970 return len;
4971
NeilBrown67918752014-12-15 12:57:01 +11004972 err = mddev_lock(mddev);
4973 if (err)
4974 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004975 /* check if we are allowed to change */
4976 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004977 err = -EBUSY;
4978 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004979 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004980 err = -EINVAL;
4981 else
4982 mddev->reshape_backwards = backwards;
4983 mddev_unlock(mddev);
4984 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004985}
4986
4987static struct md_sysfs_entry md_reshape_direction =
4988__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4989 reshape_direction_store);
4990
4991static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004992array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004993{
4994 if (mddev->external_size)
4995 return sprintf(page, "%llu\n",
4996 (unsigned long long)mddev->array_sectors/2);
4997 else
4998 return sprintf(page, "default\n");
4999}
5000
5001static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005002array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11005003{
5004 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11005005 int err;
5006
5007 err = mddev_lock(mddev);
5008 if (err)
5009 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11005010
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005011 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08005012 if (mddev_is_clustered(mddev)) {
5013 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005014 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08005015 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005016
Dan Williamsb522adc2009-03-31 15:00:31 +11005017 if (strncmp(buf, "default", 7) == 0) {
5018 if (mddev->pers)
5019 sectors = mddev->pers->size(mddev, 0, 0);
5020 else
5021 sectors = mddev->array_sectors;
5022
5023 mddev->external_size = 0;
5024 } else {
5025 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11005026 err = -EINVAL;
5027 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5028 err = -E2BIG;
5029 else
5030 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11005031 }
5032
NeilBrown67918752014-12-15 12:57:01 +11005033 if (!err) {
5034 mddev->array_sectors = sectors;
5035 if (mddev->pers) {
5036 set_capacity(mddev->gendisk, mddev->array_sectors);
5037 revalidate_disk(mddev->gendisk);
5038 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11005039 }
NeilBrown67918752014-12-15 12:57:01 +11005040 mddev_unlock(mddev);
5041 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11005042}
5043
5044static struct md_sysfs_entry md_array_size =
5045__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5046 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005047
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005048static ssize_t
5049consistency_policy_show(struct mddev *mddev, char *page)
5050{
5051 int ret;
5052
5053 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5054 ret = sprintf(page, "journal\n");
5055 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5056 ret = sprintf(page, "ppl\n");
5057 } else if (mddev->bitmap) {
5058 ret = sprintf(page, "bitmap\n");
5059 } else if (mddev->pers) {
5060 if (mddev->pers->sync_request)
5061 ret = sprintf(page, "resync\n");
5062 else
5063 ret = sprintf(page, "none\n");
5064 } else {
5065 ret = sprintf(page, "unknown\n");
5066 }
5067
5068 return ret;
5069}
5070
5071static ssize_t
5072consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5073{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005074 int err = 0;
5075
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005076 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005077 if (mddev->pers->change_consistency_policy)
5078 err = mddev->pers->change_consistency_policy(mddev, buf);
5079 else
5080 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005081 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5082 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005083 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005084 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005085 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005086
5087 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005088}
5089
5090static struct md_sysfs_entry md_consistency_policy =
5091__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5092 consistency_policy_store);
5093
NeilBrowneae17012005-11-08 21:39:23 -08005094static struct attribute *md_default_attrs[] = {
5095 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005096 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005097 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005098 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005099 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005100 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005101 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005102 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005103 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005104 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005105 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005106 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005107 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005108 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005109 &md_consistency_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005110 NULL,
5111};
5112
5113static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005114 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005115 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005116 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005117 &md_sync_min.attr,
5118 &md_sync_max.attr,
5119 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005120 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005121 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005122 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005123 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005124 &md_suspend_lo.attr,
5125 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005126 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005127 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005128 NULL,
5129};
NeilBrown411036f2005-11-08 21:39:40 -08005130static struct attribute_group md_redundancy_group = {
5131 .name = NULL,
5132 .attrs = md_redundancy_attrs,
5133};
5134
NeilBrowneae17012005-11-08 21:39:23 -08005135static ssize_t
5136md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5137{
5138 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005139 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005140 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005141
5142 if (!entry->show)
5143 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005144 spin_lock(&all_mddevs_lock);
5145 if (list_empty(&mddev->all_mddevs)) {
5146 spin_unlock(&all_mddevs_lock);
5147 return -EBUSY;
5148 }
5149 mddev_get(mddev);
5150 spin_unlock(&all_mddevs_lock);
5151
NeilBrownb7b17c92014-12-15 12:56:59 +11005152 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005153 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005154 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005155}
5156
5157static ssize_t
5158md_attr_store(struct kobject *kobj, struct attribute *attr,
5159 const char *page, size_t length)
5160{
5161 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005162 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005163 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005164
5165 if (!entry->store)
5166 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005167 if (!capable(CAP_SYS_ADMIN))
5168 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005169 spin_lock(&all_mddevs_lock);
5170 if (list_empty(&mddev->all_mddevs)) {
5171 spin_unlock(&all_mddevs_lock);
5172 return -EBUSY;
5173 }
5174 mddev_get(mddev);
5175 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005176 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005177 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005178 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005179}
5180
5181static void md_free(struct kobject *ko)
5182{
NeilBrownfd01b882011-10-11 16:47:53 +11005183 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005184
5185 if (mddev->sysfs_state)
5186 sysfs_put(mddev->sysfs_state);
5187
NeilBrown6cd18e72015-04-27 14:12:22 +10005188 if (mddev->queue)
5189 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11005190 if (mddev->gendisk) {
5191 del_gendisk(mddev->gendisk);
5192 put_disk(mddev->gendisk);
5193 }
NeilBrown4ad23a972017-03-15 14:05:14 +11005194 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005195
NeilBrowneae17012005-11-08 21:39:23 -08005196 kfree(mddev);
5197}
5198
Emese Revfy52cf25d2010-01-19 02:58:23 +01005199static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005200 .show = md_attr_show,
5201 .store = md_attr_store,
5202};
5203static struct kobj_type md_ktype = {
5204 .release = md_free,
5205 .sysfs_ops = &md_sysfs_ops,
5206 .default_attrs = md_default_attrs,
5207};
5208
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209int mdp_major = 0;
5210
Dan Williams5fd3a172009-03-04 00:57:25 -07005211static void mddev_delayed_delete(struct work_struct *ws)
5212{
NeilBrownfd01b882011-10-11 16:47:53 +11005213 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005214
NeilBrown43a70502009-12-14 12:49:55 +11005215 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005216 kobject_del(&mddev->kobj);
5217 kobject_put(&mddev->kobj);
5218}
5219
NeilBrown4ad23a972017-03-15 14:05:14 +11005220static void no_op(struct percpu_ref *r) {}
5221
NeilBrowna415c0f2017-06-05 16:05:13 +10005222int mddev_init_writes_pending(struct mddev *mddev)
5223{
5224 if (mddev->writes_pending.percpu_count_ptr)
5225 return 0;
5226 if (percpu_ref_init(&mddev->writes_pending, no_op, 0, GFP_KERNEL) < 0)
5227 return -ENOMEM;
5228 /* We want to start with the refcount at zero */
5229 percpu_ref_put(&mddev->writes_pending);
5230 return 0;
5231}
5232EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5233
NeilBrownefeb53c2009-01-09 08:31:10 +11005234static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235{
NeilBrown039b7222017-04-12 16:26:13 +10005236 /*
5237 * If dev is zero, name is the name of a device to allocate with
5238 * an arbitrary minor number. It will be "md_???"
5239 * If dev is non-zero it must be a device number with a MAJOR of
5240 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5241 * the device is being created by opening a node in /dev.
5242 * If "name" is not NULL, the device is being created by
5243 * writing to /sys/module/md_mod/parameters/new_array.
5244 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005245 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005246 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005248 int partitioned;
5249 int shift;
5250 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005251 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
5253 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005254 return -ENODEV;
5255
5256 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5257 shift = partitioned ? MdpMinorShift : 0;
5258 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259
Tejun Heoe804ac72010-10-15 15:36:08 +02005260 /* wait for any previous instance of this device to be
5261 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005262 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005263 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005264
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005265 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005266 error = -EEXIST;
5267 if (mddev->gendisk)
5268 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005269
NeilBrown039b7222017-04-12 16:26:13 +10005270 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005271 /* Need to ensure that 'name' is not a duplicate.
5272 */
NeilBrownfd01b882011-10-11 16:47:53 +11005273 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005274 spin_lock(&all_mddevs_lock);
5275
5276 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5277 if (mddev2->gendisk &&
5278 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5279 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005280 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005281 }
5282 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 }
NeilBrown039b7222017-04-12 16:26:13 +10005284 if (name && dev)
5285 /*
5286 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5287 */
5288 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005289
NeilBrown0909dc42009-07-01 12:27:21 +10005290 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005291 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005292 if (!mddev->queue)
5293 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005294 mddev->queue->queuedata = mddev;
5295
NeilBrown409c57f2009-03-31 14:39:39 +11005296 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005297 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005298
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 disk = alloc_disk(1 << shift);
5300 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005301 blk_cleanup_queue(mddev->queue);
5302 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005303 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005305 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005307 if (name)
5308 strcpy(disk->disk_name, name);
5309 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005311 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 disk->fops = &md_fops;
5314 disk->private_data = mddev;
5315 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005316 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005317 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005318 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005319 * remove it now.
5320 */
5321 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005323 /* As soon as we call add_disk(), another thread could get
5324 * through to md_open, so make sure it doesn't get too far
5325 */
5326 mutex_lock(&mddev->open_mutex);
5327 add_disk(disk);
5328
Tejun Heoed9e1982008-08-25 19:56:05 +09005329 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5330 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005331 if (error) {
5332 /* This isn't possible, but as kobject_init_and_add is marked
5333 * __must_check, we must do something with the result
5334 */
NeilBrown9d487392016-11-02 14:16:49 +11005335 pr_debug("md: cannot register %s/md - name in use\n",
5336 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005337 error = 0;
5338 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005339 if (mddev->kobj.sd &&
5340 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005341 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005342 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005343 abort:
5344 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005345 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005346 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005347 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005348 }
NeilBrownd3374822009-01-09 08:31:10 +11005349 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005350 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005351}
5352
5353static struct kobject *md_probe(dev_t dev, int *part, void *data)
5354{
NeilBrown78b63502017-04-12 16:26:13 +10005355 if (create_on_open)
5356 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 return NULL;
5358}
5359
NeilBrownefeb53c2009-01-09 08:31:10 +11005360static int add_named_array(const char *val, struct kernel_param *kp)
5361{
NeilBrown039b7222017-04-12 16:26:13 +10005362 /*
5363 * val must be "md_*" or "mdNNN".
5364 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005365 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005366 * For "mdNNN" we allocate an array with the minor number NNN
5367 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005368 */
5369 int len = strlen(val);
5370 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005371 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005372
5373 while (len && val[len-1] == '\n')
5374 len--;
5375 if (len >= DISK_NAME_LEN)
5376 return -E2BIG;
5377 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005378 if (strncmp(buf, "md_", 3) == 0)
5379 return md_alloc(0, buf);
5380 if (strncmp(buf, "md", 2) == 0 &&
5381 isdigit(buf[2]) &&
5382 kstrtoul(buf+2, 10, &devnum) == 0 &&
5383 devnum <= MINORMASK)
5384 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5385
5386 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005387}
5388
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389static void md_safemode_timeout(unsigned long data)
5390{
NeilBrownfd01b882011-10-11 16:47:53 +11005391 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
NeilBrown4ad23a972017-03-15 14:05:14 +11005393 mddev->safemode = 1;
5394 if (mddev->external)
5395 sysfs_notify_dirent_safe(mddev->sysfs_state);
5396
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 md_wakeup_thread(mddev->thread);
5398}
5399
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005400static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
NeilBrownfd01b882011-10-11 16:47:53 +11005402int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403{
NeilBrown2604b702006-01-06 00:20:36 -08005404 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005405 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005406 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407
NeilBrowna757e642005-04-16 15:26:42 -07005408 if (list_empty(&mddev->disks))
5409 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411
5412 if (mddev->pers)
5413 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005414 /* Cannot run until previous stop completes properly */
5415 if (mddev->sysfs_active)
5416 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005417
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 /*
5419 * Analyze all RAID superblock(s)
5420 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005421 if (!mddev->raid_disks) {
5422 if (!mddev->persistent)
5423 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005424 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
NeilBrownd9d166c2006-01-06 00:20:51 -08005427 if (mddev->level != LEVEL_NONE)
5428 request_module("md-level-%d", mddev->level);
5429 else if (mddev->clevel[0])
5430 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431
5432 /*
5433 * Drop all container device buffers, from now on
5434 * the only valid external interface is through the md
5435 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005437 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005438 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 continue;
5440 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005441 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005442 if (mddev->ro != 1 &&
5443 (bdev_read_only(rdev->bdev) ||
5444 bdev_read_only(rdev->meta_bdev))) {
5445 mddev->ro = 1;
5446 if (mddev->gendisk)
5447 set_disk_ro(mddev->gendisk, 1);
5448 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005449
5450 /* perform some consistency tests on the device.
5451 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005452 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005453 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005454 if (rdev->meta_bdev) {
5455 /* Nothing to check */;
5456 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005457 if (mddev->dev_sectors &&
5458 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005459 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005460 pr_warn("md: %s: data overlaps metadata\n",
5461 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005462 return -EINVAL;
5463 }
5464 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005465 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005466 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005467 pr_warn("md: %s: metadata overlaps data\n",
5468 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005469 return -EINVAL;
5470 }
5471 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005472 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 }
5474
Ming Lei10273172017-02-14 23:29:00 +08005475 if (mddev->bio_set == NULL) {
NeilBrown011067b2017-06-18 14:38:57 +10005476 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
Ming Lei10273172017-02-14 23:29:00 +08005477 if (!mddev->bio_set)
5478 return -ENOMEM;
5479 }
NeilBrown5a850712017-06-21 09:12:21 +10005480 if (mddev->sync_set == NULL) {
Linus Torvalds026d15f2017-07-08 12:50:18 -07005481 mddev->sync_set = bioset_create(BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
NeilBrown5a850712017-06-21 09:12:21 +10005482 if (!mddev->sync_set)
5483 return -ENOMEM;
5484 }
NeilBrowna167f662010-10-26 18:31:13 +11005485
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005487 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005488 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005490 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005491 pr_warn("md: personality for level %d is not loaded!\n",
5492 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005493 else
NeilBrown9d487392016-11-02 14:16:49 +11005494 pr_warn("md: personality for level %s is not loaded!\n",
5495 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 return -EINVAL;
5497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005499 if (mddev->level != pers->level) {
5500 mddev->level = pers->level;
5501 mddev->new_level = pers->level;
5502 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005503 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
NeilBrownf6705572006-03-27 01:18:11 -08005505 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005506 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005507 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005508 module_put(pers->owner);
5509 return -EINVAL;
5510 }
5511
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005512 if (pers->sync_request) {
5513 /* Warn if this is a potentially silly
5514 * configuration.
5515 */
5516 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005517 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005518 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005519
NeilBrowndafb20f2012-03-19 12:46:39 +11005520 rdev_for_each(rdev, mddev)
5521 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005522 if (rdev < rdev2 &&
5523 rdev->bdev->bd_contains ==
5524 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005525 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5526 mdname(mddev),
5527 bdevname(rdev->bdev,b),
5528 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005529 warned = 1;
5530 }
5531 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005532
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005533 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005534 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005535 }
5536
NeilBrown657390d2005-08-26 18:34:16 -07005537 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005538 /* may be over-ridden by personality */
5539 mddev->resync_max_sectors = mddev->dev_sectors;
5540
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005541 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542
NeilBrown0f9552b52009-12-30 12:08:50 +11005543 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005544 mddev->ro = 2; /* read-only, but switch on first write */
5545
Song Liua85dd7b2017-01-23 17:12:57 -08005546 /*
5547 * NOTE: some pers->run(), for example r5l_recovery_log(), wakes
5548 * up mddev->thread. It is important to initialize critical
5549 * resources for mddev->thread BEFORE calling pers->run().
5550 */
NeilBrown36d091f2014-12-15 12:56:58 +11005551 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005552 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005553 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005554 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005555 WARN_ONCE(!mddev->external_size,
5556 "%s: default size too small, but 'external_size' not in effect?\n",
5557 __func__);
5558 pr_warn("md: invalid array_size %llu > default size %llu\n",
5559 (unsigned long long)mddev->array_sectors / 2,
5560 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005561 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005562 }
NeilBrown36d091f2014-12-15 12:56:58 +11005563 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005564 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005565 struct bitmap *bitmap;
5566
5567 bitmap = bitmap_create(mddev, -1);
5568 if (IS_ERR(bitmap)) {
5569 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005570 pr_warn("%s: failed to create bitmap (%d)\n",
5571 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005572 } else
5573 mddev->bitmap = bitmap;
5574
NeilBrownb15c2e52006-01-06 00:20:16 -08005575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005577 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005578 if (mddev->private)
5579 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005580 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005581 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005582 bitmap_destroy(mddev);
5583 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 }
NeilBrown5c675f82014-12-15 12:56:56 +11005585 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005586 bool nonrot = true;
5587
5588 rdev_for_each(rdev, mddev) {
5589 if (rdev->raid_disk >= 0 &&
5590 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5591 nonrot = false;
5592 break;
5593 }
5594 }
5595 if (mddev->degraded)
5596 nonrot = false;
5597 if (nonrot)
5598 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5599 else
5600 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005601 mddev->queue->backing_dev_info->congested_data = mddev;
5602 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005603 }
NeilBrown36d091f2014-12-15 12:56:58 +11005604 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005605 if (mddev->kobj.sd &&
5606 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005607 pr_warn("md: cannot register extra attributes for %s\n",
5608 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005609 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005610 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005611 mddev->ro = 0;
5612
Robert Becker1e509152009-12-14 12:49:58 +11005613 atomic_set(&mddev->max_corr_read_errors,
5614 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005616 if (mddev_is_clustered(mddev))
5617 mddev->safemode_delay = 0;
5618 else
5619 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005621 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005622 spin_lock(&mddev->lock);
5623 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005624 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005625 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005626 if (rdev->raid_disk >= 0)
5627 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005628 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005629
NeilBrowna4a3d262015-07-17 11:57:30 +10005630 if (mddev->degraded && !mddev->ro)
5631 /* This ensures that recovering status is reported immediately
5632 * via sysfs - until a lack of spares is confirmed.
5633 */
5634 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005636
Shaohua Li29530792016-12-08 15:48:19 -08005637 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07005638 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639
NeilBrownd7603b72006-01-06 00:20:30 -08005640 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005641 sysfs_notify_dirent_safe(mddev->sysfs_state);
5642 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005643 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 return 0;
5645}
NeilBrown390ee602010-06-01 19:37:27 +10005646EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647
NeilBrownfd01b882011-10-11 16:47:53 +11005648static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005649{
5650 int err;
5651
5652 err = md_run(mddev);
5653 if (err)
5654 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005655 err = bitmap_load(mddev);
5656 if (err) {
5657 bitmap_destroy(mddev);
5658 goto out;
5659 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005660
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005661 if (mddev_is_clustered(mddev))
5662 md_allow_write(mddev);
5663
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005664 md_wakeup_thread(mddev->thread);
5665 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5666
NeilBrownfe60b012010-03-29 11:10:42 +11005667 set_capacity(mddev->gendisk, mddev->array_sectors);
5668 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005669 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005670 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5671out:
5672 return err;
5673}
5674
NeilBrownfd01b882011-10-11 16:47:53 +11005675static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676{
5677 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10005678 struct md_rdev *rdev;
5679 bool has_journal = false;
5680 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
Andre Noll80fab1d2008-07-11 22:02:21 +10005682 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005684 return -ENXIO;
5685 if (!mddev->pers)
5686 return -EINVAL;
5687 if (!mddev->ro)
5688 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005689
NeilBrown97b20ef2017-04-13 08:53:48 +10005690 rcu_read_lock();
5691 rdev_for_each_rcu(rdev, mddev) {
5692 if (test_bit(Journal, &rdev->flags) &&
5693 !test_bit(Faulty, &rdev->flags))
5694 has_journal = true;
5695 if (bdev_read_only(rdev->bdev))
5696 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07005697 }
NeilBrown97b20ef2017-04-13 08:53:48 +10005698 rcu_read_unlock();
5699 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
5700 /* Don't restart rw with journal missing/faulty */
5701 return -EINVAL;
5702 if (has_readonly)
5703 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07005704
Andre Noll80fab1d2008-07-11 22:02:21 +10005705 mddev->safemode = 0;
5706 mddev->ro = 0;
5707 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11005708 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10005709 /* Kick recovery or resync if necessary */
5710 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5711 md_wakeup_thread(mddev->thread);
5712 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005713 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715}
5716
NeilBrownfd01b882011-10-11 16:47:53 +11005717static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005718{
5719 mddev->array_sectors = 0;
5720 mddev->external_size = 0;
5721 mddev->dev_sectors = 0;
5722 mddev->raid_disks = 0;
5723 mddev->recovery_cp = 0;
5724 mddev->resync_min = 0;
5725 mddev->resync_max = MaxSector;
5726 mddev->reshape_position = MaxSector;
5727 mddev->external = 0;
5728 mddev->persistent = 0;
5729 mddev->level = LEVEL_NONE;
5730 mddev->clevel[0] = 0;
5731 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08005732 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005733 mddev->ro = 0;
5734 mddev->metadata_type[0] = 0;
5735 mddev->chunk_sectors = 0;
5736 mddev->ctime = mddev->utime = 0;
5737 mddev->layout = 0;
5738 mddev->max_disks = 0;
5739 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005740 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005741 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005742 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005743 mddev->new_level = LEVEL_NONE;
5744 mddev->new_layout = 0;
5745 mddev->new_chunk_sectors = 0;
5746 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005747 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005748 mddev->suspend_lo = mddev->suspend_hi = 0;
5749 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5750 mddev->recovery = 0;
5751 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005752 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005753 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005754 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005755 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005756 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005757 mddev->bitmap_info.offset = 0;
5758 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005759 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005760 mddev->bitmap_info.chunksize = 0;
5761 mddev->bitmap_info.daemon_sleep = 0;
5762 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005763 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005764}
5765
NeilBrownfd01b882011-10-11 16:47:53 +11005766static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005767{
NeilBrown6b6204e2013-05-09 09:48:30 +10005768 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005769 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005770 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005771 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005772 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005773 }
5774
5775 del_timer_sync(&mddev->safemode_timer);
5776
Shaohua Li034e33f2016-11-21 10:29:19 -08005777 if (mddev->pers && mddev->pers->quiesce) {
5778 mddev->pers->quiesce(mddev, 1);
5779 mddev->pers->quiesce(mddev, 0);
5780 }
NeilBrowna047e122010-03-29 12:07:53 +11005781 bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005782
NeilBrownb6d428c2013-04-24 11:42:42 +10005783 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005784 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08005785 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11005786 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005787 if (!mddev_is_clustered(mddev))
5788 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005789 md_update_sb(mddev, 1);
5790 }
5791}
NeilBrowndefad612011-01-14 09:14:33 +11005792
NeilBrownfd01b882011-10-11 16:47:53 +11005793void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005794{
NeilBrown29f097c2013-11-14 17:54:51 +11005795 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005796 __md_stop_writes(mddev);
5797 mddev_unlock(mddev);
5798}
NeilBrown390ee602010-06-01 19:37:27 +10005799EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005800
NeilBrown5aa61f42014-12-15 12:56:57 +11005801static void mddev_detach(struct mddev *mddev)
5802{
Guoqing Jiang48df4982017-03-14 09:40:20 +08005803 bitmap_wait_behind_writes(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005804 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005805 mddev->pers->quiesce(mddev, 1);
5806 mddev->pers->quiesce(mddev, 0);
5807 }
5808 md_unregister_thread(&mddev->thread);
5809 if (mddev->queue)
5810 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5811}
5812
NeilBrown5eff3c42012-11-19 10:47:48 +11005813static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005814{
NeilBrown36d091f2014-12-15 12:56:58 +11005815 struct md_personality *pers = mddev->pers;
Guoqing Jiang48df4982017-03-14 09:40:20 +08005816 bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11005817 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005818 /* Ensure ->event_work is done */
5819 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005820 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005821 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005822 spin_unlock(&mddev->lock);
5823 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005824 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005825 if (pers->sync_request && mddev->to_remove == NULL)
5826 mddev->to_remove = &md_redundancy_group;
5827 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005828 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005829}
NeilBrown5eff3c42012-11-19 10:47:48 +11005830
5831void md_stop(struct mddev *mddev)
5832{
5833 /* stop the array and free an attached data structures.
5834 * This is called from dm-raid
5835 */
5836 __md_stop(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005837 if (mddev->bio_set)
5838 bioset_free(mddev->bio_set);
5839}
5840
NeilBrown390ee602010-06-01 19:37:27 +10005841EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005842
NeilBrowna05b7ea2012-07-19 15:59:18 +10005843static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005844{
5845 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005846 int did_freeze = 0;
5847
5848 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5849 did_freeze = 1;
5850 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5851 md_wakeup_thread(mddev->thread);
5852 }
NeilBrownf851b602014-12-11 10:02:10 +11005853 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005854 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005855 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005856 /* Thread might be blocked waiting for metadata update
5857 * which will now never happen */
5858 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005859
Shaohua Li29530792016-12-08 15:48:19 -08005860 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10005861 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005862 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005863 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5864 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005865 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005866 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005867 mddev_lock_nointr(mddev);
5868
NeilBrowna4bd82d2010-03-29 13:23:10 +11005869 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005870 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005871 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005872 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005873 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005874 if (did_freeze) {
5875 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005876 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005877 md_wakeup_thread(mddev->thread);
5878 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005879 err = -EBUSY;
5880 goto out;
5881 }
5882 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005883 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005884
5885 err = -ENXIO;
5886 if (mddev->ro==1)
5887 goto out;
5888 mddev->ro = 1;
5889 set_disk_ro(mddev->gendisk, 1);
5890 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005891 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5892 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005893 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005894 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005895 }
5896out:
5897 mutex_unlock(&mddev->open_mutex);
5898 return err;
5899}
5900
NeilBrown9e653b62006-06-26 00:27:58 -07005901/* mode:
5902 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005903 * 2 - stop but do not disassemble array
5904 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005905static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005906 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005909 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005910 int did_freeze = 0;
5911
5912 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5913 did_freeze = 1;
5914 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5915 md_wakeup_thread(mddev->thread);
5916 }
NeilBrownf851b602014-12-11 10:02:10 +11005917 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005918 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005919 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005920 /* Thread might be blocked waiting for metadata update
5921 * which will now never happen */
5922 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005923
NeilBrown30b8feb2013-11-14 15:16:17 +11005924 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005925 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5926 !test_bit(MD_RECOVERY_RUNNING,
5927 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005928 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929
NeilBrownc8c00a62009-08-10 12:50:52 +10005930 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005931 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005932 mddev->sysfs_active ||
5933 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005934 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005935 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005936 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005937 if (did_freeze) {
5938 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005939 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005940 md_wakeup_thread(mddev->thread);
5941 }
NeilBrown260fa032013-08-27 16:44:13 +10005942 return -EBUSY;
5943 }
NeilBrown6e17b022010-08-07 21:41:19 +10005944 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005945 if (mddev->ro)
5946 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005947
NeilBrowndefad612011-01-14 09:14:33 +11005948 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005949 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01005950 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005951
NeilBrowna4bd82d2010-03-29 13:23:10 +11005952 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005953 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005954
NeilBrowndafb20f2012-03-19 12:46:39 +11005955 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005956 if (rdev->raid_disk >= 0)
5957 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005958
NeilBrowna4bd82d2010-03-29 13:23:10 +11005959 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005960 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005961 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005962 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005963
NeilBrowna4bd82d2010-03-29 13:23:10 +11005964 if (mddev->ro)
5965 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005966 } else
5967 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 /*
5969 * Free resources if final stop
5970 */
NeilBrown9e653b62006-06-26 00:27:58 -07005971 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005972 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973
NeilBrownc3d97142009-12-14 12:49:52 +11005974 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005975 struct file *f = mddev->bitmap_info.file;
5976 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005977 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005978 spin_unlock(&mddev->lock);
5979 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005980 }
NeilBrownc3d97142009-12-14 12:49:52 +11005981 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005982
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 export_array(mddev);
5984
NeilBrown6177b472010-03-29 11:37:13 +11005985 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11005986 if (mddev->hold_active == UNTIL_STOP)
5987 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005988 }
NeilBrownd7603b72006-01-06 00:20:30 -08005989 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005990 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992}
5993
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005994#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005995static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996{
NeilBrown3cb03002011-10-11 16:45:26 +11005997 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 int err;
5999
NeilBrowna757e642005-04-16 15:26:42 -07006000 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002
NeilBrown9d487392016-11-02 14:16:49 +11006003 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004
NeilBrowndafb20f2012-03-19 12:46:39 +11006005 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11006007 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 }
NeilBrown9d487392016-11-02 14:16:49 +11006009 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
NeilBrownd710e132008-10-13 11:55:12 +11006011 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006013 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10006014 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 }
6016}
6017
6018/*
6019 * lets try to run arrays based on all disks that have arrived
6020 * until now. (those are in pending_raid_disks)
6021 *
6022 * the method: pick the first pending disk, collect all disks with
6023 * the same UUID, remove all from the pending list and put them into
6024 * the 'same_array' list. Then order this list based on superblock
6025 * update time (freshest comes first), kick out 'old' disks and
6026 * compare superblocks. If everything's fine then run it.
6027 *
6028 * If "unit" is allocated, then bump its reference count
6029 */
6030static void autorun_devices(int part)
6031{
NeilBrown3cb03002011-10-11 16:45:26 +11006032 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006033 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 char b[BDEVNAME_SIZE];
6035
NeilBrown9d487392016-11-02 14:16:49 +11006036 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07006038 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08006040 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11006042 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043
NeilBrown9d487392016-11-02 14:16:49 +11006044 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006046 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006048 pr_debug("md: adding %s ...\n",
6049 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 list_move(&rdev->same_set, &candidates);
6051 }
6052 /*
6053 * now we have a set of devices, with all of them having
6054 * mostly sane superblocks. It's time to allocate the
6055 * mddev.
6056 */
NeilBrowne8703fe2006-10-03 01:15:59 -07006057 if (part) {
6058 dev = MKDEV(mdp_major,
6059 rdev0->preferred_minor << MdpMinorShift);
6060 unit = MINOR(dev) >> MdpMinorShift;
6061 } else {
6062 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6063 unit = MINOR(dev);
6064 }
6065 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006066 pr_warn("md: unit number in %s is bad: %d\n",
6067 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 break;
6069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
6071 md_probe(dev, NULL, NULL);
6072 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006073 if (!mddev || !mddev->gendisk) {
6074 if (mddev)
6075 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 break;
6077 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006078 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006079 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 else if (mddev->raid_disks || mddev->major_version
6081 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006082 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 mdname(mddev), bdevname(rdev0->bdev,b));
6084 mddev_unlock(mddev);
6085 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006086 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006087 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006088 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 list_del_init(&rdev->same_set);
6090 if (bind_rdev_to_array(rdev, mddev))
6091 export_rdev(rdev);
6092 }
6093 autorun_array(mddev);
6094 mddev_unlock(mddev);
6095 }
6096 /* on success, candidates will be empty, on error
6097 * it won't...
6098 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006099 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006100 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 mddev_put(mddev);
6104 }
NeilBrown9d487392016-11-02 14:16:49 +11006105 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006107#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108
NeilBrownf72ffdd2014-09-30 14:23:59 +10006109static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110{
6111 mdu_version_t ver;
6112
6113 ver.major = MD_MAJOR_VERSION;
6114 ver.minor = MD_MINOR_VERSION;
6115 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6116
6117 if (copy_to_user(arg, &ver, sizeof(ver)))
6118 return -EFAULT;
6119
6120 return 0;
6121}
6122
NeilBrownf72ffdd2014-09-30 14:23:59 +10006123static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124{
6125 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006126 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006127 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128
NeilBrown1ca69c42012-10-11 13:37:33 +11006129 nr = working = insync = failed = spare = 0;
6130 rcu_read_lock();
6131 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006133 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 failed++;
6135 else {
6136 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006137 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006138 insync++;
Song Liub347af82016-08-11 17:14:45 -07006139 else if (test_bit(Journal, &rdev->flags))
6140 /* TODO: add journal count to md_u.h */
6141 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 else
6143 spare++;
6144 }
6145 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006146 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147
6148 info.major_version = mddev->major_version;
6149 info.minor_version = mddev->minor_version;
6150 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006151 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006153 info.size = mddev->dev_sectors / 2;
6154 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006155 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 info.nr_disks = nr;
6157 info.raid_disks = mddev->raid_disks;
6158 info.md_minor = mddev->md_minor;
6159 info.not_persistent= !mddev->persistent;
6160
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006161 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 info.state = 0;
6163 if (mddev->in_sync)
6164 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006165 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006166 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006167 if (mddev_is_clustered(mddev))
6168 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006169 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 info.working_disks = working;
6171 info.failed_disks = failed;
6172 info.spare_disks = spare;
6173
6174 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006175 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176
6177 if (copy_to_user(arg, &info, sizeof(info)))
6178 return -EFAULT;
6179
6180 return 0;
6181}
6182
NeilBrownf72ffdd2014-09-30 14:23:59 +10006183static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006184{
6185 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006186 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006187 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006188
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006189 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006190 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006191 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006192
NeilBrown32a76272005-06-21 17:17:14 -07006193 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006194 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006195 /* bitmap enabled */
6196 if (mddev->bitmap_info.file) {
6197 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6198 sizeof(file->pathname));
6199 if (IS_ERR(ptr))
6200 err = PTR_ERR(ptr);
6201 else
6202 memmove(file->pathname, ptr,
6203 sizeof(file->pathname)-(ptr-file->pathname));
6204 }
NeilBrown4af1a042014-12-15 12:57:00 +11006205 spin_unlock(&mddev->lock);
6206
6207 if (err == 0 &&
6208 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006209 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006210
NeilBrown32a76272005-06-21 17:17:14 -07006211 kfree(file);
6212 return err;
6213}
6214
NeilBrownf72ffdd2014-09-30 14:23:59 +10006215static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216{
6217 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006218 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219
6220 if (copy_from_user(&info, arg, sizeof(info)))
6221 return -EFAULT;
6222
NeilBrown1ca69c42012-10-11 13:37:33 +11006223 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006224 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 if (rdev) {
6226 info.major = MAJOR(rdev->bdev->bd_dev);
6227 info.minor = MINOR(rdev->bdev->bd_dev);
6228 info.raid_disk = rdev->raid_disk;
6229 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006230 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006232 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 info.state |= (1<<MD_DISK_ACTIVE);
6234 info.state |= (1<<MD_DISK_SYNC);
6235 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006236 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006237 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006238 if (test_bit(WriteMostly, &rdev->flags))
6239 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006240 if (test_bit(FailFast, &rdev->flags))
6241 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 } else {
6243 info.major = info.minor = 0;
6244 info.raid_disk = -1;
6245 info.state = (1<<MD_DISK_REMOVED);
6246 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006247 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248
6249 if (copy_to_user(arg, &info, sizeof(info)))
6250 return -EFAULT;
6251
6252 return 0;
6253}
6254
NeilBrownf72ffdd2014-09-30 14:23:59 +10006255static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256{
6257 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006258 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 dev_t dev = MKDEV(info->major,info->minor);
6260
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006261 if (mddev_is_clustered(mddev) &&
6262 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006263 pr_warn("%s: Cannot add to clustered mddev.\n",
6264 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006265 return -EINVAL;
6266 }
6267
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6269 return -EOVERFLOW;
6270
6271 if (!mddev->raid_disks) {
6272 int err;
6273 /* expecting a device which has a superblock */
6274 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6275 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006276 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 PTR_ERR(rdev));
6278 return PTR_ERR(rdev);
6279 }
6280 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006281 struct md_rdev *rdev0
6282 = list_entry(mddev->disks.next,
6283 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006284 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 .load_super(rdev, rdev0, mddev->minor_version);
6286 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006287 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006288 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 bdevname(rdev0->bdev,b2));
6290 export_rdev(rdev);
6291 return -EINVAL;
6292 }
6293 }
6294 err = bind_rdev_to_array(rdev, mddev);
6295 if (err)
6296 export_rdev(rdev);
6297 return err;
6298 }
6299
6300 /*
6301 * add_new_disk can be used once the array is assembled
6302 * to add "hot spares". They must already have a superblock
6303 * written
6304 */
6305 if (mddev->pers) {
6306 int err;
6307 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006308 pr_warn("%s: personality does not support diskops!\n",
6309 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 return -EINVAL;
6311 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006312 if (mddev->persistent)
6313 rdev = md_import_device(dev, mddev->major_version,
6314 mddev->minor_version);
6315 else
6316 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006318 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 PTR_ERR(rdev));
6320 return PTR_ERR(rdev);
6321 }
NeilBrown1a855a02010-12-09 16:36:28 +11006322 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006323 if (!mddev->persistent) {
6324 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006325 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006326 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006327 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006328 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006329 } else
NeilBrown41158c72005-06-21 17:17:25 -07006330 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006331 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006332 } else
6333 super_types[mddev->major_version].
6334 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006335 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006336 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006337 /* This was a hot-add request, but events doesn't
6338 * match, so reject it.
6339 */
6340 export_rdev(rdev);
6341 return -EINVAL;
6342 }
6343
NeilBrownb2d444d2005-11-08 21:39:31 -08006344 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006345 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6346 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006347 else
6348 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006349 if (info->state & (1<<MD_DISK_FAILFAST))
6350 set_bit(FailFast, &rdev->flags);
6351 else
6352 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006353
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006354 if (info->state & (1<<MD_DISK_JOURNAL)) {
6355 struct md_rdev *rdev2;
6356 bool has_journal = false;
6357
6358 /* make sure no existing journal disk */
6359 rdev_for_each(rdev2, mddev) {
6360 if (test_bit(Journal, &rdev2->flags)) {
6361 has_journal = true;
6362 break;
6363 }
6364 }
6365 if (has_journal) {
6366 export_rdev(rdev);
6367 return -EBUSY;
6368 }
Song Liubac624f2015-08-13 14:31:55 -07006369 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006370 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006371 /*
6372 * check whether the device shows up in other nodes
6373 */
6374 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006375 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006376 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006377 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006378 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006379 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006380 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006381 export_rdev(rdev);
6382 return err;
6383 }
6384 }
6385 }
6386
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 rdev->raid_disk = -1;
6388 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006389
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 if (err)
6391 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006392
6393 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006394 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6395 if (!err) {
6396 err = md_cluster_ops->new_disk_ack(mddev,
6397 err == 0);
6398 if (err)
6399 md_kick_rdev_from_array(rdev);
6400 }
6401 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006402 if (err)
6403 md_cluster_ops->add_new_disk_cancel(mddev);
6404 else
6405 err = add_bound_rdev(rdev);
6406 }
6407
6408 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006409 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006410
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 return err;
6412 }
6413
6414 /* otherwise, add_new_disk is only allowed
6415 * for major_version==0 superblocks
6416 */
6417 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006418 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 return -EINVAL;
6420 }
6421
6422 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6423 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006424 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006426 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 PTR_ERR(rdev));
6428 return PTR_ERR(rdev);
6429 }
6430 rdev->desc_nr = info->number;
6431 if (info->raid_disk < mddev->raid_disks)
6432 rdev->raid_disk = info->raid_disk;
6433 else
6434 rdev->raid_disk = -1;
6435
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006437 if (info->state & (1<<MD_DISK_SYNC))
6438 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006440 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6441 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006442 if (info->state & (1<<MD_DISK_FAILFAST))
6443 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006444
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006446 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006447 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6448 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006449 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006450 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451
NeilBrown2bf071b2006-01-06 00:20:55 -08006452 err = bind_rdev_to_array(rdev, mddev);
6453 if (err) {
6454 export_rdev(rdev);
6455 return err;
6456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 }
6458
6459 return 0;
6460}
6461
NeilBrownf72ffdd2014-09-30 14:23:59 +10006462static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463{
6464 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006465 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 rdev = find_rdev(mddev, dev);
6468 if (!rdev)
6469 return -ENXIO;
6470
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006471 if (rdev->raid_disk < 0)
6472 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006473
NeilBrown3ea8929d2013-04-24 11:42:41 +10006474 clear_bit(Blocked, &rdev->flags);
6475 remove_and_add_spares(mddev, rdev);
6476
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477 if (rdev->raid_disk >= 0)
6478 goto busy;
6479
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006480kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006481 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006482 md_cluster_ops->remove_disk(mddev, rdev);
6483
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006484 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006485 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006486 if (mddev->thread)
6487 md_wakeup_thread(mddev->thread);
6488 else
6489 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006490 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491
6492 return 0;
6493busy:
NeilBrown9d487392016-11-02 14:16:49 +11006494 pr_debug("md: cannot remove active disk %s from %s ...\n",
6495 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 return -EBUSY;
6497}
6498
NeilBrownf72ffdd2014-09-30 14:23:59 +10006499static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500{
6501 char b[BDEVNAME_SIZE];
6502 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006503 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504
6505 if (!mddev->pers)
6506 return -ENODEV;
6507
6508 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006509 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510 mdname(mddev));
6511 return -EINVAL;
6512 }
6513 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006514 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 mdname(mddev));
6516 return -EINVAL;
6517 }
6518
NeilBrownd710e132008-10-13 11:55:12 +11006519 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006521 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522 PTR_ERR(rdev));
6523 return -EINVAL;
6524 }
6525
6526 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006527 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006529 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530
NeilBrown8190e752009-06-18 08:48:58 +10006531 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532
NeilBrownb2d444d2005-11-08 21:39:31 -08006533 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006534 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 bdevname(rdev->bdev,b), mdname(mddev));
6536 err = -EINVAL;
6537 goto abort_export;
6538 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006539
NeilBrownb2d444d2005-11-08 21:39:31 -08006540 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006542 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006543 err = bind_rdev_to_array(rdev, mddev);
6544 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006545 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546
6547 /*
6548 * The rest should better be atomic, we can have disk failures
6549 * noticed in interrupt contexts ...
6550 */
6551
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552 rdev->raid_disk = -1;
6553
Shaohua Li29530792016-12-08 15:48:19 -08006554 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006555 if (!mddev->thread)
6556 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 /*
6558 * Kick recovery, maybe this spare has to be added to the
6559 * array immediately.
6560 */
6561 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6562 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006563 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 return 0;
6565
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566abort_export:
6567 export_rdev(rdev);
6568 return err;
6569}
6570
NeilBrownfd01b882011-10-11 16:47:53 +11006571static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006572{
NeilBrown035328c2014-04-09 12:25:40 +10006573 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006574
NeilBrown36fa3062005-09-09 16:23:45 -07006575 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006576 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006577 return -EBUSY;
6578 if (mddev->recovery || mddev->sync_thread)
6579 return -EBUSY;
6580 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006581 }
6582
NeilBrown36fa3062005-09-09 16:23:45 -07006583 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006584 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006585 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006586
NeilBrown1e594bb2014-12-15 12:57:00 +11006587 if (mddev->bitmap || mddev->bitmap_info.file)
6588 return -EEXIST; /* cannot add when bitmap is present */
6589 f = fget(fd);
6590
6591 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006592 pr_warn("%s: error: failed to get bitmap file\n",
6593 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006594 return -EBADF;
6595 }
6596
NeilBrown1e594bb2014-12-15 12:57:00 +11006597 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006598 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006599 pr_warn("%s: error: bitmap file must be a regular file\n",
6600 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006601 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006602 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006603 pr_warn("%s: error: bitmap file must open for write\n",
6604 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006605 err = -EBADF;
6606 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006607 pr_warn("%s: error: bitmap file is already in use\n",
6608 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006609 err = -EBUSY;
6610 }
6611 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006612 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006613 return err;
6614 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006615 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006616 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006617 } else if (mddev->bitmap == NULL)
6618 return -ENOENT; /* cannot remove what isn't there */
6619 err = 0;
6620 if (mddev->pers) {
6621 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006622 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006623 struct bitmap *bitmap;
6624
6625 bitmap = bitmap_create(mddev, -1);
6626 if (!IS_ERR(bitmap)) {
6627 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006628 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006629 } else
6630 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006631 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006632 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006633 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006634 fd = -1; /* make sure to put the file */
6635 }
NeilBrown36fa3062005-09-09 16:23:45 -07006636 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006637 }
6638 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006639 struct file *f = mddev->bitmap_info.file;
6640 if (f) {
6641 spin_lock(&mddev->lock);
6642 mddev->bitmap_info.file = NULL;
6643 spin_unlock(&mddev->lock);
6644 fput(f);
6645 }
NeilBrown36fa3062005-09-09 16:23:45 -07006646 }
6647
NeilBrown32a76272005-06-21 17:17:14 -07006648 return err;
6649}
6650
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651/*
6652 * set_array_info is used two different ways
6653 * The original usage is when creating a new array.
6654 * In this usage, raid_disks is > 0 and it together with
6655 * level, size, not_persistent,layout,chunksize determine the
6656 * shape of the array.
6657 * This will always create an array with a type-0.90.0 superblock.
6658 * The newer usage is when assembling an array.
6659 * In this case raid_disks will be 0, and the major_version field is
6660 * use to determine which style super-blocks are to be found on the devices.
6661 * The minor and patch _version numbers are also kept incase the
6662 * super_block handler wishes to interpret them.
6663 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006664static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665{
6666
6667 if (info->raid_disks == 0) {
6668 /* just setting version number for superblock loading */
6669 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006670 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 super_types[info->major_version].name == NULL) {
6672 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11006673 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 info->major_version);
6675 return -EINVAL;
6676 }
6677 mddev->major_version = info->major_version;
6678 mddev->minor_version = info->minor_version;
6679 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006680 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006681 /* ensure mddev_put doesn't delete this now that there
6682 * is some minimal configuration.
6683 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006684 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 return 0;
6686 }
6687 mddev->major_version = MD_MAJOR_VERSION;
6688 mddev->minor_version = MD_MINOR_VERSION;
6689 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006690 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691
6692 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006693 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006694 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 mddev->raid_disks = info->raid_disks;
6696 /* don't set md_minor, it is determined by which /dev/md* was
6697 * openned
6698 */
6699 if (info->state & (1<<MD_SB_CLEAN))
6700 mddev->recovery_cp = MaxSector;
6701 else
6702 mddev->recovery_cp = 0;
6703 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006704 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705
6706 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006707 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708
Shaohua Li29530792016-12-08 15:48:19 -08006709 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11006710 mddev->max_disks = MD_SB_DISKS;
6711 mddev->flags = 0;
6712 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006713 }
6714 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715
NeilBrownc3d97142009-12-14 12:49:52 +11006716 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006717 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006718 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006719
NeilBrownf6705572006-03-27 01:18:11 -08006720 mddev->reshape_position = MaxSector;
6721
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 /*
6723 * Generate a 128 bit UUID
6724 */
6725 get_random_bytes(mddev->uuid, 16);
6726
NeilBrownf6705572006-03-27 01:18:11 -08006727 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006728 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006729 mddev->new_layout = mddev->layout;
6730 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006731 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006732
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733 return 0;
6734}
6735
NeilBrownfd01b882011-10-11 16:47:53 +11006736void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006737{
Dan Williamsb522adc2009-03-31 15:00:31 +11006738 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6739
6740 if (mddev->external_size)
6741 return;
6742
Dan Williams1f403622009-03-31 14:59:03 +11006743 mddev->array_sectors = array_sectors;
6744}
6745EXPORT_SYMBOL(md_set_array_sectors);
6746
NeilBrownfd01b882011-10-11 16:47:53 +11006747static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006748{
NeilBrown3cb03002011-10-11 16:45:26 +11006749 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006750 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006751 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08006752 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006753
NeilBrowna35b0d62006-01-06 00:20:49 -08006754 if (mddev->pers->resize == NULL)
6755 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006756 /* The "num_sectors" is the number of sectors of each device that
6757 * is used. This can only make sense for arrays with redundancy.
6758 * linear and raid0 always use whatever space is available. We can only
6759 * consider changing this number if no resync or reconstruction is
6760 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006761 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006762 * of each device. If num_sectors is zero, we find the largest size
6763 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006764 */
NeilBrownf851b602014-12-11 10:02:10 +11006765 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6766 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006767 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006768 if (mddev->ro)
6769 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006770
NeilBrowndafb20f2012-03-19 12:46:39 +11006771 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006772 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006773
Andre Nolld71f9f82008-07-11 22:02:22 +10006774 if (fit && (num_sectors == 0 || num_sectors > avail))
6775 num_sectors = avail;
6776 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006777 return -ENOSPC;
6778 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006779 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08006780 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08006781 if (mddev_is_clustered(mddev))
6782 md_cluster_ops->update_size(mddev, old_dev_sectors);
6783 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08006784 set_capacity(mddev->gendisk, mddev->array_sectors);
6785 revalidate_disk(mddev->gendisk);
6786 }
6787 }
NeilBrowna35b0d62006-01-06 00:20:49 -08006788 return rv;
6789}
6790
NeilBrownfd01b882011-10-11 16:47:53 +11006791static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006792{
6793 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006794 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006795 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006796 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006797 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006798 if (mddev->ro)
6799 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006800 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006801 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006802 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006803 if (mddev->sync_thread ||
6804 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6805 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006806 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006807
6808 rdev_for_each(rdev, mddev) {
6809 if (mddev->raid_disks < raid_disks &&
6810 rdev->data_offset < rdev->new_data_offset)
6811 return -EINVAL;
6812 if (mddev->raid_disks > raid_disks &&
6813 rdev->data_offset > rdev->new_data_offset)
6814 return -EINVAL;
6815 }
6816
NeilBrown63c70c42006-03-27 01:18:13 -08006817 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006818 if (mddev->delta_disks < 0)
6819 mddev->reshape_backwards = 1;
6820 else if (mddev->delta_disks > 0)
6821 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006822
6823 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006824 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006825 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006826 mddev->reshape_backwards = 0;
6827 }
NeilBrownda943b992006-01-06 00:20:54 -08006828 return rv;
6829}
6830
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831/*
6832 * update_array_info is used to change the configuration of an
6833 * on-line array.
6834 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6835 * fields in the info are checked against the array.
6836 * Any differences that cannot be handled will cause an error.
6837 * Normally, only one change can be managed at a time.
6838 */
NeilBrownfd01b882011-10-11 16:47:53 +11006839static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840{
6841 int rv = 0;
6842 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006843 int state = 0;
6844
6845 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006846 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006847 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
6849 if (mddev->major_version != info->major_version ||
6850 mddev->minor_version != info->minor_version ||
6851/* mddev->patch_version != info->patch_version || */
6852 mddev->ctime != info->ctime ||
6853 mddev->level != info->level ||
6854/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006855 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006856 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006857 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6858 ((state^info->state) & 0xfffffe00)
6859 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 return -EINVAL;
6861 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006862 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6863 cnt++;
6864 if (mddev->raid_disks != info->raid_disks)
6865 cnt++;
6866 if (mddev->layout != info->layout)
6867 cnt++;
6868 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6869 cnt++;
6870 if (cnt == 0)
6871 return 0;
6872 if (cnt > 1)
6873 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874
6875 if (mddev->layout != info->layout) {
6876 /* Change layout
6877 * we don't need to do anything at the md level, the
6878 * personality will take care of it all.
6879 */
NeilBrown50ac1682009-06-18 08:47:55 +10006880 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006882 else {
6883 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006884 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006885 if (rv)
6886 mddev->new_layout = mddev->layout;
6887 return rv;
6888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006890 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006891 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892
NeilBrownda943b992006-01-06 00:20:54 -08006893 if (mddev->raid_disks != info->raid_disks)
6894 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895
NeilBrown36fa3062005-09-09 16:23:45 -07006896 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006897 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6898 rv = -EINVAL;
6899 goto err;
6900 }
6901 if (mddev->recovery || mddev->sync_thread) {
6902 rv = -EBUSY;
6903 goto err;
6904 }
NeilBrown36fa3062005-09-09 16:23:45 -07006905 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006906 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006907 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006908 if (mddev->bitmap) {
6909 rv = -EEXIST;
6910 goto err;
6911 }
6912 if (mddev->bitmap_info.default_offset == 0) {
6913 rv = -EINVAL;
6914 goto err;
6915 }
NeilBrownc3d97142009-12-14 12:49:52 +11006916 mddev->bitmap_info.offset =
6917 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006918 mddev->bitmap_info.space =
6919 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006920 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006921 bitmap = bitmap_create(mddev, -1);
6922 if (!IS_ERR(bitmap)) {
6923 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006924 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006925 } else
6926 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006927 if (rv)
6928 bitmap_destroy(mddev);
6929 mddev->pers->quiesce(mddev, 0);
6930 } else {
6931 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006932 if (!mddev->bitmap) {
6933 rv = -ENOENT;
6934 goto err;
6935 }
6936 if (mddev->bitmap->storage.file) {
6937 rv = -EINVAL;
6938 goto err;
6939 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006940 if (mddev->bitmap_info.nodes) {
6941 /* hold PW on all the bitmap lock */
6942 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006943 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 +11006944 rv = -EPERM;
6945 md_cluster_ops->unlock_all_bitmaps(mddev);
6946 goto err;
6947 }
6948
6949 mddev->bitmap_info.nodes = 0;
6950 md_cluster_ops->leave(mddev);
6951 }
NeilBrown36fa3062005-09-09 16:23:45 -07006952 mddev->pers->quiesce(mddev, 1);
6953 bitmap_destroy(mddev);
6954 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006955 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006956 }
6957 }
NeilBrown850b2b422006-10-03 01:15:46 -07006958 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006959 return rv;
6960err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961 return rv;
6962}
6963
NeilBrownfd01b882011-10-11 16:47:53 +11006964static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965{
NeilBrown3cb03002011-10-11 16:45:26 +11006966 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006967 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968
6969 if (mddev->pers == NULL)
6970 return -ENODEV;
6971
NeilBrown1ca69c42012-10-11 13:37:33 +11006972 rcu_read_lock();
6973 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006975 err = -ENODEV;
6976 else {
6977 md_error(mddev, rdev);
6978 if (!test_bit(Faulty, &rdev->flags))
6979 err = -EBUSY;
6980 }
6981 rcu_read_unlock();
6982 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983}
6984
Andre Noll2f9618c2008-04-25 18:57:58 +02006985/*
6986 * We have a problem here : there is no easy way to give a CHS
6987 * virtual geometry. We currently pretend that we have a 2 heads
6988 * 4 sectors (with a BIG number of cylinders...). This drives
6989 * dosfs just mad... ;-)
6990 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006991static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6992{
NeilBrownfd01b882011-10-11 16:47:53 +11006993 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006994
6995 geo->heads = 2;
6996 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006997 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006998 return 0;
6999}
7000
Nicolas Schichancb335f82014-01-15 16:58:52 +01007001static inline bool md_ioctl_valid(unsigned int cmd)
7002{
7003 switch (cmd) {
7004 case ADD_NEW_DISK:
7005 case BLKROSET:
7006 case GET_ARRAY_INFO:
7007 case GET_BITMAP_FILE:
7008 case GET_DISK_INFO:
7009 case HOT_ADD_DISK:
7010 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007011 case RAID_AUTORUN:
7012 case RAID_VERSION:
7013 case RESTART_ARRAY_RW:
7014 case RUN_ARRAY:
7015 case SET_ARRAY_INFO:
7016 case SET_BITMAP_FILE:
7017 case SET_DISK_FAULTY:
7018 case STOP_ARRAY:
7019 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007020 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007021 return true;
7022 default:
7023 return false;
7024 }
7025}
7026
Al Viroa39907f2008-03-02 10:31:15 -05007027static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028 unsigned int cmd, unsigned long arg)
7029{
7030 int err = 0;
7031 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11007032 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10007033 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08007034 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035
Nicolas Schichancb335f82014-01-15 16:58:52 +01007036 if (!md_ioctl_valid(cmd))
7037 return -ENOTTY;
7038
NeilBrown506c9e42011-12-23 10:17:26 +11007039 switch (cmd) {
7040 case RAID_VERSION:
7041 case GET_ARRAY_INFO:
7042 case GET_DISK_INFO:
7043 break;
7044 default:
7045 if (!capable(CAP_SYS_ADMIN))
7046 return -EACCES;
7047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048
7049 /*
7050 * Commands dealing with the RAID driver but not any
7051 * particular array:
7052 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007053 switch (cmd) {
7054 case RAID_VERSION:
7055 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007056 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11007059 case RAID_AUTORUN:
7060 err = 0;
7061 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007062 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11007064 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 }
7066
7067 /*
7068 * Commands creating/starting a new array:
7069 */
7070
Al Viroa39907f2008-03-02 10:31:15 -05007071 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072
7073 if (!mddev) {
7074 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007075 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 }
7077
NeilBrown1ca69c42012-10-11 13:37:33 +11007078 /* Some actions do not requires the mutex */
7079 switch (cmd) {
7080 case GET_ARRAY_INFO:
7081 if (!mddev->raid_disks && !mddev->external)
7082 err = -ENODEV;
7083 else
7084 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007085 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007086
7087 case GET_DISK_INFO:
7088 if (!mddev->raid_disks && !mddev->external)
7089 err = -ENODEV;
7090 else
7091 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007092 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007093
7094 case SET_DISK_FAULTY:
7095 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007096 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007097
7098 case GET_BITMAP_FILE:
7099 err = get_bitmap_file(mddev, argp);
7100 goto out;
7101
NeilBrown1ca69c42012-10-11 13:37:33 +11007102 }
7103
NeilBrowna7a3f082012-12-11 13:35:54 +11007104 if (cmd == ADD_NEW_DISK)
7105 /* need to ensure md_delayed_delete() has completed */
7106 flush_workqueue(md_misc_wq);
7107
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007108 if (cmd == HOT_REMOVE_DISK)
7109 /* need to ensure recovery thread has run */
7110 wait_event_interruptible_timeout(mddev->sb_wait,
7111 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007112 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007113 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007114 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7115 /* Need to flush page cache, and ensure no-one else opens
7116 * and writes
7117 */
7118 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007119 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007120 mutex_unlock(&mddev->open_mutex);
7121 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007122 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007123 }
NeilBrown065e5192017-04-06 11:16:33 +08007124 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007125 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007126 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007127 mutex_unlock(&mddev->open_mutex);
7128 sync_blockdev(bdev);
7129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130 err = mddev_lock(mddev);
7131 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007132 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7133 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007134 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 }
7136
NeilBrownc02c0ae2012-12-11 13:39:21 +11007137 if (cmd == SET_ARRAY_INFO) {
7138 mdu_array_info_t info;
7139 if (!arg)
7140 memset(&info, 0, sizeof(info));
7141 else if (copy_from_user(&info, argp, sizeof(info))) {
7142 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007143 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007144 }
7145 if (mddev->pers) {
7146 err = update_array_info(mddev, &info);
7147 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007148 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007149 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007151 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007152 }
7153 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007154 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007155 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007156 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007157 }
7158 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007159 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007160 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007161 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007162 }
7163 err = set_array_info(mddev, &info);
7164 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007165 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007166 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007167 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007168 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169 }
7170
7171 /*
7172 * Commands querying/configuring an existing array:
7173 */
NeilBrown32a76272005-06-21 17:17:14 -07007174 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007175 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007176 if ((!mddev->raid_disks && !mddev->external)
7177 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7178 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7179 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007181 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182 }
7183
7184 /*
7185 * Commands even a read-only array can execute:
7186 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007187 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007188 case RESTART_ARRAY_RW:
7189 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007190 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007191
7192 case STOP_ARRAY:
7193 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007194 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007195
7196 case STOP_ARRAY_RO:
7197 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007198 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007199
NeilBrown3ea8929d2013-04-24 11:42:41 +10007200 case HOT_REMOVE_DISK:
7201 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007202 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007203
NeilBrown7ceb17e2013-04-24 11:42:42 +10007204 case ADD_NEW_DISK:
7205 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007206 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007207 * So require mddev->pers and MD_DISK_SYNC.
7208 */
7209 if (mddev->pers) {
7210 mdu_disk_info_t info;
7211 if (copy_from_user(&info, argp, sizeof(info)))
7212 err = -EFAULT;
7213 else if (!(info.state & (1<<MD_DISK_SYNC)))
7214 /* Need to clear read-only for this */
7215 break;
7216 else
7217 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007218 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007219 }
7220 break;
7221
NeilBrownc02c0ae2012-12-11 13:39:21 +11007222 case BLKROSET:
7223 if (get_user(ro, (int __user *)(arg))) {
7224 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007225 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007226 }
7227 err = -EINVAL;
7228
7229 /* if the bdev is going readonly the value of mddev->ro
7230 * does not matter, no writes are coming
7231 */
7232 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007233 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007234
NeilBrownc02c0ae2012-12-11 13:39:21 +11007235 /* are we are already prepared for writes? */
7236 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007237 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007238
7239 /* transitioning to readauto need only happen for
7240 * arrays that call md_write_start
7241 */
7242 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007244 if (err == 0) {
7245 mddev->ro = 2;
7246 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007247 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007248 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007249 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250 }
7251
7252 /*
7253 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007254 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 */
NeilBrown326eb172014-09-30 15:36:28 +10007256 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007257 if (mddev->ro == 2) {
7258 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007259 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007260 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007261 /* mddev_unlock will wake thread */
7262 /* If a device failed while we were read-only, we
7263 * need to make sure the metadata is updated now.
7264 */
Shaohua Li29530792016-12-08 15:48:19 -08007265 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007266 mddev_unlock(mddev);
7267 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007268 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7269 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007270 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007271 }
NeilBrownf91de922005-11-08 21:39:36 -08007272 } else {
7273 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007274 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276 }
7277
NeilBrownc02c0ae2012-12-11 13:39:21 +11007278 switch (cmd) {
7279 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007281 mdu_disk_info_t info;
7282 if (copy_from_user(&info, argp, sizeof(info)))
7283 err = -EFAULT;
7284 else
7285 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007286 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007289 case CLUSTERED_DISK_NACK:
7290 if (mddev_is_clustered(mddev))
7291 md_cluster_ops->new_disk_ack(mddev, false);
7292 else
7293 err = -EINVAL;
7294 goto unlock;
7295
NeilBrownc02c0ae2012-12-11 13:39:21 +11007296 case HOT_ADD_DISK:
7297 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007298 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299
NeilBrownc02c0ae2012-12-11 13:39:21 +11007300 case RUN_ARRAY:
7301 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007302 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303
NeilBrownc02c0ae2012-12-11 13:39:21 +11007304 case SET_BITMAP_FILE:
7305 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007306 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007307
NeilBrownc02c0ae2012-12-11 13:39:21 +11007308 default:
7309 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007310 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311 }
7312
NeilBrown3adc28d2014-09-30 15:46:41 +10007313unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007314 if (mddev->hold_active == UNTIL_IOCTL &&
7315 err != -EINVAL)
7316 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007318out:
NeilBrown065e5192017-04-06 11:16:33 +08007319 if(did_set_md_closing)
7320 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007321 return err;
7322}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007323#ifdef CONFIG_COMPAT
7324static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7325 unsigned int cmd, unsigned long arg)
7326{
7327 switch (cmd) {
7328 case HOT_REMOVE_DISK:
7329 case HOT_ADD_DISK:
7330 case SET_DISK_FAULTY:
7331 case SET_BITMAP_FILE:
7332 /* These take in integer arg, do not convert */
7333 break;
7334 default:
7335 arg = (unsigned long)compat_ptr(arg);
7336 break;
7337 }
7338
7339 return md_ioctl(bdev, mode, cmd, arg);
7340}
7341#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342
Al Viroa39907f2008-03-02 10:31:15 -05007343static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344{
7345 /*
7346 * Succeed if we can lock the mddev, which confirms that
7347 * it isn't being stopped right now.
7348 */
NeilBrownfd01b882011-10-11 16:47:53 +11007349 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 int err;
7351
Yuanhan Liu0c098222012-05-22 13:55:32 +10007352 if (!mddev)
7353 return -ENODEV;
7354
NeilBrownd3374822009-01-09 08:31:10 +11007355 if (mddev->gendisk != bdev->bd_disk) {
7356 /* we are racing with mddev_put which is discarding this
7357 * bd_disk.
7358 */
7359 mddev_put(mddev);
7360 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007361 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007362 /* Then retry the open from the top */
7363 return -ERESTARTSYS;
7364 }
7365 BUG_ON(mddev != bdev->bd_disk->private_data);
7366
NeilBrownc8c00a62009-08-10 12:50:52 +10007367 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368 goto out;
7369
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007370 if (test_bit(MD_CLOSING, &mddev->flags)) {
7371 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007372 err = -ENODEV;
7373 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007374 }
7375
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007377 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007378 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007380 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007382 if (err)
7383 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384 return err;
7385}
7386
Al Virodb2a1442013-05-05 21:52:57 -04007387static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007389 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007391 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007392 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007395
7396static int md_media_changed(struct gendisk *disk)
7397{
NeilBrownfd01b882011-10-11 16:47:53 +11007398 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007399
7400 return mddev->changed;
7401}
7402
7403static int md_revalidate(struct gendisk *disk)
7404{
NeilBrownfd01b882011-10-11 16:47:53 +11007405 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007406
7407 mddev->changed = 0;
7408 return 0;
7409}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007410static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411{
7412 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007413 .open = md_open,
7414 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007415 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007416#ifdef CONFIG_COMPAT
7417 .compat_ioctl = md_compat_ioctl,
7418#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007419 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007420 .media_changed = md_media_changed,
7421 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422};
7423
NeilBrownf72ffdd2014-09-30 14:23:59 +10007424static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425{
NeilBrown2b8bf342011-10-11 16:48:23 +11007426 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427
Linus Torvalds1da177e2005-04-16 15:20:36 -07007428 /*
7429 * md_thread is a 'system-thread', it's priority should be very
7430 * high. We avoid resource deadlocks individually in each
7431 * raid personality. (RAID5 does preallocation) We also use RR and
7432 * the very same RT priority as kswapd, thus we will never get
7433 * into a priority inversion deadlock.
7434 *
7435 * we definitely have to have equal or higher priority than
7436 * bdflush, otherwise bdflush will deadlock if there are too
7437 * many dirty RAID5 blocks.
7438 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439
NeilBrown6985c432005-10-19 21:23:47 -07007440 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007441 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007442
NeilBrown93588e22005-11-15 00:09:12 -08007443 /* We need to wait INTERRUPTIBLE so that
7444 * we don't add to the load-average.
7445 * That means we need to be sure no signals are
7446 * pending
7447 */
7448 if (signal_pending(current))
7449 flush_signals(current);
7450
7451 wait_event_interruptible_timeout
7452 (thread->wqueue,
7453 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007454 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007455 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456
NeilBrown6c987912011-01-14 09:13:53 +11007457 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007458 if (kthread_should_park())
7459 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007460 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007461 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007463
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 return 0;
7465}
7466
NeilBrown2b8bf342011-10-11 16:48:23 +11007467void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468{
7469 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007470 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Guoqing Jiangd1d90142017-10-09 10:32:48 +08007471 set_bit(THREAD_WAKEUP, &thread->flags);
7472 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 }
7474}
NeilBrown6c144d32014-09-30 16:15:38 +10007475EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476
Shaohua Li4ed87312012-10-11 13:34:00 +11007477struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7478 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479{
NeilBrown2b8bf342011-10-11 16:48:23 +11007480 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481
NeilBrown2b8bf342011-10-11 16:48:23 +11007482 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483 if (!thread)
7484 return NULL;
7485
Linus Torvalds1da177e2005-04-16 15:20:36 -07007486 init_waitqueue_head(&thread->wqueue);
7487
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488 thread->run = run;
7489 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007490 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007491 thread->tsk = kthread_run(md_thread, thread,
7492 "%s_%s",
7493 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007494 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007495 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496 kfree(thread);
7497 return NULL;
7498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499 return thread;
7500}
NeilBrown6c144d32014-09-30 16:15:38 +10007501EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502
NeilBrown2b8bf342011-10-11 16:48:23 +11007503void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504{
NeilBrown2b8bf342011-10-11 16:48:23 +11007505 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007506 if (!thread)
7507 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007508 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007509 /* Locking ensures that mddev_unlock does not wake_up a
7510 * non-existent thread
7511 */
7512 spin_lock(&pers_lock);
7513 *threadp = NULL;
7514 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007515
7516 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007517 kfree(thread);
7518}
NeilBrown6c144d32014-09-30 16:15:38 +10007519EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520
NeilBrownfd01b882011-10-11 16:47:53 +11007521void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522{
NeilBrownb2d444d2005-11-08 21:39:31 -08007523 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007525
NeilBrownde393cd2011-07-28 11:31:48 +10007526 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527 return;
7528 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007529 if (mddev->degraded)
7530 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007531 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7533 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7534 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007535 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007536 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007537 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538}
NeilBrown6c144d32014-09-30 16:15:38 +10007539EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007540
7541/* seq_file implementation /proc/mdstat */
7542
7543static void status_unused(struct seq_file *seq)
7544{
7545 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007546 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547
7548 seq_printf(seq, "unused devices: ");
7549
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007550 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 char b[BDEVNAME_SIZE];
7552 i++;
7553 seq_printf(seq, "%s ",
7554 bdevname(rdev->bdev,b));
7555 }
7556 if (!i)
7557 seq_printf(seq, "<none>");
7558
7559 seq_printf(seq, "\n");
7560}
7561
NeilBrownf7851be2015-07-02 17:12:58 +10007562static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563{
NeilBrowndd71cf62009-05-07 12:49:35 +10007564 sector_t max_sectors, resync, res;
7565 unsigned long dt, db;
7566 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007567 int scale;
7568 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569
NeilBrownc804cde2012-05-21 09:28:33 +10007570 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7571 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007572 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007573 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007574 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575
NeilBrownf7851be2015-07-02 17:12:58 +10007576 resync = mddev->curr_resync;
7577 if (resync <= 3) {
7578 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7579 /* Still cleaning up */
7580 resync = max_sectors;
7581 } else
7582 resync -= atomic_read(&mddev->recovery_active);
7583
7584 if (resync == 0) {
7585 if (mddev->recovery_cp < MaxSector) {
7586 seq_printf(seq, "\tresync=PENDING");
7587 return 1;
7588 }
7589 return 0;
7590 }
7591 if (resync < 3) {
7592 seq_printf(seq, "\tresync=DELAYED");
7593 return 1;
7594 }
7595
NeilBrown403df472014-09-30 15:52:29 +10007596 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007597 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007598 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007599 * u32, as those are the requirements for sector_div.
7600 * Thus 'scale' must be at least 10
7601 */
7602 scale = 10;
7603 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007604 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007605 scale++;
7606 }
7607 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007608 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007609
7610 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007611 {
NeilBrown4588b422006-03-27 01:18:04 -08007612 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613 seq_printf(seq, "[");
7614 for (i = 0; i < x; i++)
7615 seq_printf(seq, "=");
7616 seq_printf(seq, ">");
7617 for (i = 0; i < y; i++)
7618 seq_printf(seq, ".");
7619 seq_printf(seq, "] ");
7620 }
NeilBrown4588b422006-03-27 01:18:04 -08007621 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007622 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7623 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007624 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7625 "check" :
7626 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7627 "resync" : "recovery"))),
7628 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007629 (unsigned long long) resync/2,
7630 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631
7632 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 * dt: time from mark until now
7634 * db: blocks written from mark until now
7635 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007636 *
7637 * rt is a sector_t, so could be 32bit or 64bit.
7638 * So we divide before multiply in case it is 32bit and close
7639 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007640 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007641 * near the end of resync when the number of remaining sectors
7642 * is close to 'db'.
7643 * We then divide rt by 32 after multiplying by db to compensate.
7644 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007645 */
7646 dt = ((jiffies - mddev->resync_mark) / HZ);
7647 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007648 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7649 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007650
NeilBrowndd71cf62009-05-07 12:49:35 +10007651 rt = max_sectors - resync; /* number of remaining sectors */
7652 sector_div(rt, db/32+1);
7653 rt *= dt;
7654 rt >>= 5;
7655
7656 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7657 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658
NeilBrownff4e8d92006-07-10 04:44:16 -07007659 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007660 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661}
7662
7663static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7664{
7665 struct list_head *tmp;
7666 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007667 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668
7669 if (l >= 0x10000)
7670 return NULL;
7671 if (!l--)
7672 /* header */
7673 return (void*)1;
7674
7675 spin_lock(&all_mddevs_lock);
7676 list_for_each(tmp,&all_mddevs)
7677 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007678 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679 mddev_get(mddev);
7680 spin_unlock(&all_mddevs_lock);
7681 return mddev;
7682 }
7683 spin_unlock(&all_mddevs_lock);
7684 if (!l--)
7685 return (void*)2;/* tail */
7686 return NULL;
7687}
7688
7689static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7690{
7691 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007692 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007693
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694 ++*pos;
7695 if (v == (void*)2)
7696 return NULL;
7697
7698 spin_lock(&all_mddevs_lock);
7699 if (v == (void*)1)
7700 tmp = all_mddevs.next;
7701 else
7702 tmp = mddev->all_mddevs.next;
7703 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007704 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705 else {
7706 next_mddev = (void*)2;
7707 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709 spin_unlock(&all_mddevs_lock);
7710
7711 if (v != (void*)1)
7712 mddev_put(mddev);
7713 return next_mddev;
7714
7715}
7716
7717static void md_seq_stop(struct seq_file *seq, void *v)
7718{
NeilBrownfd01b882011-10-11 16:47:53 +11007719 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720
7721 if (mddev && v != (void*)1 && v != (void*)2)
7722 mddev_put(mddev);
7723}
7724
7725static int md_seq_show(struct seq_file *seq, void *v)
7726{
NeilBrownfd01b882011-10-11 16:47:53 +11007727 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007728 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007729 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007730
7731 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007732 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733 seq_printf(seq, "Personalities : ");
7734 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007735 list_for_each_entry(pers, &pers_list, list)
7736 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007737
7738 spin_unlock(&pers_lock);
7739 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007740 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007741 return 0;
7742 }
7743 if (v == (void*)2) {
7744 status_unused(seq);
7745 return 0;
7746 }
7747
NeilBrown36d091f2014-12-15 12:56:58 +11007748 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007749 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7750 seq_printf(seq, "%s : %sactive", mdname(mddev),
7751 mddev->pers ? "" : "in");
7752 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007753 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007754 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007755 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007756 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757 seq_printf(seq, " %s", mddev->pers->name);
7758 }
7759
Andre Nolldd8ac332009-03-31 14:33:13 +11007760 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007761 rcu_read_lock();
7762 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007763 char b[BDEVNAME_SIZE];
7764 seq_printf(seq, " %s[%d]",
7765 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007766 if (test_bit(WriteMostly, &rdev->flags))
7767 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007768 if (test_bit(Journal, &rdev->flags))
7769 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007770 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771 seq_printf(seq, "(F)");
7772 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007773 }
7774 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007775 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007776 if (test_bit(Replacement, &rdev->flags))
7777 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007778 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007780 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781
7782 if (!list_empty(&mddev->disks)) {
7783 if (mddev->pers)
7784 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007785 (unsigned long long)
7786 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787 else
7788 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007789 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007791 if (mddev->persistent) {
7792 if (mddev->major_version != 0 ||
7793 mddev->minor_version != 90) {
7794 seq_printf(seq," super %d.%d",
7795 mddev->major_version,
7796 mddev->minor_version);
7797 }
NeilBrowne6910632008-02-06 01:39:51 -08007798 } else if (mddev->external)
7799 seq_printf(seq, " super external:%s",
7800 mddev->metadata_type);
7801 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007802 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803
7804 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007805 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007806 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007807 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007808 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007809 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007810 }
NeilBrown32a76272005-06-21 17:17:14 -07007811 } else
7812 seq_printf(seq, "\n ");
7813
NeilBrown57148962012-03-19 12:46:40 +11007814 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007815
7816 seq_printf(seq, "\n");
7817 }
NeilBrown36d091f2014-12-15 12:56:58 +11007818 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007819
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820 return 0;
7821}
7822
Jan Engelhardt110518b2009-05-07 12:49:37 +10007823static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824 .start = md_seq_start,
7825 .next = md_seq_next,
7826 .stop = md_seq_stop,
7827 .show = md_seq_show,
7828};
7829
7830static int md_seq_open(struct inode *inode, struct file *file)
7831{
Kay Sieversf1514632011-07-12 20:48:39 +02007832 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833 int error;
7834
7835 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007836 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007837 return error;
7838
7839 seq = file->private_data;
7840 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841 return error;
7842}
7843
NeilBrowne2f23b62014-04-09 14:33:51 +10007844static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007845static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7846{
Kay Sieversf1514632011-07-12 20:48:39 +02007847 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007848 int mask;
7849
NeilBrowne2f23b62014-04-09 14:33:51 +10007850 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007851 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007852 poll_wait(filp, &md_event_waiters, wait);
7853
7854 /* always allow read */
7855 mask = POLLIN | POLLRDNORM;
7856
Kay Sieversf1514632011-07-12 20:48:39 +02007857 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007858 mask |= POLLERR | POLLPRI;
7859 return mask;
7860}
7861
Arjan van de Venfa027c22007-02-12 00:55:33 -08007862static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007863 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864 .open = md_seq_open,
7865 .read = seq_read,
7866 .llseek = seq_lseek,
Cihangir Akturk26e13042017-07-29 19:52:45 +03007867 .release = seq_release,
NeilBrownd7603b72006-01-06 00:20:30 -08007868 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869};
7870
NeilBrown84fc4b52011-10-11 16:49:58 +11007871int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872{
NeilBrown9d487392016-11-02 14:16:49 +11007873 pr_debug("md: %s personality registered for level %d\n",
7874 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007876 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877 spin_unlock(&pers_lock);
7878 return 0;
7879}
NeilBrown6c144d32014-09-30 16:15:38 +10007880EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881
NeilBrown84fc4b52011-10-11 16:49:58 +11007882int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883{
NeilBrown9d487392016-11-02 14:16:49 +11007884 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007885 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007886 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887 spin_unlock(&pers_lock);
7888 return 0;
7889}
NeilBrown6c144d32014-09-30 16:15:38 +10007890EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891
NeilBrown6022e752015-08-13 12:32:55 +10007892int register_md_cluster_operations(struct md_cluster_operations *ops,
7893 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007894{
NeilBrown6022e752015-08-13 12:32:55 +10007895 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007896 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007897 if (md_cluster_ops != NULL)
7898 ret = -EALREADY;
7899 else {
7900 md_cluster_ops = ops;
7901 md_cluster_mod = module;
7902 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007903 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007904 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007905}
7906EXPORT_SYMBOL(register_md_cluster_operations);
7907
7908int unregister_md_cluster_operations(void)
7909{
7910 spin_lock(&pers_lock);
7911 md_cluster_ops = NULL;
7912 spin_unlock(&pers_lock);
7913 return 0;
7914}
7915EXPORT_SYMBOL(unregister_md_cluster_operations);
7916
7917int md_setup_cluster(struct mddev *mddev, int nodes)
7918{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007919 if (!md_cluster_ops)
7920 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007921 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007922 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007923 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11007924 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007925 spin_unlock(&pers_lock);
7926 return -ENOENT;
7927 }
7928 spin_unlock(&pers_lock);
7929
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007930 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007931}
7932
7933void md_cluster_stop(struct mddev *mddev)
7934{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007935 if (!md_cluster_ops)
7936 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007937 md_cluster_ops->leave(mddev);
7938 module_put(md_cluster_mod);
7939}
7940
NeilBrownfd01b882011-10-11 16:47:53 +11007941static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007942{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007943 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007945 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007946
7947 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007948 rcu_read_lock();
7949 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007950 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007951 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7952 (int)part_stat_read(&disk->part0, sectors[1]) -
7953 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007954 /* sync IO will cause sync_io to increase before the disk_stats
7955 * as sync_io is counted when a request starts, and
7956 * disk_stats is counted when it completes.
7957 * So resync activity will cause curr_events to be smaller than
7958 * when there was no such activity.
7959 * non-sync IO will cause disk_stat to increase without
7960 * increasing sync_io so curr_events will (eventually)
7961 * be larger than it was before. Once it becomes
7962 * substantially larger, the test below will cause
7963 * the array to appear non-idle, and resync will slow
7964 * down.
7965 * If there is a lot of outstanding resync activity when
7966 * we set last_event to curr_events, then all that activity
7967 * completing might cause the array to appear non-idle
7968 * and resync will be slowed down even though there might
7969 * not have been non-resync activity. This will only
7970 * happen once though. 'last_events' will soon reflect
7971 * the state where there is little or no outstanding
7972 * resync requests, and further resync activity will
7973 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007974 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007975 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007976 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007977 rdev->last_events = curr_events;
7978 idle = 0;
7979 }
7980 }
NeilBrown4b809912008-07-21 17:05:25 +10007981 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982 return idle;
7983}
7984
NeilBrownfd01b882011-10-11 16:47:53 +11007985void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007986{
7987 /* another "blocks" (512byte) blocks have been synced */
7988 atomic_sub(blocks, &mddev->recovery_active);
7989 wake_up(&mddev->recovery_wait);
7990 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007991 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007992 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993 md_wakeup_thread(mddev->thread);
7994 // stop recovery, signal do_sync ....
7995 }
7996}
NeilBrown6c144d32014-09-30 16:15:38 +10007997EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007998
NeilBrown06d91a52005-06-21 17:17:12 -07007999/* md_write_start(mddev, bi)
8000 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07008001 * in superblock) before writing, schedule a superblock update
8002 * and wait for it to complete.
NeilBrowncc27b0c2017-06-05 16:49:39 +10008003 * A return value of 'false' means that the write wasn't recorded
8004 * and cannot proceed as the array is being suspend.
NeilBrown06d91a52005-06-21 17:17:12 -07008005 */
NeilBrowncc27b0c2017-06-05 16:49:39 +10008006bool md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007{
Neil Brown0fd62b82008-06-28 08:31:36 +10008008 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07008009 if (bio_data_dir(bi) != WRITE)
NeilBrowncc27b0c2017-06-05 16:49:39 +10008010 return true;
NeilBrown06d91a52005-06-21 17:17:12 -07008011
NeilBrownf91de922005-11-08 21:39:36 -08008012 BUG_ON(mddev->ro == 1);
8013 if (mddev->ro == 2) {
8014 /* need to switch to read/write */
8015 mddev->ro = 0;
8016 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8017 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08008018 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008019 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08008020 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008021 rcu_read_lock();
8022 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11008023 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07008024 if (mddev->safemode == 1)
8025 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11008026 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
NeilBrown81fe48e2017-08-08 16:56:36 +10008027 if (mddev->in_sync || mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11008028 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07008029 if (mddev->in_sync) {
8030 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008031 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8032 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07008033 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008034 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07008035 }
NeilBrown85572d72014-12-15 12:56:56 +11008036 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07008037 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008038 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10008039 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008040 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07008041 wait_event(mddev->sb_wait,
NeilBrownd47c8ad2017-10-05 16:23:16 +11008042 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8043 mddev->suspended);
NeilBrowncc27b0c2017-06-05 16:49:39 +10008044 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8045 percpu_ref_put(&mddev->writes_pending);
8046 return false;
8047 }
8048 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008049}
NeilBrown6c144d32014-09-30 16:15:38 +10008050EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008051
NeilBrown49728052017-03-15 14:05:12 +11008052/* md_write_inc can only be called when md_write_start() has
8053 * already been called at least once of the current request.
8054 * It increments the counter and is useful when a single request
8055 * is split into several parts. Each part causes an increment and
8056 * so needs a matching md_write_end().
8057 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8058 * a spinlocked region.
8059 */
8060void md_write_inc(struct mddev *mddev, struct bio *bi)
8061{
8062 if (bio_data_dir(bi) != WRITE)
8063 return;
8064 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008065 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008066}
8067EXPORT_SYMBOL(md_write_inc);
8068
NeilBrownfd01b882011-10-11 16:47:53 +11008069void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070{
NeilBrown4ad23a972017-03-15 14:05:14 +11008071 percpu_ref_put(&mddev->writes_pending);
8072
8073 if (mddev->safemode == 2)
8074 md_wakeup_thread(mddev->thread);
8075 else if (mddev->safemode_delay)
8076 /* The roundup() ensures this only performs locking once
8077 * every ->safemode_delay jiffies
8078 */
8079 mod_timer(&mddev->safemode_timer,
8080 roundup(jiffies, mddev->safemode_delay) +
8081 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008082}
NeilBrown4ad23a972017-03-15 14:05:14 +11008083
NeilBrown6c144d32014-09-30 16:15:38 +10008084EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008085
NeilBrown2a2275d2007-01-26 00:57:11 -08008086/* md_allow_write(mddev)
8087 * Calling this ensures that the array is marked 'active' so that writes
8088 * may proceed without blocking. It is important to call this before
8089 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8090 * Must be called with mddev_lock held.
8091 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008092void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008093{
8094 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008095 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008096 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008097 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008098 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008099 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008100
NeilBrown85572d72014-12-15 12:56:56 +11008101 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008102 if (mddev->in_sync) {
8103 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008104 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8105 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008106 if (mddev->safemode_delay &&
8107 mddev->safemode == 0)
8108 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008109 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008110 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008111 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008112 /* wait for the dirty state to be recorded in the metadata */
8113 wait_event(mddev->sb_wait,
8114 !test_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags) &&
8115 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008116 } else
NeilBrown85572d72014-12-15 12:56:56 +11008117 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008118}
8119EXPORT_SYMBOL_GPL(md_allow_write);
8120
Linus Torvalds1da177e2005-04-16 15:20:36 -07008121#define SYNC_MARKS 10
8122#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008123#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008124void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008125{
Shaohua Li4ed87312012-10-11 13:34:00 +11008126 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008127 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008128 unsigned int currspeed = 0,
8129 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008130 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008131 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008132 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008133 sector_t mark_cnt[SYNC_MARKS];
8134 int last_mark,m;
8135 struct list_head *tmp;
8136 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008137 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008138 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008139 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008140 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008141 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142
8143 /* just incase thread restarts... */
8144 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
8145 return;
NeilBrown3991b312014-05-28 13:39:23 +10008146 if (mddev->ro) {/* never try to sync a read-only array */
8147 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008148 return;
NeilBrown3991b312014-05-28 13:39:23 +10008149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008150
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008151 if (mddev_is_clustered(mddev)) {
8152 ret = md_cluster_ops->resync_start(mddev);
8153 if (ret)
8154 goto skip;
8155
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008156 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008157 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8158 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8159 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8160 && ((unsigned long long)mddev->curr_resync_completed
8161 < (unsigned long long)mddev->resync_max_sectors))
8162 goto skip;
8163 }
8164
NeilBrown61df9d92006-10-03 01:15:57 -07008165 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008166 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008167 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008168 action = "check";
8169 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008170 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008171 action = "repair";
8172 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008173 desc = "resync";
8174 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8175 desc = "reshape";
8176 else
8177 desc = "recovery";
8178
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008179 mddev->last_sync_action = action ?: desc;
8180
Linus Torvalds1da177e2005-04-16 15:20:36 -07008181 /* we overload curr_resync somewhat here.
8182 * 0 == not engaged in resync at all
8183 * 2 == checking that there is no conflict with another sync
8184 * 1 == like 2, but have yielded to allow conflicting resync to
8185 * commense
8186 * other == active in resync - this many blocks
8187 *
8188 * Before starting a resync we must have set curr_resync to
8189 * 2, and then checked that every "conflicting" array has curr_resync
8190 * less than ours. When we find one that is the same or higher
8191 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8192 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8193 * This will mean we have to start checking from the beginning again.
8194 *
8195 */
8196
8197 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008198 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008199 mddev->curr_resync = 2;
8200
8201 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008202 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008203 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008204 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008205 if (mddev2 == mddev)
8206 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008207 if (!mddev->parallel_resync
8208 && mddev2->curr_resync
8209 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008210 DEFINE_WAIT(wq);
8211 if (mddev < mddev2 && mddev->curr_resync == 2) {
8212 /* arbitrarily yield */
8213 mddev->curr_resync = 1;
8214 wake_up(&resync_wait);
8215 }
8216 if (mddev > mddev2 && mddev->curr_resync == 1)
8217 /* no need to wait here, we can wait the next
8218 * time 'round when curr_resync == 2
8219 */
8220 continue;
NeilBrown97441972008-09-19 11:49:54 +10008221 /* We need to wait 'interruptible' so as not to
8222 * contribute to the load average, and not to
8223 * be caught by 'softlockup'
8224 */
8225 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008226 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008227 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008228 if (mddev2_minor != mddev2->md_minor) {
8229 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008230 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8231 desc, mdname(mddev),
8232 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008234 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008235 if (signal_pending(current))
8236 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008237 schedule();
8238 finish_wait(&resync_wait, &wq);
8239 goto try_again;
8240 }
8241 finish_wait(&resync_wait, &wq);
8242 }
8243 }
8244 } while (mddev->curr_resync < 2);
8245
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008246 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008247 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008249 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008250 */
8251 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008252 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008253 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008254 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8255 j = mddev->resync_min;
8256 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008257 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008258
NeilBrownccfcc3c2006-03-27 01:18:09 -08008259 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10008260 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008261 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008262 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008263 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008264 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008265 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008266 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008267 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008268 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008269 !test_bit(Faulty, &rdev->flags) &&
8270 !test_bit(In_sync, &rdev->flags) &&
8271 rdev->recovery_offset < j)
8272 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008273 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008274
8275 /* If there is a bitmap, we need to make sure all
8276 * writes that started before we added a spare
8277 * complete before we start doing a recovery.
8278 * Otherwise the write might complete and (via
8279 * bitmap_endwrite) set a bit in the bitmap after the
8280 * recovery has checked that bit and skipped that
8281 * region.
8282 */
8283 if (mddev->bitmap) {
8284 mddev->pers->quiesce(mddev, 1);
8285 mddev->pers->quiesce(mddev, 0);
8286 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008288
NeilBrown9d487392016-11-02 14:16:49 +11008289 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8290 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8291 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8292 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008293
NeilBrowneea1bf32009-03-31 14:27:02 +11008294 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008295
NeilBrown57afd892005-06-21 17:17:13 -07008296 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008297 for (m = 0; m < SYNC_MARKS; m++) {
8298 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008299 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008300 }
8301 last_mark = 0;
8302 mddev->resync_mark = mark[last_mark];
8303 mddev->resync_mark_cnt = mark_cnt[last_mark];
8304
8305 /*
8306 * Tune reconstruction:
8307 */
8308 window = 32*(PAGE_SIZE/512);
NeilBrown9d487392016-11-02 14:16:49 +11008309 pr_debug("md: using %dk window, over a total of %lluk.\n",
8310 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008311
8312 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313 last_check = 0;
8314
8315 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008316 pr_debug("md: resuming %s of %s from checkpoint.\n",
8317 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008318 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008319 } else
8320 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008321 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008322 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8323 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008324 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008325
majianpeng7c2c57c2012-07-03 12:12:26 +10008326 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008327 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008328 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008329
NeilBrown57afd892005-06-21 17:17:13 -07008330 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008331
NeilBrown7a91ee12009-05-26 12:57:21 +10008332 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8333 ((mddev->curr_resync > mddev->curr_resync_completed &&
8334 (mddev->curr_resync - mddev->curr_resync_completed)
8335 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008336 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008337 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008338 >= mddev->resync_max - mddev->curr_resync_completed ||
8339 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008340 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008341 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008342 wait_event(mddev->recovery_wait,
8343 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008344 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008345 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8346 j > mddev->recovery_cp)
8347 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008348 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008349 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008350 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008351 }
NeilBrownacb180b2009-04-14 16:28:34 +10008352
NeilBrownc91abf52013-11-19 12:02:01 +11008353 while (j >= mddev->resync_max &&
8354 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008355 /* As this condition is controlled by user-space,
8356 * we can block indefinitely, so use '_interruptible'
8357 * to avoid triggering warnings.
8358 */
8359 flush_signals(current); /* just in case */
8360 wait_event_interruptible(mddev->recovery_wait,
8361 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008362 || test_bit(MD_RECOVERY_INTR,
8363 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008364 }
NeilBrownacb180b2009-04-14 16:28:34 +10008365
NeilBrownc91abf52013-11-19 12:02:01 +11008366 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8367 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008368
NeilBrown09314792015-02-19 16:04:40 +11008369 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008370 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008371 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008372 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008373 }
NeilBrown57afd892005-06-21 17:17:13 -07008374
8375 if (!skipped) { /* actual IO requested */
8376 io_sectors += sectors;
8377 atomic_add(sectors, &mddev->recovery_active);
8378 }
8379
NeilBrowne875ece2011-07-28 11:39:24 +10008380 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8381 break;
8382
Linus Torvalds1da177e2005-04-16 15:20:36 -07008383 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008384 if (j > max_sectors)
8385 /* when skipping, extra large numbers can be returned. */
8386 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008387 if (j > 2)
8388 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008389 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008390 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008391 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008392 * visible in /proc/mdstat
8393 */
8394 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008395
8396 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008397 continue;
8398
NeilBrown57afd892005-06-21 17:17:13 -07008399 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008400 repeat:
8401 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8402 /* step marks */
8403 int next = (last_mark+1) % SYNC_MARKS;
8404
8405 mddev->resync_mark = mark[next];
8406 mddev->resync_mark_cnt = mark_cnt[next];
8407 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008408 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008409 last_mark = next;
8410 }
8411
NeilBrownc91abf52013-11-19 12:02:01 +11008412 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8413 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414
8415 /*
8416 * this loop exits only if either when we are slower than
8417 * the 'hard' speed limit, or the system was IO-idle for
8418 * a jiffy.
8419 * the system might be non-idle CPU-wise, but we only care
8420 * about not overloading the IO subsystem. (things like an
8421 * e2fsck being done on the RAID array should execute fast)
8422 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008423 cond_resched();
8424
Xiao Niac7e50a2014-08-07 09:37:41 -04008425 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8426 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008427 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008428
NeilBrown88202a02006-01-06 00:21:36 -08008429 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008430 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008431 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008432 goto repeat;
8433 }
NeilBrownac8fa412015-02-19 16:55:00 +11008434 if (!is_mddev_idle(mddev, 0)) {
8435 /*
8436 * Give other IO more of a chance.
8437 * The faster the devices, the less we wait.
8438 */
8439 wait_event(mddev->recovery_wait,
8440 !atomic_read(&mddev->recovery_active));
8441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442 }
8443 }
NeilBrown9d487392016-11-02 14:16:49 +11008444 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8445 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8446 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008447 /*
8448 * this also signals 'finished resyncing' to md_stop
8449 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008450 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8452
NeilBrown5ed1df22015-07-24 13:27:08 +10008453 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8454 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008455 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008456 mddev->curr_resync_completed = mddev->curr_resync;
8457 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8458 }
NeilBrown09314792015-02-19 16:04:40 +11008459 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008460
NeilBrowndfc70642008-05-23 13:04:39 -07008461 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008462 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008463 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8464 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8465 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008466 pr_debug("md: checkpointing %s of %s.\n",
8467 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008468 if (test_bit(MD_RECOVERY_ERROR,
8469 &mddev->recovery))
8470 mddev->recovery_cp =
8471 mddev->curr_resync_completed;
8472 else
8473 mddev->recovery_cp =
8474 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008475 }
8476 } else
8477 mddev->recovery_cp = MaxSector;
8478 } else {
8479 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8480 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008481 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008482 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008483 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008484 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008485 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008486 !test_bit(Faulty, &rdev->flags) &&
8487 !test_bit(In_sync, &rdev->flags) &&
8488 rdev->recovery_offset < mddev->curr_resync)
8489 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008490 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008492 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008493 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008494 /* set CHANGE_PENDING here since maybe another update is needed,
8495 * so other nodes are informed. It should be harmless for normal
8496 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008497 set_mask_bits(&mddev->sb_flags, 0,
8498 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008499
NeilBrown23da4222014-12-15 12:57:01 +11008500 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008501 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8502 /* We completed so min/max setting can be forgotten if used. */
8503 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8504 mddev->resync_min = 0;
8505 mddev->resync_max = MaxSector;
8506 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8507 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008508 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008509 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008510 spin_unlock(&mddev->lock);
8511
Linus Torvalds1da177e2005-04-16 15:20:36 -07008512 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008514 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008515}
NeilBrown29269552006-03-27 01:18:10 -08008516EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008517
NeilBrown746d3202013-04-24 11:42:41 +10008518static int remove_and_add_spares(struct mddev *mddev,
8519 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008520{
NeilBrown3cb03002011-10-11 16:45:26 +11008521 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008522 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008523 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008524 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008525
NeilBrownd787be42016-06-02 16:19:53 +10008526 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008527 if ((this == NULL || rdev == this) &&
8528 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008529 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008530 test_bit(Faulty, &rdev->flags) &&
8531 atomic_read(&rdev->nr_pending)==0) {
8532 /* Faulty non-Blocked devices with nr_pending == 0
8533 * never get nr_pending incremented,
8534 * never get Faulty cleared, and never get Blocked set.
8535 * So we can synchronize_rcu now rather than once per device
8536 */
8537 remove_some = true;
8538 set_bit(RemoveSynchronized, &rdev->flags);
8539 }
8540 }
8541
8542 if (remove_some)
8543 synchronize_rcu();
8544 rdev_for_each(rdev, mddev) {
8545 if ((this == NULL || rdev == this) &&
8546 rdev->raid_disk >= 0 &&
8547 !test_bit(Blocked, &rdev->flags) &&
8548 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008549 (!test_bit(In_sync, &rdev->flags) &&
8550 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008551 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008552 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008553 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008554 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008555 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008556 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008557 }
8558 }
NeilBrownd787be42016-06-02 16:19:53 +10008559 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8560 clear_bit(RemoveSynchronized, &rdev->flags);
8561 }
8562
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008563 if (removed && mddev->kobj.sd)
8564 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008565
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008566 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008567 goto no_add;
8568
NeilBrowndafb20f2012-03-19 12:46:39 +11008569 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008570 if (this && this != rdev)
8571 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008572 if (test_bit(Candidate, &rdev->flags))
8573 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008574 if (rdev->raid_disk >= 0 &&
8575 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008576 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008577 !test_bit(Faulty, &rdev->flags))
8578 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008579 if (rdev->raid_disk >= 0)
8580 continue;
8581 if (test_bit(Faulty, &rdev->flags))
8582 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008583 if (!test_bit(Journal, &rdev->flags)) {
8584 if (mddev->ro &&
8585 ! (rdev->saved_raid_disk >= 0 &&
8586 !test_bit(Bitmap_sync, &rdev->flags)))
8587 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008588
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008589 rdev->recovery_offset = 0;
8590 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008591 if (mddev->pers->
8592 hot_add_disk(mddev, rdev) == 0) {
8593 if (sysfs_link_rdev(mddev, rdev))
8594 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008595 if (!test_bit(Journal, &rdev->flags))
8596 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008597 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08008598 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008599 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008600 }
NeilBrown746d3202013-04-24 11:42:41 +10008601no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008602 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08008603 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008604 return spares;
8605}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008606
NeilBrownac05f252014-09-30 08:10:42 +10008607static void md_start_sync(struct work_struct *ws)
8608{
8609 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008610
NeilBrownac05f252014-09-30 08:10:42 +10008611 mddev->sync_thread = md_register_thread(md_do_sync,
8612 mddev,
8613 "resync");
8614 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11008615 pr_warn("%s: could not start resync thread...\n",
8616 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008617 /* leave the spares where they are, it shouldn't hurt */
8618 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8619 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8620 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8621 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8622 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008623 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008624 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8625 &mddev->recovery))
8626 if (mddev->sysfs_action)
8627 sysfs_notify_dirent_safe(mddev->sysfs_action);
8628 } else
8629 md_wakeup_thread(mddev->sync_thread);
8630 sysfs_notify_dirent_safe(mddev->sysfs_action);
8631 md_new_event(mddev);
8632}
8633
Linus Torvalds1da177e2005-04-16 15:20:36 -07008634/*
8635 * This routine is regularly called by all per-raid-array threads to
8636 * deal with generic issues like resync and super-block update.
8637 * Raid personalities that don't have a thread (linear/raid0) do not
8638 * need this as they never do any recovery or update the superblock.
8639 *
8640 * It does not do any resync itself, but rather "forks" off other threads
8641 * to do that as needed.
8642 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8643 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008644 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645 * and wakeups up this thread which will reap the thread and finish up.
8646 * This thread also removes any faulty devices (with nr_pending == 0).
8647 *
8648 * The overall approach is:
8649 * 1/ if the superblock needs updating, update it.
8650 * 2/ If a recovery thread is running, don't do anything else.
8651 * 3/ If recovery has finished, clean up, possibly marking spares active.
8652 * 4/ If there are any faulty devices, remove them.
8653 * 5/ If array is degraded, try to add spares devices
8654 * 6/ If array has spares or is not in-sync, start a resync thread.
8655 */
NeilBrownfd01b882011-10-11 16:47:53 +11008656void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008657{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008658 if (mddev->suspended)
8659 return;
8660
NeilBrown5f404022005-06-21 17:17:16 -07008661 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008662 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008663
NeilBrownfca4d842005-06-21 17:17:11 -07008664 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008665 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11008666 pr_debug("md: %s in immediate safe mode\n",
8667 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07008668 mddev->safemode = 2;
8669 }
8670 flush_signals(current);
8671 }
8672
NeilBrownc89a8ee2008-08-05 15:54:13 +10008673 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8674 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008675 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08008676 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008678 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008679 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11008680 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07008681 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008682 ))
8683 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008684
NeilBrowndf5b89b2006-03-27 01:18:20 -08008685 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008686 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008687
Shaohua Liafc1f552017-08-11 20:34:45 -07008688 if (!mddev->external && mddev->safemode == 1)
NeilBrown33182d12017-08-08 16:56:36 +10008689 mddev->safemode = 0;
8690
NeilBrownc89a8ee2008-08-05 15:54:13 +10008691 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008692 struct md_rdev *rdev;
8693 if (!mddev->external && mddev->in_sync)
8694 /* 'Blocked' flag not needed as failed devices
8695 * will be recorded if array switched to read/write.
8696 * Leaving it set will prevent the device
8697 * from being removed.
8698 */
8699 rdev_for_each(rdev, mddev)
8700 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008701 /* On a read-only array we can:
8702 * - remove failed devices
8703 * - add already-in_sync devices if the array itself
8704 * is in-sync.
8705 * As we only add devices that are already in-sync,
8706 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008707 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008708 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008709 /* There is no thread, but we need to call
8710 * ->spare_active and clear saved_raid_disk
8711 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008712 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008713 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008714 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008715 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08008716 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008717 goto unlock;
8718 }
8719
Guoqing Jiang659b2542015-12-21 10:50:59 +11008720 if (mddev_is_clustered(mddev)) {
8721 struct md_rdev *rdev;
8722 /* kick the device if another node issued a
8723 * remove disk.
8724 */
8725 rdev_for_each(rdev, mddev) {
8726 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8727 rdev->raid_disk < 0)
8728 md_kick_rdev_from_array(rdev);
8729 }
8730 }
8731
NeilBrown6497709b2017-03-15 14:05:14 +11008732 if (!mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11008733 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11008734 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11008735 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07008736 }
NeilBrownfca4d842005-06-21 17:17:11 -07008737
Shaohua Li29530792016-12-08 15:48:19 -08008738 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07008739 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008740
Linus Torvalds1da177e2005-04-16 15:20:36 -07008741 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8742 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8743 /* resync/recovery still happening */
8744 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8745 goto unlock;
8746 }
8747 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008748 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008749 goto unlock;
8750 }
Neil Brown72a23c22008-06-28 08:31:41 +10008751 /* Set RUNNING before clearing NEEDED to avoid
8752 * any transients in the value of "sync_action".
8753 */
NeilBrown72f36d52012-10-11 14:25:57 +11008754 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008755 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008756 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008757 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008758 /* Clear some bits that don't mean anything, but
8759 * might be left set
8760 */
NeilBrown24dd4692005-11-08 21:39:26 -08008761 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8762 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008763
NeilBrowned209582012-04-24 10:23:14 +10008764 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8765 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008766 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008767 /* no recovery is running.
8768 * remove any failed drives, then
8769 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008770 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008771 * the personality to fail the re-add.
8772 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008774 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008775 if (mddev->pers->check_reshape == NULL ||
8776 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008777 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008778 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008779 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008780 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008781 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008782 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8783 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008784 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008785 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008786 } else if (mddev->recovery_cp < MaxSector) {
8787 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008788 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008789 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8790 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008791 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008792
Linus Torvalds1da177e2005-04-16 15:20:36 -07008793 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008794 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008795 /* We are adding a device or devices to an array
8796 * which has the bitmap stored on all devices.
8797 * So make sure all bitmap pages get written
8798 */
8799 bitmap_write_all(mddev->bitmap);
8800 }
NeilBrownac05f252014-09-30 08:10:42 +10008801 INIT_WORK(&mddev->del_work, md_start_sync);
8802 queue_work(md_misc_wq, &mddev->del_work);
8803 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008804 }
NeilBrownac05f252014-09-30 08:10:42 +10008805 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008806 if (!mddev->sync_thread) {
8807 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008808 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008809 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8810 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008811 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008812 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008813 }
NeilBrownac05f252014-09-30 08:10:42 +10008814 unlock:
8815 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008816 mddev_unlock(mddev);
8817 }
8818}
NeilBrown6c144d32014-09-30 16:15:38 +10008819EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008820
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008821void md_reap_sync_thread(struct mddev *mddev)
8822{
8823 struct md_rdev *rdev;
8824
8825 /* resync has finished, collect result */
8826 md_unregister_thread(&mddev->sync_thread);
8827 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8828 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8829 /* success...*/
8830 /* activate any spares */
8831 if (mddev->pers->spare_active(mddev)) {
8832 sysfs_notify(&mddev->kobj, NULL,
8833 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08008834 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008835 }
8836 }
8837 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8838 mddev->pers->finish_reshape)
8839 mddev->pers->finish_reshape(mddev);
8840
8841 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008842 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008843 */
NeilBrownf4667222013-12-09 12:04:56 +11008844 if (!mddev->degraded)
8845 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008846 rdev->saved_raid_disk = -1;
8847
8848 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08008849 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008850 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8851 * clustered raid */
8852 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8853 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008854 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008855 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008856 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8857 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8858 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8859 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008860 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008861 /* flag recovery needed just to double check */
8862 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8863 sysfs_notify_dirent_safe(mddev->sysfs_action);
8864 md_new_event(mddev);
8865 if (mddev->event_work.func)
8866 queue_work(md_misc_wq, &mddev->event_work);
8867}
NeilBrown6c144d32014-09-30 16:15:38 +10008868EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008869
NeilBrownfd01b882011-10-11 16:47:53 +11008870void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008871{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008872 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008873 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008874 !test_bit(Blocked, &rdev->flags) &&
8875 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008876 msecs_to_jiffies(5000));
8877 rdev_dec_pending(rdev, mddev);
8878}
8879EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8880
NeilBrownc6563a82012-05-21 09:27:00 +10008881void md_finish_reshape(struct mddev *mddev)
8882{
8883 /* called be personality module when reshape completes. */
8884 struct md_rdev *rdev;
8885
8886 rdev_for_each(rdev, mddev) {
8887 if (rdev->data_offset > rdev->new_data_offset)
8888 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8889 else
8890 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8891 rdev->data_offset = rdev->new_data_offset;
8892 }
8893}
8894EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008895
Vishal Vermafc974ee2015-12-24 19:20:34 -07008896/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10008897
Vishal Vermafc974ee2015-12-24 19:20:34 -07008898/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11008899int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008900 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008901{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008902 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10008903 int rv;
8904 if (is_new)
8905 s += rdev->new_data_offset;
8906 else
8907 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008908 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8909 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008910 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008911 if (test_bit(ExternalBbl, &rdev->flags))
8912 sysfs_notify(&rdev->kobj, NULL,
8913 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008914 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08008915 set_mask_bits(&mddev->sb_flags, 0,
8916 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10008917 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07008918 return 1;
8919 } else
8920 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008921}
8922EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8923
NeilBrownc6563a82012-05-21 09:27:00 +10008924int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8925 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008926{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008927 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10008928 if (is_new)
8929 s += rdev->new_data_offset;
8930 else
8931 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008932 rv = badblocks_clear(&rdev->badblocks, s, sectors);
8933 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
8934 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
8935 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008936}
8937EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8938
Adrian Bunk75c96f82005-05-05 16:16:09 -07008939static int md_notify_reboot(struct notifier_block *this,
8940 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008941{
8942 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008943 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008944 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945
NeilBrownc744a652012-03-19 12:46:37 +11008946 for_each_mddev(mddev, tmp) {
8947 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008948 if (mddev->pers)
8949 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008950 if (mddev->persistent)
8951 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008952 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008953 }
NeilBrownc744a652012-03-19 12:46:37 +11008954 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955 }
NeilBrownc744a652012-03-19 12:46:37 +11008956 /*
8957 * certain more exotic SCSI devices are known to be
8958 * volatile wrt too early system reboots. While the
8959 * right place to handle this issue is the given
8960 * driver, we do want to have a safe RAID driver ...
8961 */
8962 if (need_delay)
8963 mdelay(1000*1);
8964
Linus Torvalds1da177e2005-04-16 15:20:36 -07008965 return NOTIFY_DONE;
8966}
8967
Adrian Bunk75c96f82005-05-05 16:16:09 -07008968static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008969 .notifier_call = md_notify_reboot,
8970 .next = NULL,
8971 .priority = INT_MAX, /* before any real devices */
8972};
8973
8974static void md_geninit(void)
8975{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008976 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008977
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008978 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008979}
8980
Adrian Bunk75c96f82005-05-05 16:16:09 -07008981static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008982{
Tejun Heoe804ac72010-10-15 15:36:08 +02008983 int ret = -ENOMEM;
8984
Tejun Heoada609e2011-01-25 14:35:54 +01008985 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008986 if (!md_wq)
8987 goto err_wq;
8988
8989 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8990 if (!md_misc_wq)
8991 goto err_misc_wq;
8992
8993 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8994 goto err_md;
8995
8996 if ((ret = register_blkdev(0, "mdp")) < 0)
8997 goto err_mdp;
8998 mdp_major = ret;
8999
NeilBrownaf5628f2014-07-31 13:54:54 +10009000 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009001 md_probe, NULL, NULL);
9002 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009003 md_probe, NULL, NULL);
9004
Linus Torvalds1da177e2005-04-16 15:20:36 -07009005 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009006 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009007
9008 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009009 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009010
Tejun Heoe804ac72010-10-15 15:36:08 +02009011err_mdp:
9012 unregister_blkdev(MD_MAJOR, "md");
9013err_md:
9014 destroy_workqueue(md_misc_wq);
9015err_misc_wq:
9016 destroy_workqueue(md_wq);
9017err_wq:
9018 return ret;
9019}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009020
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009021static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009022{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009023 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9024 struct md_rdev *rdev2;
9025 int role, ret;
9026 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009027
Guoqing Jiang818da592017-03-01 16:42:40 +08009028 /*
9029 * If size is changed in another node then we need to
9030 * do resize as well.
9031 */
9032 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9033 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9034 if (ret)
9035 pr_info("md-cluster: resize failed\n");
9036 else
9037 bitmap_update_sb(mddev->bitmap);
9038 }
9039
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009040 /* Check for change of roles in the active devices */
9041 rdev_for_each(rdev2, mddev) {
9042 if (test_bit(Faulty, &rdev2->flags))
9043 continue;
9044
9045 /* Check if the roles changed */
9046 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009047
9048 if (test_bit(Candidate, &rdev2->flags)) {
9049 if (role == 0xfffe) {
9050 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9051 md_kick_rdev_from_array(rdev2);
9052 continue;
9053 }
9054 else
9055 clear_bit(Candidate, &rdev2->flags);
9056 }
9057
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009058 if (role != rdev2->raid_disk) {
9059 /* got activated */
9060 if (rdev2->raid_disk == -1 && role != 0xffff) {
9061 rdev2->saved_raid_disk = role;
9062 ret = remove_and_add_spares(mddev, rdev2);
9063 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009064 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009065 /* wakeup mddev->thread here, so array could
9066 * perform resync with the new activated disk */
9067 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9068 md_wakeup_thread(mddev->thread);
9069
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009070 }
9071 /* device faulty
9072 * We just want to do the minimum to mark the disk
9073 * as faulty. The recovery is performed by the
9074 * one who initiated the error.
9075 */
9076 if ((role == 0xfffe) || (role == 0xfffd)) {
9077 md_error(mddev, rdev2);
9078 clear_bit(Blocked, &rdev2->flags);
9079 }
9080 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009081 }
9082
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009083 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9084 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009085
9086 /* Finally set the event to be up to date */
9087 mddev->events = le64_to_cpu(sb->events);
9088}
9089
9090static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9091{
9092 int err;
9093 struct page *swapout = rdev->sb_page;
9094 struct mdp_superblock_1 *sb;
9095
9096 /* Store the sb page of the rdev in the swapout temporary
9097 * variable in case we err in the future
9098 */
9099 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009100 err = alloc_disk_sb(rdev);
9101 if (err == 0) {
9102 ClearPageUptodate(rdev->sb_page);
9103 rdev->sb_loaded = 0;
9104 err = super_types[mddev->major_version].
9105 load_super(rdev, NULL, mddev->minor_version);
9106 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009107 if (err < 0) {
9108 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9109 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009110 if (rdev->sb_page)
9111 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009112 rdev->sb_page = swapout;
9113 rdev->sb_loaded = 1;
9114 return err;
9115 }
9116
9117 sb = page_address(rdev->sb_page);
9118 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9119 * is not set
9120 */
9121
9122 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9123 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9124
9125 /* The other node finished recovery, call spare_active to set
9126 * device In_sync and mddev->degraded
9127 */
9128 if (rdev->recovery_offset == MaxSector &&
9129 !test_bit(In_sync, &rdev->flags) &&
9130 mddev->pers->spare_active(mddev))
9131 sysfs_notify(&mddev->kobj, NULL, "degraded");
9132
9133 put_page(swapout);
9134 return 0;
9135}
9136
9137void md_reload_sb(struct mddev *mddev, int nr)
9138{
9139 struct md_rdev *rdev;
9140 int err;
9141
9142 /* Find the rdev */
9143 rdev_for_each_rcu(rdev, mddev) {
9144 if (rdev->desc_nr == nr)
9145 break;
9146 }
9147
9148 if (!rdev || rdev->desc_nr != nr) {
9149 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9150 return;
9151 }
9152
9153 err = read_rdev(mddev, rdev);
9154 if (err < 0)
9155 return;
9156
9157 check_sb_changes(mddev, rdev);
9158
9159 /* Read all rdev's to update recovery_offset */
9160 rdev_for_each_rcu(rdev, mddev)
9161 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009162}
9163EXPORT_SYMBOL(md_reload_sb);
9164
Linus Torvalds1da177e2005-04-16 15:20:36 -07009165#ifndef MODULE
9166
9167/*
9168 * Searches all registered partitions for autorun RAID arrays
9169 * at boot time.
9170 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009171
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009172static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009173static LIST_HEAD(all_detected_devices);
9174struct detected_devices_node {
9175 struct list_head list;
9176 dev_t dev;
9177};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009178
9179void md_autodetect_dev(dev_t dev)
9180{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009181 struct detected_devices_node *node_detected_dev;
9182
9183 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9184 if (node_detected_dev) {
9185 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009186 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009187 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009188 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009190}
9191
Linus Torvalds1da177e2005-04-16 15:20:36 -07009192static void autostart_arrays(int part)
9193{
NeilBrown3cb03002011-10-11 16:45:26 +11009194 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009195 struct detected_devices_node *node_detected_dev;
9196 dev_t dev;
9197 int i_scanned, i_passed;
9198
9199 i_scanned = 0;
9200 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009201
NeilBrown9d487392016-11-02 14:16:49 +11009202 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009203
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009204 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009205 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9206 i_scanned++;
9207 node_detected_dev = list_entry(all_detected_devices.next,
9208 struct detected_devices_node, list);
9209 list_del(&node_detected_dev->list);
9210 dev = node_detected_dev->dev;
9211 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009212 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009213 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009214 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009215 if (IS_ERR(rdev))
9216 continue;
9217
NeilBrown403df472014-09-30 15:52:29 +10009218 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009219 continue;
NeilBrown403df472014-09-30 15:52:29 +10009220
NeilBrownd0fae182008-03-04 14:29:31 -08009221 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009222 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009223 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009224 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009225 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009226
NeilBrown9d487392016-11-02 14:16:49 +11009227 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009228
9229 autorun_devices(part);
9230}
9231
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009232#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009233
9234static __exit void md_exit(void)
9235{
NeilBrownfd01b882011-10-11 16:47:53 +11009236 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009237 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009238 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009239
NeilBrownaf5628f2014-07-31 13:54:54 +10009240 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009241 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009242
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009243 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009244 unregister_blkdev(mdp_major, "mdp");
9245 unregister_reboot_notifier(&md_notifier);
9246 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009247
9248 /* We cannot unload the modules while some process is
9249 * waiting for us in select() or poll() - wake them up
9250 */
9251 md_unloading = 1;
9252 while (waitqueue_active(&md_event_waiters)) {
9253 /* not safe to leave yet */
9254 wake_up(&md_event_waiters);
9255 msleep(delay);
9256 delay += delay;
9257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009258 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009259
NeilBrown29ac4aa2008-02-06 01:39:58 -08009260 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009262 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009263 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009264 /*
9265 * for_each_mddev() will call mddev_put() at the end of each
9266 * iteration. As the mddev is now fully clear, this will
9267 * schedule the mddev for destruction by a workqueue, and the
9268 * destroy_workqueue() below will wait for that to complete.
9269 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009270 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009271 destroy_workqueue(md_misc_wq);
9272 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009273}
9274
Dan Williams685784a2007-07-09 11:56:42 -07009275subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009276module_exit(md_exit)
9277
NeilBrownf91de922005-11-08 21:39:36 -08009278static int get_ro(char *buffer, struct kernel_param *kp)
9279{
9280 return sprintf(buffer, "%d", start_readonly);
9281}
9282static int set_ro(const char *val, struct kernel_param *kp)
9283{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009284 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009285}
9286
NeilBrown80ca3a42006-07-10 04:44:18 -07009287module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9288module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009289module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009290module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009291
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009293MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009294MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009295MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);