blob: c7894fbbd8e5e0e984c7a9e7d0c07866ecfe8c33 [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);
NeilBrown46533ff2016-11-18 16:16:11 +1100730 if (!test_bit(Faulty, &rdev->flags)
731 && (bio->bi_opf & MD_FAILFAST)) {
732 set_bit(MD_NEED_REWRITE, &mddev->flags);
733 set_bit(LastDev, &rdev->flags);
734 }
735 } else
736 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700737
NeilBrowna9701a32005-11-08 21:39:34 -0800738 if (atomic_dec_and_test(&mddev->pending_writes))
739 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700740 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700741 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700742}
743
NeilBrownfd01b882011-10-11 16:47:53 +1100744void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700745 sector_t sector, int size, struct page *page)
746{
747 /* write first size bytes of page to sector of rdev
748 * Increment mddev->pending_writes before returning
749 * and decrement it on completion, waking up sb_wait
750 * if zero is reached.
751 * If an error occurred, call md_error
752 */
NeilBrown46533ff2016-11-18 16:16:11 +1100753 struct bio *bio;
754 int ff = 0;
755
756 if (test_bit(Faulty, &rdev->flags))
757 return;
758
759 bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700760
Shaohua Lied3b98c2016-03-29 14:00:19 -0700761 atomic_inc(&rdev->nr_pending);
762
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100763 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700764 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700765 bio_add_page(bio, page, size, 0);
766 bio->bi_private = rdev;
767 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100768
769 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
770 test_bit(FailFast, &rdev->flags) &&
771 !test_bit(LastDev, &rdev->flags))
772 ff = MD_FAILFAST;
773 bio_set_op_attrs(bio, REQ_OP_WRITE, WRITE_FLUSH_FUA | ff);
NeilBrowna9701a32005-11-08 21:39:34 -0800774
NeilBrown7bfa19f2005-06-21 17:17:28 -0700775 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500776 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800777}
778
NeilBrown46533ff2016-11-18 16:16:11 +1100779int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800780{
Tejun Heoe9c74692010-09-03 11:56:18 +0200781 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000782 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown46533ff2016-11-18 16:16:11 +1100783 if (test_and_clear_bit(MD_NEED_REWRITE, &mddev->flags))
784 return -EAGAIN;
785 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700786}
787
NeilBrown3cb03002011-10-11 16:45:26 +1100788int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -0500789 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
NeilBrowna167f662010-10-26 18:31:13 +1100791 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 int ret;
793
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100794 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
795 rdev->meta_bdev : rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -0500796 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100797 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700798 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000799 else if (rdev->mddev->reshape_position != MaxSector &&
800 (rdev->mddev->reshape_backwards ==
801 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700802 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100803 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700804 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500806
807 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200809 ret = !bio->bi_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 bio_put(bio);
811 return ret;
812}
NeilBrowna8745db2006-01-06 00:20:34 -0800813EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
NeilBrownf72ffdd2014-09-30 14:23:59 +1000815static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (rdev->sb_loaded)
820 return 0;
821
Mike Christie796a5cf2016-06-05 14:32:07 -0500822 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 goto fail;
824 rdev->sb_loaded = 1;
825 return 0;
826
827fail:
NeilBrown9d487392016-11-02 14:16:49 +1100828 pr_err("md: disabled device %s, could not read superblock.\n",
829 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return -EINVAL;
831}
832
833static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
834{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000835 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000836 sb1->set_uuid1 == sb2->set_uuid1 &&
837 sb1->set_uuid2 == sb2->set_uuid2 &&
838 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
842{
843 int ret;
844 mdp_super_t *tmp1, *tmp2;
845
846 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
847 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
848
849 if (!tmp1 || !tmp2) {
850 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 goto abort;
852 }
853
854 *tmp1 = *sb1;
855 *tmp2 = *sb2;
856
857 /*
858 * nr_disks is not constant
859 */
860 tmp1->nr_disks = 0;
861 tmp2->nr_disks = 0;
862
Andre Nollce0c8e02008-07-11 22:02:20 +1000863 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700865 kfree(tmp1);
866 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return ret;
868}
869
NeilBrown4d167f02007-05-09 02:35:37 -0700870static u32 md_csum_fold(u32 csum)
871{
872 csum = (csum & 0xffff) + (csum >> 16);
873 return (csum & 0xffff) + (csum >> 16);
874}
875
NeilBrownf72ffdd2014-09-30 14:23:59 +1000876static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
NeilBrown4d167f02007-05-09 02:35:37 -0700878 u64 newcsum = 0;
879 u32 *sb32 = (u32*)sb;
880 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 unsigned int disk_csum, csum;
882
883 disk_csum = sb->sb_csum;
884 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700885
886 for (i = 0; i < MD_SB_BYTES/4 ; i++)
887 newcsum += sb32[i];
888 csum = (newcsum & 0xffffffff) + (newcsum>>32);
889
NeilBrown4d167f02007-05-09 02:35:37 -0700890#ifdef CONFIG_ALPHA
891 /* This used to use csum_partial, which was wrong for several
892 * reasons including that different results are returned on
893 * different architectures. It isn't critical that we get exactly
894 * the same return value as before (we always csum_fold before
895 * testing, and that removes any differences). However as we
896 * know that csum_partial always returned a 16bit value on
897 * alphas, do a fold to maximise conformity to previous behaviour.
898 */
899 sb->sb_csum = md_csum_fold(disk_csum);
900#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700902#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return csum;
904}
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906/*
907 * Handle superblock details.
908 * We want to be able to handle multiple superblock formats
909 * so we have a common interface to them all, and an array of
910 * different handlers.
911 * We rely on user-space to write the initial superblock, and support
912 * reading and updating of superblocks.
913 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100914 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * loads and validates a superblock on dev.
916 * if refdev != NULL, compare superblocks on both devices
917 * Return:
918 * 0 - dev has a superblock that is compatible with refdev
919 * 1 - dev has a superblock that is compatible and newer than refdev
920 * so dev should be used as the refdev in future
921 * -EINVAL superblock incompatible or invalid
922 * -othererror e.g. -EIO
923 *
NeilBrownfd01b882011-10-11 16:47:53 +1100924 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 * Verify that dev is acceptable into mddev.
926 * The first time, mddev->raid_disks will be 0, and data from
927 * dev should be merged in. Subsequent calls check that dev
928 * is new enough. Return 0 or -EINVAL
929 *
NeilBrownfd01b882011-10-11 16:47:53 +1100930 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 * Update the superblock for rdev with data in mddev
932 * This does not write to disc.
933 *
934 */
935
936struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000937 char *name;
938 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000939 int (*load_super)(struct md_rdev *rdev,
940 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000941 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000942 int (*validate_super)(struct mddev *mddev,
943 struct md_rdev *rdev);
944 void (*sync_super)(struct mddev *mddev,
945 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100946 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000947 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000948 int (*allow_new_offset)(struct md_rdev *rdev,
949 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950};
951
952/*
Andre Noll0894cc32009-06-18 08:49:23 +1000953 * Check that the given mddev has no bitmap.
954 *
955 * This function is called from the run method of all personalities that do not
956 * support bitmaps. It prints an error message and returns non-zero if mddev
957 * has a bitmap. Otherwise, it returns 0.
958 *
959 */
NeilBrownfd01b882011-10-11 16:47:53 +1100960int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000961{
NeilBrownc3d97142009-12-14 12:49:52 +1100962 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000963 return 0;
NeilBrown9d487392016-11-02 14:16:49 +1100964 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +1000965 mdname(mddev), mddev->pers->name);
966 return 1;
967}
968EXPORT_SYMBOL(md_check_no_bitmap);
969
970/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000971 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 */
NeilBrown3cb03002011-10-11 16:45:26 +1100973static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
976 mdp_super_t *sb;
977 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 /*
Andre Noll0f420352008-07-11 22:02:23 +1000980 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 * it's at the end of the disk.
982 *
983 * It also happens to be a multiple of 4Kb.
984 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100985 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
NeilBrown0002b272005-09-09 16:23:53 -0700987 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +1100988 if (ret)
989 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 ret = -EINVAL;
992
993 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000994 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +1100997 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 goto abort;
999 }
1000
1001 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001002 sb->minor_version < 90 ||
1003 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +11001004 pr_warn("Bad version number %d.%d on %s\n",
1005 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 goto abort;
1007 }
1008
1009 if (sb->raid_disks <= 0)
1010 goto abort;
1011
NeilBrown4d167f02007-05-09 02:35:37 -07001012 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001013 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 goto abort;
1015 }
1016
1017 rdev->preferred_minor = sb->md_minor;
1018 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001019 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001020 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001021 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 if (sb->level == LEVEL_MULTIPATH)
1024 rdev->desc_nr = -1;
1025 else
1026 rdev->desc_nr = sb->this_disk.number;
1027
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001028 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001030 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001032 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (!uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001034 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 b, bdevname(refdev->bdev,b2));
1036 goto abort;
1037 }
1038 if (!sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001039 pr_warn("md: %s has same UUID but different superblock to %s\n",
1040 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 goto abort;
1042 }
1043 ev1 = md_event(sb);
1044 ev2 = md_event(refsb);
1045 if (ev1 > ev2)
1046 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001047 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 ret = 0;
1049 }
NeilBrown8190e752009-06-18 08:48:58 +10001050 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001051 /* Limit to 4TB as metadata cannot record more than that.
1052 * (not needed for Linear and RAID0 as metadata doesn't
1053 * record this size)
1054 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001055 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1056 sb->level >= 1)
1057 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
NeilBrown27a7b262011-09-10 17:21:28 +10001059 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001060 /* "this cannot possibly happen" ... */
1061 ret = -EINVAL;
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 abort:
1064 return ret;
1065}
1066
1067/*
1068 * validate_super for 0.90.0
1069 */
NeilBrownfd01b882011-10-11 16:47:53 +11001070static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001073 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001074 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
NeilBrown41158c72005-06-21 17:17:25 -07001076 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001077 clear_bit(Faulty, &rdev->flags);
1078 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001079 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001080 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (mddev->raid_disks == 0) {
1083 mddev->major_version = 0;
1084 mddev->minor_version = sb->minor_version;
1085 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001086 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001087 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 mddev->ctime = sb->ctime;
1089 mddev->utime = sb->utime;
1090 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001091 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 mddev->layout = sb->layout;
1093 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001094 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001095 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001096 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001097 mddev->bitmap_info.space = 0;
1098 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001099 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001100 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001101 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
NeilBrownf6705572006-03-27 01:18:11 -08001103 if (mddev->minor_version >= 91) {
1104 mddev->reshape_position = sb->reshape_position;
1105 mddev->delta_disks = sb->delta_disks;
1106 mddev->new_level = sb->new_level;
1107 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001108 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001109 if (mddev->delta_disks < 0)
1110 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001111 } else {
1112 mddev->reshape_position = MaxSector;
1113 mddev->delta_disks = 0;
1114 mddev->new_level = mddev->level;
1115 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001116 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001117 }
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (sb->state & (1<<MD_SB_CLEAN))
1120 mddev->recovery_cp = MaxSector;
1121 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001122 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 sb->events_lo == sb->cp_events_lo) {
1124 mddev->recovery_cp = sb->recovery_cp;
1125 } else
1126 mddev->recovery_cp = 0;
1127 }
1128
1129 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1130 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1131 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1132 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1133
1134 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001135
1136 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001137 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001138 mddev->bitmap_info.offset =
1139 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001140 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001141 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001142 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001143
NeilBrown41158c72005-06-21 17:17:25 -07001144 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001145 /* Insist on good event counter while assembling, except
1146 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001148 if (sb->disks[rdev->desc_nr].state & (
1149 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001150 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001151 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001152 } else if (mddev->bitmap) {
1153 /* if adding to array with a bitmap, then we can accept an
1154 * older device ... but not too old.
1155 */
NeilBrown41158c72005-06-21 17:17:25 -07001156 if (ev1 < mddev->bitmap->events_cleared)
1157 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001158 if (ev1 < mddev->events)
1159 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001160 } else {
1161 if (ev1 < mddev->events)
1162 /* just a hot-add of a new device, leave raid_disk at -1 */
1163 return 0;
1164 }
NeilBrown41158c72005-06-21 17:17:25 -07001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 desc = sb->disks + rdev->desc_nr;
1168
1169 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001170 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001171 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1172 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001173 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001175 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001176 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1177 /* active but not in sync implies recovery up to
1178 * reshape position. We don't know exactly where
1179 * that is, so set to zero for now */
1180 if (mddev->minor_version >= 91) {
1181 rdev->recovery_offset = 0;
1182 rdev->raid_disk = desc->raid_disk;
1183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001185 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1186 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001187 if (desc->state & (1<<MD_DISK_FAILFAST))
1188 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001189 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001190 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return 0;
1192}
1193
1194/*
1195 * sync_super for 0.90.0
1196 */
NeilBrownfd01b882011-10-11 16:47:53 +11001197static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001200 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 /* make rdev->sb match mddev data..
1204 *
1205 * 1/ zero out disks
1206 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1207 * 3/ any empty disks < next_spare become removed
1208 *
1209 * disks[0] gets initialised to REMOVED because
1210 * we cannot be sure from other fields if it has
1211 * been initialised or not.
1212 */
1213 int i;
1214 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1215
NeilBrown61181562005-09-09 16:24:02 -07001216 rdev->sb_size = MD_SB_BYTES;
1217
Namhyung Kim65a06f062011-07-27 11:00:36 +10001218 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 memset(sb, 0, sizeof(*sb));
1221
1222 sb->md_magic = MD_SB_MAGIC;
1223 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 sb->patch_version = mddev->patch_version;
1225 sb->gvalid_words = 0; /* ignored */
1226 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1227 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1228 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1229 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1230
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001231 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001233 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 sb->raid_disks = mddev->raid_disks;
1235 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001236 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001237 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 sb->state = 0;
1239 sb->events_hi = (mddev->events>>32);
1240 sb->events_lo = (u32)mddev->events;
1241
NeilBrownf6705572006-03-27 01:18:11 -08001242 if (mddev->reshape_position == MaxSector)
1243 sb->minor_version = 90;
1244 else {
1245 sb->minor_version = 91;
1246 sb->reshape_position = mddev->reshape_position;
1247 sb->new_level = mddev->new_level;
1248 sb->delta_disks = mddev->delta_disks;
1249 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001250 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001251 }
1252 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (mddev->in_sync)
1254 {
1255 sb->recovery_cp = mddev->recovery_cp;
1256 sb->cp_events_hi = (mddev->events>>32);
1257 sb->cp_events_lo = (u32)mddev->events;
1258 if (mddev->recovery_cp == MaxSector)
1259 sb->state = (1<< MD_SB_CLEAN);
1260 } else
1261 sb->recovery_cp = 0;
1262
1263 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001264 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
NeilBrownc3d97142009-12-14 12:49:52 +11001266 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001267 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001270 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001272 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001273 int is_active = test_bit(In_sync, &rdev2->flags);
1274
1275 if (rdev2->raid_disk >= 0 &&
1276 sb->minor_version >= 91)
1277 /* we have nowhere to store the recovery_offset,
1278 * but if it is not below the reshape_position,
1279 * we can piggy-back on that.
1280 */
1281 is_active = 1;
1282 if (rdev2->raid_disk < 0 ||
1283 test_bit(Faulty, &rdev2->flags))
1284 is_active = 0;
1285 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001286 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001288 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001289 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 d = &sb->disks[rdev2->desc_nr];
1291 nr_disks++;
1292 d->number = rdev2->desc_nr;
1293 d->major = MAJOR(rdev2->bdev->bd_dev);
1294 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001295 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 d->raid_disk = rdev2->raid_disk;
1297 else
1298 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001299 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001301 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001303 if (test_bit(In_sync, &rdev2->flags))
1304 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 active++;
1306 working++;
1307 } else {
1308 d->state = 0;
1309 spare++;
1310 working++;
1311 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001312 if (test_bit(WriteMostly, &rdev2->flags))
1313 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001314 if (test_bit(FailFast, &rdev2->flags))
1315 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 /* now set the "removed" and "faulty" bits on any missing devices */
1318 for (i=0 ; i < mddev->raid_disks ; i++) {
1319 mdp_disk_t *d = &sb->disks[i];
1320 if (d->state == 0 && d->number == 0) {
1321 d->number = i;
1322 d->raid_disk = i;
1323 d->state = (1<<MD_DISK_REMOVED);
1324 d->state |= (1<<MD_DISK_FAULTY);
1325 failed++;
1326 }
1327 }
1328 sb->nr_disks = nr_disks;
1329 sb->active_disks = active;
1330 sb->working_disks = working;
1331 sb->failed_disks = failed;
1332 sb->spare_disks = spare;
1333
1334 sb->this_disk = sb->disks[rdev->desc_nr];
1335 sb->sb_csum = calc_sb_csum(sb);
1336}
1337
1338/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001339 * rdev_size_change for 0.90.0
1340 */
1341static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001342super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001343{
Andre Noll58c0fed2009-03-31 14:33:13 +11001344 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001345 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001346 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001347 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001348 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001349 if (!num_sectors || num_sectors > rdev->sb_start)
1350 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001351 /* Limit to 4TB as metadata cannot record more than that.
1352 * 4TB == 2^32 KB, or 2*2^32 sectors.
1353 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001354 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1355 rdev->mddev->level >= 1)
1356 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001357 do {
1358 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001359 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001360 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001361 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001362}
1363
NeilBrownc6563a82012-05-21 09:27:00 +10001364static int
1365super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1366{
1367 /* non-zero offset changes not possible with v0.90 */
1368 return new_offset == 0;
1369}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001370
1371/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 * version 1 superblock
1373 */
1374
NeilBrownf72ffdd2014-09-30 14:23:59 +10001375static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001377 __le32 disk_csum;
1378 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 unsigned long long newcsum;
1380 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001381 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 disk_csum = sb->sb_csum;
1384 sb->sb_csum = 0;
1385 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001386 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 newcsum += le32_to_cpu(*isuper++);
1388
1389 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001390 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1393 sb->sb_csum = disk_csum;
1394 return cpu_to_le32(csum);
1395}
1396
NeilBrown3cb03002011-10-11 16:45:26 +11001397static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 struct mdp_superblock_1 *sb;
1400 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001401 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001402 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001404 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 /*
Andre Noll0f420352008-07-11 22:02:23 +10001407 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 * It is always aligned to a 4K boundary and
1409 * depeding on minor_version, it can be:
1410 * 0: At least 8K, but less than 12K, from end of device
1411 * 1: At start of device
1412 * 2: 4K from start of device.
1413 */
1414 switch(minor_version) {
1415 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001416 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001417 sb_start -= 8*2;
1418 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 break;
1420 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001421 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 break;
1423 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001424 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 break;
1426 default:
1427 return -EINVAL;
1428 }
Andre Noll0f420352008-07-11 22:02:23 +10001429 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
NeilBrown0002b272005-09-09 16:23:53 -07001431 /* superblock is rarely larger than 1K, but it can be larger,
1432 * and it is safe to read 4k, so we do that
1433 */
1434 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (ret) return ret;
1436
Namhyung Kim65a06f062011-07-27 11:00:36 +10001437 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1440 sb->major_version != cpu_to_le32(1) ||
1441 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001442 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001443 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return -EINVAL;
1445
1446 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001447 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 bdevname(rdev->bdev,b));
1449 return -EINVAL;
1450 }
1451 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001452 pr_warn("md: data_size too small on %s\n",
1453 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return -EINVAL;
1455 }
NeilBrownc6563a82012-05-21 09:27:00 +10001456 if (sb->pad0 ||
1457 sb->pad3[0] ||
1458 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1459 /* Some padding is non-zero, might be a new feature */
1460 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 rdev->preferred_minor = 0xffff;
1463 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001464 rdev->new_data_offset = rdev->data_offset;
1465 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1466 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1467 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001468 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
NeilBrown0002b272005-09-09 16:23:53 -07001470 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001471 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001472 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001473 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1474
1475 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001476 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001477 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001478 if (minor_version
1479 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1480 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001481
NeilBrown31b65a02006-07-10 04:44:14 -07001482 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1483 rdev->desc_nr = -1;
1484 else
1485 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1486
NeilBrown2699b672011-07-28 11:31:47 +10001487 if (!rdev->bb_page) {
1488 rdev->bb_page = alloc_page(GFP_KERNEL);
1489 if (!rdev->bb_page)
1490 return -ENOMEM;
1491 }
1492 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1493 rdev->badblocks.count == 0) {
1494 /* need to load the bad block list.
1495 * Currently we limit it to one page.
1496 */
1497 s32 offset;
1498 sector_t bb_sector;
1499 u64 *bbp;
1500 int i;
1501 int sectors = le16_to_cpu(sb->bblog_size);
1502 if (sectors > (PAGE_SIZE / 512))
1503 return -EINVAL;
1504 offset = le32_to_cpu(sb->bblog_offset);
1505 if (offset == 0)
1506 return -EINVAL;
1507 bb_sector = (long long)offset;
1508 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001509 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001510 return -EIO;
1511 bbp = (u64 *)page_address(rdev->bb_page);
1512 rdev->badblocks.shift = sb->bblog_shift;
1513 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1514 u64 bb = le64_to_cpu(*bbp);
1515 int count = bb & (0x3ff);
1516 u64 sector = bb >> 10;
1517 sector <<= sb->bblog_shift;
1518 count <<= sb->bblog_shift;
1519 if (bb + 1 == 0)
1520 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001521 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001522 return -EINVAL;
1523 }
NeilBrown486adf72013-04-24 11:42:44 +10001524 } else if (sb->bblog_offset != 0)
1525 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001526
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001527 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001528 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001529 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001531 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1534 sb->level != refsb->level ||
1535 sb->layout != refsb->layout ||
1536 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001537 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 bdevname(rdev->bdev,b),
1539 bdevname(refdev->bdev,b2));
1540 return -EINVAL;
1541 }
1542 ev1 = le64_to_cpu(sb->events);
1543 ev2 = le64_to_cpu(refsb->events);
1544
1545 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001546 ret = 1;
1547 else
1548 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
NeilBrownc6563a82012-05-21 09:27:00 +10001550 if (minor_version) {
1551 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1552 sectors -= rdev->data_offset;
1553 } else
1554 sectors = rdev->sb_start;
1555 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001557 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001558 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
NeilBrownfd01b882011-10-11 16:47:53 +11001561static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001563 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001564 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
NeilBrown41158c72005-06-21 17:17:25 -07001566 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001567 clear_bit(Faulty, &rdev->flags);
1568 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001569 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001570 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (mddev->raid_disks == 0) {
1573 mddev->major_version = 1;
1574 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001575 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001576 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001577 mddev->ctime = le64_to_cpu(sb->ctime);
1578 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001580 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 mddev->layout = le32_to_cpu(sb->layout);
1582 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001583 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001584 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001585 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001586 mddev->bitmap_info.space = 0;
1587 /* Default location for bitmap is 1K after superblock
1588 * using 3K - total of 4K
1589 */
NeilBrownc3d97142009-12-14 12:49:52 +11001590 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001591 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001592 mddev->reshape_backwards = 0;
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1595 memcpy(mddev->uuid, sb->set_uuid, 16);
1596
1597 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001598
NeilBrown71c08052005-09-09 16:23:51 -07001599 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001600 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001601 mddev->bitmap_info.offset =
1602 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001603 /* Metadata doesn't record how much space is available.
1604 * For 1.0, we assume we can use up to the superblock
1605 * if before, else to 4K beyond superblock.
1606 * For others, assume no change is possible.
1607 */
1608 if (mddev->minor_version > 0)
1609 mddev->bitmap_info.space = 0;
1610 else if (mddev->bitmap_info.offset > 0)
1611 mddev->bitmap_info.space =
1612 8 - mddev->bitmap_info.offset;
1613 else
1614 mddev->bitmap_info.space =
1615 -mddev->bitmap_info.offset;
1616 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001617
NeilBrownf6705572006-03-27 01:18:11 -08001618 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1619 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1620 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1621 mddev->new_level = le32_to_cpu(sb->new_level);
1622 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001623 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001624 if (mddev->delta_disks < 0 ||
1625 (mddev->delta_disks == 0 &&
1626 (le32_to_cpu(sb->feature_map)
1627 & MD_FEATURE_RESHAPE_BACKWARDS)))
1628 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001629 } else {
1630 mddev->reshape_position = MaxSector;
1631 mddev->delta_disks = 0;
1632 mddev->new_level = mddev->level;
1633 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001634 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001635 }
1636
Song Liu486b0f72016-08-19 15:34:01 -07001637 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001638 set_bit(MD_HAS_JOURNAL, &mddev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001639 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001640 /* Insist of good event counter while assembling, except for
1641 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001643 if (rdev->desc_nr >= 0 &&
1644 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001645 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1646 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001647 if (ev1 < mddev->events)
1648 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001649 } else if (mddev->bitmap) {
1650 /* If adding to array with a bitmap, then we can accept an
1651 * older device, but not too old.
1652 */
NeilBrown41158c72005-06-21 17:17:25 -07001653 if (ev1 < mddev->bitmap->events_cleared)
1654 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001655 if (ev1 < mddev->events)
1656 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001657 } else {
1658 if (ev1 < mddev->events)
1659 /* just a hot-add of a new device, leave raid_disk at -1 */
1660 return 0;
1661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (mddev->level != LEVEL_MULTIPATH) {
1663 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001664 if (rdev->desc_nr < 0 ||
1665 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001666 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001667 rdev->desc_nr = -1;
1668 } else
1669 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001671 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001673 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001674 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 break;
Song Liubac624f2015-08-13 14:31:55 -07001676 case MD_DISK_ROLE_JOURNAL: /* journal device */
1677 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1678 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001679 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001680 return -EINVAL;
1681 }
1682 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001683 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001684 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001685 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 default:
NeilBrownf4667222013-12-09 12:04:56 +11001687 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001688 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001689 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001690 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001691 if (!(le32_to_cpu(sb->feature_map) &
1692 MD_FEATURE_RECOVERY_BITMAP))
1693 rdev->saved_raid_disk = -1;
1694 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001695 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 rdev->raid_disk = role;
1697 break;
1698 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001699 if (sb->devflags & WriteMostly1)
1700 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001701 if (sb->devflags & FailFast1)
1702 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001703 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1704 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001705 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001706 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return 0;
1709}
1710
NeilBrownfd01b882011-10-11 16:47:53 +11001711static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001714 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 int max_dev, i;
1716 /* make rdev->sb match mddev and rdev data. */
1717
Namhyung Kim65a06f062011-07-27 11:00:36 +10001718 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 sb->feature_map = 0;
1721 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001722 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 memset(sb->pad3, 0, sizeof(sb->pad3));
1724
1725 sb->utime = cpu_to_le64((__u64)mddev->utime);
1726 sb->events = cpu_to_le64(mddev->events);
1727 if (mddev->in_sync)
1728 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001729 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1730 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 else
1732 sb->resync_offset = cpu_to_le64(0);
1733
NeilBrown1c05b4b2006-10-21 10:24:08 -07001734 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001735
NeilBrownf0ca3402006-02-02 14:28:04 -08001736 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001737 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001738 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001739 sb->level = cpu_to_le32(mddev->level);
1740 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11001741 if (test_bit(FailFast, &rdev->flags))
1742 sb->devflags |= FailFast1;
1743 else
1744 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08001745
NeilBrownaeb9b2112011-08-25 14:43:08 +10001746 if (test_bit(WriteMostly, &rdev->flags))
1747 sb->devflags |= WriteMostly1;
1748 else
1749 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001750 sb->data_offset = cpu_to_le64(rdev->data_offset);
1751 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001752
NeilBrownc3d97142009-12-14 12:49:52 +11001753 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1754 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001755 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001756 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001757
Shaohua Lif2076e72015-10-08 21:54:12 -07001758 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001759 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001760 sb->feature_map |=
1761 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1762 sb->recovery_offset =
1763 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001764 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1765 sb->feature_map |=
1766 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001767 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001768 /* Note: recovery_offset and journal_tail share space */
1769 if (test_bit(Journal, &rdev->flags))
1770 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001771 if (test_bit(Replacement, &rdev->flags))
1772 sb->feature_map |=
1773 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001774
NeilBrownf6705572006-03-27 01:18:11 -08001775 if (mddev->reshape_position != MaxSector) {
1776 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1777 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1778 sb->new_layout = cpu_to_le32(mddev->new_layout);
1779 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1780 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001781 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001782 if (mddev->delta_disks == 0 &&
1783 mddev->reshape_backwards)
1784 sb->feature_map
1785 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001786 if (rdev->new_data_offset != rdev->data_offset) {
1787 sb->feature_map
1788 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1789 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1790 - rdev->data_offset));
1791 }
NeilBrownf6705572006-03-27 01:18:11 -08001792 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001793
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001794 if (mddev_is_clustered(mddev))
1795 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1796
NeilBrown2699b672011-07-28 11:31:47 +10001797 if (rdev->badblocks.count == 0)
1798 /* Nothing to do for bad blocks*/ ;
1799 else if (sb->bblog_offset == 0)
1800 /* Cannot record bad blocks on this device */
1801 md_error(mddev, rdev);
1802 else {
1803 struct badblocks *bb = &rdev->badblocks;
1804 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1805 u64 *p = bb->page;
1806 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1807 if (bb->changed) {
1808 unsigned seq;
1809
1810retry:
1811 seq = read_seqbegin(&bb->lock);
1812
1813 memset(bbp, 0xff, PAGE_SIZE);
1814
1815 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001816 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001817 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1818 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001819 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001820 }
NeilBrownd0962932012-03-19 12:46:41 +11001821 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001822 if (read_seqretry(&bb->lock, seq))
1823 goto retry;
1824
1825 bb->sector = (rdev->sb_start +
1826 (int)le32_to_cpu(sb->bblog_offset));
1827 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001828 }
1829 }
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001832 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (rdev2->desc_nr+1 > max_dev)
1834 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001835
NeilBrown70471da2009-08-03 10:59:57 +10001836 if (max_dev > le32_to_cpu(sb->max_dev)) {
1837 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001838 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001839 rdev->sb_size = max_dev * 2 + 256;
1840 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1841 if (rdev->sb_size & bmask)
1842 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001843 } else
1844 max_dev = le32_to_cpu(sb->max_dev);
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 for (i=0; i<max_dev;i++)
Song Liuc4d4c912015-08-13 14:31:54 -07001847 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001848
Song Liua97b7892015-10-08 21:54:09 -07001849 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1850 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
NeilBrowndafb20f2012-03-19 12:46:39 +11001852 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001854 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001855 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001856 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001858 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001859 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001860 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001861 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 else
Song Liuc4d4c912015-08-13 14:31:54 -07001863 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 sb->sb_csum = calc_sb_1_csum(sb);
1867}
1868
Chris Webb0cd17fe2008-06-28 08:31:46 +10001869static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001870super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001871{
1872 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001873 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001874 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001875 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001876 if (rdev->data_offset != rdev->new_data_offset)
1877 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001878 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001879 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001880 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001881 max_sectors -= rdev->data_offset;
1882 if (!num_sectors || num_sectors > max_sectors)
1883 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001884 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001885 /* minor version 0 with bitmap we can't move */
1886 return 0;
1887 } else {
1888 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001889 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001890 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001891 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001892 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001893 if (!num_sectors || num_sectors > max_sectors)
1894 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001895 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001896 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001897 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001898 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001899 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001900 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11001901 do {
1902 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1903 rdev->sb_page);
1904 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001905 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001906
1907}
1908
1909static int
1910super_1_allow_new_offset(struct md_rdev *rdev,
1911 unsigned long long new_offset)
1912{
1913 /* All necessary checks on new >= old have been done */
1914 struct bitmap *bitmap;
1915 if (new_offset >= rdev->data_offset)
1916 return 1;
1917
1918 /* with 1.0 metadata, there is no metadata to tread on
1919 * so we can always move back */
1920 if (rdev->mddev->minor_version == 0)
1921 return 1;
1922
1923 /* otherwise we must be sure not to step on
1924 * any metadata, so stay:
1925 * 36K beyond start of superblock
1926 * beyond end of badblocks
1927 * beyond write-intent bitmap
1928 */
1929 if (rdev->sb_start + (32+4)*2 > new_offset)
1930 return 0;
1931 bitmap = rdev->mddev->bitmap;
1932 if (bitmap && !rdev->mddev->bitmap_info.file &&
1933 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001934 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001935 return 0;
1936 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1937 return 0;
1938
1939 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001940}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Adrian Bunk75c96f82005-05-05 16:16:09 -07001942static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 [0] = {
1944 .name = "0.90.0",
1945 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001946 .load_super = super_90_load,
1947 .validate_super = super_90_validate,
1948 .sync_super = super_90_sync,
1949 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001950 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 },
1952 [1] = {
1953 .name = "md-1",
1954 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001955 .load_super = super_1_load,
1956 .validate_super = super_1_validate,
1957 .sync_super = super_1_sync,
1958 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001959 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 },
1961};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
NeilBrownfd01b882011-10-11 16:47:53 +11001963static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001964{
1965 if (mddev->sync_super) {
1966 mddev->sync_super(mddev, rdev);
1967 return;
1968 }
1969
1970 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1971
1972 super_types[mddev->major_version].sync_super(mddev, rdev);
1973}
1974
NeilBrownfd01b882011-10-11 16:47:53 +11001975static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
NeilBrown3cb03002011-10-11 16:45:26 +11001977 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
NeilBrown4b809912008-07-21 17:05:25 +10001979 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07001980 rdev_for_each_rcu(rdev, mddev1) {
1981 if (test_bit(Faulty, &rdev->flags) ||
1982 test_bit(Journal, &rdev->flags) ||
1983 rdev->raid_disk == -1)
1984 continue;
1985 rdev_for_each_rcu(rdev2, mddev2) {
1986 if (test_bit(Faulty, &rdev2->flags) ||
1987 test_bit(Journal, &rdev2->flags) ||
1988 rdev2->raid_disk == -1)
1989 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001990 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001991 rdev2->bdev->bd_contains) {
1992 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001993 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001994 }
Song Liu0b020e82015-09-03 23:00:35 -07001995 }
1996 }
NeilBrown4b809912008-07-21 17:05:25 +10001997 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return 0;
1999}
2000
2001static LIST_HEAD(pending_raid_disks);
2002
Andre Nollac5e7112009-08-03 10:59:47 +10002003/*
2004 * Try to register data integrity profile for an mddev
2005 *
2006 * This is called when an array is started and after a disk has been kicked
2007 * from the array. It only succeeds if all working and active component devices
2008 * are integrity capable with matching profiles.
2009 */
NeilBrownfd01b882011-10-11 16:47:53 +11002010int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002011{
NeilBrown3cb03002011-10-11 16:45:26 +11002012 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002013
Andre Nollac5e7112009-08-03 10:59:47 +10002014 if (list_empty(&mddev->disks))
2015 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002016 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2017 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002018 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002019 /* skip spares and non-functional disks */
2020 if (test_bit(Faulty, &rdev->flags))
2021 continue;
2022 if (rdev->raid_disk < 0)
2023 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002024 if (!reference) {
2025 /* Use the first rdev as the reference */
2026 reference = rdev;
2027 continue;
2028 }
2029 /* does this rdev's profile match the reference profile? */
2030 if (blk_integrity_compare(reference->bdev->bd_disk,
2031 rdev->bdev->bd_disk) < 0)
2032 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002033 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002034 if (!reference || !bdev_get_integrity(reference->bdev))
2035 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002036 /*
2037 * All component devices are integrity capable and have matching
2038 * profiles, register the common profile for the md device.
2039 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002040 blk_integrity_register(mddev->gendisk,
2041 bdev_get_integrity(reference->bdev));
2042
NeilBrown9d487392016-11-02 14:16:49 +11002043 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Martin K. Petersena91a2782011-03-17 11:11:05 +01002044 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002045 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002046 mdname(mddev));
2047 return -EINVAL;
2048 }
Andre Nollac5e7112009-08-03 10:59:47 +10002049 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002050}
Andre Nollac5e7112009-08-03 10:59:47 +10002051EXPORT_SYMBOL(md_integrity_register);
2052
Dan Williams1501efa2016-01-13 16:00:07 -08002053/*
2054 * Attempt to add an rdev, but only if it is consistent with the current
2055 * integrity profile
2056 */
2057int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002058{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002059 struct blk_integrity *bi_rdev;
2060 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002061 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002062
2063 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002064 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002065
2066 bi_rdev = bdev_get_integrity(rdev->bdev);
2067 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002068
2069 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002070 return 0;
2071
2072 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002073 pr_err("%s: incompatible integrity profile for %s\n",
2074 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002075 return -ENXIO;
2076 }
2077
2078 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002079}
2080EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002081
NeilBrownf72ffdd2014-09-30 14:23:59 +10002082static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002084 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002085 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002086 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Dan Williams11e2ede2008-04-30 00:52:32 -07002088 /* prevent duplicates */
2089 if (find_rdev(mddev, rdev->bdev->bd_dev))
2090 return -EEXIST;
2091
Andre Nolldd8ac332009-03-31 14:33:13 +11002092 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002093 if (!test_bit(Journal, &rdev->flags) &&
2094 rdev->sectors &&
2095 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002096 if (mddev->pers) {
2097 /* Cannot change size, so fail
2098 * If mddev->level <= 0, then we don't care
2099 * about aligning sizes (e.g. linear)
2100 */
2101 if (mddev->level > 0)
2102 return -ENOSPC;
2103 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002104 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 /* Verify rdev->desc_nr is unique.
2108 * If it is -1, assign a free number, else
2109 * check number is not in use
2110 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002111 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (rdev->desc_nr < 0) {
2113 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002114 if (mddev->pers)
2115 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002116 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 choice++;
2118 rdev->desc_nr = choice;
2119 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002120 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002121 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002125 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002126 if (!test_bit(Journal, &rdev->flags) &&
2127 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002128 pr_warn("md: %s: array is limited to %d devices\n",
2129 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002130 return -EBUSY;
2131 }
NeilBrown19133a42005-11-08 21:39:35 -08002132 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002133 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002136 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002137
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002138 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002139 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002140
Tejun Heo0762b8b2008-08-25 19:56:12 +09002141 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002142 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2143 /* failure here is OK */;
2144 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002145
NeilBrown4b809912008-07-21 17:05:25 +10002146 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002147 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002148
2149 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002150 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002153
2154 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002155 pr_warn("md: failed to register dev-%s for %s\n",
2156 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002157 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
NeilBrown177a99b2008-02-06 01:39:56 -08002160static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002161{
NeilBrown3cb03002011-10-11 16:45:26 +11002162 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002163 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002164 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002165}
2166
NeilBrownf72ffdd2014-09-30 14:23:59 +10002167static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002170
Tejun Heo49731ba2011-01-14 18:43:57 +01002171 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002172 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002173 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002175 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002176 sysfs_put(rdev->sysfs_state);
2177 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002178 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002179 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002180 * writing to 'remove' to "dev/state". We also need
2181 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002182 */
NeilBrown4b809912008-07-21 17:05:25 +10002183 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002184 INIT_WORK(&rdev->del_work, md_delayed_delete);
2185 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002186 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
2189/*
2190 * prevent the device from being mounted, repartitioned or
2191 * otherwise reused by a RAID array (or any other kernel
2192 * subsystem), by bd_claiming the device.
2193 */
NeilBrown3cb03002011-10-11 16:45:26 +11002194static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
2196 int err = 0;
2197 struct block_device *bdev;
2198 char b[BDEVNAME_SIZE];
2199
Tejun Heod4d77622010-11-13 11:55:18 +01002200 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002201 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002203 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 return PTR_ERR(bdev);
2205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 rdev->bdev = bdev;
2207 return err;
2208}
2209
NeilBrown3cb03002011-10-11 16:45:26 +11002210static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 struct block_device *bdev = rdev->bdev;
2213 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002214 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
2217void md_autodetect_dev(dev_t dev);
2218
NeilBrownf72ffdd2014-09-30 14:23:59 +10002219static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
2221 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002222
NeilBrown9d487392016-11-02 14:16:49 +11002223 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002224 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002226 if (test_bit(AutoDetected, &rdev->flags))
2227 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228#endif
2229 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002230 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002233void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
2235 unbind_rdev_from_array(rdev);
2236 export_rdev(rdev);
2237}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002238EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
NeilBrownfd01b882011-10-11 16:47:53 +11002240static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
NeilBrown0638bb02014-09-25 17:43:47 +10002242 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
NeilBrown0638bb02014-09-25 17:43:47 +10002244 while (!list_empty(&mddev->disks)) {
2245 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2246 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002247 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 mddev->raid_disks = 0;
2250 mddev->major_version = 0;
2251}
2252
NeilBrownf72ffdd2014-09-30 14:23:59 +10002253static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
NeilBrown42543762006-06-26 00:27:57 -07002255 /* Update each superblock (in-memory image), but
2256 * if we are allowed to, skip spares which already
2257 * have the right event counter, or have one earlier
2258 * (which would mean they aren't being marked as dirty
2259 * with the rest of the array)
2260 */
NeilBrown3cb03002011-10-11 16:45:26 +11002261 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002262 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002263 if (rdev->sb_events == mddev->events ||
2264 (nospares &&
2265 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002266 rdev->sb_events+1 == mddev->events)) {
2267 /* Don't update this superblock */
2268 rdev->sb_loaded = 2;
2269 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002270 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002271 rdev->sb_loaded = 1;
2272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274}
2275
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002276static bool does_sb_need_changing(struct mddev *mddev)
2277{
2278 struct md_rdev *rdev;
2279 struct mdp_superblock_1 *sb;
2280 int role;
2281
2282 /* Find a good rdev */
2283 rdev_for_each(rdev, mddev)
2284 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2285 break;
2286
2287 /* No good device found. */
2288 if (!rdev)
2289 return false;
2290
2291 sb = page_address(rdev->sb_page);
2292 /* Check if a device has become faulty or a spare become active */
2293 rdev_for_each(rdev, mddev) {
2294 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2295 /* Device activated? */
2296 if (role == 0xffff && rdev->raid_disk >=0 &&
2297 !test_bit(Faulty, &rdev->flags))
2298 return true;
2299 /* Device turned faulty? */
2300 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2301 return true;
2302 }
2303
2304 /* Check if any mddev parameters have changed */
2305 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2306 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002307 (mddev->layout != le64_to_cpu(sb->layout)) ||
2308 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2309 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2310 return true;
2311
2312 return false;
2313}
2314
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002315void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
NeilBrown3cb03002011-10-11 16:45:26 +11002317 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002318 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002319 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002320 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002321 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
NeilBrownd87f0642013-04-24 11:42:40 +10002323 if (mddev->ro) {
2324 if (force_change)
2325 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2326 return;
2327 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002328
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002329repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002330 if (mddev_is_clustered(mddev)) {
2331 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2332 force_change = 1;
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002333 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2334 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002335 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002336 /* Has someone else has updated the sb */
2337 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002338 if (ret == 0)
2339 md_cluster_ops->metadata_update_cancel(mddev);
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002340 bit_clear_unless(&mddev->flags, BIT(MD_CHANGE_PENDING),
2341 BIT(MD_CHANGE_DEVS) |
2342 BIT(MD_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002343 return;
2344 }
2345 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002346
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002347 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002348 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002349 if (rdev->raid_disk >= 0 &&
2350 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002351 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002352 !test_bit(In_sync, &rdev->flags) &&
2353 mddev->curr_resync_completed > rdev->recovery_offset)
2354 rdev->recovery_offset = mddev->curr_resync_completed;
2355
NeilBrownf72ffdd2014-09-30 14:23:59 +10002356 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002357 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002358 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002359 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002360 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002361 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002362 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002363 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002364 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002365 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002366 md_error(mddev, rdev);
2367 }
2368 clear_bit(Blocked, &rdev->flags);
2369 clear_bit(BlockedBadBlocks, &rdev->flags);
2370 wake_up(&rdev->blocked_wait);
2371 }
2372 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002373 wake_up(&mddev->sb_wait);
2374 return;
2375 }
2376
NeilBrown85572d72014-12-15 12:56:56 +11002377 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002378
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002379 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002380
NeilBrown850b2b42006-10-03 01:15:46 -07002381 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2382 force_change = 1;
2383 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2384 /* just a clean<-> dirty transition, possibly leave spares alone,
2385 * though if events isn't the right even/odd, we will have to do
2386 * spares after all
2387 */
2388 nospares = 1;
2389 if (force_change)
2390 nospares = 0;
2391 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002392 /* If the array is degraded, then skipping spares is both
2393 * dangerous and fairly pointless.
2394 * Dangerous because a device that was removed from the array
2395 * might have a event_count that still looks up-to-date,
2396 * so it can be re-added without a resync.
2397 * Pointless because if there are any spares to skip,
2398 * then a recovery will happen and soon that array won't
2399 * be degraded any more and the spare can go back to sleep then.
2400 */
NeilBrown850b2b42006-10-03 01:15:46 -07002401 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002402
NeilBrown06d91a52005-06-21 17:17:12 -07002403 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002404
2405 /* If this is just a dirty<->clean transition, and the array is clean
2406 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002407 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002408 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002409 && mddev->can_decrease_events
2410 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002411 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002412 mddev->can_decrease_events = 0;
2413 } else {
NeilBrown42543762006-06-26 00:27:57 -07002414 /* otherwise we have to go forward and ... */
2415 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002416 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
NeilBrown403df472014-09-30 15:52:29 +10002419 /*
2420 * This 64-bit counter should never wrap.
2421 * Either we are in around ~1 trillion A.C., assuming
2422 * 1 reboot per second, or we have a bug...
2423 */
2424 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002425
NeilBrowndafb20f2012-03-19 12:46:39 +11002426 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002427 if (rdev->badblocks.changed)
2428 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002429 if (test_bit(Faulty, &rdev->flags))
2430 set_bit(FaultRecorded, &rdev->flags);
2431 }
NeilBrown2699b672011-07-28 11:31:47 +10002432
NeilBrowne6910632008-02-06 01:39:51 -08002433 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002434 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
NeilBrown36a4e1f2011-10-07 14:23:17 +11002436 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2437 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Shaohua Li504634f2016-11-18 09:44:08 -08002439 if (mddev->queue)
2440 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002441rewrite:
NeilBrown4ad13662007-07-17 04:06:13 -07002442 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002443 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002445
NeilBrown42543762006-06-26 00:27:57 -07002446 if (rdev->sb_loaded != 1)
2447 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
NeilBrownf4667222013-12-09 12:04:56 +11002449 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002450 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002451 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002452 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002453 pr_debug("md: (write) %s's sb offset: %llu\n",
2454 bdevname(rdev->bdev, b),
2455 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002456 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002457 if (rdev->badblocks.size) {
2458 md_super_write(mddev, rdev,
2459 rdev->badblocks.sector,
2460 rdev->badblocks.size << 9,
2461 rdev->bb_page);
2462 rdev->badblocks.size = 0;
2463 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002464
NeilBrownf4667222013-12-09 12:04:56 +11002465 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002466 pr_debug("md: %s (skipping faulty)\n",
2467 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002468
NeilBrown7bfa19f2005-06-21 17:17:28 -07002469 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 /* only need to write one superblock... */
2471 break;
2472 }
NeilBrown46533ff2016-11-18 16:16:11 +11002473 if (md_super_wait(mddev) < 0)
2474 goto rewrite;
NeilBrown850b2b42006-10-03 01:15:46 -07002475 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002476
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002477 if (mddev_is_clustered(mddev) && ret == 0)
2478 md_cluster_ops->metadata_update_finish(mddev);
2479
NeilBrown850b2b42006-10-03 01:15:46 -07002480 if (mddev->in_sync != sync_req ||
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002481 !bit_clear_unless(&mddev->flags, BIT(MD_CHANGE_PENDING),
2482 BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002483 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002484 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002485 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002486 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2487 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002488
NeilBrowndafb20f2012-03-19 12:46:39 +11002489 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002490 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2491 clear_bit(Blocked, &rdev->flags);
2492
2493 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002494 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002495 clear_bit(BlockedBadBlocks, &rdev->flags);
2496 wake_up(&rdev->blocked_wait);
2497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002499EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002501static int add_bound_rdev(struct md_rdev *rdev)
2502{
2503 struct mddev *mddev = rdev->mddev;
2504 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002505 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002506
Shaohua Li87d4d912016-01-06 14:37:14 -08002507 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002508 /* If there is hot_add_disk but no hot_remove_disk
2509 * then added disks for geometry changes,
2510 * and should be added immediately.
2511 */
2512 super_types[mddev->major_version].
2513 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002514 if (add_journal)
2515 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002516 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002517 if (add_journal)
2518 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002519 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002520 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002521 return err;
2522 }
2523 }
2524 sysfs_notify_dirent_safe(rdev->sysfs_state);
2525
2526 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2527 if (mddev->degraded)
2528 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2529 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2530 md_new_event(mddev);
2531 md_wakeup_thread(mddev->thread);
2532 return 0;
2533}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Andre Noll7f6ce762008-03-23 18:34:54 +01002535/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002536 * We want to accept with case. For this we use cmd_match.
2537 */
2538static int cmd_match(const char *cmd, const char *str)
2539{
2540 /* See if cmd, written into a sysfs file, matches
2541 * str. They must either be the same, or cmd can
2542 * have a trailing newline
2543 */
2544 while (*cmd && *str && *cmd == *str) {
2545 cmd++;
2546 str++;
2547 }
2548 if (*cmd == '\n')
2549 cmd++;
2550 if (*str || *cmd)
2551 return 0;
2552 return 1;
2553}
2554
NeilBrown86e6ffd2005-11-08 21:39:24 -08002555struct rdev_sysfs_entry {
2556 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002557 ssize_t (*show)(struct md_rdev *, char *);
2558 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002559};
2560
2561static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002562state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002563{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002564 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002565 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002566 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002567
NeilBrown758bfc82014-12-15 12:56:59 +11002568 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002569 (!test_bit(ExternalBbl, &flags) &&
2570 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002571 len += sprintf(page+len, "faulty%s", sep);
2572 if (test_bit(In_sync, &flags))
2573 len += sprintf(page+len, "in_sync%s", sep);
2574 if (test_bit(Journal, &flags))
2575 len += sprintf(page+len, "journal%s", sep);
2576 if (test_bit(WriteMostly, &flags))
2577 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002578 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002579 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002580 && !test_bit(Faulty, &flags)))
2581 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002582 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002583 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002584 !test_bit(In_sync, &flags))
2585 len += sprintf(page+len, "spare%s", sep);
2586 if (test_bit(WriteErrorSeen, &flags))
2587 len += sprintf(page+len, "write_error%s", sep);
2588 if (test_bit(WantReplacement, &flags))
2589 len += sprintf(page+len, "want_replacement%s", sep);
2590 if (test_bit(Replacement, &flags))
2591 len += sprintf(page+len, "replacement%s", sep);
2592 if (test_bit(ExternalBbl, &flags))
2593 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002594 if (test_bit(FailFast, &flags))
2595 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002596
2597 if (len)
2598 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002599
NeilBrown86e6ffd2005-11-08 21:39:24 -08002600 return len+sprintf(page+len, "\n");
2601}
2602
NeilBrown45dc2de2006-06-26 00:27:58 -07002603static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002604state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002605{
2606 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002607 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002608 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002609 * writemostly - sets write_mostly
2610 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002611 * blocked - sets the Blocked flags
2612 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002613 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002614 * -insync - clear Insync for a device with a slot assigned,
2615 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002616 * write_error - sets WriteErrorSeen
2617 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002618 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002619 */
2620 int err = -EINVAL;
2621 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2622 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002623 if (test_bit(Faulty, &rdev->flags))
2624 err = 0;
2625 else
2626 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002627 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002628 if (rdev->mddev->pers) {
2629 clear_bit(Blocked, &rdev->flags);
2630 remove_and_add_spares(rdev->mddev, rdev);
2631 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002632 if (rdev->raid_disk >= 0)
2633 err = -EBUSY;
2634 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002635 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002636 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002637 if (mddev_is_clustered(mddev))
2638 err = md_cluster_ops->remove_disk(mddev, rdev);
2639
2640 if (err == 0) {
2641 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002642 if (mddev->pers) {
2643 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2644 md_wakeup_thread(mddev->thread);
2645 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002646 md_new_event(mddev);
2647 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002648 }
NeilBrownf6556752006-06-26 00:28:01 -07002649 } else if (cmd_match(buf, "writemostly")) {
2650 set_bit(WriteMostly, &rdev->flags);
2651 err = 0;
2652 } else if (cmd_match(buf, "-writemostly")) {
2653 clear_bit(WriteMostly, &rdev->flags);
2654 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002655 } else if (cmd_match(buf, "blocked")) {
2656 set_bit(Blocked, &rdev->flags);
2657 err = 0;
2658 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002659 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002660 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002661 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002662 /* metadata handler doesn't understand badblocks,
2663 * so we need to fail the device
2664 */
2665 md_error(rdev->mddev, rdev);
2666 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002667 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002668 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002669 wake_up(&rdev->blocked_wait);
2670 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2671 md_wakeup_thread(rdev->mddev->thread);
2672
2673 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002674 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2675 set_bit(In_sync, &rdev->flags);
2676 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11002677 } else if (cmd_match(buf, "failfast")) {
2678 set_bit(FailFast, &rdev->flags);
2679 err = 0;
2680 } else if (cmd_match(buf, "-failfast")) {
2681 clear_bit(FailFast, &rdev->flags);
2682 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002683 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2684 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002685 if (rdev->mddev->pers == NULL) {
2686 clear_bit(In_sync, &rdev->flags);
2687 rdev->saved_raid_disk = rdev->raid_disk;
2688 rdev->raid_disk = -1;
2689 err = 0;
2690 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002691 } else if (cmd_match(buf, "write_error")) {
2692 set_bit(WriteErrorSeen, &rdev->flags);
2693 err = 0;
2694 } else if (cmd_match(buf, "-write_error")) {
2695 clear_bit(WriteErrorSeen, &rdev->flags);
2696 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002697 } else if (cmd_match(buf, "want_replacement")) {
2698 /* Any non-spare device that is not a replacement can
2699 * become want_replacement at any time, but we then need to
2700 * check if recovery is needed.
2701 */
2702 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002703 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002704 !test_bit(Replacement, &rdev->flags))
2705 set_bit(WantReplacement, &rdev->flags);
2706 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2707 md_wakeup_thread(rdev->mddev->thread);
2708 err = 0;
2709 } else if (cmd_match(buf, "-want_replacement")) {
2710 /* Clearing 'want_replacement' is always allowed.
2711 * Once replacements starts it is too late though.
2712 */
2713 err = 0;
2714 clear_bit(WantReplacement, &rdev->flags);
2715 } else if (cmd_match(buf, "replacement")) {
2716 /* Can only set a device as a replacement when array has not
2717 * yet been started. Once running, replacement is automatic
2718 * from spares, or by assigning 'slot'.
2719 */
2720 if (rdev->mddev->pers)
2721 err = -EBUSY;
2722 else {
2723 set_bit(Replacement, &rdev->flags);
2724 err = 0;
2725 }
2726 } else if (cmd_match(buf, "-replacement")) {
2727 /* Similarly, can only clear Replacement before start */
2728 if (rdev->mddev->pers)
2729 err = -EBUSY;
2730 else {
2731 clear_bit(Replacement, &rdev->flags);
2732 err = 0;
2733 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002734 } else if (cmd_match(buf, "re-add")) {
2735 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002736 /* clear_bit is performed _after_ all the devices
2737 * have their local Faulty bit cleared. If any writes
2738 * happen in the meantime in the local node, they
2739 * will land in the local bitmap, which will be synced
2740 * by this node eventually
2741 */
2742 if (!mddev_is_clustered(rdev->mddev) ||
2743 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2744 clear_bit(Faulty, &rdev->flags);
2745 err = add_bound_rdev(rdev);
2746 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002747 } else
2748 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002749 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2750 set_bit(ExternalBbl, &rdev->flags);
2751 rdev->badblocks.shift = 0;
2752 err = 0;
2753 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2754 clear_bit(ExternalBbl, &rdev->flags);
2755 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002756 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002757 if (!err)
2758 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002759 return err ? err : len;
2760}
NeilBrown80ca3a42006-07-10 04:44:18 -07002761static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002762__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002763
2764static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002765errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002766{
2767 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2768}
2769
2770static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002771errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002772{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002773 unsigned int n;
2774 int rv;
2775
2776 rv = kstrtouint(buf, 10, &n);
2777 if (rv < 0)
2778 return rv;
2779 atomic_set(&rdev->corrected_errors, n);
2780 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002781}
2782static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002783__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002784
NeilBrown014236d2006-01-06 00:20:55 -08002785static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002786slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002787{
Shaohua Lif2076e72015-10-08 21:54:12 -07002788 if (test_bit(Journal, &rdev->flags))
2789 return sprintf(page, "journal\n");
2790 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002791 return sprintf(page, "none\n");
2792 else
2793 return sprintf(page, "%d\n", rdev->raid_disk);
2794}
2795
2796static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002797slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002798{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002799 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002800 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002801
Shaohua Lif2076e72015-10-08 21:54:12 -07002802 if (test_bit(Journal, &rdev->flags))
2803 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002804 if (strncmp(buf, "none", 4)==0)
2805 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002806 else {
2807 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2808 if (err < 0)
2809 return err;
2810 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002811 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002812 /* Setting 'slot' on an active array requires also
2813 * updating the 'rd%d' link, and communicating
2814 * with the personality with ->hot_*_disk.
2815 * For now we only support removing
2816 * failed/spare devices. This normally happens automatically,
2817 * but not when the metadata is externally managed.
2818 */
NeilBrownc303da62008-02-06 01:39:51 -08002819 if (rdev->raid_disk == -1)
2820 return -EEXIST;
2821 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002822 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002823 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002824 clear_bit(Blocked, &rdev->flags);
2825 remove_and_add_spares(rdev->mddev, rdev);
2826 if (rdev->raid_disk >= 0)
2827 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002828 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2829 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002830 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002831 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002832 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002833 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002834 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002835
2836 if (rdev->raid_disk != -1)
2837 return -EBUSY;
2838
NeilBrownc6751b22011-02-02 11:57:13 +11002839 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2840 return -EBUSY;
2841
Neil Brown6c2fce22008-06-28 08:31:31 +10002842 if (rdev->mddev->pers->hot_add_disk == NULL)
2843 return -EINVAL;
2844
NeilBrownba1b41b2011-01-14 09:14:34 +11002845 if (slot >= rdev->mddev->raid_disks &&
2846 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2847 return -ENOSPC;
2848
Neil Brown6c2fce22008-06-28 08:31:31 +10002849 rdev->raid_disk = slot;
2850 if (test_bit(In_sync, &rdev->flags))
2851 rdev->saved_raid_disk = slot;
2852 else
2853 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002854 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002855 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002856 err = rdev->mddev->pers->
2857 hot_add_disk(rdev->mddev, rdev);
2858 if (err) {
2859 rdev->raid_disk = -1;
2860 return err;
2861 } else
2862 sysfs_notify_dirent_safe(rdev->sysfs_state);
2863 if (sysfs_link_rdev(rdev->mddev, rdev))
2864 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002865 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002866 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002867 if (slot >= rdev->mddev->raid_disks &&
2868 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002869 return -ENOSPC;
2870 rdev->raid_disk = slot;
2871 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002872 clear_bit(Faulty, &rdev->flags);
2873 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002874 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002875 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002876 }
NeilBrown014236d2006-01-06 00:20:55 -08002877 return len;
2878}
2879
NeilBrown014236d2006-01-06 00:20:55 -08002880static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002881__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002882
NeilBrown93c8cad2006-01-06 00:20:56 -08002883static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002884offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002885{
Andrew Morton6961ece2006-01-06 00:20:59 -08002886 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002887}
2888
2889static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002890offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002891{
NeilBrownc6563a82012-05-21 09:27:00 +10002892 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002893 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002894 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002895 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002896 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002897 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002898 /* Must set offset before size, so overlap checks
2899 * can be sane */
2900 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002901 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002902 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002903 return len;
2904}
2905
2906static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002907__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002908
NeilBrownc6563a82012-05-21 09:27:00 +10002909static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2910{
2911 return sprintf(page, "%llu\n",
2912 (unsigned long long)rdev->new_data_offset);
2913}
2914
2915static ssize_t new_offset_store(struct md_rdev *rdev,
2916 const char *buf, size_t len)
2917{
2918 unsigned long long new_offset;
2919 struct mddev *mddev = rdev->mddev;
2920
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002921 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002922 return -EINVAL;
2923
NeilBrownf851b602014-12-11 10:02:10 +11002924 if (mddev->sync_thread ||
2925 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002926 return -EBUSY;
2927 if (new_offset == rdev->data_offset)
2928 /* reset is always permitted */
2929 ;
2930 else if (new_offset > rdev->data_offset) {
2931 /* must not push array size beyond rdev_sectors */
2932 if (new_offset - rdev->data_offset
2933 + mddev->dev_sectors > rdev->sectors)
2934 return -E2BIG;
2935 }
2936 /* Metadata worries about other space details. */
2937
2938 /* decreasing the offset is inconsistent with a backwards
2939 * reshape.
2940 */
2941 if (new_offset < rdev->data_offset &&
2942 mddev->reshape_backwards)
2943 return -EINVAL;
2944 /* Increasing offset is inconsistent with forwards
2945 * reshape. reshape_direction should be set to
2946 * 'backwards' first.
2947 */
2948 if (new_offset > rdev->data_offset &&
2949 !mddev->reshape_backwards)
2950 return -EINVAL;
2951
2952 if (mddev->pers && mddev->persistent &&
2953 !super_types[mddev->major_version]
2954 .allow_new_offset(rdev, new_offset))
2955 return -E2BIG;
2956 rdev->new_data_offset = new_offset;
2957 if (new_offset > rdev->data_offset)
2958 mddev->reshape_backwards = 1;
2959 else if (new_offset < rdev->data_offset)
2960 mddev->reshape_backwards = 0;
2961
2962 return len;
2963}
2964static struct rdev_sysfs_entry rdev_new_offset =
2965__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2966
NeilBrown83303b62006-01-06 00:21:06 -08002967static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002968rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002969{
Andre Nolldd8ac332009-03-31 14:33:13 +11002970 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002971}
2972
NeilBrownc5d79ad2008-02-06 01:39:54 -08002973static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2974{
2975 /* check if two start/length pairs overlap */
2976 if (s1+l1 <= s2)
2977 return 0;
2978 if (s2+l2 <= s1)
2979 return 0;
2980 return 1;
2981}
2982
Dan Williamsb522adc2009-03-31 15:00:31 +11002983static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2984{
2985 unsigned long long blocks;
2986 sector_t new;
2987
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002988 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002989 return -EINVAL;
2990
2991 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2992 return -EINVAL; /* sector conversion overflow */
2993
2994 new = blocks * 2;
2995 if (new != blocks * 2)
2996 return -EINVAL; /* unsigned long long to sector_t overflow */
2997
2998 *sectors = new;
2999 return 0;
3000}
3001
NeilBrown83303b62006-01-06 00:21:06 -08003002static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003003rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003004{
NeilBrownfd01b882011-10-11 16:47:53 +11003005 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003006 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003007 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003008
Shaohua Lif2076e72015-10-08 21:54:12 -07003009 if (test_bit(Journal, &rdev->flags))
3010 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003011 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003012 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003013 if (rdev->data_offset != rdev->new_data_offset)
3014 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003015 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003016 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003017 sectors = super_types[my_mddev->major_version].
3018 rdev_size_change(rdev, sectors);
3019 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003020 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003021 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003022 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003023 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003024 if (!my_mddev->pers->resize)
3025 /* Cannot change size for RAID0 or Linear etc */
3026 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003027 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003028 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003029 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003030
Andre Nolldd8ac332009-03-31 14:33:13 +11003031 rdev->sectors = sectors;
3032 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003033 /* Need to check that all other rdevs with the same
3034 * ->bdev do not overlap. 'rcu' is sufficient to walk
3035 * the rdev lists safely.
3036 * This check does not provide a hard guarantee, it
3037 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003038 */
NeilBrownfd01b882011-10-11 16:47:53 +11003039 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003040 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003041 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003042
NeilBrown8b1afc32014-09-29 15:33:20 +10003043 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003044 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003045 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003046
NeilBrowndafb20f2012-03-19 12:46:39 +11003047 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003048 if (rdev->bdev == rdev2->bdev &&
3049 rdev != rdev2 &&
3050 overlaps(rdev->data_offset, rdev->sectors,
3051 rdev2->data_offset,
3052 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003053 overlap = 1;
3054 break;
3055 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003056 if (overlap) {
3057 mddev_put(mddev);
3058 break;
3059 }
3060 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003061 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003062 if (overlap) {
3063 /* Someone else could have slipped in a size
3064 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003065 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003066 * safe, and trust userspace not to race with
3067 * itself
3068 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003069 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003070 return -EBUSY;
3071 }
3072 }
NeilBrown83303b62006-01-06 00:21:06 -08003073 return len;
3074}
3075
3076static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003077__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003078
NeilBrown3cb03002011-10-11 16:45:26 +11003079static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003080{
3081 unsigned long long recovery_start = rdev->recovery_offset;
3082
3083 if (test_bit(In_sync, &rdev->flags) ||
3084 recovery_start == MaxSector)
3085 return sprintf(page, "none\n");
3086
3087 return sprintf(page, "%llu\n", recovery_start);
3088}
3089
NeilBrown3cb03002011-10-11 16:45:26 +11003090static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003091{
3092 unsigned long long recovery_start;
3093
3094 if (cmd_match(buf, "none"))
3095 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003096 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003097 return -EINVAL;
3098
3099 if (rdev->mddev->pers &&
3100 rdev->raid_disk >= 0)
3101 return -EBUSY;
3102
3103 rdev->recovery_offset = recovery_start;
3104 if (recovery_start == MaxSector)
3105 set_bit(In_sync, &rdev->flags);
3106 else
3107 clear_bit(In_sync, &rdev->flags);
3108 return len;
3109}
3110
3111static struct rdev_sysfs_entry rdev_recovery_start =
3112__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3113
Vishal Vermafc974ee2015-12-24 19:20:34 -07003114/* sysfs access to bad-blocks list.
3115 * We present two files.
3116 * 'bad-blocks' lists sector numbers and lengths of ranges that
3117 * are recorded as bad. The list is truncated to fit within
3118 * the one-page limit of sysfs.
3119 * Writing "sector length" to this file adds an acknowledged
3120 * bad block list.
3121 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3122 * been acknowledged. Writing to this file adds bad blocks
3123 * without acknowledging them. This is largely for testing.
3124 */
NeilBrown3cb03002011-10-11 16:45:26 +11003125static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003126{
3127 return badblocks_show(&rdev->badblocks, page, 0);
3128}
NeilBrown3cb03002011-10-11 16:45:26 +11003129static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003130{
NeilBrownde393cd2011-07-28 11:31:48 +10003131 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3132 /* Maybe that ack was all we needed */
3133 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3134 wake_up(&rdev->blocked_wait);
3135 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003136}
3137static struct rdev_sysfs_entry rdev_bad_blocks =
3138__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3139
NeilBrown3cb03002011-10-11 16:45:26 +11003140static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003141{
3142 return badblocks_show(&rdev->badblocks, page, 1);
3143}
NeilBrown3cb03002011-10-11 16:45:26 +11003144static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003145{
3146 return badblocks_store(&rdev->badblocks, page, len, 1);
3147}
3148static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3149__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3150
NeilBrown86e6ffd2005-11-08 21:39:24 -08003151static struct attribute *rdev_default_attrs[] = {
3152 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003153 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003154 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003155 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003156 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003157 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003158 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003159 &rdev_bad_blocks.attr,
3160 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003161 NULL,
3162};
3163static ssize_t
3164rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3165{
3166 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003167 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003168
3169 if (!entry->show)
3170 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003171 if (!rdev->mddev)
3172 return -EBUSY;
3173 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003174}
3175
3176static ssize_t
3177rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3178 const char *page, size_t length)
3179{
3180 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003181 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003182 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003183 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003184
3185 if (!entry->store)
3186 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003187 if (!capable(CAP_SYS_ADMIN))
3188 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003189 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003190 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003191 if (rdev->mddev == NULL)
3192 rv = -EBUSY;
3193 else
3194 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003195 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003196 }
3197 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003198}
3199
3200static void rdev_free(struct kobject *ko)
3201{
NeilBrown3cb03002011-10-11 16:45:26 +11003202 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003203 kfree(rdev);
3204}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003205static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003206 .show = rdev_attr_show,
3207 .store = rdev_attr_store,
3208};
3209static struct kobj_type rdev_ktype = {
3210 .release = rdev_free,
3211 .sysfs_ops = &rdev_sysfs_ops,
3212 .default_attrs = rdev_default_attrs,
3213};
3214
NeilBrown3cb03002011-10-11 16:45:26 +11003215int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003216{
3217 rdev->desc_nr = -1;
3218 rdev->saved_raid_disk = -1;
3219 rdev->raid_disk = -1;
3220 rdev->flags = 0;
3221 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003222 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003223 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003224 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003225 rdev->sb_loaded = 0;
3226 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003227 atomic_set(&rdev->nr_pending, 0);
3228 atomic_set(&rdev->read_errors, 0);
3229 atomic_set(&rdev->corrected_errors, 0);
3230
3231 INIT_LIST_HEAD(&rdev->same_set);
3232 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003233
3234 /* Add space to store bad block list.
3235 * This reserves the space even on arrays where it cannot
3236 * be used - I wonder if that matters
3237 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003238 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003239}
3240EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241/*
3242 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3243 *
3244 * mark the device faulty if:
3245 *
3246 * - the device is nonexistent (zero size)
3247 * - the device has no valid superblock
3248 *
3249 * a faulty rdev _never_ has rdev->sb set.
3250 */
NeilBrown3cb03002011-10-11 16:45:26 +11003251static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
3253 char b[BDEVNAME_SIZE];
3254 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003255 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 sector_t size;
3257
NeilBrown9ffae0c2006-01-06 00:20:32 -08003258 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003259 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
NeilBrown2230dfe2011-07-28 11:31:46 +10003262 err = md_rdev_init(rdev);
3263 if (err)
3264 goto abort_free;
3265 err = alloc_disk_sb(rdev);
3266 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 goto abort_free;
3268
NeilBrownc5d79ad2008-02-06 01:39:54 -08003269 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 if (err)
3271 goto abort_free;
3272
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003273 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003274
Mike Snitzer77304d22010-11-08 14:39:12 +01003275 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003277 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 bdevname(rdev->bdev,b));
3279 err = -EINVAL;
3280 goto abort_free;
3281 }
3282
3283 if (super_format >= 0) {
3284 err = super_types[super_format].
3285 load_super(rdev, NULL, super_minor);
3286 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003287 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003288 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003289 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 goto abort_free;
3291 }
3292 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003293 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 bdevname(rdev->bdev,b));
3295 goto abort_free;
3296 }
3297 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 return rdev;
3300
3301abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003302 if (rdev->bdev)
3303 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003304 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 kfree(rdev);
3306 return ERR_PTR(err);
3307}
3308
3309/*
3310 * Check a full RAID array for plausibility
3311 */
3312
NeilBrownf72ffdd2014-09-30 14:23:59 +10003313static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314{
3315 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003316 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 char b[BDEVNAME_SIZE];
3318
3319 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003320 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 switch (super_types[mddev->major_version].
3322 load_super(rdev, freshest, mddev->minor_version)) {
3323 case 1:
3324 freshest = rdev;
3325 break;
3326 case 0:
3327 break;
3328 default:
NeilBrown9d487392016-11-02 14:16:49 +11003329 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003331 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 }
3333
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 super_types[mddev->major_version].
3335 validate_super(mddev, freshest);
3336
3337 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003338 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003339 if (mddev->max_disks &&
3340 (rdev->desc_nr >= mddev->max_disks ||
3341 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003342 pr_warn("md: %s: %s: only %d devices permitted\n",
3343 mdname(mddev), bdevname(rdev->bdev, b),
3344 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003345 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003346 continue;
3347 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003348 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 if (super_types[mddev->major_version].
3350 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003351 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003353 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 continue;
3355 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 if (mddev->level == LEVEL_MULTIPATH) {
3358 rdev->desc_nr = i++;
3359 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003360 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003361 } else if (rdev->raid_disk >=
3362 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3363 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003364 rdev->raid_disk = -1;
3365 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 }
3367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368}
3369
NeilBrown72e02072009-12-14 12:49:55 +11003370/* Read a fixed-point number.
3371 * Numbers in sysfs attributes should be in "standard" units where
3372 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003373 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003374 * milliseconds or jiffies.
3375 * This function takes a decimal number with a possible fractional
3376 * component, and produces an integer which is the result of
3377 * multiplying that number by 10^'scale'.
3378 * all without any floating-point arithmetic.
3379 */
3380int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3381{
3382 unsigned long result = 0;
3383 long decimals = -1;
3384 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3385 if (*cp == '.')
3386 decimals = 0;
3387 else if (decimals < scale) {
3388 unsigned int value;
3389 value = *cp - '0';
3390 result = result * 10 + value;
3391 if (decimals >= 0)
3392 decimals++;
3393 }
3394 cp++;
3395 }
3396 if (*cp == '\n')
3397 cp++;
3398 if (*cp)
3399 return -EINVAL;
3400 if (decimals < 0)
3401 decimals = 0;
3402 while (decimals < scale) {
3403 result *= 10;
3404 decimals ++;
3405 }
3406 *res = result;
3407 return 0;
3408}
3409
NeilBrowneae17012005-11-08 21:39:23 -08003410static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003411safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003412{
3413 int msec = (mddev->safemode_delay*1000)/HZ;
3414 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3415}
3416static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003417safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003418{
NeilBrown16f17b32006-06-26 00:27:37 -07003419 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003420
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003421 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003422 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003423 return -EINVAL;
3424 }
3425
NeilBrown72e02072009-12-14 12:49:55 +11003426 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003427 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003428 if (msec == 0)
3429 mddev->safemode_delay = 0;
3430 else {
NeilBrown19052c02008-08-05 15:54:13 +10003431 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003432 unsigned long new_delay = (msec*HZ)/1000;
3433
3434 if (new_delay == 0)
3435 new_delay = 1;
3436 mddev->safemode_delay = new_delay;
3437 if (new_delay < old_delay || old_delay == 0)
3438 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003439 }
3440 return len;
3441}
3442static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003443__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003444
3445static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003446level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003447{
NeilBrown36d091f2014-12-15 12:56:58 +11003448 struct md_personality *p;
3449 int ret;
3450 spin_lock(&mddev->lock);
3451 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003452 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003453 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003454 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003455 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003456 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003457 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003458 else
NeilBrown36d091f2014-12-15 12:56:58 +11003459 ret = 0;
3460 spin_unlock(&mddev->lock);
3461 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003462}
3463
NeilBrownd9d166c2006-01-06 00:20:51 -08003464static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003465level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003466{
Dan Williamsf2859af2010-05-02 10:04:16 -07003467 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003468 ssize_t rv;
3469 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003470 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003471 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003472 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003473 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003474
NeilBrown67918752014-12-15 12:57:01 +11003475 if (slen == 0 || slen >= sizeof(clevel))
3476 return -EINVAL;
3477
3478 rv = mddev_lock(mddev);
3479 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003480 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003481
3482 if (mddev->pers == NULL) {
3483 strncpy(mddev->clevel, buf, slen);
3484 if (mddev->clevel[slen-1] == '\n')
3485 slen--;
3486 mddev->clevel[slen] = 0;
3487 mddev->level = LEVEL_NONE;
3488 rv = len;
3489 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003490 }
NeilBrown67918752014-12-15 12:57:01 +11003491 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003492 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003493 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003494
3495 /* request to change the personality. Need to ensure:
3496 * - array is not engaged in resync/recovery/reshape
3497 * - old personality can be suspended
3498 * - new personality will access other array.
3499 */
3500
NeilBrown67918752014-12-15 12:57:01 +11003501 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003502 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003503 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003504 mddev->reshape_position != MaxSector ||
3505 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003506 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003507
NeilBrown67918752014-12-15 12:57:01 +11003508 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003509 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003510 pr_warn("md: %s: %s does not support online personality change\n",
3511 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003512 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003513 }
3514
3515 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003516 strncpy(clevel, buf, slen);
3517 if (clevel[slen-1] == '\n')
3518 slen--;
3519 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003520 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003521 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003522
Dan Williamsf2859af2010-05-02 10:04:16 -07003523 if (request_module("md-%s", clevel) != 0)
3524 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003525 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003526 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003527 if (!pers || !try_module_get(pers->owner)) {
3528 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003529 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003530 rv = -EINVAL;
3531 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003532 }
3533 spin_unlock(&pers_lock);
3534
3535 if (pers == mddev->pers) {
3536 /* Nothing to do! */
3537 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003538 rv = len;
3539 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003540 }
3541 if (!pers->takeover) {
3542 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003543 pr_warn("md: %s: %s does not support personality takeover\n",
3544 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003545 rv = -EINVAL;
3546 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003547 }
3548
NeilBrowndafb20f2012-03-19 12:46:39 +11003549 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003550 rdev->new_raid_disk = rdev->raid_disk;
3551
NeilBrown245f46c2009-03-31 14:39:39 +11003552 /* ->takeover must set new_* and/or delta_disks
3553 * if it succeeds, and may set them when it fails.
3554 */
3555 priv = pers->takeover(mddev);
3556 if (IS_ERR(priv)) {
3557 mddev->new_level = mddev->level;
3558 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003559 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003560 mddev->raid_disks -= mddev->delta_disks;
3561 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003562 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003563 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003564 pr_warn("md: %s: %s would not accept array\n",
3565 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003566 rv = PTR_ERR(priv);
3567 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003568 }
3569
3570 /* Looks like we have a winner */
3571 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003572 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003573
3574 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003575 oldpers = mddev->pers;
3576 oldpriv = mddev->private;
3577 mddev->pers = pers;
3578 mddev->private = priv;
3579 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3580 mddev->level = mddev->new_level;
3581 mddev->layout = mddev->new_layout;
3582 mddev->chunk_sectors = mddev->new_chunk_sectors;
3583 mddev->delta_disks = 0;
3584 mddev->reshape_backwards = 0;
3585 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003586 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003587
NeilBrowndb721d32014-12-15 12:56:58 +11003588 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003589 mddev->external) {
3590 /* We are converting from a no-redundancy array
3591 * to a redundancy array and metadata is managed
3592 * externally so we need to be sure that writes
3593 * won't block due to a need to transition
3594 * clean->dirty
3595 * until external management is started.
3596 */
3597 mddev->in_sync = 0;
3598 mddev->safemode_delay = 0;
3599 mddev->safemode = 0;
3600 }
3601
NeilBrowndb721d32014-12-15 12:56:58 +11003602 oldpers->free(mddev, oldpriv);
3603
3604 if (oldpers->sync_request == NULL &&
3605 pers->sync_request != NULL) {
3606 /* need to add the md_redundancy_group */
3607 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11003608 pr_warn("md: cannot register extra attributes for %s\n",
3609 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11003610 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3611 }
3612 if (oldpers->sync_request != NULL &&
3613 pers->sync_request == NULL) {
3614 /* need to remove the md_redundancy_group */
3615 if (mddev->to_remove == NULL)
3616 mddev->to_remove = &md_redundancy_group;
3617 }
3618
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003619 module_put(oldpers->owner);
3620
NeilBrowndafb20f2012-03-19 12:46:39 +11003621 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003622 if (rdev->raid_disk < 0)
3623 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003624 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003625 rdev->new_raid_disk = -1;
3626 if (rdev->new_raid_disk == rdev->raid_disk)
3627 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003628 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003629 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003630 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003631 if (rdev->raid_disk < 0)
3632 continue;
3633 if (rdev->new_raid_disk == rdev->raid_disk)
3634 continue;
3635 rdev->raid_disk = rdev->new_raid_disk;
3636 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003637 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003638 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003639 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11003640 pr_warn("md: cannot register rd%d for %s after level change\n",
3641 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003642 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003643 }
3644
NeilBrowndb721d32014-12-15 12:56:58 +11003645 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003646 /* this is now an array without redundancy, so
3647 * it must always be in_sync
3648 */
3649 mddev->in_sync = 1;
3650 del_timer_sync(&mddev->safemode_timer);
3651 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003652 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003653 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003654 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003655 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003656 if (!mddev->thread)
3657 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003658 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003659 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003660 rv = len;
3661out_unlock:
3662 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003663 return rv;
3664}
3665
3666static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003667__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003668
NeilBrownd4dbd022006-06-26 00:27:59 -07003669static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003670layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003671{
3672 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003673 if (mddev->reshape_position != MaxSector &&
3674 mddev->layout != mddev->new_layout)
3675 return sprintf(page, "%d (%d)\n",
3676 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003677 return sprintf(page, "%d\n", mddev->layout);
3678}
3679
3680static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003681layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003682{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003683 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003684 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003685
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003686 err = kstrtouint(buf, 10, &n);
3687 if (err < 0)
3688 return err;
NeilBrown67918752014-12-15 12:57:01 +11003689 err = mddev_lock(mddev);
3690 if (err)
3691 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003692
NeilBrownb3546032009-03-31 14:56:41 +11003693 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003694 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003695 err = -EBUSY;
3696 else if (mddev->ro)
3697 err = -EROFS;
3698 else {
3699 mddev->new_layout = n;
3700 err = mddev->pers->check_reshape(mddev);
3701 if (err)
3702 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003703 }
NeilBrownb3546032009-03-31 14:56:41 +11003704 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003705 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003706 if (mddev->reshape_position == MaxSector)
3707 mddev->layout = n;
3708 }
NeilBrown67918752014-12-15 12:57:01 +11003709 mddev_unlock(mddev);
3710 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003711}
3712static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003713__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003714
NeilBrowneae17012005-11-08 21:39:23 -08003715static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003716raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003717{
NeilBrownbb636542005-11-08 21:39:45 -08003718 if (mddev->raid_disks == 0)
3719 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003720 if (mddev->reshape_position != MaxSector &&
3721 mddev->delta_disks != 0)
3722 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3723 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003724 return sprintf(page, "%d\n", mddev->raid_disks);
3725}
3726
NeilBrownfd01b882011-10-11 16:47:53 +11003727static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003728
3729static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003730raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003731{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003732 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003733 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003734
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003735 err = kstrtouint(buf, 10, &n);
3736 if (err < 0)
3737 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003738
NeilBrown67918752014-12-15 12:57:01 +11003739 err = mddev_lock(mddev);
3740 if (err)
3741 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003742 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003743 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003744 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003745 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003746 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003747
NeilBrown67918752014-12-15 12:57:01 +11003748 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003749 rdev_for_each(rdev, mddev) {
3750 if (olddisks < n &&
3751 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003752 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003753 if (olddisks > n &&
3754 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003755 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003756 }
NeilBrown67918752014-12-15 12:57:01 +11003757 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003758 mddev->delta_disks = n - olddisks;
3759 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003760 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003761 } else
NeilBrownda943b992006-01-06 00:20:54 -08003762 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003763out_unlock:
3764 mddev_unlock(mddev);
3765 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003766}
3767static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003768__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003769
NeilBrown24dd4692005-11-08 21:39:26 -08003770static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003771chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003772{
NeilBrown08a02ec2007-05-09 02:35:38 -07003773 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003774 mddev->chunk_sectors != mddev->new_chunk_sectors)
3775 return sprintf(page, "%d (%d)\n",
3776 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003777 mddev->chunk_sectors << 9);
3778 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003779}
3780
3781static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003782chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003783{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003784 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003785 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003786
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003787 err = kstrtoul(buf, 10, &n);
3788 if (err < 0)
3789 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003790
NeilBrown67918752014-12-15 12:57:01 +11003791 err = mddev_lock(mddev);
3792 if (err)
3793 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003794 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003795 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003796 err = -EBUSY;
3797 else if (mddev->ro)
3798 err = -EROFS;
3799 else {
3800 mddev->new_chunk_sectors = n >> 9;
3801 err = mddev->pers->check_reshape(mddev);
3802 if (err)
3803 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003804 }
NeilBrownb3546032009-03-31 14:56:41 +11003805 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003806 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003807 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003808 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003809 }
NeilBrown67918752014-12-15 12:57:01 +11003810 mddev_unlock(mddev);
3811 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003812}
3813static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003814__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003815
NeilBrowna94213b2006-06-26 00:28:00 -07003816static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003817resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003818{
NeilBrownd1a7c502009-03-31 15:24:32 +11003819 if (mddev->recovery_cp == MaxSector)
3820 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003821 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3822}
3823
3824static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003825resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003826{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003827 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003828 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003829
3830 if (cmd_match(buf, "none"))
3831 n = MaxSector;
3832 else {
3833 err = kstrtoull(buf, 10, &n);
3834 if (err < 0)
3835 return err;
3836 if (n != (sector_t)n)
3837 return -EINVAL;
3838 }
NeilBrowna94213b2006-06-26 00:28:00 -07003839
NeilBrown67918752014-12-15 12:57:01 +11003840 err = mddev_lock(mddev);
3841 if (err)
3842 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003843 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003844 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003845
NeilBrown67918752014-12-15 12:57:01 +11003846 if (!err) {
3847 mddev->recovery_cp = n;
3848 if (mddev->pers)
3849 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3850 }
3851 mddev_unlock(mddev);
3852 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003853}
3854static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003855__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3856 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003857
NeilBrown9e653b62006-06-26 00:27:58 -07003858/*
3859 * The array state can be:
3860 *
3861 * clear
3862 * No devices, no size, no level
3863 * Equivalent to STOP_ARRAY ioctl
3864 * inactive
3865 * May have some settings, but array is not active
3866 * all IO results in error
3867 * When written, doesn't tear down array, but just stops it
3868 * suspended (not supported yet)
3869 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003870 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003871 * readonly
3872 * no resync can happen. no superblocks get written.
3873 * write requests fail
3874 * read-auto
3875 * like readonly, but behaves like 'clean' on a write request.
3876 *
3877 * clean - no pending writes, but otherwise active.
3878 * When written to inactive array, starts without resync
3879 * If a write request arrives then
3880 * if metadata is known, mark 'dirty' and switch to 'active'.
3881 * if not known, block and switch to write-pending
3882 * If written to an active array that has pending writes, then fails.
3883 * active
3884 * fully active: IO and resync can be happening.
3885 * When written to inactive array, starts with resync
3886 *
3887 * write-pending
3888 * clean, but writes are blocked waiting for 'active' to be written.
3889 *
3890 * active-idle
3891 * like active, but no writes have been seen for a while (100msec).
3892 *
3893 */
3894enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3895 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003896static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003897 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3898 "write-pending", "active-idle", NULL };
3899
3900static int match_word(const char *word, char **list)
3901{
3902 int n;
3903 for (n=0; list[n]; n++)
3904 if (cmd_match(word, list[n]))
3905 break;
3906 return n;
3907}
3908
3909static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003910array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003911{
3912 enum array_state st = inactive;
3913
3914 if (mddev->pers)
3915 switch(mddev->ro) {
3916 case 1:
3917 st = readonly;
3918 break;
3919 case 2:
3920 st = read_auto;
3921 break;
3922 case 0:
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02003923 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003924 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02003925 else if (mddev->in_sync)
3926 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07003927 else if (mddev->safemode)
3928 st = active_idle;
3929 else
3930 st = active;
3931 }
3932 else {
3933 if (list_empty(&mddev->disks) &&
3934 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003935 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003936 st = clear;
3937 else
3938 st = inactive;
3939 }
3940 return sprintf(page, "%s\n", array_states[st]);
3941}
3942
NeilBrownf72ffdd2014-09-30 14:23:59 +10003943static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3944static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3945static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003946static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003947
3948static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003949array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003950{
NeilBrown67918752014-12-15 12:57:01 +11003951 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003952 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003953
3954 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3955 /* don't take reconfig_mutex when toggling between
3956 * clean and active
3957 */
3958 spin_lock(&mddev->lock);
3959 if (st == active) {
3960 restart_array(mddev);
3961 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02003962 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11003963 wake_up(&mddev->sb_wait);
3964 err = 0;
3965 } else /* st == clean */ {
3966 restart_array(mddev);
3967 if (atomic_read(&mddev->writes_pending) == 0) {
3968 if (mddev->in_sync == 0) {
3969 mddev->in_sync = 1;
3970 if (mddev->safemode == 1)
3971 mddev->safemode = 0;
3972 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3973 }
3974 err = 0;
3975 } else
3976 err = -EBUSY;
3977 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02003978 if (!err)
3979 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11003980 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003981 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003982 }
3983 err = mddev_lock(mddev);
3984 if (err)
3985 return err;
3986 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003987 switch(st) {
3988 case bad_word:
3989 break;
3990 case clear:
3991 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003992 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003993 break;
3994 case inactive:
3995 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003996 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003997 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003998 else
NeilBrowne6910632008-02-06 01:39:51 -08003999 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004000 break;
4001 case suspended:
4002 break; /* not supported yet */
4003 case readonly:
4004 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004005 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004006 else {
4007 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004008 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004009 err = do_md_run(mddev);
4010 }
4011 break;
4012 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004013 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004014 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004015 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004016 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004017 err = restart_array(mddev);
4018 if (err == 0) {
4019 mddev->ro = 2;
4020 set_disk_ro(mddev->gendisk, 0);
4021 }
NeilBrown9e653b62006-06-26 00:27:58 -07004022 } else {
4023 mddev->ro = 2;
4024 err = do_md_run(mddev);
4025 }
4026 break;
4027 case clean:
4028 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004029 err = restart_array(mddev);
4030 if (err)
4031 break;
NeilBrown85572d72014-12-15 12:56:56 +11004032 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004033 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08004034 if (mddev->in_sync == 0) {
4035 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07004036 if (mddev->safemode == 1)
4037 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10004038 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08004039 }
4040 err = 0;
4041 } else
4042 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004043 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004044 } else
4045 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004046 break;
4047 case active:
4048 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004049 err = restart_array(mddev);
4050 if (err)
4051 break;
NeilBrown070dc6d2010-08-30 17:33:34 +10004052 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004053 wake_up(&mddev->sb_wait);
4054 err = 0;
4055 } else {
4056 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004057 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004058 err = do_md_run(mddev);
4059 }
4060 break;
4061 case write_pending:
4062 case active_idle:
4063 /* these cannot be set */
4064 break;
4065 }
NeilBrown67918752014-12-15 12:57:01 +11004066
4067 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004068 if (mddev->hold_active == UNTIL_IOCTL)
4069 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004070 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004071 }
NeilBrown67918752014-12-15 12:57:01 +11004072 mddev_unlock(mddev);
4073 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004074}
NeilBrown80ca3a42006-07-10 04:44:18 -07004075static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004076__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004077
NeilBrown6d7ff7382006-01-06 00:21:16 -08004078static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004079max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004080 return sprintf(page, "%d\n",
4081 atomic_read(&mddev->max_corr_read_errors));
4082}
4083
4084static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004085max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004086{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004087 unsigned int n;
4088 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004089
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004090 rv = kstrtouint(buf, 10, &n);
4091 if (rv < 0)
4092 return rv;
4093 atomic_set(&mddev->max_corr_read_errors, n);
4094 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004095}
4096
4097static struct md_sysfs_entry max_corr_read_errors =
4098__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4099 max_corrected_read_errors_store);
4100
4101static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004102null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004103{
4104 return -EINVAL;
4105}
4106
4107static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004108new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004109{
4110 /* buf must be %d:%d\n? giving major and minor numbers */
4111 /* The new device is added to the array.
4112 * If the array has a persistent superblock, we read the
4113 * superblock to initialise info and check validity.
4114 * Otherwise, only checking done is that in bind_rdev_to_array,
4115 * which mainly checks size.
4116 */
4117 char *e;
4118 int major = simple_strtoul(buf, &e, 10);
4119 int minor;
4120 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004121 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004122 int err;
4123
4124 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4125 return -EINVAL;
4126 minor = simple_strtoul(e+1, &e, 10);
4127 if (*e && *e != '\n')
4128 return -EINVAL;
4129 dev = MKDEV(major, minor);
4130 if (major != MAJOR(dev) ||
4131 minor != MINOR(dev))
4132 return -EOVERFLOW;
4133
NeilBrown67918752014-12-15 12:57:01 +11004134 flush_workqueue(md_misc_wq);
4135
4136 err = mddev_lock(mddev);
4137 if (err)
4138 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004139 if (mddev->persistent) {
4140 rdev = md_import_device(dev, mddev->major_version,
4141 mddev->minor_version);
4142 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004143 struct md_rdev *rdev0
4144 = list_entry(mddev->disks.next,
4145 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004146 err = super_types[mddev->major_version]
4147 .load_super(rdev, rdev0, mddev->minor_version);
4148 if (err < 0)
4149 goto out;
4150 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004151 } else if (mddev->external)
4152 rdev = md_import_device(dev, -2, -1);
4153 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004154 rdev = md_import_device(dev, -1, -1);
4155
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004156 if (IS_ERR(rdev)) {
4157 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004158 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004159 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004160 err = bind_rdev_to_array(rdev, mddev);
4161 out:
4162 if (err)
4163 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004164 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004165 return err ? err : len;
4166}
4167
4168static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004169__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004170
4171static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004172bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004173{
4174 char *end;
4175 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004176 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004177
NeilBrown67918752014-12-15 12:57:01 +11004178 err = mddev_lock(mddev);
4179 if (err)
4180 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004181 if (!mddev->bitmap)
4182 goto out;
4183 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4184 while (*buf) {
4185 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4186 if (buf == end) break;
4187 if (*end == '-') { /* range */
4188 buf = end + 1;
4189 end_chunk = simple_strtoul(buf, &end, 0);
4190 if (buf == end) break;
4191 }
4192 if (*end && !isspace(*end)) break;
4193 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004194 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004195 }
4196 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4197out:
NeilBrown67918752014-12-15 12:57:01 +11004198 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004199 return len;
4200}
4201
4202static struct md_sysfs_entry md_bitmap =
4203__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4204
4205static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004206size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004207{
Andre Noll58c0fed2009-03-31 14:33:13 +11004208 return sprintf(page, "%llu\n",
4209 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004210}
4211
NeilBrownfd01b882011-10-11 16:47:53 +11004212static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004213
4214static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004215size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004216{
4217 /* If array is inactive, we can reduce the component size, but
4218 * not increase it (except from 0).
4219 * If array is active, we can try an on-line resize
4220 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004221 sector_t sectors;
4222 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004223
Andre Noll58c0fed2009-03-31 14:33:13 +11004224 if (err < 0)
4225 return err;
NeilBrown67918752014-12-15 12:57:01 +11004226 err = mddev_lock(mddev);
4227 if (err)
4228 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004229 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004230 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004231 if (err == 0)
4232 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004233 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004234 if (mddev->dev_sectors == 0 ||
4235 mddev->dev_sectors > sectors)
4236 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004237 else
4238 err = -ENOSPC;
4239 }
NeilBrown67918752014-12-15 12:57:01 +11004240 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004241 return err ? err : len;
4242}
4243
4244static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004245__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004246
Masanari Iida83f0d772012-10-30 00:18:08 +09004247/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004248 * This is one of
4249 * 'none' for arrays with no metadata (good luck...)
4250 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004251 * or N.M for internally known formats
4252 */
4253static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004254metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004255{
4256 if (mddev->persistent)
4257 return sprintf(page, "%d.%d\n",
4258 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004259 else if (mddev->external)
4260 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004261 else
4262 return sprintf(page, "none\n");
4263}
4264
4265static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004266metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004267{
4268 int major, minor;
4269 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004270 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004271 /* Changing the details of 'external' metadata is
4272 * always permitted. Otherwise there must be
4273 * no devices attached to the array.
4274 */
NeilBrown67918752014-12-15 12:57:01 +11004275
4276 err = mddev_lock(mddev);
4277 if (err)
4278 return err;
4279 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004280 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4281 ;
4282 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004283 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004284
NeilBrown67918752014-12-15 12:57:01 +11004285 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004286 if (cmd_match(buf, "none")) {
4287 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004288 mddev->external = 0;
4289 mddev->major_version = 0;
4290 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004291 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004292 }
4293 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004294 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004295 if (namelen >= sizeof(mddev->metadata_type))
4296 namelen = sizeof(mddev->metadata_type)-1;
4297 strncpy(mddev->metadata_type, buf+9, namelen);
4298 mddev->metadata_type[namelen] = 0;
4299 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4300 mddev->metadata_type[--namelen] = 0;
4301 mddev->persistent = 0;
4302 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004303 mddev->major_version = 0;
4304 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004305 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004306 }
4307 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004308 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004309 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004310 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004311 buf = e+1;
4312 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004313 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004314 goto out_unlock;
4315 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004316 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004317 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004318 mddev->major_version = major;
4319 mddev->minor_version = minor;
4320 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004321 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004322 err = 0;
4323out_unlock:
4324 mddev_unlock(mddev);
4325 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004326}
4327
4328static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004329__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004330
NeilBrowna35b0d62006-01-06 00:20:49 -08004331static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004332action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004333{
NeilBrown7eec3142005-11-08 21:39:44 -08004334 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004335 unsigned long recovery = mddev->recovery;
4336 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004337 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004338 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4339 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4340 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004341 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004342 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4343 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004344 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004345 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004346 type = "check";
4347 else
4348 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004349 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004350 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004351 else if (mddev->reshape_position != MaxSector)
4352 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004353 }
4354 return sprintf(page, "%s\n", type);
4355}
4356
4357static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004358action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004359{
NeilBrown7eec3142005-11-08 21:39:44 -08004360 if (!mddev->pers || !mddev->pers->sync_request)
4361 return -EINVAL;
4362
NeilBrownb6a9ce62009-05-26 09:41:17 +10004363
4364 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004365 if (cmd_match(page, "frozen"))
4366 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4367 else
4368 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004369 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4370 mddev_lock(mddev) == 0) {
4371 flush_workqueue(md_misc_wq);
4372 if (mddev->sync_thread) {
4373 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004374 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004375 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004376 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004377 }
NeilBrown312045e2015-12-21 11:01:21 +11004378 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004379 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004380 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004381 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004382 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004383 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004384 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004385 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004386 int err;
4387 if (mddev->pers->start_reshape == NULL)
4388 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004389 err = mddev_lock(mddev);
4390 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004391 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4392 err = -EBUSY;
4393 else {
4394 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4395 err = mddev->pers->start_reshape(mddev);
4396 }
NeilBrown67918752014-12-15 12:57:01 +11004397 mddev_unlock(mddev);
4398 }
NeilBrown16484bf2006-03-27 01:18:13 -08004399 if (err)
4400 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004401 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004402 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004403 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004404 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004405 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004406 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004407 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004408 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4409 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004410 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004411 if (mddev->ro == 2) {
4412 /* A write to sync_action is enough to justify
4413 * canceling read-auto mode
4414 */
4415 mddev->ro = 0;
4416 md_wakeup_thread(mddev->sync_thread);
4417 }
NeilBrown03c902e2006-01-06 00:20:46 -08004418 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004419 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004420 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004421 return len;
4422}
4423
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004424static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004425__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004426
4427static ssize_t
4428last_sync_action_show(struct mddev *mddev, char *page)
4429{
4430 return sprintf(page, "%s\n", mddev->last_sync_action);
4431}
4432
4433static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4434
NeilBrown9d888832005-11-08 21:39:26 -08004435static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004436mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004437{
4438 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004439 (unsigned long long)
4440 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004441}
4442
NeilBrown80ca3a42006-07-10 04:44:18 -07004443static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004444
NeilBrown88202a02006-01-06 00:21:36 -08004445static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004446sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004447{
4448 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4449 mddev->sync_speed_min ? "local": "system");
4450}
4451
4452static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004453sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004454{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004455 unsigned int min;
4456 int rv;
4457
NeilBrown88202a02006-01-06 00:21:36 -08004458 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004459 min = 0;
4460 } else {
4461 rv = kstrtouint(buf, 10, &min);
4462 if (rv < 0)
4463 return rv;
4464 if (min == 0)
4465 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004466 }
NeilBrown88202a02006-01-06 00:21:36 -08004467 mddev->sync_speed_min = min;
4468 return len;
4469}
4470
4471static struct md_sysfs_entry md_sync_min =
4472__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4473
4474static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004475sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004476{
4477 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4478 mddev->sync_speed_max ? "local": "system");
4479}
4480
4481static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004482sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004483{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004484 unsigned int max;
4485 int rv;
4486
NeilBrown88202a02006-01-06 00:21:36 -08004487 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004488 max = 0;
4489 } else {
4490 rv = kstrtouint(buf, 10, &max);
4491 if (rv < 0)
4492 return rv;
4493 if (max == 0)
4494 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004495 }
NeilBrown88202a02006-01-06 00:21:36 -08004496 mddev->sync_speed_max = max;
4497 return len;
4498}
4499
4500static struct md_sysfs_entry md_sync_max =
4501__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4502
Iustin Popd7f3d292007-10-16 23:30:54 -07004503static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004504degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004505{
4506 return sprintf(page, "%d\n", mddev->degraded);
4507}
4508static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004509
4510static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004511sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004512{
4513 return sprintf(page, "%d\n", mddev->parallel_resync);
4514}
4515
4516static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004517sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004518{
4519 long n;
4520
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004521 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004522 return -EINVAL;
4523
4524 if (n != 0 && n != 1)
4525 return -EINVAL;
4526
4527 mddev->parallel_resync = n;
4528
4529 if (mddev->sync_thread)
4530 wake_up(&resync_wait);
4531
4532 return len;
4533}
4534
4535/* force parallel resync, even with shared block devices */
4536static struct md_sysfs_entry md_sync_force_parallel =
4537__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4538 sync_force_parallel_show, sync_force_parallel_store);
4539
4540static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004541sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004542{
4543 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004544 if (mddev->curr_resync == 0)
4545 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004546 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4547 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004548 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004549 db = resync - mddev->resync_mark_cnt;
4550 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004551}
4552
NeilBrown80ca3a42006-07-10 04:44:18 -07004553static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004554
4555static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004556sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004557{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004558 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004559
NeilBrownacb180b2009-04-14 16:28:34 +10004560 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4561 return sprintf(page, "none\n");
4562
NeilBrown72f36d52012-10-11 14:25:57 +11004563 if (mddev->curr_resync == 1 ||
4564 mddev->curr_resync == 2)
4565 return sprintf(page, "delayed\n");
4566
NeilBrownc804cde2012-05-21 09:28:33 +10004567 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4568 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004569 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004570 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004571 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004572
NeilBrownacb180b2009-04-14 16:28:34 +10004573 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004574 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004575}
4576
NeilBrown750f1992014-09-30 08:53:05 +10004577static struct md_sysfs_entry md_sync_completed =
4578 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004579
NeilBrowne464eaf2006-03-27 01:18:14 -08004580static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004581min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004582{
4583 return sprintf(page, "%llu\n",
4584 (unsigned long long)mddev->resync_min);
4585}
4586static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004587min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004588{
4589 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004590 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004591
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004592 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004593 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004594
4595 spin_lock(&mddev->lock);
4596 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004597 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004598 goto out_unlock;
4599
4600 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004601 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004602 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004603
NeilBrown50c37b12015-03-23 17:36:38 +11004604 /* Round down to multiple of 4K for safety */
4605 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004606 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004607
NeilBrown23da4222014-12-15 12:57:01 +11004608out_unlock:
4609 spin_unlock(&mddev->lock);
4610 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004611}
4612
4613static struct md_sysfs_entry md_min_sync =
4614__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4615
4616static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004617max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004618{
4619 if (mddev->resync_max == MaxSector)
4620 return sprintf(page, "max\n");
4621 else
4622 return sprintf(page, "%llu\n",
4623 (unsigned long long)mddev->resync_max);
4624}
4625static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004626max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004627{
NeilBrown23da4222014-12-15 12:57:01 +11004628 int err;
4629 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004630 if (strncmp(buf, "max", 3) == 0)
4631 mddev->resync_max = MaxSector;
4632 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004633 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004634 int chunk;
4635
4636 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004637 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004638 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004639 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004640 goto out_unlock;
4641
4642 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004643 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004644 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004645 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004646 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004647
4648 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004649 chunk = mddev->chunk_sectors;
4650 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004651 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004652
4653 err = -EINVAL;
4654 if (sector_div(temp, chunk))
4655 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004656 }
4657 mddev->resync_max = max;
4658 }
4659 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004660 err = 0;
4661out_unlock:
4662 spin_unlock(&mddev->lock);
4663 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004664}
4665
4666static struct md_sysfs_entry md_max_sync =
4667__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4668
4669static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004670suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004671{
4672 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4673}
4674
4675static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004676suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004677{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004678 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004679 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004680
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004681 err = kstrtoull(buf, 10, &new);
4682 if (err < 0)
4683 return err;
4684 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004685 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004686
NeilBrown67918752014-12-15 12:57:01 +11004687 err = mddev_lock(mddev);
4688 if (err)
4689 return err;
4690 err = -EINVAL;
4691 if (mddev->pers == NULL ||
4692 mddev->pers->quiesce == NULL)
4693 goto unlock;
4694 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004695 mddev->suspend_lo = new;
4696 if (new >= old)
4697 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004698 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004699 else {
4700 /* Expanding suspended region - need to wait */
4701 mddev->pers->quiesce(mddev, 1);
4702 mddev->pers->quiesce(mddev, 0);
4703 }
NeilBrown67918752014-12-15 12:57:01 +11004704 err = 0;
4705unlock:
4706 mddev_unlock(mddev);
4707 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004708}
4709static struct md_sysfs_entry md_suspend_lo =
4710__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4711
NeilBrowne464eaf2006-03-27 01:18:14 -08004712static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004713suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004714{
4715 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4716}
4717
4718static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004719suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004720{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004721 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004722 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004723
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004724 err = kstrtoull(buf, 10, &new);
4725 if (err < 0)
4726 return err;
4727 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004728 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004729
NeilBrown67918752014-12-15 12:57:01 +11004730 err = mddev_lock(mddev);
4731 if (err)
4732 return err;
4733 err = -EINVAL;
4734 if (mddev->pers == NULL ||
4735 mddev->pers->quiesce == NULL)
4736 goto unlock;
4737 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004738 mddev->suspend_hi = new;
4739 if (new <= old)
4740 /* Shrinking suspended region */
4741 mddev->pers->quiesce(mddev, 2);
4742 else {
4743 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004744 mddev->pers->quiesce(mddev, 1);
4745 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004746 }
NeilBrown67918752014-12-15 12:57:01 +11004747 err = 0;
4748unlock:
4749 mddev_unlock(mddev);
4750 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004751}
4752static struct md_sysfs_entry md_suspend_hi =
4753__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4754
NeilBrown08a02ec2007-05-09 02:35:38 -07004755static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004756reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004757{
4758 if (mddev->reshape_position != MaxSector)
4759 return sprintf(page, "%llu\n",
4760 (unsigned long long)mddev->reshape_position);
4761 strcpy(page, "none\n");
4762 return 5;
4763}
4764
4765static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004766reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004767{
NeilBrownc6563a82012-05-21 09:27:00 +10004768 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004769 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004770 int err;
NeilBrown67918752014-12-15 12:57:01 +11004771
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004772 err = kstrtoull(buf, 10, &new);
4773 if (err < 0)
4774 return err;
4775 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004776 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004777 err = mddev_lock(mddev);
4778 if (err)
4779 return err;
4780 err = -EBUSY;
4781 if (mddev->pers)
4782 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004783 mddev->reshape_position = new;
4784 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004785 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004786 mddev->new_level = mddev->level;
4787 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004788 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004789 rdev_for_each(rdev, mddev)
4790 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004791 err = 0;
4792unlock:
4793 mddev_unlock(mddev);
4794 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004795}
4796
4797static struct md_sysfs_entry md_reshape_position =
4798__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4799 reshape_position_store);
4800
Dan Williamsb522adc2009-03-31 15:00:31 +11004801static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004802reshape_direction_show(struct mddev *mddev, char *page)
4803{
4804 return sprintf(page, "%s\n",
4805 mddev->reshape_backwards ? "backwards" : "forwards");
4806}
4807
4808static ssize_t
4809reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4810{
4811 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004812 int err;
4813
NeilBrown2c810cd2012-05-21 09:27:00 +10004814 if (cmd_match(buf, "forwards"))
4815 backwards = 0;
4816 else if (cmd_match(buf, "backwards"))
4817 backwards = 1;
4818 else
4819 return -EINVAL;
4820 if (mddev->reshape_backwards == backwards)
4821 return len;
4822
NeilBrown67918752014-12-15 12:57:01 +11004823 err = mddev_lock(mddev);
4824 if (err)
4825 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004826 /* check if we are allowed to change */
4827 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004828 err = -EBUSY;
4829 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004830 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004831 err = -EINVAL;
4832 else
4833 mddev->reshape_backwards = backwards;
4834 mddev_unlock(mddev);
4835 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004836}
4837
4838static struct md_sysfs_entry md_reshape_direction =
4839__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4840 reshape_direction_store);
4841
4842static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004843array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004844{
4845 if (mddev->external_size)
4846 return sprintf(page, "%llu\n",
4847 (unsigned long long)mddev->array_sectors/2);
4848 else
4849 return sprintf(page, "default\n");
4850}
4851
4852static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004853array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004854{
4855 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004856 int err;
4857
4858 err = mddev_lock(mddev);
4859 if (err)
4860 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004861
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004862 /* cluster raid doesn't support change array_sectors */
4863 if (mddev_is_clustered(mddev))
4864 return -EINVAL;
4865
Dan Williamsb522adc2009-03-31 15:00:31 +11004866 if (strncmp(buf, "default", 7) == 0) {
4867 if (mddev->pers)
4868 sectors = mddev->pers->size(mddev, 0, 0);
4869 else
4870 sectors = mddev->array_sectors;
4871
4872 mddev->external_size = 0;
4873 } else {
4874 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004875 err = -EINVAL;
4876 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4877 err = -E2BIG;
4878 else
4879 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004880 }
4881
NeilBrown67918752014-12-15 12:57:01 +11004882 if (!err) {
4883 mddev->array_sectors = sectors;
4884 if (mddev->pers) {
4885 set_capacity(mddev->gendisk, mddev->array_sectors);
4886 revalidate_disk(mddev->gendisk);
4887 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004888 }
NeilBrown67918752014-12-15 12:57:01 +11004889 mddev_unlock(mddev);
4890 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004891}
4892
4893static struct md_sysfs_entry md_array_size =
4894__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4895 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004896
NeilBrowneae17012005-11-08 21:39:23 -08004897static struct attribute *md_default_attrs[] = {
4898 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004899 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004900 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004901 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004902 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004903 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004904 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004905 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004906 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004907 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004908 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004909 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004910 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004911 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004912 NULL,
4913};
4914
4915static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004916 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004917 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004918 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004919 &md_sync_min.attr,
4920 &md_sync_max.attr,
4921 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004922 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004923 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004924 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004925 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004926 &md_suspend_lo.attr,
4927 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004928 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004929 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004930 NULL,
4931};
NeilBrown411036f2005-11-08 21:39:40 -08004932static struct attribute_group md_redundancy_group = {
4933 .name = NULL,
4934 .attrs = md_redundancy_attrs,
4935};
4936
NeilBrowneae17012005-11-08 21:39:23 -08004937static ssize_t
4938md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4939{
4940 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004941 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004942 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004943
4944 if (!entry->show)
4945 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004946 spin_lock(&all_mddevs_lock);
4947 if (list_empty(&mddev->all_mddevs)) {
4948 spin_unlock(&all_mddevs_lock);
4949 return -EBUSY;
4950 }
4951 mddev_get(mddev);
4952 spin_unlock(&all_mddevs_lock);
4953
NeilBrownb7b17c92014-12-15 12:56:59 +11004954 rv = entry->show(mddev, page);
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 ssize_t
4960md_attr_store(struct kobject *kobj, struct attribute *attr,
4961 const char *page, size_t length)
4962{
4963 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004964 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004965 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004966
4967 if (!entry->store)
4968 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004969 if (!capable(CAP_SYS_ADMIN))
4970 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004971 spin_lock(&all_mddevs_lock);
4972 if (list_empty(&mddev->all_mddevs)) {
4973 spin_unlock(&all_mddevs_lock);
4974 return -EBUSY;
4975 }
4976 mddev_get(mddev);
4977 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004978 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004979 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004980 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004981}
4982
4983static void md_free(struct kobject *ko)
4984{
NeilBrownfd01b882011-10-11 16:47:53 +11004985 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004986
4987 if (mddev->sysfs_state)
4988 sysfs_put(mddev->sysfs_state);
4989
NeilBrown6cd18e72015-04-27 14:12:22 +10004990 if (mddev->queue)
4991 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004992 if (mddev->gendisk) {
4993 del_gendisk(mddev->gendisk);
4994 put_disk(mddev->gendisk);
4995 }
NeilBrowna21d1502009-01-09 08:31:09 +11004996
NeilBrowneae17012005-11-08 21:39:23 -08004997 kfree(mddev);
4998}
4999
Emese Revfy52cf25d2010-01-19 02:58:23 +01005000static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005001 .show = md_attr_show,
5002 .store = md_attr_store,
5003};
5004static struct kobj_type md_ktype = {
5005 .release = md_free,
5006 .sysfs_ops = &md_sysfs_ops,
5007 .default_attrs = md_default_attrs,
5008};
5009
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010int mdp_major = 0;
5011
Dan Williams5fd3a172009-03-04 00:57:25 -07005012static void mddev_delayed_delete(struct work_struct *ws)
5013{
NeilBrownfd01b882011-10-11 16:47:53 +11005014 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005015
NeilBrown43a70502009-12-14 12:49:55 +11005016 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005017 kobject_del(&mddev->kobj);
5018 kobject_put(&mddev->kobj);
5019}
5020
NeilBrownefeb53c2009-01-09 08:31:10 +11005021static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005023 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005024 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005026 int partitioned;
5027 int shift;
5028 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005029 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
5031 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005032 return -ENODEV;
5033
5034 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5035 shift = partitioned ? MdpMinorShift : 0;
5036 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
Tejun Heoe804ac72010-10-15 15:36:08 +02005038 /* wait for any previous instance of this device to be
5039 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005040 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005041 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005042
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005043 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005044 error = -EEXIST;
5045 if (mddev->gendisk)
5046 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005047
5048 if (name) {
5049 /* Need to ensure that 'name' is not a duplicate.
5050 */
NeilBrownfd01b882011-10-11 16:47:53 +11005051 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005052 spin_lock(&all_mddevs_lock);
5053
5054 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5055 if (mddev2->gendisk &&
5056 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5057 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005058 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005059 }
5060 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 }
NeilBrown8b765392009-01-09 08:31:08 +11005062
NeilBrown0909dc42009-07-01 12:27:21 +10005063 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005064 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005065 if (!mddev->queue)
5066 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005067 mddev->queue->queuedata = mddev;
5068
NeilBrown409c57f2009-03-31 14:39:39 +11005069 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005070 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005071
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 disk = alloc_disk(1 << shift);
5073 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005074 blk_cleanup_queue(mddev->queue);
5075 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005076 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005078 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005080 if (name)
5081 strcpy(disk->disk_name, name);
5082 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005084 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 disk->fops = &md_fops;
5087 disk->private_data = mddev;
5088 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005089 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005090 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005091 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005092 * remove it now.
5093 */
5094 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005096 /* As soon as we call add_disk(), another thread could get
5097 * through to md_open, so make sure it doesn't get too far
5098 */
5099 mutex_lock(&mddev->open_mutex);
5100 add_disk(disk);
5101
Tejun Heoed9e1982008-08-25 19:56:05 +09005102 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5103 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005104 if (error) {
5105 /* This isn't possible, but as kobject_init_and_add is marked
5106 * __must_check, we must do something with the result
5107 */
NeilBrown9d487392016-11-02 14:16:49 +11005108 pr_debug("md: cannot register %s/md - name in use\n",
5109 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005110 error = 0;
5111 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005112 if (mddev->kobj.sd &&
5113 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005114 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005115 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005116 abort:
5117 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005118 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005119 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005120 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005121 }
NeilBrownd3374822009-01-09 08:31:10 +11005122 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005123 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005124}
5125
5126static struct kobject *md_probe(dev_t dev, int *part, void *data)
5127{
5128 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 return NULL;
5130}
5131
NeilBrownefeb53c2009-01-09 08:31:10 +11005132static int add_named_array(const char *val, struct kernel_param *kp)
5133{
5134 /* val must be "md_*" where * is not all digits.
5135 * We allocate an array with a large free minor number, and
5136 * set the name to val. val must not already be an active name.
5137 */
5138 int len = strlen(val);
5139 char buf[DISK_NAME_LEN];
5140
5141 while (len && val[len-1] == '\n')
5142 len--;
5143 if (len >= DISK_NAME_LEN)
5144 return -E2BIG;
5145 strlcpy(buf, val, len+1);
5146 if (strncmp(buf, "md_", 3) != 0)
5147 return -EINVAL;
5148 return md_alloc(0, buf);
5149}
5150
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151static void md_safemode_timeout(unsigned long data)
5152{
NeilBrownfd01b882011-10-11 16:47:53 +11005153 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154
Neil Brown0fd62b82008-06-28 08:31:36 +10005155 if (!atomic_read(&mddev->writes_pending)) {
5156 mddev->safemode = 1;
5157 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10005158 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 md_wakeup_thread(mddev->thread);
5161}
5162
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005163static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164
NeilBrownfd01b882011-10-11 16:47:53 +11005165int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166{
NeilBrown2604b702006-01-06 00:20:36 -08005167 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005168 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005169 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170
NeilBrowna757e642005-04-16 15:26:42 -07005171 if (list_empty(&mddev->disks))
5172 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
5175 if (mddev->pers)
5176 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005177 /* Cannot run until previous stop completes properly */
5178 if (mddev->sysfs_active)
5179 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005180
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 /*
5182 * Analyze all RAID superblock(s)
5183 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005184 if (!mddev->raid_disks) {
5185 if (!mddev->persistent)
5186 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005187 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189
NeilBrownd9d166c2006-01-06 00:20:51 -08005190 if (mddev->level != LEVEL_NONE)
5191 request_module("md-level-%d", mddev->level);
5192 else if (mddev->clevel[0])
5193 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194
5195 /*
5196 * Drop all container device buffers, from now on
5197 * the only valid external interface is through the md
5198 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005200 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005201 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 continue;
5203 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005204 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005205
5206 /* perform some consistency tests on the device.
5207 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005208 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005209 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005210 if (rdev->meta_bdev) {
5211 /* Nothing to check */;
5212 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005213 if (mddev->dev_sectors &&
5214 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005215 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005216 pr_warn("md: %s: data overlaps metadata\n",
5217 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005218 return -EINVAL;
5219 }
5220 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005221 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005222 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005223 pr_warn("md: %s: metadata overlaps data\n",
5224 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005225 return -EINVAL;
5226 }
5227 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005228 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 }
5230
NeilBrowna167f662010-10-26 18:31:13 +11005231 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005232 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005233
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005235 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005236 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005238 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005239 pr_warn("md: personality for level %d is not loaded!\n",
5240 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005241 else
NeilBrown9d487392016-11-02 14:16:49 +11005242 pr_warn("md: personality for level %s is not loaded!\n",
5243 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 return -EINVAL;
5245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005247 if (mddev->level != pers->level) {
5248 mddev->level = pers->level;
5249 mddev->new_level = pers->level;
5250 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005251 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
NeilBrownf6705572006-03-27 01:18:11 -08005253 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005254 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005255 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005256 module_put(pers->owner);
5257 return -EINVAL;
5258 }
5259
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005260 if (pers->sync_request) {
5261 /* Warn if this is a potentially silly
5262 * configuration.
5263 */
5264 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005265 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005266 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005267
NeilBrowndafb20f2012-03-19 12:46:39 +11005268 rdev_for_each(rdev, mddev)
5269 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005270 if (rdev < rdev2 &&
5271 rdev->bdev->bd_contains ==
5272 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005273 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5274 mdname(mddev),
5275 bdevname(rdev->bdev,b),
5276 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005277 warned = 1;
5278 }
5279 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005280
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005281 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005282 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005283 }
5284
NeilBrown657390d2005-08-26 18:34:16 -07005285 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005286 /* may be over-ridden by personality */
5287 mddev->resync_max_sectors = mddev->dev_sectors;
5288
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005289 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
NeilBrown0f9552b52009-12-30 12:08:50 +11005291 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005292 mddev->ro = 2; /* read-only, but switch on first write */
5293
NeilBrown36d091f2014-12-15 12:56:58 +11005294 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005295 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005296 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005297 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005298 WARN_ONCE(!mddev->external_size,
5299 "%s: default size too small, but 'external_size' not in effect?\n",
5300 __func__);
5301 pr_warn("md: invalid array_size %llu > default size %llu\n",
5302 (unsigned long long)mddev->array_sectors / 2,
5303 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005304 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005305 }
NeilBrown36d091f2014-12-15 12:56:58 +11005306 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005307 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005308 struct bitmap *bitmap;
5309
5310 bitmap = bitmap_create(mddev, -1);
5311 if (IS_ERR(bitmap)) {
5312 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005313 pr_warn("%s: failed to create bitmap (%d)\n",
5314 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005315 } else
5316 mddev->bitmap = bitmap;
5317
NeilBrownb15c2e52006-01-06 00:20:16 -08005318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005320 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005321 if (mddev->private)
5322 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005323 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005324 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005325 bitmap_destroy(mddev);
5326 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 }
NeilBrown5c675f82014-12-15 12:56:56 +11005328 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005329 bool nonrot = true;
5330
5331 rdev_for_each(rdev, mddev) {
5332 if (rdev->raid_disk >= 0 &&
5333 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5334 nonrot = false;
5335 break;
5336 }
5337 }
5338 if (mddev->degraded)
5339 nonrot = false;
5340 if (nonrot)
5341 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5342 else
5343 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
NeilBrown5c675f82014-12-15 12:56:56 +11005344 mddev->queue->backing_dev_info.congested_data = mddev;
5345 mddev->queue->backing_dev_info.congested_fn = md_congested;
5346 }
NeilBrown36d091f2014-12-15 12:56:58 +11005347 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005348 if (mddev->kobj.sd &&
5349 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005350 pr_warn("md: cannot register extra attributes for %s\n",
5351 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005352 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005353 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005354 mddev->ro = 0;
5355
NeilBrownf72ffdd2014-09-30 14:23:59 +10005356 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005357 atomic_set(&mddev->max_corr_read_errors,
5358 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005360 if (mddev_is_clustered(mddev))
5361 mddev->safemode_delay = 0;
5362 else
5363 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005365 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005366 spin_lock(&mddev->lock);
5367 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005368 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005369 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005370 if (rdev->raid_disk >= 0)
5371 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005372 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005373
NeilBrowna4a3d262015-07-17 11:57:30 +10005374 if (mddev->degraded && !mddev->ro)
5375 /* This ensures that recovering status is reported immediately
5376 * via sysfs - until a lack of spares is confirmed.
5377 */
5378 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005380
NeilBrown7a0a5352013-08-27 16:28:23 +10005381 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07005382 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383
NeilBrownd7603b72006-01-06 00:20:30 -08005384 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005385 sysfs_notify_dirent_safe(mddev->sysfs_state);
5386 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005387 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 return 0;
5389}
NeilBrown390ee602010-06-01 19:37:27 +10005390EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391
NeilBrownfd01b882011-10-11 16:47:53 +11005392static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005393{
5394 int err;
5395
5396 err = md_run(mddev);
5397 if (err)
5398 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005399 err = bitmap_load(mddev);
5400 if (err) {
5401 bitmap_destroy(mddev);
5402 goto out;
5403 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005404
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005405 if (mddev_is_clustered(mddev))
5406 md_allow_write(mddev);
5407
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005408 md_wakeup_thread(mddev->thread);
5409 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5410
NeilBrownfe60b012010-03-29 11:10:42 +11005411 set_capacity(mddev->gendisk, mddev->array_sectors);
5412 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005413 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005414 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5415out:
5416 return err;
5417}
5418
NeilBrownfd01b882011-10-11 16:47:53 +11005419static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420{
5421 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
Andre Noll80fab1d2008-07-11 22:02:21 +10005423 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005425 return -ENXIO;
5426 if (!mddev->pers)
5427 return -EINVAL;
5428 if (!mddev->ro)
5429 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005430 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5431 struct md_rdev *rdev;
5432 bool has_journal = false;
5433
5434 rcu_read_lock();
5435 rdev_for_each_rcu(rdev, mddev) {
5436 if (test_bit(Journal, &rdev->flags) &&
5437 !test_bit(Faulty, &rdev->flags)) {
5438 has_journal = true;
5439 break;
5440 }
5441 }
5442 rcu_read_unlock();
5443
5444 /* Don't restart rw with journal missing/faulty */
5445 if (!has_journal)
5446 return -EINVAL;
5447 }
5448
Andre Noll80fab1d2008-07-11 22:02:21 +10005449 mddev->safemode = 0;
5450 mddev->ro = 0;
5451 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11005452 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10005453 /* Kick recovery or resync if necessary */
5454 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5455 md_wakeup_thread(mddev->thread);
5456 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005457 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459}
5460
NeilBrownfd01b882011-10-11 16:47:53 +11005461static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005462{
5463 mddev->array_sectors = 0;
5464 mddev->external_size = 0;
5465 mddev->dev_sectors = 0;
5466 mddev->raid_disks = 0;
5467 mddev->recovery_cp = 0;
5468 mddev->resync_min = 0;
5469 mddev->resync_max = MaxSector;
5470 mddev->reshape_position = MaxSector;
5471 mddev->external = 0;
5472 mddev->persistent = 0;
5473 mddev->level = LEVEL_NONE;
5474 mddev->clevel[0] = 0;
5475 mddev->flags = 0;
5476 mddev->ro = 0;
5477 mddev->metadata_type[0] = 0;
5478 mddev->chunk_sectors = 0;
5479 mddev->ctime = mddev->utime = 0;
5480 mddev->layout = 0;
5481 mddev->max_disks = 0;
5482 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005483 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005484 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005485 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005486 mddev->new_level = LEVEL_NONE;
5487 mddev->new_layout = 0;
5488 mddev->new_chunk_sectors = 0;
5489 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005490 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005491 mddev->suspend_lo = mddev->suspend_hi = 0;
5492 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5493 mddev->recovery = 0;
5494 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005495 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005496 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005497 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005498 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005499 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005500 mddev->bitmap_info.offset = 0;
5501 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005502 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005503 mddev->bitmap_info.chunksize = 0;
5504 mddev->bitmap_info.daemon_sleep = 0;
5505 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005506 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005507}
5508
NeilBrownfd01b882011-10-11 16:47:53 +11005509static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005510{
NeilBrown6b6204e2013-05-09 09:48:30 +10005511 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005512 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005513 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005514 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005515 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005516 }
5517
5518 del_timer_sync(&mddev->safemode_timer);
5519
Shaohua Li034e33f2016-11-21 10:29:19 -08005520 if (mddev->pers && mddev->pers->quiesce) {
5521 mddev->pers->quiesce(mddev, 1);
5522 mddev->pers->quiesce(mddev, 0);
5523 }
NeilBrowna047e122010-03-29 12:07:53 +11005524 bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005525
NeilBrownb6d428c2013-04-24 11:42:42 +10005526 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005527 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5528 (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005529 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005530 if (!mddev_is_clustered(mddev))
5531 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005532 md_update_sb(mddev, 1);
5533 }
5534}
NeilBrowndefad612011-01-14 09:14:33 +11005535
NeilBrownfd01b882011-10-11 16:47:53 +11005536void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005537{
NeilBrown29f097c2013-11-14 17:54:51 +11005538 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005539 __md_stop_writes(mddev);
5540 mddev_unlock(mddev);
5541}
NeilBrown390ee602010-06-01 19:37:27 +10005542EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005543
NeilBrown5aa61f42014-12-15 12:56:57 +11005544static void mddev_detach(struct mddev *mddev)
5545{
5546 struct bitmap *bitmap = mddev->bitmap;
5547 /* wait for behind writes to complete */
5548 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005549 pr_debug("md:%s: behind writes in progress - waiting to stop.\n",
5550 mdname(mddev));
NeilBrown5aa61f42014-12-15 12:56:57 +11005551 /* need to kick something here to make sure I/O goes? */
5552 wait_event(bitmap->behind_wait,
5553 atomic_read(&bitmap->behind_writes) == 0);
5554 }
NeilBrown36d091f2014-12-15 12:56:58 +11005555 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005556 mddev->pers->quiesce(mddev, 1);
5557 mddev->pers->quiesce(mddev, 0);
5558 }
5559 md_unregister_thread(&mddev->thread);
5560 if (mddev->queue)
5561 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5562}
5563
NeilBrown5eff3c42012-11-19 10:47:48 +11005564static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005565{
NeilBrown36d091f2014-12-15 12:56:58 +11005566 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005567 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005568 /* Ensure ->event_work is done */
5569 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005570 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005571 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005572 spin_unlock(&mddev->lock);
5573 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005574 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005575 if (pers->sync_request && mddev->to_remove == NULL)
5576 mddev->to_remove = &md_redundancy_group;
5577 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005578 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005579}
NeilBrown5eff3c42012-11-19 10:47:48 +11005580
5581void md_stop(struct mddev *mddev)
5582{
5583 /* stop the array and free an attached data structures.
5584 * This is called from dm-raid
5585 */
5586 __md_stop(mddev);
5587 bitmap_destroy(mddev);
5588 if (mddev->bio_set)
5589 bioset_free(mddev->bio_set);
5590}
5591
NeilBrown390ee602010-06-01 19:37:27 +10005592EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005593
NeilBrowna05b7ea2012-07-19 15:59:18 +10005594static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005595{
5596 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005597 int did_freeze = 0;
5598
5599 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5600 did_freeze = 1;
5601 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5602 md_wakeup_thread(mddev->thread);
5603 }
NeilBrownf851b602014-12-11 10:02:10 +11005604 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005605 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005606 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005607 /* Thread might be blocked waiting for metadata update
5608 * which will now never happen */
5609 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005610
NeilBrown88724bf2015-09-24 14:00:51 +10005611 if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags))
5612 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005613 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005614 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5615 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005616 wait_event(mddev->sb_wait,
5617 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005618 mddev_lock_nointr(mddev);
5619
NeilBrowna4bd82d2010-03-29 13:23:10 +11005620 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005621 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005622 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005623 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005624 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005625 if (did_freeze) {
5626 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005627 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005628 md_wakeup_thread(mddev->thread);
5629 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005630 err = -EBUSY;
5631 goto out;
5632 }
5633 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005634 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005635
5636 err = -ENXIO;
5637 if (mddev->ro==1)
5638 goto out;
5639 mddev->ro = 1;
5640 set_disk_ro(mddev->gendisk, 1);
5641 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005642 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5643 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005644 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005645 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005646 }
5647out:
5648 mutex_unlock(&mddev->open_mutex);
5649 return err;
5650}
5651
NeilBrown9e653b62006-06-26 00:27:58 -07005652/* mode:
5653 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005654 * 2 - stop but do not disassemble array
5655 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005656static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005657 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005660 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005661 int did_freeze = 0;
5662
5663 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5664 did_freeze = 1;
5665 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5666 md_wakeup_thread(mddev->thread);
5667 }
NeilBrownf851b602014-12-11 10:02:10 +11005668 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005669 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005670 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005671 /* Thread might be blocked waiting for metadata update
5672 * which will now never happen */
5673 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005674
NeilBrown30b8feb2013-11-14 15:16:17 +11005675 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005676 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5677 !test_bit(MD_RECOVERY_RUNNING,
5678 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005679 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
NeilBrownc8c00a62009-08-10 12:50:52 +10005681 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005682 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005683 mddev->sysfs_active ||
5684 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005685 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005686 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005687 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005688 if (did_freeze) {
5689 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005690 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005691 md_wakeup_thread(mddev->thread);
5692 }
NeilBrown260fa032013-08-27 16:44:13 +10005693 return -EBUSY;
5694 }
NeilBrown6e17b022010-08-07 21:41:19 +10005695 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005696 if (mddev->ro)
5697 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005698
NeilBrowndefad612011-01-14 09:14:33 +11005699 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005700 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005701 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005702
NeilBrowna4bd82d2010-03-29 13:23:10 +11005703 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005704 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005705
NeilBrowndafb20f2012-03-19 12:46:39 +11005706 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005707 if (rdev->raid_disk >= 0)
5708 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005709
NeilBrowna4bd82d2010-03-29 13:23:10 +11005710 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005711 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005712 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005713 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005714
NeilBrowna4bd82d2010-03-29 13:23:10 +11005715 if (mddev->ro)
5716 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005717 } else
5718 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 /*
5720 * Free resources if final stop
5721 */
NeilBrown9e653b62006-06-26 00:27:58 -07005722 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005723 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724
NeilBrown978f9462006-02-02 14:28:05 -08005725 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005726 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005727 struct file *f = mddev->bitmap_info.file;
5728 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005729 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005730 spin_unlock(&mddev->lock);
5731 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005732 }
NeilBrownc3d97142009-12-14 12:49:52 +11005733 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005734
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 export_array(mddev);
5736
NeilBrown6177b472010-03-29 11:37:13 +11005737 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11005738 if (mddev->hold_active == UNTIL_STOP)
5739 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005740 }
NeilBrownd7603b72006-01-06 00:20:30 -08005741 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005742 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005743 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744}
5745
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005746#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005747static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748{
NeilBrown3cb03002011-10-11 16:45:26 +11005749 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 int err;
5751
NeilBrowna757e642005-04-16 15:26:42 -07005752 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754
NeilBrown9d487392016-11-02 14:16:49 +11005755 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
NeilBrowndafb20f2012-03-19 12:46:39 +11005757 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11005759 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 }
NeilBrown9d487392016-11-02 14:16:49 +11005761 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762
NeilBrownd710e132008-10-13 11:55:12 +11005763 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11005765 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005766 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 }
5768}
5769
5770/*
5771 * lets try to run arrays based on all disks that have arrived
5772 * until now. (those are in pending_raid_disks)
5773 *
5774 * the method: pick the first pending disk, collect all disks with
5775 * the same UUID, remove all from the pending list and put them into
5776 * the 'same_array' list. Then order this list based on superblock
5777 * update time (freshest comes first), kick out 'old' disks and
5778 * compare superblocks. If everything's fine then run it.
5779 *
5780 * If "unit" is allocated, then bump its reference count
5781 */
5782static void autorun_devices(int part)
5783{
NeilBrown3cb03002011-10-11 16:45:26 +11005784 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005785 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 char b[BDEVNAME_SIZE];
5787
NeilBrown9d487392016-11-02 14:16:49 +11005788 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005790 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005792 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005794 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795
NeilBrown9d487392016-11-02 14:16:49 +11005796 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005798 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005800 pr_debug("md: adding %s ...\n",
5801 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 list_move(&rdev->same_set, &candidates);
5803 }
5804 /*
5805 * now we have a set of devices, with all of them having
5806 * mostly sane superblocks. It's time to allocate the
5807 * mddev.
5808 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005809 if (part) {
5810 dev = MKDEV(mdp_major,
5811 rdev0->preferred_minor << MdpMinorShift);
5812 unit = MINOR(dev) >> MdpMinorShift;
5813 } else {
5814 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5815 unit = MINOR(dev);
5816 }
5817 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11005818 pr_warn("md: unit number in %s is bad: %d\n",
5819 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 break;
5821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822
5823 md_probe(dev, NULL, NULL);
5824 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005825 if (!mddev || !mddev->gendisk) {
5826 if (mddev)
5827 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 break;
5829 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005830 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11005831 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 else if (mddev->raid_disks || mddev->major_version
5833 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11005834 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 mdname(mddev), bdevname(rdev0->bdev,b));
5836 mddev_unlock(mddev);
5837 } else {
NeilBrown9d487392016-11-02 14:16:49 +11005838 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005839 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005840 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 list_del_init(&rdev->same_set);
5842 if (bind_rdev_to_array(rdev, mddev))
5843 export_rdev(rdev);
5844 }
5845 autorun_array(mddev);
5846 mddev_unlock(mddev);
5847 }
5848 /* on success, candidates will be empty, on error
5849 * it won't...
5850 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005851 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005852 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 mddev_put(mddev);
5856 }
NeilBrown9d487392016-11-02 14:16:49 +11005857 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005859#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860
NeilBrownf72ffdd2014-09-30 14:23:59 +10005861static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862{
5863 mdu_version_t ver;
5864
5865 ver.major = MD_MAJOR_VERSION;
5866 ver.minor = MD_MINOR_VERSION;
5867 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5868
5869 if (copy_to_user(arg, &ver, sizeof(ver)))
5870 return -EFAULT;
5871
5872 return 0;
5873}
5874
NeilBrownf72ffdd2014-09-30 14:23:59 +10005875static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876{
5877 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005878 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005879 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880
NeilBrown1ca69c42012-10-11 13:37:33 +11005881 nr = working = insync = failed = spare = 0;
5882 rcu_read_lock();
5883 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005885 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 failed++;
5887 else {
5888 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005889 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005890 insync++;
Song Liub347af82016-08-11 17:14:45 -07005891 else if (test_bit(Journal, &rdev->flags))
5892 /* TODO: add journal count to md_u.h */
5893 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 else
5895 spare++;
5896 }
5897 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005898 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
5900 info.major_version = mddev->major_version;
5901 info.minor_version = mddev->minor_version;
5902 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005903 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005905 info.size = mddev->dev_sectors / 2;
5906 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005907 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 info.nr_disks = nr;
5909 info.raid_disks = mddev->raid_disks;
5910 info.md_minor = mddev->md_minor;
5911 info.not_persistent= !mddev->persistent;
5912
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005913 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 info.state = 0;
5915 if (mddev->in_sync)
5916 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005917 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005918 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005919 if (mddev_is_clustered(mddev))
5920 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005921 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 info.working_disks = working;
5923 info.failed_disks = failed;
5924 info.spare_disks = spare;
5925
5926 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005927 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928
5929 if (copy_to_user(arg, &info, sizeof(info)))
5930 return -EFAULT;
5931
5932 return 0;
5933}
5934
NeilBrownf72ffdd2014-09-30 14:23:59 +10005935static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005936{
5937 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005938 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005939 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005940
Benjamin Randazzob6878d92015-07-25 16:36:50 +02005941 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005942 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005943 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005944
NeilBrown32a76272005-06-21 17:17:14 -07005945 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005946 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02005947 /* bitmap enabled */
5948 if (mddev->bitmap_info.file) {
5949 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5950 sizeof(file->pathname));
5951 if (IS_ERR(ptr))
5952 err = PTR_ERR(ptr);
5953 else
5954 memmove(file->pathname, ptr,
5955 sizeof(file->pathname)-(ptr-file->pathname));
5956 }
NeilBrown4af1a042014-12-15 12:57:00 +11005957 spin_unlock(&mddev->lock);
5958
5959 if (err == 0 &&
5960 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005961 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005962
NeilBrown32a76272005-06-21 17:17:14 -07005963 kfree(file);
5964 return err;
5965}
5966
NeilBrownf72ffdd2014-09-30 14:23:59 +10005967static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968{
5969 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005970 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971
5972 if (copy_from_user(&info, arg, sizeof(info)))
5973 return -EFAULT;
5974
NeilBrown1ca69c42012-10-11 13:37:33 +11005975 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005976 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 if (rdev) {
5978 info.major = MAJOR(rdev->bdev->bd_dev);
5979 info.minor = MINOR(rdev->bdev->bd_dev);
5980 info.raid_disk = rdev->raid_disk;
5981 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005982 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005984 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 info.state |= (1<<MD_DISK_ACTIVE);
5986 info.state |= (1<<MD_DISK_SYNC);
5987 }
Shaohua Li9efdca12015-10-12 16:59:50 -07005988 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07005989 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005990 if (test_bit(WriteMostly, &rdev->flags))
5991 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11005992 if (test_bit(FailFast, &rdev->flags))
5993 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 } else {
5995 info.major = info.minor = 0;
5996 info.raid_disk = -1;
5997 info.state = (1<<MD_DISK_REMOVED);
5998 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005999 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000
6001 if (copy_to_user(arg, &info, sizeof(info)))
6002 return -EFAULT;
6003
6004 return 0;
6005}
6006
NeilBrownf72ffdd2014-09-30 14:23:59 +10006007static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008{
6009 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006010 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 dev_t dev = MKDEV(info->major,info->minor);
6012
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006013 if (mddev_is_clustered(mddev) &&
6014 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006015 pr_warn("%s: Cannot add to clustered mddev.\n",
6016 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006017 return -EINVAL;
6018 }
6019
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6021 return -EOVERFLOW;
6022
6023 if (!mddev->raid_disks) {
6024 int err;
6025 /* expecting a device which has a superblock */
6026 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6027 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006028 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 PTR_ERR(rdev));
6030 return PTR_ERR(rdev);
6031 }
6032 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006033 struct md_rdev *rdev0
6034 = list_entry(mddev->disks.next,
6035 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006036 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 .load_super(rdev, rdev0, mddev->minor_version);
6038 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006039 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006040 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 bdevname(rdev0->bdev,b2));
6042 export_rdev(rdev);
6043 return -EINVAL;
6044 }
6045 }
6046 err = bind_rdev_to_array(rdev, mddev);
6047 if (err)
6048 export_rdev(rdev);
6049 return err;
6050 }
6051
6052 /*
6053 * add_new_disk can be used once the array is assembled
6054 * to add "hot spares". They must already have a superblock
6055 * written
6056 */
6057 if (mddev->pers) {
6058 int err;
6059 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006060 pr_warn("%s: personality does not support diskops!\n",
6061 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 return -EINVAL;
6063 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006064 if (mddev->persistent)
6065 rdev = md_import_device(dev, mddev->major_version,
6066 mddev->minor_version);
6067 else
6068 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006070 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 PTR_ERR(rdev));
6072 return PTR_ERR(rdev);
6073 }
NeilBrown1a855a02010-12-09 16:36:28 +11006074 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006075 if (!mddev->persistent) {
6076 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006077 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006078 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006079 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006080 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006081 } else
NeilBrown41158c72005-06-21 17:17:25 -07006082 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006083 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006084 } else
6085 super_types[mddev->major_version].
6086 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006087 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006088 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006089 /* This was a hot-add request, but events doesn't
6090 * match, so reject it.
6091 */
6092 export_rdev(rdev);
6093 return -EINVAL;
6094 }
6095
NeilBrownb2d444d2005-11-08 21:39:31 -08006096 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006097 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6098 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006099 else
6100 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006101 if (info->state & (1<<MD_DISK_FAILFAST))
6102 set_bit(FailFast, &rdev->flags);
6103 else
6104 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006105
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006106 if (info->state & (1<<MD_DISK_JOURNAL)) {
6107 struct md_rdev *rdev2;
6108 bool has_journal = false;
6109
6110 /* make sure no existing journal disk */
6111 rdev_for_each(rdev2, mddev) {
6112 if (test_bit(Journal, &rdev2->flags)) {
6113 has_journal = true;
6114 break;
6115 }
6116 }
6117 if (has_journal) {
6118 export_rdev(rdev);
6119 return -EBUSY;
6120 }
Song Liubac624f2015-08-13 14:31:55 -07006121 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006122 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006123 /*
6124 * check whether the device shows up in other nodes
6125 */
6126 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006127 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006128 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006129 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006130 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006131 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006132 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006133 export_rdev(rdev);
6134 return err;
6135 }
6136 }
6137 }
6138
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 rdev->raid_disk = -1;
6140 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006141
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 if (err)
6143 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006144
6145 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006146 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6147 if (!err) {
6148 err = md_cluster_ops->new_disk_ack(mddev,
6149 err == 0);
6150 if (err)
6151 md_kick_rdev_from_array(rdev);
6152 }
6153 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006154 if (err)
6155 md_cluster_ops->add_new_disk_cancel(mddev);
6156 else
6157 err = add_bound_rdev(rdev);
6158 }
6159
6160 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006161 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006162
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 return err;
6164 }
6165
6166 /* otherwise, add_new_disk is only allowed
6167 * for major_version==0 superblocks
6168 */
6169 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006170 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171 return -EINVAL;
6172 }
6173
6174 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6175 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006176 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006178 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 PTR_ERR(rdev));
6180 return PTR_ERR(rdev);
6181 }
6182 rdev->desc_nr = info->number;
6183 if (info->raid_disk < mddev->raid_disks)
6184 rdev->raid_disk = info->raid_disk;
6185 else
6186 rdev->raid_disk = -1;
6187
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006189 if (info->state & (1<<MD_DISK_SYNC))
6190 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006192 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6193 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006194 if (info->state & (1<<MD_DISK_FAILFAST))
6195 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006196
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006198 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006199 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6200 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006201 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006202 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203
NeilBrown2bf071b2006-01-06 00:20:55 -08006204 err = bind_rdev_to_array(rdev, mddev);
6205 if (err) {
6206 export_rdev(rdev);
6207 return err;
6208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 }
6210
6211 return 0;
6212}
6213
NeilBrownf72ffdd2014-09-30 14:23:59 +10006214static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215{
6216 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006217 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 rdev = find_rdev(mddev, dev);
6220 if (!rdev)
6221 return -ENXIO;
6222
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006223 if (rdev->raid_disk < 0)
6224 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006225
NeilBrown3ea8929d2013-04-24 11:42:41 +10006226 clear_bit(Blocked, &rdev->flags);
6227 remove_and_add_spares(mddev, rdev);
6228
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 if (rdev->raid_disk >= 0)
6230 goto busy;
6231
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006232kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006233 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006234 md_cluster_ops->remove_disk(mddev, rdev);
6235
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006236 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11006237 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6238 if (mddev->thread)
6239 md_wakeup_thread(mddev->thread);
6240 else
6241 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006242 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
6244 return 0;
6245busy:
NeilBrown9d487392016-11-02 14:16:49 +11006246 pr_debug("md: cannot remove active disk %s from %s ...\n",
6247 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 return -EBUSY;
6249}
6250
NeilBrownf72ffdd2014-09-30 14:23:59 +10006251static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252{
6253 char b[BDEVNAME_SIZE];
6254 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006255 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256
6257 if (!mddev->pers)
6258 return -ENODEV;
6259
6260 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006261 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 mdname(mddev));
6263 return -EINVAL;
6264 }
6265 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006266 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 mdname(mddev));
6268 return -EINVAL;
6269 }
6270
NeilBrownd710e132008-10-13 11:55:12 +11006271 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006273 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 PTR_ERR(rdev));
6275 return -EINVAL;
6276 }
6277
6278 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006279 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006281 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282
NeilBrown8190e752009-06-18 08:48:58 +10006283 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284
NeilBrownb2d444d2005-11-08 21:39:31 -08006285 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006286 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 bdevname(rdev->bdev,b), mdname(mddev));
6288 err = -EINVAL;
6289 goto abort_export;
6290 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006291
NeilBrownb2d444d2005-11-08 21:39:31 -08006292 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006294 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006295 err = bind_rdev_to_array(rdev, mddev);
6296 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006297 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298
6299 /*
6300 * The rest should better be atomic, we can have disk failures
6301 * noticed in interrupt contexts ...
6302 */
6303
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 rdev->raid_disk = -1;
6305
NeilBrown060b0682016-11-04 16:46:03 +11006306 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6307 if (!mddev->thread)
6308 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 /*
6310 * Kick recovery, maybe this spare has to be added to the
6311 * array immediately.
6312 */
6313 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6314 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006315 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316 return 0;
6317
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318abort_export:
6319 export_rdev(rdev);
6320 return err;
6321}
6322
NeilBrownfd01b882011-10-11 16:47:53 +11006323static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006324{
NeilBrown035328c2014-04-09 12:25:40 +10006325 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006326
NeilBrown36fa3062005-09-09 16:23:45 -07006327 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006328 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006329 return -EBUSY;
6330 if (mddev->recovery || mddev->sync_thread)
6331 return -EBUSY;
6332 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006333 }
6334
NeilBrown36fa3062005-09-09 16:23:45 -07006335 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006336 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006337 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006338
NeilBrown1e594bb2014-12-15 12:57:00 +11006339 if (mddev->bitmap || mddev->bitmap_info.file)
6340 return -EEXIST; /* cannot add when bitmap is present */
6341 f = fget(fd);
6342
6343 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006344 pr_warn("%s: error: failed to get bitmap file\n",
6345 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006346 return -EBADF;
6347 }
6348
NeilBrown1e594bb2014-12-15 12:57:00 +11006349 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006350 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006351 pr_warn("%s: error: bitmap file must be a regular file\n",
6352 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006353 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006354 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006355 pr_warn("%s: error: bitmap file must open for write\n",
6356 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006357 err = -EBADF;
6358 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006359 pr_warn("%s: error: bitmap file is already in use\n",
6360 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006361 err = -EBUSY;
6362 }
6363 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006364 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006365 return err;
6366 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006367 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006368 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006369 } else if (mddev->bitmap == NULL)
6370 return -ENOENT; /* cannot remove what isn't there */
6371 err = 0;
6372 if (mddev->pers) {
6373 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006374 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006375 struct bitmap *bitmap;
6376
6377 bitmap = bitmap_create(mddev, -1);
6378 if (!IS_ERR(bitmap)) {
6379 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006380 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006381 } else
6382 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006383 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006384 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006385 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006386 fd = -1; /* make sure to put the file */
6387 }
NeilBrown36fa3062005-09-09 16:23:45 -07006388 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006389 }
6390 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006391 struct file *f = mddev->bitmap_info.file;
6392 if (f) {
6393 spin_lock(&mddev->lock);
6394 mddev->bitmap_info.file = NULL;
6395 spin_unlock(&mddev->lock);
6396 fput(f);
6397 }
NeilBrown36fa3062005-09-09 16:23:45 -07006398 }
6399
NeilBrown32a76272005-06-21 17:17:14 -07006400 return err;
6401}
6402
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403/*
6404 * set_array_info is used two different ways
6405 * The original usage is when creating a new array.
6406 * In this usage, raid_disks is > 0 and it together with
6407 * level, size, not_persistent,layout,chunksize determine the
6408 * shape of the array.
6409 * This will always create an array with a type-0.90.0 superblock.
6410 * The newer usage is when assembling an array.
6411 * In this case raid_disks will be 0, and the major_version field is
6412 * use to determine which style super-blocks are to be found on the devices.
6413 * The minor and patch _version numbers are also kept incase the
6414 * super_block handler wishes to interpret them.
6415 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006416static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417{
6418
6419 if (info->raid_disks == 0) {
6420 /* just setting version number for superblock loading */
6421 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006422 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 super_types[info->major_version].name == NULL) {
6424 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11006425 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 info->major_version);
6427 return -EINVAL;
6428 }
6429 mddev->major_version = info->major_version;
6430 mddev->minor_version = info->minor_version;
6431 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006432 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006433 /* ensure mddev_put doesn't delete this now that there
6434 * is some minimal configuration.
6435 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006436 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 return 0;
6438 }
6439 mddev->major_version = MD_MAJOR_VERSION;
6440 mddev->minor_version = MD_MINOR_VERSION;
6441 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006442 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443
6444 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006445 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006446 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 mddev->raid_disks = info->raid_disks;
6448 /* don't set md_minor, it is determined by which /dev/md* was
6449 * openned
6450 */
6451 if (info->state & (1<<MD_SB_CLEAN))
6452 mddev->recovery_cp = MaxSector;
6453 else
6454 mddev->recovery_cp = 0;
6455 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006456 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457
6458 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006459 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460
6461 mddev->max_disks = MD_SB_DISKS;
6462
NeilBrowne6910632008-02-06 01:39:51 -08006463 if (mddev->persistent)
6464 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006465 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466
NeilBrownc3d97142009-12-14 12:49:52 +11006467 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006468 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006469 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006470
NeilBrownf6705572006-03-27 01:18:11 -08006471 mddev->reshape_position = MaxSector;
6472
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 /*
6474 * Generate a 128 bit UUID
6475 */
6476 get_random_bytes(mddev->uuid, 16);
6477
NeilBrownf6705572006-03-27 01:18:11 -08006478 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006479 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006480 mddev->new_layout = mddev->layout;
6481 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006482 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006483
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 return 0;
6485}
6486
NeilBrownfd01b882011-10-11 16:47:53 +11006487void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006488{
Dan Williamsb522adc2009-03-31 15:00:31 +11006489 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6490
6491 if (mddev->external_size)
6492 return;
6493
Dan Williams1f403622009-03-31 14:59:03 +11006494 mddev->array_sectors = array_sectors;
6495}
6496EXPORT_SYMBOL(md_set_array_sectors);
6497
NeilBrownfd01b882011-10-11 16:47:53 +11006498static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006499{
NeilBrown3cb03002011-10-11 16:45:26 +11006500 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006501 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006502 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006503
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006504 /* cluster raid doesn't support update size */
6505 if (mddev_is_clustered(mddev))
6506 return -EINVAL;
6507
NeilBrowna35b0d62006-01-06 00:20:49 -08006508 if (mddev->pers->resize == NULL)
6509 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006510 /* The "num_sectors" is the number of sectors of each device that
6511 * is used. This can only make sense for arrays with redundancy.
6512 * linear and raid0 always use whatever space is available. We can only
6513 * consider changing this number if no resync or reconstruction is
6514 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006515 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006516 * of each device. If num_sectors is zero, we find the largest size
6517 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006518 */
NeilBrownf851b602014-12-11 10:02:10 +11006519 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6520 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006521 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006522 if (mddev->ro)
6523 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006524
NeilBrowndafb20f2012-03-19 12:46:39 +11006525 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006526 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006527
Andre Nolld71f9f82008-07-11 22:02:22 +10006528 if (fit && (num_sectors == 0 || num_sectors > avail))
6529 num_sectors = avail;
6530 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006531 return -ENOSPC;
6532 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006533 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006534 if (!rv)
6535 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006536 return rv;
6537}
6538
NeilBrownfd01b882011-10-11 16:47:53 +11006539static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006540{
6541 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006542 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006543 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006544 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006545 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006546 if (mddev->ro)
6547 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006548 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006549 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006550 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006551 if (mddev->sync_thread ||
6552 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6553 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006554 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006555
6556 rdev_for_each(rdev, mddev) {
6557 if (mddev->raid_disks < raid_disks &&
6558 rdev->data_offset < rdev->new_data_offset)
6559 return -EINVAL;
6560 if (mddev->raid_disks > raid_disks &&
6561 rdev->data_offset > rdev->new_data_offset)
6562 return -EINVAL;
6563 }
6564
NeilBrown63c70c42006-03-27 01:18:13 -08006565 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006566 if (mddev->delta_disks < 0)
6567 mddev->reshape_backwards = 1;
6568 else if (mddev->delta_disks > 0)
6569 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006570
6571 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006572 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006573 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006574 mddev->reshape_backwards = 0;
6575 }
NeilBrownda943b992006-01-06 00:20:54 -08006576 return rv;
6577}
6578
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579/*
6580 * update_array_info is used to change the configuration of an
6581 * on-line array.
6582 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6583 * fields in the info are checked against the array.
6584 * Any differences that cannot be handled will cause an error.
6585 * Normally, only one change can be managed at a time.
6586 */
NeilBrownfd01b882011-10-11 16:47:53 +11006587static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588{
6589 int rv = 0;
6590 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006591 int state = 0;
6592
6593 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006594 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006595 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596
6597 if (mddev->major_version != info->major_version ||
6598 mddev->minor_version != info->minor_version ||
6599/* mddev->patch_version != info->patch_version || */
6600 mddev->ctime != info->ctime ||
6601 mddev->level != info->level ||
6602/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006603 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006604 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006605 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6606 ((state^info->state) & 0xfffffe00)
6607 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 return -EINVAL;
6609 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006610 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6611 cnt++;
6612 if (mddev->raid_disks != info->raid_disks)
6613 cnt++;
6614 if (mddev->layout != info->layout)
6615 cnt++;
6616 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6617 cnt++;
6618 if (cnt == 0)
6619 return 0;
6620 if (cnt > 1)
6621 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622
6623 if (mddev->layout != info->layout) {
6624 /* Change layout
6625 * we don't need to do anything at the md level, the
6626 * personality will take care of it all.
6627 */
NeilBrown50ac1682009-06-18 08:47:55 +10006628 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006630 else {
6631 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006632 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006633 if (rv)
6634 mddev->new_layout = mddev->layout;
6635 return rv;
6636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006638 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006639 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640
NeilBrownda943b992006-01-06 00:20:54 -08006641 if (mddev->raid_disks != info->raid_disks)
6642 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643
NeilBrown36fa3062005-09-09 16:23:45 -07006644 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006645 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6646 rv = -EINVAL;
6647 goto err;
6648 }
6649 if (mddev->recovery || mddev->sync_thread) {
6650 rv = -EBUSY;
6651 goto err;
6652 }
NeilBrown36fa3062005-09-09 16:23:45 -07006653 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006654 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006655 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006656 if (mddev->bitmap) {
6657 rv = -EEXIST;
6658 goto err;
6659 }
6660 if (mddev->bitmap_info.default_offset == 0) {
6661 rv = -EINVAL;
6662 goto err;
6663 }
NeilBrownc3d97142009-12-14 12:49:52 +11006664 mddev->bitmap_info.offset =
6665 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006666 mddev->bitmap_info.space =
6667 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006668 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006669 bitmap = bitmap_create(mddev, -1);
6670 if (!IS_ERR(bitmap)) {
6671 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006672 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006673 } else
6674 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006675 if (rv)
6676 bitmap_destroy(mddev);
6677 mddev->pers->quiesce(mddev, 0);
6678 } else {
6679 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006680 if (!mddev->bitmap) {
6681 rv = -ENOENT;
6682 goto err;
6683 }
6684 if (mddev->bitmap->storage.file) {
6685 rv = -EINVAL;
6686 goto err;
6687 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006688 if (mddev->bitmap_info.nodes) {
6689 /* hold PW on all the bitmap lock */
6690 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006691 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 +11006692 rv = -EPERM;
6693 md_cluster_ops->unlock_all_bitmaps(mddev);
6694 goto err;
6695 }
6696
6697 mddev->bitmap_info.nodes = 0;
6698 md_cluster_ops->leave(mddev);
6699 }
NeilBrown36fa3062005-09-09 16:23:45 -07006700 mddev->pers->quiesce(mddev, 1);
6701 bitmap_destroy(mddev);
6702 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006703 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006704 }
6705 }
NeilBrown850b2b42006-10-03 01:15:46 -07006706 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006707 return rv;
6708err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 return rv;
6710}
6711
NeilBrownfd01b882011-10-11 16:47:53 +11006712static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713{
NeilBrown3cb03002011-10-11 16:45:26 +11006714 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006715 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716
6717 if (mddev->pers == NULL)
6718 return -ENODEV;
6719
NeilBrown1ca69c42012-10-11 13:37:33 +11006720 rcu_read_lock();
6721 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006723 err = -ENODEV;
6724 else {
6725 md_error(mddev, rdev);
6726 if (!test_bit(Faulty, &rdev->flags))
6727 err = -EBUSY;
6728 }
6729 rcu_read_unlock();
6730 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731}
6732
Andre Noll2f9618c2008-04-25 18:57:58 +02006733/*
6734 * We have a problem here : there is no easy way to give a CHS
6735 * virtual geometry. We currently pretend that we have a 2 heads
6736 * 4 sectors (with a BIG number of cylinders...). This drives
6737 * dosfs just mad... ;-)
6738 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006739static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6740{
NeilBrownfd01b882011-10-11 16:47:53 +11006741 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006742
6743 geo->heads = 2;
6744 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006745 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006746 return 0;
6747}
6748
Nicolas Schichancb335f82014-01-15 16:58:52 +01006749static inline bool md_ioctl_valid(unsigned int cmd)
6750{
6751 switch (cmd) {
6752 case ADD_NEW_DISK:
6753 case BLKROSET:
6754 case GET_ARRAY_INFO:
6755 case GET_BITMAP_FILE:
6756 case GET_DISK_INFO:
6757 case HOT_ADD_DISK:
6758 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006759 case RAID_AUTORUN:
6760 case RAID_VERSION:
6761 case RESTART_ARRAY_RW:
6762 case RUN_ARRAY:
6763 case SET_ARRAY_INFO:
6764 case SET_BITMAP_FILE:
6765 case SET_DISK_FAULTY:
6766 case STOP_ARRAY:
6767 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006768 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006769 return true;
6770 default:
6771 return false;
6772 }
6773}
6774
Al Viroa39907f2008-03-02 10:31:15 -05006775static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 unsigned int cmd, unsigned long arg)
6777{
6778 int err = 0;
6779 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006780 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006781 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782
Nicolas Schichancb335f82014-01-15 16:58:52 +01006783 if (!md_ioctl_valid(cmd))
6784 return -ENOTTY;
6785
NeilBrown506c9e42011-12-23 10:17:26 +11006786 switch (cmd) {
6787 case RAID_VERSION:
6788 case GET_ARRAY_INFO:
6789 case GET_DISK_INFO:
6790 break;
6791 default:
6792 if (!capable(CAP_SYS_ADMIN))
6793 return -EACCES;
6794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795
6796 /*
6797 * Commands dealing with the RAID driver but not any
6798 * particular array:
6799 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006800 switch (cmd) {
6801 case RAID_VERSION:
6802 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006803 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006806 case RAID_AUTORUN:
6807 err = 0;
6808 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006809 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006811 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 }
6813
6814 /*
6815 * Commands creating/starting a new array:
6816 */
6817
Al Viroa39907f2008-03-02 10:31:15 -05006818 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819
6820 if (!mddev) {
6821 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006822 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 }
6824
NeilBrown1ca69c42012-10-11 13:37:33 +11006825 /* Some actions do not requires the mutex */
6826 switch (cmd) {
6827 case GET_ARRAY_INFO:
6828 if (!mddev->raid_disks && !mddev->external)
6829 err = -ENODEV;
6830 else
6831 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006832 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006833
6834 case GET_DISK_INFO:
6835 if (!mddev->raid_disks && !mddev->external)
6836 err = -ENODEV;
6837 else
6838 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006839 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006840
6841 case SET_DISK_FAULTY:
6842 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006843 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006844
6845 case GET_BITMAP_FILE:
6846 err = get_bitmap_file(mddev, argp);
6847 goto out;
6848
NeilBrown1ca69c42012-10-11 13:37:33 +11006849 }
6850
NeilBrowna7a3f082012-12-11 13:35:54 +11006851 if (cmd == ADD_NEW_DISK)
6852 /* need to ensure md_delayed_delete() has completed */
6853 flush_workqueue(md_misc_wq);
6854
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006855 if (cmd == HOT_REMOVE_DISK)
6856 /* need to ensure recovery thread has run */
6857 wait_event_interruptible_timeout(mddev->sb_wait,
6858 !test_bit(MD_RECOVERY_NEEDED,
6859 &mddev->flags),
6860 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006861 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6862 /* Need to flush page cache, and ensure no-one else opens
6863 * and writes
6864 */
6865 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006866 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006867 mutex_unlock(&mddev->open_mutex);
6868 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006869 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006870 }
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006871 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown260fa032013-08-27 16:44:13 +10006872 mutex_unlock(&mddev->open_mutex);
6873 sync_blockdev(bdev);
6874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 err = mddev_lock(mddev);
6876 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006877 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
6878 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006879 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880 }
6881
NeilBrownc02c0ae2012-12-11 13:39:21 +11006882 if (cmd == SET_ARRAY_INFO) {
6883 mdu_array_info_t info;
6884 if (!arg)
6885 memset(&info, 0, sizeof(info));
6886 else if (copy_from_user(&info, argp, sizeof(info))) {
6887 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006888 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006889 }
6890 if (mddev->pers) {
6891 err = update_array_info(mddev, &info);
6892 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006893 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006894 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006896 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006897 }
6898 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006899 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11006900 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006901 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006902 }
6903 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11006904 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11006905 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006906 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006907 }
6908 err = set_array_info(mddev, &info);
6909 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006910 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006911 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006912 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006913 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 }
6915
6916 /*
6917 * Commands querying/configuring an existing array:
6918 */
NeilBrown32a76272005-06-21 17:17:14 -07006919 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006920 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006921 if ((!mddev->raid_disks && !mddev->external)
6922 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6923 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6924 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006926 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 }
6928
6929 /*
6930 * Commands even a read-only array can execute:
6931 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006932 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006933 case RESTART_ARRAY_RW:
6934 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006935 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006936
6937 case STOP_ARRAY:
6938 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006939 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006940
6941 case STOP_ARRAY_RO:
6942 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006943 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006944
NeilBrown3ea8929d2013-04-24 11:42:41 +10006945 case HOT_REMOVE_DISK:
6946 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006947 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006948
NeilBrown7ceb17e2013-04-24 11:42:42 +10006949 case ADD_NEW_DISK:
6950 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08006951 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10006952 * So require mddev->pers and MD_DISK_SYNC.
6953 */
6954 if (mddev->pers) {
6955 mdu_disk_info_t info;
6956 if (copy_from_user(&info, argp, sizeof(info)))
6957 err = -EFAULT;
6958 else if (!(info.state & (1<<MD_DISK_SYNC)))
6959 /* Need to clear read-only for this */
6960 break;
6961 else
6962 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006963 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006964 }
6965 break;
6966
NeilBrownc02c0ae2012-12-11 13:39:21 +11006967 case BLKROSET:
6968 if (get_user(ro, (int __user *)(arg))) {
6969 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006970 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006971 }
6972 err = -EINVAL;
6973
6974 /* if the bdev is going readonly the value of mddev->ro
6975 * does not matter, no writes are coming
6976 */
6977 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006978 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006979
NeilBrownc02c0ae2012-12-11 13:39:21 +11006980 /* are we are already prepared for writes? */
6981 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006982 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006983
6984 /* transitioning to readauto need only happen for
6985 * arrays that call md_write_start
6986 */
6987 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006989 if (err == 0) {
6990 mddev->ro = 2;
6991 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006992 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006993 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006994 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 }
6996
6997 /*
6998 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006999 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 */
NeilBrown326eb172014-09-30 15:36:28 +10007001 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007002 if (mddev->ro == 2) {
7003 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007004 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007005 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007006 /* mddev_unlock will wake thread */
7007 /* If a device failed while we were read-only, we
7008 * need to make sure the metadata is updated now.
7009 */
7010 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
7011 mddev_unlock(mddev);
7012 wait_event(mddev->sb_wait,
7013 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
7014 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007015 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007016 }
NeilBrownf91de922005-11-08 21:39:36 -08007017 } else {
7018 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007019 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021 }
7022
NeilBrownc02c0ae2012-12-11 13:39:21 +11007023 switch (cmd) {
7024 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007026 mdu_disk_info_t info;
7027 if (copy_from_user(&info, argp, sizeof(info)))
7028 err = -EFAULT;
7029 else
7030 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007031 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007034 case CLUSTERED_DISK_NACK:
7035 if (mddev_is_clustered(mddev))
7036 md_cluster_ops->new_disk_ack(mddev, false);
7037 else
7038 err = -EINVAL;
7039 goto unlock;
7040
NeilBrownc02c0ae2012-12-11 13:39:21 +11007041 case HOT_ADD_DISK:
7042 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007043 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044
NeilBrownc02c0ae2012-12-11 13:39:21 +11007045 case RUN_ARRAY:
7046 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007047 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048
NeilBrownc02c0ae2012-12-11 13:39:21 +11007049 case SET_BITMAP_FILE:
7050 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007051 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007052
NeilBrownc02c0ae2012-12-11 13:39:21 +11007053 default:
7054 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007055 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 }
7057
NeilBrown3adc28d2014-09-30 15:46:41 +10007058unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007059 if (mddev->hold_active == UNTIL_IOCTL &&
7060 err != -EINVAL)
7061 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007063out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 return err;
7065}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007066#ifdef CONFIG_COMPAT
7067static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7068 unsigned int cmd, unsigned long arg)
7069{
7070 switch (cmd) {
7071 case HOT_REMOVE_DISK:
7072 case HOT_ADD_DISK:
7073 case SET_DISK_FAULTY:
7074 case SET_BITMAP_FILE:
7075 /* These take in integer arg, do not convert */
7076 break;
7077 default:
7078 arg = (unsigned long)compat_ptr(arg);
7079 break;
7080 }
7081
7082 return md_ioctl(bdev, mode, cmd, arg);
7083}
7084#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085
Al Viroa39907f2008-03-02 10:31:15 -05007086static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087{
7088 /*
7089 * Succeed if we can lock the mddev, which confirms that
7090 * it isn't being stopped right now.
7091 */
NeilBrownfd01b882011-10-11 16:47:53 +11007092 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093 int err;
7094
Yuanhan Liu0c098222012-05-22 13:55:32 +10007095 if (!mddev)
7096 return -ENODEV;
7097
NeilBrownd3374822009-01-09 08:31:10 +11007098 if (mddev->gendisk != bdev->bd_disk) {
7099 /* we are racing with mddev_put which is discarding this
7100 * bd_disk.
7101 */
7102 mddev_put(mddev);
7103 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007104 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007105 /* Then retry the open from the top */
7106 return -ERESTARTSYS;
7107 }
7108 BUG_ON(mddev != bdev->bd_disk->private_data);
7109
NeilBrownc8c00a62009-08-10 12:50:52 +10007110 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111 goto out;
7112
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007113 if (test_bit(MD_CLOSING, &mddev->flags)) {
7114 mutex_unlock(&mddev->open_mutex);
7115 return -ENODEV;
7116 }
7117
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007119 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007120 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007122 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123 out:
7124 return err;
7125}
7126
Al Virodb2a1442013-05-05 21:52:57 -04007127static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007129 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007131 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007132 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007135
7136static int md_media_changed(struct gendisk *disk)
7137{
NeilBrownfd01b882011-10-11 16:47:53 +11007138 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007139
7140 return mddev->changed;
7141}
7142
7143static int md_revalidate(struct gendisk *disk)
7144{
NeilBrownfd01b882011-10-11 16:47:53 +11007145 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007146
7147 mddev->changed = 0;
7148 return 0;
7149}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007150static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151{
7152 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007153 .open = md_open,
7154 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007155 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007156#ifdef CONFIG_COMPAT
7157 .compat_ioctl = md_compat_ioctl,
7158#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007159 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007160 .media_changed = md_media_changed,
7161 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162};
7163
NeilBrownf72ffdd2014-09-30 14:23:59 +10007164static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165{
NeilBrown2b8bf342011-10-11 16:48:23 +11007166 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 /*
7169 * md_thread is a 'system-thread', it's priority should be very
7170 * high. We avoid resource deadlocks individually in each
7171 * raid personality. (RAID5 does preallocation) We also use RR and
7172 * the very same RT priority as kswapd, thus we will never get
7173 * into a priority inversion deadlock.
7174 *
7175 * we definitely have to have equal or higher priority than
7176 * bdflush, otherwise bdflush will deadlock if there are too
7177 * many dirty RAID5 blocks.
7178 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179
NeilBrown6985c432005-10-19 21:23:47 -07007180 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007181 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182
NeilBrown93588e22005-11-15 00:09:12 -08007183 /* We need to wait INTERRUPTIBLE so that
7184 * we don't add to the load-average.
7185 * That means we need to be sure no signals are
7186 * pending
7187 */
7188 if (signal_pending(current))
7189 flush_signals(current);
7190
7191 wait_event_interruptible_timeout
7192 (thread->wqueue,
7193 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007194 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007195 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196
NeilBrown6c987912011-01-14 09:13:53 +11007197 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007198 if (kthread_should_park())
7199 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007200 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007201 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007203
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204 return 0;
7205}
7206
NeilBrown2b8bf342011-10-11 16:48:23 +11007207void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208{
7209 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007210 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211 set_bit(THREAD_WAKEUP, &thread->flags);
7212 wake_up(&thread->wqueue);
7213 }
7214}
NeilBrown6c144d32014-09-30 16:15:38 +10007215EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216
Shaohua Li4ed87312012-10-11 13:34:00 +11007217struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7218 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219{
NeilBrown2b8bf342011-10-11 16:48:23 +11007220 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221
NeilBrown2b8bf342011-10-11 16:48:23 +11007222 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223 if (!thread)
7224 return NULL;
7225
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 init_waitqueue_head(&thread->wqueue);
7227
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 thread->run = run;
7229 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007230 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007231 thread->tsk = kthread_run(md_thread, thread,
7232 "%s_%s",
7233 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007234 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007235 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 kfree(thread);
7237 return NULL;
7238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239 return thread;
7240}
NeilBrown6c144d32014-09-30 16:15:38 +10007241EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242
NeilBrown2b8bf342011-10-11 16:48:23 +11007243void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244{
NeilBrown2b8bf342011-10-11 16:48:23 +11007245 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007246 if (!thread)
7247 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007248 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007249 /* Locking ensures that mddev_unlock does not wake_up a
7250 * non-existent thread
7251 */
7252 spin_lock(&pers_lock);
7253 *threadp = NULL;
7254 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007255
7256 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257 kfree(thread);
7258}
NeilBrown6c144d32014-09-30 16:15:38 +10007259EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260
NeilBrownfd01b882011-10-11 16:47:53 +11007261void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262{
NeilBrownb2d444d2005-11-08 21:39:31 -08007263 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007265
NeilBrownde393cd2011-07-28 11:31:48 +10007266 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267 return;
7268 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007269 if (mddev->degraded)
7270 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007271 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7273 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7274 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007275 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007276 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007277 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278}
NeilBrown6c144d32014-09-30 16:15:38 +10007279EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280
7281/* seq_file implementation /proc/mdstat */
7282
7283static void status_unused(struct seq_file *seq)
7284{
7285 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007286 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287
7288 seq_printf(seq, "unused devices: ");
7289
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007290 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291 char b[BDEVNAME_SIZE];
7292 i++;
7293 seq_printf(seq, "%s ",
7294 bdevname(rdev->bdev,b));
7295 }
7296 if (!i)
7297 seq_printf(seq, "<none>");
7298
7299 seq_printf(seq, "\n");
7300}
7301
NeilBrownf7851be2015-07-02 17:12:58 +10007302static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303{
NeilBrowndd71cf62009-05-07 12:49:35 +10007304 sector_t max_sectors, resync, res;
7305 unsigned long dt, db;
7306 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007307 int scale;
7308 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309
NeilBrownc804cde2012-05-21 09:28:33 +10007310 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7311 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007312 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007314 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315
NeilBrownf7851be2015-07-02 17:12:58 +10007316 resync = mddev->curr_resync;
7317 if (resync <= 3) {
7318 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7319 /* Still cleaning up */
7320 resync = max_sectors;
7321 } else
7322 resync -= atomic_read(&mddev->recovery_active);
7323
7324 if (resync == 0) {
7325 if (mddev->recovery_cp < MaxSector) {
7326 seq_printf(seq, "\tresync=PENDING");
7327 return 1;
7328 }
7329 return 0;
7330 }
7331 if (resync < 3) {
7332 seq_printf(seq, "\tresync=DELAYED");
7333 return 1;
7334 }
7335
NeilBrown403df472014-09-30 15:52:29 +10007336 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007337 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007338 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007339 * u32, as those are the requirements for sector_div.
7340 * Thus 'scale' must be at least 10
7341 */
7342 scale = 10;
7343 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007344 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007345 scale++;
7346 }
7347 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007348 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007349
7350 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351 {
NeilBrown4588b422006-03-27 01:18:04 -08007352 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 seq_printf(seq, "[");
7354 for (i = 0; i < x; i++)
7355 seq_printf(seq, "=");
7356 seq_printf(seq, ">");
7357 for (i = 0; i < y; i++)
7358 seq_printf(seq, ".");
7359 seq_printf(seq, "] ");
7360 }
NeilBrown4588b422006-03-27 01:18:04 -08007361 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007362 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7363 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007364 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7365 "check" :
7366 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7367 "resync" : "recovery"))),
7368 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007369 (unsigned long long) resync/2,
7370 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371
7372 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 * dt: time from mark until now
7374 * db: blocks written from mark until now
7375 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007376 *
7377 * rt is a sector_t, so could be 32bit or 64bit.
7378 * So we divide before multiply in case it is 32bit and close
7379 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007380 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007381 * near the end of resync when the number of remaining sectors
7382 * is close to 'db'.
7383 * We then divide rt by 32 after multiplying by db to compensate.
7384 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 */
7386 dt = ((jiffies - mddev->resync_mark) / HZ);
7387 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007388 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7389 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390
NeilBrowndd71cf62009-05-07 12:49:35 +10007391 rt = max_sectors - resync; /* number of remaining sectors */
7392 sector_div(rt, db/32+1);
7393 rt *= dt;
7394 rt >>= 5;
7395
7396 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7397 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398
NeilBrownff4e8d92006-07-10 04:44:16 -07007399 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007400 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401}
7402
7403static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7404{
7405 struct list_head *tmp;
7406 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007407 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408
7409 if (l >= 0x10000)
7410 return NULL;
7411 if (!l--)
7412 /* header */
7413 return (void*)1;
7414
7415 spin_lock(&all_mddevs_lock);
7416 list_for_each(tmp,&all_mddevs)
7417 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007418 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419 mddev_get(mddev);
7420 spin_unlock(&all_mddevs_lock);
7421 return mddev;
7422 }
7423 spin_unlock(&all_mddevs_lock);
7424 if (!l--)
7425 return (void*)2;/* tail */
7426 return NULL;
7427}
7428
7429static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7430{
7431 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007432 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007433
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434 ++*pos;
7435 if (v == (void*)2)
7436 return NULL;
7437
7438 spin_lock(&all_mddevs_lock);
7439 if (v == (void*)1)
7440 tmp = all_mddevs.next;
7441 else
7442 tmp = mddev->all_mddevs.next;
7443 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007444 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445 else {
7446 next_mddev = (void*)2;
7447 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449 spin_unlock(&all_mddevs_lock);
7450
7451 if (v != (void*)1)
7452 mddev_put(mddev);
7453 return next_mddev;
7454
7455}
7456
7457static void md_seq_stop(struct seq_file *seq, void *v)
7458{
NeilBrownfd01b882011-10-11 16:47:53 +11007459 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460
7461 if (mddev && v != (void*)1 && v != (void*)2)
7462 mddev_put(mddev);
7463}
7464
7465static int md_seq_show(struct seq_file *seq, void *v)
7466{
NeilBrownfd01b882011-10-11 16:47:53 +11007467 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007468 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007469 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470
7471 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007472 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 seq_printf(seq, "Personalities : ");
7474 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007475 list_for_each_entry(pers, &pers_list, list)
7476 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477
7478 spin_unlock(&pers_lock);
7479 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007480 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481 return 0;
7482 }
7483 if (v == (void*)2) {
7484 status_unused(seq);
7485 return 0;
7486 }
7487
NeilBrown36d091f2014-12-15 12:56:58 +11007488 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7490 seq_printf(seq, "%s : %sactive", mdname(mddev),
7491 mddev->pers ? "" : "in");
7492 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007493 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007495 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007496 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 seq_printf(seq, " %s", mddev->pers->name);
7498 }
7499
Andre Nolldd8ac332009-03-31 14:33:13 +11007500 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007501 rcu_read_lock();
7502 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 char b[BDEVNAME_SIZE];
7504 seq_printf(seq, " %s[%d]",
7505 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007506 if (test_bit(WriteMostly, &rdev->flags))
7507 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007508 if (test_bit(Journal, &rdev->flags))
7509 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007510 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511 seq_printf(seq, "(F)");
7512 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007513 }
7514 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007515 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007516 if (test_bit(Replacement, &rdev->flags))
7517 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007518 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007520 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521
7522 if (!list_empty(&mddev->disks)) {
7523 if (mddev->pers)
7524 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007525 (unsigned long long)
7526 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527 else
7528 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007529 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007531 if (mddev->persistent) {
7532 if (mddev->major_version != 0 ||
7533 mddev->minor_version != 90) {
7534 seq_printf(seq," super %d.%d",
7535 mddev->major_version,
7536 mddev->minor_version);
7537 }
NeilBrowne6910632008-02-06 01:39:51 -08007538 } else if (mddev->external)
7539 seq_printf(seq, " super external:%s",
7540 mddev->metadata_type);
7541 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007542 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007543
7544 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007545 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007546 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007547 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007548 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007549 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007550 }
NeilBrown32a76272005-06-21 17:17:14 -07007551 } else
7552 seq_printf(seq, "\n ");
7553
NeilBrown57148962012-03-19 12:46:40 +11007554 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555
7556 seq_printf(seq, "\n");
7557 }
NeilBrown36d091f2014-12-15 12:56:58 +11007558 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007559
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 return 0;
7561}
7562
Jan Engelhardt110518b2009-05-07 12:49:37 +10007563static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564 .start = md_seq_start,
7565 .next = md_seq_next,
7566 .stop = md_seq_stop,
7567 .show = md_seq_show,
7568};
7569
7570static int md_seq_open(struct inode *inode, struct file *file)
7571{
Kay Sieversf1514632011-07-12 20:48:39 +02007572 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007573 int error;
7574
7575 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007576 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007577 return error;
7578
7579 seq = file->private_data;
7580 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 return error;
7582}
7583
NeilBrowne2f23b62014-04-09 14:33:51 +10007584static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007585static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7586{
Kay Sieversf1514632011-07-12 20:48:39 +02007587 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007588 int mask;
7589
NeilBrowne2f23b62014-04-09 14:33:51 +10007590 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007591 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007592 poll_wait(filp, &md_event_waiters, wait);
7593
7594 /* always allow read */
7595 mask = POLLIN | POLLRDNORM;
7596
Kay Sieversf1514632011-07-12 20:48:39 +02007597 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007598 mask |= POLLERR | POLLPRI;
7599 return mask;
7600}
7601
Arjan van de Venfa027c22007-02-12 00:55:33 -08007602static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007603 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604 .open = md_seq_open,
7605 .read = seq_read,
7606 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007607 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007608 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609};
7610
NeilBrown84fc4b52011-10-11 16:49:58 +11007611int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612{
NeilBrown9d487392016-11-02 14:16:49 +11007613 pr_debug("md: %s personality registered for level %d\n",
7614 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007616 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617 spin_unlock(&pers_lock);
7618 return 0;
7619}
NeilBrown6c144d32014-09-30 16:15:38 +10007620EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621
NeilBrown84fc4b52011-10-11 16:49:58 +11007622int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623{
NeilBrown9d487392016-11-02 14:16:49 +11007624 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007626 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627 spin_unlock(&pers_lock);
7628 return 0;
7629}
NeilBrown6c144d32014-09-30 16:15:38 +10007630EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631
NeilBrown6022e752015-08-13 12:32:55 +10007632int register_md_cluster_operations(struct md_cluster_operations *ops,
7633 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007634{
NeilBrown6022e752015-08-13 12:32:55 +10007635 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007636 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007637 if (md_cluster_ops != NULL)
7638 ret = -EALREADY;
7639 else {
7640 md_cluster_ops = ops;
7641 md_cluster_mod = module;
7642 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007643 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007644 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007645}
7646EXPORT_SYMBOL(register_md_cluster_operations);
7647
7648int unregister_md_cluster_operations(void)
7649{
7650 spin_lock(&pers_lock);
7651 md_cluster_ops = NULL;
7652 spin_unlock(&pers_lock);
7653 return 0;
7654}
7655EXPORT_SYMBOL(unregister_md_cluster_operations);
7656
7657int md_setup_cluster(struct mddev *mddev, int nodes)
7658{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007659 if (!md_cluster_ops)
7660 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007661 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007662 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007663 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11007664 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007665 spin_unlock(&pers_lock);
7666 return -ENOENT;
7667 }
7668 spin_unlock(&pers_lock);
7669
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007670 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007671}
7672
7673void md_cluster_stop(struct mddev *mddev)
7674{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007675 if (!md_cluster_ops)
7676 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007677 md_cluster_ops->leave(mddev);
7678 module_put(md_cluster_mod);
7679}
7680
NeilBrownfd01b882011-10-11 16:47:53 +11007681static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007683 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007685 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007686
7687 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007688 rcu_read_lock();
7689 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007691 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7692 (int)part_stat_read(&disk->part0, sectors[1]) -
7693 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007694 /* sync IO will cause sync_io to increase before the disk_stats
7695 * as sync_io is counted when a request starts, and
7696 * disk_stats is counted when it completes.
7697 * So resync activity will cause curr_events to be smaller than
7698 * when there was no such activity.
7699 * non-sync IO will cause disk_stat to increase without
7700 * increasing sync_io so curr_events will (eventually)
7701 * be larger than it was before. Once it becomes
7702 * substantially larger, the test below will cause
7703 * the array to appear non-idle, and resync will slow
7704 * down.
7705 * If there is a lot of outstanding resync activity when
7706 * we set last_event to curr_events, then all that activity
7707 * completing might cause the array to appear non-idle
7708 * and resync will be slowed down even though there might
7709 * not have been non-resync activity. This will only
7710 * happen once though. 'last_events' will soon reflect
7711 * the state where there is little or no outstanding
7712 * resync requests, and further resync activity will
7713 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007714 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007716 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 rdev->last_events = curr_events;
7718 idle = 0;
7719 }
7720 }
NeilBrown4b809912008-07-21 17:05:25 +10007721 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007722 return idle;
7723}
7724
NeilBrownfd01b882011-10-11 16:47:53 +11007725void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007726{
7727 /* another "blocks" (512byte) blocks have been synced */
7728 atomic_sub(blocks, &mddev->recovery_active);
7729 wake_up(&mddev->recovery_wait);
7730 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007731 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007732 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733 md_wakeup_thread(mddev->thread);
7734 // stop recovery, signal do_sync ....
7735 }
7736}
NeilBrown6c144d32014-09-30 16:15:38 +10007737EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738
NeilBrown06d91a52005-06-21 17:17:12 -07007739/* md_write_start(mddev, bi)
7740 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007741 * in superblock) before writing, schedule a superblock update
7742 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007743 */
NeilBrownfd01b882011-10-11 16:47:53 +11007744void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745{
Neil Brown0fd62b82008-06-28 08:31:36 +10007746 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007747 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007748 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007749
NeilBrownf91de922005-11-08 21:39:36 -08007750 BUG_ON(mddev->ro == 1);
7751 if (mddev->ro == 2) {
7752 /* need to switch to read/write */
7753 mddev->ro = 0;
7754 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7755 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007756 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007757 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007758 }
NeilBrown06d91a52005-06-21 17:17:12 -07007759 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007760 if (mddev->safemode == 1)
7761 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007762 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007763 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007764 if (mddev->in_sync) {
7765 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007766 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007767 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007768 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007769 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007770 }
NeilBrown85572d72014-12-15 12:56:56 +11007771 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007772 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007773 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007774 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007775 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007776 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007777}
NeilBrown6c144d32014-09-30 16:15:38 +10007778EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779
NeilBrownfd01b882011-10-11 16:47:53 +11007780void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781{
7782 if (atomic_dec_and_test(&mddev->writes_pending)) {
7783 if (mddev->safemode == 2)
7784 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007785 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7787 }
7788}
NeilBrown6c144d32014-09-30 16:15:38 +10007789EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790
NeilBrown2a2275d2007-01-26 00:57:11 -08007791/* md_allow_write(mddev)
7792 * Calling this ensures that the array is marked 'active' so that writes
7793 * may proceed without blocking. It is important to call this before
7794 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7795 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007796 *
Guoqing Jiangabf35082015-12-21 10:51:01 +11007797 * In the ->external case MD_CHANGE_PENDING can not be cleared until mddev->lock
Dan Williamsb5470dc2008-06-27 21:44:04 -07007798 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007799 */
NeilBrownfd01b882011-10-11 16:47:53 +11007800int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007801{
7802 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007803 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007804 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007805 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007806 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007807 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007808
NeilBrown85572d72014-12-15 12:56:56 +11007809 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007810 if (mddev->in_sync) {
7811 mddev->in_sync = 0;
7812 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007813 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007814 if (mddev->safemode_delay &&
7815 mddev->safemode == 0)
7816 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007817 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007818 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007819 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007820 } else
NeilBrown85572d72014-12-15 12:56:56 +11007821 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007822
NeilBrown070dc6d2010-08-30 17:33:34 +10007823 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007824 return -EAGAIN;
7825 else
7826 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007827}
7828EXPORT_SYMBOL_GPL(md_allow_write);
7829
Linus Torvalds1da177e2005-04-16 15:20:36 -07007830#define SYNC_MARKS 10
7831#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007832#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007833void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007834{
Shaohua Li4ed87312012-10-11 13:34:00 +11007835 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007836 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837 unsigned int currspeed = 0,
7838 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007839 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007841 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007842 sector_t mark_cnt[SYNC_MARKS];
7843 int last_mark,m;
7844 struct list_head *tmp;
7845 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007846 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007847 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007848 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007849 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007850 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851
7852 /* just incase thread restarts... */
7853 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7854 return;
NeilBrown3991b312014-05-28 13:39:23 +10007855 if (mddev->ro) {/* never try to sync a read-only array */
7856 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007857 return;
NeilBrown3991b312014-05-28 13:39:23 +10007858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007859
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007860 if (mddev_is_clustered(mddev)) {
7861 ret = md_cluster_ops->resync_start(mddev);
7862 if (ret)
7863 goto skip;
7864
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04007865 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007866 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7867 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
7868 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
7869 && ((unsigned long long)mddev->curr_resync_completed
7870 < (unsigned long long)mddev->resync_max_sectors))
7871 goto skip;
7872 }
7873
NeilBrown61df9d92006-10-03 01:15:57 -07007874 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007875 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007876 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007877 action = "check";
7878 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007879 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007880 action = "repair";
7881 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007882 desc = "resync";
7883 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7884 desc = "reshape";
7885 else
7886 desc = "recovery";
7887
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007888 mddev->last_sync_action = action ?: desc;
7889
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890 /* we overload curr_resync somewhat here.
7891 * 0 == not engaged in resync at all
7892 * 2 == checking that there is no conflict with another sync
7893 * 1 == like 2, but have yielded to allow conflicting resync to
7894 * commense
7895 * other == active in resync - this many blocks
7896 *
7897 * Before starting a resync we must have set curr_resync to
7898 * 2, and then checked that every "conflicting" array has curr_resync
7899 * less than ours. When we find one that is the same or higher
7900 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7901 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7902 * This will mean we have to start checking from the beginning again.
7903 *
7904 */
7905
7906 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02007907 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007908 mddev->curr_resync = 2;
7909
7910 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007911 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007912 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007913 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914 if (mddev2 == mddev)
7915 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007916 if (!mddev->parallel_resync
7917 && mddev2->curr_resync
7918 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 DEFINE_WAIT(wq);
7920 if (mddev < mddev2 && mddev->curr_resync == 2) {
7921 /* arbitrarily yield */
7922 mddev->curr_resync = 1;
7923 wake_up(&resync_wait);
7924 }
7925 if (mddev > mddev2 && mddev->curr_resync == 1)
7926 /* no need to wait here, we can wait the next
7927 * time 'round when curr_resync == 2
7928 */
7929 continue;
NeilBrown97441972008-09-19 11:49:54 +10007930 /* We need to wait 'interruptible' so as not to
7931 * contribute to the load average, and not to
7932 * be caught by 'softlockup'
7933 */
7934 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007935 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007936 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02007937 if (mddev2_minor != mddev2->md_minor) {
7938 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11007939 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
7940 desc, mdname(mddev),
7941 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02007942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007943 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007944 if (signal_pending(current))
7945 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007946 schedule();
7947 finish_wait(&resync_wait, &wq);
7948 goto try_again;
7949 }
7950 finish_wait(&resync_wait, &wq);
7951 }
7952 }
7953 } while (mddev->curr_resync < 2);
7954
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007955 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007956 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007958 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007959 */
7960 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007961 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007962 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007963 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7964 j = mddev->resync_min;
7965 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007966 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007967
NeilBrownccfcc3c2006-03-27 01:18:09 -08007968 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007969 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007970 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007971 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007972 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007973 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007974 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007975 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007976 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07007977 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007978 !test_bit(Faulty, &rdev->flags) &&
7979 !test_bit(In_sync, &rdev->flags) &&
7980 rdev->recovery_offset < j)
7981 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007982 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007983
7984 /* If there is a bitmap, we need to make sure all
7985 * writes that started before we added a spare
7986 * complete before we start doing a recovery.
7987 * Otherwise the write might complete and (via
7988 * bitmap_endwrite) set a bit in the bitmap after the
7989 * recovery has checked that bit and skipped that
7990 * region.
7991 */
7992 if (mddev->bitmap) {
7993 mddev->pers->quiesce(mddev, 1);
7994 mddev->pers->quiesce(mddev, 0);
7995 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997
NeilBrown9d487392016-11-02 14:16:49 +11007998 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
7999 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8000 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8001 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008002
NeilBrowneea1bf32009-03-31 14:27:02 +11008003 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008004
NeilBrown57afd892005-06-21 17:17:13 -07008005 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006 for (m = 0; m < SYNC_MARKS; m++) {
8007 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008008 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008009 }
8010 last_mark = 0;
8011 mddev->resync_mark = mark[last_mark];
8012 mddev->resync_mark_cnt = mark_cnt[last_mark];
8013
8014 /*
8015 * Tune reconstruction:
8016 */
8017 window = 32*(PAGE_SIZE/512);
NeilBrown9d487392016-11-02 14:16:49 +11008018 pr_debug("md: using %dk window, over a total of %lluk.\n",
8019 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008020
8021 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008022 last_check = 0;
8023
8024 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008025 pr_debug("md: resuming %s of %s from checkpoint.\n",
8026 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008028 } else
8029 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008030 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008031 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8032 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008033 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034
majianpeng7c2c57c2012-07-03 12:12:26 +10008035 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008036 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008037 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008038
NeilBrown57afd892005-06-21 17:17:13 -07008039 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008040
NeilBrown7a91ee12009-05-26 12:57:21 +10008041 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8042 ((mddev->curr_resync > mddev->curr_resync_completed &&
8043 (mddev->curr_resync - mddev->curr_resync_completed)
8044 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008045 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008046 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008047 >= mddev->resync_max - mddev->curr_resync_completed ||
8048 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008049 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008050 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008051 wait_event(mddev->recovery_wait,
8052 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008053 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008054 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8055 j > mddev->recovery_cp)
8056 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008057 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10008058 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008059 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008060 }
NeilBrownacb180b2009-04-14 16:28:34 +10008061
NeilBrownc91abf52013-11-19 12:02:01 +11008062 while (j >= mddev->resync_max &&
8063 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008064 /* As this condition is controlled by user-space,
8065 * we can block indefinitely, so use '_interruptible'
8066 * to avoid triggering warnings.
8067 */
8068 flush_signals(current); /* just in case */
8069 wait_event_interruptible(mddev->recovery_wait,
8070 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008071 || test_bit(MD_RECOVERY_INTR,
8072 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008073 }
NeilBrownacb180b2009-04-14 16:28:34 +10008074
NeilBrownc91abf52013-11-19 12:02:01 +11008075 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8076 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008077
NeilBrown09314792015-02-19 16:04:40 +11008078 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008079 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008080 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008081 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008082 }
NeilBrown57afd892005-06-21 17:17:13 -07008083
8084 if (!skipped) { /* actual IO requested */
8085 io_sectors += sectors;
8086 atomic_add(sectors, &mddev->recovery_active);
8087 }
8088
NeilBrowne875ece2011-07-28 11:39:24 +10008089 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8090 break;
8091
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008093 if (j > max_sectors)
8094 /* when skipping, extra large numbers can be returned. */
8095 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008096 if (j > 2)
8097 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008098 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008099 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008100 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008101 * visible in /proc/mdstat
8102 */
8103 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008104
8105 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008106 continue;
8107
NeilBrown57afd892005-06-21 17:17:13 -07008108 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008109 repeat:
8110 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8111 /* step marks */
8112 int next = (last_mark+1) % SYNC_MARKS;
8113
8114 mddev->resync_mark = mark[next];
8115 mddev->resync_mark_cnt = mark_cnt[next];
8116 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008117 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118 last_mark = next;
8119 }
8120
NeilBrownc91abf52013-11-19 12:02:01 +11008121 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8122 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008123
8124 /*
8125 * this loop exits only if either when we are slower than
8126 * the 'hard' speed limit, or the system was IO-idle for
8127 * a jiffy.
8128 * the system might be non-idle CPU-wise, but we only care
8129 * about not overloading the IO subsystem. (things like an
8130 * e2fsck being done on the RAID array should execute fast)
8131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008132 cond_resched();
8133
Xiao Niac7e50a2014-08-07 09:37:41 -04008134 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8135 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008136 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008137
NeilBrown88202a02006-01-06 00:21:36 -08008138 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008139 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008140 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008141 goto repeat;
8142 }
NeilBrownac8fa412015-02-19 16:55:00 +11008143 if (!is_mddev_idle(mddev, 0)) {
8144 /*
8145 * Give other IO more of a chance.
8146 * The faster the devices, the less we wait.
8147 */
8148 wait_event(mddev->recovery_wait,
8149 !atomic_read(&mddev->recovery_active));
8150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151 }
8152 }
NeilBrown9d487392016-11-02 14:16:49 +11008153 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8154 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8155 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008156 /*
8157 * this also signals 'finished resyncing' to md_stop
8158 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008159 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008160 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8161
NeilBrown5ed1df22015-07-24 13:27:08 +10008162 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8163 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008164 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008165 mddev->curr_resync_completed = mddev->curr_resync;
8166 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8167 }
NeilBrown09314792015-02-19 16:04:40 +11008168 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169
NeilBrowndfc70642008-05-23 13:04:39 -07008170 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008171 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008172 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8173 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8174 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008175 pr_debug("md: checkpointing %s of %s.\n",
8176 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008177 if (test_bit(MD_RECOVERY_ERROR,
8178 &mddev->recovery))
8179 mddev->recovery_cp =
8180 mddev->curr_resync_completed;
8181 else
8182 mddev->recovery_cp =
8183 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008184 }
8185 } else
8186 mddev->recovery_cp = MaxSector;
8187 } else {
8188 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8189 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008190 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008191 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008192 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008193 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008194 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008195 !test_bit(Faulty, &rdev->flags) &&
8196 !test_bit(In_sync, &rdev->flags) &&
8197 rdev->recovery_offset < mddev->curr_resync)
8198 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008199 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008202 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008203 /* set CHANGE_PENDING here since maybe another update is needed,
8204 * so other nodes are informed. It should be harmless for normal
8205 * raid */
8206 set_mask_bits(&mddev->flags, 0,
8207 BIT(MD_CHANGE_PENDING) | BIT(MD_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008208
NeilBrown23da4222014-12-15 12:57:01 +11008209 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008210 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8211 /* We completed so min/max setting can be forgotten if used. */
8212 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8213 mddev->resync_min = 0;
8214 mddev->resync_max = MaxSector;
8215 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8216 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008217 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008218 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008219 spin_unlock(&mddev->lock);
8220
Linus Torvalds1da177e2005-04-16 15:20:36 -07008221 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008222 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008223 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224}
NeilBrown29269552006-03-27 01:18:10 -08008225EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008226
NeilBrown746d3202013-04-24 11:42:41 +10008227static int remove_and_add_spares(struct mddev *mddev,
8228 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008229{
NeilBrown3cb03002011-10-11 16:45:26 +11008230 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008231 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008232 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008233 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008234
NeilBrownd787be42016-06-02 16:19:53 +10008235 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008236 if ((this == NULL || rdev == this) &&
8237 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008238 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008239 test_bit(Faulty, &rdev->flags) &&
8240 atomic_read(&rdev->nr_pending)==0) {
8241 /* Faulty non-Blocked devices with nr_pending == 0
8242 * never get nr_pending incremented,
8243 * never get Faulty cleared, and never get Blocked set.
8244 * So we can synchronize_rcu now rather than once per device
8245 */
8246 remove_some = true;
8247 set_bit(RemoveSynchronized, &rdev->flags);
8248 }
8249 }
8250
8251 if (remove_some)
8252 synchronize_rcu();
8253 rdev_for_each(rdev, mddev) {
8254 if ((this == NULL || rdev == this) &&
8255 rdev->raid_disk >= 0 &&
8256 !test_bit(Blocked, &rdev->flags) &&
8257 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008258 (!test_bit(In_sync, &rdev->flags) &&
8259 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008260 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008261 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008262 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008263 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008264 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008265 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008266 }
8267 }
NeilBrownd787be42016-06-02 16:19:53 +10008268 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8269 clear_bit(RemoveSynchronized, &rdev->flags);
8270 }
8271
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008272 if (removed && mddev->kobj.sd)
8273 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008274
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008275 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008276 goto no_add;
8277
NeilBrowndafb20f2012-03-19 12:46:39 +11008278 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008279 if (this && this != rdev)
8280 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008281 if (test_bit(Candidate, &rdev->flags))
8282 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008283 if (rdev->raid_disk >= 0 &&
8284 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008285 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008286 !test_bit(Faulty, &rdev->flags))
8287 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008288 if (rdev->raid_disk >= 0)
8289 continue;
8290 if (test_bit(Faulty, &rdev->flags))
8291 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008292 if (!test_bit(Journal, &rdev->flags)) {
8293 if (mddev->ro &&
8294 ! (rdev->saved_raid_disk >= 0 &&
8295 !test_bit(Bitmap_sync, &rdev->flags)))
8296 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008297
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008298 rdev->recovery_offset = 0;
8299 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008300 if (mddev->pers->
8301 hot_add_disk(mddev, rdev) == 0) {
8302 if (sysfs_link_rdev(mddev, rdev))
8303 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008304 if (!test_bit(Journal, &rdev->flags))
8305 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008306 md_new_event(mddev);
8307 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008308 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008309 }
NeilBrown746d3202013-04-24 11:42:41 +10008310no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008311 if (removed)
8312 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008313 return spares;
8314}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008315
NeilBrownac05f252014-09-30 08:10:42 +10008316static void md_start_sync(struct work_struct *ws)
8317{
8318 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008319
NeilBrownac05f252014-09-30 08:10:42 +10008320 mddev->sync_thread = md_register_thread(md_do_sync,
8321 mddev,
8322 "resync");
8323 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11008324 pr_warn("%s: could not start resync thread...\n",
8325 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008326 /* leave the spares where they are, it shouldn't hurt */
8327 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8328 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8329 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8330 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8331 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008332 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008333 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8334 &mddev->recovery))
8335 if (mddev->sysfs_action)
8336 sysfs_notify_dirent_safe(mddev->sysfs_action);
8337 } else
8338 md_wakeup_thread(mddev->sync_thread);
8339 sysfs_notify_dirent_safe(mddev->sysfs_action);
8340 md_new_event(mddev);
8341}
8342
Linus Torvalds1da177e2005-04-16 15:20:36 -07008343/*
8344 * This routine is regularly called by all per-raid-array threads to
8345 * deal with generic issues like resync and super-block update.
8346 * Raid personalities that don't have a thread (linear/raid0) do not
8347 * need this as they never do any recovery or update the superblock.
8348 *
8349 * It does not do any resync itself, but rather "forks" off other threads
8350 * to do that as needed.
8351 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8352 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008353 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008354 * and wakeups up this thread which will reap the thread and finish up.
8355 * This thread also removes any faulty devices (with nr_pending == 0).
8356 *
8357 * The overall approach is:
8358 * 1/ if the superblock needs updating, update it.
8359 * 2/ If a recovery thread is running, don't do anything else.
8360 * 3/ If recovery has finished, clean up, possibly marking spares active.
8361 * 4/ If there are any faulty devices, remove them.
8362 * 5/ If array is degraded, try to add spares devices
8363 * 6/ If array has spares or is not in-sync, start a resync thread.
8364 */
NeilBrownfd01b882011-10-11 16:47:53 +11008365void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008366{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008367 if (mddev->suspended)
8368 return;
8369
NeilBrown5f404022005-06-21 17:17:16 -07008370 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008371 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008372
NeilBrownfca4d842005-06-21 17:17:11 -07008373 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008374 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11008375 pr_debug("md: %s in immediate safe mode\n",
8376 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07008377 mddev->safemode = 2;
8378 }
8379 flush_signals(current);
8380 }
8381
NeilBrownc89a8ee2008-08-05 15:54:13 +10008382 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8383 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008385 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008387 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008388 test_bit(MD_RELOAD_SB, &mddev->flags) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008389 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008390 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8391 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008392 ))
8393 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008394
NeilBrowndf5b89b2006-03-27 01:18:20 -08008395 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008396 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008397
NeilBrownc89a8ee2008-08-05 15:54:13 +10008398 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008399 struct md_rdev *rdev;
8400 if (!mddev->external && mddev->in_sync)
8401 /* 'Blocked' flag not needed as failed devices
8402 * will be recorded if array switched to read/write.
8403 * Leaving it set will prevent the device
8404 * from being removed.
8405 */
8406 rdev_for_each(rdev, mddev)
8407 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008408 /* On a read-only array we can:
8409 * - remove failed devices
8410 * - add already-in_sync devices if the array itself
8411 * is in-sync.
8412 * As we only add devices that are already in-sync,
8413 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008414 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008415 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008416 /* There is no thread, but we need to call
8417 * ->spare_active and clear saved_raid_disk
8418 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008419 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008420 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008421 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008422 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Lid4929ad2015-09-18 10:20:12 -07008423 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008424 goto unlock;
8425 }
8426
Guoqing Jiang659b2542015-12-21 10:50:59 +11008427 if (mddev_is_clustered(mddev)) {
8428 struct md_rdev *rdev;
8429 /* kick the device if another node issued a
8430 * remove disk.
8431 */
8432 rdev_for_each(rdev, mddev) {
8433 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8434 rdev->raid_disk < 0)
8435 md_kick_rdev_from_array(rdev);
8436 }
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008437
8438 if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags))
8439 md_reload_sb(mddev, mddev->good_device_nr);
Guoqing Jiang659b2542015-12-21 10:50:59 +11008440 }
8441
NeilBrown31a59e32008-04-30 00:52:30 -07008442 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008443 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008444 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008445 if (mddev->safemode &&
8446 !atomic_read(&mddev->writes_pending) &&
8447 !mddev->in_sync &&
8448 mddev->recovery_cp == MaxSector) {
8449 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008450 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008451 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008452 }
8453 if (mddev->safemode == 1)
8454 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008455 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008456 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008457 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008458 }
NeilBrownfca4d842005-06-21 17:17:11 -07008459
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05008460 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07008461 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008462
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8464 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8465 /* resync/recovery still happening */
8466 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8467 goto unlock;
8468 }
8469 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008470 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471 goto unlock;
8472 }
Neil Brown72a23c22008-06-28 08:31:41 +10008473 /* Set RUNNING before clearing NEEDED to avoid
8474 * any transients in the value of "sync_action".
8475 */
NeilBrown72f36d52012-10-11 14:25:57 +11008476 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008477 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008478 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008479 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008480 /* Clear some bits that don't mean anything, but
8481 * might be left set
8482 */
NeilBrown24dd4692005-11-08 21:39:26 -08008483 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8484 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485
NeilBrowned209582012-04-24 10:23:14 +10008486 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8487 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008488 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008489 /* no recovery is running.
8490 * remove any failed drives, then
8491 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008492 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008493 * the personality to fail the re-add.
8494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008495
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008496 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008497 if (mddev->pers->check_reshape == NULL ||
8498 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008499 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008500 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008501 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008502 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008503 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008504 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8505 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008506 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008507 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008508 } else if (mddev->recovery_cp < MaxSector) {
8509 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008510 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008511 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8512 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008513 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008514
Linus Torvalds1da177e2005-04-16 15:20:36 -07008515 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008516 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008517 /* We are adding a device or devices to an array
8518 * which has the bitmap stored on all devices.
8519 * So make sure all bitmap pages get written
8520 */
8521 bitmap_write_all(mddev->bitmap);
8522 }
NeilBrownac05f252014-09-30 08:10:42 +10008523 INIT_WORK(&mddev->del_work, md_start_sync);
8524 queue_work(md_misc_wq, &mddev->del_work);
8525 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008526 }
NeilBrownac05f252014-09-30 08:10:42 +10008527 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008528 if (!mddev->sync_thread) {
8529 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008530 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008531 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8532 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008533 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008534 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008535 }
NeilBrownac05f252014-09-30 08:10:42 +10008536 unlock:
8537 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538 mddev_unlock(mddev);
8539 }
8540}
NeilBrown6c144d32014-09-30 16:15:38 +10008541EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008542
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008543void md_reap_sync_thread(struct mddev *mddev)
8544{
8545 struct md_rdev *rdev;
8546
8547 /* resync has finished, collect result */
8548 md_unregister_thread(&mddev->sync_thread);
8549 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8550 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8551 /* success...*/
8552 /* activate any spares */
8553 if (mddev->pers->spare_active(mddev)) {
8554 sysfs_notify(&mddev->kobj, NULL,
8555 "degraded");
8556 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8557 }
8558 }
8559 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8560 mddev->pers->finish_reshape)
8561 mddev->pers->finish_reshape(mddev);
8562
8563 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008564 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008565 */
NeilBrownf4667222013-12-09 12:04:56 +11008566 if (!mddev->degraded)
8567 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008568 rdev->saved_raid_disk = -1;
8569
8570 md_update_sb(mddev, 1);
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008571 /* MD_CHANGE_PENDING should be cleared by md_update_sb, so we can
8572 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8573 * clustered raid */
8574 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8575 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008576 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008577 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008578 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8579 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8580 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8581 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008582 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008583 /* flag recovery needed just to double check */
8584 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8585 sysfs_notify_dirent_safe(mddev->sysfs_action);
8586 md_new_event(mddev);
8587 if (mddev->event_work.func)
8588 queue_work(md_misc_wq, &mddev->event_work);
8589}
NeilBrown6c144d32014-09-30 16:15:38 +10008590EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008591
NeilBrownfd01b882011-10-11 16:47:53 +11008592void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008593{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008594 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008595 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008596 !test_bit(Blocked, &rdev->flags) &&
8597 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008598 msecs_to_jiffies(5000));
8599 rdev_dec_pending(rdev, mddev);
8600}
8601EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8602
NeilBrownc6563a82012-05-21 09:27:00 +10008603void md_finish_reshape(struct mddev *mddev)
8604{
8605 /* called be personality module when reshape completes. */
8606 struct md_rdev *rdev;
8607
8608 rdev_for_each(rdev, mddev) {
8609 if (rdev->data_offset > rdev->new_data_offset)
8610 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8611 else
8612 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8613 rdev->data_offset = rdev->new_data_offset;
8614 }
8615}
8616EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008617
Vishal Vermafc974ee2015-12-24 19:20:34 -07008618/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10008619
Vishal Vermafc974ee2015-12-24 19:20:34 -07008620/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11008621int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008622 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008623{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008624 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10008625 int rv;
8626 if (is_new)
8627 s += rdev->new_data_offset;
8628 else
8629 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008630 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8631 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008632 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008633 if (test_bit(ExternalBbl, &rdev->flags))
8634 sysfs_notify(&rdev->kobj, NULL,
8635 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008636 sysfs_notify_dirent_safe(rdev->sysfs_state);
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008637 set_mask_bits(&mddev->flags, 0,
8638 BIT(MD_CHANGE_CLEAN) | BIT(MD_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10008639 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07008640 return 1;
8641 } else
8642 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008643}
8644EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8645
NeilBrownc6563a82012-05-21 09:27:00 +10008646int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8647 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008648{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008649 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10008650 if (is_new)
8651 s += rdev->new_data_offset;
8652 else
8653 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008654 rv = badblocks_clear(&rdev->badblocks, s, sectors);
8655 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
8656 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
8657 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008658}
8659EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8660
Adrian Bunk75c96f82005-05-05 16:16:09 -07008661static int md_notify_reboot(struct notifier_block *this,
8662 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008663{
8664 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008665 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008666 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008667
NeilBrownc744a652012-03-19 12:46:37 +11008668 for_each_mddev(mddev, tmp) {
8669 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008670 if (mddev->pers)
8671 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008672 if (mddev->persistent)
8673 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008674 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008675 }
NeilBrownc744a652012-03-19 12:46:37 +11008676 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677 }
NeilBrownc744a652012-03-19 12:46:37 +11008678 /*
8679 * certain more exotic SCSI devices are known to be
8680 * volatile wrt too early system reboots. While the
8681 * right place to handle this issue is the given
8682 * driver, we do want to have a safe RAID driver ...
8683 */
8684 if (need_delay)
8685 mdelay(1000*1);
8686
Linus Torvalds1da177e2005-04-16 15:20:36 -07008687 return NOTIFY_DONE;
8688}
8689
Adrian Bunk75c96f82005-05-05 16:16:09 -07008690static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008691 .notifier_call = md_notify_reboot,
8692 .next = NULL,
8693 .priority = INT_MAX, /* before any real devices */
8694};
8695
8696static void md_geninit(void)
8697{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008698 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008699
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008700 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008701}
8702
Adrian Bunk75c96f82005-05-05 16:16:09 -07008703static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008704{
Tejun Heoe804ac72010-10-15 15:36:08 +02008705 int ret = -ENOMEM;
8706
Tejun Heoada609e2011-01-25 14:35:54 +01008707 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008708 if (!md_wq)
8709 goto err_wq;
8710
8711 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8712 if (!md_misc_wq)
8713 goto err_misc_wq;
8714
8715 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8716 goto err_md;
8717
8718 if ((ret = register_blkdev(0, "mdp")) < 0)
8719 goto err_mdp;
8720 mdp_major = ret;
8721
NeilBrownaf5628f2014-07-31 13:54:54 +10008722 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008723 md_probe, NULL, NULL);
8724 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008725 md_probe, NULL, NULL);
8726
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008728 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008729
8730 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008731 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008732
Tejun Heoe804ac72010-10-15 15:36:08 +02008733err_mdp:
8734 unregister_blkdev(MD_MAJOR, "md");
8735err_md:
8736 destroy_workqueue(md_misc_wq);
8737err_misc_wq:
8738 destroy_workqueue(md_wq);
8739err_wq:
8740 return ret;
8741}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008742
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008743static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008744{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008745 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8746 struct md_rdev *rdev2;
8747 int role, ret;
8748 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008749
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008750 /* Check for change of roles in the active devices */
8751 rdev_for_each(rdev2, mddev) {
8752 if (test_bit(Faulty, &rdev2->flags))
8753 continue;
8754
8755 /* Check if the roles changed */
8756 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008757
8758 if (test_bit(Candidate, &rdev2->flags)) {
8759 if (role == 0xfffe) {
8760 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
8761 md_kick_rdev_from_array(rdev2);
8762 continue;
8763 }
8764 else
8765 clear_bit(Candidate, &rdev2->flags);
8766 }
8767
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008768 if (role != rdev2->raid_disk) {
8769 /* got activated */
8770 if (rdev2->raid_disk == -1 && role != 0xffff) {
8771 rdev2->saved_raid_disk = role;
8772 ret = remove_and_add_spares(mddev, rdev2);
8773 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11008774 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04008775 /* wakeup mddev->thread here, so array could
8776 * perform resync with the new activated disk */
8777 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8778 md_wakeup_thread(mddev->thread);
8779
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008780 }
8781 /* device faulty
8782 * We just want to do the minimum to mark the disk
8783 * as faulty. The recovery is performed by the
8784 * one who initiated the error.
8785 */
8786 if ((role == 0xfffe) || (role == 0xfffd)) {
8787 md_error(mddev, rdev2);
8788 clear_bit(Blocked, &rdev2->flags);
8789 }
8790 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008791 }
8792
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11008793 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
8794 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008795
8796 /* Finally set the event to be up to date */
8797 mddev->events = le64_to_cpu(sb->events);
8798}
8799
8800static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
8801{
8802 int err;
8803 struct page *swapout = rdev->sb_page;
8804 struct mdp_superblock_1 *sb;
8805
8806 /* Store the sb page of the rdev in the swapout temporary
8807 * variable in case we err in the future
8808 */
8809 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11008810 err = alloc_disk_sb(rdev);
8811 if (err == 0) {
8812 ClearPageUptodate(rdev->sb_page);
8813 rdev->sb_loaded = 0;
8814 err = super_types[mddev->major_version].
8815 load_super(rdev, NULL, mddev->minor_version);
8816 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008817 if (err < 0) {
8818 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
8819 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11008820 if (rdev->sb_page)
8821 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008822 rdev->sb_page = swapout;
8823 rdev->sb_loaded = 1;
8824 return err;
8825 }
8826
8827 sb = page_address(rdev->sb_page);
8828 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
8829 * is not set
8830 */
8831
8832 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
8833 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
8834
8835 /* The other node finished recovery, call spare_active to set
8836 * device In_sync and mddev->degraded
8837 */
8838 if (rdev->recovery_offset == MaxSector &&
8839 !test_bit(In_sync, &rdev->flags) &&
8840 mddev->pers->spare_active(mddev))
8841 sysfs_notify(&mddev->kobj, NULL, "degraded");
8842
8843 put_page(swapout);
8844 return 0;
8845}
8846
8847void md_reload_sb(struct mddev *mddev, int nr)
8848{
8849 struct md_rdev *rdev;
8850 int err;
8851
8852 /* Find the rdev */
8853 rdev_for_each_rcu(rdev, mddev) {
8854 if (rdev->desc_nr == nr)
8855 break;
8856 }
8857
8858 if (!rdev || rdev->desc_nr != nr) {
8859 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
8860 return;
8861 }
8862
8863 err = read_rdev(mddev, rdev);
8864 if (err < 0)
8865 return;
8866
8867 check_sb_changes(mddev, rdev);
8868
8869 /* Read all rdev's to update recovery_offset */
8870 rdev_for_each_rcu(rdev, mddev)
8871 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008872}
8873EXPORT_SYMBOL(md_reload_sb);
8874
Linus Torvalds1da177e2005-04-16 15:20:36 -07008875#ifndef MODULE
8876
8877/*
8878 * Searches all registered partitions for autorun RAID arrays
8879 * at boot time.
8880 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008881
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008882static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008883static LIST_HEAD(all_detected_devices);
8884struct detected_devices_node {
8885 struct list_head list;
8886 dev_t dev;
8887};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888
8889void md_autodetect_dev(dev_t dev)
8890{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008891 struct detected_devices_node *node_detected_dev;
8892
8893 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8894 if (node_detected_dev) {
8895 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008896 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008897 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008898 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900}
8901
Linus Torvalds1da177e2005-04-16 15:20:36 -07008902static void autostart_arrays(int part)
8903{
NeilBrown3cb03002011-10-11 16:45:26 +11008904 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008905 struct detected_devices_node *node_detected_dev;
8906 dev_t dev;
8907 int i_scanned, i_passed;
8908
8909 i_scanned = 0;
8910 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008911
NeilBrown9d487392016-11-02 14:16:49 +11008912 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008913
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008914 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008915 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8916 i_scanned++;
8917 node_detected_dev = list_entry(all_detected_devices.next,
8918 struct detected_devices_node, list);
8919 list_del(&node_detected_dev->list);
8920 dev = node_detected_dev->dev;
8921 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07008922 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07008923 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07008924 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925 if (IS_ERR(rdev))
8926 continue;
8927
NeilBrown403df472014-09-30 15:52:29 +10008928 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008929 continue;
NeilBrown403df472014-09-30 15:52:29 +10008930
NeilBrownd0fae182008-03-04 14:29:31 -08008931 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008932 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008933 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008935 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008936
NeilBrown9d487392016-11-02 14:16:49 +11008937 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008938
8939 autorun_devices(part);
8940}
8941
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008942#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943
8944static __exit void md_exit(void)
8945{
NeilBrownfd01b882011-10-11 16:47:53 +11008946 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008947 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008948 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008949
NeilBrownaf5628f2014-07-31 13:54:54 +10008950 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008951 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008952
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008953 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008954 unregister_blkdev(mdp_major, "mdp");
8955 unregister_reboot_notifier(&md_notifier);
8956 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008957
8958 /* We cannot unload the modules while some process is
8959 * waiting for us in select() or poll() - wake them up
8960 */
8961 md_unloading = 1;
8962 while (waitqueue_active(&md_event_waiters)) {
8963 /* not safe to leave yet */
8964 wake_up(&md_event_waiters);
8965 msleep(delay);
8966 delay += delay;
8967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008968 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008969
NeilBrown29ac4aa2008-02-06 01:39:58 -08008970 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008971 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008972 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008973 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008974 destroy_workqueue(md_misc_wq);
8975 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008976}
8977
Dan Williams685784a2007-07-09 11:56:42 -07008978subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008979module_exit(md_exit)
8980
NeilBrownf91de922005-11-08 21:39:36 -08008981static int get_ro(char *buffer, struct kernel_param *kp)
8982{
8983 return sprintf(buffer, "%d", start_readonly);
8984}
8985static int set_ro(const char *val, struct kernel_param *kp)
8986{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03008987 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08008988}
8989
NeilBrown80ca3a42006-07-10 04:44:18 -07008990module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8991module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11008992module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008993
Linus Torvalds1da177e2005-04-16 15:20:36 -07008994MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008995MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008996MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008997MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);