blob: 8d4408baa4282341277f2686a3f60b05357aa116 [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.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050056#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110059static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
NeilBrown01f96c02011-09-21 15:30:20 +100062/* pers_list is a list of registered personalities protected
63 * by pers_lock.
64 * pers_lock does extra service to protect accesses to
65 * mddev->thread when the mutex cannot be held.
66 */
NeilBrown2604b702006-01-06 00:20:36 -080067static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static DEFINE_SPINLOCK(pers_lock);
69
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050070struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050071EXPORT_SYMBOL(md_cluster_ops);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050072struct module *md_cluster_mod;
73EXPORT_SYMBOL(md_cluster_mod);
74
Bernd Schubert90b08712008-05-23 13:04:38 -070075static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020076static struct workqueue_struct *md_wq;
77static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070078
NeilBrown746d3202013-04-24 11:42:41 +100079static int remove_and_add_spares(struct mddev *mddev,
80 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110081static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
Robert Becker1e509152009-12-14 12:49:58 +110084 * Default number of read corrections we'll attempt on an rdev
85 * before ejecting it from the array. We divide the read error
86 * count by 2 for every hour elapsed between read errors.
87 */
88#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
89/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
91 * is 1000 KB/sec, so the extra system load does not show up that much.
92 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070093 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * subsystem is idle. There is also an 'absolute maximum' reconstruction
95 * speed limit - in case reconstruction slows down your system despite
96 * idle IO detection.
97 *
98 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080099 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
101
102static int sysctl_speed_limit_min = 1000;
103static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100104static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800105{
106 return mddev->sync_speed_min ?
107 mddev->sync_speed_min : sysctl_speed_limit_min;
108}
109
NeilBrownfd01b882011-10-11 16:47:53 +1100110static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800111{
112 return mddev->sync_speed_max ?
113 mddev->sync_speed_max : sysctl_speed_limit_max;
114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116static struct ctl_table_header *raid_table_header;
117
Joe Perches82592c32013-11-14 15:16:18 +1100118static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .procname = "speed_limit_min",
121 .data = &sysctl_speed_limit_min,
122 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700123 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800124 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 },
126 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .procname = "speed_limit_max",
128 .data = &sysctl_speed_limit_max,
129 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700130 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800131 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800133 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
Joe Perches82592c32013-11-14 15:16:18 +1100136static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .procname = "raid",
139 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700140 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .child = raid_table,
142 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800143 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
Joe Perches82592c32013-11-14 15:16:18 +1100146static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .procname = "dev",
149 .maxlen = 0,
150 .mode = 0555,
151 .child = raid_dir_table,
152 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800153 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700156static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
NeilBrownf91de922005-11-08 21:39:36 -0800158static int start_readonly;
159
NeilBrowna167f662010-10-26 18:31:13 +1100160/* bio_clone_mddev
161 * like bio_clone, but with a local bio set
162 */
163
NeilBrowna167f662010-10-26 18:31:13 +1100164struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100165 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100166{
167 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100168
169 if (!mddev || !mddev->bio_set)
170 return bio_alloc(gfp_mask, nr_iovecs);
171
Kent Overstreet395c72a2012-09-06 15:34:55 -0700172 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100173 if (!b)
174 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100175 return b;
176}
177EXPORT_SYMBOL_GPL(bio_alloc_mddev);
178
179struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100180 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100181{
NeilBrowna167f662010-10-26 18:31:13 +1100182 if (!mddev || !mddev->bio_set)
183 return bio_clone(bio, gfp_mask);
184
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700185 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100186}
187EXPORT_SYMBOL_GPL(bio_clone_mddev);
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
NeilBrownd7603b72006-01-06 00:20:30 -0800190 * We have a system wide 'event count' that is incremented
191 * on any 'interesting' event, and readers of /proc/mdstat
192 * can use 'poll' or 'select' to find out when the event
193 * count increases.
194 *
195 * Events are:
196 * start array, stop array, error, add device, remove device,
197 * start build, activate spare
198 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800199static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800200static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100201void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800202{
203 atomic_inc(&md_event_count);
204 wake_up(&md_event_waiters);
205}
NeilBrown29269552006-03-27 01:18:10 -0800206EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800207
NeilBrownc331eb02006-05-30 21:27:13 -0700208/* Alternate version that can be called from interrupts
209 * when calling sysfs_notify isn't needed.
210 */
NeilBrownfd01b882011-10-11 16:47:53 +1100211static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700212{
213 atomic_inc(&md_event_count);
214 wake_up(&md_event_waiters);
215}
216
NeilBrownd7603b72006-01-06 00:20:30 -0800217/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * Enables to iterate over all existing md arrays
219 * all_mddevs_lock protects this list.
220 */
221static LIST_HEAD(all_mddevs);
222static DEFINE_SPINLOCK(all_mddevs_lock);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/*
225 * iterates through all used mddevs in the system.
226 * We take care to grab the all_mddevs_lock whenever navigating
227 * the list, and to always hold a refcount when unlocked.
228 * Any code which breaks out of this loop while own
229 * a reference to the current mddev and must mddev_put it.
230 */
NeilBrownfd01b882011-10-11 16:47:53 +1100231#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000233 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100234 _tmp = all_mddevs.next; \
235 _mddev = NULL;}); \
236 ({ if (_tmp != &all_mddevs) \
237 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100239 if (_mddev) mddev_put(_mddev); \
240 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
241 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100243 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 )
245
NeilBrown409c57f2009-03-31 14:39:39 +1100246/* Rather than calling directly into the personality make_request function,
247 * IO requests come here first so that we can check if the device is
248 * being suspended pending a reconfiguration.
249 * We hold a refcount over the call to ->make_request. By the time that
250 * call has finished, the bio has been linked into some internal structure
251 * and so is visible to ->quiesce(), so we don't need the refcount any more.
252 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200253static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
NeilBrown49077322010-03-25 16:20:56 +1100255 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100256 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500257 unsigned int sectors;
Gu Zheng74672d02015-04-03 08:44:47 +0800258 int cpu;
NeilBrown49077322010-03-25 16:20:56 +1100259
NeilBrown0ca69882011-01-14 09:14:33 +1100260 if (mddev == NULL || mddev->pers == NULL
261 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100262 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200263 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100264 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100265 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
266 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
267 return;
268 }
NeilBrown0ca69882011-01-14 09:14:33 +1100269 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100270 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200271 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100272 DEFINE_WAIT(__wait);
273 for (;;) {
274 prepare_to_wait(&mddev->sb_wait, &__wait,
275 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200276 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100277 break;
278 rcu_read_unlock();
279 schedule();
280 rcu_read_lock();
281 }
282 finish_wait(&mddev->sb_wait, &__wait);
283 }
284 atomic_inc(&mddev->active_io);
285 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100286
Chris Masone91ece52011-02-07 19:21:48 -0500287 /*
288 * save the sectors now since our bio can
289 * go away inside make_request
290 */
291 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200292 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100293
Gu Zheng74672d02015-04-03 08:44:47 +0800294 cpu = part_stat_lock();
295 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
296 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
297 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100298
NeilBrown409c57f2009-03-31 14:39:39 +1100299 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
300 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
NeilBrown9e35b992010-04-06 14:23:02 +1000303/* mddev_suspend makes sure no new requests are submitted
304 * to the device, and that any requests that have been submitted
305 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100306 * Once mddev_detach() is called and completes, the module will be
307 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000308 */
NeilBrownfd01b882011-10-11 16:47:53 +1100309void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100310{
311 BUG_ON(mddev->suspended);
312 mddev->suspended = 1;
313 synchronize_rcu();
314 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
315 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500316
317 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100318}
NeilBrown390ee602010-06-01 19:37:27 +1000319EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100320
NeilBrownfd01b882011-10-11 16:47:53 +1100321void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100322{
323 mddev->suspended = 0;
324 wake_up(&mddev->sb_wait);
325 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500326
Jonathan Brassow47525e52012-05-22 13:55:29 +1000327 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500328 md_wakeup_thread(mddev->thread);
329 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100330}
NeilBrown390ee602010-06-01 19:37:27 +1000331EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100332
NeilBrownfd01b882011-10-11 16:47:53 +1100333int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000334{
NeilBrown5c675f82014-12-15 12:56:56 +1100335 struct md_personality *pers = mddev->pers;
336 int ret = 0;
337
338 rcu_read_lock();
339 if (mddev->suspended)
340 ret = 1;
341 else if (pers && pers->congested)
342 ret = pers->congested(mddev, bits);
343 rcu_read_unlock();
344 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000345}
NeilBrown5c675f82014-12-15 12:56:56 +1100346EXPORT_SYMBOL_GPL(mddev_congested);
347static int md_congested(void *data, int bits)
348{
349 struct mddev *mddev = data;
350 return mddev_congested(mddev, bits);
351}
NeilBrown3fa841d2009-09-23 18:10:29 +1000352
NeilBrown64590f42014-12-15 12:56:57 +1100353static int md_mergeable_bvec(struct request_queue *q,
354 struct bvec_merge_data *bvm,
355 struct bio_vec *biovec)
356{
357 struct mddev *mddev = q->queuedata;
358 int ret;
359 rcu_read_lock();
360 if (mddev->suspended) {
361 /* Must always allow one vec */
362 if (bvm->bi_size == 0)
363 ret = biovec->bv_len;
364 else
365 ret = 0;
366 } else {
367 struct md_personality *pers = mddev->pers;
368 if (pers && pers->mergeable_bvec)
369 ret = pers->mergeable_bvec(mddev, bvm, biovec);
370 else
371 ret = biovec->bv_len;
372 }
373 rcu_read_unlock();
374 return ret;
375}
NeilBrowna2826aa2009-12-14 12:49:49 +1100376/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200377 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100378 */
379
Tejun Heoe9c74692010-09-03 11:56:18 +0200380static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100381{
NeilBrown3cb03002011-10-11 16:45:26 +1100382 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100383 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100384
385 rdev_dec_pending(rdev, mddev);
386
387 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200388 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200389 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100390 }
391 bio_put(bio);
392}
393
NeilBrowna7a07e62010-12-09 16:04:25 +1100394static void md_submit_flush_data(struct work_struct *ws);
395
NeilBrowna035fc32010-12-09 16:17:51 +1100396static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100397{
NeilBrownfd01b882011-10-11 16:47:53 +1100398 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100399 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100400
NeilBrowna7a07e62010-12-09 16:04:25 +1100401 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
402 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100403 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100404 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100405 if (rdev->raid_disk >= 0 &&
406 !test_bit(Faulty, &rdev->flags)) {
407 /* Take two references, one is dropped
408 * when request finishes, one after
409 * we reclaim rcu_read_lock
410 */
411 struct bio *bi;
412 atomic_inc(&rdev->nr_pending);
413 atomic_inc(&rdev->nr_pending);
414 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000415 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200416 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100417 bi->bi_private = rdev;
418 bi->bi_bdev = rdev->bdev;
419 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200420 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100421 rcu_read_lock();
422 rdev_dec_pending(rdev, mddev);
423 }
424 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100425 if (atomic_dec_and_test(&mddev->flush_pending))
426 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100427}
428
Tejun Heoe9c74692010-09-03 11:56:18 +0200429static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100430{
NeilBrownfd01b882011-10-11 16:47:53 +1100431 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200432 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100433
Kent Overstreet4f024f32013-10-11 15:44:27 -0700434 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100435 /* an empty barrier - all done */
436 bio_endio(bio, 0);
437 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200438 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200439 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100440 }
NeilBrown2b74e122010-12-09 15:59:01 +1100441
442 mddev->flush_bio = NULL;
443 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100444}
445
NeilBrownfd01b882011-10-11 16:47:53 +1100446void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100447{
NeilBrown85572d72014-12-15 12:56:56 +1100448 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100449 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200450 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100451 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200452 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100453 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100454
NeilBrowna035fc32010-12-09 16:17:51 +1100455 INIT_WORK(&mddev->flush_work, submit_flushes);
456 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100457}
Tejun Heoe9c74692010-09-03 11:56:18 +0200458EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100459
NeilBrown74018dc2012-07-31 09:08:15 +0200460void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000461{
NeilBrown9cbb1752012-07-31 09:08:14 +0200462 struct mddev *mddev = cb->data;
463 md_wakeup_thread(mddev->thread);
464 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000465}
NeilBrown9cbb1752012-07-31 09:08:14 +0200466EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000467
NeilBrownfd01b882011-10-11 16:47:53 +1100468static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 atomic_inc(&mddev->active);
471 return mddev;
472}
473
Dan Williams5fd3a172009-03-04 00:57:25 -0700474static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100475
NeilBrownfd01b882011-10-11 16:47:53 +1100476static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
NeilBrowna167f662010-10-26 18:31:13 +1100478 struct bio_set *bs = NULL;
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
481 return;
NeilBrownd3374822009-01-09 08:31:10 +1100482 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100483 mddev->ctime == 0 && !mddev->hold_active) {
484 /* Array is not configured at all, and not held active,
485 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100486 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100487 bs = mddev->bio_set;
488 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100489 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200490 /* We did a probe so need to clean up. Call
491 * queue_work inside the spinlock so that
492 * flush_workqueue() after mddev_find will
493 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100494 */
495 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200496 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100497 } else
498 kfree(mddev);
499 }
500 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100501 if (bs)
502 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
NeilBrownfd01b882011-10-11 16:47:53 +1100505void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100506{
507 mutex_init(&mddev->open_mutex);
508 mutex_init(&mddev->reconfig_mutex);
509 mutex_init(&mddev->bitmap_info.mutex);
510 INIT_LIST_HEAD(&mddev->disks);
511 INIT_LIST_HEAD(&mddev->all_mddevs);
512 init_timer(&mddev->safemode_timer);
513 atomic_set(&mddev->active, 1);
514 atomic_set(&mddev->openers, 0);
515 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100516 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100517 atomic_set(&mddev->flush_pending, 0);
518 init_waitqueue_head(&mddev->sb_wait);
519 init_waitqueue_head(&mddev->recovery_wait);
520 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000521 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500522 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100523 mddev->resync_min = 0;
524 mddev->resync_max = MaxSector;
525 mddev->level = LEVEL_NONE;
526}
NeilBrown390ee602010-06-01 19:37:27 +1000527EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100528
NeilBrownf72ffdd2014-09-30 14:23:59 +1000529static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
NeilBrownfd01b882011-10-11 16:47:53 +1100531 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
NeilBrown8f5f02c2011-02-16 13:58:51 +1100533 if (unit && MAJOR(unit) != MD_MAJOR)
534 unit &= ~((1<<MdpMinorShift)-1);
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 retry:
537 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
NeilBrownefeb53c2009-01-09 08:31:10 +1100539 if (unit) {
540 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
541 if (mddev->unit == unit) {
542 mddev_get(mddev);
543 spin_unlock(&all_mddevs_lock);
544 kfree(new);
545 return mddev;
546 }
547
548 if (new) {
549 list_add(&new->all_mddevs, &all_mddevs);
550 spin_unlock(&all_mddevs_lock);
551 new->hold_active = UNTIL_IOCTL;
552 return new;
553 }
554 } else if (new) {
555 /* find an unused unit number */
556 static int next_minor = 512;
557 int start = next_minor;
558 int is_free = 0;
559 int dev = 0;
560 while (!is_free) {
561 dev = MKDEV(MD_MAJOR, next_minor);
562 next_minor++;
563 if (next_minor > MINORMASK)
564 next_minor = 0;
565 if (next_minor == start) {
566 /* Oh dear, all in use. */
567 spin_unlock(&all_mddevs_lock);
568 kfree(new);
569 return NULL;
570 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000571
NeilBrownefeb53c2009-01-09 08:31:10 +1100572 is_free = 1;
573 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
574 if (mddev->unit == dev) {
575 is_free = 0;
576 break;
577 }
578 }
579 new->unit = dev;
580 new->md_minor = MINOR(dev);
581 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 list_add(&new->all_mddevs, &all_mddevs);
583 spin_unlock(&all_mddevs_lock);
584 return new;
585 }
586 spin_unlock(&all_mddevs_lock);
587
NeilBrown9ffae0c2006-01-06 00:20:32 -0800588 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (!new)
590 return NULL;
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 new->unit = unit;
593 if (MAJOR(unit) == MD_MAJOR)
594 new->md_minor = MINOR(unit);
595 else
596 new->md_minor = MINOR(unit) >> MdpMinorShift;
597
NeilBrownfafd7fb2010-04-01 15:55:30 +1100598 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 goto retry;
601}
602
NeilBrownb6eb1272010-04-15 10:13:47 +1000603static struct attribute_group md_redundancy_group;
604
NeilBrown5c47daf2014-12-15 12:57:01 +1100605void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
NeilBrowna64c8762010-04-14 17:15:37 +1000607 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000608 /* These cannot be removed under reconfig_mutex as
609 * an access to the files will try to take reconfig_mutex
610 * while holding the file unremovable, which leads to
611 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000612 * So hold set sysfs_active while the remove in happeing,
613 * and anything else which might set ->to_remove or my
614 * otherwise change the sysfs namespace will fail with
615 * -EBUSY if sysfs_active is still set.
616 * We set sysfs_active under reconfig_mutex and elsewhere
617 * test it under the same mutex to ensure its correct value
618 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000619 */
NeilBrowna64c8762010-04-14 17:15:37 +1000620 struct attribute_group *to_remove = mddev->to_remove;
621 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000622 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000623 mutex_unlock(&mddev->reconfig_mutex);
624
NeilBrown00bcb4a2010-06-01 19:37:23 +1000625 if (mddev->kobj.sd) {
626 if (to_remove != &md_redundancy_group)
627 sysfs_remove_group(&mddev->kobj, to_remove);
628 if (mddev->pers == NULL ||
629 mddev->pers->sync_request == NULL) {
630 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
631 if (mddev->sysfs_action)
632 sysfs_put(mddev->sysfs_action);
633 mddev->sysfs_action = NULL;
634 }
NeilBrowna64c8762010-04-14 17:15:37 +1000635 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000636 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000637 } else
638 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Chris Dunlop751e67c2011-10-19 16:48:26 +1100640 /* As we've dropped the mutex we need a spinlock to
641 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000642 */
643 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700644 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000645 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
NeilBrown5c47daf2014-12-15 12:57:01 +1100647EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500649struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100650{
651 struct md_rdev *rdev;
652
653 rdev_for_each_rcu(rdev, mddev)
654 if (rdev->desc_nr == nr)
655 return rdev;
656
657 return NULL;
658}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500659EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100660
661static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
NeilBrown3cb03002011-10-11 16:45:26 +1100663 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
NeilBrowndafb20f2012-03-19 12:46:39 +1100665 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (rdev->bdev->bd_dev == dev)
667 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return NULL;
670}
671
NeilBrown1ca69c42012-10-11 13:37:33 +1100672static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
673{
674 struct md_rdev *rdev;
675
676 rdev_for_each_rcu(rdev, mddev)
677 if (rdev->bdev->bd_dev == dev)
678 return rdev;
679
680 return NULL;
681}
682
NeilBrown84fc4b52011-10-11 16:49:58 +1100683static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800684{
NeilBrown84fc4b52011-10-11 16:49:58 +1100685 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800686 list_for_each_entry(pers, &pers_list, list) {
687 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800688 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800689 if (strcmp(pers->name, clevel)==0)
690 return pers;
691 }
NeilBrown2604b702006-01-06 00:20:36 -0800692 return NULL;
693}
694
Andre Nollb73df2d2008-07-11 22:02:23 +1000695/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100696static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100698 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000699 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
NeilBrownf72ffdd2014-09-30 14:23:59 +1000702static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 rdev->sb_page = alloc_page(GFP_KERNEL);
705 if (!rdev->sb_page) {
706 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000707 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
710 return 0;
711}
712
NeilBrown545c8792012-05-22 13:54:30 +1000713void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800716 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 rdev->sb_loaded = 0;
718 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000719 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100720 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
NeilBrown2699b672011-07-28 11:31:47 +1000722 if (rdev->bb_page) {
723 put_page(rdev->bb_page);
724 rdev->bb_page = NULL;
725 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000726 kfree(rdev->badblocks.page);
727 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
NeilBrown545c8792012-05-22 13:54:30 +1000729EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
NeilBrown6712ecf2007-09-27 12:47:43 +0200731static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700732{
NeilBrown3cb03002011-10-11 16:45:26 +1100733 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100734 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700735
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700736 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
737 printk("md: super_written gets error=%d, uptodate=%d\n",
738 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
739 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800740 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700741 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700742
NeilBrowna9701a32005-11-08 21:39:34 -0800743 if (atomic_dec_and_test(&mddev->pending_writes))
744 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700745 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700746}
747
NeilBrownfd01b882011-10-11 16:47:53 +1100748void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700749 sector_t sector, int size, struct page *page)
750{
751 /* write first size bytes of page to sector of rdev
752 * Increment mddev->pending_writes before returning
753 * and decrement it on completion, waking up sb_wait
754 * if zero is reached.
755 * If an error occurred, call md_error
756 */
NeilBrowna167f662010-10-26 18:31:13 +1100757 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700758
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100759 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700760 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700761 bio_add_page(bio, page, size, 0);
762 bio->bi_private = rdev;
763 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800764
NeilBrown7bfa19f2005-06-21 17:17:28 -0700765 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000766 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800767}
768
NeilBrownfd01b882011-10-11 16:47:53 +1100769void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800770{
Tejun Heoe9c74692010-09-03 11:56:18 +0200771 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000772 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700773}
774
NeilBrown3cb03002011-10-11 16:45:26 +1100775int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100776 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
NeilBrowna167f662010-10-26 18:31:13 +1100778 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 int ret;
780
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100781 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
782 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100783 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700784 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000785 else if (rdev->mddev->reshape_position != MaxSector &&
786 (rdev->mddev->reshape_backwards ==
787 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700788 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100789 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700790 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700792 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
795 bio_put(bio);
796 return ret;
797}
NeilBrowna8745db2006-01-06 00:20:34 -0800798EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
NeilBrownf72ffdd2014-09-30 14:23:59 +1000800static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (rdev->sb_loaded)
805 return 0;
806
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100807 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 goto fail;
809 rdev->sb_loaded = 1;
810 return 0;
811
812fail:
813 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
814 bdevname(rdev->bdev,b));
815 return -EINVAL;
816}
817
818static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
819{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000820 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000821 sb1->set_uuid1 == sb2->set_uuid1 &&
822 sb1->set_uuid2 == sb2->set_uuid2 &&
823 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
827{
828 int ret;
829 mdp_super_t *tmp1, *tmp2;
830
831 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
832 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
833
834 if (!tmp1 || !tmp2) {
835 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100836 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 goto abort;
838 }
839
840 *tmp1 = *sb1;
841 *tmp2 = *sb2;
842
843 /*
844 * nr_disks is not constant
845 */
846 tmp1->nr_disks = 0;
847 tmp2->nr_disks = 0;
848
Andre Nollce0c8e02008-07-11 22:02:20 +1000849 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700851 kfree(tmp1);
852 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return ret;
854}
855
NeilBrown4d167f02007-05-09 02:35:37 -0700856static u32 md_csum_fold(u32 csum)
857{
858 csum = (csum & 0xffff) + (csum >> 16);
859 return (csum & 0xffff) + (csum >> 16);
860}
861
NeilBrownf72ffdd2014-09-30 14:23:59 +1000862static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
NeilBrown4d167f02007-05-09 02:35:37 -0700864 u64 newcsum = 0;
865 u32 *sb32 = (u32*)sb;
866 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 unsigned int disk_csum, csum;
868
869 disk_csum = sb->sb_csum;
870 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700871
872 for (i = 0; i < MD_SB_BYTES/4 ; i++)
873 newcsum += sb32[i];
874 csum = (newcsum & 0xffffffff) + (newcsum>>32);
875
NeilBrown4d167f02007-05-09 02:35:37 -0700876#ifdef CONFIG_ALPHA
877 /* This used to use csum_partial, which was wrong for several
878 * reasons including that different results are returned on
879 * different architectures. It isn't critical that we get exactly
880 * the same return value as before (we always csum_fold before
881 * testing, and that removes any differences). However as we
882 * know that csum_partial always returned a 16bit value on
883 * alphas, do a fold to maximise conformity to previous behaviour.
884 */
885 sb->sb_csum = md_csum_fold(disk_csum);
886#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700888#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return csum;
890}
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892/*
893 * Handle superblock details.
894 * We want to be able to handle multiple superblock formats
895 * so we have a common interface to them all, and an array of
896 * different handlers.
897 * We rely on user-space to write the initial superblock, and support
898 * reading and updating of superblocks.
899 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100900 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 * loads and validates a superblock on dev.
902 * if refdev != NULL, compare superblocks on both devices
903 * Return:
904 * 0 - dev has a superblock that is compatible with refdev
905 * 1 - dev has a superblock that is compatible and newer than refdev
906 * so dev should be used as the refdev in future
907 * -EINVAL superblock incompatible or invalid
908 * -othererror e.g. -EIO
909 *
NeilBrownfd01b882011-10-11 16:47:53 +1100910 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * Verify that dev is acceptable into mddev.
912 * The first time, mddev->raid_disks will be 0, and data from
913 * dev should be merged in. Subsequent calls check that dev
914 * is new enough. Return 0 or -EINVAL
915 *
NeilBrownfd01b882011-10-11 16:47:53 +1100916 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 * Update the superblock for rdev with data in mddev
918 * This does not write to disc.
919 *
920 */
921
922struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000923 char *name;
924 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000925 int (*load_super)(struct md_rdev *rdev,
926 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000927 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000928 int (*validate_super)(struct mddev *mddev,
929 struct md_rdev *rdev);
930 void (*sync_super)(struct mddev *mddev,
931 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100932 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000933 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000934 int (*allow_new_offset)(struct md_rdev *rdev,
935 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936};
937
938/*
Andre Noll0894cc32009-06-18 08:49:23 +1000939 * Check that the given mddev has no bitmap.
940 *
941 * This function is called from the run method of all personalities that do not
942 * support bitmaps. It prints an error message and returns non-zero if mddev
943 * has a bitmap. Otherwise, it returns 0.
944 *
945 */
NeilBrownfd01b882011-10-11 16:47:53 +1100946int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000947{
NeilBrownc3d97142009-12-14 12:49:52 +1100948 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000949 return 0;
950 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
951 mdname(mddev), mddev->pers->name);
952 return 1;
953}
954EXPORT_SYMBOL(md_check_no_bitmap);
955
956/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000957 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 */
NeilBrown3cb03002011-10-11 16:45:26 +1100959static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
962 mdp_super_t *sb;
963 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /*
Andre Noll0f420352008-07-11 22:02:23 +1000966 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 * it's at the end of the disk.
968 *
969 * It also happens to be a multiple of 4Kb.
970 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100971 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
NeilBrown0002b272005-09-09 16:23:53 -0700973 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (ret) return ret;
975
976 ret = -EINVAL;
977
978 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000979 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 if (sb->md_magic != MD_SB_MAGIC) {
982 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
983 b);
984 goto abort;
985 }
986
987 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800988 sb->minor_version < 90 ||
989 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
991 sb->major_version, sb->minor_version,
992 b);
993 goto abort;
994 }
995
996 if (sb->raid_disks <= 0)
997 goto abort;
998
NeilBrown4d167f02007-05-09 02:35:37 -0700999 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1001 b);
1002 goto abort;
1003 }
1004
1005 rdev->preferred_minor = sb->md_minor;
1006 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001007 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001008 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001009 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 if (sb->level == LEVEL_MULTIPATH)
1012 rdev->desc_nr = -1;
1013 else
1014 rdev->desc_nr = sb->this_disk.number;
1015
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001016 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001018 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001020 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (!uuid_equal(refsb, sb)) {
1022 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1023 b, bdevname(refdev->bdev,b2));
1024 goto abort;
1025 }
1026 if (!sb_equal(refsb, sb)) {
1027 printk(KERN_WARNING "md: %s has same UUID"
1028 " but different superblock to %s\n",
1029 b, bdevname(refdev->bdev, b2));
1030 goto abort;
1031 }
1032 ev1 = md_event(sb);
1033 ev2 = md_event(refsb);
1034 if (ev1 > ev2)
1035 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001036 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 ret = 0;
1038 }
NeilBrown8190e752009-06-18 08:48:58 +10001039 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001040 /* Limit to 4TB as metadata cannot record more than that.
1041 * (not needed for Linear and RAID0 as metadata doesn't
1042 * record this size)
1043 */
1044 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001045 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
NeilBrown27a7b262011-09-10 17:21:28 +10001047 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001048 /* "this cannot possibly happen" ... */
1049 ret = -EINVAL;
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 abort:
1052 return ret;
1053}
1054
1055/*
1056 * validate_super for 0.90.0
1057 */
NeilBrownfd01b882011-10-11 16:47:53 +11001058static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001061 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001062 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
NeilBrown41158c72005-06-21 17:17:25 -07001064 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001065 clear_bit(Faulty, &rdev->flags);
1066 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001067 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001068 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (mddev->raid_disks == 0) {
1071 mddev->major_version = 0;
1072 mddev->minor_version = sb->minor_version;
1073 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001074 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001075 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 mddev->ctime = sb->ctime;
1077 mddev->utime = sb->utime;
1078 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001079 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 mddev->layout = sb->layout;
1081 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001082 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001083 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001084 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001085 mddev->bitmap_info.space = 0;
1086 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001087 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001088 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001089 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
NeilBrownf6705572006-03-27 01:18:11 -08001091 if (mddev->minor_version >= 91) {
1092 mddev->reshape_position = sb->reshape_position;
1093 mddev->delta_disks = sb->delta_disks;
1094 mddev->new_level = sb->new_level;
1095 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001096 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001097 if (mddev->delta_disks < 0)
1098 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001099 } else {
1100 mddev->reshape_position = MaxSector;
1101 mddev->delta_disks = 0;
1102 mddev->new_level = mddev->level;
1103 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001104 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001105 }
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (sb->state & (1<<MD_SB_CLEAN))
1108 mddev->recovery_cp = MaxSector;
1109 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001110 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 sb->events_lo == sb->cp_events_lo) {
1112 mddev->recovery_cp = sb->recovery_cp;
1113 } else
1114 mddev->recovery_cp = 0;
1115 }
1116
1117 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1118 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1119 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1120 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1121
1122 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001123
1124 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001125 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001126 mddev->bitmap_info.offset =
1127 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001128 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001129 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001130 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001131
NeilBrown41158c72005-06-21 17:17:25 -07001132 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001133 /* Insist on good event counter while assembling, except
1134 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001136 if (sb->disks[rdev->desc_nr].state & (
1137 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001138 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001139 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001140 } else if (mddev->bitmap) {
1141 /* if adding to array with a bitmap, then we can accept an
1142 * older device ... but not too old.
1143 */
NeilBrown41158c72005-06-21 17:17:25 -07001144 if (ev1 < mddev->bitmap->events_cleared)
1145 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001146 if (ev1 < mddev->events)
1147 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001148 } else {
1149 if (ev1 < mddev->events)
1150 /* just a hot-add of a new device, leave raid_disk at -1 */
1151 return 0;
1152 }
NeilBrown41158c72005-06-21 17:17:25 -07001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 desc = sb->disks + rdev->desc_nr;
1156
1157 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001158 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001159 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1160 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001161 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001163 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001164 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1165 /* active but not in sync implies recovery up to
1166 * reshape position. We don't know exactly where
1167 * that is, so set to zero for now */
1168 if (mddev->minor_version >= 91) {
1169 rdev->recovery_offset = 0;
1170 rdev->raid_disk = desc->raid_disk;
1171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001173 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1174 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001175 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001176 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return 0;
1178}
1179
1180/*
1181 * sync_super for 0.90.0
1182 */
NeilBrownfd01b882011-10-11 16:47:53 +11001183static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001186 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 /* make rdev->sb match mddev data..
1190 *
1191 * 1/ zero out disks
1192 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1193 * 3/ any empty disks < next_spare become removed
1194 *
1195 * disks[0] gets initialised to REMOVED because
1196 * we cannot be sure from other fields if it has
1197 * been initialised or not.
1198 */
1199 int i;
1200 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1201
NeilBrown61181562005-09-09 16:24:02 -07001202 rdev->sb_size = MD_SB_BYTES;
1203
Namhyung Kim65a06f062011-07-27 11:00:36 +10001204 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 memset(sb, 0, sizeof(*sb));
1207
1208 sb->md_magic = MD_SB_MAGIC;
1209 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 sb->patch_version = mddev->patch_version;
1211 sb->gvalid_words = 0; /* ignored */
1212 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1213 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1214 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1215 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1216
1217 sb->ctime = mddev->ctime;
1218 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001219 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 sb->raid_disks = mddev->raid_disks;
1221 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001222 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 sb->utime = mddev->utime;
1224 sb->state = 0;
1225 sb->events_hi = (mddev->events>>32);
1226 sb->events_lo = (u32)mddev->events;
1227
NeilBrownf6705572006-03-27 01:18:11 -08001228 if (mddev->reshape_position == MaxSector)
1229 sb->minor_version = 90;
1230 else {
1231 sb->minor_version = 91;
1232 sb->reshape_position = mddev->reshape_position;
1233 sb->new_level = mddev->new_level;
1234 sb->delta_disks = mddev->delta_disks;
1235 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001236 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001237 }
1238 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (mddev->in_sync)
1240 {
1241 sb->recovery_cp = mddev->recovery_cp;
1242 sb->cp_events_hi = (mddev->events>>32);
1243 sb->cp_events_lo = (u32)mddev->events;
1244 if (mddev->recovery_cp == MaxSector)
1245 sb->state = (1<< MD_SB_CLEAN);
1246 } else
1247 sb->recovery_cp = 0;
1248
1249 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001250 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
NeilBrownc3d97142009-12-14 12:49:52 +11001252 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001253 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001256 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001258 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001259 int is_active = test_bit(In_sync, &rdev2->flags);
1260
1261 if (rdev2->raid_disk >= 0 &&
1262 sb->minor_version >= 91)
1263 /* we have nowhere to store the recovery_offset,
1264 * but if it is not below the reshape_position,
1265 * we can piggy-back on that.
1266 */
1267 is_active = 1;
1268 if (rdev2->raid_disk < 0 ||
1269 test_bit(Faulty, &rdev2->flags))
1270 is_active = 0;
1271 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001272 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001274 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001275 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 d = &sb->disks[rdev2->desc_nr];
1277 nr_disks++;
1278 d->number = rdev2->desc_nr;
1279 d->major = MAJOR(rdev2->bdev->bd_dev);
1280 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001281 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 d->raid_disk = rdev2->raid_disk;
1283 else
1284 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001285 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001287 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001289 if (test_bit(In_sync, &rdev2->flags))
1290 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 active++;
1292 working++;
1293 } else {
1294 d->state = 0;
1295 spare++;
1296 working++;
1297 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001298 if (test_bit(WriteMostly, &rdev2->flags))
1299 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 /* now set the "removed" and "faulty" bits on any missing devices */
1302 for (i=0 ; i < mddev->raid_disks ; i++) {
1303 mdp_disk_t *d = &sb->disks[i];
1304 if (d->state == 0 && d->number == 0) {
1305 d->number = i;
1306 d->raid_disk = i;
1307 d->state = (1<<MD_DISK_REMOVED);
1308 d->state |= (1<<MD_DISK_FAULTY);
1309 failed++;
1310 }
1311 }
1312 sb->nr_disks = nr_disks;
1313 sb->active_disks = active;
1314 sb->working_disks = working;
1315 sb->failed_disks = failed;
1316 sb->spare_disks = spare;
1317
1318 sb->this_disk = sb->disks[rdev->desc_nr];
1319 sb->sb_csum = calc_sb_csum(sb);
1320}
1321
1322/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001323 * rdev_size_change for 0.90.0
1324 */
1325static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001326super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001327{
Andre Noll58c0fed2009-03-31 14:33:13 +11001328 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001329 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001330 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001331 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001332 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001333 if (!num_sectors || num_sectors > rdev->sb_start)
1334 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001335 /* Limit to 4TB as metadata cannot record more than that.
1336 * 4TB == 2^32 KB, or 2*2^32 sectors.
1337 */
NeilBrown667a5312012-08-16 16:46:12 +10001338 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001339 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001340 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001341 rdev->sb_page);
1342 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001343 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001344}
1345
NeilBrownc6563a82012-05-21 09:27:00 +10001346static int
1347super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1348{
1349 /* non-zero offset changes not possible with v0.90 */
1350 return new_offset == 0;
1351}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001352
1353/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 * version 1 superblock
1355 */
1356
NeilBrownf72ffdd2014-09-30 14:23:59 +10001357static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001359 __le32 disk_csum;
1360 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 unsigned long long newcsum;
1362 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001363 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 disk_csum = sb->sb_csum;
1366 sb->sb_csum = 0;
1367 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001368 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 newcsum += le32_to_cpu(*isuper++);
1370
1371 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001372 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1375 sb->sb_csum = disk_csum;
1376 return cpu_to_le32(csum);
1377}
1378
NeilBrown2699b672011-07-28 11:31:47 +10001379static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1380 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001381static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 struct mdp_superblock_1 *sb;
1384 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001385 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001386 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001388 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 /*
Andre Noll0f420352008-07-11 22:02:23 +10001391 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 * It is always aligned to a 4K boundary and
1393 * depeding on minor_version, it can be:
1394 * 0: At least 8K, but less than 12K, from end of device
1395 * 1: At start of device
1396 * 2: 4K from start of device.
1397 */
1398 switch(minor_version) {
1399 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001400 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001401 sb_start -= 8*2;
1402 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 break;
1404 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001405 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 break;
1407 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001408 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 break;
1410 default:
1411 return -EINVAL;
1412 }
Andre Noll0f420352008-07-11 22:02:23 +10001413 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
NeilBrown0002b272005-09-09 16:23:53 -07001415 /* superblock is rarely larger than 1K, but it can be larger,
1416 * and it is safe to read 4k, so we do that
1417 */
1418 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (ret) return ret;
1420
Namhyung Kim65a06f062011-07-27 11:00:36 +10001421 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1424 sb->major_version != cpu_to_le32(1) ||
1425 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001426 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001427 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return -EINVAL;
1429
1430 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1431 printk("md: invalid superblock checksum on %s\n",
1432 bdevname(rdev->bdev,b));
1433 return -EINVAL;
1434 }
1435 if (le64_to_cpu(sb->data_size) < 10) {
1436 printk("md: data_size too small on %s\n",
1437 bdevname(rdev->bdev,b));
1438 return -EINVAL;
1439 }
NeilBrownc6563a82012-05-21 09:27:00 +10001440 if (sb->pad0 ||
1441 sb->pad3[0] ||
1442 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1443 /* Some padding is non-zero, might be a new feature */
1444 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 rdev->preferred_minor = 0xffff;
1447 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001448 rdev->new_data_offset = rdev->data_offset;
1449 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1450 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1451 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001452 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
NeilBrown0002b272005-09-09 16:23:53 -07001454 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001455 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001456 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001457 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1458
1459 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001460 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001461 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001462 if (minor_version
1463 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1464 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001465
NeilBrown31b65a02006-07-10 04:44:14 -07001466 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1467 rdev->desc_nr = -1;
1468 else
1469 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1470
NeilBrown2699b672011-07-28 11:31:47 +10001471 if (!rdev->bb_page) {
1472 rdev->bb_page = alloc_page(GFP_KERNEL);
1473 if (!rdev->bb_page)
1474 return -ENOMEM;
1475 }
1476 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1477 rdev->badblocks.count == 0) {
1478 /* need to load the bad block list.
1479 * Currently we limit it to one page.
1480 */
1481 s32 offset;
1482 sector_t bb_sector;
1483 u64 *bbp;
1484 int i;
1485 int sectors = le16_to_cpu(sb->bblog_size);
1486 if (sectors > (PAGE_SIZE / 512))
1487 return -EINVAL;
1488 offset = le32_to_cpu(sb->bblog_offset);
1489 if (offset == 0)
1490 return -EINVAL;
1491 bb_sector = (long long)offset;
1492 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1493 rdev->bb_page, READ, true))
1494 return -EIO;
1495 bbp = (u64 *)page_address(rdev->bb_page);
1496 rdev->badblocks.shift = sb->bblog_shift;
1497 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1498 u64 bb = le64_to_cpu(*bbp);
1499 int count = bb & (0x3ff);
1500 u64 sector = bb >> 10;
1501 sector <<= sb->bblog_shift;
1502 count <<= sb->bblog_shift;
1503 if (bb + 1 == 0)
1504 break;
1505 if (md_set_badblocks(&rdev->badblocks,
1506 sector, count, 1) == 0)
1507 return -EINVAL;
1508 }
NeilBrown486adf72013-04-24 11:42:44 +10001509 } else if (sb->bblog_offset != 0)
1510 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001511
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001512 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001513 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001514 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001516 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1519 sb->level != refsb->level ||
1520 sb->layout != refsb->layout ||
1521 sb->chunksize != refsb->chunksize) {
1522 printk(KERN_WARNING "md: %s has strangely different"
1523 " superblock to %s\n",
1524 bdevname(rdev->bdev,b),
1525 bdevname(refdev->bdev,b2));
1526 return -EINVAL;
1527 }
1528 ev1 = le64_to_cpu(sb->events);
1529 ev2 = le64_to_cpu(refsb->events);
1530
1531 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001532 ret = 1;
1533 else
1534 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
NeilBrownc6563a82012-05-21 09:27:00 +10001536 if (minor_version) {
1537 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1538 sectors -= rdev->data_offset;
1539 } else
1540 sectors = rdev->sb_start;
1541 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001543 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001544 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
NeilBrownfd01b882011-10-11 16:47:53 +11001547static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001549 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001550 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
NeilBrown41158c72005-06-21 17:17:25 -07001552 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001553 clear_bit(Faulty, &rdev->flags);
1554 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001555 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001556 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (mddev->raid_disks == 0) {
1559 mddev->major_version = 1;
1560 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001561 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001562 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1564 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1565 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001566 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 mddev->layout = le32_to_cpu(sb->layout);
1568 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001569 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001570 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001571 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001572 mddev->bitmap_info.space = 0;
1573 /* Default location for bitmap is 1K after superblock
1574 * using 3K - total of 4K
1575 */
NeilBrownc3d97142009-12-14 12:49:52 +11001576 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001577 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001578 mddev->reshape_backwards = 0;
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1581 memcpy(mddev->uuid, sb->set_uuid, 16);
1582
1583 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001584
NeilBrown71c08052005-09-09 16:23:51 -07001585 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001586 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001587 mddev->bitmap_info.offset =
1588 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001589 /* Metadata doesn't record how much space is available.
1590 * For 1.0, we assume we can use up to the superblock
1591 * if before, else to 4K beyond superblock.
1592 * For others, assume no change is possible.
1593 */
1594 if (mddev->minor_version > 0)
1595 mddev->bitmap_info.space = 0;
1596 else if (mddev->bitmap_info.offset > 0)
1597 mddev->bitmap_info.space =
1598 8 - mddev->bitmap_info.offset;
1599 else
1600 mddev->bitmap_info.space =
1601 -mddev->bitmap_info.offset;
1602 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001603
NeilBrownf6705572006-03-27 01:18:11 -08001604 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1605 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1606 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1607 mddev->new_level = le32_to_cpu(sb->new_level);
1608 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001609 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001610 if (mddev->delta_disks < 0 ||
1611 (mddev->delta_disks == 0 &&
1612 (le32_to_cpu(sb->feature_map)
1613 & MD_FEATURE_RESHAPE_BACKWARDS)))
1614 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001615 } else {
1616 mddev->reshape_position = MaxSector;
1617 mddev->delta_disks = 0;
1618 mddev->new_level = mddev->level;
1619 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001620 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001621 }
1622
NeilBrown41158c72005-06-21 17:17:25 -07001623 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001624 /* Insist of good event counter while assembling, except for
1625 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001627 if (rdev->desc_nr >= 0 &&
1628 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1629 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1630 if (ev1 < mddev->events)
1631 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001632 } else if (mddev->bitmap) {
1633 /* If adding to array with a bitmap, then we can accept an
1634 * older device, but not too old.
1635 */
NeilBrown41158c72005-06-21 17:17:25 -07001636 if (ev1 < mddev->bitmap->events_cleared)
1637 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001638 if (ev1 < mddev->events)
1639 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001640 } else {
1641 if (ev1 < mddev->events)
1642 /* just a hot-add of a new device, leave raid_disk at -1 */
1643 return 0;
1644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (mddev->level != LEVEL_MULTIPATH) {
1646 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001647 if (rdev->desc_nr < 0 ||
1648 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1649 role = 0xffff;
1650 rdev->desc_nr = -1;
1651 } else
1652 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 switch(role) {
1654 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 break;
1656 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001657 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 break;
1659 default:
NeilBrownf4667222013-12-09 12:04:56 +11001660 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001661 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001662 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001663 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001664 if (!(le32_to_cpu(sb->feature_map) &
1665 MD_FEATURE_RECOVERY_BITMAP))
1666 rdev->saved_raid_disk = -1;
1667 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001668 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 rdev->raid_disk = role;
1670 break;
1671 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001672 if (sb->devflags & WriteMostly1)
1673 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001674 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1675 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001676 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001677 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return 0;
1680}
1681
NeilBrownfd01b882011-10-11 16:47:53 +11001682static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
1684 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001685 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 int max_dev, i;
1687 /* make rdev->sb match mddev and rdev data. */
1688
Namhyung Kim65a06f062011-07-27 11:00:36 +10001689 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 sb->feature_map = 0;
1692 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001693 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 memset(sb->pad3, 0, sizeof(sb->pad3));
1695
1696 sb->utime = cpu_to_le64((__u64)mddev->utime);
1697 sb->events = cpu_to_le64(mddev->events);
1698 if (mddev->in_sync)
1699 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1700 else
1701 sb->resync_offset = cpu_to_le64(0);
1702
NeilBrown1c05b4b2006-10-21 10:24:08 -07001703 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001704
NeilBrownf0ca3402006-02-02 14:28:04 -08001705 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001706 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001707 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001708 sb->level = cpu_to_le32(mddev->level);
1709 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001710
NeilBrownaeb9b2112011-08-25 14:43:08 +10001711 if (test_bit(WriteMostly, &rdev->flags))
1712 sb->devflags |= WriteMostly1;
1713 else
1714 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001715 sb->data_offset = cpu_to_le64(rdev->data_offset);
1716 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001717
NeilBrownc3d97142009-12-14 12:49:52 +11001718 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1719 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001720 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001721 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001722
1723 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001724 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001725 sb->feature_map |=
1726 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1727 sb->recovery_offset =
1728 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001729 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1730 sb->feature_map |=
1731 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001732 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001733 if (test_bit(Replacement, &rdev->flags))
1734 sb->feature_map |=
1735 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001736
NeilBrownf6705572006-03-27 01:18:11 -08001737 if (mddev->reshape_position != MaxSector) {
1738 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1739 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1740 sb->new_layout = cpu_to_le32(mddev->new_layout);
1741 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1742 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001743 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001744 if (mddev->delta_disks == 0 &&
1745 mddev->reshape_backwards)
1746 sb->feature_map
1747 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001748 if (rdev->new_data_offset != rdev->data_offset) {
1749 sb->feature_map
1750 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1751 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1752 - rdev->data_offset));
1753 }
NeilBrownf6705572006-03-27 01:18:11 -08001754 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001755
NeilBrown2699b672011-07-28 11:31:47 +10001756 if (rdev->badblocks.count == 0)
1757 /* Nothing to do for bad blocks*/ ;
1758 else if (sb->bblog_offset == 0)
1759 /* Cannot record bad blocks on this device */
1760 md_error(mddev, rdev);
1761 else {
1762 struct badblocks *bb = &rdev->badblocks;
1763 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1764 u64 *p = bb->page;
1765 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1766 if (bb->changed) {
1767 unsigned seq;
1768
1769retry:
1770 seq = read_seqbegin(&bb->lock);
1771
1772 memset(bbp, 0xff, PAGE_SIZE);
1773
1774 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001775 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001776 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1777 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001778 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001779 }
NeilBrownd0962932012-03-19 12:46:41 +11001780 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001781 if (read_seqretry(&bb->lock, seq))
1782 goto retry;
1783
1784 bb->sector = (rdev->sb_start +
1785 (int)le32_to_cpu(sb->bblog_offset));
1786 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001787 }
1788 }
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001791 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (rdev2->desc_nr+1 > max_dev)
1793 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001794
NeilBrown70471da2009-08-03 10:59:57 +10001795 if (max_dev > le32_to_cpu(sb->max_dev)) {
1796 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001797 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001798 rdev->sb_size = max_dev * 2 + 256;
1799 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1800 if (rdev->sb_size & bmask)
1801 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001802 } else
1803 max_dev = le32_to_cpu(sb->max_dev);
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 for (i=0; i<max_dev;i++)
1806 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001807
NeilBrowndafb20f2012-03-19 12:46:39 +11001808 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001810 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001812 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001814 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001815 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 else
1817 sb->dev_roles[i] = cpu_to_le16(0xffff);
1818 }
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 sb->sb_csum = calc_sb_1_csum(sb);
1821}
1822
Chris Webb0cd17fe2008-06-28 08:31:46 +10001823static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001824super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001825{
1826 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001827 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001828 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001829 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001830 if (rdev->data_offset != rdev->new_data_offset)
1831 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001832 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001833 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001834 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001835 max_sectors -= rdev->data_offset;
1836 if (!num_sectors || num_sectors > max_sectors)
1837 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001838 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001839 /* minor version 0 with bitmap we can't move */
1840 return 0;
1841 } else {
1842 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001843 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001844 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001845 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001846 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001847 if (!num_sectors || num_sectors > max_sectors)
1848 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001849 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001850 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001851 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001852 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001853 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001854 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001855 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001856 rdev->sb_page);
1857 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001858 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001859
1860}
1861
1862static int
1863super_1_allow_new_offset(struct md_rdev *rdev,
1864 unsigned long long new_offset)
1865{
1866 /* All necessary checks on new >= old have been done */
1867 struct bitmap *bitmap;
1868 if (new_offset >= rdev->data_offset)
1869 return 1;
1870
1871 /* with 1.0 metadata, there is no metadata to tread on
1872 * so we can always move back */
1873 if (rdev->mddev->minor_version == 0)
1874 return 1;
1875
1876 /* otherwise we must be sure not to step on
1877 * any metadata, so stay:
1878 * 36K beyond start of superblock
1879 * beyond end of badblocks
1880 * beyond write-intent bitmap
1881 */
1882 if (rdev->sb_start + (32+4)*2 > new_offset)
1883 return 0;
1884 bitmap = rdev->mddev->bitmap;
1885 if (bitmap && !rdev->mddev->bitmap_info.file &&
1886 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001887 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001888 return 0;
1889 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1890 return 0;
1891
1892 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001893}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Adrian Bunk75c96f82005-05-05 16:16:09 -07001895static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 [0] = {
1897 .name = "0.90.0",
1898 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001899 .load_super = super_90_load,
1900 .validate_super = super_90_validate,
1901 .sync_super = super_90_sync,
1902 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001903 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 },
1905 [1] = {
1906 .name = "md-1",
1907 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001908 .load_super = super_1_load,
1909 .validate_super = super_1_validate,
1910 .sync_super = super_1_sync,
1911 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001912 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 },
1914};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
NeilBrownfd01b882011-10-11 16:47:53 +11001916static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001917{
1918 if (mddev->sync_super) {
1919 mddev->sync_super(mddev, rdev);
1920 return;
1921 }
1922
1923 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1924
1925 super_types[mddev->major_version].sync_super(mddev, rdev);
1926}
1927
NeilBrownfd01b882011-10-11 16:47:53 +11001928static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
NeilBrown3cb03002011-10-11 16:45:26 +11001930 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
NeilBrown4b809912008-07-21 17:05:25 +10001932 rcu_read_lock();
1933 rdev_for_each_rcu(rdev, mddev1)
1934 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001935 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001936 rdev2->bdev->bd_contains) {
1937 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001938 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001939 }
1940 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return 0;
1942}
1943
1944static LIST_HEAD(pending_raid_disks);
1945
Andre Nollac5e7112009-08-03 10:59:47 +10001946/*
1947 * Try to register data integrity profile for an mddev
1948 *
1949 * This is called when an array is started and after a disk has been kicked
1950 * from the array. It only succeeds if all working and active component devices
1951 * are integrity capable with matching profiles.
1952 */
NeilBrownfd01b882011-10-11 16:47:53 +11001953int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001954{
NeilBrown3cb03002011-10-11 16:45:26 +11001955 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001956
Andre Nollac5e7112009-08-03 10:59:47 +10001957 if (list_empty(&mddev->disks))
1958 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001959 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1960 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001961 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001962 /* skip spares and non-functional disks */
1963 if (test_bit(Faulty, &rdev->flags))
1964 continue;
1965 if (rdev->raid_disk < 0)
1966 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001967 if (!reference) {
1968 /* Use the first rdev as the reference */
1969 reference = rdev;
1970 continue;
1971 }
1972 /* does this rdev's profile match the reference profile? */
1973 if (blk_integrity_compare(reference->bdev->bd_disk,
1974 rdev->bdev->bd_disk) < 0)
1975 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001976 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001977 if (!reference || !bdev_get_integrity(reference->bdev))
1978 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001979 /*
1980 * All component devices are integrity capable and have matching
1981 * profiles, register the common profile for the md device.
1982 */
1983 if (blk_integrity_register(mddev->gendisk,
1984 bdev_get_integrity(reference->bdev)) != 0) {
1985 printk(KERN_ERR "md: failed to register integrity for %s\n",
1986 mdname(mddev));
1987 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001988 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001989 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1990 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1991 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1992 mdname(mddev));
1993 return -EINVAL;
1994 }
Andre Nollac5e7112009-08-03 10:59:47 +10001995 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001996}
Andre Nollac5e7112009-08-03 10:59:47 +10001997EXPORT_SYMBOL(md_integrity_register);
1998
1999/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002000void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002001{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002002 struct blk_integrity *bi_rdev;
2003 struct blk_integrity *bi_mddev;
2004
2005 if (!mddev->gendisk)
2006 return;
2007
2008 bi_rdev = bdev_get_integrity(rdev->bdev);
2009 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002010
2011 if (!bi_mddev) /* nothing to do */
2012 return;
2013 if (rdev->raid_disk < 0) /* skip spares */
2014 return;
2015 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2016 rdev->bdev->bd_disk) >= 0)
2017 return;
2018 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2019 blk_integrity_unregister(mddev->gendisk);
2020}
2021EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002022
NeilBrownf72ffdd2014-09-30 14:23:59 +10002023static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002025 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002026 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002027 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002028 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Dan Williams11e2ede2008-04-30 00:52:32 -07002030 /* prevent duplicates */
2031 if (find_rdev(mddev, rdev->bdev->bd_dev))
2032 return -EEXIST;
2033
Andre Nolldd8ac332009-03-31 14:33:13 +11002034 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2035 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2036 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002037 if (mddev->pers) {
2038 /* Cannot change size, so fail
2039 * If mddev->level <= 0, then we don't care
2040 * about aligning sizes (e.g. linear)
2041 */
2042 if (mddev->level > 0)
2043 return -ENOSPC;
2044 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002045 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 /* Verify rdev->desc_nr is unique.
2049 * If it is -1, assign a free number, else
2050 * check number is not in use
2051 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002052 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (rdev->desc_nr < 0) {
2054 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002055 if (mddev->pers)
2056 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002057 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 choice++;
2059 rdev->desc_nr = choice;
2060 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002061 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002062 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002066 rcu_read_unlock();
NeilBrownde01dfa2009-02-06 18:02:46 +11002067 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2068 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2069 mdname(mddev), mddev->max_disks);
2070 return -EBUSY;
2071 }
NeilBrown19133a42005-11-08 21:39:35 -08002072 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002073 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002074 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002077 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002078
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002079 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002080 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002081
Tejun Heo0762b8b2008-08-25 19:56:12 +09002082 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002083 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2084 /* failure here is OK */;
2085 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002086
NeilBrown4b809912008-07-21 17:05:25 +10002087 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002088 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002089
2090 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002091 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002094
2095 fail:
2096 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2097 b, mdname(mddev));
2098 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
NeilBrown177a99b2008-02-06 01:39:56 -08002101static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002102{
NeilBrown3cb03002011-10-11 16:45:26 +11002103 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002104 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002105 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002106}
2107
NeilBrownf72ffdd2014-09-30 14:23:59 +10002108static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
2110 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002111
Tejun Heo49731ba2011-01-14 18:43:57 +01002112 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002113 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2115 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002116 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002117 sysfs_put(rdev->sysfs_state);
2118 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002119 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002120 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002121 * writing to 'remove' to "dev/state". We also need
2122 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002123 */
NeilBrown4b809912008-07-21 17:05:25 +10002124 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002125 INIT_WORK(&rdev->del_work, md_delayed_delete);
2126 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002127 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
2130/*
2131 * prevent the device from being mounted, repartitioned or
2132 * otherwise reused by a RAID array (or any other kernel
2133 * subsystem), by bd_claiming the device.
2134 */
NeilBrown3cb03002011-10-11 16:45:26 +11002135static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
2137 int err = 0;
2138 struct block_device *bdev;
2139 char b[BDEVNAME_SIZE];
2140
Tejun Heod4d77622010-11-13 11:55:18 +01002141 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002142 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (IS_ERR(bdev)) {
2144 printk(KERN_ERR "md: could not open %s.\n",
2145 __bdevname(dev, b));
2146 return PTR_ERR(bdev);
2147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 rdev->bdev = bdev;
2149 return err;
2150}
2151
NeilBrown3cb03002011-10-11 16:45:26 +11002152static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 struct block_device *bdev = rdev->bdev;
2155 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002156 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157}
2158
2159void md_autodetect_dev(dev_t dev);
2160
NeilBrownf72ffdd2014-09-30 14:23:59 +10002161static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
2163 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 printk(KERN_INFO "md: export_rdev(%s)\n",
2166 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002167 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002169 if (test_bit(AutoDetected, &rdev->flags))
2170 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171#endif
2172 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002173 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174}
2175
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002176void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 unbind_rdev_from_array(rdev);
2179 export_rdev(rdev);
2180}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002181EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
NeilBrownfd01b882011-10-11 16:47:53 +11002183static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
NeilBrown0638bb02014-09-25 17:43:47 +10002185 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
NeilBrown0638bb02014-09-25 17:43:47 +10002187 while (!list_empty(&mddev->disks)) {
2188 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2189 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002190 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 mddev->raid_disks = 0;
2193 mddev->major_version = 0;
2194}
2195
NeilBrownf72ffdd2014-09-30 14:23:59 +10002196static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
NeilBrown42543762006-06-26 00:27:57 -07002198 /* Update each superblock (in-memory image), but
2199 * if we are allowed to, skip spares which already
2200 * have the right event counter, or have one earlier
2201 * (which would mean they aren't being marked as dirty
2202 * with the rest of the array)
2203 */
NeilBrown3cb03002011-10-11 16:45:26 +11002204 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002205 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002206 if (rdev->sb_events == mddev->events ||
2207 (nospares &&
2208 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002209 rdev->sb_events+1 == mddev->events)) {
2210 /* Don't update this superblock */
2211 rdev->sb_loaded = 2;
2212 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002213 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002214 rdev->sb_loaded = 1;
2215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
2217}
2218
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002219void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
NeilBrown3cb03002011-10-11 16:45:26 +11002221 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002222 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002223 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002224 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
NeilBrownd87f0642013-04-24 11:42:40 +10002226 if (mddev->ro) {
2227 if (force_change)
2228 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2229 return;
2230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002232 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002233 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002234 if (rdev->raid_disk >= 0 &&
2235 mddev->delta_disks >= 0 &&
2236 !test_bit(In_sync, &rdev->flags) &&
2237 mddev->curr_resync_completed > rdev->recovery_offset)
2238 rdev->recovery_offset = mddev->curr_resync_completed;
2239
NeilBrownf72ffdd2014-09-30 14:23:59 +10002240 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002241 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002242 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002243 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002244 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002245 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002246 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002247 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002248 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002249 md_ack_all_badblocks(&rdev->badblocks);
2250 md_error(mddev, rdev);
2251 }
2252 clear_bit(Blocked, &rdev->flags);
2253 clear_bit(BlockedBadBlocks, &rdev->flags);
2254 wake_up(&rdev->blocked_wait);
2255 }
2256 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002257 wake_up(&mddev->sb_wait);
2258 return;
2259 }
2260
NeilBrown85572d72014-12-15 12:56:56 +11002261 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002262
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002263 mddev->utime = get_seconds();
2264
NeilBrown850b2b422006-10-03 01:15:46 -07002265 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2266 force_change = 1;
2267 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2268 /* just a clean<-> dirty transition, possibly leave spares alone,
2269 * though if events isn't the right even/odd, we will have to do
2270 * spares after all
2271 */
2272 nospares = 1;
2273 if (force_change)
2274 nospares = 0;
2275 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002276 /* If the array is degraded, then skipping spares is both
2277 * dangerous and fairly pointless.
2278 * Dangerous because a device that was removed from the array
2279 * might have a event_count that still looks up-to-date,
2280 * so it can be re-added without a resync.
2281 * Pointless because if there are any spares to skip,
2282 * then a recovery will happen and soon that array won't
2283 * be degraded any more and the spare can go back to sleep then.
2284 */
NeilBrown850b2b422006-10-03 01:15:46 -07002285 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002286
NeilBrown06d91a52005-06-21 17:17:12 -07002287 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002288
2289 /* If this is just a dirty<->clean transition, and the array is clean
2290 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002291 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002292 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002293 && mddev->can_decrease_events
2294 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002295 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002296 mddev->can_decrease_events = 0;
2297 } else {
NeilBrown42543762006-06-26 00:27:57 -07002298 /* otherwise we have to go forward and ... */
2299 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002300 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
NeilBrown403df472014-09-30 15:52:29 +10002303 /*
2304 * This 64-bit counter should never wrap.
2305 * Either we are in around ~1 trillion A.C., assuming
2306 * 1 reboot per second, or we have a bug...
2307 */
2308 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002309
NeilBrowndafb20f2012-03-19 12:46:39 +11002310 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002311 if (rdev->badblocks.changed)
2312 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002313 if (test_bit(Faulty, &rdev->flags))
2314 set_bit(FaultRecorded, &rdev->flags);
2315 }
NeilBrown2699b672011-07-28 11:31:47 +10002316
NeilBrowne6910632008-02-06 01:39:51 -08002317 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002318 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
NeilBrown36a4e1f2011-10-07 14:23:17 +11002320 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2321 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
NeilBrown4ad13662007-07-17 04:06:13 -07002323 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002324 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002326
NeilBrown42543762006-06-26 00:27:57 -07002327 if (rdev->sb_loaded != 1)
2328 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
NeilBrownf4667222013-12-09 12:04:56 +11002330 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002331 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002332 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002333 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002334 pr_debug("md: (write) %s's sb offset: %llu\n",
2335 bdevname(rdev->bdev, b),
2336 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002337 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002338 if (rdev->badblocks.size) {
2339 md_super_write(mddev, rdev,
2340 rdev->badblocks.sector,
2341 rdev->badblocks.size << 9,
2342 rdev->bb_page);
2343 rdev->badblocks.size = 0;
2344 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002345
NeilBrownf4667222013-12-09 12:04:56 +11002346 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002347 pr_debug("md: %s (skipping faulty)\n",
2348 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002349
NeilBrown7bfa19f2005-06-21 17:17:28 -07002350 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 /* only need to write one superblock... */
2352 break;
2353 }
NeilBrowna9701a32005-11-08 21:39:34 -08002354 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002355 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002356
NeilBrown85572d72014-12-15 12:56:56 +11002357 spin_lock(&mddev->lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002358 if (mddev->in_sync != sync_req ||
2359 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002360 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002361 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002362 goto repeat;
2363 }
NeilBrown850b2b422006-10-03 01:15:46 -07002364 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002365 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002366 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002367 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2368 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002369
NeilBrowndafb20f2012-03-19 12:46:39 +11002370 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002371 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2372 clear_bit(Blocked, &rdev->flags);
2373
2374 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002375 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002376 clear_bit(BlockedBadBlocks, &rdev->flags);
2377 wake_up(&rdev->blocked_wait);
2378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002380EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002382static int add_bound_rdev(struct md_rdev *rdev)
2383{
2384 struct mddev *mddev = rdev->mddev;
2385 int err = 0;
2386
2387 if (!mddev->pers->hot_remove_disk) {
2388 /* If there is hot_add_disk but no hot_remove_disk
2389 * then added disks for geometry changes,
2390 * and should be added immediately.
2391 */
2392 super_types[mddev->major_version].
2393 validate_super(mddev, rdev);
2394 err = mddev->pers->hot_add_disk(mddev, rdev);
2395 if (err) {
2396 unbind_rdev_from_array(rdev);
2397 export_rdev(rdev);
2398 return err;
2399 }
2400 }
2401 sysfs_notify_dirent_safe(rdev->sysfs_state);
2402
2403 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2404 if (mddev->degraded)
2405 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2406 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2407 md_new_event(mddev);
2408 md_wakeup_thread(mddev->thread);
2409 return 0;
2410}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Andre Noll7f6ce762008-03-23 18:34:54 +01002412/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002413 * We want to accept with case. For this we use cmd_match.
2414 */
2415static int cmd_match(const char *cmd, const char *str)
2416{
2417 /* See if cmd, written into a sysfs file, matches
2418 * str. They must either be the same, or cmd can
2419 * have a trailing newline
2420 */
2421 while (*cmd && *str && *cmd == *str) {
2422 cmd++;
2423 str++;
2424 }
2425 if (*cmd == '\n')
2426 cmd++;
2427 if (*str || *cmd)
2428 return 0;
2429 return 1;
2430}
2431
NeilBrown86e6ffd2005-11-08 21:39:24 -08002432struct rdev_sysfs_entry {
2433 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002434 ssize_t (*show)(struct md_rdev *, char *);
2435 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002436};
2437
2438static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002439state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002440{
2441 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002442 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002443 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002444
NeilBrown758bfc82014-12-15 12:56:59 +11002445 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002446 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002447 len+= sprintf(page+len, "%sfaulty",sep);
2448 sep = ",";
2449 }
NeilBrown758bfc82014-12-15 12:56:59 +11002450 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002451 len += sprintf(page+len, "%sin_sync",sep);
2452 sep = ",";
2453 }
NeilBrown758bfc82014-12-15 12:56:59 +11002454 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002455 len += sprintf(page+len, "%swrite_mostly",sep);
2456 sep = ",";
2457 }
NeilBrown758bfc82014-12-15 12:56:59 +11002458 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002459 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002460 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002461 len += sprintf(page+len, "%sblocked", sep);
2462 sep = ",";
2463 }
NeilBrown758bfc82014-12-15 12:56:59 +11002464 if (!test_bit(Faulty, &flags) &&
2465 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002466 len += sprintf(page+len, "%sspare", sep);
2467 sep = ",";
2468 }
NeilBrown758bfc82014-12-15 12:56:59 +11002469 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002470 len += sprintf(page+len, "%swrite_error", sep);
2471 sep = ",";
2472 }
NeilBrown758bfc82014-12-15 12:56:59 +11002473 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002474 len += sprintf(page+len, "%swant_replacement", sep);
2475 sep = ",";
2476 }
NeilBrown758bfc82014-12-15 12:56:59 +11002477 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002478 len += sprintf(page+len, "%sreplacement", sep);
2479 sep = ",";
2480 }
2481
NeilBrown86e6ffd2005-11-08 21:39:24 -08002482 return len+sprintf(page+len, "\n");
2483}
2484
NeilBrown45dc2de2006-06-26 00:27:58 -07002485static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002486state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002487{
2488 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002489 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002490 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002491 * writemostly - sets write_mostly
2492 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002493 * blocked - sets the Blocked flags
2494 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002495 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002496 * -insync - clear Insync for a device with a slot assigned,
2497 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002498 * write_error - sets WriteErrorSeen
2499 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002500 */
2501 int err = -EINVAL;
2502 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2503 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002504 if (test_bit(Faulty, &rdev->flags))
2505 err = 0;
2506 else
2507 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002508 } else if (cmd_match(buf, "remove")) {
2509 if (rdev->raid_disk >= 0)
2510 err = -EBUSY;
2511 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002512 struct mddev *mddev = rdev->mddev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05002513 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05002514 md_cluster_ops->remove_disk(mddev, rdev);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002515 md_kick_rdev_from_array(rdev);
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05002516 if (mddev_is_clustered(mddev))
2517 md_cluster_ops->metadata_update_start(mddev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002518 if (mddev->pers)
2519 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002520 md_new_event(mddev);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05002521 if (mddev_is_clustered(mddev))
2522 md_cluster_ops->metadata_update_finish(mddev);
NeilBrown45dc2de2006-06-26 00:27:58 -07002523 err = 0;
2524 }
NeilBrownf6556752006-06-26 00:28:01 -07002525 } else if (cmd_match(buf, "writemostly")) {
2526 set_bit(WriteMostly, &rdev->flags);
2527 err = 0;
2528 } else if (cmd_match(buf, "-writemostly")) {
2529 clear_bit(WriteMostly, &rdev->flags);
2530 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002531 } else if (cmd_match(buf, "blocked")) {
2532 set_bit(Blocked, &rdev->flags);
2533 err = 0;
2534 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002535 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002536 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002537 /* metadata handler doesn't understand badblocks,
2538 * so we need to fail the device
2539 */
2540 md_error(rdev->mddev, rdev);
2541 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002542 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002543 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002544 wake_up(&rdev->blocked_wait);
2545 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2546 md_wakeup_thread(rdev->mddev->thread);
2547
2548 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002549 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2550 set_bit(In_sync, &rdev->flags);
2551 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002552 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
NeilBrowne1960f82014-09-30 15:24:25 +10002553 if (rdev->mddev->pers == NULL) {
2554 clear_bit(In_sync, &rdev->flags);
2555 rdev->saved_raid_disk = rdev->raid_disk;
2556 rdev->raid_disk = -1;
2557 err = 0;
2558 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002559 } else if (cmd_match(buf, "write_error")) {
2560 set_bit(WriteErrorSeen, &rdev->flags);
2561 err = 0;
2562 } else if (cmd_match(buf, "-write_error")) {
2563 clear_bit(WriteErrorSeen, &rdev->flags);
2564 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002565 } else if (cmd_match(buf, "want_replacement")) {
2566 /* Any non-spare device that is not a replacement can
2567 * become want_replacement at any time, but we then need to
2568 * check if recovery is needed.
2569 */
2570 if (rdev->raid_disk >= 0 &&
2571 !test_bit(Replacement, &rdev->flags))
2572 set_bit(WantReplacement, &rdev->flags);
2573 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2574 md_wakeup_thread(rdev->mddev->thread);
2575 err = 0;
2576 } else if (cmd_match(buf, "-want_replacement")) {
2577 /* Clearing 'want_replacement' is always allowed.
2578 * Once replacements starts it is too late though.
2579 */
2580 err = 0;
2581 clear_bit(WantReplacement, &rdev->flags);
2582 } else if (cmd_match(buf, "replacement")) {
2583 /* Can only set a device as a replacement when array has not
2584 * yet been started. Once running, replacement is automatic
2585 * from spares, or by assigning 'slot'.
2586 */
2587 if (rdev->mddev->pers)
2588 err = -EBUSY;
2589 else {
2590 set_bit(Replacement, &rdev->flags);
2591 err = 0;
2592 }
2593 } else if (cmd_match(buf, "-replacement")) {
2594 /* Similarly, can only clear Replacement before start */
2595 if (rdev->mddev->pers)
2596 err = -EBUSY;
2597 else {
2598 clear_bit(Replacement, &rdev->flags);
2599 err = 0;
2600 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002601 } else if (cmd_match(buf, "re-add")) {
2602 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002603 /* clear_bit is performed _after_ all the devices
2604 * have their local Faulty bit cleared. If any writes
2605 * happen in the meantime in the local node, they
2606 * will land in the local bitmap, which will be synced
2607 * by this node eventually
2608 */
2609 if (!mddev_is_clustered(rdev->mddev) ||
2610 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2611 clear_bit(Faulty, &rdev->flags);
2612 err = add_bound_rdev(rdev);
2613 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002614 } else
2615 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002616 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002617 if (!err)
2618 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002619 return err ? err : len;
2620}
NeilBrown80ca3a42006-07-10 04:44:18 -07002621static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002622__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002623
2624static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002625errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002626{
2627 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2628}
2629
2630static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002631errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002632{
2633 char *e;
2634 unsigned long n = simple_strtoul(buf, &e, 10);
2635 if (*buf && (*e == 0 || *e == '\n')) {
2636 atomic_set(&rdev->corrected_errors, n);
2637 return len;
2638 }
2639 return -EINVAL;
2640}
2641static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002642__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002643
NeilBrown014236d2006-01-06 00:20:55 -08002644static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002645slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002646{
2647 if (rdev->raid_disk < 0)
2648 return sprintf(page, "none\n");
2649 else
2650 return sprintf(page, "%d\n", rdev->raid_disk);
2651}
2652
2653static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002654slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002655{
2656 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002657 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002658 int slot = simple_strtoul(buf, &e, 10);
2659 if (strncmp(buf, "none", 4)==0)
2660 slot = -1;
2661 else if (e==buf || (*e && *e!= '\n'))
2662 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002663 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002664 /* Setting 'slot' on an active array requires also
2665 * updating the 'rd%d' link, and communicating
2666 * with the personality with ->hot_*_disk.
2667 * For now we only support removing
2668 * failed/spare devices. This normally happens automatically,
2669 * but not when the metadata is externally managed.
2670 */
NeilBrownc303da62008-02-06 01:39:51 -08002671 if (rdev->raid_disk == -1)
2672 return -EEXIST;
2673 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002674 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002675 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002676 clear_bit(Blocked, &rdev->flags);
2677 remove_and_add_spares(rdev->mddev, rdev);
2678 if (rdev->raid_disk >= 0)
2679 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002680 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2681 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002682 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002683 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002684 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002685 */
2686
2687 if (rdev->raid_disk != -1)
2688 return -EBUSY;
2689
NeilBrownc6751b22011-02-02 11:57:13 +11002690 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2691 return -EBUSY;
2692
Neil Brown6c2fce22008-06-28 08:31:31 +10002693 if (rdev->mddev->pers->hot_add_disk == NULL)
2694 return -EINVAL;
2695
NeilBrownba1b41b2011-01-14 09:14:34 +11002696 if (slot >= rdev->mddev->raid_disks &&
2697 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2698 return -ENOSPC;
2699
Neil Brown6c2fce22008-06-28 08:31:31 +10002700 rdev->raid_disk = slot;
2701 if (test_bit(In_sync, &rdev->flags))
2702 rdev->saved_raid_disk = slot;
2703 else
2704 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002705 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002706 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002707 err = rdev->mddev->pers->
2708 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002709 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002710 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002711 return err;
Neil Brown52664732008-06-28 08:31:44 +10002712 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002713 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002714 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002715 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002716 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002717 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002718 if (slot >= rdev->mddev->raid_disks &&
2719 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002720 return -ENOSPC;
2721 rdev->raid_disk = slot;
2722 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002723 clear_bit(Faulty, &rdev->flags);
2724 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002725 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002726 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002727 }
NeilBrown014236d2006-01-06 00:20:55 -08002728 return len;
2729}
2730
NeilBrown014236d2006-01-06 00:20:55 -08002731static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002732__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002733
NeilBrown93c8cad2006-01-06 00:20:56 -08002734static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002735offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002736{
Andrew Morton6961ece2006-01-06 00:20:59 -08002737 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002738}
2739
2740static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002741offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002742{
NeilBrownc6563a82012-05-21 09:27:00 +10002743 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002744 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002745 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002746 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002747 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002748 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002749 /* Must set offset before size, so overlap checks
2750 * can be sane */
2751 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002752 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002753 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002754 return len;
2755}
2756
2757static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002758__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002759
NeilBrownc6563a82012-05-21 09:27:00 +10002760static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2761{
2762 return sprintf(page, "%llu\n",
2763 (unsigned long long)rdev->new_data_offset);
2764}
2765
2766static ssize_t new_offset_store(struct md_rdev *rdev,
2767 const char *buf, size_t len)
2768{
2769 unsigned long long new_offset;
2770 struct mddev *mddev = rdev->mddev;
2771
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002772 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002773 return -EINVAL;
2774
NeilBrownf851b602014-12-11 10:02:10 +11002775 if (mddev->sync_thread ||
2776 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002777 return -EBUSY;
2778 if (new_offset == rdev->data_offset)
2779 /* reset is always permitted */
2780 ;
2781 else if (new_offset > rdev->data_offset) {
2782 /* must not push array size beyond rdev_sectors */
2783 if (new_offset - rdev->data_offset
2784 + mddev->dev_sectors > rdev->sectors)
2785 return -E2BIG;
2786 }
2787 /* Metadata worries about other space details. */
2788
2789 /* decreasing the offset is inconsistent with a backwards
2790 * reshape.
2791 */
2792 if (new_offset < rdev->data_offset &&
2793 mddev->reshape_backwards)
2794 return -EINVAL;
2795 /* Increasing offset is inconsistent with forwards
2796 * reshape. reshape_direction should be set to
2797 * 'backwards' first.
2798 */
2799 if (new_offset > rdev->data_offset &&
2800 !mddev->reshape_backwards)
2801 return -EINVAL;
2802
2803 if (mddev->pers && mddev->persistent &&
2804 !super_types[mddev->major_version]
2805 .allow_new_offset(rdev, new_offset))
2806 return -E2BIG;
2807 rdev->new_data_offset = new_offset;
2808 if (new_offset > rdev->data_offset)
2809 mddev->reshape_backwards = 1;
2810 else if (new_offset < rdev->data_offset)
2811 mddev->reshape_backwards = 0;
2812
2813 return len;
2814}
2815static struct rdev_sysfs_entry rdev_new_offset =
2816__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2817
NeilBrown83303b62006-01-06 00:21:06 -08002818static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002819rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002820{
Andre Nolldd8ac332009-03-31 14:33:13 +11002821 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002822}
2823
NeilBrownc5d79ad2008-02-06 01:39:54 -08002824static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2825{
2826 /* check if two start/length pairs overlap */
2827 if (s1+l1 <= s2)
2828 return 0;
2829 if (s2+l2 <= s1)
2830 return 0;
2831 return 1;
2832}
2833
Dan Williamsb522adc2009-03-31 15:00:31 +11002834static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2835{
2836 unsigned long long blocks;
2837 sector_t new;
2838
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002839 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002840 return -EINVAL;
2841
2842 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2843 return -EINVAL; /* sector conversion overflow */
2844
2845 new = blocks * 2;
2846 if (new != blocks * 2)
2847 return -EINVAL; /* unsigned long long to sector_t overflow */
2848
2849 *sectors = new;
2850 return 0;
2851}
2852
NeilBrown83303b62006-01-06 00:21:06 -08002853static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002854rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002855{
NeilBrownfd01b882011-10-11 16:47:53 +11002856 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002857 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002858 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002859
Dan Williamsb522adc2009-03-31 15:00:31 +11002860 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002861 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002862 if (rdev->data_offset != rdev->new_data_offset)
2863 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002864 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002865 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002866 sectors = super_types[my_mddev->major_version].
2867 rdev_size_change(rdev, sectors);
2868 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002869 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002870 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002871 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002872 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002873 if (!my_mddev->pers->resize)
2874 /* Cannot change size for RAID0 or Linear etc */
2875 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002876 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002877 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002878 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002879
Andre Nolldd8ac332009-03-31 14:33:13 +11002880 rdev->sectors = sectors;
2881 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002882 /* Need to check that all other rdevs with the same
2883 * ->bdev do not overlap. 'rcu' is sufficient to walk
2884 * the rdev lists safely.
2885 * This check does not provide a hard guarantee, it
2886 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002887 */
NeilBrownfd01b882011-10-11 16:47:53 +11002888 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002889 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002890 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002891
NeilBrown8b1afc32014-09-29 15:33:20 +10002892 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002893 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002894 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002895
NeilBrowndafb20f2012-03-19 12:46:39 +11002896 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002897 if (rdev->bdev == rdev2->bdev &&
2898 rdev != rdev2 &&
2899 overlaps(rdev->data_offset, rdev->sectors,
2900 rdev2->data_offset,
2901 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002902 overlap = 1;
2903 break;
2904 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002905 if (overlap) {
2906 mddev_put(mddev);
2907 break;
2908 }
2909 }
NeilBrown8b1afc32014-09-29 15:33:20 +10002910 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08002911 if (overlap) {
2912 /* Someone else could have slipped in a size
2913 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002914 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002915 * safe, and trust userspace not to race with
2916 * itself
2917 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002918 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002919 return -EBUSY;
2920 }
2921 }
NeilBrown83303b62006-01-06 00:21:06 -08002922 return len;
2923}
2924
2925static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002926__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002927
NeilBrown3cb03002011-10-11 16:45:26 +11002928static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002929{
2930 unsigned long long recovery_start = rdev->recovery_offset;
2931
2932 if (test_bit(In_sync, &rdev->flags) ||
2933 recovery_start == MaxSector)
2934 return sprintf(page, "none\n");
2935
2936 return sprintf(page, "%llu\n", recovery_start);
2937}
2938
NeilBrown3cb03002011-10-11 16:45:26 +11002939static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002940{
2941 unsigned long long recovery_start;
2942
2943 if (cmd_match(buf, "none"))
2944 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002945 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07002946 return -EINVAL;
2947
2948 if (rdev->mddev->pers &&
2949 rdev->raid_disk >= 0)
2950 return -EBUSY;
2951
2952 rdev->recovery_offset = recovery_start;
2953 if (recovery_start == MaxSector)
2954 set_bit(In_sync, &rdev->flags);
2955 else
2956 clear_bit(In_sync, &rdev->flags);
2957 return len;
2958}
2959
2960static struct rdev_sysfs_entry rdev_recovery_start =
2961__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2962
NeilBrown16c791a2011-07-28 11:31:47 +10002963static ssize_t
2964badblocks_show(struct badblocks *bb, char *page, int unack);
2965static ssize_t
2966badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2967
NeilBrown3cb03002011-10-11 16:45:26 +11002968static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002969{
2970 return badblocks_show(&rdev->badblocks, page, 0);
2971}
NeilBrown3cb03002011-10-11 16:45:26 +11002972static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002973{
NeilBrownde393cd2011-07-28 11:31:48 +10002974 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2975 /* Maybe that ack was all we needed */
2976 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2977 wake_up(&rdev->blocked_wait);
2978 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002979}
2980static struct rdev_sysfs_entry rdev_bad_blocks =
2981__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2982
NeilBrown3cb03002011-10-11 16:45:26 +11002983static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002984{
2985 return badblocks_show(&rdev->badblocks, page, 1);
2986}
NeilBrown3cb03002011-10-11 16:45:26 +11002987static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002988{
2989 return badblocks_store(&rdev->badblocks, page, len, 1);
2990}
2991static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2992__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2993
NeilBrown86e6ffd2005-11-08 21:39:24 -08002994static struct attribute *rdev_default_attrs[] = {
2995 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002996 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002997 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002998 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10002999 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003000 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003001 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003002 &rdev_bad_blocks.attr,
3003 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003004 NULL,
3005};
3006static ssize_t
3007rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3008{
3009 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003010 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003011
3012 if (!entry->show)
3013 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003014 if (!rdev->mddev)
3015 return -EBUSY;
3016 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003017}
3018
3019static ssize_t
3020rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3021 const char *page, size_t length)
3022{
3023 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003024 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003025 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003026 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003027
3028 if (!entry->store)
3029 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003030 if (!capable(CAP_SYS_ADMIN))
3031 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003032 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003033 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003034 if (rdev->mddev == NULL)
3035 rv = -EBUSY;
3036 else
3037 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003038 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003039 }
3040 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003041}
3042
3043static void rdev_free(struct kobject *ko)
3044{
NeilBrown3cb03002011-10-11 16:45:26 +11003045 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003046 kfree(rdev);
3047}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003048static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003049 .show = rdev_attr_show,
3050 .store = rdev_attr_store,
3051};
3052static struct kobj_type rdev_ktype = {
3053 .release = rdev_free,
3054 .sysfs_ops = &rdev_sysfs_ops,
3055 .default_attrs = rdev_default_attrs,
3056};
3057
NeilBrown3cb03002011-10-11 16:45:26 +11003058int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003059{
3060 rdev->desc_nr = -1;
3061 rdev->saved_raid_disk = -1;
3062 rdev->raid_disk = -1;
3063 rdev->flags = 0;
3064 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003065 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003066 rdev->sb_events = 0;
3067 rdev->last_read_error.tv_sec = 0;
3068 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003069 rdev->sb_loaded = 0;
3070 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003071 atomic_set(&rdev->nr_pending, 0);
3072 atomic_set(&rdev->read_errors, 0);
3073 atomic_set(&rdev->corrected_errors, 0);
3074
3075 INIT_LIST_HEAD(&rdev->same_set);
3076 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003077
3078 /* Add space to store bad block list.
3079 * This reserves the space even on arrays where it cannot
3080 * be used - I wonder if that matters
3081 */
3082 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003083 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003084 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3085 seqlock_init(&rdev->badblocks.lock);
3086 if (rdev->badblocks.page == NULL)
3087 return -ENOMEM;
3088
3089 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003090}
3091EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092/*
3093 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3094 *
3095 * mark the device faulty if:
3096 *
3097 * - the device is nonexistent (zero size)
3098 * - the device has no valid superblock
3099 *
3100 * a faulty rdev _never_ has rdev->sb set.
3101 */
NeilBrown3cb03002011-10-11 16:45:26 +11003102static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103{
3104 char b[BDEVNAME_SIZE];
3105 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003106 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 sector_t size;
3108
NeilBrown9ffae0c2006-01-06 00:20:32 -08003109 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (!rdev) {
3111 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3112 return ERR_PTR(-ENOMEM);
3113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
NeilBrown2230dfe2011-07-28 11:31:46 +10003115 err = md_rdev_init(rdev);
3116 if (err)
3117 goto abort_free;
3118 err = alloc_disk_sb(rdev);
3119 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 goto abort_free;
3121
NeilBrownc5d79ad2008-02-06 01:39:54 -08003122 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 if (err)
3124 goto abort_free;
3125
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003126 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003127
Mike Snitzer77304d22010-11-08 14:39:12 +01003128 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003130 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 "md: %s has zero or unknown size, marking faulty!\n",
3132 bdevname(rdev->bdev,b));
3133 err = -EINVAL;
3134 goto abort_free;
3135 }
3136
3137 if (super_format >= 0) {
3138 err = super_types[super_format].
3139 load_super(rdev, NULL, super_minor);
3140 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003141 printk(KERN_WARNING
3142 "md: %s does not have a valid v%d.%d "
3143 "superblock, not importing!\n",
3144 bdevname(rdev->bdev,b),
3145 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 goto abort_free;
3147 }
3148 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003149 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 "md: could not read %s's sb, not importing!\n",
3151 bdevname(rdev->bdev,b));
3152 goto abort_free;
3153 }
3154 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003155
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 return rdev;
3157
3158abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003159 if (rdev->bdev)
3160 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003161 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 kfree(rdev);
3163 return ERR_PTR(err);
3164}
3165
3166/*
3167 * Check a full RAID array for plausibility
3168 */
3169
NeilBrownf72ffdd2014-09-30 14:23:59 +10003170static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
3172 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003173 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 char b[BDEVNAME_SIZE];
3175
3176 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003177 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 switch (super_types[mddev->major_version].
3179 load_super(rdev, freshest, mddev->minor_version)) {
3180 case 1:
3181 freshest = rdev;
3182 break;
3183 case 0:
3184 break;
3185 default:
3186 printk( KERN_ERR \
3187 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003188 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003190 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
3192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 super_types[mddev->major_version].
3194 validate_super(mddev, freshest);
3195
3196 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003197 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003198 if (mddev->max_disks &&
3199 (rdev->desc_nr >= mddev->max_disks ||
3200 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003201 printk(KERN_WARNING
3202 "md: %s: %s: only %d devices permitted\n",
3203 mdname(mddev), bdevname(rdev->bdev, b),
3204 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003205 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003206 continue;
3207 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003208 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 if (super_types[mddev->major_version].
3210 validate_super(mddev, rdev)) {
3211 printk(KERN_WARNING "md: kicking non-fresh %s"
3212 " from array!\n",
3213 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003214 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 continue;
3216 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003217 /* No device should have a Candidate flag
3218 * when reading devices
3219 */
3220 if (test_bit(Candidate, &rdev->flags)) {
3221 pr_info("md: kicking Cluster Candidate %s from array!\n",
3222 bdevname(rdev->bdev, b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003223 md_kick_rdev_from_array(rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003224 }
3225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 if (mddev->level == LEVEL_MULTIPATH) {
3227 rdev->desc_nr = i++;
3228 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003229 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003230 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003231 rdev->raid_disk = -1;
3232 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 }
3234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235}
3236
NeilBrown72e02072009-12-14 12:49:55 +11003237/* Read a fixed-point number.
3238 * Numbers in sysfs attributes should be in "standard" units where
3239 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003240 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003241 * milliseconds or jiffies.
3242 * This function takes a decimal number with a possible fractional
3243 * component, and produces an integer which is the result of
3244 * multiplying that number by 10^'scale'.
3245 * all without any floating-point arithmetic.
3246 */
3247int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3248{
3249 unsigned long result = 0;
3250 long decimals = -1;
3251 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3252 if (*cp == '.')
3253 decimals = 0;
3254 else if (decimals < scale) {
3255 unsigned int value;
3256 value = *cp - '0';
3257 result = result * 10 + value;
3258 if (decimals >= 0)
3259 decimals++;
3260 }
3261 cp++;
3262 }
3263 if (*cp == '\n')
3264 cp++;
3265 if (*cp)
3266 return -EINVAL;
3267 if (decimals < 0)
3268 decimals = 0;
3269 while (decimals < scale) {
3270 result *= 10;
3271 decimals ++;
3272 }
3273 *res = result;
3274 return 0;
3275}
3276
NeilBrown19052c02008-08-05 15:54:13 +10003277static void md_safemode_timeout(unsigned long data);
3278
NeilBrowneae17012005-11-08 21:39:23 -08003279static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003280safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003281{
3282 int msec = (mddev->safemode_delay*1000)/HZ;
3283 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3284}
3285static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003286safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003287{
NeilBrown16f17b32006-06-26 00:27:37 -07003288 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003289
NeilBrown72e02072009-12-14 12:49:55 +11003290 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003291 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003292 if (msec == 0)
3293 mddev->safemode_delay = 0;
3294 else {
NeilBrown19052c02008-08-05 15:54:13 +10003295 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003296 unsigned long new_delay = (msec*HZ)/1000;
3297
3298 if (new_delay == 0)
3299 new_delay = 1;
3300 mddev->safemode_delay = new_delay;
3301 if (new_delay < old_delay || old_delay == 0)
3302 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003303 }
3304 return len;
3305}
3306static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003307__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003308
3309static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003310level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003311{
NeilBrown36d091f2014-12-15 12:56:58 +11003312 struct md_personality *p;
3313 int ret;
3314 spin_lock(&mddev->lock);
3315 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003316 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003317 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003318 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003319 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003320 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003321 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003322 else
NeilBrown36d091f2014-12-15 12:56:58 +11003323 ret = 0;
3324 spin_unlock(&mddev->lock);
3325 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003326}
3327
NeilBrownd9d166c2006-01-06 00:20:51 -08003328static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003329level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003330{
Dan Williamsf2859af2010-05-02 10:04:16 -07003331 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003332 ssize_t rv;
3333 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003334 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003335 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003336 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003337 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003338
NeilBrown67918752014-12-15 12:57:01 +11003339 if (slen == 0 || slen >= sizeof(clevel))
3340 return -EINVAL;
3341
3342 rv = mddev_lock(mddev);
3343 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003344 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003345
3346 if (mddev->pers == NULL) {
3347 strncpy(mddev->clevel, buf, slen);
3348 if (mddev->clevel[slen-1] == '\n')
3349 slen--;
3350 mddev->clevel[slen] = 0;
3351 mddev->level = LEVEL_NONE;
3352 rv = len;
3353 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003354 }
NeilBrown67918752014-12-15 12:57:01 +11003355 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003356 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003357 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003358
3359 /* request to change the personality. Need to ensure:
3360 * - array is not engaged in resync/recovery/reshape
3361 * - old personality can be suspended
3362 * - new personality will access other array.
3363 */
3364
NeilBrown67918752014-12-15 12:57:01 +11003365 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003366 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003367 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003368 mddev->reshape_position != MaxSector ||
3369 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003370 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003371
NeilBrown67918752014-12-15 12:57:01 +11003372 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003373 if (!mddev->pers->quiesce) {
3374 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3375 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003376 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003377 }
3378
3379 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003380 strncpy(clevel, buf, slen);
3381 if (clevel[slen-1] == '\n')
3382 slen--;
3383 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003384 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003385 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003386
Dan Williamsf2859af2010-05-02 10:04:16 -07003387 if (request_module("md-%s", clevel) != 0)
3388 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003389 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003390 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003391 if (!pers || !try_module_get(pers->owner)) {
3392 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003393 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003394 rv = -EINVAL;
3395 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003396 }
3397 spin_unlock(&pers_lock);
3398
3399 if (pers == mddev->pers) {
3400 /* Nothing to do! */
3401 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003402 rv = len;
3403 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003404 }
3405 if (!pers->takeover) {
3406 module_put(pers->owner);
3407 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003408 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003409 rv = -EINVAL;
3410 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003411 }
3412
NeilBrowndafb20f2012-03-19 12:46:39 +11003413 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003414 rdev->new_raid_disk = rdev->raid_disk;
3415
NeilBrown245f46c2009-03-31 14:39:39 +11003416 /* ->takeover must set new_* and/or delta_disks
3417 * if it succeeds, and may set them when it fails.
3418 */
3419 priv = pers->takeover(mddev);
3420 if (IS_ERR(priv)) {
3421 mddev->new_level = mddev->level;
3422 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003423 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003424 mddev->raid_disks -= mddev->delta_disks;
3425 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003426 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003427 module_put(pers->owner);
3428 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003429 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003430 rv = PTR_ERR(priv);
3431 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003432 }
3433
3434 /* Looks like we have a winner */
3435 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003436 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003437
3438 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003439 oldpers = mddev->pers;
3440 oldpriv = mddev->private;
3441 mddev->pers = pers;
3442 mddev->private = priv;
3443 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3444 mddev->level = mddev->new_level;
3445 mddev->layout = mddev->new_layout;
3446 mddev->chunk_sectors = mddev->new_chunk_sectors;
3447 mddev->delta_disks = 0;
3448 mddev->reshape_backwards = 0;
3449 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003450 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003451
NeilBrowndb721d32014-12-15 12:56:58 +11003452 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003453 mddev->external) {
3454 /* We are converting from a no-redundancy array
3455 * to a redundancy array and metadata is managed
3456 * externally so we need to be sure that writes
3457 * won't block due to a need to transition
3458 * clean->dirty
3459 * until external management is started.
3460 */
3461 mddev->in_sync = 0;
3462 mddev->safemode_delay = 0;
3463 mddev->safemode = 0;
3464 }
3465
NeilBrowndb721d32014-12-15 12:56:58 +11003466 oldpers->free(mddev, oldpriv);
3467
3468 if (oldpers->sync_request == NULL &&
3469 pers->sync_request != NULL) {
3470 /* need to add the md_redundancy_group */
3471 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3472 printk(KERN_WARNING
3473 "md: cannot register extra attributes for %s\n",
3474 mdname(mddev));
3475 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3476 }
3477 if (oldpers->sync_request != NULL &&
3478 pers->sync_request == NULL) {
3479 /* need to remove the md_redundancy_group */
3480 if (mddev->to_remove == NULL)
3481 mddev->to_remove = &md_redundancy_group;
3482 }
3483
NeilBrowndafb20f2012-03-19 12:46:39 +11003484 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003485 if (rdev->raid_disk < 0)
3486 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003487 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003488 rdev->new_raid_disk = -1;
3489 if (rdev->new_raid_disk == rdev->raid_disk)
3490 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003491 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003492 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003493 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003494 if (rdev->raid_disk < 0)
3495 continue;
3496 if (rdev->new_raid_disk == rdev->raid_disk)
3497 continue;
3498 rdev->raid_disk = rdev->new_raid_disk;
3499 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003500 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003501 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003502 if (sysfs_link_rdev(mddev, rdev))
3503 printk(KERN_WARNING "md: cannot register rd%d"
3504 " for %s after level change\n",
3505 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003506 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003507 }
3508
NeilBrowndb721d32014-12-15 12:56:58 +11003509 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003510 /* this is now an array without redundancy, so
3511 * it must always be in_sync
3512 */
3513 mddev->in_sync = 1;
3514 del_timer_sync(&mddev->safemode_timer);
3515 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003516 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003517 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003518 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003519 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003520 if (!mddev->thread)
3521 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003522 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003523 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003524 rv = len;
3525out_unlock:
3526 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003527 return rv;
3528}
3529
3530static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003531__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003532
NeilBrownd4dbd022006-06-26 00:27:59 -07003533static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003534layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003535{
3536 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003537 if (mddev->reshape_position != MaxSector &&
3538 mddev->layout != mddev->new_layout)
3539 return sprintf(page, "%d (%d)\n",
3540 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003541 return sprintf(page, "%d\n", mddev->layout);
3542}
3543
3544static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003545layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003546{
3547 char *e;
3548 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11003549 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003550
3551 if (!*buf || (*e && *e != '\n'))
3552 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11003553 err = mddev_lock(mddev);
3554 if (err)
3555 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003556
NeilBrownb3546032009-03-31 14:56:41 +11003557 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003558 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003559 err = -EBUSY;
3560 else if (mddev->ro)
3561 err = -EROFS;
3562 else {
3563 mddev->new_layout = n;
3564 err = mddev->pers->check_reshape(mddev);
3565 if (err)
3566 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003567 }
NeilBrownb3546032009-03-31 14:56:41 +11003568 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003569 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003570 if (mddev->reshape_position == MaxSector)
3571 mddev->layout = n;
3572 }
NeilBrown67918752014-12-15 12:57:01 +11003573 mddev_unlock(mddev);
3574 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003575}
3576static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003577__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003578
NeilBrowneae17012005-11-08 21:39:23 -08003579static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003580raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003581{
NeilBrownbb636542005-11-08 21:39:45 -08003582 if (mddev->raid_disks == 0)
3583 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003584 if (mddev->reshape_position != MaxSector &&
3585 mddev->delta_disks != 0)
3586 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3587 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003588 return sprintf(page, "%d\n", mddev->raid_disks);
3589}
3590
NeilBrownfd01b882011-10-11 16:47:53 +11003591static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003592
3593static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003594raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003595{
NeilBrownda943b992006-01-06 00:20:54 -08003596 char *e;
NeilBrown67918752014-12-15 12:57:01 +11003597 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003598 unsigned long n = simple_strtoul(buf, &e, 10);
3599
3600 if (!*buf || (*e && *e != '\n'))
3601 return -EINVAL;
3602
NeilBrown67918752014-12-15 12:57:01 +11003603 err = mddev_lock(mddev);
3604 if (err)
3605 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003606 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003607 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003608 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003609 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003610 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003611
NeilBrown67918752014-12-15 12:57:01 +11003612 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003613 rdev_for_each(rdev, mddev) {
3614 if (olddisks < n &&
3615 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003616 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003617 if (olddisks > n &&
3618 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003619 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003620 }
NeilBrown67918752014-12-15 12:57:01 +11003621 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003622 mddev->delta_disks = n - olddisks;
3623 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003624 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003625 } else
NeilBrownda943b992006-01-06 00:20:54 -08003626 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003627out_unlock:
3628 mddev_unlock(mddev);
3629 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003630}
3631static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003632__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003633
NeilBrown24dd4692005-11-08 21:39:26 -08003634static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003635chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003636{
NeilBrown08a02ec2007-05-09 02:35:38 -07003637 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003638 mddev->chunk_sectors != mddev->new_chunk_sectors)
3639 return sprintf(page, "%d (%d)\n",
3640 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003641 mddev->chunk_sectors << 9);
3642 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003643}
3644
3645static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003646chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003647{
NeilBrown67918752014-12-15 12:57:01 +11003648 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003649 char *e;
3650 unsigned long n = simple_strtoul(buf, &e, 10);
3651
NeilBrown3b343802006-01-06 00:20:47 -08003652 if (!*buf || (*e && *e != '\n'))
3653 return -EINVAL;
3654
NeilBrown67918752014-12-15 12:57:01 +11003655 err = mddev_lock(mddev);
3656 if (err)
3657 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003658 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003659 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003660 err = -EBUSY;
3661 else if (mddev->ro)
3662 err = -EROFS;
3663 else {
3664 mddev->new_chunk_sectors = n >> 9;
3665 err = mddev->pers->check_reshape(mddev);
3666 if (err)
3667 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003668 }
NeilBrownb3546032009-03-31 14:56:41 +11003669 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003670 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003671 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003672 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003673 }
NeilBrown67918752014-12-15 12:57:01 +11003674 mddev_unlock(mddev);
3675 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003676}
3677static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003678__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003679
NeilBrowna94213b2006-06-26 00:28:00 -07003680static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003681resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003682{
NeilBrownd1a7c502009-03-31 15:24:32 +11003683 if (mddev->recovery_cp == MaxSector)
3684 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003685 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3686}
3687
3688static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003689resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003690{
NeilBrown67918752014-12-15 12:57:01 +11003691 int err;
NeilBrowna94213b2006-06-26 00:28:00 -07003692 char *e;
3693 unsigned long long n = simple_strtoull(buf, &e, 10);
3694
NeilBrown67918752014-12-15 12:57:01 +11003695 err = mddev_lock(mddev);
3696 if (err)
3697 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003698 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003699 err = -EBUSY;
3700 else if (cmd_match(buf, "none"))
Dan Williams06e3c812009-12-12 21:17:12 -07003701 n = MaxSector;
3702 else if (!*buf || (*e && *e != '\n'))
NeilBrown67918752014-12-15 12:57:01 +11003703 err = -EINVAL;
NeilBrowna94213b2006-06-26 00:28:00 -07003704
NeilBrown67918752014-12-15 12:57:01 +11003705 if (!err) {
3706 mddev->recovery_cp = n;
3707 if (mddev->pers)
3708 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3709 }
3710 mddev_unlock(mddev);
3711 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003712}
3713static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003714__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3715 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003716
NeilBrown9e653b62006-06-26 00:27:58 -07003717/*
3718 * The array state can be:
3719 *
3720 * clear
3721 * No devices, no size, no level
3722 * Equivalent to STOP_ARRAY ioctl
3723 * inactive
3724 * May have some settings, but array is not active
3725 * all IO results in error
3726 * When written, doesn't tear down array, but just stops it
3727 * suspended (not supported yet)
3728 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003729 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003730 * readonly
3731 * no resync can happen. no superblocks get written.
3732 * write requests fail
3733 * read-auto
3734 * like readonly, but behaves like 'clean' on a write request.
3735 *
3736 * clean - no pending writes, but otherwise active.
3737 * When written to inactive array, starts without resync
3738 * If a write request arrives then
3739 * if metadata is known, mark 'dirty' and switch to 'active'.
3740 * if not known, block and switch to write-pending
3741 * If written to an active array that has pending writes, then fails.
3742 * active
3743 * fully active: IO and resync can be happening.
3744 * When written to inactive array, starts with resync
3745 *
3746 * write-pending
3747 * clean, but writes are blocked waiting for 'active' to be written.
3748 *
3749 * active-idle
3750 * like active, but no writes have been seen for a while (100msec).
3751 *
3752 */
3753enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3754 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003755static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003756 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3757 "write-pending", "active-idle", NULL };
3758
3759static int match_word(const char *word, char **list)
3760{
3761 int n;
3762 for (n=0; list[n]; n++)
3763 if (cmd_match(word, list[n]))
3764 break;
3765 return n;
3766}
3767
3768static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003769array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003770{
3771 enum array_state st = inactive;
3772
3773 if (mddev->pers)
3774 switch(mddev->ro) {
3775 case 1:
3776 st = readonly;
3777 break;
3778 case 2:
3779 st = read_auto;
3780 break;
3781 case 0:
3782 if (mddev->in_sync)
3783 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003784 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003785 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003786 else if (mddev->safemode)
3787 st = active_idle;
3788 else
3789 st = active;
3790 }
3791 else {
3792 if (list_empty(&mddev->disks) &&
3793 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003794 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003795 st = clear;
3796 else
3797 st = inactive;
3798 }
3799 return sprintf(page, "%s\n", array_states[st]);
3800}
3801
NeilBrownf72ffdd2014-09-30 14:23:59 +10003802static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3803static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3804static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003805static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003806
3807static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003808array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003809{
NeilBrown67918752014-12-15 12:57:01 +11003810 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003811 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003812
3813 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3814 /* don't take reconfig_mutex when toggling between
3815 * clean and active
3816 */
3817 spin_lock(&mddev->lock);
3818 if (st == active) {
3819 restart_array(mddev);
3820 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3821 wake_up(&mddev->sb_wait);
3822 err = 0;
3823 } else /* st == clean */ {
3824 restart_array(mddev);
3825 if (atomic_read(&mddev->writes_pending) == 0) {
3826 if (mddev->in_sync == 0) {
3827 mddev->in_sync = 1;
3828 if (mddev->safemode == 1)
3829 mddev->safemode = 0;
3830 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3831 }
3832 err = 0;
3833 } else
3834 err = -EBUSY;
3835 }
3836 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003837 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003838 }
3839 err = mddev_lock(mddev);
3840 if (err)
3841 return err;
3842 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003843 switch(st) {
3844 case bad_word:
3845 break;
3846 case clear:
3847 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003848 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003849 break;
3850 case inactive:
3851 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003852 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003853 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003854 else
NeilBrowne6910632008-02-06 01:39:51 -08003855 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003856 break;
3857 case suspended:
3858 break; /* not supported yet */
3859 case readonly:
3860 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003861 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003862 else {
3863 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003864 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003865 err = do_md_run(mddev);
3866 }
3867 break;
3868 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003869 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003870 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003871 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003872 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003873 err = restart_array(mddev);
3874 if (err == 0) {
3875 mddev->ro = 2;
3876 set_disk_ro(mddev->gendisk, 0);
3877 }
NeilBrown9e653b62006-06-26 00:27:58 -07003878 } else {
3879 mddev->ro = 2;
3880 err = do_md_run(mddev);
3881 }
3882 break;
3883 case clean:
3884 if (mddev->pers) {
3885 restart_array(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11003886 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003887 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003888 if (mddev->in_sync == 0) {
3889 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003890 if (mddev->safemode == 1)
3891 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003892 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003893 }
3894 err = 0;
3895 } else
3896 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11003897 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003898 } else
3899 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003900 break;
3901 case active:
3902 if (mddev->pers) {
3903 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003904 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003905 wake_up(&mddev->sb_wait);
3906 err = 0;
3907 } else {
3908 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003909 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003910 err = do_md_run(mddev);
3911 }
3912 break;
3913 case write_pending:
3914 case active_idle:
3915 /* these cannot be set */
3916 break;
3917 }
NeilBrown67918752014-12-15 12:57:01 +11003918
3919 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11003920 if (mddev->hold_active == UNTIL_IOCTL)
3921 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003922 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10003923 }
NeilBrown67918752014-12-15 12:57:01 +11003924 mddev_unlock(mddev);
3925 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07003926}
NeilBrown80ca3a42006-07-10 04:44:18 -07003927static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10003928__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003929
NeilBrown6d7ff7382006-01-06 00:21:16 -08003930static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003931max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003932 return sprintf(page, "%d\n",
3933 atomic_read(&mddev->max_corr_read_errors));
3934}
3935
3936static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003937max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003938{
3939 char *e;
3940 unsigned long n = simple_strtoul(buf, &e, 10);
3941
3942 if (*buf && (*e == 0 || *e == '\n')) {
3943 atomic_set(&mddev->max_corr_read_errors, n);
3944 return len;
3945 }
3946 return -EINVAL;
3947}
3948
3949static struct md_sysfs_entry max_corr_read_errors =
3950__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3951 max_corrected_read_errors_store);
3952
3953static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003954null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003955{
3956 return -EINVAL;
3957}
3958
3959static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003960new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003961{
3962 /* buf must be %d:%d\n? giving major and minor numbers */
3963 /* The new device is added to the array.
3964 * If the array has a persistent superblock, we read the
3965 * superblock to initialise info and check validity.
3966 * Otherwise, only checking done is that in bind_rdev_to_array,
3967 * which mainly checks size.
3968 */
3969 char *e;
3970 int major = simple_strtoul(buf, &e, 10);
3971 int minor;
3972 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003973 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003974 int err;
3975
3976 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3977 return -EINVAL;
3978 minor = simple_strtoul(e+1, &e, 10);
3979 if (*e && *e != '\n')
3980 return -EINVAL;
3981 dev = MKDEV(major, minor);
3982 if (major != MAJOR(dev) ||
3983 minor != MINOR(dev))
3984 return -EOVERFLOW;
3985
NeilBrown67918752014-12-15 12:57:01 +11003986 flush_workqueue(md_misc_wq);
3987
3988 err = mddev_lock(mddev);
3989 if (err)
3990 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003991 if (mddev->persistent) {
3992 rdev = md_import_device(dev, mddev->major_version,
3993 mddev->minor_version);
3994 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003995 struct md_rdev *rdev0
3996 = list_entry(mddev->disks.next,
3997 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08003998 err = super_types[mddev->major_version]
3999 .load_super(rdev, rdev0, mddev->minor_version);
4000 if (err < 0)
4001 goto out;
4002 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004003 } else if (mddev->external)
4004 rdev = md_import_device(dev, -2, -1);
4005 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004006 rdev = md_import_device(dev, -1, -1);
4007
4008 if (IS_ERR(rdev))
4009 return PTR_ERR(rdev);
4010 err = bind_rdev_to_array(rdev, mddev);
4011 out:
4012 if (err)
4013 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004014 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004015 return err ? err : len;
4016}
4017
4018static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004019__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004020
4021static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004022bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004023{
4024 char *end;
4025 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004026 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004027
NeilBrown67918752014-12-15 12:57:01 +11004028 err = mddev_lock(mddev);
4029 if (err)
4030 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004031 if (!mddev->bitmap)
4032 goto out;
4033 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4034 while (*buf) {
4035 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4036 if (buf == end) break;
4037 if (*end == '-') { /* range */
4038 buf = end + 1;
4039 end_chunk = simple_strtoul(buf, &end, 0);
4040 if (buf == end) break;
4041 }
4042 if (*end && !isspace(*end)) break;
4043 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004044 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004045 }
4046 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4047out:
NeilBrown67918752014-12-15 12:57:01 +11004048 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004049 return len;
4050}
4051
4052static struct md_sysfs_entry md_bitmap =
4053__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4054
4055static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004056size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004057{
Andre Noll58c0fed2009-03-31 14:33:13 +11004058 return sprintf(page, "%llu\n",
4059 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004060}
4061
NeilBrownfd01b882011-10-11 16:47:53 +11004062static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004063
4064static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004065size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004066{
4067 /* If array is inactive, we can reduce the component size, but
4068 * not increase it (except from 0).
4069 * If array is active, we can try an on-line resize
4070 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004071 sector_t sectors;
4072 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004073
Andre Noll58c0fed2009-03-31 14:33:13 +11004074 if (err < 0)
4075 return err;
NeilBrown67918752014-12-15 12:57:01 +11004076 err = mddev_lock(mddev);
4077 if (err)
4078 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004079 if (mddev->pers) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05004080 if (mddev_is_clustered(mddev))
4081 md_cluster_ops->metadata_update_start(mddev);
Andre Noll58c0fed2009-03-31 14:33:13 +11004082 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004083 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05004084 if (mddev_is_clustered(mddev))
4085 md_cluster_ops->metadata_update_finish(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004086 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004087 if (mddev->dev_sectors == 0 ||
4088 mddev->dev_sectors > sectors)
4089 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004090 else
4091 err = -ENOSPC;
4092 }
NeilBrown67918752014-12-15 12:57:01 +11004093 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004094 return err ? err : len;
4095}
4096
4097static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004098__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004099
Masanari Iida83f0d772012-10-30 00:18:08 +09004100/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004101 * This is one of
4102 * 'none' for arrays with no metadata (good luck...)
4103 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004104 * or N.M for internally known formats
4105 */
4106static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004107metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004108{
4109 if (mddev->persistent)
4110 return sprintf(page, "%d.%d\n",
4111 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004112 else if (mddev->external)
4113 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004114 else
4115 return sprintf(page, "none\n");
4116}
4117
4118static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004119metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004120{
4121 int major, minor;
4122 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004123 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004124 /* Changing the details of 'external' metadata is
4125 * always permitted. Otherwise there must be
4126 * no devices attached to the array.
4127 */
NeilBrown67918752014-12-15 12:57:01 +11004128
4129 err = mddev_lock(mddev);
4130 if (err)
4131 return err;
4132 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004133 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4134 ;
4135 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004136 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004137
NeilBrown67918752014-12-15 12:57:01 +11004138 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004139 if (cmd_match(buf, "none")) {
4140 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004141 mddev->external = 0;
4142 mddev->major_version = 0;
4143 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004144 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004145 }
4146 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004147 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004148 if (namelen >= sizeof(mddev->metadata_type))
4149 namelen = sizeof(mddev->metadata_type)-1;
4150 strncpy(mddev->metadata_type, buf+9, namelen);
4151 mddev->metadata_type[namelen] = 0;
4152 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4153 mddev->metadata_type[--namelen] = 0;
4154 mddev->persistent = 0;
4155 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004156 mddev->major_version = 0;
4157 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004158 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004159 }
4160 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004161 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004162 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004163 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004164 buf = e+1;
4165 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004166 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004167 goto out_unlock;
4168 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004169 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004170 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004171 mddev->major_version = major;
4172 mddev->minor_version = minor;
4173 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004174 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004175 err = 0;
4176out_unlock:
4177 mddev_unlock(mddev);
4178 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004179}
4180
4181static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004182__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004183
NeilBrowna35b0d62006-01-06 00:20:49 -08004184static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004185action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004186{
NeilBrown7eec3142005-11-08 21:39:44 -08004187 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004188 unsigned long recovery = mddev->recovery;
4189 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004190 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004191 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4192 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4193 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004194 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004195 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4196 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004197 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004198 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004199 type = "check";
4200 else
4201 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004202 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004203 type = "recover";
4204 }
4205 return sprintf(page, "%s\n", type);
4206}
4207
4208static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004209action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004210{
NeilBrown7eec3142005-11-08 21:39:44 -08004211 if (!mddev->pers || !mddev->pers->sync_request)
4212 return -EINVAL;
4213
NeilBrownb6a9ce62009-05-26 09:41:17 +10004214
4215 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004216 if (cmd_match(page, "frozen"))
4217 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4218 else
4219 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004220 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4221 mddev_lock(mddev) == 0) {
4222 flush_workqueue(md_misc_wq);
4223 if (mddev->sync_thread) {
4224 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004225 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004226 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004227 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004228 }
NeilBrown03c902e2006-01-06 00:20:46 -08004229 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4230 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004231 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004232 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004233 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004234 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004235 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004236 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004237 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004238 int err;
4239 if (mddev->pers->start_reshape == NULL)
4240 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004241 err = mddev_lock(mddev);
4242 if (!err) {
NeilBrown56ccc112015-05-28 17:53:29 +10004243 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004244 err = mddev->pers->start_reshape(mddev);
4245 mddev_unlock(mddev);
4246 }
NeilBrown16484bf2006-03-27 01:18:13 -08004247 if (err)
4248 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004249 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004250 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004251 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004252 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004253 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004254 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004255 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004256 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4257 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004258 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004259 if (mddev->ro == 2) {
4260 /* A write to sync_action is enough to justify
4261 * canceling read-auto mode
4262 */
4263 mddev->ro = 0;
4264 md_wakeup_thread(mddev->sync_thread);
4265 }
NeilBrown03c902e2006-01-06 00:20:46 -08004266 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004267 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004268 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004269 return len;
4270}
4271
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004272static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004273__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004274
4275static ssize_t
4276last_sync_action_show(struct mddev *mddev, char *page)
4277{
4278 return sprintf(page, "%s\n", mddev->last_sync_action);
4279}
4280
4281static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4282
NeilBrown9d888832005-11-08 21:39:26 -08004283static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004284mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004285{
4286 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004287 (unsigned long long)
4288 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004289}
4290
NeilBrown80ca3a42006-07-10 04:44:18 -07004291static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004292
NeilBrown88202a02006-01-06 00:21:36 -08004293static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004294sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004295{
4296 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4297 mddev->sync_speed_min ? "local": "system");
4298}
4299
4300static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004301sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004302{
4303 int min;
4304 char *e;
4305 if (strncmp(buf, "system", 6)==0) {
4306 mddev->sync_speed_min = 0;
4307 return len;
4308 }
4309 min = simple_strtoul(buf, &e, 10);
4310 if (buf == e || (*e && *e != '\n') || min <= 0)
4311 return -EINVAL;
4312 mddev->sync_speed_min = min;
4313 return len;
4314}
4315
4316static struct md_sysfs_entry md_sync_min =
4317__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4318
4319static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004320sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004321{
4322 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4323 mddev->sync_speed_max ? "local": "system");
4324}
4325
4326static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004327sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004328{
4329 int max;
4330 char *e;
4331 if (strncmp(buf, "system", 6)==0) {
4332 mddev->sync_speed_max = 0;
4333 return len;
4334 }
4335 max = simple_strtoul(buf, &e, 10);
4336 if (buf == e || (*e && *e != '\n') || max <= 0)
4337 return -EINVAL;
4338 mddev->sync_speed_max = max;
4339 return len;
4340}
4341
4342static struct md_sysfs_entry md_sync_max =
4343__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4344
Iustin Popd7f3d292007-10-16 23:30:54 -07004345static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004346degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004347{
4348 return sprintf(page, "%d\n", mddev->degraded);
4349}
4350static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004351
4352static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004353sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004354{
4355 return sprintf(page, "%d\n", mddev->parallel_resync);
4356}
4357
4358static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004359sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004360{
4361 long n;
4362
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004363 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004364 return -EINVAL;
4365
4366 if (n != 0 && n != 1)
4367 return -EINVAL;
4368
4369 mddev->parallel_resync = n;
4370
4371 if (mddev->sync_thread)
4372 wake_up(&resync_wait);
4373
4374 return len;
4375}
4376
4377/* force parallel resync, even with shared block devices */
4378static struct md_sysfs_entry md_sync_force_parallel =
4379__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4380 sync_force_parallel_show, sync_force_parallel_store);
4381
4382static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004383sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004384{
4385 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004386 if (mddev->curr_resync == 0)
4387 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004388 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4389 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004390 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004391 db = resync - mddev->resync_mark_cnt;
4392 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004393}
4394
NeilBrown80ca3a42006-07-10 04:44:18 -07004395static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004396
4397static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004398sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004399{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004400 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004401
NeilBrownacb180b2009-04-14 16:28:34 +10004402 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4403 return sprintf(page, "none\n");
4404
NeilBrown72f36d52012-10-11 14:25:57 +11004405 if (mddev->curr_resync == 1 ||
4406 mddev->curr_resync == 2)
4407 return sprintf(page, "delayed\n");
4408
NeilBrownc804cde2012-05-21 09:28:33 +10004409 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4410 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004411 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004412 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004413 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004414
NeilBrownacb180b2009-04-14 16:28:34 +10004415 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004416 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004417}
4418
NeilBrown750f1992014-09-30 08:53:05 +10004419static struct md_sysfs_entry md_sync_completed =
4420 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004421
NeilBrowne464eaf2006-03-27 01:18:14 -08004422static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004423min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004424{
4425 return sprintf(page, "%llu\n",
4426 (unsigned long long)mddev->resync_min);
4427}
4428static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004429min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004430{
4431 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004432 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004433
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004434 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004435 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004436
4437 spin_lock(&mddev->lock);
4438 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004439 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004440 goto out_unlock;
4441
4442 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004443 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004444 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004445
NeilBrown50c37b12015-03-23 17:36:38 +11004446 /* Round down to multiple of 4K for safety */
4447 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004448 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004449
NeilBrown23da4222014-12-15 12:57:01 +11004450out_unlock:
4451 spin_unlock(&mddev->lock);
4452 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004453}
4454
4455static struct md_sysfs_entry md_min_sync =
4456__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4457
4458static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004459max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004460{
4461 if (mddev->resync_max == MaxSector)
4462 return sprintf(page, "max\n");
4463 else
4464 return sprintf(page, "%llu\n",
4465 (unsigned long long)mddev->resync_max);
4466}
4467static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004468max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004469{
NeilBrown23da4222014-12-15 12:57:01 +11004470 int err;
4471 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004472 if (strncmp(buf, "max", 3) == 0)
4473 mddev->resync_max = MaxSector;
4474 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004475 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004476 int chunk;
4477
4478 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004479 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004480 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004481 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004482 goto out_unlock;
4483
4484 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004485 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004486 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004487 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004488 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004489
4490 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004491 chunk = mddev->chunk_sectors;
4492 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004493 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004494
4495 err = -EINVAL;
4496 if (sector_div(temp, chunk))
4497 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004498 }
4499 mddev->resync_max = max;
4500 }
4501 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004502 err = 0;
4503out_unlock:
4504 spin_unlock(&mddev->lock);
4505 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004506}
4507
4508static struct md_sysfs_entry md_max_sync =
4509__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4510
4511static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004512suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004513{
4514 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4515}
4516
4517static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004518suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004519{
4520 char *e;
4521 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004522 unsigned long long old;
4523 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004524
NeilBrowne464eaf2006-03-27 01:18:14 -08004525 if (buf == e || (*e && *e != '\n'))
4526 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004527
NeilBrown67918752014-12-15 12:57:01 +11004528 err = mddev_lock(mddev);
4529 if (err)
4530 return err;
4531 err = -EINVAL;
4532 if (mddev->pers == NULL ||
4533 mddev->pers->quiesce == NULL)
4534 goto unlock;
4535 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004536 mddev->suspend_lo = new;
4537 if (new >= old)
4538 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004539 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004540 else {
4541 /* Expanding suspended region - need to wait */
4542 mddev->pers->quiesce(mddev, 1);
4543 mddev->pers->quiesce(mddev, 0);
4544 }
NeilBrown67918752014-12-15 12:57:01 +11004545 err = 0;
4546unlock:
4547 mddev_unlock(mddev);
4548 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004549}
4550static struct md_sysfs_entry md_suspend_lo =
4551__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4552
NeilBrowne464eaf2006-03-27 01:18:14 -08004553static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004554suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004555{
4556 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4557}
4558
4559static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004560suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004561{
4562 char *e;
4563 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004564 unsigned long long old;
4565 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004566
NeilBrowne464eaf2006-03-27 01:18:14 -08004567 if (buf == e || (*e && *e != '\n'))
4568 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004569
NeilBrown67918752014-12-15 12:57:01 +11004570 err = mddev_lock(mddev);
4571 if (err)
4572 return err;
4573 err = -EINVAL;
4574 if (mddev->pers == NULL ||
4575 mddev->pers->quiesce == NULL)
4576 goto unlock;
4577 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004578 mddev->suspend_hi = new;
4579 if (new <= old)
4580 /* Shrinking suspended region */
4581 mddev->pers->quiesce(mddev, 2);
4582 else {
4583 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004584 mddev->pers->quiesce(mddev, 1);
4585 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004586 }
NeilBrown67918752014-12-15 12:57:01 +11004587 err = 0;
4588unlock:
4589 mddev_unlock(mddev);
4590 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004591}
4592static struct md_sysfs_entry md_suspend_hi =
4593__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4594
NeilBrown08a02ec2007-05-09 02:35:38 -07004595static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004596reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004597{
4598 if (mddev->reshape_position != MaxSector)
4599 return sprintf(page, "%llu\n",
4600 (unsigned long long)mddev->reshape_position);
4601 strcpy(page, "none\n");
4602 return 5;
4603}
4604
4605static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004606reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004607{
NeilBrownc6563a82012-05-21 09:27:00 +10004608 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004609 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004610 int err;
NeilBrown08a02ec2007-05-09 02:35:38 -07004611 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004612
NeilBrown08a02ec2007-05-09 02:35:38 -07004613 if (buf == e || (*e && *e != '\n'))
4614 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004615 err = mddev_lock(mddev);
4616 if (err)
4617 return err;
4618 err = -EBUSY;
4619 if (mddev->pers)
4620 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004621 mddev->reshape_position = new;
4622 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004623 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004624 mddev->new_level = mddev->level;
4625 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004626 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004627 rdev_for_each(rdev, mddev)
4628 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004629 err = 0;
4630unlock:
4631 mddev_unlock(mddev);
4632 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004633}
4634
4635static struct md_sysfs_entry md_reshape_position =
4636__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4637 reshape_position_store);
4638
Dan Williamsb522adc2009-03-31 15:00:31 +11004639static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004640reshape_direction_show(struct mddev *mddev, char *page)
4641{
4642 return sprintf(page, "%s\n",
4643 mddev->reshape_backwards ? "backwards" : "forwards");
4644}
4645
4646static ssize_t
4647reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4648{
4649 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004650 int err;
4651
NeilBrown2c810cd2012-05-21 09:27:00 +10004652 if (cmd_match(buf, "forwards"))
4653 backwards = 0;
4654 else if (cmd_match(buf, "backwards"))
4655 backwards = 1;
4656 else
4657 return -EINVAL;
4658 if (mddev->reshape_backwards == backwards)
4659 return len;
4660
NeilBrown67918752014-12-15 12:57:01 +11004661 err = mddev_lock(mddev);
4662 if (err)
4663 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004664 /* check if we are allowed to change */
4665 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004666 err = -EBUSY;
4667 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004668 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004669 err = -EINVAL;
4670 else
4671 mddev->reshape_backwards = backwards;
4672 mddev_unlock(mddev);
4673 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004674}
4675
4676static struct md_sysfs_entry md_reshape_direction =
4677__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4678 reshape_direction_store);
4679
4680static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004681array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004682{
4683 if (mddev->external_size)
4684 return sprintf(page, "%llu\n",
4685 (unsigned long long)mddev->array_sectors/2);
4686 else
4687 return sprintf(page, "default\n");
4688}
4689
4690static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004691array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004692{
4693 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004694 int err;
4695
4696 err = mddev_lock(mddev);
4697 if (err)
4698 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004699
4700 if (strncmp(buf, "default", 7) == 0) {
4701 if (mddev->pers)
4702 sectors = mddev->pers->size(mddev, 0, 0);
4703 else
4704 sectors = mddev->array_sectors;
4705
4706 mddev->external_size = 0;
4707 } else {
4708 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004709 err = -EINVAL;
4710 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4711 err = -E2BIG;
4712 else
4713 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004714 }
4715
NeilBrown67918752014-12-15 12:57:01 +11004716 if (!err) {
4717 mddev->array_sectors = sectors;
4718 if (mddev->pers) {
4719 set_capacity(mddev->gendisk, mddev->array_sectors);
4720 revalidate_disk(mddev->gendisk);
4721 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004722 }
NeilBrown67918752014-12-15 12:57:01 +11004723 mddev_unlock(mddev);
4724 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004725}
4726
4727static struct md_sysfs_entry md_array_size =
4728__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4729 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004730
NeilBrowneae17012005-11-08 21:39:23 -08004731static struct attribute *md_default_attrs[] = {
4732 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004733 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004734 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004735 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004736 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004737 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004738 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004739 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004740 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004741 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004742 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004743 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004744 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004745 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004746 NULL,
4747};
4748
4749static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004750 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004751 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004752 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004753 &md_sync_min.attr,
4754 &md_sync_max.attr,
4755 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004756 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004757 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004758 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004759 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004760 &md_suspend_lo.attr,
4761 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004762 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004763 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004764 NULL,
4765};
NeilBrown411036f2005-11-08 21:39:40 -08004766static struct attribute_group md_redundancy_group = {
4767 .name = NULL,
4768 .attrs = md_redundancy_attrs,
4769};
4770
NeilBrowneae17012005-11-08 21:39:23 -08004771static ssize_t
4772md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4773{
4774 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004775 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004776 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004777
4778 if (!entry->show)
4779 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004780 spin_lock(&all_mddevs_lock);
4781 if (list_empty(&mddev->all_mddevs)) {
4782 spin_unlock(&all_mddevs_lock);
4783 return -EBUSY;
4784 }
4785 mddev_get(mddev);
4786 spin_unlock(&all_mddevs_lock);
4787
NeilBrownb7b17c92014-12-15 12:56:59 +11004788 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004789 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004790 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004791}
4792
4793static ssize_t
4794md_attr_store(struct kobject *kobj, struct attribute *attr,
4795 const char *page, size_t length)
4796{
4797 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004798 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004799 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004800
4801 if (!entry->store)
4802 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004803 if (!capable(CAP_SYS_ADMIN))
4804 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004805 spin_lock(&all_mddevs_lock);
4806 if (list_empty(&mddev->all_mddevs)) {
4807 spin_unlock(&all_mddevs_lock);
4808 return -EBUSY;
4809 }
4810 mddev_get(mddev);
4811 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004812 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004813 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004814 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004815}
4816
4817static void md_free(struct kobject *ko)
4818{
NeilBrownfd01b882011-10-11 16:47:53 +11004819 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004820
4821 if (mddev->sysfs_state)
4822 sysfs_put(mddev->sysfs_state);
4823
NeilBrown6cd18e72015-04-27 14:12:22 +10004824 if (mddev->queue)
4825 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004826 if (mddev->gendisk) {
4827 del_gendisk(mddev->gendisk);
4828 put_disk(mddev->gendisk);
4829 }
NeilBrowna21d1502009-01-09 08:31:09 +11004830
NeilBrowneae17012005-11-08 21:39:23 -08004831 kfree(mddev);
4832}
4833
Emese Revfy52cf25d2010-01-19 02:58:23 +01004834static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004835 .show = md_attr_show,
4836 .store = md_attr_store,
4837};
4838static struct kobj_type md_ktype = {
4839 .release = md_free,
4840 .sysfs_ops = &md_sysfs_ops,
4841 .default_attrs = md_default_attrs,
4842};
4843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844int mdp_major = 0;
4845
Dan Williams5fd3a172009-03-04 00:57:25 -07004846static void mddev_delayed_delete(struct work_struct *ws)
4847{
NeilBrownfd01b882011-10-11 16:47:53 +11004848 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004849
NeilBrown43a70502009-12-14 12:49:55 +11004850 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004851 kobject_del(&mddev->kobj);
4852 kobject_put(&mddev->kobj);
4853}
4854
NeilBrownefeb53c2009-01-09 08:31:10 +11004855static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004857 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004858 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004860 int partitioned;
4861 int shift;
4862 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004863 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864
4865 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004866 return -ENODEV;
4867
4868 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4869 shift = partitioned ? MdpMinorShift : 0;
4870 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
Tejun Heoe804ac72010-10-15 15:36:08 +02004872 /* wait for any previous instance of this device to be
4873 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004874 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004875 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004876
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004877 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004878 error = -EEXIST;
4879 if (mddev->gendisk)
4880 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004881
4882 if (name) {
4883 /* Need to ensure that 'name' is not a duplicate.
4884 */
NeilBrownfd01b882011-10-11 16:47:53 +11004885 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004886 spin_lock(&all_mddevs_lock);
4887
4888 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4889 if (mddev2->gendisk &&
4890 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4891 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004892 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004893 }
4894 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 }
NeilBrown8b765392009-01-09 08:31:08 +11004896
NeilBrown0909dc42009-07-01 12:27:21 +10004897 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004898 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004899 if (!mddev->queue)
4900 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004901 mddev->queue->queuedata = mddev;
4902
NeilBrown409c57f2009-03-31 14:39:39 +11004903 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004904 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004905
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 disk = alloc_disk(1 << shift);
4907 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004908 blk_cleanup_queue(mddev->queue);
4909 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004910 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004912 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004914 if (name)
4915 strcpy(disk->disk_name, name);
4916 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004918 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 disk->fops = &md_fops;
4921 disk->private_data = mddev;
4922 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004923 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004924 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004925 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004926 * remove it now.
4927 */
4928 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004930 /* As soon as we call add_disk(), another thread could get
4931 * through to md_open, so make sure it doesn't get too far
4932 */
4933 mutex_lock(&mddev->open_mutex);
4934 add_disk(disk);
4935
Tejun Heoed9e1982008-08-25 19:56:05 +09004936 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4937 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004938 if (error) {
4939 /* This isn't possible, but as kobject_init_and_add is marked
4940 * __must_check, we must do something with the result
4941 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004942 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4943 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004944 error = 0;
4945 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004946 if (mddev->kobj.sd &&
4947 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004948 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004949 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004950 abort:
4951 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004952 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004953 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004954 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004955 }
NeilBrownd3374822009-01-09 08:31:10 +11004956 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004957 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004958}
4959
4960static struct kobject *md_probe(dev_t dev, int *part, void *data)
4961{
4962 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 return NULL;
4964}
4965
NeilBrownefeb53c2009-01-09 08:31:10 +11004966static int add_named_array(const char *val, struct kernel_param *kp)
4967{
4968 /* val must be "md_*" where * is not all digits.
4969 * We allocate an array with a large free minor number, and
4970 * set the name to val. val must not already be an active name.
4971 */
4972 int len = strlen(val);
4973 char buf[DISK_NAME_LEN];
4974
4975 while (len && val[len-1] == '\n')
4976 len--;
4977 if (len >= DISK_NAME_LEN)
4978 return -E2BIG;
4979 strlcpy(buf, val, len+1);
4980 if (strncmp(buf, "md_", 3) != 0)
4981 return -EINVAL;
4982 return md_alloc(0, buf);
4983}
4984
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985static void md_safemode_timeout(unsigned long data)
4986{
NeilBrownfd01b882011-10-11 16:47:53 +11004987 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988
Neil Brown0fd62b82008-06-28 08:31:36 +10004989 if (!atomic_read(&mddev->writes_pending)) {
4990 mddev->safemode = 1;
4991 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004992 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 md_wakeup_thread(mddev->thread);
4995}
4996
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004997static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
NeilBrownfd01b882011-10-11 16:47:53 +11004999int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000{
NeilBrown2604b702006-01-06 00:20:36 -08005001 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005002 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005003 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
NeilBrowna757e642005-04-16 15:26:42 -07005005 if (list_empty(&mddev->disks))
5006 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
5009 if (mddev->pers)
5010 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005011 /* Cannot run until previous stop completes properly */
5012 if (mddev->sysfs_active)
5013 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005014
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 /*
5016 * Analyze all RAID superblock(s)
5017 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005018 if (!mddev->raid_disks) {
5019 if (!mddev->persistent)
5020 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005021 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
NeilBrownd9d166c2006-01-06 00:20:51 -08005024 if (mddev->level != LEVEL_NONE)
5025 request_module("md-level-%d", mddev->level);
5026 else if (mddev->clevel[0])
5027 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
5029 /*
5030 * Drop all container device buffers, from now on
5031 * the only valid external interface is through the md
5032 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005034 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005035 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 continue;
5037 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005038 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005039
5040 /* perform some consistency tests on the device.
5041 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005042 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005043 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005044 if (rdev->meta_bdev) {
5045 /* Nothing to check */;
5046 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005047 if (mddev->dev_sectors &&
5048 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005049 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005050 printk("md: %s: data overlaps metadata\n",
5051 mdname(mddev));
5052 return -EINVAL;
5053 }
5054 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005055 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005056 > rdev->data_offset) {
5057 printk("md: %s: metadata overlaps data\n",
5058 mdname(mddev));
5059 return -EINVAL;
5060 }
5061 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005062 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 }
5064
NeilBrowna167f662010-10-26 18:31:13 +11005065 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005066 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005067
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005069 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005070 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005072 if (mddev->level != LEVEL_NONE)
5073 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5074 mddev->level);
5075 else
5076 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5077 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 return -EINVAL;
5079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005081 if (mddev->level != pers->level) {
5082 mddev->level = pers->level;
5083 mddev->new_level = pers->level;
5084 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005085 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086
NeilBrownf6705572006-03-27 01:18:11 -08005087 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005088 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005089 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005090 module_put(pers->owner);
5091 return -EINVAL;
5092 }
5093
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005094 if (pers->sync_request) {
5095 /* Warn if this is a potentially silly
5096 * configuration.
5097 */
5098 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005099 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005100 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005101
NeilBrowndafb20f2012-03-19 12:46:39 +11005102 rdev_for_each(rdev, mddev)
5103 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005104 if (rdev < rdev2 &&
5105 rdev->bdev->bd_contains ==
5106 rdev2->bdev->bd_contains) {
5107 printk(KERN_WARNING
5108 "%s: WARNING: %s appears to be"
5109 " on the same physical disk as"
5110 " %s.\n",
5111 mdname(mddev),
5112 bdevname(rdev->bdev,b),
5113 bdevname(rdev2->bdev,b2));
5114 warned = 1;
5115 }
5116 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005117
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005118 if (warned)
5119 printk(KERN_WARNING
5120 "True protection against single-disk"
5121 " failure might be compromised.\n");
5122 }
5123
NeilBrown657390d2005-08-26 18:34:16 -07005124 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005125 /* may be over-ridden by personality */
5126 mddev->resync_max_sectors = mddev->dev_sectors;
5127
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005128 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129
NeilBrown0f9552b52009-12-30 12:08:50 +11005130 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005131 mddev->ro = 2; /* read-only, but switch on first write */
5132
NeilBrown36d091f2014-12-15 12:56:58 +11005133 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005134 if (err)
5135 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005136 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005137 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5138 " but 'external_size' not in effect?\n", __func__);
5139 printk(KERN_ERR
5140 "md: invalid array_size %llu > default size %llu\n",
5141 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005142 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005143 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005144 }
NeilBrown36d091f2014-12-15 12:56:58 +11005145 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005146 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005147 struct bitmap *bitmap;
5148
5149 bitmap = bitmap_create(mddev, -1);
5150 if (IS_ERR(bitmap)) {
5151 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005152 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5153 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005154 } else
5155 mddev->bitmap = bitmap;
5156
NeilBrownb15c2e52006-01-06 00:20:16 -08005157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005159 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005160 if (mddev->private)
5161 pers->free(mddev, mddev->private);
NeilBrown36d091f2014-12-15 12:56:58 +11005162 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005163 bitmap_destroy(mddev);
5164 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 }
NeilBrown5c675f82014-12-15 12:56:56 +11005166 if (mddev->queue) {
5167 mddev->queue->backing_dev_info.congested_data = mddev;
5168 mddev->queue->backing_dev_info.congested_fn = md_congested;
NeilBrown64590f42014-12-15 12:56:57 +11005169 blk_queue_merge_bvec(mddev->queue, md_mergeable_bvec);
NeilBrown5c675f82014-12-15 12:56:56 +11005170 }
NeilBrown36d091f2014-12-15 12:56:58 +11005171 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005172 if (mddev->kobj.sd &&
5173 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005174 printk(KERN_WARNING
5175 "md: cannot register extra attributes for %s\n",
5176 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005177 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005178 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005179 mddev->ro = 0;
5180
NeilBrownf72ffdd2014-09-30 14:23:59 +10005181 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005182 atomic_set(&mddev->max_corr_read_errors,
5183 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 mddev->safemode = 0;
5185 mddev->safemode_timer.function = md_safemode_timeout;
5186 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005187 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005189 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005190 spin_lock(&mddev->lock);
5191 mddev->pers = pers;
NeilBrown0ca69882011-01-14 09:14:33 +11005192 mddev->ready = 1;
NeilBrown36d091f2014-12-15 12:56:58 +11005193 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005194 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005195 if (rdev->raid_disk >= 0)
5196 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005197 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005198
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005200
NeilBrown7a0a5352013-08-27 16:28:23 +10005201 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07005202 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
NeilBrownd7603b72006-01-06 00:20:30 -08005204 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005205 sysfs_notify_dirent_safe(mddev->sysfs_state);
5206 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005207 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 return 0;
5209}
NeilBrown390ee602010-06-01 19:37:27 +10005210EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211
NeilBrownfd01b882011-10-11 16:47:53 +11005212static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005213{
5214 int err;
5215
5216 err = md_run(mddev);
5217 if (err)
5218 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005219 err = bitmap_load(mddev);
5220 if (err) {
5221 bitmap_destroy(mddev);
5222 goto out;
5223 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005224
5225 md_wakeup_thread(mddev->thread);
5226 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5227
NeilBrownfe60b012010-03-29 11:10:42 +11005228 set_capacity(mddev->gendisk, mddev->array_sectors);
5229 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005230 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005231 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5232out:
5233 return err;
5234}
5235
NeilBrownfd01b882011-10-11 16:47:53 +11005236static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237{
5238 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239
Andre Noll80fab1d2008-07-11 22:02:21 +10005240 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005242 return -ENXIO;
5243 if (!mddev->pers)
5244 return -EINVAL;
5245 if (!mddev->ro)
5246 return -EBUSY;
5247 mddev->safemode = 0;
5248 mddev->ro = 0;
5249 set_disk_ro(disk, 0);
5250 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5251 mdname(mddev));
5252 /* Kick recovery or resync if necessary */
5253 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5254 md_wakeup_thread(mddev->thread);
5255 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005256 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258}
5259
NeilBrownfd01b882011-10-11 16:47:53 +11005260static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005261{
5262 mddev->array_sectors = 0;
5263 mddev->external_size = 0;
5264 mddev->dev_sectors = 0;
5265 mddev->raid_disks = 0;
5266 mddev->recovery_cp = 0;
5267 mddev->resync_min = 0;
5268 mddev->resync_max = MaxSector;
5269 mddev->reshape_position = MaxSector;
5270 mddev->external = 0;
5271 mddev->persistent = 0;
5272 mddev->level = LEVEL_NONE;
5273 mddev->clevel[0] = 0;
5274 mddev->flags = 0;
5275 mddev->ro = 0;
5276 mddev->metadata_type[0] = 0;
5277 mddev->chunk_sectors = 0;
5278 mddev->ctime = mddev->utime = 0;
5279 mddev->layout = 0;
5280 mddev->max_disks = 0;
5281 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005282 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005283 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005284 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005285 mddev->new_level = LEVEL_NONE;
5286 mddev->new_layout = 0;
5287 mddev->new_chunk_sectors = 0;
5288 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005289 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005290 mddev->suspend_lo = mddev->suspend_hi = 0;
5291 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5292 mddev->recovery = 0;
5293 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005294 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005295 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005296 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005297 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005298 mddev->bitmap_info.offset = 0;
5299 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005300 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005301 mddev->bitmap_info.chunksize = 0;
5302 mddev->bitmap_info.daemon_sleep = 0;
5303 mddev->bitmap_info.max_write_behind = 0;
5304}
5305
NeilBrownfd01b882011-10-11 16:47:53 +11005306static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005307{
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005308 if (mddev_is_clustered(mddev))
5309 md_cluster_ops->metadata_update_start(mddev);
NeilBrown6b6204e2013-05-09 09:48:30 +10005310 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005311 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005312 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005313 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005314 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005315 }
5316
5317 del_timer_sync(&mddev->safemode_timer);
5318
5319 bitmap_flush(mddev);
5320 md_super_wait(mddev);
5321
NeilBrownb6d428c2013-04-24 11:42:42 +10005322 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005323 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005324 /* mark array as shutdown cleanly */
5325 mddev->in_sync = 1;
5326 md_update_sb(mddev, 1);
5327 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005328 if (mddev_is_clustered(mddev))
5329 md_cluster_ops->metadata_update_finish(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005330}
NeilBrowndefad612011-01-14 09:14:33 +11005331
NeilBrownfd01b882011-10-11 16:47:53 +11005332void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005333{
NeilBrown29f097c2013-11-14 17:54:51 +11005334 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005335 __md_stop_writes(mddev);
5336 mddev_unlock(mddev);
5337}
NeilBrown390ee602010-06-01 19:37:27 +10005338EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005339
NeilBrown5aa61f42014-12-15 12:56:57 +11005340static void mddev_detach(struct mddev *mddev)
5341{
5342 struct bitmap *bitmap = mddev->bitmap;
5343 /* wait for behind writes to complete */
5344 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5345 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5346 mdname(mddev));
5347 /* need to kick something here to make sure I/O goes? */
5348 wait_event(bitmap->behind_wait,
5349 atomic_read(&bitmap->behind_writes) == 0);
5350 }
NeilBrown36d091f2014-12-15 12:56:58 +11005351 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005352 mddev->pers->quiesce(mddev, 1);
5353 mddev->pers->quiesce(mddev, 0);
5354 }
5355 md_unregister_thread(&mddev->thread);
5356 if (mddev->queue)
5357 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5358}
5359
NeilBrown5eff3c42012-11-19 10:47:48 +11005360static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005361{
NeilBrown36d091f2014-12-15 12:56:58 +11005362 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005363 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005364 spin_lock(&mddev->lock);
5365 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005366 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005367 spin_unlock(&mddev->lock);
5368 pers->free(mddev, mddev->private);
5369 if (pers->sync_request && mddev->to_remove == NULL)
5370 mddev->to_remove = &md_redundancy_group;
5371 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005372 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005373}
NeilBrown5eff3c42012-11-19 10:47:48 +11005374
5375void md_stop(struct mddev *mddev)
5376{
5377 /* stop the array and free an attached data structures.
5378 * This is called from dm-raid
5379 */
5380 __md_stop(mddev);
5381 bitmap_destroy(mddev);
5382 if (mddev->bio_set)
5383 bioset_free(mddev->bio_set);
5384}
5385
NeilBrown390ee602010-06-01 19:37:27 +10005386EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005387
NeilBrowna05b7ea2012-07-19 15:59:18 +10005388static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005389{
5390 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005391 int did_freeze = 0;
5392
5393 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5394 did_freeze = 1;
5395 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5396 md_wakeup_thread(mddev->thread);
5397 }
NeilBrownf851b602014-12-11 10:02:10 +11005398 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005399 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005400 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005401 /* Thread might be blocked waiting for metadata update
5402 * which will now never happen */
5403 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005404
NeilBrown30b8feb2013-11-14 15:16:17 +11005405 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005406 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5407 &mddev->recovery));
NeilBrown30b8feb2013-11-14 15:16:17 +11005408 mddev_lock_nointr(mddev);
5409
NeilBrowna4bd82d2010-03-29 13:23:10 +11005410 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005411 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005412 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005413 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005414 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005415 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005416 if (did_freeze) {
5417 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005418 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005419 md_wakeup_thread(mddev->thread);
5420 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005421 err = -EBUSY;
5422 goto out;
5423 }
5424 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005425 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005426
5427 err = -ENXIO;
5428 if (mddev->ro==1)
5429 goto out;
5430 mddev->ro = 1;
5431 set_disk_ro(mddev->gendisk, 1);
5432 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005433 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5434 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005435 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005436 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005437 }
5438out:
5439 mutex_unlock(&mddev->open_mutex);
5440 return err;
5441}
5442
NeilBrown9e653b62006-06-26 00:27:58 -07005443/* mode:
5444 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005445 * 2 - stop but do not disassemble array
5446 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005447static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005448 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005451 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005452 int did_freeze = 0;
5453
5454 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5455 did_freeze = 1;
5456 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5457 md_wakeup_thread(mddev->thread);
5458 }
NeilBrownf851b602014-12-11 10:02:10 +11005459 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005460 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005461 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005462 /* Thread might be blocked waiting for metadata update
5463 * which will now never happen */
5464 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005465
NeilBrown30b8feb2013-11-14 15:16:17 +11005466 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005467 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5468 !test_bit(MD_RECOVERY_RUNNING,
5469 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005470 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471
NeilBrownc8c00a62009-08-10 12:50:52 +10005472 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005473 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005474 mddev->sysfs_active ||
5475 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005476 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005477 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005478 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005479 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005480 if (did_freeze) {
5481 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005482 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005483 md_wakeup_thread(mddev->thread);
5484 }
NeilBrown260fa032013-08-27 16:44:13 +10005485 return -EBUSY;
5486 }
NeilBrown6e17b022010-08-07 21:41:19 +10005487 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005488 if (mddev->ro)
5489 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005490
NeilBrowndefad612011-01-14 09:14:33 +11005491 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005492 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005493 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005494 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005495
NeilBrowna4bd82d2010-03-29 13:23:10 +11005496 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005497 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005498
NeilBrowndafb20f2012-03-19 12:46:39 +11005499 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005500 if (rdev->raid_disk >= 0)
5501 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005502
NeilBrowna4bd82d2010-03-29 13:23:10 +11005503 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005504 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005505 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005506 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005507
NeilBrowna4bd82d2010-03-29 13:23:10 +11005508 if (mddev->ro)
5509 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005510 } else
5511 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 /*
5513 * Free resources if final stop
5514 */
NeilBrown9e653b62006-06-26 00:27:58 -07005515 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5517
NeilBrown978f9462006-02-02 14:28:05 -08005518 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005519 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005520 struct file *f = mddev->bitmap_info.file;
5521 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005522 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005523 spin_unlock(&mddev->lock);
5524 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005525 }
NeilBrownc3d97142009-12-14 12:49:52 +11005526 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005527
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 export_array(mddev);
5529
NeilBrown6177b472010-03-29 11:37:13 +11005530 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005531 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005532 if (mddev->hold_active == UNTIL_STOP)
5533 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005534 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005535 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005536 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005537 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539}
5540
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005541#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005542static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543{
NeilBrown3cb03002011-10-11 16:45:26 +11005544 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 int err;
5546
NeilBrowna757e642005-04-16 15:26:42 -07005547 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549
5550 printk(KERN_INFO "md: running: ");
5551
NeilBrowndafb20f2012-03-19 12:46:39 +11005552 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 char b[BDEVNAME_SIZE];
5554 printk("<%s>", bdevname(rdev->bdev,b));
5555 }
5556 printk("\n");
5557
NeilBrownd710e132008-10-13 11:55:12 +11005558 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 if (err) {
5560 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005561 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 }
5563}
5564
5565/*
5566 * lets try to run arrays based on all disks that have arrived
5567 * until now. (those are in pending_raid_disks)
5568 *
5569 * the method: pick the first pending disk, collect all disks with
5570 * the same UUID, remove all from the pending list and put them into
5571 * the 'same_array' list. Then order this list based on superblock
5572 * update time (freshest comes first), kick out 'old' disks and
5573 * compare superblocks. If everything's fine then run it.
5574 *
5575 * If "unit" is allocated, then bump its reference count
5576 */
5577static void autorun_devices(int part)
5578{
NeilBrown3cb03002011-10-11 16:45:26 +11005579 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005580 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 char b[BDEVNAME_SIZE];
5582
5583 printk(KERN_INFO "md: autorun ...\n");
5584 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005585 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005587 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005589 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
5591 printk(KERN_INFO "md: considering %s ...\n",
5592 bdevname(rdev0->bdev,b));
5593 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005594 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 if (super_90_load(rdev, rdev0, 0) >= 0) {
5596 printk(KERN_INFO "md: adding %s ...\n",
5597 bdevname(rdev->bdev,b));
5598 list_move(&rdev->same_set, &candidates);
5599 }
5600 /*
5601 * now we have a set of devices, with all of them having
5602 * mostly sane superblocks. It's time to allocate the
5603 * mddev.
5604 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005605 if (part) {
5606 dev = MKDEV(mdp_major,
5607 rdev0->preferred_minor << MdpMinorShift);
5608 unit = MINOR(dev) >> MdpMinorShift;
5609 } else {
5610 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5611 unit = MINOR(dev);
5612 }
5613 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5615 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5616 break;
5617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618
5619 md_probe(dev, NULL, NULL);
5620 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005621 if (!mddev || !mddev->gendisk) {
5622 if (mddev)
5623 mddev_put(mddev);
5624 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 "md: cannot allocate memory for md drive.\n");
5626 break;
5627 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005628 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 printk(KERN_WARNING "md: %s locked, cannot run\n",
5630 mdname(mddev));
5631 else if (mddev->raid_disks || mddev->major_version
5632 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005633 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 "md: %s already running, cannot run %s\n",
5635 mdname(mddev), bdevname(rdev0->bdev,b));
5636 mddev_unlock(mddev);
5637 } else {
5638 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005639 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005640 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 list_del_init(&rdev->same_set);
5642 if (bind_rdev_to_array(rdev, mddev))
5643 export_rdev(rdev);
5644 }
5645 autorun_array(mddev);
5646 mddev_unlock(mddev);
5647 }
5648 /* on success, candidates will be empty, on error
5649 * it won't...
5650 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005651 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005652 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 mddev_put(mddev);
5656 }
5657 printk(KERN_INFO "md: ... autorun DONE.\n");
5658}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005659#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660
NeilBrownf72ffdd2014-09-30 14:23:59 +10005661static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662{
5663 mdu_version_t ver;
5664
5665 ver.major = MD_MAJOR_VERSION;
5666 ver.minor = MD_MINOR_VERSION;
5667 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5668
5669 if (copy_to_user(arg, &ver, sizeof(ver)))
5670 return -EFAULT;
5671
5672 return 0;
5673}
5674
NeilBrownf72ffdd2014-09-30 14:23:59 +10005675static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676{
5677 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005678 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005679 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
NeilBrown1ca69c42012-10-11 13:37:33 +11005681 nr = working = insync = failed = spare = 0;
5682 rcu_read_lock();
5683 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005685 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 failed++;
5687 else {
5688 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005689 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005690 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 else
5692 spare++;
5693 }
5694 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005695 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
5697 info.major_version = mddev->major_version;
5698 info.minor_version = mddev->minor_version;
5699 info.patch_version = MD_PATCHLEVEL_VERSION;
5700 info.ctime = mddev->ctime;
5701 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005702 info.size = mddev->dev_sectors / 2;
5703 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005704 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 info.nr_disks = nr;
5706 info.raid_disks = mddev->raid_disks;
5707 info.md_minor = mddev->md_minor;
5708 info.not_persistent= !mddev->persistent;
5709
5710 info.utime = mddev->utime;
5711 info.state = 0;
5712 if (mddev->in_sync)
5713 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005714 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005715 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005716 if (mddev_is_clustered(mddev))
5717 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005718 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 info.working_disks = working;
5720 info.failed_disks = failed;
5721 info.spare_disks = spare;
5722
5723 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005724 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
5726 if (copy_to_user(arg, &info, sizeof(info)))
5727 return -EFAULT;
5728
5729 return 0;
5730}
5731
NeilBrownf72ffdd2014-09-30 14:23:59 +10005732static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005733{
5734 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005735 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005736 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005737
NeilBrown60559da2013-07-16 16:44:29 +10005738 file = kmalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005739 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005740 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005741
NeilBrown32a76272005-06-21 17:17:14 -07005742 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005743 spin_lock(&mddev->lock);
5744 /* bitmap disabled, zero the first byte and copy out */
5745 if (!mddev->bitmap_info.file)
5746 file->pathname[0] = '\0';
5747 else if ((ptr = d_path(&mddev->bitmap_info.file->f_path,
5748 file->pathname, sizeof(file->pathname))),
5749 IS_ERR(ptr))
5750 err = PTR_ERR(ptr);
5751 else
5752 memmove(file->pathname, ptr,
5753 sizeof(file->pathname)-(ptr-file->pathname));
5754 spin_unlock(&mddev->lock);
5755
5756 if (err == 0 &&
5757 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005758 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005759
NeilBrown32a76272005-06-21 17:17:14 -07005760 kfree(file);
5761 return err;
5762}
5763
NeilBrownf72ffdd2014-09-30 14:23:59 +10005764static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765{
5766 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005767 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768
5769 if (copy_from_user(&info, arg, sizeof(info)))
5770 return -EFAULT;
5771
NeilBrown1ca69c42012-10-11 13:37:33 +11005772 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005773 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 if (rdev) {
5775 info.major = MAJOR(rdev->bdev->bd_dev);
5776 info.minor = MINOR(rdev->bdev->bd_dev);
5777 info.raid_disk = rdev->raid_disk;
5778 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005779 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005781 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 info.state |= (1<<MD_DISK_ACTIVE);
5783 info.state |= (1<<MD_DISK_SYNC);
5784 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005785 if (test_bit(WriteMostly, &rdev->flags))
5786 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 } else {
5788 info.major = info.minor = 0;
5789 info.raid_disk = -1;
5790 info.state = (1<<MD_DISK_REMOVED);
5791 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005792 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793
5794 if (copy_to_user(arg, &info, sizeof(info)))
5795 return -EFAULT;
5796
5797 return 0;
5798}
5799
NeilBrownf72ffdd2014-09-30 14:23:59 +10005800static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801{
5802 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005803 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 dev_t dev = MKDEV(info->major,info->minor);
5805
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005806 if (mddev_is_clustered(mddev) &&
5807 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005808 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005809 mdname(mddev));
5810 return -EINVAL;
5811 }
5812
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5814 return -EOVERFLOW;
5815
5816 if (!mddev->raid_disks) {
5817 int err;
5818 /* expecting a device which has a superblock */
5819 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5820 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005821 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 "md: md_import_device returned %ld\n",
5823 PTR_ERR(rdev));
5824 return PTR_ERR(rdev);
5825 }
5826 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005827 struct md_rdev *rdev0
5828 = list_entry(mddev->disks.next,
5829 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005830 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 .load_super(rdev, rdev0, mddev->minor_version);
5832 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005833 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005835 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 bdevname(rdev0->bdev,b2));
5837 export_rdev(rdev);
5838 return -EINVAL;
5839 }
5840 }
5841 err = bind_rdev_to_array(rdev, mddev);
5842 if (err)
5843 export_rdev(rdev);
5844 return err;
5845 }
5846
5847 /*
5848 * add_new_disk can be used once the array is assembled
5849 * to add "hot spares". They must already have a superblock
5850 * written
5851 */
5852 if (mddev->pers) {
5853 int err;
5854 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005855 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 "%s: personality does not support diskops!\n",
5857 mdname(mddev));
5858 return -EINVAL;
5859 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005860 if (mddev->persistent)
5861 rdev = md_import_device(dev, mddev->major_version,
5862 mddev->minor_version);
5863 else
5864 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005866 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 "md: md_import_device returned %ld\n",
5868 PTR_ERR(rdev));
5869 return PTR_ERR(rdev);
5870 }
NeilBrown1a855a02010-12-09 16:36:28 +11005871 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005872 if (!mddev->persistent) {
5873 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005874 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005875 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005876 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005877 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005878 } else
NeilBrown41158c72005-06-21 17:17:25 -07005879 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005880 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005881 } else
5882 super_types[mddev->major_version].
5883 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005884 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005885 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005886 /* This was a hot-add request, but events doesn't
5887 * match, so reject it.
5888 */
5889 export_rdev(rdev);
5890 return -EINVAL;
5891 }
5892
NeilBrownb2d444d2005-11-08 21:39:31 -08005893 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005894 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5895 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005896 else
5897 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005898
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005899 /*
5900 * check whether the device shows up in other nodes
5901 */
5902 if (mddev_is_clustered(mddev)) {
5903 if (info->state & (1 << MD_DISK_CANDIDATE)) {
5904 /* Through --cluster-confirm */
5905 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005906 err = md_cluster_ops->new_disk_ack(mddev, true);
5907 if (err) {
5908 export_rdev(rdev);
5909 return err;
5910 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005911 } else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
5912 /* --add initiated by this node */
5913 err = md_cluster_ops->add_new_disk_start(mddev, rdev);
5914 if (err) {
5915 md_cluster_ops->add_new_disk_finish(mddev);
5916 export_rdev(rdev);
5917 return err;
5918 }
5919 }
5920 }
5921
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 rdev->raid_disk = -1;
5923 err = bind_rdev_to_array(rdev, mddev);
5924 if (err)
5925 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005926 else
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05005927 err = add_bound_rdev(rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005928 if (mddev_is_clustered(mddev) &&
5929 (info->state & (1 << MD_DISK_CLUSTER_ADD)))
5930 md_cluster_ops->add_new_disk_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 return err;
5932 }
5933
5934 /* otherwise, add_new_disk is only allowed
5935 * for major_version==0 superblocks
5936 */
5937 if (mddev->major_version != 0) {
5938 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5939 mdname(mddev));
5940 return -EINVAL;
5941 }
5942
5943 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5944 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005945 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005947 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 "md: error, md_import_device() returned %ld\n",
5949 PTR_ERR(rdev));
5950 return PTR_ERR(rdev);
5951 }
5952 rdev->desc_nr = info->number;
5953 if (info->raid_disk < mddev->raid_disks)
5954 rdev->raid_disk = info->raid_disk;
5955 else
5956 rdev->raid_disk = -1;
5957
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005959 if (info->state & (1<<MD_DISK_SYNC))
5960 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005962 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5963 set_bit(WriteMostly, &rdev->flags);
5964
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 if (!mddev->persistent) {
5966 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005967 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5968 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005969 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005970 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971
NeilBrown2bf071b2006-01-06 00:20:55 -08005972 err = bind_rdev_to_array(rdev, mddev);
5973 if (err) {
5974 export_rdev(rdev);
5975 return err;
5976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 }
5978
5979 return 0;
5980}
5981
NeilBrownf72ffdd2014-09-30 14:23:59 +10005982static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983{
5984 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005985 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 rdev = find_rdev(mddev, dev);
5988 if (!rdev)
5989 return -ENXIO;
5990
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005991 if (mddev_is_clustered(mddev))
5992 md_cluster_ops->metadata_update_start(mddev);
5993
NeilBrown3ea8929d2013-04-24 11:42:41 +10005994 clear_bit(Blocked, &rdev->flags);
5995 remove_and_add_spares(mddev, rdev);
5996
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 if (rdev->raid_disk >= 0)
5998 goto busy;
5999
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006000 if (mddev_is_clustered(mddev))
6001 md_cluster_ops->remove_disk(mddev, rdev);
6002
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006003 md_kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07006004 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006005 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006007 if (mddev_is_clustered(mddev))
6008 md_cluster_ops->metadata_update_finish(mddev);
6009
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 return 0;
6011busy:
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006012 if (mddev_is_clustered(mddev))
6013 md_cluster_ops->metadata_update_cancel(mddev);
Nick Andrewfdefa4d2008-04-21 22:42:58 +00006014 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 bdevname(rdev->bdev,b), mdname(mddev));
6016 return -EBUSY;
6017}
6018
NeilBrownf72ffdd2014-09-30 14:23:59 +10006019static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020{
6021 char b[BDEVNAME_SIZE];
6022 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006023 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024
6025 if (!mddev->pers)
6026 return -ENODEV;
6027
6028 if (mddev->major_version != 0) {
6029 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6030 " version-0 superblocks.\n",
6031 mdname(mddev));
6032 return -EINVAL;
6033 }
6034 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006035 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 "%s: personality does not support diskops!\n",
6037 mdname(mddev));
6038 return -EINVAL;
6039 }
6040
NeilBrownd710e132008-10-13 11:55:12 +11006041 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006043 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 "md: error, md_import_device() returned %ld\n",
6045 PTR_ERR(rdev));
6046 return -EINVAL;
6047 }
6048
6049 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006050 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006052 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
NeilBrown8190e752009-06-18 08:48:58 +10006054 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055
NeilBrownb2d444d2005-11-08 21:39:31 -08006056 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006057 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 "md: can not hot-add faulty %s disk to %s!\n",
6059 bdevname(rdev->bdev,b), mdname(mddev));
6060 err = -EINVAL;
6061 goto abort_export;
6062 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006063
6064 if (mddev_is_clustered(mddev))
6065 md_cluster_ops->metadata_update_start(mddev);
NeilBrownb2d444d2005-11-08 21:39:31 -08006066 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006068 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006069 err = bind_rdev_to_array(rdev, mddev);
6070 if (err)
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006071 goto abort_clustered;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
6073 /*
6074 * The rest should better be atomic, we can have disk failures
6075 * noticed in interrupt contexts ...
6076 */
6077
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 rdev->raid_disk = -1;
6079
NeilBrown850b2b422006-10-03 01:15:46 -07006080 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006082 if (mddev_is_clustered(mddev))
6083 md_cluster_ops->metadata_update_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 /*
6085 * Kick recovery, maybe this spare has to be added to the
6086 * array immediately.
6087 */
6088 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6089 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006090 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 return 0;
6092
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006093abort_clustered:
6094 if (mddev_is_clustered(mddev))
6095 md_cluster_ops->metadata_update_cancel(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096abort_export:
6097 export_rdev(rdev);
6098 return err;
6099}
6100
NeilBrownfd01b882011-10-11 16:47:53 +11006101static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006102{
NeilBrown035328c2014-04-09 12:25:40 +10006103 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006104
NeilBrown36fa3062005-09-09 16:23:45 -07006105 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006106 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006107 return -EBUSY;
6108 if (mddev->recovery || mddev->sync_thread)
6109 return -EBUSY;
6110 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006111 }
6112
NeilBrown36fa3062005-09-09 16:23:45 -07006113 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006114 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006115 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006116
NeilBrown1e594bb2014-12-15 12:57:00 +11006117 if (mddev->bitmap || mddev->bitmap_info.file)
6118 return -EEXIST; /* cannot add when bitmap is present */
6119 f = fget(fd);
6120
6121 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006122 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6123 mdname(mddev));
6124 return -EBADF;
6125 }
6126
NeilBrown1e594bb2014-12-15 12:57:00 +11006127 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006128 if (!S_ISREG(inode->i_mode)) {
6129 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6130 mdname(mddev));
6131 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006132 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006133 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6134 mdname(mddev));
6135 err = -EBADF;
6136 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006137 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6138 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006139 err = -EBUSY;
6140 }
6141 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006142 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006143 return err;
6144 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006145 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006146 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006147 } else if (mddev->bitmap == NULL)
6148 return -ENOENT; /* cannot remove what isn't there */
6149 err = 0;
6150 if (mddev->pers) {
6151 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006152 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006153 struct bitmap *bitmap;
6154
6155 bitmap = bitmap_create(mddev, -1);
6156 if (!IS_ERR(bitmap)) {
6157 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006158 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006159 } else
6160 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006161 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006162 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006163 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006164 fd = -1; /* make sure to put the file */
6165 }
NeilBrown36fa3062005-09-09 16:23:45 -07006166 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006167 }
6168 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006169 struct file *f = mddev->bitmap_info.file;
6170 if (f) {
6171 spin_lock(&mddev->lock);
6172 mddev->bitmap_info.file = NULL;
6173 spin_unlock(&mddev->lock);
6174 fput(f);
6175 }
NeilBrown36fa3062005-09-09 16:23:45 -07006176 }
6177
NeilBrown32a76272005-06-21 17:17:14 -07006178 return err;
6179}
6180
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181/*
6182 * set_array_info is used two different ways
6183 * The original usage is when creating a new array.
6184 * In this usage, raid_disks is > 0 and it together with
6185 * level, size, not_persistent,layout,chunksize determine the
6186 * shape of the array.
6187 * This will always create an array with a type-0.90.0 superblock.
6188 * The newer usage is when assembling an array.
6189 * In this case raid_disks will be 0, and the major_version field is
6190 * use to determine which style super-blocks are to be found on the devices.
6191 * The minor and patch _version numbers are also kept incase the
6192 * super_block handler wishes to interpret them.
6193 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006194static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195{
6196
6197 if (info->raid_disks == 0) {
6198 /* just setting version number for superblock loading */
6199 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006200 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 super_types[info->major_version].name == NULL) {
6202 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006203 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 "md: superblock version %d not known\n",
6205 info->major_version);
6206 return -EINVAL;
6207 }
6208 mddev->major_version = info->major_version;
6209 mddev->minor_version = info->minor_version;
6210 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006211 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006212 /* ensure mddev_put doesn't delete this now that there
6213 * is some minimal configuration.
6214 */
6215 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 return 0;
6217 }
6218 mddev->major_version = MD_MAJOR_VERSION;
6219 mddev->minor_version = MD_MINOR_VERSION;
6220 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6221 mddev->ctime = get_seconds();
6222
6223 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006224 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006225 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 mddev->raid_disks = info->raid_disks;
6227 /* don't set md_minor, it is determined by which /dev/md* was
6228 * openned
6229 */
6230 if (info->state & (1<<MD_SB_CLEAN))
6231 mddev->recovery_cp = MaxSector;
6232 else
6233 mddev->recovery_cp = 0;
6234 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006235 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236
6237 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006238 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239
6240 mddev->max_disks = MD_SB_DISKS;
6241
NeilBrowne6910632008-02-06 01:39:51 -08006242 if (mddev->persistent)
6243 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006244 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245
NeilBrownc3d97142009-12-14 12:49:52 +11006246 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006247 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006248 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006249
NeilBrownf6705572006-03-27 01:18:11 -08006250 mddev->reshape_position = MaxSector;
6251
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 /*
6253 * Generate a 128 bit UUID
6254 */
6255 get_random_bytes(mddev->uuid, 16);
6256
NeilBrownf6705572006-03-27 01:18:11 -08006257 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006258 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006259 mddev->new_layout = mddev->layout;
6260 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006261 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006262
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 return 0;
6264}
6265
NeilBrownfd01b882011-10-11 16:47:53 +11006266void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006267{
Dan Williamsb522adc2009-03-31 15:00:31 +11006268 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6269
6270 if (mddev->external_size)
6271 return;
6272
Dan Williams1f403622009-03-31 14:59:03 +11006273 mddev->array_sectors = array_sectors;
6274}
6275EXPORT_SYMBOL(md_set_array_sectors);
6276
NeilBrownfd01b882011-10-11 16:47:53 +11006277static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006278{
NeilBrown3cb03002011-10-11 16:45:26 +11006279 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006280 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006281 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006282
6283 if (mddev->pers->resize == NULL)
6284 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006285 /* The "num_sectors" is the number of sectors of each device that
6286 * is used. This can only make sense for arrays with redundancy.
6287 * linear and raid0 always use whatever space is available. We can only
6288 * consider changing this number if no resync or reconstruction is
6289 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006290 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006291 * of each device. If num_sectors is zero, we find the largest size
6292 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006293 */
NeilBrownf851b602014-12-11 10:02:10 +11006294 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6295 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006296 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006297 if (mddev->ro)
6298 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006299
NeilBrowndafb20f2012-03-19 12:46:39 +11006300 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006301 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006302
Andre Nolld71f9f82008-07-11 22:02:22 +10006303 if (fit && (num_sectors == 0 || num_sectors > avail))
6304 num_sectors = avail;
6305 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006306 return -ENOSPC;
6307 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006308 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006309 if (!rv)
6310 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006311 return rv;
6312}
6313
NeilBrownfd01b882011-10-11 16:47:53 +11006314static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006315{
6316 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006317 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006318 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006319 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006320 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006321 if (mddev->ro)
6322 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006323 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006324 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006325 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006326 if (mddev->sync_thread ||
6327 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6328 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006329 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006330
6331 rdev_for_each(rdev, mddev) {
6332 if (mddev->raid_disks < raid_disks &&
6333 rdev->data_offset < rdev->new_data_offset)
6334 return -EINVAL;
6335 if (mddev->raid_disks > raid_disks &&
6336 rdev->data_offset > rdev->new_data_offset)
6337 return -EINVAL;
6338 }
6339
NeilBrown63c70c42006-03-27 01:18:13 -08006340 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006341 if (mddev->delta_disks < 0)
6342 mddev->reshape_backwards = 1;
6343 else if (mddev->delta_disks > 0)
6344 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006345
6346 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006347 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006348 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006349 mddev->reshape_backwards = 0;
6350 }
NeilBrownda943b992006-01-06 00:20:54 -08006351 return rv;
6352}
6353
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354/*
6355 * update_array_info is used to change the configuration of an
6356 * on-line array.
6357 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6358 * fields in the info are checked against the array.
6359 * Any differences that cannot be handled will cause an error.
6360 * Normally, only one change can be managed at a time.
6361 */
NeilBrownfd01b882011-10-11 16:47:53 +11006362static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363{
6364 int rv = 0;
6365 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006366 int state = 0;
6367
6368 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006369 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006370 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371
6372 if (mddev->major_version != info->major_version ||
6373 mddev->minor_version != info->minor_version ||
6374/* mddev->patch_version != info->patch_version || */
6375 mddev->ctime != info->ctime ||
6376 mddev->level != info->level ||
6377/* mddev->layout != info->layout || */
6378 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006379 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006380 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6381 ((state^info->state) & 0xfffffe00)
6382 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 return -EINVAL;
6384 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006385 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6386 cnt++;
6387 if (mddev->raid_disks != info->raid_disks)
6388 cnt++;
6389 if (mddev->layout != info->layout)
6390 cnt++;
6391 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6392 cnt++;
6393 if (cnt == 0)
6394 return 0;
6395 if (cnt > 1)
6396 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397
6398 if (mddev->layout != info->layout) {
6399 /* Change layout
6400 * we don't need to do anything at the md level, the
6401 * personality will take care of it all.
6402 */
NeilBrown50ac1682009-06-18 08:47:55 +10006403 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006405 else {
6406 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006407 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006408 if (rv)
6409 mddev->new_layout = mddev->layout;
6410 return rv;
6411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006413 if (mddev_is_clustered(mddev))
6414 md_cluster_ops->metadata_update_start(mddev);
Andre Noll58c0fed2009-03-31 14:33:13 +11006415 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006416 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417
NeilBrownda943b992006-01-06 00:20:54 -08006418 if (mddev->raid_disks != info->raid_disks)
6419 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420
NeilBrown36fa3062005-09-09 16:23:45 -07006421 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006422 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6423 rv = -EINVAL;
6424 goto err;
6425 }
6426 if (mddev->recovery || mddev->sync_thread) {
6427 rv = -EBUSY;
6428 goto err;
6429 }
NeilBrown36fa3062005-09-09 16:23:45 -07006430 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006431 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006432 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006433 if (mddev->bitmap) {
6434 rv = -EEXIST;
6435 goto err;
6436 }
6437 if (mddev->bitmap_info.default_offset == 0) {
6438 rv = -EINVAL;
6439 goto err;
6440 }
NeilBrownc3d97142009-12-14 12:49:52 +11006441 mddev->bitmap_info.offset =
6442 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006443 mddev->bitmap_info.space =
6444 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006445 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006446 bitmap = bitmap_create(mddev, -1);
6447 if (!IS_ERR(bitmap)) {
6448 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006449 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006450 } else
6451 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006452 if (rv)
6453 bitmap_destroy(mddev);
6454 mddev->pers->quiesce(mddev, 0);
6455 } else {
6456 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006457 if (!mddev->bitmap) {
6458 rv = -ENOENT;
6459 goto err;
6460 }
6461 if (mddev->bitmap->storage.file) {
6462 rv = -EINVAL;
6463 goto err;
6464 }
NeilBrown36fa3062005-09-09 16:23:45 -07006465 mddev->pers->quiesce(mddev, 1);
6466 bitmap_destroy(mddev);
6467 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006468 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006469 }
6470 }
NeilBrown850b2b422006-10-03 01:15:46 -07006471 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006472 if (mddev_is_clustered(mddev))
6473 md_cluster_ops->metadata_update_finish(mddev);
6474 return rv;
6475err:
6476 if (mddev_is_clustered(mddev))
6477 md_cluster_ops->metadata_update_cancel(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 return rv;
6479}
6480
NeilBrownfd01b882011-10-11 16:47:53 +11006481static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482{
NeilBrown3cb03002011-10-11 16:45:26 +11006483 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006484 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485
6486 if (mddev->pers == NULL)
6487 return -ENODEV;
6488
NeilBrown1ca69c42012-10-11 13:37:33 +11006489 rcu_read_lock();
6490 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006492 err = -ENODEV;
6493 else {
6494 md_error(mddev, rdev);
6495 if (!test_bit(Faulty, &rdev->flags))
6496 err = -EBUSY;
6497 }
6498 rcu_read_unlock();
6499 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500}
6501
Andre Noll2f9618c2008-04-25 18:57:58 +02006502/*
6503 * We have a problem here : there is no easy way to give a CHS
6504 * virtual geometry. We currently pretend that we have a 2 heads
6505 * 4 sectors (with a BIG number of cylinders...). This drives
6506 * dosfs just mad... ;-)
6507 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006508static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6509{
NeilBrownfd01b882011-10-11 16:47:53 +11006510 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006511
6512 geo->heads = 2;
6513 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006514 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006515 return 0;
6516}
6517
Nicolas Schichancb335f82014-01-15 16:58:52 +01006518static inline bool md_ioctl_valid(unsigned int cmd)
6519{
6520 switch (cmd) {
6521 case ADD_NEW_DISK:
6522 case BLKROSET:
6523 case GET_ARRAY_INFO:
6524 case GET_BITMAP_FILE:
6525 case GET_DISK_INFO:
6526 case HOT_ADD_DISK:
6527 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006528 case RAID_AUTORUN:
6529 case RAID_VERSION:
6530 case RESTART_ARRAY_RW:
6531 case RUN_ARRAY:
6532 case SET_ARRAY_INFO:
6533 case SET_BITMAP_FILE:
6534 case SET_DISK_FAULTY:
6535 case STOP_ARRAY:
6536 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006537 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006538 return true;
6539 default:
6540 return false;
6541 }
6542}
6543
Al Viroa39907f2008-03-02 10:31:15 -05006544static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545 unsigned int cmd, unsigned long arg)
6546{
6547 int err = 0;
6548 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006549 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006550 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551
Nicolas Schichancb335f82014-01-15 16:58:52 +01006552 if (!md_ioctl_valid(cmd))
6553 return -ENOTTY;
6554
NeilBrown506c9e42011-12-23 10:17:26 +11006555 switch (cmd) {
6556 case RAID_VERSION:
6557 case GET_ARRAY_INFO:
6558 case GET_DISK_INFO:
6559 break;
6560 default:
6561 if (!capable(CAP_SYS_ADMIN))
6562 return -EACCES;
6563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564
6565 /*
6566 * Commands dealing with the RAID driver but not any
6567 * particular array:
6568 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006569 switch (cmd) {
6570 case RAID_VERSION:
6571 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006572 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006575 case RAID_AUTORUN:
6576 err = 0;
6577 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006578 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006580 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 }
6582
6583 /*
6584 * Commands creating/starting a new array:
6585 */
6586
Al Viroa39907f2008-03-02 10:31:15 -05006587 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588
6589 if (!mddev) {
6590 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006591 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 }
6593
NeilBrown1ca69c42012-10-11 13:37:33 +11006594 /* Some actions do not requires the mutex */
6595 switch (cmd) {
6596 case GET_ARRAY_INFO:
6597 if (!mddev->raid_disks && !mddev->external)
6598 err = -ENODEV;
6599 else
6600 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006601 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006602
6603 case GET_DISK_INFO:
6604 if (!mddev->raid_disks && !mddev->external)
6605 err = -ENODEV;
6606 else
6607 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006608 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006609
6610 case SET_DISK_FAULTY:
6611 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006612 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006613
6614 case GET_BITMAP_FILE:
6615 err = get_bitmap_file(mddev, argp);
6616 goto out;
6617
NeilBrown1ca69c42012-10-11 13:37:33 +11006618 }
6619
NeilBrowna7a3f082012-12-11 13:35:54 +11006620 if (cmd == ADD_NEW_DISK)
6621 /* need to ensure md_delayed_delete() has completed */
6622 flush_workqueue(md_misc_wq);
6623
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006624 if (cmd == HOT_REMOVE_DISK)
6625 /* need to ensure recovery thread has run */
6626 wait_event_interruptible_timeout(mddev->sb_wait,
6627 !test_bit(MD_RECOVERY_NEEDED,
6628 &mddev->flags),
6629 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006630 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6631 /* Need to flush page cache, and ensure no-one else opens
6632 * and writes
6633 */
6634 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006635 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006636 mutex_unlock(&mddev->open_mutex);
6637 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006638 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006639 }
6640 set_bit(MD_STILL_CLOSED, &mddev->flags);
6641 mutex_unlock(&mddev->open_mutex);
6642 sync_blockdev(bdev);
6643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644 err = mddev_lock(mddev);
6645 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006646 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6648 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006649 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 }
6651
NeilBrownc02c0ae2012-12-11 13:39:21 +11006652 if (cmd == SET_ARRAY_INFO) {
6653 mdu_array_info_t info;
6654 if (!arg)
6655 memset(&info, 0, sizeof(info));
6656 else if (copy_from_user(&info, argp, sizeof(info))) {
6657 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006658 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006659 }
6660 if (mddev->pers) {
6661 err = update_array_info(mddev, &info);
6662 if (err) {
6663 printk(KERN_WARNING "md: couldn't update"
6664 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006665 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006667 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006668 }
6669 if (!list_empty(&mddev->disks)) {
6670 printk(KERN_WARNING
6671 "md: array %s already has disks!\n",
6672 mdname(mddev));
6673 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006674 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006675 }
6676 if (mddev->raid_disks) {
6677 printk(KERN_WARNING
6678 "md: array %s already initialised!\n",
6679 mdname(mddev));
6680 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006681 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006682 }
6683 err = set_array_info(mddev, &info);
6684 if (err) {
6685 printk(KERN_WARNING "md: couldn't set"
6686 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006687 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006688 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006689 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 }
6691
6692 /*
6693 * Commands querying/configuring an existing array:
6694 */
NeilBrown32a76272005-06-21 17:17:14 -07006695 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006696 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006697 if ((!mddev->raid_disks && !mddev->external)
6698 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6699 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6700 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006702 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 }
6704
6705 /*
6706 * Commands even a read-only array can execute:
6707 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006708 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006709 case RESTART_ARRAY_RW:
6710 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006711 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006712
6713 case STOP_ARRAY:
6714 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006715 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006716
6717 case STOP_ARRAY_RO:
6718 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006719 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006720
NeilBrown3ea8929d2013-04-24 11:42:41 +10006721 case HOT_REMOVE_DISK:
6722 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006723 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006724
NeilBrown7ceb17e2013-04-24 11:42:42 +10006725 case ADD_NEW_DISK:
6726 /* We can support ADD_NEW_DISK on read-only arrays
6727 * on if we are re-adding a preexisting device.
6728 * So require mddev->pers and MD_DISK_SYNC.
6729 */
6730 if (mddev->pers) {
6731 mdu_disk_info_t info;
6732 if (copy_from_user(&info, argp, sizeof(info)))
6733 err = -EFAULT;
6734 else if (!(info.state & (1<<MD_DISK_SYNC)))
6735 /* Need to clear read-only for this */
6736 break;
6737 else
6738 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006739 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006740 }
6741 break;
6742
NeilBrownc02c0ae2012-12-11 13:39:21 +11006743 case BLKROSET:
6744 if (get_user(ro, (int __user *)(arg))) {
6745 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006746 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006747 }
6748 err = -EINVAL;
6749
6750 /* if the bdev is going readonly the value of mddev->ro
6751 * does not matter, no writes are coming
6752 */
6753 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006754 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006755
NeilBrownc02c0ae2012-12-11 13:39:21 +11006756 /* are we are already prepared for writes? */
6757 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006758 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006759
6760 /* transitioning to readauto need only happen for
6761 * arrays that call md_write_start
6762 */
6763 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006765 if (err == 0) {
6766 mddev->ro = 2;
6767 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006768 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006769 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006770 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 }
6772
6773 /*
6774 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006775 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 */
NeilBrown326eb172014-09-30 15:36:28 +10006777 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006778 if (mddev->ro == 2) {
6779 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006780 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006781 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006782 /* mddev_unlock will wake thread */
6783 /* If a device failed while we were read-only, we
6784 * need to make sure the metadata is updated now.
6785 */
6786 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6787 mddev_unlock(mddev);
6788 wait_event(mddev->sb_wait,
6789 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6790 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006791 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006792 }
NeilBrownf91de922005-11-08 21:39:36 -08006793 } else {
6794 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006795 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797 }
6798
NeilBrownc02c0ae2012-12-11 13:39:21 +11006799 switch (cmd) {
6800 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006802 mdu_disk_info_t info;
6803 if (copy_from_user(&info, argp, sizeof(info)))
6804 err = -EFAULT;
6805 else
6806 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006807 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006810 case CLUSTERED_DISK_NACK:
6811 if (mddev_is_clustered(mddev))
6812 md_cluster_ops->new_disk_ack(mddev, false);
6813 else
6814 err = -EINVAL;
6815 goto unlock;
6816
NeilBrownc02c0ae2012-12-11 13:39:21 +11006817 case HOT_ADD_DISK:
6818 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006819 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820
NeilBrownc02c0ae2012-12-11 13:39:21 +11006821 case RUN_ARRAY:
6822 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006823 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824
NeilBrownc02c0ae2012-12-11 13:39:21 +11006825 case SET_BITMAP_FILE:
6826 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006827 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006828
NeilBrownc02c0ae2012-12-11 13:39:21 +11006829 default:
6830 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006831 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 }
6833
NeilBrown3adc28d2014-09-30 15:46:41 +10006834unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006835 if (mddev->hold_active == UNTIL_IOCTL &&
6836 err != -EINVAL)
6837 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006839out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 return err;
6841}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006842#ifdef CONFIG_COMPAT
6843static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6844 unsigned int cmd, unsigned long arg)
6845{
6846 switch (cmd) {
6847 case HOT_REMOVE_DISK:
6848 case HOT_ADD_DISK:
6849 case SET_DISK_FAULTY:
6850 case SET_BITMAP_FILE:
6851 /* These take in integer arg, do not convert */
6852 break;
6853 default:
6854 arg = (unsigned long)compat_ptr(arg);
6855 break;
6856 }
6857
6858 return md_ioctl(bdev, mode, cmd, arg);
6859}
6860#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861
Al Viroa39907f2008-03-02 10:31:15 -05006862static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863{
6864 /*
6865 * Succeed if we can lock the mddev, which confirms that
6866 * it isn't being stopped right now.
6867 */
NeilBrownfd01b882011-10-11 16:47:53 +11006868 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869 int err;
6870
Yuanhan Liu0c098222012-05-22 13:55:32 +10006871 if (!mddev)
6872 return -ENODEV;
6873
NeilBrownd3374822009-01-09 08:31:10 +11006874 if (mddev->gendisk != bdev->bd_disk) {
6875 /* we are racing with mddev_put which is discarding this
6876 * bd_disk.
6877 */
6878 mddev_put(mddev);
6879 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006880 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006881 /* Then retry the open from the top */
6882 return -ERESTARTSYS;
6883 }
6884 BUG_ON(mddev != bdev->bd_disk->private_data);
6885
NeilBrownc8c00a62009-08-10 12:50:52 +10006886 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887 goto out;
6888
6889 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006890 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006891 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006892 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006894 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 out:
6896 return err;
6897}
6898
Al Virodb2a1442013-05-05 21:52:57 -04006899static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900{
NeilBrownf72ffdd2014-09-30 14:23:59 +10006901 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006903 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006904 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006907
6908static int md_media_changed(struct gendisk *disk)
6909{
NeilBrownfd01b882011-10-11 16:47:53 +11006910 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006911
6912 return mddev->changed;
6913}
6914
6915static int md_revalidate(struct gendisk *disk)
6916{
NeilBrownfd01b882011-10-11 16:47:53 +11006917 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006918
6919 mddev->changed = 0;
6920 return 0;
6921}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006922static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923{
6924 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006925 .open = md_open,
6926 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006927 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006928#ifdef CONFIG_COMPAT
6929 .compat_ioctl = md_compat_ioctl,
6930#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006931 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006932 .media_changed = md_media_changed,
6933 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934};
6935
NeilBrownf72ffdd2014-09-30 14:23:59 +10006936static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937{
NeilBrown2b8bf342011-10-11 16:48:23 +11006938 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 /*
6941 * md_thread is a 'system-thread', it's priority should be very
6942 * high. We avoid resource deadlocks individually in each
6943 * raid personality. (RAID5 does preallocation) We also use RR and
6944 * the very same RT priority as kswapd, thus we will never get
6945 * into a priority inversion deadlock.
6946 *
6947 * we definitely have to have equal or higher priority than
6948 * bdflush, otherwise bdflush will deadlock if there are too
6949 * many dirty RAID5 blocks.
6950 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951
NeilBrown6985c432005-10-19 21:23:47 -07006952 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006953 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954
NeilBrown93588e22005-11-15 00:09:12 -08006955 /* We need to wait INTERRUPTIBLE so that
6956 * we don't add to the load-average.
6957 * That means we need to be sure no signals are
6958 * pending
6959 */
6960 if (signal_pending(current))
6961 flush_signals(current);
6962
6963 wait_event_interruptible_timeout
6964 (thread->wqueue,
6965 test_bit(THREAD_WAKEUP, &thread->flags)
6966 || kthread_should_stop(),
6967 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968
NeilBrown6c987912011-01-14 09:13:53 +11006969 clear_bit(THREAD_WAKEUP, &thread->flags);
6970 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006971 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006973
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974 return 0;
6975}
6976
NeilBrown2b8bf342011-10-11 16:48:23 +11006977void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978{
6979 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006980 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981 set_bit(THREAD_WAKEUP, &thread->flags);
6982 wake_up(&thread->wqueue);
6983 }
6984}
NeilBrown6c144d32014-09-30 16:15:38 +10006985EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986
Shaohua Li4ed87312012-10-11 13:34:00 +11006987struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6988 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989{
NeilBrown2b8bf342011-10-11 16:48:23 +11006990 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991
NeilBrown2b8bf342011-10-11 16:48:23 +11006992 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 if (!thread)
6994 return NULL;
6995
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996 init_waitqueue_head(&thread->wqueue);
6997
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 thread->run = run;
6999 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007000 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007001 thread->tsk = kthread_run(md_thread, thread,
7002 "%s_%s",
7003 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007004 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007005 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006 kfree(thread);
7007 return NULL;
7008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009 return thread;
7010}
NeilBrown6c144d32014-09-30 16:15:38 +10007011EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012
NeilBrown2b8bf342011-10-11 16:48:23 +11007013void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014{
NeilBrown2b8bf342011-10-11 16:48:23 +11007015 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007016 if (!thread)
7017 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007018 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007019 /* Locking ensures that mddev_unlock does not wake_up a
7020 * non-existent thread
7021 */
7022 spin_lock(&pers_lock);
7023 *threadp = NULL;
7024 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007025
7026 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 kfree(thread);
7028}
NeilBrown6c144d32014-09-30 16:15:38 +10007029EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030
NeilBrownfd01b882011-10-11 16:47:53 +11007031void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032{
NeilBrownb2d444d2005-11-08 21:39:31 -08007033 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007035
NeilBrownde393cd2011-07-28 11:31:48 +10007036 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037 return;
7038 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007039 if (mddev->degraded)
7040 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007041 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7043 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7044 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007045 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007046 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07007047 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048}
NeilBrown6c144d32014-09-30 16:15:38 +10007049EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050
7051/* seq_file implementation /proc/mdstat */
7052
7053static void status_unused(struct seq_file *seq)
7054{
7055 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007056 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057
7058 seq_printf(seq, "unused devices: ");
7059
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007060 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061 char b[BDEVNAME_SIZE];
7062 i++;
7063 seq_printf(seq, "%s ",
7064 bdevname(rdev->bdev,b));
7065 }
7066 if (!i)
7067 seq_printf(seq, "<none>");
7068
7069 seq_printf(seq, "\n");
7070}
7071
NeilBrownf72ffdd2014-09-30 14:23:59 +10007072static void status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073{
NeilBrowndd71cf62009-05-07 12:49:35 +10007074 sector_t max_sectors, resync, res;
7075 unsigned long dt, db;
7076 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007077 int scale;
7078 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079
NeilBrown72f36d52012-10-11 14:25:57 +11007080 if (mddev->curr_resync <= 3)
7081 resync = 0;
7082 else
7083 resync = mddev->curr_resync
7084 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085
NeilBrownc804cde2012-05-21 09:28:33 +10007086 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7087 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007088 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007090 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091
NeilBrown403df472014-09-30 15:52:29 +10007092 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007093 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007094 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007095 * u32, as those are the requirements for sector_div.
7096 * Thus 'scale' must be at least 10
7097 */
7098 scale = 10;
7099 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007100 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007101 scale++;
7102 }
7103 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007104 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007105
7106 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107 {
NeilBrown4588b422006-03-27 01:18:04 -08007108 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 seq_printf(seq, "[");
7110 for (i = 0; i < x; i++)
7111 seq_printf(seq, "=");
7112 seq_printf(seq, ">");
7113 for (i = 0; i < y; i++)
7114 seq_printf(seq, ".");
7115 seq_printf(seq, "] ");
7116 }
NeilBrown4588b422006-03-27 01:18:04 -08007117 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007118 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7119 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007120 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7121 "check" :
7122 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7123 "resync" : "recovery"))),
7124 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007125 (unsigned long long) resync/2,
7126 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127
7128 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 * dt: time from mark until now
7130 * db: blocks written from mark until now
7131 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007132 *
7133 * rt is a sector_t, so could be 32bit or 64bit.
7134 * So we divide before multiply in case it is 32bit and close
7135 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007136 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007137 * near the end of resync when the number of remaining sectors
7138 * is close to 'db'.
7139 * We then divide rt by 32 after multiplying by db to compensate.
7140 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 */
7142 dt = ((jiffies - mddev->resync_mark) / HZ);
7143 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007144 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7145 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146
NeilBrowndd71cf62009-05-07 12:49:35 +10007147 rt = max_sectors - resync; /* number of remaining sectors */
7148 sector_div(rt, db/32+1);
7149 rt *= dt;
7150 rt >>= 5;
7151
7152 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7153 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154
NeilBrownff4e8d92006-07-10 04:44:16 -07007155 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156}
7157
7158static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7159{
7160 struct list_head *tmp;
7161 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007162 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163
7164 if (l >= 0x10000)
7165 return NULL;
7166 if (!l--)
7167 /* header */
7168 return (void*)1;
7169
7170 spin_lock(&all_mddevs_lock);
7171 list_for_each(tmp,&all_mddevs)
7172 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007173 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 mddev_get(mddev);
7175 spin_unlock(&all_mddevs_lock);
7176 return mddev;
7177 }
7178 spin_unlock(&all_mddevs_lock);
7179 if (!l--)
7180 return (void*)2;/* tail */
7181 return NULL;
7182}
7183
7184static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7185{
7186 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007187 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007188
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189 ++*pos;
7190 if (v == (void*)2)
7191 return NULL;
7192
7193 spin_lock(&all_mddevs_lock);
7194 if (v == (void*)1)
7195 tmp = all_mddevs.next;
7196 else
7197 tmp = mddev->all_mddevs.next;
7198 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007199 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200 else {
7201 next_mddev = (void*)2;
7202 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204 spin_unlock(&all_mddevs_lock);
7205
7206 if (v != (void*)1)
7207 mddev_put(mddev);
7208 return next_mddev;
7209
7210}
7211
7212static void md_seq_stop(struct seq_file *seq, void *v)
7213{
NeilBrownfd01b882011-10-11 16:47:53 +11007214 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215
7216 if (mddev && v != (void*)1 && v != (void*)2)
7217 mddev_put(mddev);
7218}
7219
7220static int md_seq_show(struct seq_file *seq, void *v)
7221{
NeilBrownfd01b882011-10-11 16:47:53 +11007222 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007223 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007224 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225
7226 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007227 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 seq_printf(seq, "Personalities : ");
7229 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007230 list_for_each_entry(pers, &pers_list, list)
7231 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232
7233 spin_unlock(&pers_lock);
7234 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007235 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 return 0;
7237 }
7238 if (v == (void*)2) {
7239 status_unused(seq);
7240 return 0;
7241 }
7242
NeilBrown36d091f2014-12-15 12:56:58 +11007243 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7245 seq_printf(seq, "%s : %sactive", mdname(mddev),
7246 mddev->pers ? "" : "in");
7247 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007248 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007250 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007251 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 seq_printf(seq, " %s", mddev->pers->name);
7253 }
7254
Andre Nolldd8ac332009-03-31 14:33:13 +11007255 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007256 rcu_read_lock();
7257 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258 char b[BDEVNAME_SIZE];
7259 seq_printf(seq, " %s[%d]",
7260 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007261 if (test_bit(WriteMostly, &rdev->flags))
7262 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007263 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264 seq_printf(seq, "(F)");
7265 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007266 }
7267 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007268 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007269 if (test_bit(Replacement, &rdev->flags))
7270 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007271 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007273 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274
7275 if (!list_empty(&mddev->disks)) {
7276 if (mddev->pers)
7277 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007278 (unsigned long long)
7279 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 else
7281 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007282 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007284 if (mddev->persistent) {
7285 if (mddev->major_version != 0 ||
7286 mddev->minor_version != 90) {
7287 seq_printf(seq," super %d.%d",
7288 mddev->major_version,
7289 mddev->minor_version);
7290 }
NeilBrowne6910632008-02-06 01:39:51 -08007291 } else if (mddev->external)
7292 seq_printf(seq, " super external:%s",
7293 mddev->metadata_type);
7294 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007295 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296
7297 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007298 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007299 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007300 if (mddev->pers->sync_request) {
7301 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007302 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007303 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007304 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007305 seq_printf(seq, "\tresync=DELAYED\n ");
7306 else if (mddev->recovery_cp < MaxSector)
7307 seq_printf(seq, "\tresync=PENDING\n ");
7308 }
NeilBrown32a76272005-06-21 17:17:14 -07007309 } else
7310 seq_printf(seq, "\n ");
7311
NeilBrown57148962012-03-19 12:46:40 +11007312 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313
7314 seq_printf(seq, "\n");
7315 }
NeilBrown36d091f2014-12-15 12:56:58 +11007316 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007317
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 return 0;
7319}
7320
Jan Engelhardt110518b2009-05-07 12:49:37 +10007321static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322 .start = md_seq_start,
7323 .next = md_seq_next,
7324 .stop = md_seq_stop,
7325 .show = md_seq_show,
7326};
7327
7328static int md_seq_open(struct inode *inode, struct file *file)
7329{
Kay Sieversf1514632011-07-12 20:48:39 +02007330 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331 int error;
7332
7333 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007334 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007335 return error;
7336
7337 seq = file->private_data;
7338 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339 return error;
7340}
7341
NeilBrowne2f23b62014-04-09 14:33:51 +10007342static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007343static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7344{
Kay Sieversf1514632011-07-12 20:48:39 +02007345 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007346 int mask;
7347
NeilBrowne2f23b62014-04-09 14:33:51 +10007348 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007349 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007350 poll_wait(filp, &md_event_waiters, wait);
7351
7352 /* always allow read */
7353 mask = POLLIN | POLLRDNORM;
7354
Kay Sieversf1514632011-07-12 20:48:39 +02007355 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007356 mask |= POLLERR | POLLPRI;
7357 return mask;
7358}
7359
Arjan van de Venfa027c22007-02-12 00:55:33 -08007360static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007361 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 .open = md_seq_open,
7363 .read = seq_read,
7364 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007365 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007366 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367};
7368
NeilBrown84fc4b52011-10-11 16:49:58 +11007369int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370{
Chao Yu50bd3772014-09-25 15:28:34 +08007371 printk(KERN_INFO "md: %s personality registered for level %d\n",
7372 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007374 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 spin_unlock(&pers_lock);
7376 return 0;
7377}
NeilBrown6c144d32014-09-30 16:15:38 +10007378EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379
NeilBrown84fc4b52011-10-11 16:49:58 +11007380int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381{
NeilBrown2604b702006-01-06 00:20:36 -08007382 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007384 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 spin_unlock(&pers_lock);
7386 return 0;
7387}
NeilBrown6c144d32014-09-30 16:15:38 +10007388EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007390int register_md_cluster_operations(struct md_cluster_operations *ops, struct module *module)
7391{
7392 if (md_cluster_ops != NULL)
7393 return -EALREADY;
7394 spin_lock(&pers_lock);
7395 md_cluster_ops = ops;
7396 md_cluster_mod = module;
7397 spin_unlock(&pers_lock);
7398 return 0;
7399}
7400EXPORT_SYMBOL(register_md_cluster_operations);
7401
7402int unregister_md_cluster_operations(void)
7403{
7404 spin_lock(&pers_lock);
7405 md_cluster_ops = NULL;
7406 spin_unlock(&pers_lock);
7407 return 0;
7408}
7409EXPORT_SYMBOL(unregister_md_cluster_operations);
7410
7411int md_setup_cluster(struct mddev *mddev, int nodes)
7412{
7413 int err;
7414
7415 err = request_module("md-cluster");
7416 if (err) {
7417 pr_err("md-cluster module not found.\n");
7418 return err;
7419 }
7420
7421 spin_lock(&pers_lock);
7422 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7423 spin_unlock(&pers_lock);
7424 return -ENOENT;
7425 }
7426 spin_unlock(&pers_lock);
7427
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007428 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007429}
7430
7431void md_cluster_stop(struct mddev *mddev)
7432{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007433 if (!md_cluster_ops)
7434 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007435 md_cluster_ops->leave(mddev);
7436 module_put(md_cluster_mod);
7437}
7438
NeilBrownfd01b882011-10-11 16:47:53 +11007439static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007441 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007442 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007443 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444
7445 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007446 rcu_read_lock();
7447 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007449 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7450 (int)part_stat_read(&disk->part0, sectors[1]) -
7451 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007452 /* sync IO will cause sync_io to increase before the disk_stats
7453 * as sync_io is counted when a request starts, and
7454 * disk_stats is counted when it completes.
7455 * So resync activity will cause curr_events to be smaller than
7456 * when there was no such activity.
7457 * non-sync IO will cause disk_stat to increase without
7458 * increasing sync_io so curr_events will (eventually)
7459 * be larger than it was before. Once it becomes
7460 * substantially larger, the test below will cause
7461 * the array to appear non-idle, and resync will slow
7462 * down.
7463 * If there is a lot of outstanding resync activity when
7464 * we set last_event to curr_events, then all that activity
7465 * completing might cause the array to appear non-idle
7466 * and resync will be slowed down even though there might
7467 * not have been non-resync activity. This will only
7468 * happen once though. 'last_events' will soon reflect
7469 * the state where there is little or no outstanding
7470 * resync requests, and further resync activity will
7471 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007472 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007474 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475 rdev->last_events = curr_events;
7476 idle = 0;
7477 }
7478 }
NeilBrown4b809912008-07-21 17:05:25 +10007479 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480 return idle;
7481}
7482
NeilBrownfd01b882011-10-11 16:47:53 +11007483void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484{
7485 /* another "blocks" (512byte) blocks have been synced */
7486 atomic_sub(blocks, &mddev->recovery_active);
7487 wake_up(&mddev->recovery_wait);
7488 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007489 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007490 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491 md_wakeup_thread(mddev->thread);
7492 // stop recovery, signal do_sync ....
7493 }
7494}
NeilBrown6c144d32014-09-30 16:15:38 +10007495EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496
NeilBrown06d91a52005-06-21 17:17:12 -07007497/* md_write_start(mddev, bi)
7498 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007499 * in superblock) before writing, schedule a superblock update
7500 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007501 */
NeilBrownfd01b882011-10-11 16:47:53 +11007502void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503{
Neil Brown0fd62b82008-06-28 08:31:36 +10007504 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007505 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007506 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007507
NeilBrownf91de922005-11-08 21:39:36 -08007508 BUG_ON(mddev->ro == 1);
7509 if (mddev->ro == 2) {
7510 /* need to switch to read/write */
7511 mddev->ro = 0;
7512 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7513 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007514 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007515 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007516 }
NeilBrown06d91a52005-06-21 17:17:12 -07007517 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007518 if (mddev->safemode == 1)
7519 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007520 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007521 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007522 if (mddev->in_sync) {
7523 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007524 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007525 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007526 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007527 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007528 }
NeilBrown85572d72014-12-15 12:56:56 +11007529 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007530 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007531 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007532 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007533 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007534 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535}
NeilBrown6c144d32014-09-30 16:15:38 +10007536EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537
NeilBrownfd01b882011-10-11 16:47:53 +11007538void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539{
7540 if (atomic_dec_and_test(&mddev->writes_pending)) {
7541 if (mddev->safemode == 2)
7542 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007543 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7545 }
7546}
NeilBrown6c144d32014-09-30 16:15:38 +10007547EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548
NeilBrown2a2275d2007-01-26 00:57:11 -08007549/* md_allow_write(mddev)
7550 * Calling this ensures that the array is marked 'active' so that writes
7551 * may proceed without blocking. It is important to call this before
7552 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7553 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007554 *
7555 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7556 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007557 */
NeilBrownfd01b882011-10-11 16:47:53 +11007558int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007559{
7560 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007561 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007562 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007563 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007564 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007565 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007566
NeilBrown85572d72014-12-15 12:56:56 +11007567 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007568 if (mddev->in_sync) {
7569 mddev->in_sync = 0;
7570 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007571 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007572 if (mddev->safemode_delay &&
7573 mddev->safemode == 0)
7574 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007575 spin_unlock(&mddev->lock);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007576 if (mddev_is_clustered(mddev))
7577 md_cluster_ops->metadata_update_start(mddev);
NeilBrown2a2275d2007-01-26 00:57:11 -08007578 md_update_sb(mddev, 0);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007579 if (mddev_is_clustered(mddev))
7580 md_cluster_ops->metadata_update_finish(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007581 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007582 } else
NeilBrown85572d72014-12-15 12:56:56 +11007583 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007584
NeilBrown070dc6d2010-08-30 17:33:34 +10007585 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007586 return -EAGAIN;
7587 else
7588 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007589}
7590EXPORT_SYMBOL_GPL(md_allow_write);
7591
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592#define SYNC_MARKS 10
7593#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007594#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007595void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596{
Shaohua Li4ed87312012-10-11 13:34:00 +11007597 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007598 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599 unsigned int currspeed = 0,
7600 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007601 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007602 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007603 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604 sector_t mark_cnt[SYNC_MARKS];
7605 int last_mark,m;
7606 struct list_head *tmp;
7607 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007608 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007609 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007610 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007611 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612
7613 /* just incase thread restarts... */
7614 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7615 return;
NeilBrown3991b312014-05-28 13:39:23 +10007616 if (mddev->ro) {/* never try to sync a read-only array */
7617 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007618 return;
NeilBrown3991b312014-05-28 13:39:23 +10007619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620
NeilBrown61df9d92006-10-03 01:15:57 -07007621 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007622 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007623 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007624 action = "check";
7625 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007626 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007627 action = "repair";
7628 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007629 desc = "resync";
7630 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7631 desc = "reshape";
7632 else
7633 desc = "recovery";
7634
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007635 mddev->last_sync_action = action ?: desc;
7636
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637 /* we overload curr_resync somewhat here.
7638 * 0 == not engaged in resync at all
7639 * 2 == checking that there is no conflict with another sync
7640 * 1 == like 2, but have yielded to allow conflicting resync to
7641 * commense
7642 * other == active in resync - this many blocks
7643 *
7644 * Before starting a resync we must have set curr_resync to
7645 * 2, and then checked that every "conflicting" array has curr_resync
7646 * less than ours. When we find one that is the same or higher
7647 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7648 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7649 * This will mean we have to start checking from the beginning again.
7650 *
7651 */
7652
7653 do {
7654 mddev->curr_resync = 2;
7655
7656 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007657 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007659 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660 if (mddev2 == mddev)
7661 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007662 if (!mddev->parallel_resync
7663 && mddev2->curr_resync
7664 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665 DEFINE_WAIT(wq);
7666 if (mddev < mddev2 && mddev->curr_resync == 2) {
7667 /* arbitrarily yield */
7668 mddev->curr_resync = 1;
7669 wake_up(&resync_wait);
7670 }
7671 if (mddev > mddev2 && mddev->curr_resync == 1)
7672 /* no need to wait here, we can wait the next
7673 * time 'round when curr_resync == 2
7674 */
7675 continue;
NeilBrown97441972008-09-19 11:49:54 +10007676 /* We need to wait 'interruptible' so as not to
7677 * contribute to the load average, and not to
7678 * be caught by 'softlockup'
7679 */
7680 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007681 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007682 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007683 printk(KERN_INFO "md: delaying %s of %s"
7684 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007686 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007687 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007688 if (signal_pending(current))
7689 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690 schedule();
7691 finish_wait(&resync_wait, &wq);
7692 goto try_again;
7693 }
7694 finish_wait(&resync_wait, &wq);
7695 }
7696 }
7697 } while (mddev->curr_resync < 2);
7698
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007699 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007700 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007701 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007702 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007703 */
7704 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007705 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007706 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007707 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7708 j = mddev->resync_min;
7709 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007710 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007711
NeilBrownccfcc3c2006-03-27 01:18:09 -08007712 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007713 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007714 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007716 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007717 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007718 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007719 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007720 if (rdev->raid_disk >= 0 &&
7721 !test_bit(Faulty, &rdev->flags) &&
7722 !test_bit(In_sync, &rdev->flags) &&
7723 rdev->recovery_offset < j)
7724 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007725 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007726
7727 /* If there is a bitmap, we need to make sure all
7728 * writes that started before we added a spare
7729 * complete before we start doing a recovery.
7730 * Otherwise the write might complete and (via
7731 * bitmap_endwrite) set a bit in the bitmap after the
7732 * recovery has checked that bit and skipped that
7733 * region.
7734 */
7735 if (mddev->bitmap) {
7736 mddev->pers->quiesce(mddev, 1);
7737 mddev->pers->quiesce(mddev, 0);
7738 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740
NeilBrown61df9d92006-10-03 01:15:57 -07007741 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7742 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7743 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007744 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007745 "(but not more than %d KB/sec) for %s.\n",
7746 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007747
NeilBrowneea1bf32009-03-31 14:27:02 +11007748 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007749
NeilBrown57afd892005-06-21 17:17:13 -07007750 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751 for (m = 0; m < SYNC_MARKS; m++) {
7752 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007753 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007754 }
7755 last_mark = 0;
7756 mddev->resync_mark = mark[last_mark];
7757 mddev->resync_mark_cnt = mark_cnt[last_mark];
7758
7759 /*
7760 * Tune reconstruction:
7761 */
7762 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007763 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7764 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007765
7766 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007767 last_check = 0;
7768
7769 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007770 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007771 "md: resuming %s of %s from checkpoint.\n",
7772 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007773 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007774 } else
7775 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007776 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007777 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7778 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007779 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007781 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -05007782 md_cluster_ops->resync_start(mddev, j, max_sectors);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007783
majianpeng7c2c57c2012-07-03 12:12:26 +10007784 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007786 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787
NeilBrown57afd892005-06-21 17:17:13 -07007788 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007789
NeilBrown7a91ee12009-05-26 12:57:21 +10007790 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7791 ((mddev->curr_resync > mddev->curr_resync_completed &&
7792 (mddev->curr_resync - mddev->curr_resync_completed)
7793 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007794 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007795 (j - mddev->curr_resync_completed)*2
7796 >= mddev->resync_max - mddev->curr_resync_completed
7797 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007798 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007799 wait_event(mddev->recovery_wait,
7800 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007801 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007802 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7803 j > mddev->recovery_cp)
7804 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007805 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007806 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007807 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007808 }
NeilBrownacb180b2009-04-14 16:28:34 +10007809
NeilBrownc91abf52013-11-19 12:02:01 +11007810 while (j >= mddev->resync_max &&
7811 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007812 /* As this condition is controlled by user-space,
7813 * we can block indefinitely, so use '_interruptible'
7814 * to avoid triggering warnings.
7815 */
7816 flush_signals(current); /* just in case */
7817 wait_event_interruptible(mddev->recovery_wait,
7818 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007819 || test_bit(MD_RECOVERY_INTR,
7820 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007821 }
NeilBrownacb180b2009-04-14 16:28:34 +10007822
NeilBrownc91abf52013-11-19 12:02:01 +11007823 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7824 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007825
NeilBrown09314792015-02-19 16:04:40 +11007826 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07007827 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007828 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007829 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007830 }
NeilBrown57afd892005-06-21 17:17:13 -07007831
7832 if (!skipped) { /* actual IO requested */
7833 io_sectors += sectors;
7834 atomic_add(sectors, &mddev->recovery_active);
7835 }
7836
NeilBrowne875ece2011-07-28 11:39:24 +10007837 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7838 break;
7839
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007841 if (j > 2)
7842 mddev->curr_resync = j;
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007843 if (mddev_is_clustered(mddev))
7844 md_cluster_ops->resync_info_update(mddev, j, max_sectors);
NeilBrownff4e8d92006-07-10 04:44:16 -07007845 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007846 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007847 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007848 * visible in /proc/mdstat
7849 */
7850 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007851
7852 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853 continue;
7854
NeilBrown57afd892005-06-21 17:17:13 -07007855 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856 repeat:
7857 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7858 /* step marks */
7859 int next = (last_mark+1) % SYNC_MARKS;
7860
7861 mddev->resync_mark = mark[next];
7862 mddev->resync_mark_cnt = mark_cnt[next];
7863 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007864 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865 last_mark = next;
7866 }
7867
NeilBrownc91abf52013-11-19 12:02:01 +11007868 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7869 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870
7871 /*
7872 * this loop exits only if either when we are slower than
7873 * the 'hard' speed limit, or the system was IO-idle for
7874 * a jiffy.
7875 * the system might be non-idle CPU-wise, but we only care
7876 * about not overloading the IO subsystem. (things like an
7877 * e2fsck being done on the RAID array should execute fast)
7878 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879 cond_resched();
7880
Xiao Niac7e50a2014-08-07 09:37:41 -04007881 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7882 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07007883 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007884
NeilBrown88202a02006-01-06 00:21:36 -08007885 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11007886 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007887 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888 goto repeat;
7889 }
NeilBrownac8fa412015-02-19 16:55:00 +11007890 if (!is_mddev_idle(mddev, 0)) {
7891 /*
7892 * Give other IO more of a chance.
7893 * The faster the devices, the less we wait.
7894 */
7895 wait_event(mddev->recovery_wait,
7896 !atomic_read(&mddev->recovery_active));
7897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898 }
7899 }
NeilBrownc91abf52013-11-19 12:02:01 +11007900 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7901 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7902 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903 /*
7904 * this also signals 'finished resyncing' to md_stop
7905 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007906 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007907 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7908
7909 /* tell personality that we are finished */
NeilBrown09314792015-02-19 16:04:40 +11007910 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007912 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -05007913 md_cluster_ops->resync_finish(mddev);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007914
NeilBrowndfc70642008-05-23 13:04:39 -07007915 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007916 mddev->curr_resync > 2) {
7917 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7918 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7919 if (mddev->curr_resync >= mddev->recovery_cp) {
7920 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007921 "md: checkpointing %s of %s.\n",
7922 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007923 if (test_bit(MD_RECOVERY_ERROR,
7924 &mddev->recovery))
7925 mddev->recovery_cp =
7926 mddev->curr_resync_completed;
7927 else
7928 mddev->recovery_cp =
7929 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007930 }
7931 } else
7932 mddev->recovery_cp = MaxSector;
7933 } else {
7934 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7935 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007936 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007937 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007938 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007939 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007940 !test_bit(Faulty, &rdev->flags) &&
7941 !test_bit(In_sync, &rdev->flags) &&
7942 rdev->recovery_offset < mddev->curr_resync)
7943 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007944 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007946 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007947 skip:
NeilBrown17571282006-12-10 02:20:52 -08007948 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949
NeilBrown23da4222014-12-15 12:57:01 +11007950 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11007951 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7952 /* We completed so min/max setting can be forgotten if used. */
7953 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7954 mddev->resync_min = 0;
7955 mddev->resync_max = MaxSector;
7956 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7957 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007958 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11007959 spin_unlock(&mddev->lock);
7960
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961 wake_up(&resync_wait);
7962 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7963 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007964 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965}
NeilBrown29269552006-03-27 01:18:10 -08007966EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007967
NeilBrown746d3202013-04-24 11:42:41 +10007968static int remove_and_add_spares(struct mddev *mddev,
7969 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007970{
NeilBrown3cb03002011-10-11 16:45:26 +11007971 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007972 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007973 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007974
NeilBrowndafb20f2012-03-19 12:46:39 +11007975 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007976 if ((this == NULL || rdev == this) &&
7977 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007978 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007979 (test_bit(Faulty, &rdev->flags) ||
7980 ! test_bit(In_sync, &rdev->flags)) &&
7981 atomic_read(&rdev->nr_pending)==0) {
7982 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007983 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007984 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007985 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007986 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007987 }
7988 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007989 if (removed && mddev->kobj.sd)
7990 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007991
NeilBrown746d3202013-04-24 11:42:41 +10007992 if (this)
7993 goto no_add;
7994
NeilBrowndafb20f2012-03-19 12:46:39 +11007995 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007996 if (rdev->raid_disk >= 0 &&
7997 !test_bit(In_sync, &rdev->flags) &&
7998 !test_bit(Faulty, &rdev->flags))
7999 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008000 if (rdev->raid_disk >= 0)
8001 continue;
8002 if (test_bit(Faulty, &rdev->flags))
8003 continue;
8004 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11008005 ! (rdev->saved_raid_disk >= 0 &&
8006 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10008007 continue;
8008
NeilBrown7eb41882014-01-14 15:55:14 +11008009 if (rdev->saved_raid_disk < 0)
8010 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008011 if (mddev->pers->
8012 hot_add_disk(mddev, rdev) == 0) {
8013 if (sysfs_link_rdev(mddev, rdev))
8014 /* failure here is OK */;
8015 spares++;
8016 md_new_event(mddev);
8017 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008018 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008019 }
NeilBrown746d3202013-04-24 11:42:41 +10008020no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008021 if (removed)
8022 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008023 return spares;
8024}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008025
NeilBrownac05f252014-09-30 08:10:42 +10008026static void md_start_sync(struct work_struct *ws)
8027{
8028 struct mddev *mddev = container_of(ws, struct mddev, del_work);
8029
8030 mddev->sync_thread = md_register_thread(md_do_sync,
8031 mddev,
8032 "resync");
8033 if (!mddev->sync_thread) {
8034 printk(KERN_ERR "%s: could not start resync"
8035 " thread...\n",
8036 mdname(mddev));
8037 /* leave the spares where they are, it shouldn't hurt */
8038 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8039 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8040 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8041 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8042 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008043 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008044 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8045 &mddev->recovery))
8046 if (mddev->sysfs_action)
8047 sysfs_notify_dirent_safe(mddev->sysfs_action);
8048 } else
8049 md_wakeup_thread(mddev->sync_thread);
8050 sysfs_notify_dirent_safe(mddev->sysfs_action);
8051 md_new_event(mddev);
8052}
8053
Linus Torvalds1da177e2005-04-16 15:20:36 -07008054/*
8055 * This routine is regularly called by all per-raid-array threads to
8056 * deal with generic issues like resync and super-block update.
8057 * Raid personalities that don't have a thread (linear/raid0) do not
8058 * need this as they never do any recovery or update the superblock.
8059 *
8060 * It does not do any resync itself, but rather "forks" off other threads
8061 * to do that as needed.
8062 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8063 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008064 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008065 * and wakeups up this thread which will reap the thread and finish up.
8066 * This thread also removes any faulty devices (with nr_pending == 0).
8067 *
8068 * The overall approach is:
8069 * 1/ if the superblock needs updating, update it.
8070 * 2/ If a recovery thread is running, don't do anything else.
8071 * 3/ If recovery has finished, clean up, possibly marking spares active.
8072 * 4/ If there are any faulty devices, remove them.
8073 * 5/ If array is degraded, try to add spares devices
8074 * 6/ If array has spares or is not in-sync, start a resync thread.
8075 */
NeilBrownfd01b882011-10-11 16:47:53 +11008076void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008077{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008078 if (mddev->suspended)
8079 return;
8080
NeilBrown5f404022005-06-21 17:17:16 -07008081 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008082 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008083
NeilBrownfca4d842005-06-21 17:17:11 -07008084 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008085 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008086 printk(KERN_INFO "md: %s in immediate safe mode\n",
8087 mdname(mddev));
8088 mddev->safemode = 2;
8089 }
8090 flush_signals(current);
8091 }
8092
NeilBrownc89a8ee2008-08-05 15:54:13 +10008093 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8094 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008095 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008096 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008097 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008098 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008099 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008100 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8101 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008102 ))
8103 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008104
NeilBrowndf5b89b2006-03-27 01:18:20 -08008105 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008106 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008107
NeilBrownc89a8ee2008-08-05 15:54:13 +10008108 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10008109 /* On a read-only array we can:
8110 * - remove failed devices
8111 * - add already-in_sync devices if the array itself
8112 * is in-sync.
8113 * As we only add devices that are already in-sync,
8114 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008115 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008116 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008117 /* There is no thread, but we need to call
8118 * ->spare_active and clear saved_raid_disk
8119 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008120 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008121 md_reap_sync_thread(mddev);
8122 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008123 goto unlock;
8124 }
8125
NeilBrown31a59e32008-04-30 00:52:30 -07008126 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008127 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008128 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008129 if (mddev->safemode &&
8130 !atomic_read(&mddev->writes_pending) &&
8131 !mddev->in_sync &&
8132 mddev->recovery_cp == MaxSector) {
8133 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008134 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008135 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008136 }
8137 if (mddev->safemode == 1)
8138 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008139 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008140 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008141 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008142 }
NeilBrownfca4d842005-06-21 17:17:11 -07008143
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008144 if (mddev->flags & MD_UPDATE_SB_FLAGS) {
8145 if (mddev_is_clustered(mddev))
8146 md_cluster_ops->metadata_update_start(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07008147 md_update_sb(mddev, 0);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008148 if (mddev_is_clustered(mddev))
8149 md_cluster_ops->metadata_update_finish(mddev);
8150 }
NeilBrown06d91a52005-06-21 17:17:12 -07008151
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8153 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8154 /* resync/recovery still happening */
8155 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8156 goto unlock;
8157 }
8158 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008159 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008160 goto unlock;
8161 }
Neil Brown72a23c22008-06-28 08:31:41 +10008162 /* Set RUNNING before clearing NEEDED to avoid
8163 * any transients in the value of "sync_action".
8164 */
NeilBrown72f36d52012-10-11 14:25:57 +11008165 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008166 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008167 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008168 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008169 /* Clear some bits that don't mean anything, but
8170 * might be left set
8171 */
NeilBrown24dd4692005-11-08 21:39:26 -08008172 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8173 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008174
NeilBrowned209582012-04-24 10:23:14 +10008175 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8176 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008177 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178 /* no recovery is running.
8179 * remove any failed drives, then
8180 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008181 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008182 * the personality to fail the re-add.
8183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008184
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008185 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008186 if (mddev->pers->check_reshape == NULL ||
8187 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008188 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008189 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008190 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008191 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008192 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008193 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8194 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008195 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008196 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008197 } else if (mddev->recovery_cp < MaxSector) {
8198 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008199 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008200 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8201 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008202 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008203
Linus Torvalds1da177e2005-04-16 15:20:36 -07008204 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008205 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008206 /* We are adding a device or devices to an array
8207 * which has the bitmap stored on all devices.
8208 * So make sure all bitmap pages get written
8209 */
8210 bitmap_write_all(mddev->bitmap);
8211 }
NeilBrownac05f252014-09-30 08:10:42 +10008212 INIT_WORK(&mddev->del_work, md_start_sync);
8213 queue_work(md_misc_wq, &mddev->del_work);
8214 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215 }
NeilBrownac05f252014-09-30 08:10:42 +10008216 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008217 if (!mddev->sync_thread) {
8218 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008219 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008220 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8221 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008222 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008223 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008224 }
NeilBrownac05f252014-09-30 08:10:42 +10008225 unlock:
8226 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227 mddev_unlock(mddev);
8228 }
8229}
NeilBrown6c144d32014-09-30 16:15:38 +10008230EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008231
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008232void md_reap_sync_thread(struct mddev *mddev)
8233{
8234 struct md_rdev *rdev;
8235
8236 /* resync has finished, collect result */
8237 md_unregister_thread(&mddev->sync_thread);
8238 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8239 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8240 /* success...*/
8241 /* activate any spares */
8242 if (mddev->pers->spare_active(mddev)) {
8243 sysfs_notify(&mddev->kobj, NULL,
8244 "degraded");
8245 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8246 }
8247 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008248 if (mddev_is_clustered(mddev))
8249 md_cluster_ops->metadata_update_start(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008250 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8251 mddev->pers->finish_reshape)
8252 mddev->pers->finish_reshape(mddev);
8253
8254 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008255 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008256 */
NeilBrownf4667222013-12-09 12:04:56 +11008257 if (!mddev->degraded)
8258 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008259 rdev->saved_raid_disk = -1;
8260
8261 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008262 if (mddev_is_clustered(mddev))
8263 md_cluster_ops->metadata_update_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008264 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8265 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8266 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8267 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8268 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008269 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008270 /* flag recovery needed just to double check */
8271 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8272 sysfs_notify_dirent_safe(mddev->sysfs_action);
8273 md_new_event(mddev);
8274 if (mddev->event_work.func)
8275 queue_work(md_misc_wq, &mddev->event_work);
8276}
NeilBrown6c144d32014-09-30 16:15:38 +10008277EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008278
NeilBrownfd01b882011-10-11 16:47:53 +11008279void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008280{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008281 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008282 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008283 !test_bit(Blocked, &rdev->flags) &&
8284 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008285 msecs_to_jiffies(5000));
8286 rdev_dec_pending(rdev, mddev);
8287}
8288EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8289
NeilBrownc6563a82012-05-21 09:27:00 +10008290void md_finish_reshape(struct mddev *mddev)
8291{
8292 /* called be personality module when reshape completes. */
8293 struct md_rdev *rdev;
8294
8295 rdev_for_each(rdev, mddev) {
8296 if (rdev->data_offset > rdev->new_data_offset)
8297 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8298 else
8299 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8300 rdev->data_offset = rdev->new_data_offset;
8301 }
8302}
8303EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008304
8305/* Bad block management.
8306 * We can record which blocks on each device are 'bad' and so just
8307 * fail those blocks, or that stripe, rather than the whole device.
8308 * Entries in the bad-block table are 64bits wide. This comprises:
8309 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8310 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8311 * A 'shift' can be set so that larger blocks are tracked and
8312 * consequently larger devices can be covered.
8313 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8314 *
8315 * Locking of the bad-block table uses a seqlock so md_is_badblock
8316 * might need to retry if it is very unlucky.
8317 * We will sometimes want to check for bad blocks in a bi_end_io function,
8318 * so we use the write_seqlock_irq variant.
8319 *
8320 * When looking for a bad block we specify a range and want to
8321 * know if any block in the range is bad. So we binary-search
8322 * to the last range that starts at-or-before the given endpoint,
8323 * (or "before the sector after the target range")
8324 * then see if it ends after the given start.
8325 * We return
8326 * 0 if there are no known bad blocks in the range
8327 * 1 if there are known bad block which are all acknowledged
8328 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8329 * plus the start/length of the first bad section we overlap.
8330 */
8331int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8332 sector_t *first_bad, int *bad_sectors)
8333{
8334 int hi;
majianpengab056132012-11-06 17:13:44 +08008335 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008336 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008337 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008338 sector_t target = s + sectors;
8339 unsigned seq;
8340
8341 if (bb->shift > 0) {
8342 /* round the start down, and the end up */
8343 s >>= bb->shift;
8344 target += (1<<bb->shift) - 1;
8345 target >>= bb->shift;
8346 sectors = target - s;
8347 }
8348 /* 'target' is now the first block after the bad range */
8349
8350retry:
8351 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008352 lo = 0;
8353 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008354 hi = bb->count;
8355
8356 /* Binary search between lo and hi for 'target'
8357 * i.e. for the last range that starts before 'target'
8358 */
8359 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8360 * are known not to be the last range before target.
8361 * VARIANT: hi-lo is the number of possible
8362 * ranges, and decreases until it reaches 1
8363 */
8364 while (hi - lo > 1) {
8365 int mid = (lo + hi) / 2;
8366 sector_t a = BB_OFFSET(p[mid]);
8367 if (a < target)
8368 /* This could still be the one, earlier ranges
8369 * could not. */
8370 lo = mid;
8371 else
8372 /* This and later ranges are definitely out. */
8373 hi = mid;
8374 }
8375 /* 'lo' might be the last that started before target, but 'hi' isn't */
8376 if (hi > lo) {
8377 /* need to check all range that end after 's' to see if
8378 * any are unacknowledged.
8379 */
8380 while (lo >= 0 &&
8381 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8382 if (BB_OFFSET(p[lo]) < target) {
8383 /* starts before the end, and finishes after
8384 * the start, so they must overlap
8385 */
8386 if (rv != -1 && BB_ACK(p[lo]))
8387 rv = 1;
8388 else
8389 rv = -1;
8390 *first_bad = BB_OFFSET(p[lo]);
8391 *bad_sectors = BB_LEN(p[lo]);
8392 }
8393 lo--;
8394 }
8395 }
8396
8397 if (read_seqretry(&bb->lock, seq))
8398 goto retry;
8399
8400 return rv;
8401}
8402EXPORT_SYMBOL_GPL(md_is_badblock);
8403
8404/*
8405 * Add a range of bad blocks to the table.
8406 * This might extend the table, or might contract it
8407 * if two adjacent ranges can be merged.
8408 * We binary-search to find the 'insertion' point, then
8409 * decide how best to handle it.
8410 */
8411static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8412 int acknowledged)
8413{
8414 u64 *p;
8415 int lo, hi;
8416 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008417 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008418
8419 if (bb->shift < 0)
8420 /* badblocks are disabled */
8421 return 0;
8422
8423 if (bb->shift) {
8424 /* round the start down, and the end up */
8425 sector_t next = s + sectors;
8426 s >>= bb->shift;
8427 next += (1<<bb->shift) - 1;
8428 next >>= bb->shift;
8429 sectors = next - s;
8430 }
8431
Bian Yu905b0292013-10-12 01:10:03 -04008432 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008433
8434 p = bb->page;
8435 lo = 0;
8436 hi = bb->count;
8437 /* Find the last range that starts at-or-before 's' */
8438 while (hi - lo > 1) {
8439 int mid = (lo + hi) / 2;
8440 sector_t a = BB_OFFSET(p[mid]);
8441 if (a <= s)
8442 lo = mid;
8443 else
8444 hi = mid;
8445 }
8446 if (hi > lo && BB_OFFSET(p[lo]) > s)
8447 hi = lo;
8448
8449 if (hi > lo) {
8450 /* we found a range that might merge with the start
8451 * of our new range
8452 */
8453 sector_t a = BB_OFFSET(p[lo]);
8454 sector_t e = a + BB_LEN(p[lo]);
8455 int ack = BB_ACK(p[lo]);
8456 if (e >= s) {
8457 /* Yes, we can merge with a previous range */
8458 if (s == a && s + sectors >= e)
8459 /* new range covers old */
8460 ack = acknowledged;
8461 else
8462 ack = ack && acknowledged;
8463
8464 if (e < s + sectors)
8465 e = s + sectors;
8466 if (e - a <= BB_MAX_LEN) {
8467 p[lo] = BB_MAKE(a, e-a, ack);
8468 s = e;
8469 } else {
8470 /* does not all fit in one range,
8471 * make p[lo] maximal
8472 */
8473 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8474 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8475 s = a + BB_MAX_LEN;
8476 }
8477 sectors = e - s;
8478 }
8479 }
8480 if (sectors && hi < bb->count) {
8481 /* 'hi' points to the first range that starts after 's'.
8482 * Maybe we can merge with the start of that range */
8483 sector_t a = BB_OFFSET(p[hi]);
8484 sector_t e = a + BB_LEN(p[hi]);
8485 int ack = BB_ACK(p[hi]);
8486 if (a <= s + sectors) {
8487 /* merging is possible */
8488 if (e <= s + sectors) {
8489 /* full overlap */
8490 e = s + sectors;
8491 ack = acknowledged;
8492 } else
8493 ack = ack && acknowledged;
8494
8495 a = s;
8496 if (e - a <= BB_MAX_LEN) {
8497 p[hi] = BB_MAKE(a, e-a, ack);
8498 s = e;
8499 } else {
8500 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8501 s = a + BB_MAX_LEN;
8502 }
8503 sectors = e - s;
8504 lo = hi;
8505 hi++;
8506 }
8507 }
8508 if (sectors == 0 && hi < bb->count) {
8509 /* we might be able to combine lo and hi */
8510 /* Note: 's' is at the end of 'lo' */
8511 sector_t a = BB_OFFSET(p[hi]);
8512 int lolen = BB_LEN(p[lo]);
8513 int hilen = BB_LEN(p[hi]);
8514 int newlen = lolen + hilen - (s - a);
8515 if (s >= a && newlen < BB_MAX_LEN) {
8516 /* yes, we can combine them */
8517 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8518 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8519 memmove(p + hi, p + hi + 1,
8520 (bb->count - hi - 1) * 8);
8521 bb->count--;
8522 }
8523 }
8524 while (sectors) {
8525 /* didn't merge (it all).
8526 * Need to add a range just before 'hi' */
8527 if (bb->count >= MD_MAX_BADBLOCKS) {
8528 /* No room for more */
8529 rv = 0;
8530 break;
8531 } else {
8532 int this_sectors = sectors;
8533 memmove(p + hi + 1, p + hi,
8534 (bb->count - hi) * 8);
8535 bb->count++;
8536
8537 if (this_sectors > BB_MAX_LEN)
8538 this_sectors = BB_MAX_LEN;
8539 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8540 sectors -= this_sectors;
8541 s += this_sectors;
8542 }
8543 }
8544
8545 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008546 if (!acknowledged)
8547 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008548 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008549
8550 return rv;
8551}
8552
NeilBrown3cb03002011-10-11 16:45:26 +11008553int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008554 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008555{
NeilBrownc6563a82012-05-21 09:27:00 +10008556 int rv;
8557 if (is_new)
8558 s += rdev->new_data_offset;
8559 else
8560 s += rdev->data_offset;
8561 rv = md_set_badblocks(&rdev->badblocks,
8562 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008563 if (rv) {
8564 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008565 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008566 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8567 md_wakeup_thread(rdev->mddev->thread);
8568 }
8569 return rv;
8570}
8571EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8572
8573/*
8574 * Remove a range of bad blocks from the table.
8575 * This may involve extending the table if we spilt a region,
8576 * but it must not fail. So if the table becomes full, we just
8577 * drop the remove request.
8578 */
8579static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8580{
8581 u64 *p;
8582 int lo, hi;
8583 sector_t target = s + sectors;
8584 int rv = 0;
8585
8586 if (bb->shift > 0) {
8587 /* When clearing we round the start up and the end down.
8588 * This should not matter as the shift should align with
8589 * the block size and no rounding should ever be needed.
8590 * However it is better the think a block is bad when it
8591 * isn't than to think a block is not bad when it is.
8592 */
8593 s += (1<<bb->shift) - 1;
8594 s >>= bb->shift;
8595 target >>= bb->shift;
8596 sectors = target - s;
8597 }
8598
8599 write_seqlock_irq(&bb->lock);
8600
8601 p = bb->page;
8602 lo = 0;
8603 hi = bb->count;
8604 /* Find the last range that starts before 'target' */
8605 while (hi - lo > 1) {
8606 int mid = (lo + hi) / 2;
8607 sector_t a = BB_OFFSET(p[mid]);
8608 if (a < target)
8609 lo = mid;
8610 else
8611 hi = mid;
8612 }
8613 if (hi > lo) {
8614 /* p[lo] is the last range that could overlap the
8615 * current range. Earlier ranges could also overlap,
8616 * but only this one can overlap the end of the range.
8617 */
8618 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8619 /* Partial overlap, leave the tail of this range */
8620 int ack = BB_ACK(p[lo]);
8621 sector_t a = BB_OFFSET(p[lo]);
8622 sector_t end = a + BB_LEN(p[lo]);
8623
8624 if (a < s) {
8625 /* we need to split this range */
8626 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008627 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008628 goto out;
8629 }
8630 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8631 bb->count++;
8632 p[lo] = BB_MAKE(a, s-a, ack);
8633 lo++;
8634 }
8635 p[lo] = BB_MAKE(target, end - target, ack);
8636 /* there is no longer an overlap */
8637 hi = lo;
8638 lo--;
8639 }
8640 while (lo >= 0 &&
8641 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8642 /* This range does overlap */
8643 if (BB_OFFSET(p[lo]) < s) {
8644 /* Keep the early parts of this range. */
8645 int ack = BB_ACK(p[lo]);
8646 sector_t start = BB_OFFSET(p[lo]);
8647 p[lo] = BB_MAKE(start, s - start, ack);
8648 /* now low doesn't overlap, so.. */
8649 break;
8650 }
8651 lo--;
8652 }
8653 /* 'lo' is strictly before, 'hi' is strictly after,
8654 * anything between needs to be discarded
8655 */
8656 if (hi - lo > 1) {
8657 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8658 bb->count -= (hi - lo - 1);
8659 }
8660 }
8661
8662 bb->changed = 1;
8663out:
8664 write_sequnlock_irq(&bb->lock);
8665 return rv;
8666}
8667
NeilBrownc6563a82012-05-21 09:27:00 +10008668int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8669 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008670{
NeilBrownc6563a82012-05-21 09:27:00 +10008671 if (is_new)
8672 s += rdev->new_data_offset;
8673 else
8674 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008675 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008676 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008677}
8678EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8679
8680/*
8681 * Acknowledge all bad blocks in a list.
8682 * This only succeeds if ->changed is clear. It is used by
8683 * in-kernel metadata updates
8684 */
8685void md_ack_all_badblocks(struct badblocks *bb)
8686{
8687 if (bb->page == NULL || bb->changed)
8688 /* no point even trying */
8689 return;
8690 write_seqlock_irq(&bb->lock);
8691
majianpengecb178b2012-03-19 12:46:42 +11008692 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008693 u64 *p = bb->page;
8694 int i;
8695 for (i = 0; i < bb->count ; i++) {
8696 if (!BB_ACK(p[i])) {
8697 sector_t start = BB_OFFSET(p[i]);
8698 int len = BB_LEN(p[i]);
8699 p[i] = BB_MAKE(start, len, 1);
8700 }
8701 }
NeilBrownde393cd2011-07-28 11:31:48 +10008702 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008703 }
8704 write_sequnlock_irq(&bb->lock);
8705}
8706EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8707
NeilBrown16c791a2011-07-28 11:31:47 +10008708/* sysfs access to bad-blocks list.
8709 * We present two files.
8710 * 'bad-blocks' lists sector numbers and lengths of ranges that
8711 * are recorded as bad. The list is truncated to fit within
8712 * the one-page limit of sysfs.
8713 * Writing "sector length" to this file adds an acknowledged
8714 * bad block list.
8715 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8716 * been acknowledged. Writing to this file adds bad blocks
8717 * without acknowledging them. This is largely for testing.
8718 */
8719
8720static ssize_t
8721badblocks_show(struct badblocks *bb, char *page, int unack)
8722{
8723 size_t len;
8724 int i;
8725 u64 *p = bb->page;
8726 unsigned seq;
8727
8728 if (bb->shift < 0)
8729 return 0;
8730
8731retry:
8732 seq = read_seqbegin(&bb->lock);
8733
8734 len = 0;
8735 i = 0;
8736
8737 while (len < PAGE_SIZE && i < bb->count) {
8738 sector_t s = BB_OFFSET(p[i]);
8739 unsigned int length = BB_LEN(p[i]);
8740 int ack = BB_ACK(p[i]);
8741 i++;
8742
8743 if (unack && ack)
8744 continue;
8745
8746 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8747 (unsigned long long)s << bb->shift,
8748 length << bb->shift);
8749 }
NeilBrownde393cd2011-07-28 11:31:48 +10008750 if (unack && len == 0)
8751 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008752
8753 if (read_seqretry(&bb->lock, seq))
8754 goto retry;
8755
8756 return len;
8757}
8758
8759#define DO_DEBUG 1
8760
8761static ssize_t
8762badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8763{
8764 unsigned long long sector;
8765 int length;
8766 char newline;
8767#ifdef DO_DEBUG
8768 /* Allow clearing via sysfs *only* for testing/debugging.
8769 * Normally only a successful write may clear a badblock
8770 */
8771 int clear = 0;
8772 if (page[0] == '-') {
8773 clear = 1;
8774 page++;
8775 }
8776#endif /* DO_DEBUG */
8777
8778 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8779 case 3:
8780 if (newline != '\n')
8781 return -EINVAL;
8782 case 2:
8783 if (length <= 0)
8784 return -EINVAL;
8785 break;
8786 default:
8787 return -EINVAL;
8788 }
8789
8790#ifdef DO_DEBUG
8791 if (clear) {
8792 md_clear_badblocks(bb, sector, length);
8793 return len;
8794 }
8795#endif /* DO_DEBUG */
8796 if (md_set_badblocks(bb, sector, length, !unack))
8797 return len;
8798 else
8799 return -ENOSPC;
8800}
8801
Adrian Bunk75c96f82005-05-05 16:16:09 -07008802static int md_notify_reboot(struct notifier_block *this,
8803 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008804{
8805 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008806 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008807 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008808
NeilBrownc744a652012-03-19 12:46:37 +11008809 for_each_mddev(mddev, tmp) {
8810 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008811 if (mddev->pers)
8812 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008813 if (mddev->persistent)
8814 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008815 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008816 }
NeilBrownc744a652012-03-19 12:46:37 +11008817 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008818 }
NeilBrownc744a652012-03-19 12:46:37 +11008819 /*
8820 * certain more exotic SCSI devices are known to be
8821 * volatile wrt too early system reboots. While the
8822 * right place to handle this issue is the given
8823 * driver, we do want to have a safe RAID driver ...
8824 */
8825 if (need_delay)
8826 mdelay(1000*1);
8827
Linus Torvalds1da177e2005-04-16 15:20:36 -07008828 return NOTIFY_DONE;
8829}
8830
Adrian Bunk75c96f82005-05-05 16:16:09 -07008831static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008832 .notifier_call = md_notify_reboot,
8833 .next = NULL,
8834 .priority = INT_MAX, /* before any real devices */
8835};
8836
8837static void md_geninit(void)
8838{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008839 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008840
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008841 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008842}
8843
Adrian Bunk75c96f82005-05-05 16:16:09 -07008844static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008845{
Tejun Heoe804ac72010-10-15 15:36:08 +02008846 int ret = -ENOMEM;
8847
Tejun Heoada609e2011-01-25 14:35:54 +01008848 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008849 if (!md_wq)
8850 goto err_wq;
8851
8852 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8853 if (!md_misc_wq)
8854 goto err_misc_wq;
8855
8856 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8857 goto err_md;
8858
8859 if ((ret = register_blkdev(0, "mdp")) < 0)
8860 goto err_mdp;
8861 mdp_major = ret;
8862
NeilBrownaf5628f2014-07-31 13:54:54 +10008863 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008864 md_probe, NULL, NULL);
8865 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008866 md_probe, NULL, NULL);
8867
Linus Torvalds1da177e2005-04-16 15:20:36 -07008868 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008869 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008870
8871 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873
Tejun Heoe804ac72010-10-15 15:36:08 +02008874err_mdp:
8875 unregister_blkdev(MD_MAJOR, "md");
8876err_md:
8877 destroy_workqueue(md_misc_wq);
8878err_misc_wq:
8879 destroy_workqueue(md_wq);
8880err_wq:
8881 return ret;
8882}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008883
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008884void md_reload_sb(struct mddev *mddev)
8885{
8886 struct md_rdev *rdev, *tmp;
8887
8888 rdev_for_each_safe(rdev, tmp, mddev) {
8889 rdev->sb_loaded = 0;
8890 ClearPageUptodate(rdev->sb_page);
8891 }
8892 mddev->raid_disks = 0;
8893 analyze_sbs(mddev);
8894 rdev_for_each_safe(rdev, tmp, mddev) {
8895 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8896 /* since we don't write to faulty devices, we figure out if the
8897 * disk is faulty by comparing events
8898 */
8899 if (mddev->events > sb->events)
8900 set_bit(Faulty, &rdev->flags);
8901 }
8902
8903}
8904EXPORT_SYMBOL(md_reload_sb);
8905
Linus Torvalds1da177e2005-04-16 15:20:36 -07008906#ifndef MODULE
8907
8908/*
8909 * Searches all registered partitions for autorun RAID arrays
8910 * at boot time.
8911 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008912
8913static LIST_HEAD(all_detected_devices);
8914struct detected_devices_node {
8915 struct list_head list;
8916 dev_t dev;
8917};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008918
8919void md_autodetect_dev(dev_t dev)
8920{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008921 struct detected_devices_node *node_detected_dev;
8922
8923 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8924 if (node_detected_dev) {
8925 node_detected_dev->dev = dev;
8926 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8927 } else {
8928 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8929 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008931}
8932
Linus Torvalds1da177e2005-04-16 15:20:36 -07008933static void autostart_arrays(int part)
8934{
NeilBrown3cb03002011-10-11 16:45:26 +11008935 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008936 struct detected_devices_node *node_detected_dev;
8937 dev_t dev;
8938 int i_scanned, i_passed;
8939
8940 i_scanned = 0;
8941 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008942
8943 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8944
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008945 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8946 i_scanned++;
8947 node_detected_dev = list_entry(all_detected_devices.next,
8948 struct detected_devices_node, list);
8949 list_del(&node_detected_dev->list);
8950 dev = node_detected_dev->dev;
8951 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008952 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008953 if (IS_ERR(rdev))
8954 continue;
8955
NeilBrown403df472014-09-30 15:52:29 +10008956 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008957 continue;
NeilBrown403df472014-09-30 15:52:29 +10008958
NeilBrownd0fae182008-03-04 14:29:31 -08008959 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008960 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008961 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008962 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008963
8964 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8965 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008966
8967 autorun_devices(part);
8968}
8969
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008970#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008971
8972static __exit void md_exit(void)
8973{
NeilBrownfd01b882011-10-11 16:47:53 +11008974 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008975 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008976 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008977
NeilBrownaf5628f2014-07-31 13:54:54 +10008978 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008979 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008980
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008981 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008982 unregister_blkdev(mdp_major, "mdp");
8983 unregister_reboot_notifier(&md_notifier);
8984 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008985
8986 /* We cannot unload the modules while some process is
8987 * waiting for us in select() or poll() - wake them up
8988 */
8989 md_unloading = 1;
8990 while (waitqueue_active(&md_event_waiters)) {
8991 /* not safe to leave yet */
8992 wake_up(&md_event_waiters);
8993 msleep(delay);
8994 delay += delay;
8995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008996 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008997
NeilBrown29ac4aa2008-02-06 01:39:58 -08008998 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008999 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11009000 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009001 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009002 destroy_workqueue(md_misc_wq);
9003 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009004}
9005
Dan Williams685784a2007-07-09 11:56:42 -07009006subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009007module_exit(md_exit)
9008
NeilBrownf91de922005-11-08 21:39:36 -08009009static int get_ro(char *buffer, struct kernel_param *kp)
9010{
9011 return sprintf(buffer, "%d", start_readonly);
9012}
9013static int set_ro(const char *val, struct kernel_param *kp)
9014{
9015 char *e;
9016 int num = simple_strtoul(val, &e, 10);
9017 if (*val && (*e == '\0' || *e == '\n')) {
9018 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08009019 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08009020 }
9021 return -EINVAL;
9022}
9023
NeilBrown80ca3a42006-07-10 04:44:18 -07009024module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9025module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009026module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009027
Linus Torvalds1da177e2005-04-16 15:20:36 -07009028MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009029MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009030MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009031MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);