blob: 2cf0e89cc56c9ae051b0a92ebbc6200175001c33 [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
NeilBrowna6fb0932005-09-09 16:23:56 -070047#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110048#include <linux/blkdev.h>
Vishal Vermafc974ee2015-12-24 19:20:34 -070049#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040052#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080053#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070054#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080055#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110056#include <linux/hdreg.h>
57#include <linux/proc_fs.h>
58#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040059#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110060#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070061#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110062#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110063#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110064#include <linux/raid/md_p.h>
65#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090066#include <linux/slab.h>
Shaohua Li504634f2016-11-18 09:44:08 -080067#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110068#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110069#include "bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050070#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110073static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#endif
75
NeilBrown01f96c02011-09-21 15:30:20 +100076/* pers_list is a list of registered personalities protected
77 * by pers_lock.
78 * pers_lock does extra service to protect accesses to
79 * mddev->thread when the mutex cannot be held.
80 */
NeilBrown2604b702006-01-06 00:20:36 -080081static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static DEFINE_SPINLOCK(pers_lock);
83
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050084struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050085EXPORT_SYMBOL(md_cluster_ops);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050086struct module *md_cluster_mod;
87EXPORT_SYMBOL(md_cluster_mod);
88
Bernd Schubert90b08712008-05-23 13:04:38 -070089static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020090static struct workqueue_struct *md_wq;
91static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070092
NeilBrown746d3202013-04-24 11:42:41 +100093static int remove_and_add_spares(struct mddev *mddev,
94 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110095static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*
Robert Becker1e509152009-12-14 12:49:58 +110098 * Default number of read corrections we'll attempt on an rdev
99 * before ejecting it from the array. We divide the read error
100 * count by 2 for every hour elapsed between read errors.
101 */
102#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
103/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
105 * is 1000 KB/sec, so the extra system load does not show up that much.
106 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -0700107 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * subsystem is idle. There is also an 'absolute maximum' reconstruction
109 * speed limit - in case reconstruction slows down your system despite
110 * idle IO detection.
111 *
112 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800113 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
115
116static int sysctl_speed_limit_min = 1000;
117static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100118static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800119{
120 return mddev->sync_speed_min ?
121 mddev->sync_speed_min : sysctl_speed_limit_min;
122}
123
NeilBrownfd01b882011-10-11 16:47:53 +1100124static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800125{
126 return mddev->sync_speed_max ?
127 mddev->sync_speed_max : sysctl_speed_limit_max;
128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130static struct ctl_table_header *raid_table_header;
131
Joe Perches82592c32013-11-14 15:16:18 +1100132static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .procname = "speed_limit_min",
135 .data = &sysctl_speed_limit_min,
136 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700137 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800138 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 },
140 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .procname = "speed_limit_max",
142 .data = &sysctl_speed_limit_max,
143 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700144 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800145 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800147 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Joe Perches82592c32013-11-14 15:16:18 +1100150static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 .procname = "raid",
153 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700154 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .child = raid_table,
156 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800157 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
Joe Perches82592c32013-11-14 15:16:18 +1100160static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 .procname = "dev",
163 .maxlen = 0,
164 .mode = 0555,
165 .child = raid_dir_table,
166 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800167 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168};
169
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700170static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
NeilBrownf91de922005-11-08 21:39:36 -0800172static int start_readonly;
173
NeilBrowna167f662010-10-26 18:31:13 +1100174/* bio_clone_mddev
175 * like bio_clone, but with a local bio set
176 */
177
NeilBrowna167f662010-10-26 18:31:13 +1100178struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100179 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100180{
181 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100182
183 if (!mddev || !mddev->bio_set)
184 return bio_alloc(gfp_mask, nr_iovecs);
185
Kent Overstreet395c72a2012-09-06 15:34:55 -0700186 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100187 if (!b)
188 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100189 return b;
190}
191EXPORT_SYMBOL_GPL(bio_alloc_mddev);
192
193struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100194 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100195{
NeilBrowna167f662010-10-26 18:31:13 +1100196 if (!mddev || !mddev->bio_set)
197 return bio_clone(bio, gfp_mask);
198
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700199 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100200}
201EXPORT_SYMBOL_GPL(bio_clone_mddev);
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*
NeilBrownd7603b72006-01-06 00:20:30 -0800204 * We have a system wide 'event count' that is incremented
205 * on any 'interesting' event, and readers of /proc/mdstat
206 * can use 'poll' or 'select' to find out when the event
207 * count increases.
208 *
209 * Events are:
210 * start array, stop array, error, add device, remove device,
211 * start build, activate spare
212 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800213static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800214static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100215void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800216{
217 atomic_inc(&md_event_count);
218 wake_up(&md_event_waiters);
219}
NeilBrown29269552006-03-27 01:18:10 -0800220EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800221
222/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * Enables to iterate over all existing md arrays
224 * all_mddevs_lock protects this list.
225 */
226static LIST_HEAD(all_mddevs);
227static DEFINE_SPINLOCK(all_mddevs_lock);
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/*
230 * iterates through all used mddevs in the system.
231 * We take care to grab the all_mddevs_lock whenever navigating
232 * the list, and to always hold a refcount when unlocked.
233 * Any code which breaks out of this loop while own
234 * a reference to the current mddev and must mddev_put it.
235 */
NeilBrownfd01b882011-10-11 16:47:53 +1100236#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000238 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100239 _tmp = all_mddevs.next; \
240 _mddev = NULL;}); \
241 ({ if (_tmp != &all_mddevs) \
242 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100244 if (_mddev) mddev_put(_mddev); \
245 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
246 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100248 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 )
250
NeilBrown409c57f2009-03-31 14:39:39 +1100251/* Rather than calling directly into the personality make_request function,
252 * IO requests come here first so that we can check if the device is
253 * being suspended pending a reconfiguration.
254 * We hold a refcount over the call to ->make_request. By the time that
255 * call has finished, the bio has been linked into some internal structure
256 * and so is visible to ->quiesce(), so we don't need the refcount any more.
257 */
Jens Axboedece1632015-11-05 10:41:16 -0700258static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
NeilBrown49077322010-03-25 16:20:56 +1100260 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100261 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500262 unsigned int sectors;
Gu Zheng74672d02015-04-03 08:44:47 +0800263 int cpu;
NeilBrown49077322010-03-25 16:20:56 +1100264
Kent Overstreet54efd502015-04-23 22:37:18 -0700265 blk_queue_split(q, &bio, q->bio_split);
266
NeilBrown274d8cb2016-01-04 16:16:58 +1100267 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100268 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700269 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100270 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100271 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200272 if (bio_sectors(bio) != 0)
273 bio->bi_error = -EROFS;
274 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700275 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100276 }
NeilBrown0ca69882011-01-14 09:14:33 +1100277 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100278 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200279 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100280 DEFINE_WAIT(__wait);
281 for (;;) {
282 prepare_to_wait(&mddev->sb_wait, &__wait,
283 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200284 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100285 break;
286 rcu_read_unlock();
287 schedule();
288 rcu_read_lock();
289 }
290 finish_wait(&mddev->sb_wait, &__wait);
291 }
292 atomic_inc(&mddev->active_io);
293 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100294
Chris Masone91ece52011-02-07 19:21:48 -0500295 /*
296 * save the sectors now since our bio can
297 * go away inside make_request
298 */
299 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700300 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600301 bio->bi_opf &= ~REQ_NOMERGE;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200302 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100303
Gu Zheng74672d02015-04-03 08:44:47 +0800304 cpu = part_stat_lock();
305 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
306 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
307 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100308
NeilBrown409c57f2009-03-31 14:39:39 +1100309 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
310 wake_up(&mddev->sb_wait);
Jens Axboedece1632015-11-05 10:41:16 -0700311
312 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
NeilBrown9e35b992010-04-06 14:23:02 +1000315/* mddev_suspend makes sure no new requests are submitted
316 * to the device, and that any requests that have been submitted
317 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100318 * Once mddev_detach() is called and completes, the module will be
319 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000320 */
NeilBrownfd01b882011-10-11 16:47:53 +1100321void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100322{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200323 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100324 if (mddev->suspended++)
325 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100326 synchronize_rcu();
327 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
328 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500329
330 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100331}
NeilBrown390ee602010-06-01 19:37:27 +1000332EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100333
NeilBrownfd01b882011-10-11 16:47:53 +1100334void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100335{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100336 if (--mddev->suspended)
337 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100338 wake_up(&mddev->sb_wait);
339 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500340
Jonathan Brassow47525e52012-05-22 13:55:29 +1000341 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500342 md_wakeup_thread(mddev->thread);
343 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100344}
NeilBrown390ee602010-06-01 19:37:27 +1000345EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100346
NeilBrownfd01b882011-10-11 16:47:53 +1100347int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000348{
NeilBrown5c675f82014-12-15 12:56:56 +1100349 struct md_personality *pers = mddev->pers;
350 int ret = 0;
351
352 rcu_read_lock();
353 if (mddev->suspended)
354 ret = 1;
355 else if (pers && pers->congested)
356 ret = pers->congested(mddev, bits);
357 rcu_read_unlock();
358 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000359}
NeilBrown5c675f82014-12-15 12:56:56 +1100360EXPORT_SYMBOL_GPL(mddev_congested);
361static int md_congested(void *data, int bits)
362{
363 struct mddev *mddev = data;
364 return mddev_congested(mddev, bits);
365}
NeilBrown3fa841d2009-09-23 18:10:29 +1000366
NeilBrowna2826aa2009-12-14 12:49:49 +1100367/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200368 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100369 */
370
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200371static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100372{
NeilBrown3cb03002011-10-11 16:45:26 +1100373 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100374 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100375
376 rdev_dec_pending(rdev, mddev);
377
378 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200379 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200380 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100381 }
382 bio_put(bio);
383}
384
NeilBrowna7a07e62010-12-09 16:04:25 +1100385static void md_submit_flush_data(struct work_struct *ws);
386
NeilBrowna035fc32010-12-09 16:17:51 +1100387static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100388{
NeilBrownfd01b882011-10-11 16:47:53 +1100389 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100390 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100391
NeilBrowna7a07e62010-12-09 16:04:25 +1100392 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
393 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100394 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100395 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100396 if (rdev->raid_disk >= 0 &&
397 !test_bit(Faulty, &rdev->flags)) {
398 /* Take two references, one is dropped
399 * when request finishes, one after
400 * we reclaim rcu_read_lock
401 */
402 struct bio *bi;
403 atomic_inc(&rdev->nr_pending);
404 atomic_inc(&rdev->nr_pending);
405 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000406 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200407 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100408 bi->bi_private = rdev;
409 bi->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -0500410 bio_set_op_attrs(bi, REQ_OP_WRITE, WRITE_FLUSH);
NeilBrowna2826aa2009-12-14 12:49:49 +1100411 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500412 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100413 rcu_read_lock();
414 rdev_dec_pending(rdev, mddev);
415 }
416 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100417 if (atomic_dec_and_test(&mddev->flush_pending))
418 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100419}
420
Tejun Heoe9c74692010-09-03 11:56:18 +0200421static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100422{
NeilBrownfd01b882011-10-11 16:47:53 +1100423 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200424 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100425
Kent Overstreet4f024f32013-10-11 15:44:27 -0700426 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100427 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200428 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100429 else {
Jens Axboe1eff9d32016-08-05 15:35:16 -0600430 bio->bi_opf &= ~REQ_PREFLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200431 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100432 }
NeilBrown2b74e122010-12-09 15:59:01 +1100433
434 mddev->flush_bio = NULL;
435 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100436}
437
NeilBrownfd01b882011-10-11 16:47:53 +1100438void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100439{
NeilBrown85572d72014-12-15 12:56:56 +1100440 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100441 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200442 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100443 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200444 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100445 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100446
NeilBrowna035fc32010-12-09 16:17:51 +1100447 INIT_WORK(&mddev->flush_work, submit_flushes);
448 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100449}
Tejun Heoe9c74692010-09-03 11:56:18 +0200450EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100451
NeilBrown74018dc2012-07-31 09:08:15 +0200452void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000453{
NeilBrown9cbb1752012-07-31 09:08:14 +0200454 struct mddev *mddev = cb->data;
455 md_wakeup_thread(mddev->thread);
456 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000457}
NeilBrown9cbb1752012-07-31 09:08:14 +0200458EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000459
NeilBrownfd01b882011-10-11 16:47:53 +1100460static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 atomic_inc(&mddev->active);
463 return mddev;
464}
465
Dan Williams5fd3a172009-03-04 00:57:25 -0700466static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100467
NeilBrownfd01b882011-10-11 16:47:53 +1100468static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
NeilBrowna167f662010-10-26 18:31:13 +1100470 struct bio_set *bs = NULL;
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
473 return;
NeilBrownd3374822009-01-09 08:31:10 +1100474 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100475 mddev->ctime == 0 && !mddev->hold_active) {
476 /* Array is not configured at all, and not held active,
477 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100478 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100479 bs = mddev->bio_set;
480 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100481 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200482 /* We did a probe so need to clean up. Call
483 * queue_work inside the spinlock so that
484 * flush_workqueue() after mddev_find will
485 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100486 */
487 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200488 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100489 } else
490 kfree(mddev);
491 }
492 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100493 if (bs)
494 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Sasha Levin25b2edf2015-07-24 18:19:58 -0400497static void md_safemode_timeout(unsigned long data);
498
NeilBrownfd01b882011-10-11 16:47:53 +1100499void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100500{
501 mutex_init(&mddev->open_mutex);
502 mutex_init(&mddev->reconfig_mutex);
503 mutex_init(&mddev->bitmap_info.mutex);
504 INIT_LIST_HEAD(&mddev->disks);
505 INIT_LIST_HEAD(&mddev->all_mddevs);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400506 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
507 (unsigned long) mddev);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100508 atomic_set(&mddev->active, 1);
509 atomic_set(&mddev->openers, 0);
510 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100511 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100512 atomic_set(&mddev->flush_pending, 0);
513 init_waitqueue_head(&mddev->sb_wait);
514 init_waitqueue_head(&mddev->recovery_wait);
515 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000516 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500517 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100518 mddev->resync_min = 0;
519 mddev->resync_max = MaxSector;
520 mddev->level = LEVEL_NONE;
521}
NeilBrown390ee602010-06-01 19:37:27 +1000522EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100523
NeilBrownf72ffdd2014-09-30 14:23:59 +1000524static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
NeilBrownfd01b882011-10-11 16:47:53 +1100526 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
NeilBrown8f5f02c2011-02-16 13:58:51 +1100528 if (unit && MAJOR(unit) != MD_MAJOR)
529 unit &= ~((1<<MdpMinorShift)-1);
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 retry:
532 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
NeilBrownefeb53c2009-01-09 08:31:10 +1100534 if (unit) {
535 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
536 if (mddev->unit == unit) {
537 mddev_get(mddev);
538 spin_unlock(&all_mddevs_lock);
539 kfree(new);
540 return mddev;
541 }
542
543 if (new) {
544 list_add(&new->all_mddevs, &all_mddevs);
545 spin_unlock(&all_mddevs_lock);
546 new->hold_active = UNTIL_IOCTL;
547 return new;
548 }
549 } else if (new) {
550 /* find an unused unit number */
551 static int next_minor = 512;
552 int start = next_minor;
553 int is_free = 0;
554 int dev = 0;
555 while (!is_free) {
556 dev = MKDEV(MD_MAJOR, next_minor);
557 next_minor++;
558 if (next_minor > MINORMASK)
559 next_minor = 0;
560 if (next_minor == start) {
561 /* Oh dear, all in use. */
562 spin_unlock(&all_mddevs_lock);
563 kfree(new);
564 return NULL;
565 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000566
NeilBrownefeb53c2009-01-09 08:31:10 +1100567 is_free = 1;
568 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
569 if (mddev->unit == dev) {
570 is_free = 0;
571 break;
572 }
573 }
574 new->unit = dev;
575 new->md_minor = MINOR(dev);
576 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 list_add(&new->all_mddevs, &all_mddevs);
578 spin_unlock(&all_mddevs_lock);
579 return new;
580 }
581 spin_unlock(&all_mddevs_lock);
582
NeilBrown9ffae0c2006-01-06 00:20:32 -0800583 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (!new)
585 return NULL;
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 new->unit = unit;
588 if (MAJOR(unit) == MD_MAJOR)
589 new->md_minor = MINOR(unit);
590 else
591 new->md_minor = MINOR(unit) >> MdpMinorShift;
592
NeilBrownfafd7fb2010-04-01 15:55:30 +1100593 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 goto retry;
596}
597
NeilBrownb6eb1272010-04-15 10:13:47 +1000598static struct attribute_group md_redundancy_group;
599
NeilBrown5c47daf2014-12-15 12:57:01 +1100600void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
NeilBrowna64c8762010-04-14 17:15:37 +1000602 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000603 /* These cannot be removed under reconfig_mutex as
604 * an access to the files will try to take reconfig_mutex
605 * while holding the file unremovable, which leads to
606 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000607 * So hold set sysfs_active while the remove in happeing,
608 * and anything else which might set ->to_remove or my
609 * otherwise change the sysfs namespace will fail with
610 * -EBUSY if sysfs_active is still set.
611 * We set sysfs_active under reconfig_mutex and elsewhere
612 * test it under the same mutex to ensure its correct value
613 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000614 */
NeilBrowna64c8762010-04-14 17:15:37 +1000615 struct attribute_group *to_remove = mddev->to_remove;
616 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000617 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000618 mutex_unlock(&mddev->reconfig_mutex);
619
NeilBrown00bcb4a2010-06-01 19:37:23 +1000620 if (mddev->kobj.sd) {
621 if (to_remove != &md_redundancy_group)
622 sysfs_remove_group(&mddev->kobj, to_remove);
623 if (mddev->pers == NULL ||
624 mddev->pers->sync_request == NULL) {
625 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
626 if (mddev->sysfs_action)
627 sysfs_put(mddev->sysfs_action);
628 mddev->sysfs_action = NULL;
629 }
NeilBrowna64c8762010-04-14 17:15:37 +1000630 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000631 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000632 } else
633 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Chris Dunlop751e67c2011-10-19 16:48:26 +1100635 /* As we've dropped the mutex we need a spinlock to
636 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000637 */
638 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700639 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000640 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
NeilBrown5c47daf2014-12-15 12:57:01 +1100642EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500644struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100645{
646 struct md_rdev *rdev;
647
648 rdev_for_each_rcu(rdev, mddev)
649 if (rdev->desc_nr == nr)
650 return rdev;
651
652 return NULL;
653}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500654EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100655
656static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
NeilBrown3cb03002011-10-11 16:45:26 +1100658 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
NeilBrowndafb20f2012-03-19 12:46:39 +1100660 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (rdev->bdev->bd_dev == dev)
662 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return NULL;
665}
666
NeilBrown1ca69c42012-10-11 13:37:33 +1100667static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
668{
669 struct md_rdev *rdev;
670
671 rdev_for_each_rcu(rdev, mddev)
672 if (rdev->bdev->bd_dev == dev)
673 return rdev;
674
675 return NULL;
676}
677
NeilBrown84fc4b52011-10-11 16:49:58 +1100678static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800679{
NeilBrown84fc4b52011-10-11 16:49:58 +1100680 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800681 list_for_each_entry(pers, &pers_list, list) {
682 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800683 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800684 if (strcmp(pers->name, clevel)==0)
685 return pers;
686 }
NeilBrown2604b702006-01-06 00:20:36 -0800687 return NULL;
688}
689
Andre Nollb73df2d2008-07-11 22:02:23 +1000690/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100691static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100693 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000694 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
NeilBrownf72ffdd2014-09-30 14:23:59 +1000697static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100700 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000701 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return 0;
703}
704
NeilBrown545c8792012-05-22 13:54:30 +1000705void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800708 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 rdev->sb_loaded = 0;
710 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000711 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100712 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
NeilBrown2699b672011-07-28 11:31:47 +1000714 if (rdev->bb_page) {
715 put_page(rdev->bb_page);
716 rdev->bb_page = NULL;
717 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800718 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
NeilBrown545c8792012-05-22 13:54:30 +1000720EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200722static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700723{
NeilBrown3cb03002011-10-11 16:45:26 +1100724 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100725 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700726
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200727 if (bio->bi_error) {
NeilBrown9d487392016-11-02 14:16:49 +1100728 pr_err("md: super_written gets error=%d\n", bio->bi_error);
NeilBrowna9701a32005-11-08 21:39:34 -0800729 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700730 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700731
NeilBrowna9701a32005-11-08 21:39:34 -0800732 if (atomic_dec_and_test(&mddev->pending_writes))
733 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700734 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700735 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700736}
737
NeilBrownfd01b882011-10-11 16:47:53 +1100738void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700739 sector_t sector, int size, struct page *page)
740{
741 /* write first size bytes of page to sector of rdev
742 * Increment mddev->pending_writes before returning
743 * and decrement it on completion, waking up sb_wait
744 * if zero is reached.
745 * If an error occurred, call md_error
746 */
NeilBrowna167f662010-10-26 18:31:13 +1100747 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700748
Shaohua Lied3b98c2016-03-29 14:00:19 -0700749 atomic_inc(&rdev->nr_pending);
750
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100751 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700752 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700753 bio_add_page(bio, page, size, 0);
754 bio->bi_private = rdev;
755 bio->bi_end_io = super_written;
Mike Christie796a5cf2016-06-05 14:32:07 -0500756 bio_set_op_attrs(bio, REQ_OP_WRITE, WRITE_FLUSH_FUA);
NeilBrowna9701a32005-11-08 21:39:34 -0800757
NeilBrown7bfa19f2005-06-21 17:17:28 -0700758 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500759 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800760}
761
NeilBrownfd01b882011-10-11 16:47:53 +1100762void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800763{
Tejun Heoe9c74692010-09-03 11:56:18 +0200764 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000765 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700766}
767
NeilBrown3cb03002011-10-11 16:45:26 +1100768int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -0500769 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
NeilBrowna167f662010-10-26 18:31:13 +1100771 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 int ret;
773
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100774 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
775 rdev->meta_bdev : rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -0500776 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100777 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700778 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000779 else if (rdev->mddev->reshape_position != MaxSector &&
780 (rdev->mddev->reshape_backwards ==
781 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700782 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100783 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700784 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500786
787 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200789 ret = !bio->bi_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 bio_put(bio);
791 return ret;
792}
NeilBrowna8745db2006-01-06 00:20:34 -0800793EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
NeilBrownf72ffdd2014-09-30 14:23:59 +1000795static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (rdev->sb_loaded)
800 return 0;
801
Mike Christie796a5cf2016-06-05 14:32:07 -0500802 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 goto fail;
804 rdev->sb_loaded = 1;
805 return 0;
806
807fail:
NeilBrown9d487392016-11-02 14:16:49 +1100808 pr_err("md: disabled device %s, could not read superblock.\n",
809 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return -EINVAL;
811}
812
813static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
814{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000815 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000816 sb1->set_uuid1 == sb2->set_uuid1 &&
817 sb1->set_uuid2 == sb2->set_uuid2 &&
818 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
822{
823 int ret;
824 mdp_super_t *tmp1, *tmp2;
825
826 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
827 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
828
829 if (!tmp1 || !tmp2) {
830 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto abort;
832 }
833
834 *tmp1 = *sb1;
835 *tmp2 = *sb2;
836
837 /*
838 * nr_disks is not constant
839 */
840 tmp1->nr_disks = 0;
841 tmp2->nr_disks = 0;
842
Andre Nollce0c8e02008-07-11 22:02:20 +1000843 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700845 kfree(tmp1);
846 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return ret;
848}
849
NeilBrown4d167f02007-05-09 02:35:37 -0700850static u32 md_csum_fold(u32 csum)
851{
852 csum = (csum & 0xffff) + (csum >> 16);
853 return (csum & 0xffff) + (csum >> 16);
854}
855
NeilBrownf72ffdd2014-09-30 14:23:59 +1000856static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
NeilBrown4d167f02007-05-09 02:35:37 -0700858 u64 newcsum = 0;
859 u32 *sb32 = (u32*)sb;
860 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 unsigned int disk_csum, csum;
862
863 disk_csum = sb->sb_csum;
864 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700865
866 for (i = 0; i < MD_SB_BYTES/4 ; i++)
867 newcsum += sb32[i];
868 csum = (newcsum & 0xffffffff) + (newcsum>>32);
869
NeilBrown4d167f02007-05-09 02:35:37 -0700870#ifdef CONFIG_ALPHA
871 /* This used to use csum_partial, which was wrong for several
872 * reasons including that different results are returned on
873 * different architectures. It isn't critical that we get exactly
874 * the same return value as before (we always csum_fold before
875 * testing, and that removes any differences). However as we
876 * know that csum_partial always returned a 16bit value on
877 * alphas, do a fold to maximise conformity to previous behaviour.
878 */
879 sb->sb_csum = md_csum_fold(disk_csum);
880#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700882#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return csum;
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/*
887 * Handle superblock details.
888 * We want to be able to handle multiple superblock formats
889 * so we have a common interface to them all, and an array of
890 * different handlers.
891 * We rely on user-space to write the initial superblock, and support
892 * reading and updating of superblocks.
893 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100894 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 * loads and validates a superblock on dev.
896 * if refdev != NULL, compare superblocks on both devices
897 * Return:
898 * 0 - dev has a superblock that is compatible with refdev
899 * 1 - dev has a superblock that is compatible and newer than refdev
900 * so dev should be used as the refdev in future
901 * -EINVAL superblock incompatible or invalid
902 * -othererror e.g. -EIO
903 *
NeilBrownfd01b882011-10-11 16:47:53 +1100904 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 * Verify that dev is acceptable into mddev.
906 * The first time, mddev->raid_disks will be 0, and data from
907 * dev should be merged in. Subsequent calls check that dev
908 * is new enough. Return 0 or -EINVAL
909 *
NeilBrownfd01b882011-10-11 16:47:53 +1100910 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * Update the superblock for rdev with data in mddev
912 * This does not write to disc.
913 *
914 */
915
916struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000917 char *name;
918 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000919 int (*load_super)(struct md_rdev *rdev,
920 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000921 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000922 int (*validate_super)(struct mddev *mddev,
923 struct md_rdev *rdev);
924 void (*sync_super)(struct mddev *mddev,
925 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100926 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000927 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000928 int (*allow_new_offset)(struct md_rdev *rdev,
929 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930};
931
932/*
Andre Noll0894cc32009-06-18 08:49:23 +1000933 * Check that the given mddev has no bitmap.
934 *
935 * This function is called from the run method of all personalities that do not
936 * support bitmaps. It prints an error message and returns non-zero if mddev
937 * has a bitmap. Otherwise, it returns 0.
938 *
939 */
NeilBrownfd01b882011-10-11 16:47:53 +1100940int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000941{
NeilBrownc3d97142009-12-14 12:49:52 +1100942 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000943 return 0;
NeilBrown9d487392016-11-02 14:16:49 +1100944 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +1000945 mdname(mddev), mddev->pers->name);
946 return 1;
947}
948EXPORT_SYMBOL(md_check_no_bitmap);
949
950/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000951 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 */
NeilBrown3cb03002011-10-11 16:45:26 +1100953static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
956 mdp_super_t *sb;
957 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /*
Andre Noll0f420352008-07-11 22:02:23 +1000960 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * it's at the end of the disk.
962 *
963 * It also happens to be a multiple of 4Kb.
964 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100965 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
NeilBrown0002b272005-09-09 16:23:53 -0700967 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +1100968 if (ret)
969 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 ret = -EINVAL;
972
973 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000974 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +1100977 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 goto abort;
979 }
980
981 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800982 sb->minor_version < 90 ||
983 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +1100984 pr_warn("Bad version number %d.%d on %s\n",
985 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 goto abort;
987 }
988
989 if (sb->raid_disks <= 0)
990 goto abort;
991
NeilBrown4d167f02007-05-09 02:35:37 -0700992 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +1100993 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 goto abort;
995 }
996
997 rdev->preferred_minor = sb->md_minor;
998 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +1000999 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001000 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001001 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 if (sb->level == LEVEL_MULTIPATH)
1004 rdev->desc_nr = -1;
1005 else
1006 rdev->desc_nr = sb->this_disk.number;
1007
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001008 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001010 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001012 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (!uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001014 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 b, bdevname(refdev->bdev,b2));
1016 goto abort;
1017 }
1018 if (!sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001019 pr_warn("md: %s has same UUID but different superblock to %s\n",
1020 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 goto abort;
1022 }
1023 ev1 = md_event(sb);
1024 ev2 = md_event(refsb);
1025 if (ev1 > ev2)
1026 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001027 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 ret = 0;
1029 }
NeilBrown8190e752009-06-18 08:48:58 +10001030 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001031 /* Limit to 4TB as metadata cannot record more than that.
1032 * (not needed for Linear and RAID0 as metadata doesn't
1033 * record this size)
1034 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001035 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1036 sb->level >= 1)
1037 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
NeilBrown27a7b262011-09-10 17:21:28 +10001039 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001040 /* "this cannot possibly happen" ... */
1041 ret = -EINVAL;
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 abort:
1044 return ret;
1045}
1046
1047/*
1048 * validate_super for 0.90.0
1049 */
NeilBrownfd01b882011-10-11 16:47:53 +11001050static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001053 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001054 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
NeilBrown41158c72005-06-21 17:17:25 -07001056 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001057 clear_bit(Faulty, &rdev->flags);
1058 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001059 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001060 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (mddev->raid_disks == 0) {
1063 mddev->major_version = 0;
1064 mddev->minor_version = sb->minor_version;
1065 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001066 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001067 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 mddev->ctime = sb->ctime;
1069 mddev->utime = sb->utime;
1070 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001071 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 mddev->layout = sb->layout;
1073 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001074 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001075 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001076 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001077 mddev->bitmap_info.space = 0;
1078 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001079 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001080 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001081 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
NeilBrownf6705572006-03-27 01:18:11 -08001083 if (mddev->minor_version >= 91) {
1084 mddev->reshape_position = sb->reshape_position;
1085 mddev->delta_disks = sb->delta_disks;
1086 mddev->new_level = sb->new_level;
1087 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001088 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001089 if (mddev->delta_disks < 0)
1090 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001091 } else {
1092 mddev->reshape_position = MaxSector;
1093 mddev->delta_disks = 0;
1094 mddev->new_level = mddev->level;
1095 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001096 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001097 }
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (sb->state & (1<<MD_SB_CLEAN))
1100 mddev->recovery_cp = MaxSector;
1101 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001102 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 sb->events_lo == sb->cp_events_lo) {
1104 mddev->recovery_cp = sb->recovery_cp;
1105 } else
1106 mddev->recovery_cp = 0;
1107 }
1108
1109 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1110 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1111 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1112 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1113
1114 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001115
1116 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001117 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001118 mddev->bitmap_info.offset =
1119 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001120 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001121 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001122 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001123
NeilBrown41158c72005-06-21 17:17:25 -07001124 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001125 /* Insist on good event counter while assembling, except
1126 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001128 if (sb->disks[rdev->desc_nr].state & (
1129 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001130 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001131 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001132 } else if (mddev->bitmap) {
1133 /* if adding to array with a bitmap, then we can accept an
1134 * older device ... but not too old.
1135 */
NeilBrown41158c72005-06-21 17:17:25 -07001136 if (ev1 < mddev->bitmap->events_cleared)
1137 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001138 if (ev1 < mddev->events)
1139 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001140 } else {
1141 if (ev1 < mddev->events)
1142 /* just a hot-add of a new device, leave raid_disk at -1 */
1143 return 0;
1144 }
NeilBrown41158c72005-06-21 17:17:25 -07001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 desc = sb->disks + rdev->desc_nr;
1148
1149 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001150 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001151 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1152 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001153 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001155 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001156 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1157 /* active but not in sync implies recovery up to
1158 * reshape position. We don't know exactly where
1159 * that is, so set to zero for now */
1160 if (mddev->minor_version >= 91) {
1161 rdev->recovery_offset = 0;
1162 rdev->raid_disk = desc->raid_disk;
1163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001165 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1166 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001167 if (desc->state & (1<<MD_DISK_FAILFAST))
1168 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001169 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001170 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return 0;
1172}
1173
1174/*
1175 * sync_super for 0.90.0
1176 */
NeilBrownfd01b882011-10-11 16:47:53 +11001177static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
1179 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001180 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /* make rdev->sb match mddev data..
1184 *
1185 * 1/ zero out disks
1186 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1187 * 3/ any empty disks < next_spare become removed
1188 *
1189 * disks[0] gets initialised to REMOVED because
1190 * we cannot be sure from other fields if it has
1191 * been initialised or not.
1192 */
1193 int i;
1194 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1195
NeilBrown61181562005-09-09 16:24:02 -07001196 rdev->sb_size = MD_SB_BYTES;
1197
Namhyung Kim65a06f062011-07-27 11:00:36 +10001198 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 memset(sb, 0, sizeof(*sb));
1201
1202 sb->md_magic = MD_SB_MAGIC;
1203 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 sb->patch_version = mddev->patch_version;
1205 sb->gvalid_words = 0; /* ignored */
1206 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1207 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1208 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1209 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1210
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001211 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001213 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 sb->raid_disks = mddev->raid_disks;
1215 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001216 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001217 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 sb->state = 0;
1219 sb->events_hi = (mddev->events>>32);
1220 sb->events_lo = (u32)mddev->events;
1221
NeilBrownf6705572006-03-27 01:18:11 -08001222 if (mddev->reshape_position == MaxSector)
1223 sb->minor_version = 90;
1224 else {
1225 sb->minor_version = 91;
1226 sb->reshape_position = mddev->reshape_position;
1227 sb->new_level = mddev->new_level;
1228 sb->delta_disks = mddev->delta_disks;
1229 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001230 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001231 }
1232 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (mddev->in_sync)
1234 {
1235 sb->recovery_cp = mddev->recovery_cp;
1236 sb->cp_events_hi = (mddev->events>>32);
1237 sb->cp_events_lo = (u32)mddev->events;
1238 if (mddev->recovery_cp == MaxSector)
1239 sb->state = (1<< MD_SB_CLEAN);
1240 } else
1241 sb->recovery_cp = 0;
1242
1243 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001244 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
NeilBrownc3d97142009-12-14 12:49:52 +11001246 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001247 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001250 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001252 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001253 int is_active = test_bit(In_sync, &rdev2->flags);
1254
1255 if (rdev2->raid_disk >= 0 &&
1256 sb->minor_version >= 91)
1257 /* we have nowhere to store the recovery_offset,
1258 * but if it is not below the reshape_position,
1259 * we can piggy-back on that.
1260 */
1261 is_active = 1;
1262 if (rdev2->raid_disk < 0 ||
1263 test_bit(Faulty, &rdev2->flags))
1264 is_active = 0;
1265 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001266 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001268 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001269 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 d = &sb->disks[rdev2->desc_nr];
1271 nr_disks++;
1272 d->number = rdev2->desc_nr;
1273 d->major = MAJOR(rdev2->bdev->bd_dev);
1274 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001275 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 d->raid_disk = rdev2->raid_disk;
1277 else
1278 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001279 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001281 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001283 if (test_bit(In_sync, &rdev2->flags))
1284 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 active++;
1286 working++;
1287 } else {
1288 d->state = 0;
1289 spare++;
1290 working++;
1291 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001292 if (test_bit(WriteMostly, &rdev2->flags))
1293 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001294 if (test_bit(FailFast, &rdev2->flags))
1295 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /* now set the "removed" and "faulty" bits on any missing devices */
1298 for (i=0 ; i < mddev->raid_disks ; i++) {
1299 mdp_disk_t *d = &sb->disks[i];
1300 if (d->state == 0 && d->number == 0) {
1301 d->number = i;
1302 d->raid_disk = i;
1303 d->state = (1<<MD_DISK_REMOVED);
1304 d->state |= (1<<MD_DISK_FAULTY);
1305 failed++;
1306 }
1307 }
1308 sb->nr_disks = nr_disks;
1309 sb->active_disks = active;
1310 sb->working_disks = working;
1311 sb->failed_disks = failed;
1312 sb->spare_disks = spare;
1313
1314 sb->this_disk = sb->disks[rdev->desc_nr];
1315 sb->sb_csum = calc_sb_csum(sb);
1316}
1317
1318/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001319 * rdev_size_change for 0.90.0
1320 */
1321static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001322super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001323{
Andre Noll58c0fed2009-03-31 14:33:13 +11001324 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001325 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001326 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001327 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001328 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001329 if (!num_sectors || num_sectors > rdev->sb_start)
1330 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001331 /* Limit to 4TB as metadata cannot record more than that.
1332 * 4TB == 2^32 KB, or 2*2^32 sectors.
1333 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001334 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1335 rdev->mddev->level >= 1)
1336 num_sectors = (sector_t)(2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001337 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001338 rdev->sb_page);
1339 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001340 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001341}
1342
NeilBrownc6563a82012-05-21 09:27:00 +10001343static int
1344super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1345{
1346 /* non-zero offset changes not possible with v0.90 */
1347 return new_offset == 0;
1348}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001349
1350/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 * version 1 superblock
1352 */
1353
NeilBrownf72ffdd2014-09-30 14:23:59 +10001354static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001356 __le32 disk_csum;
1357 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 unsigned long long newcsum;
1359 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001360 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 disk_csum = sb->sb_csum;
1363 sb->sb_csum = 0;
1364 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001365 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 newcsum += le32_to_cpu(*isuper++);
1367
1368 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001369 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1372 sb->sb_csum = disk_csum;
1373 return cpu_to_le32(csum);
1374}
1375
NeilBrown3cb03002011-10-11 16:45:26 +11001376static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 struct mdp_superblock_1 *sb;
1379 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001380 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001381 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001383 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /*
Andre Noll0f420352008-07-11 22:02:23 +10001386 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 * It is always aligned to a 4K boundary and
1388 * depeding on minor_version, it can be:
1389 * 0: At least 8K, but less than 12K, from end of device
1390 * 1: At start of device
1391 * 2: 4K from start of device.
1392 */
1393 switch(minor_version) {
1394 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001395 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001396 sb_start -= 8*2;
1397 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 break;
1399 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001400 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 break;
1402 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001403 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 break;
1405 default:
1406 return -EINVAL;
1407 }
Andre Noll0f420352008-07-11 22:02:23 +10001408 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
NeilBrown0002b272005-09-09 16:23:53 -07001410 /* superblock is rarely larger than 1K, but it can be larger,
1411 * and it is safe to read 4k, so we do that
1412 */
1413 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (ret) return ret;
1415
Namhyung Kim65a06f062011-07-27 11:00:36 +10001416 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1419 sb->major_version != cpu_to_le32(1) ||
1420 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001421 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001422 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return -EINVAL;
1424
1425 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001426 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 bdevname(rdev->bdev,b));
1428 return -EINVAL;
1429 }
1430 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001431 pr_warn("md: data_size too small on %s\n",
1432 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return -EINVAL;
1434 }
NeilBrownc6563a82012-05-21 09:27:00 +10001435 if (sb->pad0 ||
1436 sb->pad3[0] ||
1437 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1438 /* Some padding is non-zero, might be a new feature */
1439 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 rdev->preferred_minor = 0xffff;
1442 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001443 rdev->new_data_offset = rdev->data_offset;
1444 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1445 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1446 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001447 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
NeilBrown0002b272005-09-09 16:23:53 -07001449 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001450 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001451 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001452 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1453
1454 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001455 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001456 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001457 if (minor_version
1458 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1459 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001460
NeilBrown31b65a02006-07-10 04:44:14 -07001461 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1462 rdev->desc_nr = -1;
1463 else
1464 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1465
NeilBrown2699b672011-07-28 11:31:47 +10001466 if (!rdev->bb_page) {
1467 rdev->bb_page = alloc_page(GFP_KERNEL);
1468 if (!rdev->bb_page)
1469 return -ENOMEM;
1470 }
1471 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1472 rdev->badblocks.count == 0) {
1473 /* need to load the bad block list.
1474 * Currently we limit it to one page.
1475 */
1476 s32 offset;
1477 sector_t bb_sector;
1478 u64 *bbp;
1479 int i;
1480 int sectors = le16_to_cpu(sb->bblog_size);
1481 if (sectors > (PAGE_SIZE / 512))
1482 return -EINVAL;
1483 offset = le32_to_cpu(sb->bblog_offset);
1484 if (offset == 0)
1485 return -EINVAL;
1486 bb_sector = (long long)offset;
1487 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001488 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001489 return -EIO;
1490 bbp = (u64 *)page_address(rdev->bb_page);
1491 rdev->badblocks.shift = sb->bblog_shift;
1492 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1493 u64 bb = le64_to_cpu(*bbp);
1494 int count = bb & (0x3ff);
1495 u64 sector = bb >> 10;
1496 sector <<= sb->bblog_shift;
1497 count <<= sb->bblog_shift;
1498 if (bb + 1 == 0)
1499 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001500 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001501 return -EINVAL;
1502 }
NeilBrown486adf72013-04-24 11:42:44 +10001503 } else if (sb->bblog_offset != 0)
1504 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001505
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001506 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001507 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001508 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001510 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1513 sb->level != refsb->level ||
1514 sb->layout != refsb->layout ||
1515 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001516 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 bdevname(rdev->bdev,b),
1518 bdevname(refdev->bdev,b2));
1519 return -EINVAL;
1520 }
1521 ev1 = le64_to_cpu(sb->events);
1522 ev2 = le64_to_cpu(refsb->events);
1523
1524 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001525 ret = 1;
1526 else
1527 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
NeilBrownc6563a82012-05-21 09:27:00 +10001529 if (minor_version) {
1530 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1531 sectors -= rdev->data_offset;
1532 } else
1533 sectors = rdev->sb_start;
1534 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001536 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001537 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
NeilBrownfd01b882011-10-11 16:47:53 +11001540static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001542 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001543 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
NeilBrown41158c72005-06-21 17:17:25 -07001545 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001546 clear_bit(Faulty, &rdev->flags);
1547 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001548 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001549 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (mddev->raid_disks == 0) {
1552 mddev->major_version = 1;
1553 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001554 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001555 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001556 mddev->ctime = le64_to_cpu(sb->ctime);
1557 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001559 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 mddev->layout = le32_to_cpu(sb->layout);
1561 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001562 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001563 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001564 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001565 mddev->bitmap_info.space = 0;
1566 /* Default location for bitmap is 1K after superblock
1567 * using 3K - total of 4K
1568 */
NeilBrownc3d97142009-12-14 12:49:52 +11001569 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001570 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001571 mddev->reshape_backwards = 0;
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1574 memcpy(mddev->uuid, sb->set_uuid, 16);
1575
1576 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001577
NeilBrown71c08052005-09-09 16:23:51 -07001578 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001579 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001580 mddev->bitmap_info.offset =
1581 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001582 /* Metadata doesn't record how much space is available.
1583 * For 1.0, we assume we can use up to the superblock
1584 * if before, else to 4K beyond superblock.
1585 * For others, assume no change is possible.
1586 */
1587 if (mddev->minor_version > 0)
1588 mddev->bitmap_info.space = 0;
1589 else if (mddev->bitmap_info.offset > 0)
1590 mddev->bitmap_info.space =
1591 8 - mddev->bitmap_info.offset;
1592 else
1593 mddev->bitmap_info.space =
1594 -mddev->bitmap_info.offset;
1595 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001596
NeilBrownf6705572006-03-27 01:18:11 -08001597 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1598 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1599 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1600 mddev->new_level = le32_to_cpu(sb->new_level);
1601 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001602 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001603 if (mddev->delta_disks < 0 ||
1604 (mddev->delta_disks == 0 &&
1605 (le32_to_cpu(sb->feature_map)
1606 & MD_FEATURE_RESHAPE_BACKWARDS)))
1607 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001608 } else {
1609 mddev->reshape_position = MaxSector;
1610 mddev->delta_disks = 0;
1611 mddev->new_level = mddev->level;
1612 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001613 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001614 }
1615
Song Liu486b0f72016-08-19 15:34:01 -07001616 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001617 set_bit(MD_HAS_JOURNAL, &mddev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001618 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001619 /* Insist of good event counter while assembling, except for
1620 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001622 if (rdev->desc_nr >= 0 &&
1623 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001624 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1625 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001626 if (ev1 < mddev->events)
1627 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001628 } else if (mddev->bitmap) {
1629 /* If adding to array with a bitmap, then we can accept an
1630 * older device, but not too old.
1631 */
NeilBrown41158c72005-06-21 17:17:25 -07001632 if (ev1 < mddev->bitmap->events_cleared)
1633 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001634 if (ev1 < mddev->events)
1635 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001636 } else {
1637 if (ev1 < mddev->events)
1638 /* just a hot-add of a new device, leave raid_disk at -1 */
1639 return 0;
1640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (mddev->level != LEVEL_MULTIPATH) {
1642 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001643 if (rdev->desc_nr < 0 ||
1644 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001645 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001646 rdev->desc_nr = -1;
1647 } else
1648 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001650 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001652 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001653 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 break;
Song Liubac624f2015-08-13 14:31:55 -07001655 case MD_DISK_ROLE_JOURNAL: /* journal device */
1656 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1657 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001658 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001659 return -EINVAL;
1660 }
1661 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001662 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001663 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001664 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 default:
NeilBrownf4667222013-12-09 12:04:56 +11001666 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001667 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001668 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001669 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001670 if (!(le32_to_cpu(sb->feature_map) &
1671 MD_FEATURE_RECOVERY_BITMAP))
1672 rdev->saved_raid_disk = -1;
1673 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001674 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 rdev->raid_disk = role;
1676 break;
1677 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001678 if (sb->devflags & WriteMostly1)
1679 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001680 if (sb->devflags & FailFast1)
1681 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001682 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1683 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001684 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001685 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return 0;
1688}
1689
NeilBrownfd01b882011-10-11 16:47:53 +11001690static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
1692 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001693 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 int max_dev, i;
1695 /* make rdev->sb match mddev and rdev data. */
1696
Namhyung Kim65a06f062011-07-27 11:00:36 +10001697 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 sb->feature_map = 0;
1700 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001701 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 memset(sb->pad3, 0, sizeof(sb->pad3));
1703
1704 sb->utime = cpu_to_le64((__u64)mddev->utime);
1705 sb->events = cpu_to_le64(mddev->events);
1706 if (mddev->in_sync)
1707 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001708 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1709 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 else
1711 sb->resync_offset = cpu_to_le64(0);
1712
NeilBrown1c05b4b2006-10-21 10:24:08 -07001713 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001714
NeilBrownf0ca3402006-02-02 14:28:04 -08001715 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001716 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001717 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001718 sb->level = cpu_to_le32(mddev->level);
1719 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11001720 if (test_bit(FailFast, &rdev->flags))
1721 sb->devflags |= FailFast1;
1722 else
1723 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08001724
NeilBrownaeb9b2112011-08-25 14:43:08 +10001725 if (test_bit(WriteMostly, &rdev->flags))
1726 sb->devflags |= WriteMostly1;
1727 else
1728 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001729 sb->data_offset = cpu_to_le64(rdev->data_offset);
1730 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001731
NeilBrownc3d97142009-12-14 12:49:52 +11001732 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1733 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001734 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001735 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001736
Shaohua Lif2076e72015-10-08 21:54:12 -07001737 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001738 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001739 sb->feature_map |=
1740 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1741 sb->recovery_offset =
1742 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001743 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1744 sb->feature_map |=
1745 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001746 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001747 /* Note: recovery_offset and journal_tail share space */
1748 if (test_bit(Journal, &rdev->flags))
1749 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001750 if (test_bit(Replacement, &rdev->flags))
1751 sb->feature_map |=
1752 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001753
NeilBrownf6705572006-03-27 01:18:11 -08001754 if (mddev->reshape_position != MaxSector) {
1755 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1756 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1757 sb->new_layout = cpu_to_le32(mddev->new_layout);
1758 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1759 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001760 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001761 if (mddev->delta_disks == 0 &&
1762 mddev->reshape_backwards)
1763 sb->feature_map
1764 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001765 if (rdev->new_data_offset != rdev->data_offset) {
1766 sb->feature_map
1767 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1768 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1769 - rdev->data_offset));
1770 }
NeilBrownf6705572006-03-27 01:18:11 -08001771 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001772
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001773 if (mddev_is_clustered(mddev))
1774 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1775
NeilBrown2699b672011-07-28 11:31:47 +10001776 if (rdev->badblocks.count == 0)
1777 /* Nothing to do for bad blocks*/ ;
1778 else if (sb->bblog_offset == 0)
1779 /* Cannot record bad blocks on this device */
1780 md_error(mddev, rdev);
1781 else {
1782 struct badblocks *bb = &rdev->badblocks;
1783 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1784 u64 *p = bb->page;
1785 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1786 if (bb->changed) {
1787 unsigned seq;
1788
1789retry:
1790 seq = read_seqbegin(&bb->lock);
1791
1792 memset(bbp, 0xff, PAGE_SIZE);
1793
1794 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001795 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001796 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1797 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001798 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001799 }
NeilBrownd0962932012-03-19 12:46:41 +11001800 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001801 if (read_seqretry(&bb->lock, seq))
1802 goto retry;
1803
1804 bb->sector = (rdev->sb_start +
1805 (int)le32_to_cpu(sb->bblog_offset));
1806 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001807 }
1808 }
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001811 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (rdev2->desc_nr+1 > max_dev)
1813 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001814
NeilBrown70471da2009-08-03 10:59:57 +10001815 if (max_dev > le32_to_cpu(sb->max_dev)) {
1816 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001817 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001818 rdev->sb_size = max_dev * 2 + 256;
1819 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1820 if (rdev->sb_size & bmask)
1821 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001822 } else
1823 max_dev = le32_to_cpu(sb->max_dev);
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 for (i=0; i<max_dev;i++)
Song Liuc4d4c912015-08-13 14:31:54 -07001826 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001827
Song Liua97b7892015-10-08 21:54:09 -07001828 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1829 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
NeilBrowndafb20f2012-03-19 12:46:39 +11001831 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001833 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001834 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001835 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001837 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001838 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001839 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001840 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 else
Song Liuc4d4c912015-08-13 14:31:54 -07001842 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 sb->sb_csum = calc_sb_1_csum(sb);
1846}
1847
Chris Webb0cd17fe2008-06-28 08:31:46 +10001848static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001849super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001850{
1851 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001852 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001853 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001854 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001855 if (rdev->data_offset != rdev->new_data_offset)
1856 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001857 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001858 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001859 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001860 max_sectors -= rdev->data_offset;
1861 if (!num_sectors || num_sectors > max_sectors)
1862 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001863 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001864 /* minor version 0 with bitmap we can't move */
1865 return 0;
1866 } else {
1867 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001868 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001869 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001870 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001871 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001872 if (!num_sectors || num_sectors > max_sectors)
1873 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001874 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001875 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001876 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001877 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001878 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001879 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001880 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001881 rdev->sb_page);
1882 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001883 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001884
1885}
1886
1887static int
1888super_1_allow_new_offset(struct md_rdev *rdev,
1889 unsigned long long new_offset)
1890{
1891 /* All necessary checks on new >= old have been done */
1892 struct bitmap *bitmap;
1893 if (new_offset >= rdev->data_offset)
1894 return 1;
1895
1896 /* with 1.0 metadata, there is no metadata to tread on
1897 * so we can always move back */
1898 if (rdev->mddev->minor_version == 0)
1899 return 1;
1900
1901 /* otherwise we must be sure not to step on
1902 * any metadata, so stay:
1903 * 36K beyond start of superblock
1904 * beyond end of badblocks
1905 * beyond write-intent bitmap
1906 */
1907 if (rdev->sb_start + (32+4)*2 > new_offset)
1908 return 0;
1909 bitmap = rdev->mddev->bitmap;
1910 if (bitmap && !rdev->mddev->bitmap_info.file &&
1911 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001912 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001913 return 0;
1914 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1915 return 0;
1916
1917 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001918}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Adrian Bunk75c96f82005-05-05 16:16:09 -07001920static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 [0] = {
1922 .name = "0.90.0",
1923 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001924 .load_super = super_90_load,
1925 .validate_super = super_90_validate,
1926 .sync_super = super_90_sync,
1927 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001928 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 },
1930 [1] = {
1931 .name = "md-1",
1932 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001933 .load_super = super_1_load,
1934 .validate_super = super_1_validate,
1935 .sync_super = super_1_sync,
1936 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001937 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 },
1939};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
NeilBrownfd01b882011-10-11 16:47:53 +11001941static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001942{
1943 if (mddev->sync_super) {
1944 mddev->sync_super(mddev, rdev);
1945 return;
1946 }
1947
1948 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1949
1950 super_types[mddev->major_version].sync_super(mddev, rdev);
1951}
1952
NeilBrownfd01b882011-10-11 16:47:53 +11001953static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
NeilBrown3cb03002011-10-11 16:45:26 +11001955 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
NeilBrown4b809912008-07-21 17:05:25 +10001957 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07001958 rdev_for_each_rcu(rdev, mddev1) {
1959 if (test_bit(Faulty, &rdev->flags) ||
1960 test_bit(Journal, &rdev->flags) ||
1961 rdev->raid_disk == -1)
1962 continue;
1963 rdev_for_each_rcu(rdev2, mddev2) {
1964 if (test_bit(Faulty, &rdev2->flags) ||
1965 test_bit(Journal, &rdev2->flags) ||
1966 rdev2->raid_disk == -1)
1967 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001968 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001969 rdev2->bdev->bd_contains) {
1970 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001971 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001972 }
Song Liu0b020e82015-09-03 23:00:35 -07001973 }
1974 }
NeilBrown4b809912008-07-21 17:05:25 +10001975 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 return 0;
1977}
1978
1979static LIST_HEAD(pending_raid_disks);
1980
Andre Nollac5e7112009-08-03 10:59:47 +10001981/*
1982 * Try to register data integrity profile for an mddev
1983 *
1984 * This is called when an array is started and after a disk has been kicked
1985 * from the array. It only succeeds if all working and active component devices
1986 * are integrity capable with matching profiles.
1987 */
NeilBrownfd01b882011-10-11 16:47:53 +11001988int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001989{
NeilBrown3cb03002011-10-11 16:45:26 +11001990 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001991
Andre Nollac5e7112009-08-03 10:59:47 +10001992 if (list_empty(&mddev->disks))
1993 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001994 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1995 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001996 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001997 /* skip spares and non-functional disks */
1998 if (test_bit(Faulty, &rdev->flags))
1999 continue;
2000 if (rdev->raid_disk < 0)
2001 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002002 if (!reference) {
2003 /* Use the first rdev as the reference */
2004 reference = rdev;
2005 continue;
2006 }
2007 /* does this rdev's profile match the reference profile? */
2008 if (blk_integrity_compare(reference->bdev->bd_disk,
2009 rdev->bdev->bd_disk) < 0)
2010 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002011 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002012 if (!reference || !bdev_get_integrity(reference->bdev))
2013 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002014 /*
2015 * All component devices are integrity capable and have matching
2016 * profiles, register the common profile for the md device.
2017 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002018 blk_integrity_register(mddev->gendisk,
2019 bdev_get_integrity(reference->bdev));
2020
NeilBrown9d487392016-11-02 14:16:49 +11002021 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Martin K. Petersena91a2782011-03-17 11:11:05 +01002022 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002023 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002024 mdname(mddev));
2025 return -EINVAL;
2026 }
Andre Nollac5e7112009-08-03 10:59:47 +10002027 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002028}
Andre Nollac5e7112009-08-03 10:59:47 +10002029EXPORT_SYMBOL(md_integrity_register);
2030
Dan Williams1501efa2016-01-13 16:00:07 -08002031/*
2032 * Attempt to add an rdev, but only if it is consistent with the current
2033 * integrity profile
2034 */
2035int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002036{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002037 struct blk_integrity *bi_rdev;
2038 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002039 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002040
2041 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002042 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002043
2044 bi_rdev = bdev_get_integrity(rdev->bdev);
2045 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002046
2047 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002048 return 0;
2049
2050 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002051 pr_err("%s: incompatible integrity profile for %s\n",
2052 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002053 return -ENXIO;
2054 }
2055
2056 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002057}
2058EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002059
NeilBrownf72ffdd2014-09-30 14:23:59 +10002060static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002062 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002063 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002064 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Dan Williams11e2ede2008-04-30 00:52:32 -07002066 /* prevent duplicates */
2067 if (find_rdev(mddev, rdev->bdev->bd_dev))
2068 return -EEXIST;
2069
Andre Nolldd8ac332009-03-31 14:33:13 +11002070 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002071 if (!test_bit(Journal, &rdev->flags) &&
2072 rdev->sectors &&
2073 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002074 if (mddev->pers) {
2075 /* Cannot change size, so fail
2076 * If mddev->level <= 0, then we don't care
2077 * about aligning sizes (e.g. linear)
2078 */
2079 if (mddev->level > 0)
2080 return -ENOSPC;
2081 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002082 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 /* Verify rdev->desc_nr is unique.
2086 * If it is -1, assign a free number, else
2087 * check number is not in use
2088 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002089 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (rdev->desc_nr < 0) {
2091 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002092 if (mddev->pers)
2093 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002094 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 choice++;
2096 rdev->desc_nr = choice;
2097 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002098 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002099 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002103 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002104 if (!test_bit(Journal, &rdev->flags) &&
2105 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002106 pr_warn("md: %s: array is limited to %d devices\n",
2107 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002108 return -EBUSY;
2109 }
NeilBrown19133a42005-11-08 21:39:35 -08002110 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002111 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002114 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002115
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002116 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002117 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002118
Tejun Heo0762b8b2008-08-25 19:56:12 +09002119 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002120 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2121 /* failure here is OK */;
2122 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002123
NeilBrown4b809912008-07-21 17:05:25 +10002124 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002125 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002126
2127 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002128 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002131
2132 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002133 pr_warn("md: failed to register dev-%s for %s\n",
2134 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002135 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136}
2137
NeilBrown177a99b2008-02-06 01:39:56 -08002138static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002139{
NeilBrown3cb03002011-10-11 16:45:26 +11002140 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002141 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002142 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002143}
2144
NeilBrownf72ffdd2014-09-30 14:23:59 +10002145static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
2147 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002148
Tejun Heo49731ba2011-01-14 18:43:57 +01002149 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002150 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002151 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002153 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002154 sysfs_put(rdev->sysfs_state);
2155 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002156 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002157 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002158 * writing to 'remove' to "dev/state". We also need
2159 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002160 */
NeilBrown4b809912008-07-21 17:05:25 +10002161 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002162 INIT_WORK(&rdev->del_work, md_delayed_delete);
2163 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002164 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
2167/*
2168 * prevent the device from being mounted, repartitioned or
2169 * otherwise reused by a RAID array (or any other kernel
2170 * subsystem), by bd_claiming the device.
2171 */
NeilBrown3cb03002011-10-11 16:45:26 +11002172static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 int err = 0;
2175 struct block_device *bdev;
2176 char b[BDEVNAME_SIZE];
2177
Tejun Heod4d77622010-11-13 11:55:18 +01002178 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002179 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002181 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return PTR_ERR(bdev);
2183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 rdev->bdev = bdev;
2185 return err;
2186}
2187
NeilBrown3cb03002011-10-11 16:45:26 +11002188static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
2190 struct block_device *bdev = rdev->bdev;
2191 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002192 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
2195void md_autodetect_dev(dev_t dev);
2196
NeilBrownf72ffdd2014-09-30 14:23:59 +10002197static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
2199 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002200
NeilBrown9d487392016-11-02 14:16:49 +11002201 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002202 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002204 if (test_bit(AutoDetected, &rdev->flags))
2205 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206#endif
2207 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002208 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002211void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
2213 unbind_rdev_from_array(rdev);
2214 export_rdev(rdev);
2215}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002216EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
NeilBrownfd01b882011-10-11 16:47:53 +11002218static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
NeilBrown0638bb02014-09-25 17:43:47 +10002220 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
NeilBrown0638bb02014-09-25 17:43:47 +10002222 while (!list_empty(&mddev->disks)) {
2223 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2224 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002225 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 mddev->raid_disks = 0;
2228 mddev->major_version = 0;
2229}
2230
NeilBrownf72ffdd2014-09-30 14:23:59 +10002231static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
NeilBrown42543762006-06-26 00:27:57 -07002233 /* Update each superblock (in-memory image), but
2234 * if we are allowed to, skip spares which already
2235 * have the right event counter, or have one earlier
2236 * (which would mean they aren't being marked as dirty
2237 * with the rest of the array)
2238 */
NeilBrown3cb03002011-10-11 16:45:26 +11002239 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002240 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002241 if (rdev->sb_events == mddev->events ||
2242 (nospares &&
2243 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002244 rdev->sb_events+1 == mddev->events)) {
2245 /* Don't update this superblock */
2246 rdev->sb_loaded = 2;
2247 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002248 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002249 rdev->sb_loaded = 1;
2250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
2252}
2253
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002254static bool does_sb_need_changing(struct mddev *mddev)
2255{
2256 struct md_rdev *rdev;
2257 struct mdp_superblock_1 *sb;
2258 int role;
2259
2260 /* Find a good rdev */
2261 rdev_for_each(rdev, mddev)
2262 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2263 break;
2264
2265 /* No good device found. */
2266 if (!rdev)
2267 return false;
2268
2269 sb = page_address(rdev->sb_page);
2270 /* Check if a device has become faulty or a spare become active */
2271 rdev_for_each(rdev, mddev) {
2272 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2273 /* Device activated? */
2274 if (role == 0xffff && rdev->raid_disk >=0 &&
2275 !test_bit(Faulty, &rdev->flags))
2276 return true;
2277 /* Device turned faulty? */
2278 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2279 return true;
2280 }
2281
2282 /* Check if any mddev parameters have changed */
2283 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2284 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002285 (mddev->layout != le64_to_cpu(sb->layout)) ||
2286 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2287 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2288 return true;
2289
2290 return false;
2291}
2292
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002293void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
NeilBrown3cb03002011-10-11 16:45:26 +11002295 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002296 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002297 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002298 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002299 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
NeilBrownd87f0642013-04-24 11:42:40 +10002301 if (mddev->ro) {
2302 if (force_change)
2303 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2304 return;
2305 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002306
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002307repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002308 if (mddev_is_clustered(mddev)) {
2309 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2310 force_change = 1;
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002311 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2312 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002313 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002314 /* Has someone else has updated the sb */
2315 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002316 if (ret == 0)
2317 md_cluster_ops->metadata_update_cancel(mddev);
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002318 bit_clear_unless(&mddev->flags, BIT(MD_CHANGE_PENDING),
2319 BIT(MD_CHANGE_DEVS) |
2320 BIT(MD_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002321 return;
2322 }
2323 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002324
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002325 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002326 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002327 if (rdev->raid_disk >= 0 &&
2328 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002329 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002330 !test_bit(In_sync, &rdev->flags) &&
2331 mddev->curr_resync_completed > rdev->recovery_offset)
2332 rdev->recovery_offset = mddev->curr_resync_completed;
2333
NeilBrownf72ffdd2014-09-30 14:23:59 +10002334 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002335 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002336 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002337 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002338 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002339 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002340 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002341 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002342 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002343 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002344 md_error(mddev, rdev);
2345 }
2346 clear_bit(Blocked, &rdev->flags);
2347 clear_bit(BlockedBadBlocks, &rdev->flags);
2348 wake_up(&rdev->blocked_wait);
2349 }
2350 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002351 wake_up(&mddev->sb_wait);
2352 return;
2353 }
2354
NeilBrown85572d72014-12-15 12:56:56 +11002355 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002356
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002357 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002358
NeilBrown850b2b42006-10-03 01:15:46 -07002359 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2360 force_change = 1;
2361 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2362 /* just a clean<-> dirty transition, possibly leave spares alone,
2363 * though if events isn't the right even/odd, we will have to do
2364 * spares after all
2365 */
2366 nospares = 1;
2367 if (force_change)
2368 nospares = 0;
2369 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002370 /* If the array is degraded, then skipping spares is both
2371 * dangerous and fairly pointless.
2372 * Dangerous because a device that was removed from the array
2373 * might have a event_count that still looks up-to-date,
2374 * so it can be re-added without a resync.
2375 * Pointless because if there are any spares to skip,
2376 * then a recovery will happen and soon that array won't
2377 * be degraded any more and the spare can go back to sleep then.
2378 */
NeilBrown850b2b42006-10-03 01:15:46 -07002379 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002380
NeilBrown06d91a52005-06-21 17:17:12 -07002381 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002382
2383 /* If this is just a dirty<->clean transition, and the array is clean
2384 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002385 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002386 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002387 && mddev->can_decrease_events
2388 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002389 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002390 mddev->can_decrease_events = 0;
2391 } else {
NeilBrown42543762006-06-26 00:27:57 -07002392 /* otherwise we have to go forward and ... */
2393 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002394 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
NeilBrown403df472014-09-30 15:52:29 +10002397 /*
2398 * This 64-bit counter should never wrap.
2399 * Either we are in around ~1 trillion A.C., assuming
2400 * 1 reboot per second, or we have a bug...
2401 */
2402 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002403
NeilBrowndafb20f2012-03-19 12:46:39 +11002404 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002405 if (rdev->badblocks.changed)
2406 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002407 if (test_bit(Faulty, &rdev->flags))
2408 set_bit(FaultRecorded, &rdev->flags);
2409 }
NeilBrown2699b672011-07-28 11:31:47 +10002410
NeilBrowne6910632008-02-06 01:39:51 -08002411 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002412 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
NeilBrown36a4e1f2011-10-07 14:23:17 +11002414 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2415 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Shaohua Li504634f2016-11-18 09:44:08 -08002417 if (mddev->queue)
2418 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown4ad13662007-07-17 04:06:13 -07002419 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002420 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002422
NeilBrown42543762006-06-26 00:27:57 -07002423 if (rdev->sb_loaded != 1)
2424 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
NeilBrownf4667222013-12-09 12:04:56 +11002426 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002427 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002428 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002429 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002430 pr_debug("md: (write) %s's sb offset: %llu\n",
2431 bdevname(rdev->bdev, b),
2432 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002433 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002434 if (rdev->badblocks.size) {
2435 md_super_write(mddev, rdev,
2436 rdev->badblocks.sector,
2437 rdev->badblocks.size << 9,
2438 rdev->bb_page);
2439 rdev->badblocks.size = 0;
2440 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002441
NeilBrownf4667222013-12-09 12:04:56 +11002442 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002443 pr_debug("md: %s (skipping faulty)\n",
2444 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002445
NeilBrown7bfa19f2005-06-21 17:17:28 -07002446 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 /* only need to write one superblock... */
2448 break;
2449 }
NeilBrowna9701a32005-11-08 21:39:34 -08002450 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002451 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002452
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002453 if (mddev_is_clustered(mddev) && ret == 0)
2454 md_cluster_ops->metadata_update_finish(mddev);
2455
NeilBrown850b2b42006-10-03 01:15:46 -07002456 if (mddev->in_sync != sync_req ||
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002457 !bit_clear_unless(&mddev->flags, BIT(MD_CHANGE_PENDING),
2458 BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002459 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002460 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002461 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002462 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2463 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002464
NeilBrowndafb20f2012-03-19 12:46:39 +11002465 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002466 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2467 clear_bit(Blocked, &rdev->flags);
2468
2469 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002470 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002471 clear_bit(BlockedBadBlocks, &rdev->flags);
2472 wake_up(&rdev->blocked_wait);
2473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002475EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002477static int add_bound_rdev(struct md_rdev *rdev)
2478{
2479 struct mddev *mddev = rdev->mddev;
2480 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002481 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002482
Shaohua Li87d4d912016-01-06 14:37:14 -08002483 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002484 /* If there is hot_add_disk but no hot_remove_disk
2485 * then added disks for geometry changes,
2486 * and should be added immediately.
2487 */
2488 super_types[mddev->major_version].
2489 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002490 if (add_journal)
2491 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002492 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002493 if (add_journal)
2494 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002495 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002496 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002497 return err;
2498 }
2499 }
2500 sysfs_notify_dirent_safe(rdev->sysfs_state);
2501
2502 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2503 if (mddev->degraded)
2504 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2505 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2506 md_new_event(mddev);
2507 md_wakeup_thread(mddev->thread);
2508 return 0;
2509}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Andre Noll7f6ce762008-03-23 18:34:54 +01002511/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002512 * We want to accept with case. For this we use cmd_match.
2513 */
2514static int cmd_match(const char *cmd, const char *str)
2515{
2516 /* See if cmd, written into a sysfs file, matches
2517 * str. They must either be the same, or cmd can
2518 * have a trailing newline
2519 */
2520 while (*cmd && *str && *cmd == *str) {
2521 cmd++;
2522 str++;
2523 }
2524 if (*cmd == '\n')
2525 cmd++;
2526 if (*str || *cmd)
2527 return 0;
2528 return 1;
2529}
2530
NeilBrown86e6ffd2005-11-08 21:39:24 -08002531struct rdev_sysfs_entry {
2532 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002533 ssize_t (*show)(struct md_rdev *, char *);
2534 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002535};
2536
2537static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002538state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002539{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002540 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002541 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002542 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002543
NeilBrown758bfc82014-12-15 12:56:59 +11002544 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002545 (!test_bit(ExternalBbl, &flags) &&
2546 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002547 len += sprintf(page+len, "faulty%s", sep);
2548 if (test_bit(In_sync, &flags))
2549 len += sprintf(page+len, "in_sync%s", sep);
2550 if (test_bit(Journal, &flags))
2551 len += sprintf(page+len, "journal%s", sep);
2552 if (test_bit(WriteMostly, &flags))
2553 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002554 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002555 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002556 && !test_bit(Faulty, &flags)))
2557 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002558 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002559 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002560 !test_bit(In_sync, &flags))
2561 len += sprintf(page+len, "spare%s", sep);
2562 if (test_bit(WriteErrorSeen, &flags))
2563 len += sprintf(page+len, "write_error%s", sep);
2564 if (test_bit(WantReplacement, &flags))
2565 len += sprintf(page+len, "want_replacement%s", sep);
2566 if (test_bit(Replacement, &flags))
2567 len += sprintf(page+len, "replacement%s", sep);
2568 if (test_bit(ExternalBbl, &flags))
2569 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002570 if (test_bit(FailFast, &flags))
2571 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002572
2573 if (len)
2574 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002575
NeilBrown86e6ffd2005-11-08 21:39:24 -08002576 return len+sprintf(page+len, "\n");
2577}
2578
NeilBrown45dc2de2006-06-26 00:27:58 -07002579static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002580state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002581{
2582 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002583 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002584 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002585 * writemostly - sets write_mostly
2586 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002587 * blocked - sets the Blocked flags
2588 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002589 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002590 * -insync - clear Insync for a device with a slot assigned,
2591 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002592 * write_error - sets WriteErrorSeen
2593 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002594 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002595 */
2596 int err = -EINVAL;
2597 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2598 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002599 if (test_bit(Faulty, &rdev->flags))
2600 err = 0;
2601 else
2602 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002603 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002604 if (rdev->mddev->pers) {
2605 clear_bit(Blocked, &rdev->flags);
2606 remove_and_add_spares(rdev->mddev, rdev);
2607 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002608 if (rdev->raid_disk >= 0)
2609 err = -EBUSY;
2610 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002611 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002612 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002613 if (mddev_is_clustered(mddev))
2614 err = md_cluster_ops->remove_disk(mddev, rdev);
2615
2616 if (err == 0) {
2617 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002618 if (mddev->pers) {
2619 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2620 md_wakeup_thread(mddev->thread);
2621 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002622 md_new_event(mddev);
2623 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002624 }
NeilBrownf6556752006-06-26 00:28:01 -07002625 } else if (cmd_match(buf, "writemostly")) {
2626 set_bit(WriteMostly, &rdev->flags);
2627 err = 0;
2628 } else if (cmd_match(buf, "-writemostly")) {
2629 clear_bit(WriteMostly, &rdev->flags);
2630 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002631 } else if (cmd_match(buf, "blocked")) {
2632 set_bit(Blocked, &rdev->flags);
2633 err = 0;
2634 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002635 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002636 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002637 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002638 /* metadata handler doesn't understand badblocks,
2639 * so we need to fail the device
2640 */
2641 md_error(rdev->mddev, rdev);
2642 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002643 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002644 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002645 wake_up(&rdev->blocked_wait);
2646 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2647 md_wakeup_thread(rdev->mddev->thread);
2648
2649 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002650 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2651 set_bit(In_sync, &rdev->flags);
2652 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11002653 } else if (cmd_match(buf, "failfast")) {
2654 set_bit(FailFast, &rdev->flags);
2655 err = 0;
2656 } else if (cmd_match(buf, "-failfast")) {
2657 clear_bit(FailFast, &rdev->flags);
2658 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002659 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2660 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002661 if (rdev->mddev->pers == NULL) {
2662 clear_bit(In_sync, &rdev->flags);
2663 rdev->saved_raid_disk = rdev->raid_disk;
2664 rdev->raid_disk = -1;
2665 err = 0;
2666 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002667 } else if (cmd_match(buf, "write_error")) {
2668 set_bit(WriteErrorSeen, &rdev->flags);
2669 err = 0;
2670 } else if (cmd_match(buf, "-write_error")) {
2671 clear_bit(WriteErrorSeen, &rdev->flags);
2672 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002673 } else if (cmd_match(buf, "want_replacement")) {
2674 /* Any non-spare device that is not a replacement can
2675 * become want_replacement at any time, but we then need to
2676 * check if recovery is needed.
2677 */
2678 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002679 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002680 !test_bit(Replacement, &rdev->flags))
2681 set_bit(WantReplacement, &rdev->flags);
2682 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2683 md_wakeup_thread(rdev->mddev->thread);
2684 err = 0;
2685 } else if (cmd_match(buf, "-want_replacement")) {
2686 /* Clearing 'want_replacement' is always allowed.
2687 * Once replacements starts it is too late though.
2688 */
2689 err = 0;
2690 clear_bit(WantReplacement, &rdev->flags);
2691 } else if (cmd_match(buf, "replacement")) {
2692 /* Can only set a device as a replacement when array has not
2693 * yet been started. Once running, replacement is automatic
2694 * from spares, or by assigning 'slot'.
2695 */
2696 if (rdev->mddev->pers)
2697 err = -EBUSY;
2698 else {
2699 set_bit(Replacement, &rdev->flags);
2700 err = 0;
2701 }
2702 } else if (cmd_match(buf, "-replacement")) {
2703 /* Similarly, can only clear Replacement before start */
2704 if (rdev->mddev->pers)
2705 err = -EBUSY;
2706 else {
2707 clear_bit(Replacement, &rdev->flags);
2708 err = 0;
2709 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002710 } else if (cmd_match(buf, "re-add")) {
2711 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002712 /* clear_bit is performed _after_ all the devices
2713 * have their local Faulty bit cleared. If any writes
2714 * happen in the meantime in the local node, they
2715 * will land in the local bitmap, which will be synced
2716 * by this node eventually
2717 */
2718 if (!mddev_is_clustered(rdev->mddev) ||
2719 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2720 clear_bit(Faulty, &rdev->flags);
2721 err = add_bound_rdev(rdev);
2722 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002723 } else
2724 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002725 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2726 set_bit(ExternalBbl, &rdev->flags);
2727 rdev->badblocks.shift = 0;
2728 err = 0;
2729 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2730 clear_bit(ExternalBbl, &rdev->flags);
2731 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002732 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002733 if (!err)
2734 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002735 return err ? err : len;
2736}
NeilBrown80ca3a42006-07-10 04:44:18 -07002737static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002738__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002739
2740static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002741errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002742{
2743 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2744}
2745
2746static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002747errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002748{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002749 unsigned int n;
2750 int rv;
2751
2752 rv = kstrtouint(buf, 10, &n);
2753 if (rv < 0)
2754 return rv;
2755 atomic_set(&rdev->corrected_errors, n);
2756 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002757}
2758static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002759__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002760
NeilBrown014236d2006-01-06 00:20:55 -08002761static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002762slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002763{
Shaohua Lif2076e72015-10-08 21:54:12 -07002764 if (test_bit(Journal, &rdev->flags))
2765 return sprintf(page, "journal\n");
2766 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002767 return sprintf(page, "none\n");
2768 else
2769 return sprintf(page, "%d\n", rdev->raid_disk);
2770}
2771
2772static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002773slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002774{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002775 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002776 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002777
Shaohua Lif2076e72015-10-08 21:54:12 -07002778 if (test_bit(Journal, &rdev->flags))
2779 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002780 if (strncmp(buf, "none", 4)==0)
2781 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002782 else {
2783 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2784 if (err < 0)
2785 return err;
2786 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002787 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002788 /* Setting 'slot' on an active array requires also
2789 * updating the 'rd%d' link, and communicating
2790 * with the personality with ->hot_*_disk.
2791 * For now we only support removing
2792 * failed/spare devices. This normally happens automatically,
2793 * but not when the metadata is externally managed.
2794 */
NeilBrownc303da62008-02-06 01:39:51 -08002795 if (rdev->raid_disk == -1)
2796 return -EEXIST;
2797 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002798 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002799 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002800 clear_bit(Blocked, &rdev->flags);
2801 remove_and_add_spares(rdev->mddev, rdev);
2802 if (rdev->raid_disk >= 0)
2803 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002804 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2805 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002806 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002807 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002808 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002809 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002810 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002811
2812 if (rdev->raid_disk != -1)
2813 return -EBUSY;
2814
NeilBrownc6751b22011-02-02 11:57:13 +11002815 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2816 return -EBUSY;
2817
Neil Brown6c2fce22008-06-28 08:31:31 +10002818 if (rdev->mddev->pers->hot_add_disk == NULL)
2819 return -EINVAL;
2820
NeilBrownba1b41b2011-01-14 09:14:34 +11002821 if (slot >= rdev->mddev->raid_disks &&
2822 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2823 return -ENOSPC;
2824
Neil Brown6c2fce22008-06-28 08:31:31 +10002825 rdev->raid_disk = slot;
2826 if (test_bit(In_sync, &rdev->flags))
2827 rdev->saved_raid_disk = slot;
2828 else
2829 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002830 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002831 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002832 err = rdev->mddev->pers->
2833 hot_add_disk(rdev->mddev, rdev);
2834 if (err) {
2835 rdev->raid_disk = -1;
2836 return err;
2837 } else
2838 sysfs_notify_dirent_safe(rdev->sysfs_state);
2839 if (sysfs_link_rdev(rdev->mddev, rdev))
2840 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002841 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002842 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002843 if (slot >= rdev->mddev->raid_disks &&
2844 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002845 return -ENOSPC;
2846 rdev->raid_disk = slot;
2847 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002848 clear_bit(Faulty, &rdev->flags);
2849 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002850 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002851 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002852 }
NeilBrown014236d2006-01-06 00:20:55 -08002853 return len;
2854}
2855
NeilBrown014236d2006-01-06 00:20:55 -08002856static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002857__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002858
NeilBrown93c8cad2006-01-06 00:20:56 -08002859static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002860offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002861{
Andrew Morton6961ece2006-01-06 00:20:59 -08002862 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002863}
2864
2865static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002866offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002867{
NeilBrownc6563a82012-05-21 09:27:00 +10002868 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002869 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002870 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002871 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002872 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002873 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002874 /* Must set offset before size, so overlap checks
2875 * can be sane */
2876 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002877 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002878 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002879 return len;
2880}
2881
2882static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002883__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002884
NeilBrownc6563a82012-05-21 09:27:00 +10002885static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2886{
2887 return sprintf(page, "%llu\n",
2888 (unsigned long long)rdev->new_data_offset);
2889}
2890
2891static ssize_t new_offset_store(struct md_rdev *rdev,
2892 const char *buf, size_t len)
2893{
2894 unsigned long long new_offset;
2895 struct mddev *mddev = rdev->mddev;
2896
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002897 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002898 return -EINVAL;
2899
NeilBrownf851b602014-12-11 10:02:10 +11002900 if (mddev->sync_thread ||
2901 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002902 return -EBUSY;
2903 if (new_offset == rdev->data_offset)
2904 /* reset is always permitted */
2905 ;
2906 else if (new_offset > rdev->data_offset) {
2907 /* must not push array size beyond rdev_sectors */
2908 if (new_offset - rdev->data_offset
2909 + mddev->dev_sectors > rdev->sectors)
2910 return -E2BIG;
2911 }
2912 /* Metadata worries about other space details. */
2913
2914 /* decreasing the offset is inconsistent with a backwards
2915 * reshape.
2916 */
2917 if (new_offset < rdev->data_offset &&
2918 mddev->reshape_backwards)
2919 return -EINVAL;
2920 /* Increasing offset is inconsistent with forwards
2921 * reshape. reshape_direction should be set to
2922 * 'backwards' first.
2923 */
2924 if (new_offset > rdev->data_offset &&
2925 !mddev->reshape_backwards)
2926 return -EINVAL;
2927
2928 if (mddev->pers && mddev->persistent &&
2929 !super_types[mddev->major_version]
2930 .allow_new_offset(rdev, new_offset))
2931 return -E2BIG;
2932 rdev->new_data_offset = new_offset;
2933 if (new_offset > rdev->data_offset)
2934 mddev->reshape_backwards = 1;
2935 else if (new_offset < rdev->data_offset)
2936 mddev->reshape_backwards = 0;
2937
2938 return len;
2939}
2940static struct rdev_sysfs_entry rdev_new_offset =
2941__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2942
NeilBrown83303b62006-01-06 00:21:06 -08002943static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002944rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002945{
Andre Nolldd8ac332009-03-31 14:33:13 +11002946 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002947}
2948
NeilBrownc5d79ad2008-02-06 01:39:54 -08002949static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2950{
2951 /* check if two start/length pairs overlap */
2952 if (s1+l1 <= s2)
2953 return 0;
2954 if (s2+l2 <= s1)
2955 return 0;
2956 return 1;
2957}
2958
Dan Williamsb522adc2009-03-31 15:00:31 +11002959static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2960{
2961 unsigned long long blocks;
2962 sector_t new;
2963
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002964 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002965 return -EINVAL;
2966
2967 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2968 return -EINVAL; /* sector conversion overflow */
2969
2970 new = blocks * 2;
2971 if (new != blocks * 2)
2972 return -EINVAL; /* unsigned long long to sector_t overflow */
2973
2974 *sectors = new;
2975 return 0;
2976}
2977
NeilBrown83303b62006-01-06 00:21:06 -08002978static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002979rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002980{
NeilBrownfd01b882011-10-11 16:47:53 +11002981 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002982 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002983 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002984
Shaohua Lif2076e72015-10-08 21:54:12 -07002985 if (test_bit(Journal, &rdev->flags))
2986 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11002987 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002988 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002989 if (rdev->data_offset != rdev->new_data_offset)
2990 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002991 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002992 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002993 sectors = super_types[my_mddev->major_version].
2994 rdev_size_change(rdev, sectors);
2995 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002996 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002997 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002998 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002999 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003000 if (!my_mddev->pers->resize)
3001 /* Cannot change size for RAID0 or Linear etc */
3002 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003003 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003004 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003005 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003006
Andre Nolldd8ac332009-03-31 14:33:13 +11003007 rdev->sectors = sectors;
3008 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003009 /* Need to check that all other rdevs with the same
3010 * ->bdev do not overlap. 'rcu' is sufficient to walk
3011 * the rdev lists safely.
3012 * This check does not provide a hard guarantee, it
3013 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003014 */
NeilBrownfd01b882011-10-11 16:47:53 +11003015 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003016 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003017 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003018
NeilBrown8b1afc32014-09-29 15:33:20 +10003019 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003020 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003021 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003022
NeilBrowndafb20f2012-03-19 12:46:39 +11003023 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003024 if (rdev->bdev == rdev2->bdev &&
3025 rdev != rdev2 &&
3026 overlaps(rdev->data_offset, rdev->sectors,
3027 rdev2->data_offset,
3028 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003029 overlap = 1;
3030 break;
3031 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003032 if (overlap) {
3033 mddev_put(mddev);
3034 break;
3035 }
3036 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003037 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003038 if (overlap) {
3039 /* Someone else could have slipped in a size
3040 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003041 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003042 * safe, and trust userspace not to race with
3043 * itself
3044 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003045 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003046 return -EBUSY;
3047 }
3048 }
NeilBrown83303b62006-01-06 00:21:06 -08003049 return len;
3050}
3051
3052static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003053__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003054
NeilBrown3cb03002011-10-11 16:45:26 +11003055static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003056{
3057 unsigned long long recovery_start = rdev->recovery_offset;
3058
3059 if (test_bit(In_sync, &rdev->flags) ||
3060 recovery_start == MaxSector)
3061 return sprintf(page, "none\n");
3062
3063 return sprintf(page, "%llu\n", recovery_start);
3064}
3065
NeilBrown3cb03002011-10-11 16:45:26 +11003066static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003067{
3068 unsigned long long recovery_start;
3069
3070 if (cmd_match(buf, "none"))
3071 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003072 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003073 return -EINVAL;
3074
3075 if (rdev->mddev->pers &&
3076 rdev->raid_disk >= 0)
3077 return -EBUSY;
3078
3079 rdev->recovery_offset = recovery_start;
3080 if (recovery_start == MaxSector)
3081 set_bit(In_sync, &rdev->flags);
3082 else
3083 clear_bit(In_sync, &rdev->flags);
3084 return len;
3085}
3086
3087static struct rdev_sysfs_entry rdev_recovery_start =
3088__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3089
Vishal Vermafc974ee2015-12-24 19:20:34 -07003090/* sysfs access to bad-blocks list.
3091 * We present two files.
3092 * 'bad-blocks' lists sector numbers and lengths of ranges that
3093 * are recorded as bad. The list is truncated to fit within
3094 * the one-page limit of sysfs.
3095 * Writing "sector length" to this file adds an acknowledged
3096 * bad block list.
3097 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3098 * been acknowledged. Writing to this file adds bad blocks
3099 * without acknowledging them. This is largely for testing.
3100 */
NeilBrown3cb03002011-10-11 16:45:26 +11003101static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003102{
3103 return badblocks_show(&rdev->badblocks, page, 0);
3104}
NeilBrown3cb03002011-10-11 16:45:26 +11003105static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003106{
NeilBrownde393cd2011-07-28 11:31:48 +10003107 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3108 /* Maybe that ack was all we needed */
3109 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3110 wake_up(&rdev->blocked_wait);
3111 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003112}
3113static struct rdev_sysfs_entry rdev_bad_blocks =
3114__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3115
NeilBrown3cb03002011-10-11 16:45:26 +11003116static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003117{
3118 return badblocks_show(&rdev->badblocks, page, 1);
3119}
NeilBrown3cb03002011-10-11 16:45:26 +11003120static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003121{
3122 return badblocks_store(&rdev->badblocks, page, len, 1);
3123}
3124static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3125__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3126
NeilBrown86e6ffd2005-11-08 21:39:24 -08003127static struct attribute *rdev_default_attrs[] = {
3128 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003129 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003130 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003131 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003132 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003133 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003134 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003135 &rdev_bad_blocks.attr,
3136 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003137 NULL,
3138};
3139static ssize_t
3140rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3141{
3142 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003143 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003144
3145 if (!entry->show)
3146 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003147 if (!rdev->mddev)
3148 return -EBUSY;
3149 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003150}
3151
3152static ssize_t
3153rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3154 const char *page, size_t length)
3155{
3156 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003157 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003158 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003159 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003160
3161 if (!entry->store)
3162 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003163 if (!capable(CAP_SYS_ADMIN))
3164 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003165 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003166 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003167 if (rdev->mddev == NULL)
3168 rv = -EBUSY;
3169 else
3170 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003171 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003172 }
3173 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003174}
3175
3176static void rdev_free(struct kobject *ko)
3177{
NeilBrown3cb03002011-10-11 16:45:26 +11003178 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003179 kfree(rdev);
3180}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003181static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003182 .show = rdev_attr_show,
3183 .store = rdev_attr_store,
3184};
3185static struct kobj_type rdev_ktype = {
3186 .release = rdev_free,
3187 .sysfs_ops = &rdev_sysfs_ops,
3188 .default_attrs = rdev_default_attrs,
3189};
3190
NeilBrown3cb03002011-10-11 16:45:26 +11003191int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003192{
3193 rdev->desc_nr = -1;
3194 rdev->saved_raid_disk = -1;
3195 rdev->raid_disk = -1;
3196 rdev->flags = 0;
3197 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003198 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003199 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003200 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003201 rdev->sb_loaded = 0;
3202 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003203 atomic_set(&rdev->nr_pending, 0);
3204 atomic_set(&rdev->read_errors, 0);
3205 atomic_set(&rdev->corrected_errors, 0);
3206
3207 INIT_LIST_HEAD(&rdev->same_set);
3208 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003209
3210 /* Add space to store bad block list.
3211 * This reserves the space even on arrays where it cannot
3212 * be used - I wonder if that matters
3213 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003214 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003215}
3216EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217/*
3218 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3219 *
3220 * mark the device faulty if:
3221 *
3222 * - the device is nonexistent (zero size)
3223 * - the device has no valid superblock
3224 *
3225 * a faulty rdev _never_ has rdev->sb set.
3226 */
NeilBrown3cb03002011-10-11 16:45:26 +11003227static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228{
3229 char b[BDEVNAME_SIZE];
3230 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003231 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 sector_t size;
3233
NeilBrown9ffae0c2006-01-06 00:20:32 -08003234 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003235 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
NeilBrown2230dfe2011-07-28 11:31:46 +10003238 err = md_rdev_init(rdev);
3239 if (err)
3240 goto abort_free;
3241 err = alloc_disk_sb(rdev);
3242 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 goto abort_free;
3244
NeilBrownc5d79ad2008-02-06 01:39:54 -08003245 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 if (err)
3247 goto abort_free;
3248
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003249 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003250
Mike Snitzer77304d22010-11-08 14:39:12 +01003251 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003253 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 bdevname(rdev->bdev,b));
3255 err = -EINVAL;
3256 goto abort_free;
3257 }
3258
3259 if (super_format >= 0) {
3260 err = super_types[super_format].
3261 load_super(rdev, NULL, super_minor);
3262 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003263 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003264 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003265 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 goto abort_free;
3267 }
3268 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003269 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 bdevname(rdev->bdev,b));
3271 goto abort_free;
3272 }
3273 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003274
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 return rdev;
3276
3277abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003278 if (rdev->bdev)
3279 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003280 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 kfree(rdev);
3282 return ERR_PTR(err);
3283}
3284
3285/*
3286 * Check a full RAID array for plausibility
3287 */
3288
NeilBrownf72ffdd2014-09-30 14:23:59 +10003289static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290{
3291 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003292 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 char b[BDEVNAME_SIZE];
3294
3295 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003296 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 switch (super_types[mddev->major_version].
3298 load_super(rdev, freshest, mddev->minor_version)) {
3299 case 1:
3300 freshest = rdev;
3301 break;
3302 case 0:
3303 break;
3304 default:
NeilBrown9d487392016-11-02 14:16:49 +11003305 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003307 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 }
3309
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 super_types[mddev->major_version].
3311 validate_super(mddev, freshest);
3312
3313 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003314 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003315 if (mddev->max_disks &&
3316 (rdev->desc_nr >= mddev->max_disks ||
3317 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003318 pr_warn("md: %s: %s: only %d devices permitted\n",
3319 mdname(mddev), bdevname(rdev->bdev, b),
3320 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003321 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003322 continue;
3323 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003324 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 if (super_types[mddev->major_version].
3326 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003327 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003329 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 continue;
3331 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 if (mddev->level == LEVEL_MULTIPATH) {
3334 rdev->desc_nr = i++;
3335 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003336 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003337 } else if (rdev->raid_disk >=
3338 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3339 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003340 rdev->raid_disk = -1;
3341 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
3343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344}
3345
NeilBrown72e02072009-12-14 12:49:55 +11003346/* Read a fixed-point number.
3347 * Numbers in sysfs attributes should be in "standard" units where
3348 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003349 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003350 * milliseconds or jiffies.
3351 * This function takes a decimal number with a possible fractional
3352 * component, and produces an integer which is the result of
3353 * multiplying that number by 10^'scale'.
3354 * all without any floating-point arithmetic.
3355 */
3356int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3357{
3358 unsigned long result = 0;
3359 long decimals = -1;
3360 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3361 if (*cp == '.')
3362 decimals = 0;
3363 else if (decimals < scale) {
3364 unsigned int value;
3365 value = *cp - '0';
3366 result = result * 10 + value;
3367 if (decimals >= 0)
3368 decimals++;
3369 }
3370 cp++;
3371 }
3372 if (*cp == '\n')
3373 cp++;
3374 if (*cp)
3375 return -EINVAL;
3376 if (decimals < 0)
3377 decimals = 0;
3378 while (decimals < scale) {
3379 result *= 10;
3380 decimals ++;
3381 }
3382 *res = result;
3383 return 0;
3384}
3385
NeilBrowneae17012005-11-08 21:39:23 -08003386static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003387safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003388{
3389 int msec = (mddev->safemode_delay*1000)/HZ;
3390 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3391}
3392static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003393safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003394{
NeilBrown16f17b32006-06-26 00:27:37 -07003395 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003396
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003397 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003398 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003399 return -EINVAL;
3400 }
3401
NeilBrown72e02072009-12-14 12:49:55 +11003402 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003403 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003404 if (msec == 0)
3405 mddev->safemode_delay = 0;
3406 else {
NeilBrown19052c02008-08-05 15:54:13 +10003407 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003408 unsigned long new_delay = (msec*HZ)/1000;
3409
3410 if (new_delay == 0)
3411 new_delay = 1;
3412 mddev->safemode_delay = new_delay;
3413 if (new_delay < old_delay || old_delay == 0)
3414 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003415 }
3416 return len;
3417}
3418static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003419__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003420
3421static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003422level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003423{
NeilBrown36d091f2014-12-15 12:56:58 +11003424 struct md_personality *p;
3425 int ret;
3426 spin_lock(&mddev->lock);
3427 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003428 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003429 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003430 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003431 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003432 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003433 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003434 else
NeilBrown36d091f2014-12-15 12:56:58 +11003435 ret = 0;
3436 spin_unlock(&mddev->lock);
3437 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003438}
3439
NeilBrownd9d166c2006-01-06 00:20:51 -08003440static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003441level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003442{
Dan Williamsf2859af2010-05-02 10:04:16 -07003443 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003444 ssize_t rv;
3445 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003446 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003447 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003448 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003449 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003450
NeilBrown67918752014-12-15 12:57:01 +11003451 if (slen == 0 || slen >= sizeof(clevel))
3452 return -EINVAL;
3453
3454 rv = mddev_lock(mddev);
3455 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003456 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003457
3458 if (mddev->pers == NULL) {
3459 strncpy(mddev->clevel, buf, slen);
3460 if (mddev->clevel[slen-1] == '\n')
3461 slen--;
3462 mddev->clevel[slen] = 0;
3463 mddev->level = LEVEL_NONE;
3464 rv = len;
3465 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003466 }
NeilBrown67918752014-12-15 12:57:01 +11003467 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003468 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003469 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003470
3471 /* request to change the personality. Need to ensure:
3472 * - array is not engaged in resync/recovery/reshape
3473 * - old personality can be suspended
3474 * - new personality will access other array.
3475 */
3476
NeilBrown67918752014-12-15 12:57:01 +11003477 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003478 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003479 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003480 mddev->reshape_position != MaxSector ||
3481 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003482 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003483
NeilBrown67918752014-12-15 12:57:01 +11003484 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003485 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003486 pr_warn("md: %s: %s does not support online personality change\n",
3487 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003488 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003489 }
3490
3491 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003492 strncpy(clevel, buf, slen);
3493 if (clevel[slen-1] == '\n')
3494 slen--;
3495 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003496 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003497 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003498
Dan Williamsf2859af2010-05-02 10:04:16 -07003499 if (request_module("md-%s", clevel) != 0)
3500 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003501 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003502 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003503 if (!pers || !try_module_get(pers->owner)) {
3504 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003505 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003506 rv = -EINVAL;
3507 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003508 }
3509 spin_unlock(&pers_lock);
3510
3511 if (pers == mddev->pers) {
3512 /* Nothing to do! */
3513 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003514 rv = len;
3515 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003516 }
3517 if (!pers->takeover) {
3518 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003519 pr_warn("md: %s: %s does not support personality takeover\n",
3520 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003521 rv = -EINVAL;
3522 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003523 }
3524
NeilBrowndafb20f2012-03-19 12:46:39 +11003525 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003526 rdev->new_raid_disk = rdev->raid_disk;
3527
NeilBrown245f46c2009-03-31 14:39:39 +11003528 /* ->takeover must set new_* and/or delta_disks
3529 * if it succeeds, and may set them when it fails.
3530 */
3531 priv = pers->takeover(mddev);
3532 if (IS_ERR(priv)) {
3533 mddev->new_level = mddev->level;
3534 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003535 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003536 mddev->raid_disks -= mddev->delta_disks;
3537 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003538 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003539 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003540 pr_warn("md: %s: %s would not accept array\n",
3541 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003542 rv = PTR_ERR(priv);
3543 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003544 }
3545
3546 /* Looks like we have a winner */
3547 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003548 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003549
3550 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003551 oldpers = mddev->pers;
3552 oldpriv = mddev->private;
3553 mddev->pers = pers;
3554 mddev->private = priv;
3555 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3556 mddev->level = mddev->new_level;
3557 mddev->layout = mddev->new_layout;
3558 mddev->chunk_sectors = mddev->new_chunk_sectors;
3559 mddev->delta_disks = 0;
3560 mddev->reshape_backwards = 0;
3561 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003562 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003563
NeilBrowndb721d32014-12-15 12:56:58 +11003564 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003565 mddev->external) {
3566 /* We are converting from a no-redundancy array
3567 * to a redundancy array and metadata is managed
3568 * externally so we need to be sure that writes
3569 * won't block due to a need to transition
3570 * clean->dirty
3571 * until external management is started.
3572 */
3573 mddev->in_sync = 0;
3574 mddev->safemode_delay = 0;
3575 mddev->safemode = 0;
3576 }
3577
NeilBrowndb721d32014-12-15 12:56:58 +11003578 oldpers->free(mddev, oldpriv);
3579
3580 if (oldpers->sync_request == NULL &&
3581 pers->sync_request != NULL) {
3582 /* need to add the md_redundancy_group */
3583 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11003584 pr_warn("md: cannot register extra attributes for %s\n",
3585 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11003586 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3587 }
3588 if (oldpers->sync_request != NULL &&
3589 pers->sync_request == NULL) {
3590 /* need to remove the md_redundancy_group */
3591 if (mddev->to_remove == NULL)
3592 mddev->to_remove = &md_redundancy_group;
3593 }
3594
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003595 module_put(oldpers->owner);
3596
NeilBrowndafb20f2012-03-19 12:46:39 +11003597 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003598 if (rdev->raid_disk < 0)
3599 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003600 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003601 rdev->new_raid_disk = -1;
3602 if (rdev->new_raid_disk == rdev->raid_disk)
3603 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003604 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003605 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003606 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003607 if (rdev->raid_disk < 0)
3608 continue;
3609 if (rdev->new_raid_disk == rdev->raid_disk)
3610 continue;
3611 rdev->raid_disk = rdev->new_raid_disk;
3612 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003613 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003614 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003615 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11003616 pr_warn("md: cannot register rd%d for %s after level change\n",
3617 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003618 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003619 }
3620
NeilBrowndb721d32014-12-15 12:56:58 +11003621 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003622 /* this is now an array without redundancy, so
3623 * it must always be in_sync
3624 */
3625 mddev->in_sync = 1;
3626 del_timer_sync(&mddev->safemode_timer);
3627 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003628 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003629 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003630 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003631 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003632 if (!mddev->thread)
3633 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003634 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003635 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003636 rv = len;
3637out_unlock:
3638 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003639 return rv;
3640}
3641
3642static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003643__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003644
NeilBrownd4dbd022006-06-26 00:27:59 -07003645static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003646layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003647{
3648 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003649 if (mddev->reshape_position != MaxSector &&
3650 mddev->layout != mddev->new_layout)
3651 return sprintf(page, "%d (%d)\n",
3652 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003653 return sprintf(page, "%d\n", mddev->layout);
3654}
3655
3656static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003657layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003658{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003659 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003660 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003661
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003662 err = kstrtouint(buf, 10, &n);
3663 if (err < 0)
3664 return err;
NeilBrown67918752014-12-15 12:57:01 +11003665 err = mddev_lock(mddev);
3666 if (err)
3667 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003668
NeilBrownb3546032009-03-31 14:56:41 +11003669 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003670 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003671 err = -EBUSY;
3672 else if (mddev->ro)
3673 err = -EROFS;
3674 else {
3675 mddev->new_layout = n;
3676 err = mddev->pers->check_reshape(mddev);
3677 if (err)
3678 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003679 }
NeilBrownb3546032009-03-31 14:56:41 +11003680 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003681 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003682 if (mddev->reshape_position == MaxSector)
3683 mddev->layout = n;
3684 }
NeilBrown67918752014-12-15 12:57:01 +11003685 mddev_unlock(mddev);
3686 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003687}
3688static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003689__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003690
NeilBrowneae17012005-11-08 21:39:23 -08003691static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003692raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003693{
NeilBrownbb636542005-11-08 21:39:45 -08003694 if (mddev->raid_disks == 0)
3695 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003696 if (mddev->reshape_position != MaxSector &&
3697 mddev->delta_disks != 0)
3698 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3699 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003700 return sprintf(page, "%d\n", mddev->raid_disks);
3701}
3702
NeilBrownfd01b882011-10-11 16:47:53 +11003703static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003704
3705static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003706raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003707{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003708 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003709 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003710
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003711 err = kstrtouint(buf, 10, &n);
3712 if (err < 0)
3713 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003714
NeilBrown67918752014-12-15 12:57:01 +11003715 err = mddev_lock(mddev);
3716 if (err)
3717 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003718 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003719 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003720 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003721 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003722 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003723
NeilBrown67918752014-12-15 12:57:01 +11003724 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003725 rdev_for_each(rdev, mddev) {
3726 if (olddisks < n &&
3727 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003728 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003729 if (olddisks > n &&
3730 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003731 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003732 }
NeilBrown67918752014-12-15 12:57:01 +11003733 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003734 mddev->delta_disks = n - olddisks;
3735 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003736 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003737 } else
NeilBrownda943b992006-01-06 00:20:54 -08003738 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003739out_unlock:
3740 mddev_unlock(mddev);
3741 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003742}
3743static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003744__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003745
NeilBrown24dd4692005-11-08 21:39:26 -08003746static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003747chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003748{
NeilBrown08a02ec2007-05-09 02:35:38 -07003749 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003750 mddev->chunk_sectors != mddev->new_chunk_sectors)
3751 return sprintf(page, "%d (%d)\n",
3752 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003753 mddev->chunk_sectors << 9);
3754 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003755}
3756
3757static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003758chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003759{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003760 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003761 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003762
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003763 err = kstrtoul(buf, 10, &n);
3764 if (err < 0)
3765 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003766
NeilBrown67918752014-12-15 12:57:01 +11003767 err = mddev_lock(mddev);
3768 if (err)
3769 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003770 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003771 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003772 err = -EBUSY;
3773 else if (mddev->ro)
3774 err = -EROFS;
3775 else {
3776 mddev->new_chunk_sectors = n >> 9;
3777 err = mddev->pers->check_reshape(mddev);
3778 if (err)
3779 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003780 }
NeilBrownb3546032009-03-31 14:56:41 +11003781 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003782 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003783 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003784 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003785 }
NeilBrown67918752014-12-15 12:57:01 +11003786 mddev_unlock(mddev);
3787 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003788}
3789static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003790__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003791
NeilBrowna94213b2006-06-26 00:28:00 -07003792static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003793resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003794{
NeilBrownd1a7c502009-03-31 15:24:32 +11003795 if (mddev->recovery_cp == MaxSector)
3796 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003797 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3798}
3799
3800static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003801resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003802{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003803 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003804 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003805
3806 if (cmd_match(buf, "none"))
3807 n = MaxSector;
3808 else {
3809 err = kstrtoull(buf, 10, &n);
3810 if (err < 0)
3811 return err;
3812 if (n != (sector_t)n)
3813 return -EINVAL;
3814 }
NeilBrowna94213b2006-06-26 00:28:00 -07003815
NeilBrown67918752014-12-15 12:57:01 +11003816 err = mddev_lock(mddev);
3817 if (err)
3818 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003819 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003820 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003821
NeilBrown67918752014-12-15 12:57:01 +11003822 if (!err) {
3823 mddev->recovery_cp = n;
3824 if (mddev->pers)
3825 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3826 }
3827 mddev_unlock(mddev);
3828 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003829}
3830static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003831__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3832 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003833
NeilBrown9e653b62006-06-26 00:27:58 -07003834/*
3835 * The array state can be:
3836 *
3837 * clear
3838 * No devices, no size, no level
3839 * Equivalent to STOP_ARRAY ioctl
3840 * inactive
3841 * May have some settings, but array is not active
3842 * all IO results in error
3843 * When written, doesn't tear down array, but just stops it
3844 * suspended (not supported yet)
3845 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003846 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003847 * readonly
3848 * no resync can happen. no superblocks get written.
3849 * write requests fail
3850 * read-auto
3851 * like readonly, but behaves like 'clean' on a write request.
3852 *
3853 * clean - no pending writes, but otherwise active.
3854 * When written to inactive array, starts without resync
3855 * If a write request arrives then
3856 * if metadata is known, mark 'dirty' and switch to 'active'.
3857 * if not known, block and switch to write-pending
3858 * If written to an active array that has pending writes, then fails.
3859 * active
3860 * fully active: IO and resync can be happening.
3861 * When written to inactive array, starts with resync
3862 *
3863 * write-pending
3864 * clean, but writes are blocked waiting for 'active' to be written.
3865 *
3866 * active-idle
3867 * like active, but no writes have been seen for a while (100msec).
3868 *
3869 */
3870enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3871 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003872static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003873 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3874 "write-pending", "active-idle", NULL };
3875
3876static int match_word(const char *word, char **list)
3877{
3878 int n;
3879 for (n=0; list[n]; n++)
3880 if (cmd_match(word, list[n]))
3881 break;
3882 return n;
3883}
3884
3885static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003886array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003887{
3888 enum array_state st = inactive;
3889
3890 if (mddev->pers)
3891 switch(mddev->ro) {
3892 case 1:
3893 st = readonly;
3894 break;
3895 case 2:
3896 st = read_auto;
3897 break;
3898 case 0:
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02003899 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003900 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02003901 else if (mddev->in_sync)
3902 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07003903 else if (mddev->safemode)
3904 st = active_idle;
3905 else
3906 st = active;
3907 }
3908 else {
3909 if (list_empty(&mddev->disks) &&
3910 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003911 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003912 st = clear;
3913 else
3914 st = inactive;
3915 }
3916 return sprintf(page, "%s\n", array_states[st]);
3917}
3918
NeilBrownf72ffdd2014-09-30 14:23:59 +10003919static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3920static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3921static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003922static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003923
3924static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003925array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003926{
NeilBrown67918752014-12-15 12:57:01 +11003927 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003928 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003929
3930 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3931 /* don't take reconfig_mutex when toggling between
3932 * clean and active
3933 */
3934 spin_lock(&mddev->lock);
3935 if (st == active) {
3936 restart_array(mddev);
3937 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02003938 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11003939 wake_up(&mddev->sb_wait);
3940 err = 0;
3941 } else /* st == clean */ {
3942 restart_array(mddev);
3943 if (atomic_read(&mddev->writes_pending) == 0) {
3944 if (mddev->in_sync == 0) {
3945 mddev->in_sync = 1;
3946 if (mddev->safemode == 1)
3947 mddev->safemode = 0;
3948 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3949 }
3950 err = 0;
3951 } else
3952 err = -EBUSY;
3953 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02003954 if (!err)
3955 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11003956 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003957 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003958 }
3959 err = mddev_lock(mddev);
3960 if (err)
3961 return err;
3962 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003963 switch(st) {
3964 case bad_word:
3965 break;
3966 case clear:
3967 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003968 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003969 break;
3970 case inactive:
3971 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003972 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003973 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003974 else
NeilBrowne6910632008-02-06 01:39:51 -08003975 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003976 break;
3977 case suspended:
3978 break; /* not supported yet */
3979 case readonly:
3980 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003981 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003982 else {
3983 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003984 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003985 err = do_md_run(mddev);
3986 }
3987 break;
3988 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003989 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003990 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003991 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003992 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003993 err = restart_array(mddev);
3994 if (err == 0) {
3995 mddev->ro = 2;
3996 set_disk_ro(mddev->gendisk, 0);
3997 }
NeilBrown9e653b62006-06-26 00:27:58 -07003998 } else {
3999 mddev->ro = 2;
4000 err = do_md_run(mddev);
4001 }
4002 break;
4003 case clean:
4004 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004005 err = restart_array(mddev);
4006 if (err)
4007 break;
NeilBrown85572d72014-12-15 12:56:56 +11004008 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004009 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08004010 if (mddev->in_sync == 0) {
4011 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07004012 if (mddev->safemode == 1)
4013 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10004014 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08004015 }
4016 err = 0;
4017 } else
4018 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004019 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004020 } else
4021 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004022 break;
4023 case active:
4024 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004025 err = restart_array(mddev);
4026 if (err)
4027 break;
NeilBrown070dc6d2010-08-30 17:33:34 +10004028 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004029 wake_up(&mddev->sb_wait);
4030 err = 0;
4031 } else {
4032 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004033 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004034 err = do_md_run(mddev);
4035 }
4036 break;
4037 case write_pending:
4038 case active_idle:
4039 /* these cannot be set */
4040 break;
4041 }
NeilBrown67918752014-12-15 12:57:01 +11004042
4043 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004044 if (mddev->hold_active == UNTIL_IOCTL)
4045 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004046 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004047 }
NeilBrown67918752014-12-15 12:57:01 +11004048 mddev_unlock(mddev);
4049 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004050}
NeilBrown80ca3a42006-07-10 04:44:18 -07004051static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004052__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004053
NeilBrown6d7ff7382006-01-06 00:21:16 -08004054static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004055max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004056 return sprintf(page, "%d\n",
4057 atomic_read(&mddev->max_corr_read_errors));
4058}
4059
4060static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004061max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004062{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004063 unsigned int n;
4064 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004065
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004066 rv = kstrtouint(buf, 10, &n);
4067 if (rv < 0)
4068 return rv;
4069 atomic_set(&mddev->max_corr_read_errors, n);
4070 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004071}
4072
4073static struct md_sysfs_entry max_corr_read_errors =
4074__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4075 max_corrected_read_errors_store);
4076
4077static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004078null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004079{
4080 return -EINVAL;
4081}
4082
4083static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004084new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004085{
4086 /* buf must be %d:%d\n? giving major and minor numbers */
4087 /* The new device is added to the array.
4088 * If the array has a persistent superblock, we read the
4089 * superblock to initialise info and check validity.
4090 * Otherwise, only checking done is that in bind_rdev_to_array,
4091 * which mainly checks size.
4092 */
4093 char *e;
4094 int major = simple_strtoul(buf, &e, 10);
4095 int minor;
4096 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004097 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004098 int err;
4099
4100 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4101 return -EINVAL;
4102 minor = simple_strtoul(e+1, &e, 10);
4103 if (*e && *e != '\n')
4104 return -EINVAL;
4105 dev = MKDEV(major, minor);
4106 if (major != MAJOR(dev) ||
4107 minor != MINOR(dev))
4108 return -EOVERFLOW;
4109
NeilBrown67918752014-12-15 12:57:01 +11004110 flush_workqueue(md_misc_wq);
4111
4112 err = mddev_lock(mddev);
4113 if (err)
4114 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004115 if (mddev->persistent) {
4116 rdev = md_import_device(dev, mddev->major_version,
4117 mddev->minor_version);
4118 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004119 struct md_rdev *rdev0
4120 = list_entry(mddev->disks.next,
4121 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004122 err = super_types[mddev->major_version]
4123 .load_super(rdev, rdev0, mddev->minor_version);
4124 if (err < 0)
4125 goto out;
4126 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004127 } else if (mddev->external)
4128 rdev = md_import_device(dev, -2, -1);
4129 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004130 rdev = md_import_device(dev, -1, -1);
4131
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004132 if (IS_ERR(rdev)) {
4133 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004134 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004135 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004136 err = bind_rdev_to_array(rdev, mddev);
4137 out:
4138 if (err)
4139 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004140 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004141 return err ? err : len;
4142}
4143
4144static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004145__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004146
4147static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004148bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004149{
4150 char *end;
4151 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004152 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004153
NeilBrown67918752014-12-15 12:57:01 +11004154 err = mddev_lock(mddev);
4155 if (err)
4156 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004157 if (!mddev->bitmap)
4158 goto out;
4159 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4160 while (*buf) {
4161 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4162 if (buf == end) break;
4163 if (*end == '-') { /* range */
4164 buf = end + 1;
4165 end_chunk = simple_strtoul(buf, &end, 0);
4166 if (buf == end) break;
4167 }
4168 if (*end && !isspace(*end)) break;
4169 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004170 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004171 }
4172 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4173out:
NeilBrown67918752014-12-15 12:57:01 +11004174 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004175 return len;
4176}
4177
4178static struct md_sysfs_entry md_bitmap =
4179__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4180
4181static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004182size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004183{
Andre Noll58c0fed2009-03-31 14:33:13 +11004184 return sprintf(page, "%llu\n",
4185 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004186}
4187
NeilBrownfd01b882011-10-11 16:47:53 +11004188static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004189
4190static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004191size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004192{
4193 /* If array is inactive, we can reduce the component size, but
4194 * not increase it (except from 0).
4195 * If array is active, we can try an on-line resize
4196 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004197 sector_t sectors;
4198 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004199
Andre Noll58c0fed2009-03-31 14:33:13 +11004200 if (err < 0)
4201 return err;
NeilBrown67918752014-12-15 12:57:01 +11004202 err = mddev_lock(mddev);
4203 if (err)
4204 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004205 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004206 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004207 if (err == 0)
4208 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004209 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004210 if (mddev->dev_sectors == 0 ||
4211 mddev->dev_sectors > sectors)
4212 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004213 else
4214 err = -ENOSPC;
4215 }
NeilBrown67918752014-12-15 12:57:01 +11004216 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004217 return err ? err : len;
4218}
4219
4220static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004221__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004222
Masanari Iida83f0d772012-10-30 00:18:08 +09004223/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004224 * This is one of
4225 * 'none' for arrays with no metadata (good luck...)
4226 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004227 * or N.M for internally known formats
4228 */
4229static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004230metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004231{
4232 if (mddev->persistent)
4233 return sprintf(page, "%d.%d\n",
4234 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004235 else if (mddev->external)
4236 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004237 else
4238 return sprintf(page, "none\n");
4239}
4240
4241static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004242metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004243{
4244 int major, minor;
4245 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004246 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004247 /* Changing the details of 'external' metadata is
4248 * always permitted. Otherwise there must be
4249 * no devices attached to the array.
4250 */
NeilBrown67918752014-12-15 12:57:01 +11004251
4252 err = mddev_lock(mddev);
4253 if (err)
4254 return err;
4255 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004256 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4257 ;
4258 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004259 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004260
NeilBrown67918752014-12-15 12:57:01 +11004261 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004262 if (cmd_match(buf, "none")) {
4263 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004264 mddev->external = 0;
4265 mddev->major_version = 0;
4266 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004267 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004268 }
4269 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004270 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004271 if (namelen >= sizeof(mddev->metadata_type))
4272 namelen = sizeof(mddev->metadata_type)-1;
4273 strncpy(mddev->metadata_type, buf+9, namelen);
4274 mddev->metadata_type[namelen] = 0;
4275 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4276 mddev->metadata_type[--namelen] = 0;
4277 mddev->persistent = 0;
4278 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004279 mddev->major_version = 0;
4280 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004281 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004282 }
4283 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004284 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004285 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004286 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004287 buf = e+1;
4288 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004289 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004290 goto out_unlock;
4291 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004292 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004293 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004294 mddev->major_version = major;
4295 mddev->minor_version = minor;
4296 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004297 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004298 err = 0;
4299out_unlock:
4300 mddev_unlock(mddev);
4301 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004302}
4303
4304static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004305__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004306
NeilBrowna35b0d62006-01-06 00:20:49 -08004307static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004308action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004309{
NeilBrown7eec3142005-11-08 21:39:44 -08004310 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004311 unsigned long recovery = mddev->recovery;
4312 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004313 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004314 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4315 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4316 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004317 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004318 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4319 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004320 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004321 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004322 type = "check";
4323 else
4324 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004325 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004326 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004327 else if (mddev->reshape_position != MaxSector)
4328 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004329 }
4330 return sprintf(page, "%s\n", type);
4331}
4332
4333static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004334action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004335{
NeilBrown7eec3142005-11-08 21:39:44 -08004336 if (!mddev->pers || !mddev->pers->sync_request)
4337 return -EINVAL;
4338
NeilBrownb6a9ce62009-05-26 09:41:17 +10004339
4340 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004341 if (cmd_match(page, "frozen"))
4342 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4343 else
4344 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004345 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4346 mddev_lock(mddev) == 0) {
4347 flush_workqueue(md_misc_wq);
4348 if (mddev->sync_thread) {
4349 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004350 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004351 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004352 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004353 }
NeilBrown312045e2015-12-21 11:01:21 +11004354 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004355 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004356 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004357 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004358 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004359 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004360 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004361 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004362 int err;
4363 if (mddev->pers->start_reshape == NULL)
4364 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004365 err = mddev_lock(mddev);
4366 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004367 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4368 err = -EBUSY;
4369 else {
4370 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4371 err = mddev->pers->start_reshape(mddev);
4372 }
NeilBrown67918752014-12-15 12:57:01 +11004373 mddev_unlock(mddev);
4374 }
NeilBrown16484bf2006-03-27 01:18:13 -08004375 if (err)
4376 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004377 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004378 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004379 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004380 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004381 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004382 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004383 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004384 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4385 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004386 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004387 if (mddev->ro == 2) {
4388 /* A write to sync_action is enough to justify
4389 * canceling read-auto mode
4390 */
4391 mddev->ro = 0;
4392 md_wakeup_thread(mddev->sync_thread);
4393 }
NeilBrown03c902e2006-01-06 00:20:46 -08004394 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004395 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004396 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004397 return len;
4398}
4399
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004400static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004401__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004402
4403static ssize_t
4404last_sync_action_show(struct mddev *mddev, char *page)
4405{
4406 return sprintf(page, "%s\n", mddev->last_sync_action);
4407}
4408
4409static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4410
NeilBrown9d888832005-11-08 21:39:26 -08004411static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004412mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004413{
4414 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004415 (unsigned long long)
4416 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004417}
4418
NeilBrown80ca3a42006-07-10 04:44:18 -07004419static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004420
NeilBrown88202a02006-01-06 00:21:36 -08004421static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004422sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004423{
4424 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4425 mddev->sync_speed_min ? "local": "system");
4426}
4427
4428static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004429sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004430{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004431 unsigned int min;
4432 int rv;
4433
NeilBrown88202a02006-01-06 00:21:36 -08004434 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004435 min = 0;
4436 } else {
4437 rv = kstrtouint(buf, 10, &min);
4438 if (rv < 0)
4439 return rv;
4440 if (min == 0)
4441 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004442 }
NeilBrown88202a02006-01-06 00:21:36 -08004443 mddev->sync_speed_min = min;
4444 return len;
4445}
4446
4447static struct md_sysfs_entry md_sync_min =
4448__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4449
4450static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004451sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004452{
4453 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4454 mddev->sync_speed_max ? "local": "system");
4455}
4456
4457static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004458sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004459{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004460 unsigned int max;
4461 int rv;
4462
NeilBrown88202a02006-01-06 00:21:36 -08004463 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004464 max = 0;
4465 } else {
4466 rv = kstrtouint(buf, 10, &max);
4467 if (rv < 0)
4468 return rv;
4469 if (max == 0)
4470 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004471 }
NeilBrown88202a02006-01-06 00:21:36 -08004472 mddev->sync_speed_max = max;
4473 return len;
4474}
4475
4476static struct md_sysfs_entry md_sync_max =
4477__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4478
Iustin Popd7f3d292007-10-16 23:30:54 -07004479static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004480degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004481{
4482 return sprintf(page, "%d\n", mddev->degraded);
4483}
4484static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004485
4486static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004487sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004488{
4489 return sprintf(page, "%d\n", mddev->parallel_resync);
4490}
4491
4492static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004493sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004494{
4495 long n;
4496
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004497 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004498 return -EINVAL;
4499
4500 if (n != 0 && n != 1)
4501 return -EINVAL;
4502
4503 mddev->parallel_resync = n;
4504
4505 if (mddev->sync_thread)
4506 wake_up(&resync_wait);
4507
4508 return len;
4509}
4510
4511/* force parallel resync, even with shared block devices */
4512static struct md_sysfs_entry md_sync_force_parallel =
4513__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4514 sync_force_parallel_show, sync_force_parallel_store);
4515
4516static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004517sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004518{
4519 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004520 if (mddev->curr_resync == 0)
4521 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004522 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4523 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004524 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004525 db = resync - mddev->resync_mark_cnt;
4526 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004527}
4528
NeilBrown80ca3a42006-07-10 04:44:18 -07004529static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004530
4531static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004532sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004533{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004534 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004535
NeilBrownacb180b2009-04-14 16:28:34 +10004536 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4537 return sprintf(page, "none\n");
4538
NeilBrown72f36d52012-10-11 14:25:57 +11004539 if (mddev->curr_resync == 1 ||
4540 mddev->curr_resync == 2)
4541 return sprintf(page, "delayed\n");
4542
NeilBrownc804cde2012-05-21 09:28:33 +10004543 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4544 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004545 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004546 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004547 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004548
NeilBrownacb180b2009-04-14 16:28:34 +10004549 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004550 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004551}
4552
NeilBrown750f1992014-09-30 08:53:05 +10004553static struct md_sysfs_entry md_sync_completed =
4554 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004555
NeilBrowne464eaf2006-03-27 01:18:14 -08004556static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004557min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004558{
4559 return sprintf(page, "%llu\n",
4560 (unsigned long long)mddev->resync_min);
4561}
4562static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004563min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004564{
4565 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004566 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004567
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004568 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004569 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004570
4571 spin_lock(&mddev->lock);
4572 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004573 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004574 goto out_unlock;
4575
4576 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004577 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004578 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004579
NeilBrown50c37b12015-03-23 17:36:38 +11004580 /* Round down to multiple of 4K for safety */
4581 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004582 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004583
NeilBrown23da4222014-12-15 12:57:01 +11004584out_unlock:
4585 spin_unlock(&mddev->lock);
4586 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004587}
4588
4589static struct md_sysfs_entry md_min_sync =
4590__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4591
4592static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004593max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004594{
4595 if (mddev->resync_max == MaxSector)
4596 return sprintf(page, "max\n");
4597 else
4598 return sprintf(page, "%llu\n",
4599 (unsigned long long)mddev->resync_max);
4600}
4601static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004602max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004603{
NeilBrown23da4222014-12-15 12:57:01 +11004604 int err;
4605 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004606 if (strncmp(buf, "max", 3) == 0)
4607 mddev->resync_max = MaxSector;
4608 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004609 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004610 int chunk;
4611
4612 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004613 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004614 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004615 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004616 goto out_unlock;
4617
4618 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004619 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004620 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004621 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004622 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004623
4624 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004625 chunk = mddev->chunk_sectors;
4626 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004627 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004628
4629 err = -EINVAL;
4630 if (sector_div(temp, chunk))
4631 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004632 }
4633 mddev->resync_max = max;
4634 }
4635 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004636 err = 0;
4637out_unlock:
4638 spin_unlock(&mddev->lock);
4639 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004640}
4641
4642static struct md_sysfs_entry md_max_sync =
4643__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4644
4645static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004646suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004647{
4648 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4649}
4650
4651static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004652suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004653{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004654 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004655 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004656
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004657 err = kstrtoull(buf, 10, &new);
4658 if (err < 0)
4659 return err;
4660 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004661 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004662
NeilBrown67918752014-12-15 12:57:01 +11004663 err = mddev_lock(mddev);
4664 if (err)
4665 return err;
4666 err = -EINVAL;
4667 if (mddev->pers == NULL ||
4668 mddev->pers->quiesce == NULL)
4669 goto unlock;
4670 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004671 mddev->suspend_lo = new;
4672 if (new >= old)
4673 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004674 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004675 else {
4676 /* Expanding suspended region - need to wait */
4677 mddev->pers->quiesce(mddev, 1);
4678 mddev->pers->quiesce(mddev, 0);
4679 }
NeilBrown67918752014-12-15 12:57:01 +11004680 err = 0;
4681unlock:
4682 mddev_unlock(mddev);
4683 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004684}
4685static struct md_sysfs_entry md_suspend_lo =
4686__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4687
NeilBrowne464eaf2006-03-27 01:18:14 -08004688static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004689suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004690{
4691 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4692}
4693
4694static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004695suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004696{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004697 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004698 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004699
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004700 err = kstrtoull(buf, 10, &new);
4701 if (err < 0)
4702 return err;
4703 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004704 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004705
NeilBrown67918752014-12-15 12:57:01 +11004706 err = mddev_lock(mddev);
4707 if (err)
4708 return err;
4709 err = -EINVAL;
4710 if (mddev->pers == NULL ||
4711 mddev->pers->quiesce == NULL)
4712 goto unlock;
4713 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004714 mddev->suspend_hi = new;
4715 if (new <= old)
4716 /* Shrinking suspended region */
4717 mddev->pers->quiesce(mddev, 2);
4718 else {
4719 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004720 mddev->pers->quiesce(mddev, 1);
4721 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004722 }
NeilBrown67918752014-12-15 12:57:01 +11004723 err = 0;
4724unlock:
4725 mddev_unlock(mddev);
4726 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004727}
4728static struct md_sysfs_entry md_suspend_hi =
4729__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4730
NeilBrown08a02ec2007-05-09 02:35:38 -07004731static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004732reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004733{
4734 if (mddev->reshape_position != MaxSector)
4735 return sprintf(page, "%llu\n",
4736 (unsigned long long)mddev->reshape_position);
4737 strcpy(page, "none\n");
4738 return 5;
4739}
4740
4741static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004742reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004743{
NeilBrownc6563a82012-05-21 09:27:00 +10004744 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004745 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004746 int err;
NeilBrown67918752014-12-15 12:57:01 +11004747
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004748 err = kstrtoull(buf, 10, &new);
4749 if (err < 0)
4750 return err;
4751 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004752 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004753 err = mddev_lock(mddev);
4754 if (err)
4755 return err;
4756 err = -EBUSY;
4757 if (mddev->pers)
4758 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004759 mddev->reshape_position = new;
4760 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004761 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004762 mddev->new_level = mddev->level;
4763 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004764 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004765 rdev_for_each(rdev, mddev)
4766 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004767 err = 0;
4768unlock:
4769 mddev_unlock(mddev);
4770 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004771}
4772
4773static struct md_sysfs_entry md_reshape_position =
4774__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4775 reshape_position_store);
4776
Dan Williamsb522adc2009-03-31 15:00:31 +11004777static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004778reshape_direction_show(struct mddev *mddev, char *page)
4779{
4780 return sprintf(page, "%s\n",
4781 mddev->reshape_backwards ? "backwards" : "forwards");
4782}
4783
4784static ssize_t
4785reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4786{
4787 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004788 int err;
4789
NeilBrown2c810cd2012-05-21 09:27:00 +10004790 if (cmd_match(buf, "forwards"))
4791 backwards = 0;
4792 else if (cmd_match(buf, "backwards"))
4793 backwards = 1;
4794 else
4795 return -EINVAL;
4796 if (mddev->reshape_backwards == backwards)
4797 return len;
4798
NeilBrown67918752014-12-15 12:57:01 +11004799 err = mddev_lock(mddev);
4800 if (err)
4801 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004802 /* check if we are allowed to change */
4803 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004804 err = -EBUSY;
4805 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004806 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004807 err = -EINVAL;
4808 else
4809 mddev->reshape_backwards = backwards;
4810 mddev_unlock(mddev);
4811 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004812}
4813
4814static struct md_sysfs_entry md_reshape_direction =
4815__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4816 reshape_direction_store);
4817
4818static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004819array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004820{
4821 if (mddev->external_size)
4822 return sprintf(page, "%llu\n",
4823 (unsigned long long)mddev->array_sectors/2);
4824 else
4825 return sprintf(page, "default\n");
4826}
4827
4828static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004829array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004830{
4831 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004832 int err;
4833
4834 err = mddev_lock(mddev);
4835 if (err)
4836 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004837
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004838 /* cluster raid doesn't support change array_sectors */
4839 if (mddev_is_clustered(mddev))
4840 return -EINVAL;
4841
Dan Williamsb522adc2009-03-31 15:00:31 +11004842 if (strncmp(buf, "default", 7) == 0) {
4843 if (mddev->pers)
4844 sectors = mddev->pers->size(mddev, 0, 0);
4845 else
4846 sectors = mddev->array_sectors;
4847
4848 mddev->external_size = 0;
4849 } else {
4850 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004851 err = -EINVAL;
4852 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4853 err = -E2BIG;
4854 else
4855 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004856 }
4857
NeilBrown67918752014-12-15 12:57:01 +11004858 if (!err) {
4859 mddev->array_sectors = sectors;
4860 if (mddev->pers) {
4861 set_capacity(mddev->gendisk, mddev->array_sectors);
4862 revalidate_disk(mddev->gendisk);
4863 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004864 }
NeilBrown67918752014-12-15 12:57:01 +11004865 mddev_unlock(mddev);
4866 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004867}
4868
4869static struct md_sysfs_entry md_array_size =
4870__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4871 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004872
NeilBrowneae17012005-11-08 21:39:23 -08004873static struct attribute *md_default_attrs[] = {
4874 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004875 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004876 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004877 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004878 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004879 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004880 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004881 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004882 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004883 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004884 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004885 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004886 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004887 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004888 NULL,
4889};
4890
4891static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004892 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004893 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004894 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004895 &md_sync_min.attr,
4896 &md_sync_max.attr,
4897 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004898 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004899 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004900 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004901 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004902 &md_suspend_lo.attr,
4903 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004904 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004905 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004906 NULL,
4907};
NeilBrown411036f2005-11-08 21:39:40 -08004908static struct attribute_group md_redundancy_group = {
4909 .name = NULL,
4910 .attrs = md_redundancy_attrs,
4911};
4912
NeilBrowneae17012005-11-08 21:39:23 -08004913static ssize_t
4914md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4915{
4916 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004917 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004918 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004919
4920 if (!entry->show)
4921 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004922 spin_lock(&all_mddevs_lock);
4923 if (list_empty(&mddev->all_mddevs)) {
4924 spin_unlock(&all_mddevs_lock);
4925 return -EBUSY;
4926 }
4927 mddev_get(mddev);
4928 spin_unlock(&all_mddevs_lock);
4929
NeilBrownb7b17c92014-12-15 12:56:59 +11004930 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004931 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004932 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004933}
4934
4935static ssize_t
4936md_attr_store(struct kobject *kobj, struct attribute *attr,
4937 const char *page, size_t length)
4938{
4939 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004940 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004941 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004942
4943 if (!entry->store)
4944 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004945 if (!capable(CAP_SYS_ADMIN))
4946 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004947 spin_lock(&all_mddevs_lock);
4948 if (list_empty(&mddev->all_mddevs)) {
4949 spin_unlock(&all_mddevs_lock);
4950 return -EBUSY;
4951 }
4952 mddev_get(mddev);
4953 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004954 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004955 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004956 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004957}
4958
4959static void md_free(struct kobject *ko)
4960{
NeilBrownfd01b882011-10-11 16:47:53 +11004961 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004962
4963 if (mddev->sysfs_state)
4964 sysfs_put(mddev->sysfs_state);
4965
NeilBrown6cd18e72015-04-27 14:12:22 +10004966 if (mddev->queue)
4967 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004968 if (mddev->gendisk) {
4969 del_gendisk(mddev->gendisk);
4970 put_disk(mddev->gendisk);
4971 }
NeilBrowna21d1502009-01-09 08:31:09 +11004972
NeilBrowneae17012005-11-08 21:39:23 -08004973 kfree(mddev);
4974}
4975
Emese Revfy52cf25d2010-01-19 02:58:23 +01004976static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004977 .show = md_attr_show,
4978 .store = md_attr_store,
4979};
4980static struct kobj_type md_ktype = {
4981 .release = md_free,
4982 .sysfs_ops = &md_sysfs_ops,
4983 .default_attrs = md_default_attrs,
4984};
4985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986int mdp_major = 0;
4987
Dan Williams5fd3a172009-03-04 00:57:25 -07004988static void mddev_delayed_delete(struct work_struct *ws)
4989{
NeilBrownfd01b882011-10-11 16:47:53 +11004990 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004991
NeilBrown43a70502009-12-14 12:49:55 +11004992 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004993 kobject_del(&mddev->kobj);
4994 kobject_put(&mddev->kobj);
4995}
4996
NeilBrownefeb53c2009-01-09 08:31:10 +11004997static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004999 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005000 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005002 int partitioned;
5003 int shift;
5004 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005005 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
5007 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005008 return -ENODEV;
5009
5010 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5011 shift = partitioned ? MdpMinorShift : 0;
5012 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013
Tejun Heoe804ac72010-10-15 15:36:08 +02005014 /* wait for any previous instance of this device to be
5015 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005016 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005017 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005018
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005019 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005020 error = -EEXIST;
5021 if (mddev->gendisk)
5022 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005023
5024 if (name) {
5025 /* Need to ensure that 'name' is not a duplicate.
5026 */
NeilBrownfd01b882011-10-11 16:47:53 +11005027 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005028 spin_lock(&all_mddevs_lock);
5029
5030 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5031 if (mddev2->gendisk &&
5032 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5033 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005034 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005035 }
5036 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 }
NeilBrown8b765392009-01-09 08:31:08 +11005038
NeilBrown0909dc42009-07-01 12:27:21 +10005039 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005040 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005041 if (!mddev->queue)
5042 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005043 mddev->queue->queuedata = mddev;
5044
NeilBrown409c57f2009-03-31 14:39:39 +11005045 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005046 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005047
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 disk = alloc_disk(1 << shift);
5049 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005050 blk_cleanup_queue(mddev->queue);
5051 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005052 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005054 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005056 if (name)
5057 strcpy(disk->disk_name, name);
5058 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005060 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 disk->fops = &md_fops;
5063 disk->private_data = mddev;
5064 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005065 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005066 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005067 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005068 * remove it now.
5069 */
5070 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005072 /* As soon as we call add_disk(), another thread could get
5073 * through to md_open, so make sure it doesn't get too far
5074 */
5075 mutex_lock(&mddev->open_mutex);
5076 add_disk(disk);
5077
Tejun Heoed9e1982008-08-25 19:56:05 +09005078 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5079 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005080 if (error) {
5081 /* This isn't possible, but as kobject_init_and_add is marked
5082 * __must_check, we must do something with the result
5083 */
NeilBrown9d487392016-11-02 14:16:49 +11005084 pr_debug("md: cannot register %s/md - name in use\n",
5085 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005086 error = 0;
5087 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005088 if (mddev->kobj.sd &&
5089 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005090 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005091 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005092 abort:
5093 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005094 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005095 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005096 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005097 }
NeilBrownd3374822009-01-09 08:31:10 +11005098 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005099 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005100}
5101
5102static struct kobject *md_probe(dev_t dev, int *part, void *data)
5103{
5104 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 return NULL;
5106}
5107
NeilBrownefeb53c2009-01-09 08:31:10 +11005108static int add_named_array(const char *val, struct kernel_param *kp)
5109{
5110 /* val must be "md_*" where * is not all digits.
5111 * We allocate an array with a large free minor number, and
5112 * set the name to val. val must not already be an active name.
5113 */
5114 int len = strlen(val);
5115 char buf[DISK_NAME_LEN];
5116
5117 while (len && val[len-1] == '\n')
5118 len--;
5119 if (len >= DISK_NAME_LEN)
5120 return -E2BIG;
5121 strlcpy(buf, val, len+1);
5122 if (strncmp(buf, "md_", 3) != 0)
5123 return -EINVAL;
5124 return md_alloc(0, buf);
5125}
5126
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127static void md_safemode_timeout(unsigned long data)
5128{
NeilBrownfd01b882011-10-11 16:47:53 +11005129 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130
Neil Brown0fd62b82008-06-28 08:31:36 +10005131 if (!atomic_read(&mddev->writes_pending)) {
5132 mddev->safemode = 1;
5133 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10005134 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 md_wakeup_thread(mddev->thread);
5137}
5138
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005139static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
NeilBrownfd01b882011-10-11 16:47:53 +11005141int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142{
NeilBrown2604b702006-01-06 00:20:36 -08005143 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005144 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005145 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146
NeilBrowna757e642005-04-16 15:26:42 -07005147 if (list_empty(&mddev->disks))
5148 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150
5151 if (mddev->pers)
5152 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005153 /* Cannot run until previous stop completes properly */
5154 if (mddev->sysfs_active)
5155 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005156
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 /*
5158 * Analyze all RAID superblock(s)
5159 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005160 if (!mddev->raid_disks) {
5161 if (!mddev->persistent)
5162 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005163 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
NeilBrownd9d166c2006-01-06 00:20:51 -08005166 if (mddev->level != LEVEL_NONE)
5167 request_module("md-level-%d", mddev->level);
5168 else if (mddev->clevel[0])
5169 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170
5171 /*
5172 * Drop all container device buffers, from now on
5173 * the only valid external interface is through the md
5174 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005176 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005177 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 continue;
5179 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005180 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005181
5182 /* perform some consistency tests on the device.
5183 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005184 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005185 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005186 if (rdev->meta_bdev) {
5187 /* Nothing to check */;
5188 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005189 if (mddev->dev_sectors &&
5190 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005191 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005192 pr_warn("md: %s: data overlaps metadata\n",
5193 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005194 return -EINVAL;
5195 }
5196 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005197 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005198 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005199 pr_warn("md: %s: metadata overlaps data\n",
5200 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005201 return -EINVAL;
5202 }
5203 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005204 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 }
5206
NeilBrowna167f662010-10-26 18:31:13 +11005207 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005208 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005209
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005211 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005212 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005214 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005215 pr_warn("md: personality for level %d is not loaded!\n",
5216 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005217 else
NeilBrown9d487392016-11-02 14:16:49 +11005218 pr_warn("md: personality for level %s is not loaded!\n",
5219 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 return -EINVAL;
5221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005223 if (mddev->level != pers->level) {
5224 mddev->level = pers->level;
5225 mddev->new_level = pers->level;
5226 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005227 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228
NeilBrownf6705572006-03-27 01:18:11 -08005229 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005230 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005231 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005232 module_put(pers->owner);
5233 return -EINVAL;
5234 }
5235
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005236 if (pers->sync_request) {
5237 /* Warn if this is a potentially silly
5238 * configuration.
5239 */
5240 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005241 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005242 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005243
NeilBrowndafb20f2012-03-19 12:46:39 +11005244 rdev_for_each(rdev, mddev)
5245 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005246 if (rdev < rdev2 &&
5247 rdev->bdev->bd_contains ==
5248 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005249 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5250 mdname(mddev),
5251 bdevname(rdev->bdev,b),
5252 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005253 warned = 1;
5254 }
5255 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005256
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005257 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005258 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005259 }
5260
NeilBrown657390d2005-08-26 18:34:16 -07005261 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005262 /* may be over-ridden by personality */
5263 mddev->resync_max_sectors = mddev->dev_sectors;
5264
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005265 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266
NeilBrown0f9552b52009-12-30 12:08:50 +11005267 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005268 mddev->ro = 2; /* read-only, but switch on first write */
5269
NeilBrown36d091f2014-12-15 12:56:58 +11005270 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005271 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005272 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005273 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005274 WARN_ONCE(!mddev->external_size,
5275 "%s: default size too small, but 'external_size' not in effect?\n",
5276 __func__);
5277 pr_warn("md: invalid array_size %llu > default size %llu\n",
5278 (unsigned long long)mddev->array_sectors / 2,
5279 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005280 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005281 }
NeilBrown36d091f2014-12-15 12:56:58 +11005282 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005283 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005284 struct bitmap *bitmap;
5285
5286 bitmap = bitmap_create(mddev, -1);
5287 if (IS_ERR(bitmap)) {
5288 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005289 pr_warn("%s: failed to create bitmap (%d)\n",
5290 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005291 } else
5292 mddev->bitmap = bitmap;
5293
NeilBrownb15c2e52006-01-06 00:20:16 -08005294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005296 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005297 if (mddev->private)
5298 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005299 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005300 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005301 bitmap_destroy(mddev);
5302 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 }
NeilBrown5c675f82014-12-15 12:56:56 +11005304 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005305 bool nonrot = true;
5306
5307 rdev_for_each(rdev, mddev) {
5308 if (rdev->raid_disk >= 0 &&
5309 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5310 nonrot = false;
5311 break;
5312 }
5313 }
5314 if (mddev->degraded)
5315 nonrot = false;
5316 if (nonrot)
5317 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5318 else
5319 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
NeilBrown5c675f82014-12-15 12:56:56 +11005320 mddev->queue->backing_dev_info.congested_data = mddev;
5321 mddev->queue->backing_dev_info.congested_fn = md_congested;
5322 }
NeilBrown36d091f2014-12-15 12:56:58 +11005323 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005324 if (mddev->kobj.sd &&
5325 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005326 pr_warn("md: cannot register extra attributes for %s\n",
5327 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005328 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005329 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005330 mddev->ro = 0;
5331
NeilBrownf72ffdd2014-09-30 14:23:59 +10005332 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005333 atomic_set(&mddev->max_corr_read_errors,
5334 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005336 if (mddev_is_clustered(mddev))
5337 mddev->safemode_delay = 0;
5338 else
5339 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005341 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005342 spin_lock(&mddev->lock);
5343 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005344 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005345 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005346 if (rdev->raid_disk >= 0)
5347 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005348 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005349
NeilBrowna4a3d262015-07-17 11:57:30 +10005350 if (mddev->degraded && !mddev->ro)
5351 /* This ensures that recovering status is reported immediately
5352 * via sysfs - until a lack of spares is confirmed.
5353 */
5354 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005356
NeilBrown7a0a5352013-08-27 16:28:23 +10005357 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07005358 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
NeilBrownd7603b72006-01-06 00:20:30 -08005360 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005361 sysfs_notify_dirent_safe(mddev->sysfs_state);
5362 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005363 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 return 0;
5365}
NeilBrown390ee602010-06-01 19:37:27 +10005366EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367
NeilBrownfd01b882011-10-11 16:47:53 +11005368static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005369{
5370 int err;
5371
5372 err = md_run(mddev);
5373 if (err)
5374 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005375 err = bitmap_load(mddev);
5376 if (err) {
5377 bitmap_destroy(mddev);
5378 goto out;
5379 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005380
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005381 if (mddev_is_clustered(mddev))
5382 md_allow_write(mddev);
5383
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005384 md_wakeup_thread(mddev->thread);
5385 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5386
NeilBrownfe60b012010-03-29 11:10:42 +11005387 set_capacity(mddev->gendisk, mddev->array_sectors);
5388 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005389 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005390 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5391out:
5392 return err;
5393}
5394
NeilBrownfd01b882011-10-11 16:47:53 +11005395static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396{
5397 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
Andre Noll80fab1d2008-07-11 22:02:21 +10005399 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005401 return -ENXIO;
5402 if (!mddev->pers)
5403 return -EINVAL;
5404 if (!mddev->ro)
5405 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005406 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5407 struct md_rdev *rdev;
5408 bool has_journal = false;
5409
5410 rcu_read_lock();
5411 rdev_for_each_rcu(rdev, mddev) {
5412 if (test_bit(Journal, &rdev->flags) &&
5413 !test_bit(Faulty, &rdev->flags)) {
5414 has_journal = true;
5415 break;
5416 }
5417 }
5418 rcu_read_unlock();
5419
5420 /* Don't restart rw with journal missing/faulty */
5421 if (!has_journal)
5422 return -EINVAL;
5423 }
5424
Andre Noll80fab1d2008-07-11 22:02:21 +10005425 mddev->safemode = 0;
5426 mddev->ro = 0;
5427 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11005428 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10005429 /* Kick recovery or resync if necessary */
5430 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5431 md_wakeup_thread(mddev->thread);
5432 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005433 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435}
5436
NeilBrownfd01b882011-10-11 16:47:53 +11005437static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005438{
5439 mddev->array_sectors = 0;
5440 mddev->external_size = 0;
5441 mddev->dev_sectors = 0;
5442 mddev->raid_disks = 0;
5443 mddev->recovery_cp = 0;
5444 mddev->resync_min = 0;
5445 mddev->resync_max = MaxSector;
5446 mddev->reshape_position = MaxSector;
5447 mddev->external = 0;
5448 mddev->persistent = 0;
5449 mddev->level = LEVEL_NONE;
5450 mddev->clevel[0] = 0;
5451 mddev->flags = 0;
5452 mddev->ro = 0;
5453 mddev->metadata_type[0] = 0;
5454 mddev->chunk_sectors = 0;
5455 mddev->ctime = mddev->utime = 0;
5456 mddev->layout = 0;
5457 mddev->max_disks = 0;
5458 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005459 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005460 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005461 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005462 mddev->new_level = LEVEL_NONE;
5463 mddev->new_layout = 0;
5464 mddev->new_chunk_sectors = 0;
5465 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005466 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005467 mddev->suspend_lo = mddev->suspend_hi = 0;
5468 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5469 mddev->recovery = 0;
5470 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005471 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005472 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005473 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005474 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005475 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005476 mddev->bitmap_info.offset = 0;
5477 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005478 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005479 mddev->bitmap_info.chunksize = 0;
5480 mddev->bitmap_info.daemon_sleep = 0;
5481 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005482 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005483}
5484
NeilBrownfd01b882011-10-11 16:47:53 +11005485static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005486{
NeilBrown6b6204e2013-05-09 09:48:30 +10005487 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005488 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005489 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005490 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005491 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005492 }
5493
5494 del_timer_sync(&mddev->safemode_timer);
5495
5496 bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005497
NeilBrownb6d428c2013-04-24 11:42:42 +10005498 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005499 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5500 (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005501 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005502 if (!mddev_is_clustered(mddev))
5503 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005504 md_update_sb(mddev, 1);
5505 }
5506}
NeilBrowndefad612011-01-14 09:14:33 +11005507
NeilBrownfd01b882011-10-11 16:47:53 +11005508void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005509{
NeilBrown29f097c2013-11-14 17:54:51 +11005510 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005511 __md_stop_writes(mddev);
5512 mddev_unlock(mddev);
5513}
NeilBrown390ee602010-06-01 19:37:27 +10005514EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005515
NeilBrown5aa61f42014-12-15 12:56:57 +11005516static void mddev_detach(struct mddev *mddev)
5517{
5518 struct bitmap *bitmap = mddev->bitmap;
5519 /* wait for behind writes to complete */
5520 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005521 pr_debug("md:%s: behind writes in progress - waiting to stop.\n",
5522 mdname(mddev));
NeilBrown5aa61f42014-12-15 12:56:57 +11005523 /* need to kick something here to make sure I/O goes? */
5524 wait_event(bitmap->behind_wait,
5525 atomic_read(&bitmap->behind_writes) == 0);
5526 }
NeilBrown36d091f2014-12-15 12:56:58 +11005527 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005528 mddev->pers->quiesce(mddev, 1);
5529 mddev->pers->quiesce(mddev, 0);
5530 }
5531 md_unregister_thread(&mddev->thread);
5532 if (mddev->queue)
5533 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5534}
5535
NeilBrown5eff3c42012-11-19 10:47:48 +11005536static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005537{
NeilBrown36d091f2014-12-15 12:56:58 +11005538 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005539 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005540 /* Ensure ->event_work is done */
5541 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005542 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005543 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005544 spin_unlock(&mddev->lock);
5545 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005546 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005547 if (pers->sync_request && mddev->to_remove == NULL)
5548 mddev->to_remove = &md_redundancy_group;
5549 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005550 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005551}
NeilBrown5eff3c42012-11-19 10:47:48 +11005552
5553void md_stop(struct mddev *mddev)
5554{
5555 /* stop the array and free an attached data structures.
5556 * This is called from dm-raid
5557 */
5558 __md_stop(mddev);
5559 bitmap_destroy(mddev);
5560 if (mddev->bio_set)
5561 bioset_free(mddev->bio_set);
5562}
5563
NeilBrown390ee602010-06-01 19:37:27 +10005564EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005565
NeilBrowna05b7ea2012-07-19 15:59:18 +10005566static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005567{
5568 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005569 int did_freeze = 0;
5570
5571 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5572 did_freeze = 1;
5573 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5574 md_wakeup_thread(mddev->thread);
5575 }
NeilBrownf851b602014-12-11 10:02:10 +11005576 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005577 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005578 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005579 /* Thread might be blocked waiting for metadata update
5580 * which will now never happen */
5581 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005582
NeilBrown88724bf2015-09-24 14:00:51 +10005583 if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags))
5584 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005585 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005586 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5587 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005588 wait_event(mddev->sb_wait,
5589 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005590 mddev_lock_nointr(mddev);
5591
NeilBrowna4bd82d2010-03-29 13:23:10 +11005592 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005593 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005594 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005595 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005596 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005597 if (did_freeze) {
5598 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005599 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005600 md_wakeup_thread(mddev->thread);
5601 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005602 err = -EBUSY;
5603 goto out;
5604 }
5605 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005606 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005607
5608 err = -ENXIO;
5609 if (mddev->ro==1)
5610 goto out;
5611 mddev->ro = 1;
5612 set_disk_ro(mddev->gendisk, 1);
5613 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005614 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5615 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005616 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005617 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005618 }
5619out:
5620 mutex_unlock(&mddev->open_mutex);
5621 return err;
5622}
5623
NeilBrown9e653b62006-06-26 00:27:58 -07005624/* mode:
5625 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005626 * 2 - stop but do not disassemble array
5627 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005628static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005629 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005632 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005633 int did_freeze = 0;
5634
5635 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5636 did_freeze = 1;
5637 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5638 md_wakeup_thread(mddev->thread);
5639 }
NeilBrownf851b602014-12-11 10:02:10 +11005640 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005641 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005642 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005643 /* Thread might be blocked waiting for metadata update
5644 * which will now never happen */
5645 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005646
NeilBrown30b8feb2013-11-14 15:16:17 +11005647 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005648 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5649 !test_bit(MD_RECOVERY_RUNNING,
5650 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005651 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
NeilBrownc8c00a62009-08-10 12:50:52 +10005653 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005654 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005655 mddev->sysfs_active ||
5656 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005657 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005658 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005659 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005660 if (did_freeze) {
5661 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005662 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005663 md_wakeup_thread(mddev->thread);
5664 }
NeilBrown260fa032013-08-27 16:44:13 +10005665 return -EBUSY;
5666 }
NeilBrown6e17b022010-08-07 21:41:19 +10005667 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005668 if (mddev->ro)
5669 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005670
NeilBrowndefad612011-01-14 09:14:33 +11005671 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005672 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005673 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005674
NeilBrowna4bd82d2010-03-29 13:23:10 +11005675 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005676 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005677
NeilBrowndafb20f2012-03-19 12:46:39 +11005678 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005679 if (rdev->raid_disk >= 0)
5680 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005681
NeilBrowna4bd82d2010-03-29 13:23:10 +11005682 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005683 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005684 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005685 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005686
NeilBrowna4bd82d2010-03-29 13:23:10 +11005687 if (mddev->ro)
5688 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005689 } else
5690 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 /*
5692 * Free resources if final stop
5693 */
NeilBrown9e653b62006-06-26 00:27:58 -07005694 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005695 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
NeilBrown978f9462006-02-02 14:28:05 -08005697 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005698 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005699 struct file *f = mddev->bitmap_info.file;
5700 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005701 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005702 spin_unlock(&mddev->lock);
5703 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005704 }
NeilBrownc3d97142009-12-14 12:49:52 +11005705 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005706
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 export_array(mddev);
5708
NeilBrown6177b472010-03-29 11:37:13 +11005709 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11005710 if (mddev->hold_active == UNTIL_STOP)
5711 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005712 }
NeilBrownd7603b72006-01-06 00:20:30 -08005713 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005714 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716}
5717
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005718#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005719static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720{
NeilBrown3cb03002011-10-11 16:45:26 +11005721 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 int err;
5723
NeilBrowna757e642005-04-16 15:26:42 -07005724 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726
NeilBrown9d487392016-11-02 14:16:49 +11005727 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
NeilBrowndafb20f2012-03-19 12:46:39 +11005729 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11005731 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 }
NeilBrown9d487392016-11-02 14:16:49 +11005733 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
NeilBrownd710e132008-10-13 11:55:12 +11005735 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11005737 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005738 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 }
5740}
5741
5742/*
5743 * lets try to run arrays based on all disks that have arrived
5744 * until now. (those are in pending_raid_disks)
5745 *
5746 * the method: pick the first pending disk, collect all disks with
5747 * the same UUID, remove all from the pending list and put them into
5748 * the 'same_array' list. Then order this list based on superblock
5749 * update time (freshest comes first), kick out 'old' disks and
5750 * compare superblocks. If everything's fine then run it.
5751 *
5752 * If "unit" is allocated, then bump its reference count
5753 */
5754static void autorun_devices(int part)
5755{
NeilBrown3cb03002011-10-11 16:45:26 +11005756 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005757 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 char b[BDEVNAME_SIZE];
5759
NeilBrown9d487392016-11-02 14:16:49 +11005760 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005762 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005764 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005766 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767
NeilBrown9d487392016-11-02 14:16:49 +11005768 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005770 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005772 pr_debug("md: adding %s ...\n",
5773 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 list_move(&rdev->same_set, &candidates);
5775 }
5776 /*
5777 * now we have a set of devices, with all of them having
5778 * mostly sane superblocks. It's time to allocate the
5779 * mddev.
5780 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005781 if (part) {
5782 dev = MKDEV(mdp_major,
5783 rdev0->preferred_minor << MdpMinorShift);
5784 unit = MINOR(dev) >> MdpMinorShift;
5785 } else {
5786 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5787 unit = MINOR(dev);
5788 }
5789 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11005790 pr_warn("md: unit number in %s is bad: %d\n",
5791 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 break;
5793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794
5795 md_probe(dev, NULL, NULL);
5796 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005797 if (!mddev || !mddev->gendisk) {
5798 if (mddev)
5799 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 break;
5801 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005802 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11005803 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 else if (mddev->raid_disks || mddev->major_version
5805 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11005806 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 mdname(mddev), bdevname(rdev0->bdev,b));
5808 mddev_unlock(mddev);
5809 } else {
NeilBrown9d487392016-11-02 14:16:49 +11005810 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005811 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005812 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 list_del_init(&rdev->same_set);
5814 if (bind_rdev_to_array(rdev, mddev))
5815 export_rdev(rdev);
5816 }
5817 autorun_array(mddev);
5818 mddev_unlock(mddev);
5819 }
5820 /* on success, candidates will be empty, on error
5821 * it won't...
5822 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005823 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005824 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 mddev_put(mddev);
5828 }
NeilBrown9d487392016-11-02 14:16:49 +11005829 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005831#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
NeilBrownf72ffdd2014-09-30 14:23:59 +10005833static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834{
5835 mdu_version_t ver;
5836
5837 ver.major = MD_MAJOR_VERSION;
5838 ver.minor = MD_MINOR_VERSION;
5839 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5840
5841 if (copy_to_user(arg, &ver, sizeof(ver)))
5842 return -EFAULT;
5843
5844 return 0;
5845}
5846
NeilBrownf72ffdd2014-09-30 14:23:59 +10005847static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848{
5849 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005850 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005851 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852
NeilBrown1ca69c42012-10-11 13:37:33 +11005853 nr = working = insync = failed = spare = 0;
5854 rcu_read_lock();
5855 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005857 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 failed++;
5859 else {
5860 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005861 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005862 insync++;
Song Liub347af82016-08-11 17:14:45 -07005863 else if (test_bit(Journal, &rdev->flags))
5864 /* TODO: add journal count to md_u.h */
5865 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 else
5867 spare++;
5868 }
5869 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005870 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871
5872 info.major_version = mddev->major_version;
5873 info.minor_version = mddev->minor_version;
5874 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005875 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005877 info.size = mddev->dev_sectors / 2;
5878 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005879 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 info.nr_disks = nr;
5881 info.raid_disks = mddev->raid_disks;
5882 info.md_minor = mddev->md_minor;
5883 info.not_persistent= !mddev->persistent;
5884
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005885 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 info.state = 0;
5887 if (mddev->in_sync)
5888 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005889 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005890 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005891 if (mddev_is_clustered(mddev))
5892 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005893 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 info.working_disks = working;
5895 info.failed_disks = failed;
5896 info.spare_disks = spare;
5897
5898 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005899 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
5901 if (copy_to_user(arg, &info, sizeof(info)))
5902 return -EFAULT;
5903
5904 return 0;
5905}
5906
NeilBrownf72ffdd2014-09-30 14:23:59 +10005907static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005908{
5909 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005910 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005911 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005912
Benjamin Randazzob6878d92015-07-25 16:36:50 +02005913 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005914 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005915 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005916
NeilBrown32a76272005-06-21 17:17:14 -07005917 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005918 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02005919 /* bitmap enabled */
5920 if (mddev->bitmap_info.file) {
5921 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5922 sizeof(file->pathname));
5923 if (IS_ERR(ptr))
5924 err = PTR_ERR(ptr);
5925 else
5926 memmove(file->pathname, ptr,
5927 sizeof(file->pathname)-(ptr-file->pathname));
5928 }
NeilBrown4af1a042014-12-15 12:57:00 +11005929 spin_unlock(&mddev->lock);
5930
5931 if (err == 0 &&
5932 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005933 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005934
NeilBrown32a76272005-06-21 17:17:14 -07005935 kfree(file);
5936 return err;
5937}
5938
NeilBrownf72ffdd2014-09-30 14:23:59 +10005939static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940{
5941 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005942 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943
5944 if (copy_from_user(&info, arg, sizeof(info)))
5945 return -EFAULT;
5946
NeilBrown1ca69c42012-10-11 13:37:33 +11005947 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005948 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 if (rdev) {
5950 info.major = MAJOR(rdev->bdev->bd_dev);
5951 info.minor = MINOR(rdev->bdev->bd_dev);
5952 info.raid_disk = rdev->raid_disk;
5953 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005954 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005956 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 info.state |= (1<<MD_DISK_ACTIVE);
5958 info.state |= (1<<MD_DISK_SYNC);
5959 }
Shaohua Li9efdca12015-10-12 16:59:50 -07005960 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07005961 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005962 if (test_bit(WriteMostly, &rdev->flags))
5963 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11005964 if (test_bit(FailFast, &rdev->flags))
5965 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 } else {
5967 info.major = info.minor = 0;
5968 info.raid_disk = -1;
5969 info.state = (1<<MD_DISK_REMOVED);
5970 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005971 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972
5973 if (copy_to_user(arg, &info, sizeof(info)))
5974 return -EFAULT;
5975
5976 return 0;
5977}
5978
NeilBrownf72ffdd2014-09-30 14:23:59 +10005979static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980{
5981 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005982 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 dev_t dev = MKDEV(info->major,info->minor);
5984
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005985 if (mddev_is_clustered(mddev) &&
5986 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11005987 pr_warn("%s: Cannot add to clustered mddev.\n",
5988 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005989 return -EINVAL;
5990 }
5991
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5993 return -EOVERFLOW;
5994
5995 if (!mddev->raid_disks) {
5996 int err;
5997 /* expecting a device which has a superblock */
5998 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5999 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006000 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 PTR_ERR(rdev));
6002 return PTR_ERR(rdev);
6003 }
6004 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006005 struct md_rdev *rdev0
6006 = list_entry(mddev->disks.next,
6007 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006008 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 .load_super(rdev, rdev0, mddev->minor_version);
6010 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006011 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006012 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 bdevname(rdev0->bdev,b2));
6014 export_rdev(rdev);
6015 return -EINVAL;
6016 }
6017 }
6018 err = bind_rdev_to_array(rdev, mddev);
6019 if (err)
6020 export_rdev(rdev);
6021 return err;
6022 }
6023
6024 /*
6025 * add_new_disk can be used once the array is assembled
6026 * to add "hot spares". They must already have a superblock
6027 * written
6028 */
6029 if (mddev->pers) {
6030 int err;
6031 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006032 pr_warn("%s: personality does not support diskops!\n",
6033 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 return -EINVAL;
6035 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006036 if (mddev->persistent)
6037 rdev = md_import_device(dev, mddev->major_version,
6038 mddev->minor_version);
6039 else
6040 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006042 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 PTR_ERR(rdev));
6044 return PTR_ERR(rdev);
6045 }
NeilBrown1a855a02010-12-09 16:36:28 +11006046 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006047 if (!mddev->persistent) {
6048 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006049 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006050 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006051 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006052 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006053 } else
NeilBrown41158c72005-06-21 17:17:25 -07006054 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006055 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006056 } else
6057 super_types[mddev->major_version].
6058 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006059 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006060 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006061 /* This was a hot-add request, but events doesn't
6062 * match, so reject it.
6063 */
6064 export_rdev(rdev);
6065 return -EINVAL;
6066 }
6067
NeilBrownb2d444d2005-11-08 21:39:31 -08006068 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006069 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6070 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006071 else
6072 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006073 if (info->state & (1<<MD_DISK_FAILFAST))
6074 set_bit(FailFast, &rdev->flags);
6075 else
6076 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006077
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006078 if (info->state & (1<<MD_DISK_JOURNAL)) {
6079 struct md_rdev *rdev2;
6080 bool has_journal = false;
6081
6082 /* make sure no existing journal disk */
6083 rdev_for_each(rdev2, mddev) {
6084 if (test_bit(Journal, &rdev2->flags)) {
6085 has_journal = true;
6086 break;
6087 }
6088 }
6089 if (has_journal) {
6090 export_rdev(rdev);
6091 return -EBUSY;
6092 }
Song Liubac624f2015-08-13 14:31:55 -07006093 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006094 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006095 /*
6096 * check whether the device shows up in other nodes
6097 */
6098 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006099 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006100 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006101 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006102 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006103 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006104 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006105 export_rdev(rdev);
6106 return err;
6107 }
6108 }
6109 }
6110
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 rdev->raid_disk = -1;
6112 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006113
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 if (err)
6115 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006116
6117 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006118 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6119 if (!err) {
6120 err = md_cluster_ops->new_disk_ack(mddev,
6121 err == 0);
6122 if (err)
6123 md_kick_rdev_from_array(rdev);
6124 }
6125 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006126 if (err)
6127 md_cluster_ops->add_new_disk_cancel(mddev);
6128 else
6129 err = add_bound_rdev(rdev);
6130 }
6131
6132 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006133 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006134
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 return err;
6136 }
6137
6138 /* otherwise, add_new_disk is only allowed
6139 * for major_version==0 superblocks
6140 */
6141 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006142 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 return -EINVAL;
6144 }
6145
6146 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6147 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006148 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006150 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 PTR_ERR(rdev));
6152 return PTR_ERR(rdev);
6153 }
6154 rdev->desc_nr = info->number;
6155 if (info->raid_disk < mddev->raid_disks)
6156 rdev->raid_disk = info->raid_disk;
6157 else
6158 rdev->raid_disk = -1;
6159
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006161 if (info->state & (1<<MD_DISK_SYNC))
6162 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006164 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6165 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006166 if (info->state & (1<<MD_DISK_FAILFAST))
6167 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006168
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006170 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006171 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6172 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006173 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006174 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175
NeilBrown2bf071b2006-01-06 00:20:55 -08006176 err = bind_rdev_to_array(rdev, mddev);
6177 if (err) {
6178 export_rdev(rdev);
6179 return err;
6180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 }
6182
6183 return 0;
6184}
6185
NeilBrownf72ffdd2014-09-30 14:23:59 +10006186static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187{
6188 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006189 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 rdev = find_rdev(mddev, dev);
6192 if (!rdev)
6193 return -ENXIO;
6194
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006195 if (rdev->raid_disk < 0)
6196 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006197
NeilBrown3ea8929d2013-04-24 11:42:41 +10006198 clear_bit(Blocked, &rdev->flags);
6199 remove_and_add_spares(mddev, rdev);
6200
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 if (rdev->raid_disk >= 0)
6202 goto busy;
6203
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006204kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006205 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006206 md_cluster_ops->remove_disk(mddev, rdev);
6207
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006208 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11006209 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6210 if (mddev->thread)
6211 md_wakeup_thread(mddev->thread);
6212 else
6213 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006214 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215
6216 return 0;
6217busy:
NeilBrown9d487392016-11-02 14:16:49 +11006218 pr_debug("md: cannot remove active disk %s from %s ...\n",
6219 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 return -EBUSY;
6221}
6222
NeilBrownf72ffdd2014-09-30 14:23:59 +10006223static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224{
6225 char b[BDEVNAME_SIZE];
6226 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006227 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228
6229 if (!mddev->pers)
6230 return -ENODEV;
6231
6232 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006233 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 mdname(mddev));
6235 return -EINVAL;
6236 }
6237 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006238 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 mdname(mddev));
6240 return -EINVAL;
6241 }
6242
NeilBrownd710e132008-10-13 11:55:12 +11006243 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006245 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 PTR_ERR(rdev));
6247 return -EINVAL;
6248 }
6249
6250 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006251 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006253 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254
NeilBrown8190e752009-06-18 08:48:58 +10006255 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256
NeilBrownb2d444d2005-11-08 21:39:31 -08006257 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006258 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 bdevname(rdev->bdev,b), mdname(mddev));
6260 err = -EINVAL;
6261 goto abort_export;
6262 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006263
NeilBrownb2d444d2005-11-08 21:39:31 -08006264 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006266 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006267 err = bind_rdev_to_array(rdev, mddev);
6268 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006269 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270
6271 /*
6272 * The rest should better be atomic, we can have disk failures
6273 * noticed in interrupt contexts ...
6274 */
6275
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 rdev->raid_disk = -1;
6277
NeilBrown060b0682016-11-04 16:46:03 +11006278 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6279 if (!mddev->thread)
6280 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 /*
6282 * Kick recovery, maybe this spare has to be added to the
6283 * array immediately.
6284 */
6285 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6286 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006287 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 return 0;
6289
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290abort_export:
6291 export_rdev(rdev);
6292 return err;
6293}
6294
NeilBrownfd01b882011-10-11 16:47:53 +11006295static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006296{
NeilBrown035328c2014-04-09 12:25:40 +10006297 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006298
NeilBrown36fa3062005-09-09 16:23:45 -07006299 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006300 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006301 return -EBUSY;
6302 if (mddev->recovery || mddev->sync_thread)
6303 return -EBUSY;
6304 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006305 }
6306
NeilBrown36fa3062005-09-09 16:23:45 -07006307 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006308 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006309 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006310
NeilBrown1e594bb2014-12-15 12:57:00 +11006311 if (mddev->bitmap || mddev->bitmap_info.file)
6312 return -EEXIST; /* cannot add when bitmap is present */
6313 f = fget(fd);
6314
6315 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006316 pr_warn("%s: error: failed to get bitmap file\n",
6317 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006318 return -EBADF;
6319 }
6320
NeilBrown1e594bb2014-12-15 12:57:00 +11006321 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006322 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006323 pr_warn("%s: error: bitmap file must be a regular file\n",
6324 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006325 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006326 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006327 pr_warn("%s: error: bitmap file must open for write\n",
6328 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006329 err = -EBADF;
6330 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006331 pr_warn("%s: error: bitmap file is already in use\n",
6332 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006333 err = -EBUSY;
6334 }
6335 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006336 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006337 return err;
6338 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006339 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006340 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006341 } else if (mddev->bitmap == NULL)
6342 return -ENOENT; /* cannot remove what isn't there */
6343 err = 0;
6344 if (mddev->pers) {
6345 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006346 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006347 struct bitmap *bitmap;
6348
6349 bitmap = bitmap_create(mddev, -1);
6350 if (!IS_ERR(bitmap)) {
6351 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006352 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006353 } else
6354 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006355 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006356 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006357 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006358 fd = -1; /* make sure to put the file */
6359 }
NeilBrown36fa3062005-09-09 16:23:45 -07006360 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006361 }
6362 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006363 struct file *f = mddev->bitmap_info.file;
6364 if (f) {
6365 spin_lock(&mddev->lock);
6366 mddev->bitmap_info.file = NULL;
6367 spin_unlock(&mddev->lock);
6368 fput(f);
6369 }
NeilBrown36fa3062005-09-09 16:23:45 -07006370 }
6371
NeilBrown32a76272005-06-21 17:17:14 -07006372 return err;
6373}
6374
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375/*
6376 * set_array_info is used two different ways
6377 * The original usage is when creating a new array.
6378 * In this usage, raid_disks is > 0 and it together with
6379 * level, size, not_persistent,layout,chunksize determine the
6380 * shape of the array.
6381 * This will always create an array with a type-0.90.0 superblock.
6382 * The newer usage is when assembling an array.
6383 * In this case raid_disks will be 0, and the major_version field is
6384 * use to determine which style super-blocks are to be found on the devices.
6385 * The minor and patch _version numbers are also kept incase the
6386 * super_block handler wishes to interpret them.
6387 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006388static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389{
6390
6391 if (info->raid_disks == 0) {
6392 /* just setting version number for superblock loading */
6393 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006394 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 super_types[info->major_version].name == NULL) {
6396 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11006397 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 info->major_version);
6399 return -EINVAL;
6400 }
6401 mddev->major_version = info->major_version;
6402 mddev->minor_version = info->minor_version;
6403 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006404 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006405 /* ensure mddev_put doesn't delete this now that there
6406 * is some minimal configuration.
6407 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006408 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 return 0;
6410 }
6411 mddev->major_version = MD_MAJOR_VERSION;
6412 mddev->minor_version = MD_MINOR_VERSION;
6413 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006414 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415
6416 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006417 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006418 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 mddev->raid_disks = info->raid_disks;
6420 /* don't set md_minor, it is determined by which /dev/md* was
6421 * openned
6422 */
6423 if (info->state & (1<<MD_SB_CLEAN))
6424 mddev->recovery_cp = MaxSector;
6425 else
6426 mddev->recovery_cp = 0;
6427 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006428 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429
6430 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006431 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432
6433 mddev->max_disks = MD_SB_DISKS;
6434
NeilBrowne6910632008-02-06 01:39:51 -08006435 if (mddev->persistent)
6436 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006437 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438
NeilBrownc3d97142009-12-14 12:49:52 +11006439 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006440 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006441 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006442
NeilBrownf6705572006-03-27 01:18:11 -08006443 mddev->reshape_position = MaxSector;
6444
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 /*
6446 * Generate a 128 bit UUID
6447 */
6448 get_random_bytes(mddev->uuid, 16);
6449
NeilBrownf6705572006-03-27 01:18:11 -08006450 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006451 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006452 mddev->new_layout = mddev->layout;
6453 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006454 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006455
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 return 0;
6457}
6458
NeilBrownfd01b882011-10-11 16:47:53 +11006459void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006460{
Dan Williamsb522adc2009-03-31 15:00:31 +11006461 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6462
6463 if (mddev->external_size)
6464 return;
6465
Dan Williams1f403622009-03-31 14:59:03 +11006466 mddev->array_sectors = array_sectors;
6467}
6468EXPORT_SYMBOL(md_set_array_sectors);
6469
NeilBrownfd01b882011-10-11 16:47:53 +11006470static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006471{
NeilBrown3cb03002011-10-11 16:45:26 +11006472 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006473 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006474 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006475
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006476 /* cluster raid doesn't support update size */
6477 if (mddev_is_clustered(mddev))
6478 return -EINVAL;
6479
NeilBrowna35b0d62006-01-06 00:20:49 -08006480 if (mddev->pers->resize == NULL)
6481 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006482 /* The "num_sectors" is the number of sectors of each device that
6483 * is used. This can only make sense for arrays with redundancy.
6484 * linear and raid0 always use whatever space is available. We can only
6485 * consider changing this number if no resync or reconstruction is
6486 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006487 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006488 * of each device. If num_sectors is zero, we find the largest size
6489 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006490 */
NeilBrownf851b602014-12-11 10:02:10 +11006491 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6492 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006493 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006494 if (mddev->ro)
6495 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006496
NeilBrowndafb20f2012-03-19 12:46:39 +11006497 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006498 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006499
Andre Nolld71f9f82008-07-11 22:02:22 +10006500 if (fit && (num_sectors == 0 || num_sectors > avail))
6501 num_sectors = avail;
6502 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006503 return -ENOSPC;
6504 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006505 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006506 if (!rv)
6507 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006508 return rv;
6509}
6510
NeilBrownfd01b882011-10-11 16:47:53 +11006511static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006512{
6513 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006514 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006515 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006516 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006517 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006518 if (mddev->ro)
6519 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006520 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006521 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006522 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006523 if (mddev->sync_thread ||
6524 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6525 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006526 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006527
6528 rdev_for_each(rdev, mddev) {
6529 if (mddev->raid_disks < raid_disks &&
6530 rdev->data_offset < rdev->new_data_offset)
6531 return -EINVAL;
6532 if (mddev->raid_disks > raid_disks &&
6533 rdev->data_offset > rdev->new_data_offset)
6534 return -EINVAL;
6535 }
6536
NeilBrown63c70c42006-03-27 01:18:13 -08006537 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006538 if (mddev->delta_disks < 0)
6539 mddev->reshape_backwards = 1;
6540 else if (mddev->delta_disks > 0)
6541 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006542
6543 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006544 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006545 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006546 mddev->reshape_backwards = 0;
6547 }
NeilBrownda943b992006-01-06 00:20:54 -08006548 return rv;
6549}
6550
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551/*
6552 * update_array_info is used to change the configuration of an
6553 * on-line array.
6554 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6555 * fields in the info are checked against the array.
6556 * Any differences that cannot be handled will cause an error.
6557 * Normally, only one change can be managed at a time.
6558 */
NeilBrownfd01b882011-10-11 16:47:53 +11006559static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560{
6561 int rv = 0;
6562 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006563 int state = 0;
6564
6565 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006566 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006567 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568
6569 if (mddev->major_version != info->major_version ||
6570 mddev->minor_version != info->minor_version ||
6571/* mddev->patch_version != info->patch_version || */
6572 mddev->ctime != info->ctime ||
6573 mddev->level != info->level ||
6574/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006575 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006576 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006577 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6578 ((state^info->state) & 0xfffffe00)
6579 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 return -EINVAL;
6581 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006582 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6583 cnt++;
6584 if (mddev->raid_disks != info->raid_disks)
6585 cnt++;
6586 if (mddev->layout != info->layout)
6587 cnt++;
6588 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6589 cnt++;
6590 if (cnt == 0)
6591 return 0;
6592 if (cnt > 1)
6593 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594
6595 if (mddev->layout != info->layout) {
6596 /* Change layout
6597 * we don't need to do anything at the md level, the
6598 * personality will take care of it all.
6599 */
NeilBrown50ac1682009-06-18 08:47:55 +10006600 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006602 else {
6603 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006604 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006605 if (rv)
6606 mddev->new_layout = mddev->layout;
6607 return rv;
6608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006610 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006611 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612
NeilBrownda943b992006-01-06 00:20:54 -08006613 if (mddev->raid_disks != info->raid_disks)
6614 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615
NeilBrown36fa3062005-09-09 16:23:45 -07006616 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006617 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6618 rv = -EINVAL;
6619 goto err;
6620 }
6621 if (mddev->recovery || mddev->sync_thread) {
6622 rv = -EBUSY;
6623 goto err;
6624 }
NeilBrown36fa3062005-09-09 16:23:45 -07006625 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006626 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006627 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006628 if (mddev->bitmap) {
6629 rv = -EEXIST;
6630 goto err;
6631 }
6632 if (mddev->bitmap_info.default_offset == 0) {
6633 rv = -EINVAL;
6634 goto err;
6635 }
NeilBrownc3d97142009-12-14 12:49:52 +11006636 mddev->bitmap_info.offset =
6637 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006638 mddev->bitmap_info.space =
6639 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006640 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006641 bitmap = bitmap_create(mddev, -1);
6642 if (!IS_ERR(bitmap)) {
6643 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006644 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006645 } else
6646 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006647 if (rv)
6648 bitmap_destroy(mddev);
6649 mddev->pers->quiesce(mddev, 0);
6650 } else {
6651 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006652 if (!mddev->bitmap) {
6653 rv = -ENOENT;
6654 goto err;
6655 }
6656 if (mddev->bitmap->storage.file) {
6657 rv = -EINVAL;
6658 goto err;
6659 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006660 if (mddev->bitmap_info.nodes) {
6661 /* hold PW on all the bitmap lock */
6662 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006663 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 +11006664 rv = -EPERM;
6665 md_cluster_ops->unlock_all_bitmaps(mddev);
6666 goto err;
6667 }
6668
6669 mddev->bitmap_info.nodes = 0;
6670 md_cluster_ops->leave(mddev);
6671 }
NeilBrown36fa3062005-09-09 16:23:45 -07006672 mddev->pers->quiesce(mddev, 1);
6673 bitmap_destroy(mddev);
6674 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006675 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006676 }
6677 }
NeilBrown850b2b42006-10-03 01:15:46 -07006678 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006679 return rv;
6680err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681 return rv;
6682}
6683
NeilBrownfd01b882011-10-11 16:47:53 +11006684static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685{
NeilBrown3cb03002011-10-11 16:45:26 +11006686 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006687 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688
6689 if (mddev->pers == NULL)
6690 return -ENODEV;
6691
NeilBrown1ca69c42012-10-11 13:37:33 +11006692 rcu_read_lock();
6693 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006695 err = -ENODEV;
6696 else {
6697 md_error(mddev, rdev);
6698 if (!test_bit(Faulty, &rdev->flags))
6699 err = -EBUSY;
6700 }
6701 rcu_read_unlock();
6702 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703}
6704
Andre Noll2f9618c2008-04-25 18:57:58 +02006705/*
6706 * We have a problem here : there is no easy way to give a CHS
6707 * virtual geometry. We currently pretend that we have a 2 heads
6708 * 4 sectors (with a BIG number of cylinders...). This drives
6709 * dosfs just mad... ;-)
6710 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006711static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6712{
NeilBrownfd01b882011-10-11 16:47:53 +11006713 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006714
6715 geo->heads = 2;
6716 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006717 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006718 return 0;
6719}
6720
Nicolas Schichancb335f82014-01-15 16:58:52 +01006721static inline bool md_ioctl_valid(unsigned int cmd)
6722{
6723 switch (cmd) {
6724 case ADD_NEW_DISK:
6725 case BLKROSET:
6726 case GET_ARRAY_INFO:
6727 case GET_BITMAP_FILE:
6728 case GET_DISK_INFO:
6729 case HOT_ADD_DISK:
6730 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006731 case RAID_AUTORUN:
6732 case RAID_VERSION:
6733 case RESTART_ARRAY_RW:
6734 case RUN_ARRAY:
6735 case SET_ARRAY_INFO:
6736 case SET_BITMAP_FILE:
6737 case SET_DISK_FAULTY:
6738 case STOP_ARRAY:
6739 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006740 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006741 return true;
6742 default:
6743 return false;
6744 }
6745}
6746
Al Viroa39907f2008-03-02 10:31:15 -05006747static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748 unsigned int cmd, unsigned long arg)
6749{
6750 int err = 0;
6751 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006752 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006753 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754
Nicolas Schichancb335f82014-01-15 16:58:52 +01006755 if (!md_ioctl_valid(cmd))
6756 return -ENOTTY;
6757
NeilBrown506c9e42011-12-23 10:17:26 +11006758 switch (cmd) {
6759 case RAID_VERSION:
6760 case GET_ARRAY_INFO:
6761 case GET_DISK_INFO:
6762 break;
6763 default:
6764 if (!capable(CAP_SYS_ADMIN))
6765 return -EACCES;
6766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767
6768 /*
6769 * Commands dealing with the RAID driver but not any
6770 * particular array:
6771 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006772 switch (cmd) {
6773 case RAID_VERSION:
6774 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006775 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006778 case RAID_AUTORUN:
6779 err = 0;
6780 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006781 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006783 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 }
6785
6786 /*
6787 * Commands creating/starting a new array:
6788 */
6789
Al Viroa39907f2008-03-02 10:31:15 -05006790 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791
6792 if (!mddev) {
6793 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006794 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 }
6796
NeilBrown1ca69c42012-10-11 13:37:33 +11006797 /* Some actions do not requires the mutex */
6798 switch (cmd) {
6799 case GET_ARRAY_INFO:
6800 if (!mddev->raid_disks && !mddev->external)
6801 err = -ENODEV;
6802 else
6803 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006804 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006805
6806 case GET_DISK_INFO:
6807 if (!mddev->raid_disks && !mddev->external)
6808 err = -ENODEV;
6809 else
6810 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006811 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006812
6813 case SET_DISK_FAULTY:
6814 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006815 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006816
6817 case GET_BITMAP_FILE:
6818 err = get_bitmap_file(mddev, argp);
6819 goto out;
6820
NeilBrown1ca69c42012-10-11 13:37:33 +11006821 }
6822
NeilBrowna7a3f082012-12-11 13:35:54 +11006823 if (cmd == ADD_NEW_DISK)
6824 /* need to ensure md_delayed_delete() has completed */
6825 flush_workqueue(md_misc_wq);
6826
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006827 if (cmd == HOT_REMOVE_DISK)
6828 /* need to ensure recovery thread has run */
6829 wait_event_interruptible_timeout(mddev->sb_wait,
6830 !test_bit(MD_RECOVERY_NEEDED,
6831 &mddev->flags),
6832 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006833 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6834 /* Need to flush page cache, and ensure no-one else opens
6835 * and writes
6836 */
6837 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006838 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006839 mutex_unlock(&mddev->open_mutex);
6840 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006841 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006842 }
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006843 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown260fa032013-08-27 16:44:13 +10006844 mutex_unlock(&mddev->open_mutex);
6845 sync_blockdev(bdev);
6846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 err = mddev_lock(mddev);
6848 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006849 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
6850 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006851 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 }
6853
NeilBrownc02c0ae2012-12-11 13:39:21 +11006854 if (cmd == SET_ARRAY_INFO) {
6855 mdu_array_info_t info;
6856 if (!arg)
6857 memset(&info, 0, sizeof(info));
6858 else if (copy_from_user(&info, argp, sizeof(info))) {
6859 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006860 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006861 }
6862 if (mddev->pers) {
6863 err = update_array_info(mddev, &info);
6864 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006865 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006866 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006868 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006869 }
6870 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006871 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11006872 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006873 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006874 }
6875 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11006876 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11006877 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006878 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006879 }
6880 err = set_array_info(mddev, &info);
6881 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006882 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006883 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006884 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006885 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886 }
6887
6888 /*
6889 * Commands querying/configuring an existing array:
6890 */
NeilBrown32a76272005-06-21 17:17:14 -07006891 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006892 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006893 if ((!mddev->raid_disks && !mddev->external)
6894 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6895 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6896 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006898 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 }
6900
6901 /*
6902 * Commands even a read-only array can execute:
6903 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006904 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006905 case RESTART_ARRAY_RW:
6906 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006907 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006908
6909 case STOP_ARRAY:
6910 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006911 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006912
6913 case STOP_ARRAY_RO:
6914 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006915 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006916
NeilBrown3ea8929d2013-04-24 11:42:41 +10006917 case HOT_REMOVE_DISK:
6918 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006919 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006920
NeilBrown7ceb17e2013-04-24 11:42:42 +10006921 case ADD_NEW_DISK:
6922 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08006923 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10006924 * So require mddev->pers and MD_DISK_SYNC.
6925 */
6926 if (mddev->pers) {
6927 mdu_disk_info_t info;
6928 if (copy_from_user(&info, argp, sizeof(info)))
6929 err = -EFAULT;
6930 else if (!(info.state & (1<<MD_DISK_SYNC)))
6931 /* Need to clear read-only for this */
6932 break;
6933 else
6934 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006935 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006936 }
6937 break;
6938
NeilBrownc02c0ae2012-12-11 13:39:21 +11006939 case BLKROSET:
6940 if (get_user(ro, (int __user *)(arg))) {
6941 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006942 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006943 }
6944 err = -EINVAL;
6945
6946 /* if the bdev is going readonly the value of mddev->ro
6947 * does not matter, no writes are coming
6948 */
6949 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006950 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006951
NeilBrownc02c0ae2012-12-11 13:39:21 +11006952 /* are we are already prepared for writes? */
6953 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006954 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006955
6956 /* transitioning to readauto need only happen for
6957 * arrays that call md_write_start
6958 */
6959 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006961 if (err == 0) {
6962 mddev->ro = 2;
6963 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006964 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006965 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006966 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967 }
6968
6969 /*
6970 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006971 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 */
NeilBrown326eb172014-09-30 15:36:28 +10006973 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006974 if (mddev->ro == 2) {
6975 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006976 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006977 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006978 /* mddev_unlock will wake thread */
6979 /* If a device failed while we were read-only, we
6980 * need to make sure the metadata is updated now.
6981 */
6982 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6983 mddev_unlock(mddev);
6984 wait_event(mddev->sb_wait,
6985 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6986 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006987 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006988 }
NeilBrownf91de922005-11-08 21:39:36 -08006989 } else {
6990 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006991 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 }
6994
NeilBrownc02c0ae2012-12-11 13:39:21 +11006995 switch (cmd) {
6996 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006998 mdu_disk_info_t info;
6999 if (copy_from_user(&info, argp, sizeof(info)))
7000 err = -EFAULT;
7001 else
7002 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007003 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007006 case CLUSTERED_DISK_NACK:
7007 if (mddev_is_clustered(mddev))
7008 md_cluster_ops->new_disk_ack(mddev, false);
7009 else
7010 err = -EINVAL;
7011 goto unlock;
7012
NeilBrownc02c0ae2012-12-11 13:39:21 +11007013 case HOT_ADD_DISK:
7014 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007015 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016
NeilBrownc02c0ae2012-12-11 13:39:21 +11007017 case RUN_ARRAY:
7018 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007019 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020
NeilBrownc02c0ae2012-12-11 13:39:21 +11007021 case SET_BITMAP_FILE:
7022 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007023 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007024
NeilBrownc02c0ae2012-12-11 13:39:21 +11007025 default:
7026 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007027 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028 }
7029
NeilBrown3adc28d2014-09-30 15:46:41 +10007030unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007031 if (mddev->hold_active == UNTIL_IOCTL &&
7032 err != -EINVAL)
7033 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007035out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 return err;
7037}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007038#ifdef CONFIG_COMPAT
7039static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7040 unsigned int cmd, unsigned long arg)
7041{
7042 switch (cmd) {
7043 case HOT_REMOVE_DISK:
7044 case HOT_ADD_DISK:
7045 case SET_DISK_FAULTY:
7046 case SET_BITMAP_FILE:
7047 /* These take in integer arg, do not convert */
7048 break;
7049 default:
7050 arg = (unsigned long)compat_ptr(arg);
7051 break;
7052 }
7053
7054 return md_ioctl(bdev, mode, cmd, arg);
7055}
7056#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057
Al Viroa39907f2008-03-02 10:31:15 -05007058static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059{
7060 /*
7061 * Succeed if we can lock the mddev, which confirms that
7062 * it isn't being stopped right now.
7063 */
NeilBrownfd01b882011-10-11 16:47:53 +11007064 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 int err;
7066
Yuanhan Liu0c098222012-05-22 13:55:32 +10007067 if (!mddev)
7068 return -ENODEV;
7069
NeilBrownd3374822009-01-09 08:31:10 +11007070 if (mddev->gendisk != bdev->bd_disk) {
7071 /* we are racing with mddev_put which is discarding this
7072 * bd_disk.
7073 */
7074 mddev_put(mddev);
7075 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007076 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007077 /* Then retry the open from the top */
7078 return -ERESTARTSYS;
7079 }
7080 BUG_ON(mddev != bdev->bd_disk->private_data);
7081
NeilBrownc8c00a62009-08-10 12:50:52 +10007082 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083 goto out;
7084
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007085 if (test_bit(MD_CLOSING, &mddev->flags)) {
7086 mutex_unlock(&mddev->open_mutex);
7087 return -ENODEV;
7088 }
7089
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007091 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007092 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007094 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 out:
7096 return err;
7097}
7098
Al Virodb2a1442013-05-05 21:52:57 -04007099static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007101 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007103 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007104 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007107
7108static int md_media_changed(struct gendisk *disk)
7109{
NeilBrownfd01b882011-10-11 16:47:53 +11007110 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007111
7112 return mddev->changed;
7113}
7114
7115static int md_revalidate(struct gendisk *disk)
7116{
NeilBrownfd01b882011-10-11 16:47:53 +11007117 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007118
7119 mddev->changed = 0;
7120 return 0;
7121}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007122static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123{
7124 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007125 .open = md_open,
7126 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007127 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007128#ifdef CONFIG_COMPAT
7129 .compat_ioctl = md_compat_ioctl,
7130#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007131 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007132 .media_changed = md_media_changed,
7133 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134};
7135
NeilBrownf72ffdd2014-09-30 14:23:59 +10007136static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137{
NeilBrown2b8bf342011-10-11 16:48:23 +11007138 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 /*
7141 * md_thread is a 'system-thread', it's priority should be very
7142 * high. We avoid resource deadlocks individually in each
7143 * raid personality. (RAID5 does preallocation) We also use RR and
7144 * the very same RT priority as kswapd, thus we will never get
7145 * into a priority inversion deadlock.
7146 *
7147 * we definitely have to have equal or higher priority than
7148 * bdflush, otherwise bdflush will deadlock if there are too
7149 * many dirty RAID5 blocks.
7150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151
NeilBrown6985c432005-10-19 21:23:47 -07007152 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007153 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154
NeilBrown93588e22005-11-15 00:09:12 -08007155 /* We need to wait INTERRUPTIBLE so that
7156 * we don't add to the load-average.
7157 * That means we need to be sure no signals are
7158 * pending
7159 */
7160 if (signal_pending(current))
7161 flush_signals(current);
7162
7163 wait_event_interruptible_timeout
7164 (thread->wqueue,
7165 test_bit(THREAD_WAKEUP, &thread->flags)
7166 || kthread_should_stop(),
7167 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168
NeilBrown6c987912011-01-14 09:13:53 +11007169 clear_bit(THREAD_WAKEUP, &thread->flags);
7170 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007171 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007173
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 return 0;
7175}
7176
NeilBrown2b8bf342011-10-11 16:48:23 +11007177void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178{
7179 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007180 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 set_bit(THREAD_WAKEUP, &thread->flags);
7182 wake_up(&thread->wqueue);
7183 }
7184}
NeilBrown6c144d32014-09-30 16:15:38 +10007185EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186
Shaohua Li4ed87312012-10-11 13:34:00 +11007187struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7188 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189{
NeilBrown2b8bf342011-10-11 16:48:23 +11007190 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191
NeilBrown2b8bf342011-10-11 16:48:23 +11007192 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 if (!thread)
7194 return NULL;
7195
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196 init_waitqueue_head(&thread->wqueue);
7197
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 thread->run = run;
7199 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007200 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007201 thread->tsk = kthread_run(md_thread, thread,
7202 "%s_%s",
7203 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007204 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007205 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 kfree(thread);
7207 return NULL;
7208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209 return thread;
7210}
NeilBrown6c144d32014-09-30 16:15:38 +10007211EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212
NeilBrown2b8bf342011-10-11 16:48:23 +11007213void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214{
NeilBrown2b8bf342011-10-11 16:48:23 +11007215 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007216 if (!thread)
7217 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007218 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007219 /* Locking ensures that mddev_unlock does not wake_up a
7220 * non-existent thread
7221 */
7222 spin_lock(&pers_lock);
7223 *threadp = NULL;
7224 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007225
7226 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 kfree(thread);
7228}
NeilBrown6c144d32014-09-30 16:15:38 +10007229EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230
NeilBrownfd01b882011-10-11 16:47:53 +11007231void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232{
NeilBrownb2d444d2005-11-08 21:39:31 -08007233 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007235
NeilBrownde393cd2011-07-28 11:31:48 +10007236 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 return;
7238 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007239 if (mddev->degraded)
7240 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007241 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7243 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7244 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007245 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007246 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007247 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248}
NeilBrown6c144d32014-09-30 16:15:38 +10007249EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250
7251/* seq_file implementation /proc/mdstat */
7252
7253static void status_unused(struct seq_file *seq)
7254{
7255 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007256 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257
7258 seq_printf(seq, "unused devices: ");
7259
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007260 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261 char b[BDEVNAME_SIZE];
7262 i++;
7263 seq_printf(seq, "%s ",
7264 bdevname(rdev->bdev,b));
7265 }
7266 if (!i)
7267 seq_printf(seq, "<none>");
7268
7269 seq_printf(seq, "\n");
7270}
7271
NeilBrownf7851be2015-07-02 17:12:58 +10007272static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273{
NeilBrowndd71cf62009-05-07 12:49:35 +10007274 sector_t max_sectors, resync, res;
7275 unsigned long dt, db;
7276 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007277 int scale;
7278 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279
NeilBrownc804cde2012-05-21 09:28:33 +10007280 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7281 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007282 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007284 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285
NeilBrownf7851be2015-07-02 17:12:58 +10007286 resync = mddev->curr_resync;
7287 if (resync <= 3) {
7288 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7289 /* Still cleaning up */
7290 resync = max_sectors;
7291 } else
7292 resync -= atomic_read(&mddev->recovery_active);
7293
7294 if (resync == 0) {
7295 if (mddev->recovery_cp < MaxSector) {
7296 seq_printf(seq, "\tresync=PENDING");
7297 return 1;
7298 }
7299 return 0;
7300 }
7301 if (resync < 3) {
7302 seq_printf(seq, "\tresync=DELAYED");
7303 return 1;
7304 }
7305
NeilBrown403df472014-09-30 15:52:29 +10007306 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007307 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007308 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007309 * u32, as those are the requirements for sector_div.
7310 * Thus 'scale' must be at least 10
7311 */
7312 scale = 10;
7313 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007314 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007315 scale++;
7316 }
7317 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007318 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007319
7320 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007321 {
NeilBrown4588b422006-03-27 01:18:04 -08007322 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323 seq_printf(seq, "[");
7324 for (i = 0; i < x; i++)
7325 seq_printf(seq, "=");
7326 seq_printf(seq, ">");
7327 for (i = 0; i < y; i++)
7328 seq_printf(seq, ".");
7329 seq_printf(seq, "] ");
7330 }
NeilBrown4588b422006-03-27 01:18:04 -08007331 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007332 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7333 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007334 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7335 "check" :
7336 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7337 "resync" : "recovery"))),
7338 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007339 (unsigned long long) resync/2,
7340 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341
7342 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343 * dt: time from mark until now
7344 * db: blocks written from mark until now
7345 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007346 *
7347 * rt is a sector_t, so could be 32bit or 64bit.
7348 * So we divide before multiply in case it is 32bit and close
7349 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007350 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007351 * near the end of resync when the number of remaining sectors
7352 * is close to 'db'.
7353 * We then divide rt by 32 after multiplying by db to compensate.
7354 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 */
7356 dt = ((jiffies - mddev->resync_mark) / HZ);
7357 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007358 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7359 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360
NeilBrowndd71cf62009-05-07 12:49:35 +10007361 rt = max_sectors - resync; /* number of remaining sectors */
7362 sector_div(rt, db/32+1);
7363 rt *= dt;
7364 rt >>= 5;
7365
7366 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7367 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368
NeilBrownff4e8d92006-07-10 04:44:16 -07007369 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007370 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371}
7372
7373static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7374{
7375 struct list_head *tmp;
7376 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007377 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378
7379 if (l >= 0x10000)
7380 return NULL;
7381 if (!l--)
7382 /* header */
7383 return (void*)1;
7384
7385 spin_lock(&all_mddevs_lock);
7386 list_for_each(tmp,&all_mddevs)
7387 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007388 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389 mddev_get(mddev);
7390 spin_unlock(&all_mddevs_lock);
7391 return mddev;
7392 }
7393 spin_unlock(&all_mddevs_lock);
7394 if (!l--)
7395 return (void*)2;/* tail */
7396 return NULL;
7397}
7398
7399static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7400{
7401 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007402 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007403
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 ++*pos;
7405 if (v == (void*)2)
7406 return NULL;
7407
7408 spin_lock(&all_mddevs_lock);
7409 if (v == (void*)1)
7410 tmp = all_mddevs.next;
7411 else
7412 tmp = mddev->all_mddevs.next;
7413 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007414 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415 else {
7416 next_mddev = (void*)2;
7417 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419 spin_unlock(&all_mddevs_lock);
7420
7421 if (v != (void*)1)
7422 mddev_put(mddev);
7423 return next_mddev;
7424
7425}
7426
7427static void md_seq_stop(struct seq_file *seq, void *v)
7428{
NeilBrownfd01b882011-10-11 16:47:53 +11007429 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430
7431 if (mddev && v != (void*)1 && v != (void*)2)
7432 mddev_put(mddev);
7433}
7434
7435static int md_seq_show(struct seq_file *seq, void *v)
7436{
NeilBrownfd01b882011-10-11 16:47:53 +11007437 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007438 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007439 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440
7441 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007442 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443 seq_printf(seq, "Personalities : ");
7444 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007445 list_for_each_entry(pers, &pers_list, list)
7446 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447
7448 spin_unlock(&pers_lock);
7449 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007450 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451 return 0;
7452 }
7453 if (v == (void*)2) {
7454 status_unused(seq);
7455 return 0;
7456 }
7457
NeilBrown36d091f2014-12-15 12:56:58 +11007458 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7460 seq_printf(seq, "%s : %sactive", mdname(mddev),
7461 mddev->pers ? "" : "in");
7462 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007463 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007465 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007466 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467 seq_printf(seq, " %s", mddev->pers->name);
7468 }
7469
Andre Nolldd8ac332009-03-31 14:33:13 +11007470 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007471 rcu_read_lock();
7472 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 char b[BDEVNAME_SIZE];
7474 seq_printf(seq, " %s[%d]",
7475 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007476 if (test_bit(WriteMostly, &rdev->flags))
7477 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007478 if (test_bit(Journal, &rdev->flags))
7479 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007480 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481 seq_printf(seq, "(F)");
7482 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007483 }
7484 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007485 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007486 if (test_bit(Replacement, &rdev->flags))
7487 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007488 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007490 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491
7492 if (!list_empty(&mddev->disks)) {
7493 if (mddev->pers)
7494 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007495 (unsigned long long)
7496 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 else
7498 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007499 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007501 if (mddev->persistent) {
7502 if (mddev->major_version != 0 ||
7503 mddev->minor_version != 90) {
7504 seq_printf(seq," super %d.%d",
7505 mddev->major_version,
7506 mddev->minor_version);
7507 }
NeilBrowne6910632008-02-06 01:39:51 -08007508 } else if (mddev->external)
7509 seq_printf(seq, " super external:%s",
7510 mddev->metadata_type);
7511 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007512 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513
7514 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007515 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007516 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007517 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007518 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007519 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007520 }
NeilBrown32a76272005-06-21 17:17:14 -07007521 } else
7522 seq_printf(seq, "\n ");
7523
NeilBrown57148962012-03-19 12:46:40 +11007524 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525
7526 seq_printf(seq, "\n");
7527 }
NeilBrown36d091f2014-12-15 12:56:58 +11007528 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007529
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 return 0;
7531}
7532
Jan Engelhardt110518b2009-05-07 12:49:37 +10007533static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534 .start = md_seq_start,
7535 .next = md_seq_next,
7536 .stop = md_seq_stop,
7537 .show = md_seq_show,
7538};
7539
7540static int md_seq_open(struct inode *inode, struct file *file)
7541{
Kay Sieversf1514632011-07-12 20:48:39 +02007542 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007543 int error;
7544
7545 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007546 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007547 return error;
7548
7549 seq = file->private_data;
7550 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 return error;
7552}
7553
NeilBrowne2f23b62014-04-09 14:33:51 +10007554static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007555static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7556{
Kay Sieversf1514632011-07-12 20:48:39 +02007557 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007558 int mask;
7559
NeilBrowne2f23b62014-04-09 14:33:51 +10007560 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007561 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007562 poll_wait(filp, &md_event_waiters, wait);
7563
7564 /* always allow read */
7565 mask = POLLIN | POLLRDNORM;
7566
Kay Sieversf1514632011-07-12 20:48:39 +02007567 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007568 mask |= POLLERR | POLLPRI;
7569 return mask;
7570}
7571
Arjan van de Venfa027c22007-02-12 00:55:33 -08007572static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007573 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 .open = md_seq_open,
7575 .read = seq_read,
7576 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007577 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007578 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579};
7580
NeilBrown84fc4b52011-10-11 16:49:58 +11007581int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582{
NeilBrown9d487392016-11-02 14:16:49 +11007583 pr_debug("md: %s personality registered for level %d\n",
7584 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007586 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 spin_unlock(&pers_lock);
7588 return 0;
7589}
NeilBrown6c144d32014-09-30 16:15:38 +10007590EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591
NeilBrown84fc4b52011-10-11 16:49:58 +11007592int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593{
NeilBrown9d487392016-11-02 14:16:49 +11007594 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007596 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007597 spin_unlock(&pers_lock);
7598 return 0;
7599}
NeilBrown6c144d32014-09-30 16:15:38 +10007600EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007601
NeilBrown6022e752015-08-13 12:32:55 +10007602int register_md_cluster_operations(struct md_cluster_operations *ops,
7603 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007604{
NeilBrown6022e752015-08-13 12:32:55 +10007605 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007606 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007607 if (md_cluster_ops != NULL)
7608 ret = -EALREADY;
7609 else {
7610 md_cluster_ops = ops;
7611 md_cluster_mod = module;
7612 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007613 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007614 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007615}
7616EXPORT_SYMBOL(register_md_cluster_operations);
7617
7618int unregister_md_cluster_operations(void)
7619{
7620 spin_lock(&pers_lock);
7621 md_cluster_ops = NULL;
7622 spin_unlock(&pers_lock);
7623 return 0;
7624}
7625EXPORT_SYMBOL(unregister_md_cluster_operations);
7626
7627int md_setup_cluster(struct mddev *mddev, int nodes)
7628{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007629 if (!md_cluster_ops)
7630 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007631 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007632 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007633 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11007634 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007635 spin_unlock(&pers_lock);
7636 return -ENOENT;
7637 }
7638 spin_unlock(&pers_lock);
7639
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007640 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007641}
7642
7643void md_cluster_stop(struct mddev *mddev)
7644{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007645 if (!md_cluster_ops)
7646 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007647 md_cluster_ops->leave(mddev);
7648 module_put(md_cluster_mod);
7649}
7650
NeilBrownfd01b882011-10-11 16:47:53 +11007651static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007653 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007654 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007655 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656
7657 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007658 rcu_read_lock();
7659 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007661 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7662 (int)part_stat_read(&disk->part0, sectors[1]) -
7663 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007664 /* sync IO will cause sync_io to increase before the disk_stats
7665 * as sync_io is counted when a request starts, and
7666 * disk_stats is counted when it completes.
7667 * So resync activity will cause curr_events to be smaller than
7668 * when there was no such activity.
7669 * non-sync IO will cause disk_stat to increase without
7670 * increasing sync_io so curr_events will (eventually)
7671 * be larger than it was before. Once it becomes
7672 * substantially larger, the test below will cause
7673 * the array to appear non-idle, and resync will slow
7674 * down.
7675 * If there is a lot of outstanding resync activity when
7676 * we set last_event to curr_events, then all that activity
7677 * completing might cause the array to appear non-idle
7678 * and resync will be slowed down even though there might
7679 * not have been non-resync activity. This will only
7680 * happen once though. 'last_events' will soon reflect
7681 * the state where there is little or no outstanding
7682 * resync requests, and further resync activity will
7683 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007684 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007686 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007687 rdev->last_events = curr_events;
7688 idle = 0;
7689 }
7690 }
NeilBrown4b809912008-07-21 17:05:25 +10007691 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007692 return idle;
7693}
7694
NeilBrownfd01b882011-10-11 16:47:53 +11007695void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696{
7697 /* another "blocks" (512byte) blocks have been synced */
7698 atomic_sub(blocks, &mddev->recovery_active);
7699 wake_up(&mddev->recovery_wait);
7700 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007701 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007702 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007703 md_wakeup_thread(mddev->thread);
7704 // stop recovery, signal do_sync ....
7705 }
7706}
NeilBrown6c144d32014-09-30 16:15:38 +10007707EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007708
NeilBrown06d91a52005-06-21 17:17:12 -07007709/* md_write_start(mddev, bi)
7710 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007711 * in superblock) before writing, schedule a superblock update
7712 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007713 */
NeilBrownfd01b882011-10-11 16:47:53 +11007714void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715{
Neil Brown0fd62b82008-06-28 08:31:36 +10007716 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007717 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007718 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007719
NeilBrownf91de922005-11-08 21:39:36 -08007720 BUG_ON(mddev->ro == 1);
7721 if (mddev->ro == 2) {
7722 /* need to switch to read/write */
7723 mddev->ro = 0;
7724 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7725 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007726 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007727 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007728 }
NeilBrown06d91a52005-06-21 17:17:12 -07007729 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007730 if (mddev->safemode == 1)
7731 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007732 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007733 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007734 if (mddev->in_sync) {
7735 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007736 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007737 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007738 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007739 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007740 }
NeilBrown85572d72014-12-15 12:56:56 +11007741 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007742 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007743 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007744 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007745 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007746 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007747}
NeilBrown6c144d32014-09-30 16:15:38 +10007748EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007749
NeilBrownfd01b882011-10-11 16:47:53 +11007750void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751{
7752 if (atomic_dec_and_test(&mddev->writes_pending)) {
7753 if (mddev->safemode == 2)
7754 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007755 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007756 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7757 }
7758}
NeilBrown6c144d32014-09-30 16:15:38 +10007759EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760
NeilBrown2a2275d2007-01-26 00:57:11 -08007761/* md_allow_write(mddev)
7762 * Calling this ensures that the array is marked 'active' so that writes
7763 * may proceed without blocking. It is important to call this before
7764 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7765 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007766 *
Guoqing Jiangabf35082015-12-21 10:51:01 +11007767 * In the ->external case MD_CHANGE_PENDING can not be cleared until mddev->lock
Dan Williamsb5470dc2008-06-27 21:44:04 -07007768 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007769 */
NeilBrownfd01b882011-10-11 16:47:53 +11007770int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007771{
7772 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007773 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007774 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007775 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007776 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007777 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007778
NeilBrown85572d72014-12-15 12:56:56 +11007779 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007780 if (mddev->in_sync) {
7781 mddev->in_sync = 0;
7782 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007783 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007784 if (mddev->safemode_delay &&
7785 mddev->safemode == 0)
7786 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007787 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007788 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007789 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007790 } else
NeilBrown85572d72014-12-15 12:56:56 +11007791 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007792
NeilBrown070dc6d2010-08-30 17:33:34 +10007793 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007794 return -EAGAIN;
7795 else
7796 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007797}
7798EXPORT_SYMBOL_GPL(md_allow_write);
7799
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800#define SYNC_MARKS 10
7801#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007802#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007803void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804{
Shaohua Li4ed87312012-10-11 13:34:00 +11007805 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007806 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807 unsigned int currspeed = 0,
7808 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007809 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007810 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007811 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812 sector_t mark_cnt[SYNC_MARKS];
7813 int last_mark,m;
7814 struct list_head *tmp;
7815 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007816 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007817 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007818 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007819 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007820 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821
7822 /* just incase thread restarts... */
7823 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7824 return;
NeilBrown3991b312014-05-28 13:39:23 +10007825 if (mddev->ro) {/* never try to sync a read-only array */
7826 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007827 return;
NeilBrown3991b312014-05-28 13:39:23 +10007828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007830 if (mddev_is_clustered(mddev)) {
7831 ret = md_cluster_ops->resync_start(mddev);
7832 if (ret)
7833 goto skip;
7834
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04007835 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007836 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7837 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
7838 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
7839 && ((unsigned long long)mddev->curr_resync_completed
7840 < (unsigned long long)mddev->resync_max_sectors))
7841 goto skip;
7842 }
7843
NeilBrown61df9d92006-10-03 01:15:57 -07007844 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007845 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007846 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007847 action = "check";
7848 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007849 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007850 action = "repair";
7851 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007852 desc = "resync";
7853 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7854 desc = "reshape";
7855 else
7856 desc = "recovery";
7857
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007858 mddev->last_sync_action = action ?: desc;
7859
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860 /* we overload curr_resync somewhat here.
7861 * 0 == not engaged in resync at all
7862 * 2 == checking that there is no conflict with another sync
7863 * 1 == like 2, but have yielded to allow conflicting resync to
7864 * commense
7865 * other == active in resync - this many blocks
7866 *
7867 * Before starting a resync we must have set curr_resync to
7868 * 2, and then checked that every "conflicting" array has curr_resync
7869 * less than ours. When we find one that is the same or higher
7870 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7871 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7872 * This will mean we have to start checking from the beginning again.
7873 *
7874 */
7875
7876 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02007877 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 mddev->curr_resync = 2;
7879
7880 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007881 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007883 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007884 if (mddev2 == mddev)
7885 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007886 if (!mddev->parallel_resync
7887 && mddev2->curr_resync
7888 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 DEFINE_WAIT(wq);
7890 if (mddev < mddev2 && mddev->curr_resync == 2) {
7891 /* arbitrarily yield */
7892 mddev->curr_resync = 1;
7893 wake_up(&resync_wait);
7894 }
7895 if (mddev > mddev2 && mddev->curr_resync == 1)
7896 /* no need to wait here, we can wait the next
7897 * time 'round when curr_resync == 2
7898 */
7899 continue;
NeilBrown97441972008-09-19 11:49:54 +10007900 /* We need to wait 'interruptible' so as not to
7901 * contribute to the load average, and not to
7902 * be caught by 'softlockup'
7903 */
7904 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007905 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007906 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02007907 if (mddev2_minor != mddev2->md_minor) {
7908 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11007909 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
7910 desc, mdname(mddev),
7911 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02007912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007914 if (signal_pending(current))
7915 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916 schedule();
7917 finish_wait(&resync_wait, &wq);
7918 goto try_again;
7919 }
7920 finish_wait(&resync_wait, &wq);
7921 }
7922 }
7923 } while (mddev->curr_resync < 2);
7924
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007925 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007926 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007928 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929 */
7930 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007931 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007932 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007933 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7934 j = mddev->resync_min;
7935 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007936 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007937
NeilBrownccfcc3c2006-03-27 01:18:09 -08007938 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007939 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007940 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007942 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007943 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007944 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007945 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007946 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07007947 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007948 !test_bit(Faulty, &rdev->flags) &&
7949 !test_bit(In_sync, &rdev->flags) &&
7950 rdev->recovery_offset < j)
7951 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007952 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007953
7954 /* If there is a bitmap, we need to make sure all
7955 * writes that started before we added a spare
7956 * complete before we start doing a recovery.
7957 * Otherwise the write might complete and (via
7958 * bitmap_endwrite) set a bit in the bitmap after the
7959 * recovery has checked that bit and skipped that
7960 * region.
7961 */
7962 if (mddev->bitmap) {
7963 mddev->pers->quiesce(mddev, 1);
7964 mddev->pers->quiesce(mddev, 0);
7965 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007967
NeilBrown9d487392016-11-02 14:16:49 +11007968 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
7969 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
7970 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
7971 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972
NeilBrowneea1bf32009-03-31 14:27:02 +11007973 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007974
NeilBrown57afd892005-06-21 17:17:13 -07007975 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976 for (m = 0; m < SYNC_MARKS; m++) {
7977 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007978 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979 }
7980 last_mark = 0;
7981 mddev->resync_mark = mark[last_mark];
7982 mddev->resync_mark_cnt = mark_cnt[last_mark];
7983
7984 /*
7985 * Tune reconstruction:
7986 */
7987 window = 32*(PAGE_SIZE/512);
NeilBrown9d487392016-11-02 14:16:49 +11007988 pr_debug("md: using %dk window, over a total of %lluk.\n",
7989 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007990
7991 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007992 last_check = 0;
7993
7994 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11007995 pr_debug("md: resuming %s of %s from checkpoint.\n",
7996 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007998 } else
7999 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008000 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008001 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8002 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008003 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008004
majianpeng7c2c57c2012-07-03 12:12:26 +10008005 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008007 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008008
NeilBrown57afd892005-06-21 17:17:13 -07008009 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008010
NeilBrown7a91ee12009-05-26 12:57:21 +10008011 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8012 ((mddev->curr_resync > mddev->curr_resync_completed &&
8013 (mddev->curr_resync - mddev->curr_resync_completed)
8014 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008015 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008016 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008017 >= mddev->resync_max - mddev->curr_resync_completed ||
8018 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008019 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008020 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008021 wait_event(mddev->recovery_wait,
8022 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008023 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008024 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8025 j > mddev->recovery_cp)
8026 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008027 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10008028 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008029 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008030 }
NeilBrownacb180b2009-04-14 16:28:34 +10008031
NeilBrownc91abf52013-11-19 12:02:01 +11008032 while (j >= mddev->resync_max &&
8033 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008034 /* As this condition is controlled by user-space,
8035 * we can block indefinitely, so use '_interruptible'
8036 * to avoid triggering warnings.
8037 */
8038 flush_signals(current); /* just in case */
8039 wait_event_interruptible(mddev->recovery_wait,
8040 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008041 || test_bit(MD_RECOVERY_INTR,
8042 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008043 }
NeilBrownacb180b2009-04-14 16:28:34 +10008044
NeilBrownc91abf52013-11-19 12:02:01 +11008045 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8046 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008047
NeilBrown09314792015-02-19 16:04:40 +11008048 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008049 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008050 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008051 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008052 }
NeilBrown57afd892005-06-21 17:17:13 -07008053
8054 if (!skipped) { /* actual IO requested */
8055 io_sectors += sectors;
8056 atomic_add(sectors, &mddev->recovery_active);
8057 }
8058
NeilBrowne875ece2011-07-28 11:39:24 +10008059 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8060 break;
8061
Linus Torvalds1da177e2005-04-16 15:20:36 -07008062 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008063 if (j > max_sectors)
8064 /* when skipping, extra large numbers can be returned. */
8065 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008066 if (j > 2)
8067 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008068 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008069 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008070 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008071 * visible in /proc/mdstat
8072 */
8073 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008074
8075 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008076 continue;
8077
NeilBrown57afd892005-06-21 17:17:13 -07008078 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008079 repeat:
8080 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8081 /* step marks */
8082 int next = (last_mark+1) % SYNC_MARKS;
8083
8084 mddev->resync_mark = mark[next];
8085 mddev->resync_mark_cnt = mark_cnt[next];
8086 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008087 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008088 last_mark = next;
8089 }
8090
NeilBrownc91abf52013-11-19 12:02:01 +11008091 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8092 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008093
8094 /*
8095 * this loop exits only if either when we are slower than
8096 * the 'hard' speed limit, or the system was IO-idle for
8097 * a jiffy.
8098 * the system might be non-idle CPU-wise, but we only care
8099 * about not overloading the IO subsystem. (things like an
8100 * e2fsck being done on the RAID array should execute fast)
8101 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008102 cond_resched();
8103
Xiao Niac7e50a2014-08-07 09:37:41 -04008104 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8105 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008106 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008107
NeilBrown88202a02006-01-06 00:21:36 -08008108 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008109 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008110 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008111 goto repeat;
8112 }
NeilBrownac8fa412015-02-19 16:55:00 +11008113 if (!is_mddev_idle(mddev, 0)) {
8114 /*
8115 * Give other IO more of a chance.
8116 * The faster the devices, the less we wait.
8117 */
8118 wait_event(mddev->recovery_wait,
8119 !atomic_read(&mddev->recovery_active));
8120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008121 }
8122 }
NeilBrown9d487392016-11-02 14:16:49 +11008123 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8124 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8125 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008126 /*
8127 * this also signals 'finished resyncing' to md_stop
8128 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008129 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008130 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8131
NeilBrown5ed1df22015-07-24 13:27:08 +10008132 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8133 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008134 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008135 mddev->curr_resync_completed = mddev->curr_resync;
8136 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8137 }
NeilBrown09314792015-02-19 16:04:40 +11008138 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008139
NeilBrowndfc70642008-05-23 13:04:39 -07008140 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008141 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008142 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8143 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8144 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008145 pr_debug("md: checkpointing %s of %s.\n",
8146 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008147 if (test_bit(MD_RECOVERY_ERROR,
8148 &mddev->recovery))
8149 mddev->recovery_cp =
8150 mddev->curr_resync_completed;
8151 else
8152 mddev->recovery_cp =
8153 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008154 }
8155 } else
8156 mddev->recovery_cp = MaxSector;
8157 } else {
8158 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8159 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008160 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008161 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008162 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008163 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008164 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008165 !test_bit(Faulty, &rdev->flags) &&
8166 !test_bit(In_sync, &rdev->flags) &&
8167 rdev->recovery_offset < mddev->curr_resync)
8168 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008169 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008171 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008172 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008173 /* set CHANGE_PENDING here since maybe another update is needed,
8174 * so other nodes are informed. It should be harmless for normal
8175 * raid */
8176 set_mask_bits(&mddev->flags, 0,
8177 BIT(MD_CHANGE_PENDING) | BIT(MD_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008178
NeilBrown23da4222014-12-15 12:57:01 +11008179 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008180 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8181 /* We completed so min/max setting can be forgotten if used. */
8182 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8183 mddev->resync_min = 0;
8184 mddev->resync_max = MaxSector;
8185 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8186 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008187 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008189 spin_unlock(&mddev->lock);
8190
Linus Torvalds1da177e2005-04-16 15:20:36 -07008191 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008193 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008194}
NeilBrown29269552006-03-27 01:18:10 -08008195EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196
NeilBrown746d3202013-04-24 11:42:41 +10008197static int remove_and_add_spares(struct mddev *mddev,
8198 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008199{
NeilBrown3cb03002011-10-11 16:45:26 +11008200 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008201 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008202 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008203 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008204
NeilBrownd787be42016-06-02 16:19:53 +10008205 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008206 if ((this == NULL || rdev == this) &&
8207 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008208 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008209 test_bit(Faulty, &rdev->flags) &&
8210 atomic_read(&rdev->nr_pending)==0) {
8211 /* Faulty non-Blocked devices with nr_pending == 0
8212 * never get nr_pending incremented,
8213 * never get Faulty cleared, and never get Blocked set.
8214 * So we can synchronize_rcu now rather than once per device
8215 */
8216 remove_some = true;
8217 set_bit(RemoveSynchronized, &rdev->flags);
8218 }
8219 }
8220
8221 if (remove_some)
8222 synchronize_rcu();
8223 rdev_for_each(rdev, mddev) {
8224 if ((this == NULL || rdev == this) &&
8225 rdev->raid_disk >= 0 &&
8226 !test_bit(Blocked, &rdev->flags) &&
8227 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008228 (!test_bit(In_sync, &rdev->flags) &&
8229 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008230 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008231 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008232 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008233 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008234 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008235 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008236 }
8237 }
NeilBrownd787be42016-06-02 16:19:53 +10008238 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8239 clear_bit(RemoveSynchronized, &rdev->flags);
8240 }
8241
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008242 if (removed && mddev->kobj.sd)
8243 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008244
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008245 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008246 goto no_add;
8247
NeilBrowndafb20f2012-03-19 12:46:39 +11008248 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008249 if (this && this != rdev)
8250 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008251 if (test_bit(Candidate, &rdev->flags))
8252 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008253 if (rdev->raid_disk >= 0 &&
8254 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008255 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008256 !test_bit(Faulty, &rdev->flags))
8257 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008258 if (rdev->raid_disk >= 0)
8259 continue;
8260 if (test_bit(Faulty, &rdev->flags))
8261 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008262 if (!test_bit(Journal, &rdev->flags)) {
8263 if (mddev->ro &&
8264 ! (rdev->saved_raid_disk >= 0 &&
8265 !test_bit(Bitmap_sync, &rdev->flags)))
8266 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008267
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008268 rdev->recovery_offset = 0;
8269 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008270 if (mddev->pers->
8271 hot_add_disk(mddev, rdev) == 0) {
8272 if (sysfs_link_rdev(mddev, rdev))
8273 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008274 if (!test_bit(Journal, &rdev->flags))
8275 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008276 md_new_event(mddev);
8277 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008278 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008279 }
NeilBrown746d3202013-04-24 11:42:41 +10008280no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008281 if (removed)
8282 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008283 return spares;
8284}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008285
NeilBrownac05f252014-09-30 08:10:42 +10008286static void md_start_sync(struct work_struct *ws)
8287{
8288 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008289
NeilBrownac05f252014-09-30 08:10:42 +10008290 mddev->sync_thread = md_register_thread(md_do_sync,
8291 mddev,
8292 "resync");
8293 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11008294 pr_warn("%s: could not start resync thread...\n",
8295 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008296 /* leave the spares where they are, it shouldn't hurt */
8297 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8298 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8299 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8300 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8301 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008302 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008303 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8304 &mddev->recovery))
8305 if (mddev->sysfs_action)
8306 sysfs_notify_dirent_safe(mddev->sysfs_action);
8307 } else
8308 md_wakeup_thread(mddev->sync_thread);
8309 sysfs_notify_dirent_safe(mddev->sysfs_action);
8310 md_new_event(mddev);
8311}
8312
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313/*
8314 * This routine is regularly called by all per-raid-array threads to
8315 * deal with generic issues like resync and super-block update.
8316 * Raid personalities that don't have a thread (linear/raid0) do not
8317 * need this as they never do any recovery or update the superblock.
8318 *
8319 * It does not do any resync itself, but rather "forks" off other threads
8320 * to do that as needed.
8321 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8322 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008323 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324 * and wakeups up this thread which will reap the thread and finish up.
8325 * This thread also removes any faulty devices (with nr_pending == 0).
8326 *
8327 * The overall approach is:
8328 * 1/ if the superblock needs updating, update it.
8329 * 2/ If a recovery thread is running, don't do anything else.
8330 * 3/ If recovery has finished, clean up, possibly marking spares active.
8331 * 4/ If there are any faulty devices, remove them.
8332 * 5/ If array is degraded, try to add spares devices
8333 * 6/ If array has spares or is not in-sync, start a resync thread.
8334 */
NeilBrownfd01b882011-10-11 16:47:53 +11008335void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008336{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008337 if (mddev->suspended)
8338 return;
8339
NeilBrown5f404022005-06-21 17:17:16 -07008340 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008341 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008342
NeilBrownfca4d842005-06-21 17:17:11 -07008343 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008344 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11008345 pr_debug("md: %s in immediate safe mode\n",
8346 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07008347 mddev->safemode = 2;
8348 }
8349 flush_signals(current);
8350 }
8351
NeilBrownc89a8ee2008-08-05 15:54:13 +10008352 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8353 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008354 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008355 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008356 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008357 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008358 test_bit(MD_RELOAD_SB, &mddev->flags) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008359 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008360 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8361 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362 ))
8363 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008364
NeilBrowndf5b89b2006-03-27 01:18:20 -08008365 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008366 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008367
NeilBrownc89a8ee2008-08-05 15:54:13 +10008368 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008369 struct md_rdev *rdev;
8370 if (!mddev->external && mddev->in_sync)
8371 /* 'Blocked' flag not needed as failed devices
8372 * will be recorded if array switched to read/write.
8373 * Leaving it set will prevent the device
8374 * from being removed.
8375 */
8376 rdev_for_each(rdev, mddev)
8377 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008378 /* On a read-only array we can:
8379 * - remove failed devices
8380 * - add already-in_sync devices if the array itself
8381 * is in-sync.
8382 * As we only add devices that are already in-sync,
8383 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008384 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008385 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008386 /* There is no thread, but we need to call
8387 * ->spare_active and clear saved_raid_disk
8388 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008389 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008390 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008391 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008392 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Lid4929ad2015-09-18 10:20:12 -07008393 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008394 goto unlock;
8395 }
8396
Guoqing Jiang659b2542015-12-21 10:50:59 +11008397 if (mddev_is_clustered(mddev)) {
8398 struct md_rdev *rdev;
8399 /* kick the device if another node issued a
8400 * remove disk.
8401 */
8402 rdev_for_each(rdev, mddev) {
8403 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8404 rdev->raid_disk < 0)
8405 md_kick_rdev_from_array(rdev);
8406 }
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008407
8408 if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags))
8409 md_reload_sb(mddev, mddev->good_device_nr);
Guoqing Jiang659b2542015-12-21 10:50:59 +11008410 }
8411
NeilBrown31a59e32008-04-30 00:52:30 -07008412 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008413 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008414 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008415 if (mddev->safemode &&
8416 !atomic_read(&mddev->writes_pending) &&
8417 !mddev->in_sync &&
8418 mddev->recovery_cp == MaxSector) {
8419 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008420 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008421 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008422 }
8423 if (mddev->safemode == 1)
8424 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008425 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008426 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008427 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008428 }
NeilBrownfca4d842005-06-21 17:17:11 -07008429
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05008430 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07008431 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008432
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8434 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8435 /* resync/recovery still happening */
8436 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8437 goto unlock;
8438 }
8439 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008440 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008441 goto unlock;
8442 }
Neil Brown72a23c22008-06-28 08:31:41 +10008443 /* Set RUNNING before clearing NEEDED to avoid
8444 * any transients in the value of "sync_action".
8445 */
NeilBrown72f36d52012-10-11 14:25:57 +11008446 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008447 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008448 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008449 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008450 /* Clear some bits that don't mean anything, but
8451 * might be left set
8452 */
NeilBrown24dd4692005-11-08 21:39:26 -08008453 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8454 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008455
NeilBrowned209582012-04-24 10:23:14 +10008456 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8457 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008458 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008459 /* no recovery is running.
8460 * remove any failed drives, then
8461 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008462 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463 * the personality to fail the re-add.
8464 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008465
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008466 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008467 if (mddev->pers->check_reshape == NULL ||
8468 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008469 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008470 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008471 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008472 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008473 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008474 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8475 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008476 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008477 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008478 } else if (mddev->recovery_cp < MaxSector) {
8479 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008480 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008481 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8482 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008483 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008484
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008486 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008487 /* We are adding a device or devices to an array
8488 * which has the bitmap stored on all devices.
8489 * So make sure all bitmap pages get written
8490 */
8491 bitmap_write_all(mddev->bitmap);
8492 }
NeilBrownac05f252014-09-30 08:10:42 +10008493 INIT_WORK(&mddev->del_work, md_start_sync);
8494 queue_work(md_misc_wq, &mddev->del_work);
8495 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008496 }
NeilBrownac05f252014-09-30 08:10:42 +10008497 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008498 if (!mddev->sync_thread) {
8499 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008500 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008501 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8502 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008503 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008504 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008505 }
NeilBrownac05f252014-09-30 08:10:42 +10008506 unlock:
8507 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508 mddev_unlock(mddev);
8509 }
8510}
NeilBrown6c144d32014-09-30 16:15:38 +10008511EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008512
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008513void md_reap_sync_thread(struct mddev *mddev)
8514{
8515 struct md_rdev *rdev;
8516
8517 /* resync has finished, collect result */
8518 md_unregister_thread(&mddev->sync_thread);
8519 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8520 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8521 /* success...*/
8522 /* activate any spares */
8523 if (mddev->pers->spare_active(mddev)) {
8524 sysfs_notify(&mddev->kobj, NULL,
8525 "degraded");
8526 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8527 }
8528 }
8529 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8530 mddev->pers->finish_reshape)
8531 mddev->pers->finish_reshape(mddev);
8532
8533 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008534 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008535 */
NeilBrownf4667222013-12-09 12:04:56 +11008536 if (!mddev->degraded)
8537 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008538 rdev->saved_raid_disk = -1;
8539
8540 md_update_sb(mddev, 1);
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008541 /* MD_CHANGE_PENDING should be cleared by md_update_sb, so we can
8542 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8543 * clustered raid */
8544 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8545 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008546 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008547 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008548 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8549 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8550 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8551 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008552 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008553 /* flag recovery needed just to double check */
8554 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8555 sysfs_notify_dirent_safe(mddev->sysfs_action);
8556 md_new_event(mddev);
8557 if (mddev->event_work.func)
8558 queue_work(md_misc_wq, &mddev->event_work);
8559}
NeilBrown6c144d32014-09-30 16:15:38 +10008560EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008561
NeilBrownfd01b882011-10-11 16:47:53 +11008562void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008563{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008564 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008565 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008566 !test_bit(Blocked, &rdev->flags) &&
8567 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008568 msecs_to_jiffies(5000));
8569 rdev_dec_pending(rdev, mddev);
8570}
8571EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8572
NeilBrownc6563a82012-05-21 09:27:00 +10008573void md_finish_reshape(struct mddev *mddev)
8574{
8575 /* called be personality module when reshape completes. */
8576 struct md_rdev *rdev;
8577
8578 rdev_for_each(rdev, mddev) {
8579 if (rdev->data_offset > rdev->new_data_offset)
8580 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8581 else
8582 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8583 rdev->data_offset = rdev->new_data_offset;
8584 }
8585}
8586EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008587
Vishal Vermafc974ee2015-12-24 19:20:34 -07008588/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10008589
Vishal Vermafc974ee2015-12-24 19:20:34 -07008590/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11008591int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008592 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008593{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008594 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10008595 int rv;
8596 if (is_new)
8597 s += rdev->new_data_offset;
8598 else
8599 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008600 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8601 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008602 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008603 if (test_bit(ExternalBbl, &rdev->flags))
8604 sysfs_notify(&rdev->kobj, NULL,
8605 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008606 sysfs_notify_dirent_safe(rdev->sysfs_state);
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008607 set_mask_bits(&mddev->flags, 0,
8608 BIT(MD_CHANGE_CLEAN) | BIT(MD_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10008609 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07008610 return 1;
8611 } else
8612 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008613}
8614EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8615
NeilBrownc6563a82012-05-21 09:27:00 +10008616int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8617 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008618{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008619 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10008620 if (is_new)
8621 s += rdev->new_data_offset;
8622 else
8623 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008624 rv = badblocks_clear(&rdev->badblocks, s, sectors);
8625 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
8626 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
8627 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008628}
8629EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8630
Adrian Bunk75c96f82005-05-05 16:16:09 -07008631static int md_notify_reboot(struct notifier_block *this,
8632 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008633{
8634 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008635 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008636 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008637
NeilBrownc744a652012-03-19 12:46:37 +11008638 for_each_mddev(mddev, tmp) {
8639 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008640 if (mddev->pers)
8641 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008642 if (mddev->persistent)
8643 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008644 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008645 }
NeilBrownc744a652012-03-19 12:46:37 +11008646 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647 }
NeilBrownc744a652012-03-19 12:46:37 +11008648 /*
8649 * certain more exotic SCSI devices are known to be
8650 * volatile wrt too early system reboots. While the
8651 * right place to handle this issue is the given
8652 * driver, we do want to have a safe RAID driver ...
8653 */
8654 if (need_delay)
8655 mdelay(1000*1);
8656
Linus Torvalds1da177e2005-04-16 15:20:36 -07008657 return NOTIFY_DONE;
8658}
8659
Adrian Bunk75c96f82005-05-05 16:16:09 -07008660static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008661 .notifier_call = md_notify_reboot,
8662 .next = NULL,
8663 .priority = INT_MAX, /* before any real devices */
8664};
8665
8666static void md_geninit(void)
8667{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008668 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008669
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008670 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008671}
8672
Adrian Bunk75c96f82005-05-05 16:16:09 -07008673static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008674{
Tejun Heoe804ac72010-10-15 15:36:08 +02008675 int ret = -ENOMEM;
8676
Tejun Heoada609e2011-01-25 14:35:54 +01008677 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008678 if (!md_wq)
8679 goto err_wq;
8680
8681 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8682 if (!md_misc_wq)
8683 goto err_misc_wq;
8684
8685 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8686 goto err_md;
8687
8688 if ((ret = register_blkdev(0, "mdp")) < 0)
8689 goto err_mdp;
8690 mdp_major = ret;
8691
NeilBrownaf5628f2014-07-31 13:54:54 +10008692 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008693 md_probe, NULL, NULL);
8694 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008695 md_probe, NULL, NULL);
8696
Linus Torvalds1da177e2005-04-16 15:20:36 -07008697 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008698 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008699
8700 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702
Tejun Heoe804ac72010-10-15 15:36:08 +02008703err_mdp:
8704 unregister_blkdev(MD_MAJOR, "md");
8705err_md:
8706 destroy_workqueue(md_misc_wq);
8707err_misc_wq:
8708 destroy_workqueue(md_wq);
8709err_wq:
8710 return ret;
8711}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008712
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008713static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008714{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008715 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8716 struct md_rdev *rdev2;
8717 int role, ret;
8718 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008719
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008720 /* Check for change of roles in the active devices */
8721 rdev_for_each(rdev2, mddev) {
8722 if (test_bit(Faulty, &rdev2->flags))
8723 continue;
8724
8725 /* Check if the roles changed */
8726 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008727
8728 if (test_bit(Candidate, &rdev2->flags)) {
8729 if (role == 0xfffe) {
8730 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
8731 md_kick_rdev_from_array(rdev2);
8732 continue;
8733 }
8734 else
8735 clear_bit(Candidate, &rdev2->flags);
8736 }
8737
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008738 if (role != rdev2->raid_disk) {
8739 /* got activated */
8740 if (rdev2->raid_disk == -1 && role != 0xffff) {
8741 rdev2->saved_raid_disk = role;
8742 ret = remove_and_add_spares(mddev, rdev2);
8743 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11008744 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04008745 /* wakeup mddev->thread here, so array could
8746 * perform resync with the new activated disk */
8747 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8748 md_wakeup_thread(mddev->thread);
8749
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008750 }
8751 /* device faulty
8752 * We just want to do the minimum to mark the disk
8753 * as faulty. The recovery is performed by the
8754 * one who initiated the error.
8755 */
8756 if ((role == 0xfffe) || (role == 0xfffd)) {
8757 md_error(mddev, rdev2);
8758 clear_bit(Blocked, &rdev2->flags);
8759 }
8760 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008761 }
8762
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11008763 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
8764 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008765
8766 /* Finally set the event to be up to date */
8767 mddev->events = le64_to_cpu(sb->events);
8768}
8769
8770static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
8771{
8772 int err;
8773 struct page *swapout = rdev->sb_page;
8774 struct mdp_superblock_1 *sb;
8775
8776 /* Store the sb page of the rdev in the swapout temporary
8777 * variable in case we err in the future
8778 */
8779 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11008780 err = alloc_disk_sb(rdev);
8781 if (err == 0) {
8782 ClearPageUptodate(rdev->sb_page);
8783 rdev->sb_loaded = 0;
8784 err = super_types[mddev->major_version].
8785 load_super(rdev, NULL, mddev->minor_version);
8786 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008787 if (err < 0) {
8788 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
8789 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11008790 if (rdev->sb_page)
8791 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008792 rdev->sb_page = swapout;
8793 rdev->sb_loaded = 1;
8794 return err;
8795 }
8796
8797 sb = page_address(rdev->sb_page);
8798 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
8799 * is not set
8800 */
8801
8802 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
8803 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
8804
8805 /* The other node finished recovery, call spare_active to set
8806 * device In_sync and mddev->degraded
8807 */
8808 if (rdev->recovery_offset == MaxSector &&
8809 !test_bit(In_sync, &rdev->flags) &&
8810 mddev->pers->spare_active(mddev))
8811 sysfs_notify(&mddev->kobj, NULL, "degraded");
8812
8813 put_page(swapout);
8814 return 0;
8815}
8816
8817void md_reload_sb(struct mddev *mddev, int nr)
8818{
8819 struct md_rdev *rdev;
8820 int err;
8821
8822 /* Find the rdev */
8823 rdev_for_each_rcu(rdev, mddev) {
8824 if (rdev->desc_nr == nr)
8825 break;
8826 }
8827
8828 if (!rdev || rdev->desc_nr != nr) {
8829 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
8830 return;
8831 }
8832
8833 err = read_rdev(mddev, rdev);
8834 if (err < 0)
8835 return;
8836
8837 check_sb_changes(mddev, rdev);
8838
8839 /* Read all rdev's to update recovery_offset */
8840 rdev_for_each_rcu(rdev, mddev)
8841 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008842}
8843EXPORT_SYMBOL(md_reload_sb);
8844
Linus Torvalds1da177e2005-04-16 15:20:36 -07008845#ifndef MODULE
8846
8847/*
8848 * Searches all registered partitions for autorun RAID arrays
8849 * at boot time.
8850 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008851
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008852static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008853static LIST_HEAD(all_detected_devices);
8854struct detected_devices_node {
8855 struct list_head list;
8856 dev_t dev;
8857};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858
8859void md_autodetect_dev(dev_t dev)
8860{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008861 struct detected_devices_node *node_detected_dev;
8862
8863 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8864 if (node_detected_dev) {
8865 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008866 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008867 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008868 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008870}
8871
Linus Torvalds1da177e2005-04-16 15:20:36 -07008872static void autostart_arrays(int part)
8873{
NeilBrown3cb03002011-10-11 16:45:26 +11008874 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008875 struct detected_devices_node *node_detected_dev;
8876 dev_t dev;
8877 int i_scanned, i_passed;
8878
8879 i_scanned = 0;
8880 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008881
NeilBrown9d487392016-11-02 14:16:49 +11008882 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008883
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008884 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008885 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8886 i_scanned++;
8887 node_detected_dev = list_entry(all_detected_devices.next,
8888 struct detected_devices_node, list);
8889 list_del(&node_detected_dev->list);
8890 dev = node_detected_dev->dev;
8891 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07008892 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07008893 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07008894 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008895 if (IS_ERR(rdev))
8896 continue;
8897
NeilBrown403df472014-09-30 15:52:29 +10008898 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008899 continue;
NeilBrown403df472014-09-30 15:52:29 +10008900
NeilBrownd0fae182008-03-04 14:29:31 -08008901 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008902 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008903 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008904 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008905 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008906
NeilBrown9d487392016-11-02 14:16:49 +11008907 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008908
8909 autorun_devices(part);
8910}
8911
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008912#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008913
8914static __exit void md_exit(void)
8915{
NeilBrownfd01b882011-10-11 16:47:53 +11008916 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008917 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008918 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008919
NeilBrownaf5628f2014-07-31 13:54:54 +10008920 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008921 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008922
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008923 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008924 unregister_blkdev(mdp_major, "mdp");
8925 unregister_reboot_notifier(&md_notifier);
8926 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008927
8928 /* We cannot unload the modules while some process is
8929 * waiting for us in select() or poll() - wake them up
8930 */
8931 md_unloading = 1;
8932 while (waitqueue_active(&md_event_waiters)) {
8933 /* not safe to leave yet */
8934 wake_up(&md_event_waiters);
8935 msleep(delay);
8936 delay += delay;
8937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008938 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008939
NeilBrown29ac4aa2008-02-06 01:39:58 -08008940 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008941 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008942 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008944 destroy_workqueue(md_misc_wq);
8945 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008946}
8947
Dan Williams685784a2007-07-09 11:56:42 -07008948subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008949module_exit(md_exit)
8950
NeilBrownf91de922005-11-08 21:39:36 -08008951static int get_ro(char *buffer, struct kernel_param *kp)
8952{
8953 return sprintf(buffer, "%d", start_readonly);
8954}
8955static int set_ro(const char *val, struct kernel_param *kp)
8956{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03008957 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08008958}
8959
NeilBrown80ca3a42006-07-10 04:44:18 -07008960module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8961module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11008962module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008963
Linus Torvalds1da177e2005-04-16 15:20:36 -07008964MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008965MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008966MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008967MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);