blob: a7e9fae6c6390c64e0e329df396552b30ca2cbe4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown01f96c02011-09-21 15:30:20 +100061/* pers_list is a list of registered personalities protected
62 * by pers_lock.
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
65 */
NeilBrown2604b702006-01-06 00:20:36 -080066static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_SPINLOCK(pers_lock);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069static void md_print_devices(void);
70
Bernd Schubert90b08712008-05-23 13:04:38 -070071static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020072static struct workqueue_struct *md_wq;
73static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070074
NeilBrown746d3202013-04-24 11:42:41 +100075static int remove_and_add_spares(struct mddev *mddev,
76 struct md_rdev *this);
77
Adrian Bunk5e563412006-06-26 00:27:42 -070078#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
Robert Becker1e509152009-12-14 12:49:58 +110081 * Default number of read corrections we'll attempt on an rdev
82 * before ejecting it from the array. We divide the read error
83 * count by 2 for every hour elapsed between read errors.
84 */
85#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
86/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
88 * is 1000 KB/sec, so the extra system load does not show up that much.
89 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070090 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * subsystem is idle. There is also an 'absolute maximum' reconstruction
92 * speed limit - in case reconstruction slows down your system despite
93 * idle IO detection.
94 *
95 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080096 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98
99static int sysctl_speed_limit_min = 1000;
100static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100101static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800102{
103 return mddev->sync_speed_min ?
104 mddev->sync_speed_min : sysctl_speed_limit_min;
105}
106
NeilBrownfd01b882011-10-11 16:47:53 +1100107static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800108{
109 return mddev->sync_speed_max ?
110 mddev->sync_speed_max : sysctl_speed_limit_max;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113static struct ctl_table_header *raid_table_header;
114
Joe Perches82592c32013-11-14 15:16:18 +1100115static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .procname = "speed_limit_min",
118 .data = &sysctl_speed_limit_min,
119 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700120 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800121 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 },
123 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .procname = "speed_limit_max",
125 .data = &sysctl_speed_limit_max,
126 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700127 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800128 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800130 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Joe Perches82592c32013-11-14 15:16:18 +1100133static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .procname = "raid",
136 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700137 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .child = raid_table,
139 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800140 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
Joe Perches82592c32013-11-14 15:16:18 +1100143static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .procname = "dev",
146 .maxlen = 0,
147 .mode = 0555,
148 .child = raid_dir_table,
149 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800150 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700153static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
NeilBrownf91de922005-11-08 21:39:36 -0800155static int start_readonly;
156
NeilBrowna167f662010-10-26 18:31:13 +1100157/* bio_clone_mddev
158 * like bio_clone, but with a local bio set
159 */
160
NeilBrowna167f662010-10-26 18:31:13 +1100161struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100162 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100163{
164 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100165
166 if (!mddev || !mddev->bio_set)
167 return bio_alloc(gfp_mask, nr_iovecs);
168
Kent Overstreet395c72a2012-09-06 15:34:55 -0700169 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100170 if (!b)
171 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100172 return b;
173}
174EXPORT_SYMBOL_GPL(bio_alloc_mddev);
175
176struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100177 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100178{
NeilBrowna167f662010-10-26 18:31:13 +1100179 if (!mddev || !mddev->bio_set)
180 return bio_clone(bio, gfp_mask);
181
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700182 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100183}
184EXPORT_SYMBOL_GPL(bio_clone_mddev);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
NeilBrownd7603b72006-01-06 00:20:30 -0800187 * We have a system wide 'event count' that is incremented
188 * on any 'interesting' event, and readers of /proc/mdstat
189 * can use 'poll' or 'select' to find out when the event
190 * count increases.
191 *
192 * Events are:
193 * start array, stop array, error, add device, remove device,
194 * start build, activate spare
195 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800196static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800197static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100198void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800199{
200 atomic_inc(&md_event_count);
201 wake_up(&md_event_waiters);
202}
NeilBrown29269552006-03-27 01:18:10 -0800203EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800204
NeilBrownc331eb02006-05-30 21:27:13 -0700205/* Alternate version that can be called from interrupts
206 * when calling sysfs_notify isn't needed.
207 */
NeilBrownfd01b882011-10-11 16:47:53 +1100208static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700209{
210 atomic_inc(&md_event_count);
211 wake_up(&md_event_waiters);
212}
213
NeilBrownd7603b72006-01-06 00:20:30 -0800214/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * Enables to iterate over all existing md arrays
216 * all_mddevs_lock protects this list.
217 */
218static LIST_HEAD(all_mddevs);
219static DEFINE_SPINLOCK(all_mddevs_lock);
220
221
222/*
223 * iterates through all used mddevs in the system.
224 * We take care to grab the all_mddevs_lock whenever navigating
225 * the list, and to always hold a refcount when unlocked.
226 * Any code which breaks out of this loop while own
227 * a reference to the current mddev and must mddev_put it.
228 */
NeilBrownfd01b882011-10-11 16:47:53 +1100229#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 \
231 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100232 _tmp = all_mddevs.next; \
233 _mddev = NULL;}); \
234 ({ if (_tmp != &all_mddevs) \
235 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100237 if (_mddev) mddev_put(_mddev); \
238 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
239 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100241 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 )
243
244
NeilBrown409c57f2009-03-31 14:39:39 +1100245/* Rather than calling directly into the personality make_request function,
246 * IO requests come here first so that we can check if the device is
247 * being suspended pending a reconfiguration.
248 * We hold a refcount over the call to ->make_request. By the time that
249 * call has finished, the bio has been linked into some internal structure
250 * and so is visible to ->quiesce(), so we don't need the refcount any more.
251 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200252static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
NeilBrown49077322010-03-25 16:20:56 +1100254 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100255 struct mddev *mddev = q->queuedata;
NeilBrown49077322010-03-25 16:20:56 +1100256 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500257 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100258
NeilBrown0ca69882011-01-14 09:14:33 +1100259 if (mddev == NULL || mddev->pers == NULL
260 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100261 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200262 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100263 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100264 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
265 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
266 return;
267 }
NeilBrown0ca69882011-01-14 09:14:33 +1100268 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100269 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200270 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100271 DEFINE_WAIT(__wait);
272 for (;;) {
273 prepare_to_wait(&mddev->sb_wait, &__wait,
274 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200275 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100276 break;
277 rcu_read_unlock();
278 schedule();
279 rcu_read_lock();
280 }
281 finish_wait(&mddev->sb_wait, &__wait);
282 }
283 atomic_inc(&mddev->active_io);
284 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100285
Chris Masone91ece52011-02-07 19:21:48 -0500286 /*
287 * save the sectors now since our bio can
288 * go away inside make_request
289 */
290 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200291 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100292
293 cpu = part_stat_lock();
294 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500295 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100296 part_stat_unlock();
297
NeilBrown409c57f2009-03-31 14:39:39 +1100298 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
299 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
NeilBrown9e35b992010-04-06 14:23:02 +1000302/* mddev_suspend makes sure no new requests are submitted
303 * to the device, and that any requests that have been submitted
304 * are completely handled.
305 * Once ->stop is called and completes, the module will be completely
306 * unused.
307 */
NeilBrownfd01b882011-10-11 16:47:53 +1100308void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100309{
310 BUG_ON(mddev->suspended);
311 mddev->suspended = 1;
312 synchronize_rcu();
313 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
314 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500315
316 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100317}
NeilBrown390ee602010-06-01 19:37:27 +1000318EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100319
NeilBrownfd01b882011-10-11 16:47:53 +1100320void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100321{
322 mddev->suspended = 0;
323 wake_up(&mddev->sb_wait);
324 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500325
Jonathan Brassow47525e52012-05-22 13:55:29 +1000326 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500327 md_wakeup_thread(mddev->thread);
328 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100329}
NeilBrown390ee602010-06-01 19:37:27 +1000330EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100331
NeilBrownfd01b882011-10-11 16:47:53 +1100332int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000333{
334 return mddev->suspended;
335}
336EXPORT_SYMBOL(mddev_congested);
337
NeilBrowna2826aa2009-12-14 12:49:49 +1100338/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200339 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100340 */
341
Tejun Heoe9c74692010-09-03 11:56:18 +0200342static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100343{
NeilBrown3cb03002011-10-11 16:45:26 +1100344 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100345 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100346
347 rdev_dec_pending(rdev, mddev);
348
349 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200350 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200351 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100352 }
353 bio_put(bio);
354}
355
NeilBrowna7a07e62010-12-09 16:04:25 +1100356static void md_submit_flush_data(struct work_struct *ws);
357
NeilBrowna035fc32010-12-09 16:17:51 +1100358static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100359{
NeilBrownfd01b882011-10-11 16:47:53 +1100360 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100361 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100362
NeilBrowna7a07e62010-12-09 16:04:25 +1100363 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
364 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100365 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100366 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100367 if (rdev->raid_disk >= 0 &&
368 !test_bit(Faulty, &rdev->flags)) {
369 /* Take two references, one is dropped
370 * when request finishes, one after
371 * we reclaim rcu_read_lock
372 */
373 struct bio *bi;
374 atomic_inc(&rdev->nr_pending);
375 atomic_inc(&rdev->nr_pending);
376 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000377 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200378 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100379 bi->bi_private = rdev;
380 bi->bi_bdev = rdev->bdev;
381 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200382 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100383 rcu_read_lock();
384 rdev_dec_pending(rdev, mddev);
385 }
386 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100387 if (atomic_dec_and_test(&mddev->flush_pending))
388 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100389}
390
Tejun Heoe9c74692010-09-03 11:56:18 +0200391static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100392{
NeilBrownfd01b882011-10-11 16:47:53 +1100393 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200394 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100395
Kent Overstreet4f024f32013-10-11 15:44:27 -0700396 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100397 /* an empty barrier - all done */
398 bio_endio(bio, 0);
399 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200400 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200401 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100402 }
NeilBrown2b74e122010-12-09 15:59:01 +1100403
404 mddev->flush_bio = NULL;
405 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100406}
407
NeilBrownfd01b882011-10-11 16:47:53 +1100408void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100409{
410 spin_lock_irq(&mddev->write_lock);
411 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200412 !mddev->flush_bio,
Lukas Czernereed8c022012-11-30 11:42:40 +0100413 mddev->write_lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200414 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100415 spin_unlock_irq(&mddev->write_lock);
416
NeilBrowna035fc32010-12-09 16:17:51 +1100417 INIT_WORK(&mddev->flush_work, submit_flushes);
418 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100419}
Tejun Heoe9c74692010-09-03 11:56:18 +0200420EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100421
NeilBrown74018dc2012-07-31 09:08:15 +0200422void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000423{
NeilBrown9cbb1752012-07-31 09:08:14 +0200424 struct mddev *mddev = cb->data;
425 md_wakeup_thread(mddev->thread);
426 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000427}
NeilBrown9cbb1752012-07-31 09:08:14 +0200428EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000429
NeilBrownfd01b882011-10-11 16:47:53 +1100430static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 atomic_inc(&mddev->active);
433 return mddev;
434}
435
Dan Williams5fd3a172009-03-04 00:57:25 -0700436static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100437
NeilBrownfd01b882011-10-11 16:47:53 +1100438static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
NeilBrowna167f662010-10-26 18:31:13 +1100440 struct bio_set *bs = NULL;
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
443 return;
NeilBrownd3374822009-01-09 08:31:10 +1100444 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100445 mddev->ctime == 0 && !mddev->hold_active) {
446 /* Array is not configured at all, and not held active,
447 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100448 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100449 bs = mddev->bio_set;
450 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100451 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200452 /* We did a probe so need to clean up. Call
453 * queue_work inside the spinlock so that
454 * flush_workqueue() after mddev_find will
455 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100456 */
457 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200458 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100459 } else
460 kfree(mddev);
461 }
462 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100463 if (bs)
464 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
NeilBrownfd01b882011-10-11 16:47:53 +1100467void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100468{
469 mutex_init(&mddev->open_mutex);
470 mutex_init(&mddev->reconfig_mutex);
471 mutex_init(&mddev->bitmap_info.mutex);
472 INIT_LIST_HEAD(&mddev->disks);
473 INIT_LIST_HEAD(&mddev->all_mddevs);
474 init_timer(&mddev->safemode_timer);
475 atomic_set(&mddev->active, 1);
476 atomic_set(&mddev->openers, 0);
477 atomic_set(&mddev->active_io, 0);
478 spin_lock_init(&mddev->write_lock);
479 atomic_set(&mddev->flush_pending, 0);
480 init_waitqueue_head(&mddev->sb_wait);
481 init_waitqueue_head(&mddev->recovery_wait);
482 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000483 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500484 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100485 mddev->resync_min = 0;
486 mddev->resync_max = MaxSector;
487 mddev->level = LEVEL_NONE;
488}
NeilBrown390ee602010-06-01 19:37:27 +1000489EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100490
NeilBrownfd01b882011-10-11 16:47:53 +1100491static struct mddev * mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
NeilBrownfd01b882011-10-11 16:47:53 +1100493 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
NeilBrown8f5f02c2011-02-16 13:58:51 +1100495 if (unit && MAJOR(unit) != MD_MAJOR)
496 unit &= ~((1<<MdpMinorShift)-1);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 retry:
499 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
NeilBrownefeb53c2009-01-09 08:31:10 +1100501 if (unit) {
502 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
503 if (mddev->unit == unit) {
504 mddev_get(mddev);
505 spin_unlock(&all_mddevs_lock);
506 kfree(new);
507 return mddev;
508 }
509
510 if (new) {
511 list_add(&new->all_mddevs, &all_mddevs);
512 spin_unlock(&all_mddevs_lock);
513 new->hold_active = UNTIL_IOCTL;
514 return new;
515 }
516 } else if (new) {
517 /* find an unused unit number */
518 static int next_minor = 512;
519 int start = next_minor;
520 int is_free = 0;
521 int dev = 0;
522 while (!is_free) {
523 dev = MKDEV(MD_MAJOR, next_minor);
524 next_minor++;
525 if (next_minor > MINORMASK)
526 next_minor = 0;
527 if (next_minor == start) {
528 /* Oh dear, all in use. */
529 spin_unlock(&all_mddevs_lock);
530 kfree(new);
531 return NULL;
532 }
533
534 is_free = 1;
535 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
536 if (mddev->unit == dev) {
537 is_free = 0;
538 break;
539 }
540 }
541 new->unit = dev;
542 new->md_minor = MINOR(dev);
543 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 list_add(&new->all_mddevs, &all_mddevs);
545 spin_unlock(&all_mddevs_lock);
546 return new;
547 }
548 spin_unlock(&all_mddevs_lock);
549
NeilBrown9ffae0c2006-01-06 00:20:32 -0800550 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!new)
552 return NULL;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 new->unit = unit;
555 if (MAJOR(unit) == MD_MAJOR)
556 new->md_minor = MINOR(unit);
557 else
558 new->md_minor = MINOR(unit) >> MdpMinorShift;
559
NeilBrownfafd7fb2010-04-01 15:55:30 +1100560 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto retry;
563}
564
NeilBrown29f097c2013-11-14 17:54:51 +1100565static inline int __must_check mddev_lock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800567 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
NeilBrown29f097c2013-11-14 17:54:51 +1100570/* Sometimes we need to take the lock in a situation where
571 * failure due to interrupts is not acceptable.
572 */
573static inline void mddev_lock_nointr(struct mddev * mddev)
574{
575 mutex_lock(&mddev->reconfig_mutex);
576}
577
NeilBrownfd01b882011-10-11 16:47:53 +1100578static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100579{
580 return mutex_is_locked(&mddev->reconfig_mutex);
581}
582
NeilBrownfd01b882011-10-11 16:47:53 +1100583static inline int mddev_trylock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800585 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
NeilBrownb6eb1272010-04-15 10:13:47 +1000588static struct attribute_group md_redundancy_group;
589
NeilBrownfd01b882011-10-11 16:47:53 +1100590static void mddev_unlock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
NeilBrowna64c8762010-04-14 17:15:37 +1000592 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000593 /* These cannot be removed under reconfig_mutex as
594 * an access to the files will try to take reconfig_mutex
595 * while holding the file unremovable, which leads to
596 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000597 * So hold set sysfs_active while the remove in happeing,
598 * and anything else which might set ->to_remove or my
599 * otherwise change the sysfs namespace will fail with
600 * -EBUSY if sysfs_active is still set.
601 * We set sysfs_active under reconfig_mutex and elsewhere
602 * test it under the same mutex to ensure its correct value
603 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000604 */
NeilBrowna64c8762010-04-14 17:15:37 +1000605 struct attribute_group *to_remove = mddev->to_remove;
606 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000607 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000608 mutex_unlock(&mddev->reconfig_mutex);
609
NeilBrown00bcb4a2010-06-01 19:37:23 +1000610 if (mddev->kobj.sd) {
611 if (to_remove != &md_redundancy_group)
612 sysfs_remove_group(&mddev->kobj, to_remove);
613 if (mddev->pers == NULL ||
614 mddev->pers->sync_request == NULL) {
615 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
616 if (mddev->sysfs_action)
617 sysfs_put(mddev->sysfs_action);
618 mddev->sysfs_action = NULL;
619 }
NeilBrowna64c8762010-04-14 17:15:37 +1000620 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000621 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000622 } else
623 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Chris Dunlop751e67c2011-10-19 16:48:26 +1100625 /* As we've dropped the mutex we need a spinlock to
626 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000627 */
628 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700629 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000630 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
NeilBrown1ca69c42012-10-11 13:37:33 +1100633static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
634{
635 struct md_rdev *rdev;
636
637 rdev_for_each_rcu(rdev, mddev)
638 if (rdev->desc_nr == nr)
639 return rdev;
640
641 return NULL;
642}
643
644static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
NeilBrown3cb03002011-10-11 16:45:26 +1100646 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
NeilBrowndafb20f2012-03-19 12:46:39 +1100648 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (rdev->bdev->bd_dev == dev)
650 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return NULL;
653}
654
NeilBrown1ca69c42012-10-11 13:37:33 +1100655static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
656{
657 struct md_rdev *rdev;
658
659 rdev_for_each_rcu(rdev, mddev)
660 if (rdev->bdev->bd_dev == dev)
661 return rdev;
662
663 return NULL;
664}
665
NeilBrown84fc4b52011-10-11 16:49:58 +1100666static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800667{
NeilBrown84fc4b52011-10-11 16:49:58 +1100668 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800669 list_for_each_entry(pers, &pers_list, list) {
670 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800671 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800672 if (strcmp(pers->name, clevel)==0)
673 return pers;
674 }
NeilBrown2604b702006-01-06 00:20:36 -0800675 return NULL;
676}
677
Andre Nollb73df2d2008-07-11 22:02:23 +1000678/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100679static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100681 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000682 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
NeilBrown3cb03002011-10-11 16:45:26 +1100685static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 if (rdev->sb_page)
688 MD_BUG();
689
690 rdev->sb_page = alloc_page(GFP_KERNEL);
691 if (!rdev->sb_page) {
692 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000693 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695
696 return 0;
697}
698
NeilBrown545c8792012-05-22 13:54:30 +1000699void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800702 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 rdev->sb_loaded = 0;
704 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000705 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100706 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
NeilBrown2699b672011-07-28 11:31:47 +1000708 if (rdev->bb_page) {
709 put_page(rdev->bb_page);
710 rdev->bb_page = NULL;
711 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000712 kfree(rdev->badblocks.page);
713 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
NeilBrown545c8792012-05-22 13:54:30 +1000715EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
NeilBrown6712ecf2007-09-27 12:47:43 +0200717static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700718{
NeilBrown3cb03002011-10-11 16:45:26 +1100719 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100720 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700721
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700722 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
723 printk("md: super_written gets error=%d, uptodate=%d\n",
724 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
725 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800726 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700727 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700728
NeilBrowna9701a32005-11-08 21:39:34 -0800729 if (atomic_dec_and_test(&mddev->pending_writes))
730 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700731 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700732}
733
NeilBrownfd01b882011-10-11 16:47:53 +1100734void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700735 sector_t sector, int size, struct page *page)
736{
737 /* write first size bytes of page to sector of rdev
738 * Increment mddev->pending_writes before returning
739 * and decrement it on completion, waking up sb_wait
740 * if zero is reached.
741 * If an error occurred, call md_error
742 */
NeilBrowna167f662010-10-26 18:31:13 +1100743 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700744
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100745 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700746 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700747 bio_add_page(bio, page, size, 0);
748 bio->bi_private = rdev;
749 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800750
NeilBrown7bfa19f2005-06-21 17:17:28 -0700751 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000752 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800753}
754
NeilBrownfd01b882011-10-11 16:47:53 +1100755void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800756{
Tejun Heoe9c74692010-09-03 11:56:18 +0200757 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000758 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700759}
760
NeilBrown3cb03002011-10-11 16:45:26 +1100761int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100762 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
NeilBrowna167f662010-10-26 18:31:13 +1100764 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 int ret;
766
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100767 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
768 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100769 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700770 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000771 else if (rdev->mddev->reshape_position != MaxSector &&
772 (rdev->mddev->reshape_backwards ==
773 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700774 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100775 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700776 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700778 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
781 bio_put(bio);
782 return ret;
783}
NeilBrowna8745db2006-01-06 00:20:34 -0800784EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
NeilBrown3cb03002011-10-11 16:45:26 +1100786static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 char b[BDEVNAME_SIZE];
789 if (!rdev->sb_page) {
790 MD_BUG();
791 return -EINVAL;
792 }
793 if (rdev->sb_loaded)
794 return 0;
795
796
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100797 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto fail;
799 rdev->sb_loaded = 1;
800 return 0;
801
802fail:
803 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
804 bdevname(rdev->bdev,b));
805 return -EINVAL;
806}
807
808static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
809{
Andre Noll05710462008-07-11 22:02:20 +1000810 return sb1->set_uuid0 == sb2->set_uuid0 &&
811 sb1->set_uuid1 == sb2->set_uuid1 &&
812 sb1->set_uuid2 == sb2->set_uuid2 &&
813 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
817{
818 int ret;
819 mdp_super_t *tmp1, *tmp2;
820
821 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
822 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
823
824 if (!tmp1 || !tmp2) {
825 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100826 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 goto abort;
828 }
829
830 *tmp1 = *sb1;
831 *tmp2 = *sb2;
832
833 /*
834 * nr_disks is not constant
835 */
836 tmp1->nr_disks = 0;
837 tmp2->nr_disks = 0;
838
Andre Nollce0c8e02008-07-11 22:02:20 +1000839 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700841 kfree(tmp1);
842 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return ret;
844}
845
NeilBrown4d167f02007-05-09 02:35:37 -0700846
847static u32 md_csum_fold(u32 csum)
848{
849 csum = (csum & 0xffff) + (csum >> 16);
850 return (csum & 0xffff) + (csum >> 16);
851}
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853static unsigned int calc_sb_csum(mdp_super_t * sb)
854{
NeilBrown4d167f02007-05-09 02:35:37 -0700855 u64 newcsum = 0;
856 u32 *sb32 = (u32*)sb;
857 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 unsigned int disk_csum, csum;
859
860 disk_csum = sb->sb_csum;
861 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700862
863 for (i = 0; i < MD_SB_BYTES/4 ; i++)
864 newcsum += sb32[i];
865 csum = (newcsum & 0xffffffff) + (newcsum>>32);
866
867
868#ifdef CONFIG_ALPHA
869 /* This used to use csum_partial, which was wrong for several
870 * reasons including that different results are returned on
871 * different architectures. It isn't critical that we get exactly
872 * the same return value as before (we always csum_fold before
873 * testing, and that removes any differences). However as we
874 * know that csum_partial always returned a 16bit value on
875 * alphas, do a fold to maximise conformity to previous behaviour.
876 */
877 sb->sb_csum = md_csum_fold(disk_csum);
878#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700880#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return csum;
882}
883
884
885/*
886 * Handle superblock details.
887 * We want to be able to handle multiple superblock formats
888 * so we have a common interface to them all, and an array of
889 * different handlers.
890 * We rely on user-space to write the initial superblock, and support
891 * reading and updating of superblocks.
892 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100893 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 * loads and validates a superblock on dev.
895 * if refdev != NULL, compare superblocks on both devices
896 * Return:
897 * 0 - dev has a superblock that is compatible with refdev
898 * 1 - dev has a superblock that is compatible and newer than refdev
899 * so dev should be used as the refdev in future
900 * -EINVAL superblock incompatible or invalid
901 * -othererror e.g. -EIO
902 *
NeilBrownfd01b882011-10-11 16:47:53 +1100903 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 * Verify that dev is acceptable into mddev.
905 * The first time, mddev->raid_disks will be 0, and data from
906 * dev should be merged in. Subsequent calls check that dev
907 * is new enough. Return 0 or -EINVAL
908 *
NeilBrownfd01b882011-10-11 16:47:53 +1100909 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * Update the superblock for rdev with data in mddev
911 * This does not write to disc.
912 *
913 */
914
915struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000916 char *name;
917 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000918 int (*load_super)(struct md_rdev *rdev,
919 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000920 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000921 int (*validate_super)(struct mddev *mddev,
922 struct md_rdev *rdev);
923 void (*sync_super)(struct mddev *mddev,
924 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100925 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000926 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000927 int (*allow_new_offset)(struct md_rdev *rdev,
928 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929};
930
931/*
Andre Noll0894cc32009-06-18 08:49:23 +1000932 * Check that the given mddev has no bitmap.
933 *
934 * This function is called from the run method of all personalities that do not
935 * support bitmaps. It prints an error message and returns non-zero if mddev
936 * has a bitmap. Otherwise, it returns 0.
937 *
938 */
NeilBrownfd01b882011-10-11 16:47:53 +1100939int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000940{
NeilBrownc3d97142009-12-14 12:49:52 +1100941 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000942 return 0;
943 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
944 mdname(mddev), mddev->pers->name);
945 return 1;
946}
947EXPORT_SYMBOL(md_check_no_bitmap);
948
949/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 * load_super for 0.90.0
951 */
NeilBrown3cb03002011-10-11 16:45:26 +1100952static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
955 mdp_super_t *sb;
956 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 /*
Andre Noll0f420352008-07-11 22:02:23 +1000959 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 * it's at the end of the disk.
961 *
962 * It also happens to be a multiple of 4Kb.
963 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100964 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
NeilBrown0002b272005-09-09 16:23:53 -0700966 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (ret) return ret;
968
969 ret = -EINVAL;
970
971 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000972 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 if (sb->md_magic != MD_SB_MAGIC) {
975 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
976 b);
977 goto abort;
978 }
979
980 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800981 sb->minor_version < 90 ||
982 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
984 sb->major_version, sb->minor_version,
985 b);
986 goto abort;
987 }
988
989 if (sb->raid_disks <= 0)
990 goto abort;
991
NeilBrown4d167f02007-05-09 02:35:37 -0700992 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
994 b);
995 goto abort;
996 }
997
998 rdev->preferred_minor = sb->md_minor;
999 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001000 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001001 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001002 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 if (sb->level == LEVEL_MULTIPATH)
1005 rdev->desc_nr = -1;
1006 else
1007 rdev->desc_nr = sb->this_disk.number;
1008
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001009 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001011 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001013 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (!uuid_equal(refsb, sb)) {
1015 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1016 b, bdevname(refdev->bdev,b2));
1017 goto abort;
1018 }
1019 if (!sb_equal(refsb, sb)) {
1020 printk(KERN_WARNING "md: %s has same UUID"
1021 " but different superblock to %s\n",
1022 b, bdevname(refdev->bdev, b2));
1023 goto abort;
1024 }
1025 ev1 = md_event(sb);
1026 ev2 = md_event(refsb);
1027 if (ev1 > ev2)
1028 ret = 1;
1029 else
1030 ret = 0;
1031 }
NeilBrown8190e752009-06-18 08:48:58 +10001032 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001033 /* Limit to 4TB as metadata cannot record more than that.
1034 * (not needed for Linear and RAID0 as metadata doesn't
1035 * record this size)
1036 */
1037 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001038 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
NeilBrown27a7b262011-09-10 17:21:28 +10001040 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001041 /* "this cannot possibly happen" ... */
1042 ret = -EINVAL;
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 abort:
1045 return ret;
1046}
1047
1048/*
1049 * validate_super for 0.90.0
1050 */
NeilBrownfd01b882011-10-11 16:47:53 +11001051static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
1053 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001054 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001055 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
NeilBrown41158c72005-06-21 17:17:25 -07001057 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001058 clear_bit(Faulty, &rdev->flags);
1059 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001060 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001061 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (mddev->raid_disks == 0) {
1064 mddev->major_version = 0;
1065 mddev->minor_version = sb->minor_version;
1066 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001067 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001068 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 mddev->ctime = sb->ctime;
1070 mddev->utime = sb->utime;
1071 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001072 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 mddev->layout = sb->layout;
1074 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001075 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001076 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001077 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001078 mddev->bitmap_info.space = 0;
1079 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001080 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001081 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001082 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
NeilBrownf6705572006-03-27 01:18:11 -08001084 if (mddev->minor_version >= 91) {
1085 mddev->reshape_position = sb->reshape_position;
1086 mddev->delta_disks = sb->delta_disks;
1087 mddev->new_level = sb->new_level;
1088 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001089 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001090 if (mddev->delta_disks < 0)
1091 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001092 } else {
1093 mddev->reshape_position = MaxSector;
1094 mddev->delta_disks = 0;
1095 mddev->new_level = mddev->level;
1096 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001097 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001098 }
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (sb->state & (1<<MD_SB_CLEAN))
1101 mddev->recovery_cp = MaxSector;
1102 else {
1103 if (sb->events_hi == sb->cp_events_hi &&
1104 sb->events_lo == sb->cp_events_lo) {
1105 mddev->recovery_cp = sb->recovery_cp;
1106 } else
1107 mddev->recovery_cp = 0;
1108 }
1109
1110 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1111 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1112 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1113 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1114
1115 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001116
1117 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001118 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001119 mddev->bitmap_info.offset =
1120 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001121 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001122 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001123 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001124
NeilBrown41158c72005-06-21 17:17:25 -07001125 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001126 /* Insist on good event counter while assembling, except
1127 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001129 if (sb->disks[rdev->desc_nr].state & (
1130 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1131 if (ev1 < mddev->events)
1132 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001133 } else if (mddev->bitmap) {
1134 /* if adding to array with a bitmap, then we can accept an
1135 * older device ... but not too old.
1136 */
NeilBrown41158c72005-06-21 17:17:25 -07001137 if (ev1 < mddev->bitmap->events_cleared)
1138 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001139 if (ev1 < mddev->events)
1140 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001141 } else {
1142 if (ev1 < mddev->events)
1143 /* just a hot-add of a new device, leave raid_disk at -1 */
1144 return 0;
1145 }
NeilBrown41158c72005-06-21 17:17:25 -07001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 desc = sb->disks + rdev->desc_nr;
1149
1150 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001151 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001152 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1153 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001154 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001156 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001157 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1158 /* active but not in sync implies recovery up to
1159 * reshape position. We don't know exactly where
1160 * that is, so set to zero for now */
1161 if (mddev->minor_version >= 91) {
1162 rdev->recovery_offset = 0;
1163 rdev->raid_disk = desc->raid_disk;
1164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001166 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1167 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001168 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001169 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return 0;
1171}
1172
1173/*
1174 * sync_super for 0.90.0
1175 */
NeilBrownfd01b882011-10-11 16:47:53 +11001176static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001179 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 /* make rdev->sb match mddev data..
1184 *
1185 * 1/ zero out disks
1186 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1187 * 3/ any empty disks < next_spare become removed
1188 *
1189 * disks[0] gets initialised to REMOVED because
1190 * we cannot be sure from other fields if it has
1191 * been initialised or not.
1192 */
1193 int i;
1194 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1195
NeilBrown61181562005-09-09 16:24:02 -07001196 rdev->sb_size = MD_SB_BYTES;
1197
Namhyung Kim65a06f062011-07-27 11:00:36 +10001198 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 memset(sb, 0, sizeof(*sb));
1201
1202 sb->md_magic = MD_SB_MAGIC;
1203 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 sb->patch_version = mddev->patch_version;
1205 sb->gvalid_words = 0; /* ignored */
1206 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1207 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1208 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1209 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1210
1211 sb->ctime = mddev->ctime;
1212 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001213 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 sb->raid_disks = mddev->raid_disks;
1215 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001216 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 sb->utime = mddev->utime;
1218 sb->state = 0;
1219 sb->events_hi = (mddev->events>>32);
1220 sb->events_lo = (u32)mddev->events;
1221
NeilBrownf6705572006-03-27 01:18:11 -08001222 if (mddev->reshape_position == MaxSector)
1223 sb->minor_version = 90;
1224 else {
1225 sb->minor_version = 91;
1226 sb->reshape_position = mddev->reshape_position;
1227 sb->new_level = mddev->new_level;
1228 sb->delta_disks = mddev->delta_disks;
1229 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001230 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001231 }
1232 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (mddev->in_sync)
1234 {
1235 sb->recovery_cp = mddev->recovery_cp;
1236 sb->cp_events_hi = (mddev->events>>32);
1237 sb->cp_events_lo = (u32)mddev->events;
1238 if (mddev->recovery_cp == MaxSector)
1239 sb->state = (1<< MD_SB_CLEAN);
1240 } else
1241 sb->recovery_cp = 0;
1242
1243 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001244 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
NeilBrownc3d97142009-12-14 12:49:52 +11001246 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001247 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001250 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001252 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001253 int is_active = test_bit(In_sync, &rdev2->flags);
1254
1255 if (rdev2->raid_disk >= 0 &&
1256 sb->minor_version >= 91)
1257 /* we have nowhere to store the recovery_offset,
1258 * but if it is not below the reshape_position,
1259 * we can piggy-back on that.
1260 */
1261 is_active = 1;
1262 if (rdev2->raid_disk < 0 ||
1263 test_bit(Faulty, &rdev2->flags))
1264 is_active = 0;
1265 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001266 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001268 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001269 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 d = &sb->disks[rdev2->desc_nr];
1271 nr_disks++;
1272 d->number = rdev2->desc_nr;
1273 d->major = MAJOR(rdev2->bdev->bd_dev);
1274 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001275 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 d->raid_disk = rdev2->raid_disk;
1277 else
1278 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001279 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001281 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001283 if (test_bit(In_sync, &rdev2->flags))
1284 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 active++;
1286 working++;
1287 } else {
1288 d->state = 0;
1289 spare++;
1290 working++;
1291 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001292 if (test_bit(WriteMostly, &rdev2->flags))
1293 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 /* now set the "removed" and "faulty" bits on any missing devices */
1296 for (i=0 ; i < mddev->raid_disks ; i++) {
1297 mdp_disk_t *d = &sb->disks[i];
1298 if (d->state == 0 && d->number == 0) {
1299 d->number = i;
1300 d->raid_disk = i;
1301 d->state = (1<<MD_DISK_REMOVED);
1302 d->state |= (1<<MD_DISK_FAULTY);
1303 failed++;
1304 }
1305 }
1306 sb->nr_disks = nr_disks;
1307 sb->active_disks = active;
1308 sb->working_disks = working;
1309 sb->failed_disks = failed;
1310 sb->spare_disks = spare;
1311
1312 sb->this_disk = sb->disks[rdev->desc_nr];
1313 sb->sb_csum = calc_sb_csum(sb);
1314}
1315
1316/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001317 * rdev_size_change for 0.90.0
1318 */
1319static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001320super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001321{
Andre Noll58c0fed2009-03-31 14:33:13 +11001322 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001323 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001324 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001325 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001326 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001327 if (!num_sectors || num_sectors > rdev->sb_start)
1328 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001329 /* Limit to 4TB as metadata cannot record more than that.
1330 * 4TB == 2^32 KB, or 2*2^32 sectors.
1331 */
NeilBrown667a5312012-08-16 16:46:12 +10001332 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001333 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001334 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001335 rdev->sb_page);
1336 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001337 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001338}
1339
NeilBrownc6563a82012-05-21 09:27:00 +10001340static int
1341super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1342{
1343 /* non-zero offset changes not possible with v0.90 */
1344 return new_offset == 0;
1345}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001346
1347/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 * version 1 superblock
1349 */
1350
NeilBrown1c05b4b2006-10-21 10:24:08 -07001351static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001353 __le32 disk_csum;
1354 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 unsigned long long newcsum;
1356 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001357 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 disk_csum = sb->sb_csum;
1360 sb->sb_csum = 0;
1361 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001362 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 newcsum += le32_to_cpu(*isuper++);
1364
1365 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001366 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1369 sb->sb_csum = disk_csum;
1370 return cpu_to_le32(csum);
1371}
1372
NeilBrown2699b672011-07-28 11:31:47 +10001373static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1374 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001375static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 struct mdp_superblock_1 *sb;
1378 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001379 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001380 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001382 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 /*
Andre Noll0f420352008-07-11 22:02:23 +10001385 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * It is always aligned to a 4K boundary and
1387 * depeding on minor_version, it can be:
1388 * 0: At least 8K, but less than 12K, from end of device
1389 * 1: At start of device
1390 * 2: 4K from start of device.
1391 */
1392 switch(minor_version) {
1393 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001394 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001395 sb_start -= 8*2;
1396 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 break;
1398 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001399 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 break;
1401 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001402 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 break;
1404 default:
1405 return -EINVAL;
1406 }
Andre Noll0f420352008-07-11 22:02:23 +10001407 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
NeilBrown0002b272005-09-09 16:23:53 -07001409 /* superblock is rarely larger than 1K, but it can be larger,
1410 * and it is safe to read 4k, so we do that
1411 */
1412 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (ret) return ret;
1414
1415
Namhyung Kim65a06f062011-07-27 11:00:36 +10001416 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1419 sb->major_version != cpu_to_le32(1) ||
1420 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001421 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001422 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return -EINVAL;
1424
1425 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1426 printk("md: invalid superblock checksum on %s\n",
1427 bdevname(rdev->bdev,b));
1428 return -EINVAL;
1429 }
1430 if (le64_to_cpu(sb->data_size) < 10) {
1431 printk("md: data_size too small on %s\n",
1432 bdevname(rdev->bdev,b));
1433 return -EINVAL;
1434 }
NeilBrownc6563a82012-05-21 09:27:00 +10001435 if (sb->pad0 ||
1436 sb->pad3[0] ||
1437 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1438 /* Some padding is non-zero, might be a new feature */
1439 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 rdev->preferred_minor = 0xffff;
1442 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001443 rdev->new_data_offset = rdev->data_offset;
1444 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1445 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1446 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001447 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
NeilBrown0002b272005-09-09 16:23:53 -07001449 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001450 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001451 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001452 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1453
1454 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001455 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001456 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001457 if (minor_version
1458 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1459 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001460
NeilBrown31b65a02006-07-10 04:44:14 -07001461 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1462 rdev->desc_nr = -1;
1463 else
1464 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1465
NeilBrown2699b672011-07-28 11:31:47 +10001466 if (!rdev->bb_page) {
1467 rdev->bb_page = alloc_page(GFP_KERNEL);
1468 if (!rdev->bb_page)
1469 return -ENOMEM;
1470 }
1471 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1472 rdev->badblocks.count == 0) {
1473 /* need to load the bad block list.
1474 * Currently we limit it to one page.
1475 */
1476 s32 offset;
1477 sector_t bb_sector;
1478 u64 *bbp;
1479 int i;
1480 int sectors = le16_to_cpu(sb->bblog_size);
1481 if (sectors > (PAGE_SIZE / 512))
1482 return -EINVAL;
1483 offset = le32_to_cpu(sb->bblog_offset);
1484 if (offset == 0)
1485 return -EINVAL;
1486 bb_sector = (long long)offset;
1487 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1488 rdev->bb_page, READ, true))
1489 return -EIO;
1490 bbp = (u64 *)page_address(rdev->bb_page);
1491 rdev->badblocks.shift = sb->bblog_shift;
1492 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1493 u64 bb = le64_to_cpu(*bbp);
1494 int count = bb & (0x3ff);
1495 u64 sector = bb >> 10;
1496 sector <<= sb->bblog_shift;
1497 count <<= sb->bblog_shift;
1498 if (bb + 1 == 0)
1499 break;
1500 if (md_set_badblocks(&rdev->badblocks,
1501 sector, count, 1) == 0)
1502 return -EINVAL;
1503 }
NeilBrown486adf72013-04-24 11:42:44 +10001504 } else if (sb->bblog_offset != 0)
1505 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001506
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001507 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001508 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001509 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001511 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1514 sb->level != refsb->level ||
1515 sb->layout != refsb->layout ||
1516 sb->chunksize != refsb->chunksize) {
1517 printk(KERN_WARNING "md: %s has strangely different"
1518 " superblock to %s\n",
1519 bdevname(rdev->bdev,b),
1520 bdevname(refdev->bdev,b2));
1521 return -EINVAL;
1522 }
1523 ev1 = le64_to_cpu(sb->events);
1524 ev2 = le64_to_cpu(refsb->events);
1525
1526 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001527 ret = 1;
1528 else
1529 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
NeilBrownc6563a82012-05-21 09:27:00 +10001531 if (minor_version) {
1532 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1533 sectors -= rdev->data_offset;
1534 } else
1535 sectors = rdev->sb_start;
1536 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001538 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001539 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
NeilBrownfd01b882011-10-11 16:47:53 +11001542static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001544 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001545 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
NeilBrown41158c72005-06-21 17:17:25 -07001547 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001548 clear_bit(Faulty, &rdev->flags);
1549 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001550 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001551 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (mddev->raid_disks == 0) {
1554 mddev->major_version = 1;
1555 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001556 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001557 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1559 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1560 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001561 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 mddev->layout = le32_to_cpu(sb->layout);
1563 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001564 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001565 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001566 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001567 mddev->bitmap_info.space = 0;
1568 /* Default location for bitmap is 1K after superblock
1569 * using 3K - total of 4K
1570 */
NeilBrownc3d97142009-12-14 12:49:52 +11001571 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001572 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001573 mddev->reshape_backwards = 0;
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1576 memcpy(mddev->uuid, sb->set_uuid, 16);
1577
1578 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001579
NeilBrown71c08052005-09-09 16:23:51 -07001580 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001581 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001582 mddev->bitmap_info.offset =
1583 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001584 /* Metadata doesn't record how much space is available.
1585 * For 1.0, we assume we can use up to the superblock
1586 * if before, else to 4K beyond superblock.
1587 * For others, assume no change is possible.
1588 */
1589 if (mddev->minor_version > 0)
1590 mddev->bitmap_info.space = 0;
1591 else if (mddev->bitmap_info.offset > 0)
1592 mddev->bitmap_info.space =
1593 8 - mddev->bitmap_info.offset;
1594 else
1595 mddev->bitmap_info.space =
1596 -mddev->bitmap_info.offset;
1597 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001598
NeilBrownf6705572006-03-27 01:18:11 -08001599 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1600 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1601 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1602 mddev->new_level = le32_to_cpu(sb->new_level);
1603 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001604 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001605 if (mddev->delta_disks < 0 ||
1606 (mddev->delta_disks == 0 &&
1607 (le32_to_cpu(sb->feature_map)
1608 & MD_FEATURE_RESHAPE_BACKWARDS)))
1609 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001610 } else {
1611 mddev->reshape_position = MaxSector;
1612 mddev->delta_disks = 0;
1613 mddev->new_level = mddev->level;
1614 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001615 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001616 }
1617
NeilBrown41158c72005-06-21 17:17:25 -07001618 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001619 /* Insist of good event counter while assembling, except for
1620 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001622 if (rdev->desc_nr >= 0 &&
1623 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1624 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1625 if (ev1 < mddev->events)
1626 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001627 } else if (mddev->bitmap) {
1628 /* If adding to array with a bitmap, then we can accept an
1629 * older device, but not too old.
1630 */
NeilBrown41158c72005-06-21 17:17:25 -07001631 if (ev1 < mddev->bitmap->events_cleared)
1632 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001633 if (ev1 < mddev->events)
1634 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001635 } else {
1636 if (ev1 < mddev->events)
1637 /* just a hot-add of a new device, leave raid_disk at -1 */
1638 return 0;
1639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (mddev->level != LEVEL_MULTIPATH) {
1641 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001642 if (rdev->desc_nr < 0 ||
1643 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1644 role = 0xffff;
1645 rdev->desc_nr = -1;
1646 } else
1647 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 switch(role) {
1649 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 break;
1651 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001652 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 break;
1654 default:
NeilBrownf4667222013-12-09 12:04:56 +11001655 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001656 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001657 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001658 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001659 if (!(le32_to_cpu(sb->feature_map) &
1660 MD_FEATURE_RECOVERY_BITMAP))
1661 rdev->saved_raid_disk = -1;
1662 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001663 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 rdev->raid_disk = role;
1665 break;
1666 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001667 if (sb->devflags & WriteMostly1)
1668 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001669 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1670 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001671 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001672 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return 0;
1675}
1676
NeilBrownfd01b882011-10-11 16:47:53 +11001677static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001680 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 int max_dev, i;
1682 /* make rdev->sb match mddev and rdev data. */
1683
Namhyung Kim65a06f062011-07-27 11:00:36 +10001684 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 sb->feature_map = 0;
1687 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001688 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 memset(sb->pad3, 0, sizeof(sb->pad3));
1690
1691 sb->utime = cpu_to_le64((__u64)mddev->utime);
1692 sb->events = cpu_to_le64(mddev->events);
1693 if (mddev->in_sync)
1694 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1695 else
1696 sb->resync_offset = cpu_to_le64(0);
1697
NeilBrown1c05b4b2006-10-21 10:24:08 -07001698 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001699
NeilBrownf0ca3402006-02-02 14:28:04 -08001700 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001701 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001702 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001703 sb->level = cpu_to_le32(mddev->level);
1704 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001705
NeilBrownaeb9b2112011-08-25 14:43:08 +10001706 if (test_bit(WriteMostly, &rdev->flags))
1707 sb->devflags |= WriteMostly1;
1708 else
1709 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001710 sb->data_offset = cpu_to_le64(rdev->data_offset);
1711 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001712
NeilBrownc3d97142009-12-14 12:49:52 +11001713 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1714 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001715 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001716 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001717
1718 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001719 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001720 sb->feature_map |=
1721 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1722 sb->recovery_offset =
1723 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001724 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1725 sb->feature_map |=
1726 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001727 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001728 if (test_bit(Replacement, &rdev->flags))
1729 sb->feature_map |=
1730 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001731
NeilBrownf6705572006-03-27 01:18:11 -08001732 if (mddev->reshape_position != MaxSector) {
1733 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1734 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1735 sb->new_layout = cpu_to_le32(mddev->new_layout);
1736 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1737 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001738 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001739 if (mddev->delta_disks == 0 &&
1740 mddev->reshape_backwards)
1741 sb->feature_map
1742 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001743 if (rdev->new_data_offset != rdev->data_offset) {
1744 sb->feature_map
1745 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1746 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1747 - rdev->data_offset));
1748 }
NeilBrownf6705572006-03-27 01:18:11 -08001749 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001750
NeilBrown2699b672011-07-28 11:31:47 +10001751 if (rdev->badblocks.count == 0)
1752 /* Nothing to do for bad blocks*/ ;
1753 else if (sb->bblog_offset == 0)
1754 /* Cannot record bad blocks on this device */
1755 md_error(mddev, rdev);
1756 else {
1757 struct badblocks *bb = &rdev->badblocks;
1758 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1759 u64 *p = bb->page;
1760 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1761 if (bb->changed) {
1762 unsigned seq;
1763
1764retry:
1765 seq = read_seqbegin(&bb->lock);
1766
1767 memset(bbp, 0xff, PAGE_SIZE);
1768
1769 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001770 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001771 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1772 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001773 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001774 }
NeilBrownd0962932012-03-19 12:46:41 +11001775 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001776 if (read_seqretry(&bb->lock, seq))
1777 goto retry;
1778
1779 bb->sector = (rdev->sb_start +
1780 (int)le32_to_cpu(sb->bblog_offset));
1781 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001782 }
1783 }
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001786 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (rdev2->desc_nr+1 > max_dev)
1788 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001789
NeilBrown70471da2009-08-03 10:59:57 +10001790 if (max_dev > le32_to_cpu(sb->max_dev)) {
1791 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001792 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001793 rdev->sb_size = max_dev * 2 + 256;
1794 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1795 if (rdev->sb_size & bmask)
1796 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001797 } else
1798 max_dev = le32_to_cpu(sb->max_dev);
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 for (i=0; i<max_dev;i++)
1801 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1802
NeilBrowndafb20f2012-03-19 12:46:39 +11001803 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001805 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001807 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001809 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001810 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 else
1812 sb->dev_roles[i] = cpu_to_le16(0xffff);
1813 }
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 sb->sb_csum = calc_sb_1_csum(sb);
1816}
1817
Chris Webb0cd17fe2008-06-28 08:31:46 +10001818static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001819super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001820{
1821 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001822 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001823 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001824 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001825 if (rdev->data_offset != rdev->new_data_offset)
1826 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001827 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001828 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001829 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001830 max_sectors -= rdev->data_offset;
1831 if (!num_sectors || num_sectors > max_sectors)
1832 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001833 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001834 /* minor version 0 with bitmap we can't move */
1835 return 0;
1836 } else {
1837 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001838 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001839 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001840 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001841 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001842 if (!num_sectors || num_sectors > max_sectors)
1843 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001844 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001845 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001846 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001847 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001848 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001849 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001850 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001851 rdev->sb_page);
1852 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001853 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001854
1855}
1856
1857static int
1858super_1_allow_new_offset(struct md_rdev *rdev,
1859 unsigned long long new_offset)
1860{
1861 /* All necessary checks on new >= old have been done */
1862 struct bitmap *bitmap;
1863 if (new_offset >= rdev->data_offset)
1864 return 1;
1865
1866 /* with 1.0 metadata, there is no metadata to tread on
1867 * so we can always move back */
1868 if (rdev->mddev->minor_version == 0)
1869 return 1;
1870
1871 /* otherwise we must be sure not to step on
1872 * any metadata, so stay:
1873 * 36K beyond start of superblock
1874 * beyond end of badblocks
1875 * beyond write-intent bitmap
1876 */
1877 if (rdev->sb_start + (32+4)*2 > new_offset)
1878 return 0;
1879 bitmap = rdev->mddev->bitmap;
1880 if (bitmap && !rdev->mddev->bitmap_info.file &&
1881 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001882 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001883 return 0;
1884 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1885 return 0;
1886
1887 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001888}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Adrian Bunk75c96f82005-05-05 16:16:09 -07001890static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 [0] = {
1892 .name = "0.90.0",
1893 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001894 .load_super = super_90_load,
1895 .validate_super = super_90_validate,
1896 .sync_super = super_90_sync,
1897 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001898 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 },
1900 [1] = {
1901 .name = "md-1",
1902 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001903 .load_super = super_1_load,
1904 .validate_super = super_1_validate,
1905 .sync_super = super_1_sync,
1906 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001907 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 },
1909};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
NeilBrownfd01b882011-10-11 16:47:53 +11001911static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001912{
1913 if (mddev->sync_super) {
1914 mddev->sync_super(mddev, rdev);
1915 return;
1916 }
1917
1918 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1919
1920 super_types[mddev->major_version].sync_super(mddev, rdev);
1921}
1922
NeilBrownfd01b882011-10-11 16:47:53 +11001923static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
NeilBrown3cb03002011-10-11 16:45:26 +11001925 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
NeilBrown4b809912008-07-21 17:05:25 +10001927 rcu_read_lock();
1928 rdev_for_each_rcu(rdev, mddev1)
1929 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001930 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001931 rdev2->bdev->bd_contains) {
1932 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001933 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001934 }
1935 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return 0;
1937}
1938
1939static LIST_HEAD(pending_raid_disks);
1940
Andre Nollac5e7112009-08-03 10:59:47 +10001941/*
1942 * Try to register data integrity profile for an mddev
1943 *
1944 * This is called when an array is started and after a disk has been kicked
1945 * from the array. It only succeeds if all working and active component devices
1946 * are integrity capable with matching profiles.
1947 */
NeilBrownfd01b882011-10-11 16:47:53 +11001948int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001949{
NeilBrown3cb03002011-10-11 16:45:26 +11001950 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001951
Andre Nollac5e7112009-08-03 10:59:47 +10001952 if (list_empty(&mddev->disks))
1953 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001954 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1955 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001956 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001957 /* skip spares and non-functional disks */
1958 if (test_bit(Faulty, &rdev->flags))
1959 continue;
1960 if (rdev->raid_disk < 0)
1961 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001962 if (!reference) {
1963 /* Use the first rdev as the reference */
1964 reference = rdev;
1965 continue;
1966 }
1967 /* does this rdev's profile match the reference profile? */
1968 if (blk_integrity_compare(reference->bdev->bd_disk,
1969 rdev->bdev->bd_disk) < 0)
1970 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001971 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001972 if (!reference || !bdev_get_integrity(reference->bdev))
1973 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001974 /*
1975 * All component devices are integrity capable and have matching
1976 * profiles, register the common profile for the md device.
1977 */
1978 if (blk_integrity_register(mddev->gendisk,
1979 bdev_get_integrity(reference->bdev)) != 0) {
1980 printk(KERN_ERR "md: failed to register integrity for %s\n",
1981 mdname(mddev));
1982 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001983 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001984 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1985 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1986 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1987 mdname(mddev));
1988 return -EINVAL;
1989 }
Andre Nollac5e7112009-08-03 10:59:47 +10001990 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001991}
Andre Nollac5e7112009-08-03 10:59:47 +10001992EXPORT_SYMBOL(md_integrity_register);
1993
1994/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001995void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10001996{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001997 struct blk_integrity *bi_rdev;
1998 struct blk_integrity *bi_mddev;
1999
2000 if (!mddev->gendisk)
2001 return;
2002
2003 bi_rdev = bdev_get_integrity(rdev->bdev);
2004 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002005
2006 if (!bi_mddev) /* nothing to do */
2007 return;
2008 if (rdev->raid_disk < 0) /* skip spares */
2009 return;
2010 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2011 rdev->bdev->bd_disk) >= 0)
2012 return;
2013 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2014 blk_integrity_unregister(mddev->gendisk);
2015}
2016EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002017
NeilBrownfd01b882011-10-11 16:47:53 +11002018static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002020 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002021 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002022 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002023 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 if (rdev->mddev) {
2026 MD_BUG();
2027 return -EINVAL;
2028 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002029
2030 /* 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;
2057 while (find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 choice++;
2059 rdev->desc_nr = choice;
2060 } else {
NeilBrown4878e9e2014-09-25 17:00:11 +10002061 if (find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2062 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
NeilBrown3cb03002011-10-11 16:45:26 +11002108static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
2110 char b[BDEVNAME_SIZE];
2111 if (!rdev->mddev) {
2112 MD_BUG();
2113 return;
2114 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002115 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002116 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2118 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002119 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002120 sysfs_put(rdev->sysfs_state);
2121 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002122 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002123 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002124 * writing to 'remove' to "dev/state". We also need
2125 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002126 */
NeilBrown4b809912008-07-21 17:05:25 +10002127 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002128 INIT_WORK(&rdev->del_work, md_delayed_delete);
2129 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002130 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
2133/*
2134 * prevent the device from being mounted, repartitioned or
2135 * otherwise reused by a RAID array (or any other kernel
2136 * subsystem), by bd_claiming the device.
2137 */
NeilBrown3cb03002011-10-11 16:45:26 +11002138static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140 int err = 0;
2141 struct block_device *bdev;
2142 char b[BDEVNAME_SIZE];
2143
Tejun Heod4d77622010-11-13 11:55:18 +01002144 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002145 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (IS_ERR(bdev)) {
2147 printk(KERN_ERR "md: could not open %s.\n",
2148 __bdevname(dev, b));
2149 return PTR_ERR(bdev);
2150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 rdev->bdev = bdev;
2152 return err;
2153}
2154
NeilBrown3cb03002011-10-11 16:45:26 +11002155static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 struct block_device *bdev = rdev->bdev;
2158 rdev->bdev = NULL;
2159 if (!bdev)
2160 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002161 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
2164void md_autodetect_dev(dev_t dev);
2165
NeilBrown3cb03002011-10-11 16:45:26 +11002166static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
2168 char b[BDEVNAME_SIZE];
2169 printk(KERN_INFO "md: export_rdev(%s)\n",
2170 bdevname(rdev->bdev,b));
2171 if (rdev->mddev)
2172 MD_BUG();
NeilBrown545c8792012-05-22 13:54:30 +10002173 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002175 if (test_bit(AutoDetected, &rdev->flags))
2176 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177#endif
2178 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002179 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
NeilBrown3cb03002011-10-11 16:45:26 +11002182static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
2184 unbind_rdev_from_array(rdev);
2185 export_rdev(rdev);
2186}
2187
NeilBrownfd01b882011-10-11 16:47:53 +11002188static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
NeilBrown0638bb02014-09-25 17:43:47 +10002190 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
NeilBrown0638bb02014-09-25 17:43:47 +10002192 while (!list_empty(&mddev->disks)) {
2193 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2194 same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 kick_rdev_from_array(rdev);
2196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 mddev->raid_disks = 0;
2198 mddev->major_version = 0;
2199}
2200
2201static void print_desc(mdp_disk_t *desc)
2202{
2203 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2204 desc->major,desc->minor,desc->raid_disk,desc->state);
2205}
2206
Cheng Renquancd2ac932009-01-09 08:31:08 +11002207static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
2209 int i;
2210
2211 printk(KERN_INFO
2212 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2213 sb->major_version, sb->minor_version, sb->patch_version,
2214 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2215 sb->ctime);
2216 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2217 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2218 sb->md_minor, sb->layout, sb->chunk_size);
2219 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2220 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2221 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2222 sb->failed_disks, sb->spare_disks,
2223 sb->sb_csum, (unsigned long)sb->events_lo);
2224
2225 printk(KERN_INFO);
2226 for (i = 0; i < MD_SB_DISKS; i++) {
2227 mdp_disk_t *desc;
2228
2229 desc = sb->disks + i;
2230 if (desc->number || desc->major || desc->minor ||
2231 desc->raid_disk || (desc->state && (desc->state != 4))) {
2232 printk(" D %2d: ", i);
2233 print_desc(desc);
2234 }
2235 }
2236 printk(KERN_INFO "md: THIS: ");
2237 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Cheng Renquancd2ac932009-01-09 08:31:08 +11002240static void print_sb_1(struct mdp_superblock_1 *sb)
2241{
2242 __u8 *uuid;
2243
2244 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002245 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002246 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002247 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002248 le32_to_cpu(sb->major_version),
2249 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002250 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002251 sb->set_name,
2252 (unsigned long long)le64_to_cpu(sb->ctime)
2253 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2254
2255 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002256 printk(KERN_INFO
2257 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002258 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002259 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002260 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2261 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002262 le32_to_cpu(sb->level),
2263 (unsigned long long)le64_to_cpu(sb->size),
2264 le32_to_cpu(sb->raid_disks),
2265 le32_to_cpu(sb->layout),
2266 le32_to_cpu(sb->chunksize),
2267 (unsigned long long)le64_to_cpu(sb->data_offset),
2268 (unsigned long long)le64_to_cpu(sb->data_size),
2269 (unsigned long long)le64_to_cpu(sb->super_offset),
2270 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2271 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002272 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002273 sb->devflags,
2274 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2275 (unsigned long long)le64_to_cpu(sb->events),
2276 (unsigned long long)le64_to_cpu(sb->resync_offset),
2277 le32_to_cpu(sb->sb_csum),
2278 le32_to_cpu(sb->max_dev)
2279 );
2280}
2281
NeilBrown3cb03002011-10-11 16:45:26 +11002282static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
2284 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002285 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2286 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002287 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2288 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002290 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2291 switch (major_version) {
2292 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002293 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002294 break;
2295 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002296 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002297 break;
2298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 } else
2300 printk(KERN_INFO "md: no rdev superblock!\n");
2301}
2302
Adrian Bunk5e563412006-06-26 00:27:42 -07002303static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002305 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002306 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002307 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 char b[BDEVNAME_SIZE];
2309
2310 printk("\n");
2311 printk("md: **********************************\n");
2312 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2313 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002314 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
NeilBrown32a76272005-06-21 17:17:14 -07002316 if (mddev->bitmap)
2317 bitmap_print_sb(mddev->bitmap);
2318 else
2319 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002320 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 printk("<%s>", bdevname(rdev->bdev,b));
2322 printk("\n");
2323
NeilBrowndafb20f2012-03-19 12:46:39 +11002324 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002325 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 }
2327 printk("md: **********************************\n");
2328 printk("\n");
2329}
2330
2331
NeilBrownfd01b882011-10-11 16:47:53 +11002332static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
NeilBrown42543762006-06-26 00:27:57 -07002334 /* Update each superblock (in-memory image), but
2335 * if we are allowed to, skip spares which already
2336 * have the right event counter, or have one earlier
2337 * (which would mean they aren't being marked as dirty
2338 * with the rest of the array)
2339 */
NeilBrown3cb03002011-10-11 16:45:26 +11002340 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002341 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002342 if (rdev->sb_events == mddev->events ||
2343 (nospares &&
2344 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002345 rdev->sb_events+1 == mddev->events)) {
2346 /* Don't update this superblock */
2347 rdev->sb_loaded = 2;
2348 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002349 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002350 rdev->sb_loaded = 1;
2351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 }
2353}
2354
NeilBrownfd01b882011-10-11 16:47:53 +11002355static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
NeilBrown3cb03002011-10-11 16:45:26 +11002357 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002358 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002359 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002360 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
NeilBrownd87f0642013-04-24 11:42:40 +10002362 if (mddev->ro) {
2363 if (force_change)
2364 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2365 return;
2366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002368 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002369 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002370 if (rdev->raid_disk >= 0 &&
2371 mddev->delta_disks >= 0 &&
2372 !test_bit(In_sync, &rdev->flags) &&
2373 mddev->curr_resync_completed > rdev->recovery_offset)
2374 rdev->recovery_offset = mddev->curr_resync_completed;
2375
2376 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002377 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002378 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002379 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002380 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002381 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002382 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002383 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002384 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002385 md_ack_all_badblocks(&rdev->badblocks);
2386 md_error(mddev, rdev);
2387 }
2388 clear_bit(Blocked, &rdev->flags);
2389 clear_bit(BlockedBadBlocks, &rdev->flags);
2390 wake_up(&rdev->blocked_wait);
2391 }
2392 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002393 wake_up(&mddev->sb_wait);
2394 return;
2395 }
2396
NeilBrowna9701a32005-11-08 21:39:34 -08002397 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002398
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002399 mddev->utime = get_seconds();
2400
NeilBrown850b2b422006-10-03 01:15:46 -07002401 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2402 force_change = 1;
2403 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2404 /* just a clean<-> dirty transition, possibly leave spares alone,
2405 * though if events isn't the right even/odd, we will have to do
2406 * spares after all
2407 */
2408 nospares = 1;
2409 if (force_change)
2410 nospares = 0;
2411 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002412 /* If the array is degraded, then skipping spares is both
2413 * dangerous and fairly pointless.
2414 * Dangerous because a device that was removed from the array
2415 * might have a event_count that still looks up-to-date,
2416 * so it can be re-added without a resync.
2417 * Pointless because if there are any spares to skip,
2418 * then a recovery will happen and soon that array won't
2419 * be degraded any more and the spare can go back to sleep then.
2420 */
NeilBrown850b2b422006-10-03 01:15:46 -07002421 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002422
NeilBrown06d91a52005-06-21 17:17:12 -07002423 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002424
2425 /* If this is just a dirty<->clean transition, and the array is clean
2426 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002427 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002428 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002429 && mddev->can_decrease_events
2430 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002431 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002432 mddev->can_decrease_events = 0;
2433 } else {
NeilBrown42543762006-06-26 00:27:57 -07002434 /* otherwise we have to go forward and ... */
2435 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002436 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439 if (!mddev->events) {
2440 /*
2441 * oops, this 64-bit counter should never wrap.
2442 * Either we are in around ~1 trillion A.C., assuming
2443 * 1 reboot per second, or we have a bug:
2444 */
2445 MD_BUG();
2446 mddev->events --;
2447 }
NeilBrown2699b672011-07-28 11:31:47 +10002448
NeilBrowndafb20f2012-03-19 12:46:39 +11002449 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002450 if (rdev->badblocks.changed)
2451 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002452 if (test_bit(Faulty, &rdev->flags))
2453 set_bit(FaultRecorded, &rdev->flags);
2454 }
NeilBrown2699b672011-07-28 11:31:47 +10002455
NeilBrowne6910632008-02-06 01:39:51 -08002456 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002457 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
NeilBrown36a4e1f2011-10-07 14:23:17 +11002459 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2460 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
NeilBrown4ad13662007-07-17 04:06:13 -07002462 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002463 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002465
NeilBrown42543762006-06-26 00:27:57 -07002466 if (rdev->sb_loaded != 1)
2467 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
NeilBrownf4667222013-12-09 12:04:56 +11002469 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002470 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002471 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002472 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002473 pr_debug("md: (write) %s's sb offset: %llu\n",
2474 bdevname(rdev->bdev, b),
2475 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002476 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002477 if (rdev->badblocks.size) {
2478 md_super_write(mddev, rdev,
2479 rdev->badblocks.sector,
2480 rdev->badblocks.size << 9,
2481 rdev->bb_page);
2482 rdev->badblocks.size = 0;
2483 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002484
NeilBrownf4667222013-12-09 12:04:56 +11002485 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002486 pr_debug("md: %s (skipping faulty)\n",
2487 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002488
NeilBrown7bfa19f2005-06-21 17:17:28 -07002489 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 /* only need to write one superblock... */
2491 break;
2492 }
NeilBrowna9701a32005-11-08 21:39:34 -08002493 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002494 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002495
NeilBrowna9701a32005-11-08 21:39:34 -08002496 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002497 if (mddev->in_sync != sync_req ||
2498 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002499 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002500 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002501 goto repeat;
2502 }
NeilBrown850b2b422006-10-03 01:15:46 -07002503 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002504 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002505 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002506 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2507 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002508
NeilBrowndafb20f2012-03-19 12:46:39 +11002509 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002510 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2511 clear_bit(Blocked, &rdev->flags);
2512
2513 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002514 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002515 clear_bit(BlockedBadBlocks, &rdev->flags);
2516 wake_up(&rdev->blocked_wait);
2517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
2519
Andre Noll7f6ce762008-03-23 18:34:54 +01002520/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002521 * We want to accept with case. For this we use cmd_match.
2522 */
2523static int cmd_match(const char *cmd, const char *str)
2524{
2525 /* See if cmd, written into a sysfs file, matches
2526 * str. They must either be the same, or cmd can
2527 * have a trailing newline
2528 */
2529 while (*cmd && *str && *cmd == *str) {
2530 cmd++;
2531 str++;
2532 }
2533 if (*cmd == '\n')
2534 cmd++;
2535 if (*str || *cmd)
2536 return 0;
2537 return 1;
2538}
2539
NeilBrown86e6ffd2005-11-08 21:39:24 -08002540struct rdev_sysfs_entry {
2541 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002542 ssize_t (*show)(struct md_rdev *, char *);
2543 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002544};
2545
2546static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002547state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002548{
2549 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002550 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002551
NeilBrownde393cd2011-07-28 11:31:48 +10002552 if (test_bit(Faulty, &rdev->flags) ||
2553 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002554 len+= sprintf(page+len, "%sfaulty",sep);
2555 sep = ",";
2556 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002557 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002558 len += sprintf(page+len, "%sin_sync",sep);
2559 sep = ",";
2560 }
NeilBrownf6556752006-06-26 00:28:01 -07002561 if (test_bit(WriteMostly, &rdev->flags)) {
2562 len += sprintf(page+len, "%swrite_mostly",sep);
2563 sep = ",";
2564 }
NeilBrownde393cd2011-07-28 11:31:48 +10002565 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002566 (rdev->badblocks.unacked_exist
2567 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002568 len += sprintf(page+len, "%sblocked", sep);
2569 sep = ",";
2570 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002571 if (!test_bit(Faulty, &rdev->flags) &&
2572 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002573 len += sprintf(page+len, "%sspare", sep);
2574 sep = ",";
2575 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002576 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2577 len += sprintf(page+len, "%swrite_error", sep);
2578 sep = ",";
2579 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002580 if (test_bit(WantReplacement, &rdev->flags)) {
2581 len += sprintf(page+len, "%swant_replacement", sep);
2582 sep = ",";
2583 }
2584 if (test_bit(Replacement, &rdev->flags)) {
2585 len += sprintf(page+len, "%sreplacement", sep);
2586 sep = ",";
2587 }
2588
NeilBrown86e6ffd2005-11-08 21:39:24 -08002589 return len+sprintf(page+len, "\n");
2590}
2591
NeilBrown45dc2de2006-06-26 00:27:58 -07002592static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002593state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002594{
2595 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002596 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002597 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002598 * writemostly - sets write_mostly
2599 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002600 * blocked - sets the Blocked flags
2601 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002602 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002603 * -insync - clear Insync for a device with a slot assigned,
2604 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002605 * write_error - sets WriteErrorSeen
2606 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002607 */
2608 int err = -EINVAL;
2609 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2610 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002611 if (test_bit(Faulty, &rdev->flags))
2612 err = 0;
2613 else
2614 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002615 } else if (cmd_match(buf, "remove")) {
2616 if (rdev->raid_disk >= 0)
2617 err = -EBUSY;
2618 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002619 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002620 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002621 if (mddev->pers)
2622 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002623 md_new_event(mddev);
2624 err = 0;
2625 }
NeilBrownf6556752006-06-26 00:28:01 -07002626 } else if (cmd_match(buf, "writemostly")) {
2627 set_bit(WriteMostly, &rdev->flags);
2628 err = 0;
2629 } else if (cmd_match(buf, "-writemostly")) {
2630 clear_bit(WriteMostly, &rdev->flags);
2631 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002632 } else if (cmd_match(buf, "blocked")) {
2633 set_bit(Blocked, &rdev->flags);
2634 err = 0;
2635 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002636 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002637 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002638 /* metadata handler doesn't understand badblocks,
2639 * so we need to fail the device
2640 */
2641 md_error(rdev->mddev, rdev);
2642 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002643 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002644 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002645 wake_up(&rdev->blocked_wait);
2646 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2647 md_wakeup_thread(rdev->mddev->thread);
2648
2649 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002650 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2651 set_bit(In_sync, &rdev->flags);
2652 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002653 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
2654 clear_bit(In_sync, &rdev->flags);
2655 rdev->saved_raid_disk = rdev->raid_disk;
2656 rdev->raid_disk = -1;
2657 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002658 } else if (cmd_match(buf, "write_error")) {
2659 set_bit(WriteErrorSeen, &rdev->flags);
2660 err = 0;
2661 } else if (cmd_match(buf, "-write_error")) {
2662 clear_bit(WriteErrorSeen, &rdev->flags);
2663 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002664 } else if (cmd_match(buf, "want_replacement")) {
2665 /* Any non-spare device that is not a replacement can
2666 * become want_replacement at any time, but we then need to
2667 * check if recovery is needed.
2668 */
2669 if (rdev->raid_disk >= 0 &&
2670 !test_bit(Replacement, &rdev->flags))
2671 set_bit(WantReplacement, &rdev->flags);
2672 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2673 md_wakeup_thread(rdev->mddev->thread);
2674 err = 0;
2675 } else if (cmd_match(buf, "-want_replacement")) {
2676 /* Clearing 'want_replacement' is always allowed.
2677 * Once replacements starts it is too late though.
2678 */
2679 err = 0;
2680 clear_bit(WantReplacement, &rdev->flags);
2681 } else if (cmd_match(buf, "replacement")) {
2682 /* Can only set a device as a replacement when array has not
2683 * yet been started. Once running, replacement is automatic
2684 * from spares, or by assigning 'slot'.
2685 */
2686 if (rdev->mddev->pers)
2687 err = -EBUSY;
2688 else {
2689 set_bit(Replacement, &rdev->flags);
2690 err = 0;
2691 }
2692 } else if (cmd_match(buf, "-replacement")) {
2693 /* Similarly, can only clear Replacement before start */
2694 if (rdev->mddev->pers)
2695 err = -EBUSY;
2696 else {
2697 clear_bit(Replacement, &rdev->flags);
2698 err = 0;
2699 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002700 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002701 if (!err)
2702 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002703 return err ? err : len;
2704}
NeilBrown80ca3a42006-07-10 04:44:18 -07002705static struct rdev_sysfs_entry rdev_state =
2706__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002707
2708static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002709errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002710{
2711 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2712}
2713
2714static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002715errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002716{
2717 char *e;
2718 unsigned long n = simple_strtoul(buf, &e, 10);
2719 if (*buf && (*e == 0 || *e == '\n')) {
2720 atomic_set(&rdev->corrected_errors, n);
2721 return len;
2722 }
2723 return -EINVAL;
2724}
2725static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002726__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002727
NeilBrown014236d2006-01-06 00:20:55 -08002728static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002729slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002730{
2731 if (rdev->raid_disk < 0)
2732 return sprintf(page, "none\n");
2733 else
2734 return sprintf(page, "%d\n", rdev->raid_disk);
2735}
2736
2737static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002738slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002739{
2740 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002741 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002742 int slot = simple_strtoul(buf, &e, 10);
2743 if (strncmp(buf, "none", 4)==0)
2744 slot = -1;
2745 else if (e==buf || (*e && *e!= '\n'))
2746 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002747 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002748 /* Setting 'slot' on an active array requires also
2749 * updating the 'rd%d' link, and communicating
2750 * with the personality with ->hot_*_disk.
2751 * For now we only support removing
2752 * failed/spare devices. This normally happens automatically,
2753 * but not when the metadata is externally managed.
2754 */
NeilBrownc303da62008-02-06 01:39:51 -08002755 if (rdev->raid_disk == -1)
2756 return -EEXIST;
2757 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002758 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002759 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002760 clear_bit(Blocked, &rdev->flags);
2761 remove_and_add_spares(rdev->mddev, rdev);
2762 if (rdev->raid_disk >= 0)
2763 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002764 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2765 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002766 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002767 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002768 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002769 */
2770
2771 if (rdev->raid_disk != -1)
2772 return -EBUSY;
2773
NeilBrownc6751b22011-02-02 11:57:13 +11002774 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2775 return -EBUSY;
2776
Neil Brown6c2fce22008-06-28 08:31:31 +10002777 if (rdev->mddev->pers->hot_add_disk == NULL)
2778 return -EINVAL;
2779
NeilBrownba1b41b2011-01-14 09:14:34 +11002780 if (slot >= rdev->mddev->raid_disks &&
2781 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2782 return -ENOSPC;
2783
Neil Brown6c2fce22008-06-28 08:31:31 +10002784 rdev->raid_disk = slot;
2785 if (test_bit(In_sync, &rdev->flags))
2786 rdev->saved_raid_disk = slot;
2787 else
2788 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002789 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002790 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002791 err = rdev->mddev->pers->
2792 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002793 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002794 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002795 return err;
Neil Brown52664732008-06-28 08:31:44 +10002796 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002797 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002798 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002799 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002800 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002801 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002802 if (slot >= rdev->mddev->raid_disks &&
2803 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002804 return -ENOSPC;
2805 rdev->raid_disk = slot;
2806 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002807 clear_bit(Faulty, &rdev->flags);
2808 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002809 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002810 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002811 }
NeilBrown014236d2006-01-06 00:20:55 -08002812 return len;
2813}
2814
2815
2816static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002817__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002818
NeilBrown93c8cad2006-01-06 00:20:56 -08002819static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002820offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002821{
Andrew Morton6961ece2006-01-06 00:20:59 -08002822 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002823}
2824
2825static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002826offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002827{
NeilBrownc6563a82012-05-21 09:27:00 +10002828 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002829 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002830 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002831 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002832 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002833 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002834 /* Must set offset before size, so overlap checks
2835 * can be sane */
2836 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002837 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002838 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002839 return len;
2840}
2841
2842static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002843__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002844
NeilBrownc6563a82012-05-21 09:27:00 +10002845static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2846{
2847 return sprintf(page, "%llu\n",
2848 (unsigned long long)rdev->new_data_offset);
2849}
2850
2851static ssize_t new_offset_store(struct md_rdev *rdev,
2852 const char *buf, size_t len)
2853{
2854 unsigned long long new_offset;
2855 struct mddev *mddev = rdev->mddev;
2856
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002857 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002858 return -EINVAL;
2859
2860 if (mddev->sync_thread)
2861 return -EBUSY;
2862 if (new_offset == rdev->data_offset)
2863 /* reset is always permitted */
2864 ;
2865 else if (new_offset > rdev->data_offset) {
2866 /* must not push array size beyond rdev_sectors */
2867 if (new_offset - rdev->data_offset
2868 + mddev->dev_sectors > rdev->sectors)
2869 return -E2BIG;
2870 }
2871 /* Metadata worries about other space details. */
2872
2873 /* decreasing the offset is inconsistent with a backwards
2874 * reshape.
2875 */
2876 if (new_offset < rdev->data_offset &&
2877 mddev->reshape_backwards)
2878 return -EINVAL;
2879 /* Increasing offset is inconsistent with forwards
2880 * reshape. reshape_direction should be set to
2881 * 'backwards' first.
2882 */
2883 if (new_offset > rdev->data_offset &&
2884 !mddev->reshape_backwards)
2885 return -EINVAL;
2886
2887 if (mddev->pers && mddev->persistent &&
2888 !super_types[mddev->major_version]
2889 .allow_new_offset(rdev, new_offset))
2890 return -E2BIG;
2891 rdev->new_data_offset = new_offset;
2892 if (new_offset > rdev->data_offset)
2893 mddev->reshape_backwards = 1;
2894 else if (new_offset < rdev->data_offset)
2895 mddev->reshape_backwards = 0;
2896
2897 return len;
2898}
2899static struct rdev_sysfs_entry rdev_new_offset =
2900__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2901
NeilBrown83303b62006-01-06 00:21:06 -08002902static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002903rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002904{
Andre Nolldd8ac332009-03-31 14:33:13 +11002905 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002906}
2907
NeilBrownc5d79ad2008-02-06 01:39:54 -08002908static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2909{
2910 /* check if two start/length pairs overlap */
2911 if (s1+l1 <= s2)
2912 return 0;
2913 if (s2+l2 <= s1)
2914 return 0;
2915 return 1;
2916}
2917
Dan Williamsb522adc2009-03-31 15:00:31 +11002918static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2919{
2920 unsigned long long blocks;
2921 sector_t new;
2922
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002923 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002924 return -EINVAL;
2925
2926 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2927 return -EINVAL; /* sector conversion overflow */
2928
2929 new = blocks * 2;
2930 if (new != blocks * 2)
2931 return -EINVAL; /* unsigned long long to sector_t overflow */
2932
2933 *sectors = new;
2934 return 0;
2935}
2936
NeilBrown83303b62006-01-06 00:21:06 -08002937static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002938rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002939{
NeilBrownfd01b882011-10-11 16:47:53 +11002940 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002941 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002942 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002943
Dan Williamsb522adc2009-03-31 15:00:31 +11002944 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002945 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002946 if (rdev->data_offset != rdev->new_data_offset)
2947 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002948 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002949 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002950 sectors = super_types[my_mddev->major_version].
2951 rdev_size_change(rdev, sectors);
2952 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002953 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002954 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002955 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002956 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002957 if (!my_mddev->pers->resize)
2958 /* Cannot change size for RAID0 or Linear etc */
2959 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002960 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002961 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002962 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002963
Andre Nolldd8ac332009-03-31 14:33:13 +11002964 rdev->sectors = sectors;
2965 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002966 /* Need to check that all other rdevs with the same
2967 * ->bdev do not overlap. 'rcu' is sufficient to walk
2968 * the rdev lists safely.
2969 * This check does not provide a hard guarantee, it
2970 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002971 */
NeilBrownfd01b882011-10-11 16:47:53 +11002972 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002973 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002974 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002975
NeilBrown8b1afc32014-09-29 15:33:20 +10002976 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002977 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002978 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002979
NeilBrowndafb20f2012-03-19 12:46:39 +11002980 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002981 if (rdev->bdev == rdev2->bdev &&
2982 rdev != rdev2 &&
2983 overlaps(rdev->data_offset, rdev->sectors,
2984 rdev2->data_offset,
2985 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002986 overlap = 1;
2987 break;
2988 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002989 if (overlap) {
2990 mddev_put(mddev);
2991 break;
2992 }
2993 }
NeilBrown8b1afc32014-09-29 15:33:20 +10002994 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08002995 if (overlap) {
2996 /* Someone else could have slipped in a size
2997 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002998 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002999 * safe, and trust userspace not to race with
3000 * itself
3001 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003002 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003003 return -EBUSY;
3004 }
3005 }
NeilBrown83303b62006-01-06 00:21:06 -08003006 return len;
3007}
3008
3009static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003010__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003011
Dan Williams06e3c812009-12-12 21:17:12 -07003012
NeilBrown3cb03002011-10-11 16:45:26 +11003013static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003014{
3015 unsigned long long recovery_start = rdev->recovery_offset;
3016
3017 if (test_bit(In_sync, &rdev->flags) ||
3018 recovery_start == MaxSector)
3019 return sprintf(page, "none\n");
3020
3021 return sprintf(page, "%llu\n", recovery_start);
3022}
3023
NeilBrown3cb03002011-10-11 16:45:26 +11003024static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003025{
3026 unsigned long long recovery_start;
3027
3028 if (cmd_match(buf, "none"))
3029 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003030 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003031 return -EINVAL;
3032
3033 if (rdev->mddev->pers &&
3034 rdev->raid_disk >= 0)
3035 return -EBUSY;
3036
3037 rdev->recovery_offset = recovery_start;
3038 if (recovery_start == MaxSector)
3039 set_bit(In_sync, &rdev->flags);
3040 else
3041 clear_bit(In_sync, &rdev->flags);
3042 return len;
3043}
3044
3045static struct rdev_sysfs_entry rdev_recovery_start =
3046__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3047
NeilBrown16c791a2011-07-28 11:31:47 +10003048
3049static ssize_t
3050badblocks_show(struct badblocks *bb, char *page, int unack);
3051static ssize_t
3052badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3053
NeilBrown3cb03002011-10-11 16:45:26 +11003054static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003055{
3056 return badblocks_show(&rdev->badblocks, page, 0);
3057}
NeilBrown3cb03002011-10-11 16:45:26 +11003058static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003059{
NeilBrownde393cd2011-07-28 11:31:48 +10003060 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3061 /* Maybe that ack was all we needed */
3062 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3063 wake_up(&rdev->blocked_wait);
3064 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003065}
3066static struct rdev_sysfs_entry rdev_bad_blocks =
3067__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3068
3069
NeilBrown3cb03002011-10-11 16:45:26 +11003070static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003071{
3072 return badblocks_show(&rdev->badblocks, page, 1);
3073}
NeilBrown3cb03002011-10-11 16:45:26 +11003074static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003075{
3076 return badblocks_store(&rdev->badblocks, page, len, 1);
3077}
3078static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3079__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3080
NeilBrown86e6ffd2005-11-08 21:39:24 -08003081static struct attribute *rdev_default_attrs[] = {
3082 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003083 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003084 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003085 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003086 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003087 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003088 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003089 &rdev_bad_blocks.attr,
3090 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003091 NULL,
3092};
3093static ssize_t
3094rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3095{
3096 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003097 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003098 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003099 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003100
3101 if (!entry->show)
3102 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003103
3104 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3105 if (!rv) {
3106 if (rdev->mddev == NULL)
3107 rv = -EBUSY;
3108 else
3109 rv = entry->show(rdev, page);
3110 mddev_unlock(mddev);
3111 }
3112 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003113}
3114
3115static ssize_t
3116rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3117 const char *page, size_t length)
3118{
3119 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003120 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003121 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003122 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003123
3124 if (!entry->store)
3125 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003126 if (!capable(CAP_SYS_ADMIN))
3127 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003128 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003129 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003130 if (rdev->mddev == NULL)
3131 rv = -EBUSY;
3132 else
3133 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003134 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003135 }
3136 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003137}
3138
3139static void rdev_free(struct kobject *ko)
3140{
NeilBrown3cb03002011-10-11 16:45:26 +11003141 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003142 kfree(rdev);
3143}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003144static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003145 .show = rdev_attr_show,
3146 .store = rdev_attr_store,
3147};
3148static struct kobj_type rdev_ktype = {
3149 .release = rdev_free,
3150 .sysfs_ops = &rdev_sysfs_ops,
3151 .default_attrs = rdev_default_attrs,
3152};
3153
NeilBrown3cb03002011-10-11 16:45:26 +11003154int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003155{
3156 rdev->desc_nr = -1;
3157 rdev->saved_raid_disk = -1;
3158 rdev->raid_disk = -1;
3159 rdev->flags = 0;
3160 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003161 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003162 rdev->sb_events = 0;
3163 rdev->last_read_error.tv_sec = 0;
3164 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003165 rdev->sb_loaded = 0;
3166 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003167 atomic_set(&rdev->nr_pending, 0);
3168 atomic_set(&rdev->read_errors, 0);
3169 atomic_set(&rdev->corrected_errors, 0);
3170
3171 INIT_LIST_HEAD(&rdev->same_set);
3172 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003173
3174 /* Add space to store bad block list.
3175 * This reserves the space even on arrays where it cannot
3176 * be used - I wonder if that matters
3177 */
3178 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003179 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003180 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3181 seqlock_init(&rdev->badblocks.lock);
3182 if (rdev->badblocks.page == NULL)
3183 return -ENOMEM;
3184
3185 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003186}
3187EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188/*
3189 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3190 *
3191 * mark the device faulty if:
3192 *
3193 * - the device is nonexistent (zero size)
3194 * - the device has no valid superblock
3195 *
3196 * a faulty rdev _never_ has rdev->sb set.
3197 */
NeilBrown3cb03002011-10-11 16:45:26 +11003198static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
3200 char b[BDEVNAME_SIZE];
3201 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003202 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 sector_t size;
3204
NeilBrown9ffae0c2006-01-06 00:20:32 -08003205 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 if (!rdev) {
3207 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3208 return ERR_PTR(-ENOMEM);
3209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
NeilBrown2230dfe2011-07-28 11:31:46 +10003211 err = md_rdev_init(rdev);
3212 if (err)
3213 goto abort_free;
3214 err = alloc_disk_sb(rdev);
3215 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 goto abort_free;
3217
NeilBrownc5d79ad2008-02-06 01:39:54 -08003218 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 if (err)
3220 goto abort_free;
3221
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003222 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003223
Mike Snitzer77304d22010-11-08 14:39:12 +01003224 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 if (!size) {
3226 printk(KERN_WARNING
3227 "md: %s has zero or unknown size, marking faulty!\n",
3228 bdevname(rdev->bdev,b));
3229 err = -EINVAL;
3230 goto abort_free;
3231 }
3232
3233 if (super_format >= 0) {
3234 err = super_types[super_format].
3235 load_super(rdev, NULL, super_minor);
3236 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003237 printk(KERN_WARNING
3238 "md: %s does not have a valid v%d.%d "
3239 "superblock, not importing!\n",
3240 bdevname(rdev->bdev,b),
3241 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 goto abort_free;
3243 }
3244 if (err < 0) {
3245 printk(KERN_WARNING
3246 "md: could not read %s's sb, not importing!\n",
3247 bdevname(rdev->bdev,b));
3248 goto abort_free;
3249 }
3250 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003251
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 return rdev;
3253
3254abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003255 if (rdev->bdev)
3256 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003257 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 kfree(rdev);
3259 return ERR_PTR(err);
3260}
3261
3262/*
3263 * Check a full RAID array for plausibility
3264 */
3265
3266
NeilBrownfd01b882011-10-11 16:47:53 +11003267static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
3269 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003270 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 char b[BDEVNAME_SIZE];
3272
3273 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003274 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 switch (super_types[mddev->major_version].
3276 load_super(rdev, freshest, mddev->minor_version)) {
3277 case 1:
3278 freshest = rdev;
3279 break;
3280 case 0:
3281 break;
3282 default:
3283 printk( KERN_ERR \
3284 "md: fatal superblock inconsistency in %s"
3285 " -- removing from array\n",
3286 bdevname(rdev->bdev,b));
3287 kick_rdev_from_array(rdev);
3288 }
3289
3290
3291 super_types[mddev->major_version].
3292 validate_super(mddev, freshest);
3293
3294 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003295 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003296 if (mddev->max_disks &&
3297 (rdev->desc_nr >= mddev->max_disks ||
3298 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003299 printk(KERN_WARNING
3300 "md: %s: %s: only %d devices permitted\n",
3301 mdname(mddev), bdevname(rdev->bdev, b),
3302 mddev->max_disks);
3303 kick_rdev_from_array(rdev);
3304 continue;
3305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 if (rdev != freshest)
3307 if (super_types[mddev->major_version].
3308 validate_super(mddev, rdev)) {
3309 printk(KERN_WARNING "md: kicking non-fresh %s"
3310 " from array!\n",
3311 bdevname(rdev->bdev,b));
3312 kick_rdev_from_array(rdev);
3313 continue;
3314 }
3315 if (mddev->level == LEVEL_MULTIPATH) {
3316 rdev->desc_nr = i++;
3317 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003318 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003319 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003320 rdev->raid_disk = -1;
3321 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 }
3323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324}
3325
NeilBrown72e02072009-12-14 12:49:55 +11003326/* Read a fixed-point number.
3327 * Numbers in sysfs attributes should be in "standard" units where
3328 * possible, so time should be in seconds.
3329 * However we internally use a a much smaller unit such as
3330 * milliseconds or jiffies.
3331 * This function takes a decimal number with a possible fractional
3332 * component, and produces an integer which is the result of
3333 * multiplying that number by 10^'scale'.
3334 * all without any floating-point arithmetic.
3335 */
3336int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3337{
3338 unsigned long result = 0;
3339 long decimals = -1;
3340 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3341 if (*cp == '.')
3342 decimals = 0;
3343 else if (decimals < scale) {
3344 unsigned int value;
3345 value = *cp - '0';
3346 result = result * 10 + value;
3347 if (decimals >= 0)
3348 decimals++;
3349 }
3350 cp++;
3351 }
3352 if (*cp == '\n')
3353 cp++;
3354 if (*cp)
3355 return -EINVAL;
3356 if (decimals < 0)
3357 decimals = 0;
3358 while (decimals < scale) {
3359 result *= 10;
3360 decimals ++;
3361 }
3362 *res = result;
3363 return 0;
3364}
3365
3366
NeilBrown19052c02008-08-05 15:54:13 +10003367static void md_safemode_timeout(unsigned long data);
3368
NeilBrowneae17012005-11-08 21:39:23 -08003369static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003370safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003371{
3372 int msec = (mddev->safemode_delay*1000)/HZ;
3373 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3374}
3375static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003376safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003377{
NeilBrown16f17b32006-06-26 00:27:37 -07003378 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003379
NeilBrown72e02072009-12-14 12:49:55 +11003380 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003381 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003382 if (msec == 0)
3383 mddev->safemode_delay = 0;
3384 else {
NeilBrown19052c02008-08-05 15:54:13 +10003385 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003386 mddev->safemode_delay = (msec*HZ)/1000;
3387 if (mddev->safemode_delay == 0)
3388 mddev->safemode_delay = 1;
NeilBrown275c51c2013-08-08 09:20:32 +10003389 if (mddev->safemode_delay < old_delay || old_delay == 0)
NeilBrown19052c02008-08-05 15:54:13 +10003390 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003391 }
3392 return len;
3393}
3394static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003395__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003396
3397static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003398level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003399{
NeilBrown84fc4b52011-10-11 16:49:58 +11003400 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003401 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003402 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003403 else if (mddev->clevel[0])
3404 return sprintf(page, "%s\n", mddev->clevel);
3405 else if (mddev->level != LEVEL_NONE)
3406 return sprintf(page, "%d\n", mddev->level);
3407 else
3408 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003409}
3410
NeilBrownd9d166c2006-01-06 00:20:51 -08003411static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003412level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003413{
Dan Williamsf2859af2010-05-02 10:04:16 -07003414 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003415 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003416 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003417 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003418 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003419 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003420
3421 if (mddev->pers == NULL) {
3422 if (len == 0)
3423 return 0;
3424 if (len >= sizeof(mddev->clevel))
3425 return -ENOSPC;
3426 strncpy(mddev->clevel, buf, len);
3427 if (mddev->clevel[len-1] == '\n')
3428 len--;
3429 mddev->clevel[len] = 0;
3430 mddev->level = LEVEL_NONE;
3431 return rv;
3432 }
NeilBrownbd8839e2014-05-28 13:39:21 +10003433 if (mddev->ro)
3434 return -EROFS;
NeilBrown245f46c2009-03-31 14:39:39 +11003435
3436 /* request to change the personality. Need to ensure:
3437 * - array is not engaged in resync/recovery/reshape
3438 * - old personality can be suspended
3439 * - new personality will access other array.
3440 */
3441
NeilBrownbb4f1e92010-08-08 21:18:03 +10003442 if (mddev->sync_thread ||
3443 mddev->reshape_position != MaxSector ||
3444 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003445 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003446
3447 if (!mddev->pers->quiesce) {
3448 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3449 mdname(mddev), mddev->pers->name);
3450 return -EINVAL;
3451 }
3452
3453 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003454 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003455 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003456 strncpy(clevel, buf, len);
3457 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003458 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003459 clevel[len] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003460 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003461 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003462
Dan Williamsf2859af2010-05-02 10:04:16 -07003463 if (request_module("md-%s", clevel) != 0)
3464 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003465 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003466 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003467 if (!pers || !try_module_get(pers->owner)) {
3468 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003469 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003470 return -EINVAL;
3471 }
3472 spin_unlock(&pers_lock);
3473
3474 if (pers == mddev->pers) {
3475 /* Nothing to do! */
3476 module_put(pers->owner);
3477 return rv;
3478 }
3479 if (!pers->takeover) {
3480 module_put(pers->owner);
3481 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003482 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003483 return -EINVAL;
3484 }
3485
NeilBrowndafb20f2012-03-19 12:46:39 +11003486 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003487 rdev->new_raid_disk = rdev->raid_disk;
3488
NeilBrown245f46c2009-03-31 14:39:39 +11003489 /* ->takeover must set new_* and/or delta_disks
3490 * if it succeeds, and may set them when it fails.
3491 */
3492 priv = pers->takeover(mddev);
3493 if (IS_ERR(priv)) {
3494 mddev->new_level = mddev->level;
3495 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003496 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003497 mddev->raid_disks -= mddev->delta_disks;
3498 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003499 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003500 module_put(pers->owner);
3501 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003502 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003503 return PTR_ERR(priv);
3504 }
3505
3506 /* Looks like we have a winner */
3507 mddev_suspend(mddev);
3508 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003509
3510 if (mddev->pers->sync_request == NULL &&
3511 pers->sync_request != NULL) {
3512 /* need to add the md_redundancy_group */
3513 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3514 printk(KERN_WARNING
3515 "md: cannot register extra attributes for %s\n",
3516 mdname(mddev));
Tejun Heo388975c2013-09-11 23:19:13 -04003517 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003518 }
3519 if (mddev->pers->sync_request != NULL &&
3520 pers->sync_request == NULL) {
3521 /* need to remove the md_redundancy_group */
3522 if (mddev->to_remove == NULL)
3523 mddev->to_remove = &md_redundancy_group;
3524 }
3525
Trela Maciej54071b32010-03-08 16:02:42 +11003526 if (mddev->pers->sync_request == NULL &&
3527 mddev->external) {
3528 /* We are converting from a no-redundancy array
3529 * to a redundancy array and metadata is managed
3530 * externally so we need to be sure that writes
3531 * won't block due to a need to transition
3532 * clean->dirty
3533 * until external management is started.
3534 */
3535 mddev->in_sync = 0;
3536 mddev->safemode_delay = 0;
3537 mddev->safemode = 0;
3538 }
3539
NeilBrowndafb20f2012-03-19 12:46:39 +11003540 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003541 if (rdev->raid_disk < 0)
3542 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003543 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003544 rdev->new_raid_disk = -1;
3545 if (rdev->new_raid_disk == rdev->raid_disk)
3546 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003547 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003548 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003549 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003550 if (rdev->raid_disk < 0)
3551 continue;
3552 if (rdev->new_raid_disk == rdev->raid_disk)
3553 continue;
3554 rdev->raid_disk = rdev->new_raid_disk;
3555 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003556 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003557 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003558 if (sysfs_link_rdev(mddev, rdev))
3559 printk(KERN_WARNING "md: cannot register rd%d"
3560 " for %s after level change\n",
3561 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003562 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003563 }
3564
3565 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003566 mddev->pers = pers;
3567 mddev->private = priv;
3568 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3569 mddev->level = mddev->new_level;
3570 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003571 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003572 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003573 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003574 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003575 if (mddev->pers->sync_request == NULL) {
3576 /* this is now an array without redundancy, so
3577 * it must always be in_sync
3578 */
3579 mddev->in_sync = 1;
3580 del_timer_sync(&mddev->safemode_timer);
3581 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003582 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003583 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003584 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003585 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003586 if (!mddev->thread)
3587 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003588 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003589 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003590 return rv;
3591}
3592
3593static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003594__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003595
NeilBrownd4dbd022006-06-26 00:27:59 -07003596
3597static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003598layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003599{
3600 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003601 if (mddev->reshape_position != MaxSector &&
3602 mddev->layout != mddev->new_layout)
3603 return sprintf(page, "%d (%d)\n",
3604 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003605 return sprintf(page, "%d\n", mddev->layout);
3606}
3607
3608static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003609layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003610{
3611 char *e;
3612 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003613
3614 if (!*buf || (*e && *e != '\n'))
3615 return -EINVAL;
3616
NeilBrownb3546032009-03-31 14:56:41 +11003617 if (mddev->pers) {
3618 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003619 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003620 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10003621 if (mddev->ro)
3622 return -EROFS;
NeilBrown597a7112009-06-18 08:47:42 +10003623 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003624 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003625 if (err) {
3626 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003627 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003628 }
NeilBrownb3546032009-03-31 14:56:41 +11003629 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003630 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003631 if (mddev->reshape_position == MaxSector)
3632 mddev->layout = n;
3633 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003634 return len;
3635}
3636static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003637__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003638
3639
NeilBrowneae17012005-11-08 21:39:23 -08003640static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003641raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003642{
NeilBrownbb636542005-11-08 21:39:45 -08003643 if (mddev->raid_disks == 0)
3644 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003645 if (mddev->reshape_position != MaxSector &&
3646 mddev->delta_disks != 0)
3647 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3648 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003649 return sprintf(page, "%d\n", mddev->raid_disks);
3650}
3651
NeilBrownfd01b882011-10-11 16:47:53 +11003652static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003653
3654static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003655raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003656{
NeilBrownda943b992006-01-06 00:20:54 -08003657 char *e;
3658 int rv = 0;
3659 unsigned long n = simple_strtoul(buf, &e, 10);
3660
3661 if (!*buf || (*e && *e != '\n'))
3662 return -EINVAL;
3663
3664 if (mddev->pers)
3665 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003666 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003667 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003668 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003669
3670 rdev_for_each(rdev, mddev) {
3671 if (olddisks < n &&
3672 rdev->data_offset < rdev->new_data_offset)
3673 return -EINVAL;
3674 if (olddisks > n &&
3675 rdev->data_offset > rdev->new_data_offset)
3676 return -EINVAL;
3677 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003678 mddev->delta_disks = n - olddisks;
3679 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003680 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003681 } else
NeilBrownda943b992006-01-06 00:20:54 -08003682 mddev->raid_disks = n;
3683 return rv ? rv : len;
3684}
3685static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003686__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003687
NeilBrown24dd4692005-11-08 21:39:26 -08003688static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003689chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003690{
NeilBrown08a02ec2007-05-09 02:35:38 -07003691 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003692 mddev->chunk_sectors != mddev->new_chunk_sectors)
3693 return sprintf(page, "%d (%d)\n",
3694 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003695 mddev->chunk_sectors << 9);
3696 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003697}
3698
3699static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003700chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003701{
NeilBrown3b343802006-01-06 00:20:47 -08003702 char *e;
3703 unsigned long n = simple_strtoul(buf, &e, 10);
3704
NeilBrown3b343802006-01-06 00:20:47 -08003705 if (!*buf || (*e && *e != '\n'))
3706 return -EINVAL;
3707
NeilBrownb3546032009-03-31 14:56:41 +11003708 if (mddev->pers) {
3709 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003710 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003711 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10003712 if (mddev->ro)
3713 return -EROFS;
NeilBrown597a7112009-06-18 08:47:42 +10003714 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003715 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003716 if (err) {
3717 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003718 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003719 }
NeilBrownb3546032009-03-31 14:56:41 +11003720 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003721 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003722 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003723 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003724 }
NeilBrown3b343802006-01-06 00:20:47 -08003725 return len;
3726}
3727static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003728__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003729
NeilBrowna94213b2006-06-26 00:28:00 -07003730static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003731resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003732{
NeilBrownd1a7c502009-03-31 15:24:32 +11003733 if (mddev->recovery_cp == MaxSector)
3734 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003735 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3736}
3737
3738static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003739resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003740{
NeilBrowna94213b2006-06-26 00:28:00 -07003741 char *e;
3742 unsigned long long n = simple_strtoull(buf, &e, 10);
3743
NeilBrownb0986362011-05-11 15:52:21 +10003744 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003745 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003746 if (cmd_match(buf, "none"))
3747 n = MaxSector;
3748 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003749 return -EINVAL;
3750
3751 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003752 if (mddev->pers)
3753 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003754 return len;
3755}
3756static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003757__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003758
NeilBrown9e653b62006-06-26 00:27:58 -07003759/*
3760 * The array state can be:
3761 *
3762 * clear
3763 * No devices, no size, no level
3764 * Equivalent to STOP_ARRAY ioctl
3765 * inactive
3766 * May have some settings, but array is not active
3767 * all IO results in error
3768 * When written, doesn't tear down array, but just stops it
3769 * suspended (not supported yet)
3770 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003771 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003772 * readonly
3773 * no resync can happen. no superblocks get written.
3774 * write requests fail
3775 * read-auto
3776 * like readonly, but behaves like 'clean' on a write request.
3777 *
3778 * clean - no pending writes, but otherwise active.
3779 * When written to inactive array, starts without resync
3780 * If a write request arrives then
3781 * if metadata is known, mark 'dirty' and switch to 'active'.
3782 * if not known, block and switch to write-pending
3783 * If written to an active array that has pending writes, then fails.
3784 * active
3785 * fully active: IO and resync can be happening.
3786 * When written to inactive array, starts with resync
3787 *
3788 * write-pending
3789 * clean, but writes are blocked waiting for 'active' to be written.
3790 *
3791 * active-idle
3792 * like active, but no writes have been seen for a while (100msec).
3793 *
3794 */
3795enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3796 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003797static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003798 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3799 "write-pending", "active-idle", NULL };
3800
3801static int match_word(const char *word, char **list)
3802{
3803 int n;
3804 for (n=0; list[n]; n++)
3805 if (cmd_match(word, list[n]))
3806 break;
3807 return n;
3808}
3809
3810static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003811array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003812{
3813 enum array_state st = inactive;
3814
3815 if (mddev->pers)
3816 switch(mddev->ro) {
3817 case 1:
3818 st = readonly;
3819 break;
3820 case 2:
3821 st = read_auto;
3822 break;
3823 case 0:
3824 if (mddev->in_sync)
3825 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003826 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003827 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003828 else if (mddev->safemode)
3829 st = active_idle;
3830 else
3831 st = active;
3832 }
3833 else {
3834 if (list_empty(&mddev->disks) &&
3835 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003836 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003837 st = clear;
3838 else
3839 st = inactive;
3840 }
3841 return sprintf(page, "%s\n", array_states[st]);
3842}
3843
NeilBrowna05b7ea2012-07-19 15:59:18 +10003844static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3845static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11003846static int do_md_run(struct mddev * mddev);
3847static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003848
3849static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003850array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003851{
3852 int err = -EINVAL;
3853 enum array_state st = match_word(buf, array_states);
3854 switch(st) {
3855 case bad_word:
3856 break;
3857 case clear:
3858 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003859 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003860 break;
3861 case inactive:
3862 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003863 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003864 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003865 else
NeilBrowne6910632008-02-06 01:39:51 -08003866 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003867 break;
3868 case suspended:
3869 break; /* not supported yet */
3870 case readonly:
3871 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003872 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003873 else {
3874 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003875 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003876 err = do_md_run(mddev);
3877 }
3878 break;
3879 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003880 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003881 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003882 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003883 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003884 err = restart_array(mddev);
3885 if (err == 0) {
3886 mddev->ro = 2;
3887 set_disk_ro(mddev->gendisk, 0);
3888 }
NeilBrown9e653b62006-06-26 00:27:58 -07003889 } else {
3890 mddev->ro = 2;
3891 err = do_md_run(mddev);
3892 }
3893 break;
3894 case clean:
3895 if (mddev->pers) {
3896 restart_array(mddev);
3897 spin_lock_irq(&mddev->write_lock);
3898 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003899 if (mddev->in_sync == 0) {
3900 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003901 if (mddev->safemode == 1)
3902 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003903 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003904 }
3905 err = 0;
3906 } else
3907 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003908 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003909 } else
3910 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003911 break;
3912 case active:
3913 if (mddev->pers) {
3914 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003915 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003916 wake_up(&mddev->sb_wait);
3917 err = 0;
3918 } else {
3919 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003920 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003921 err = do_md_run(mddev);
3922 }
3923 break;
3924 case write_pending:
3925 case active_idle:
3926 /* these cannot be set */
3927 break;
3928 }
3929 if (err)
3930 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003931 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003932 if (mddev->hold_active == UNTIL_IOCTL)
3933 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003934 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003935 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003936 }
NeilBrown9e653b62006-06-26 00:27:58 -07003937}
NeilBrown80ca3a42006-07-10 04:44:18 -07003938static struct md_sysfs_entry md_array_state =
3939__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003940
NeilBrown6d7ff7382006-01-06 00:21:16 -08003941static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003942max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003943 return sprintf(page, "%d\n",
3944 atomic_read(&mddev->max_corr_read_errors));
3945}
3946
3947static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003948max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003949{
3950 char *e;
3951 unsigned long n = simple_strtoul(buf, &e, 10);
3952
3953 if (*buf && (*e == 0 || *e == '\n')) {
3954 atomic_set(&mddev->max_corr_read_errors, n);
3955 return len;
3956 }
3957 return -EINVAL;
3958}
3959
3960static struct md_sysfs_entry max_corr_read_errors =
3961__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3962 max_corrected_read_errors_store);
3963
3964static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003965null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003966{
3967 return -EINVAL;
3968}
3969
3970static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003971new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003972{
3973 /* buf must be %d:%d\n? giving major and minor numbers */
3974 /* The new device is added to the array.
3975 * If the array has a persistent superblock, we read the
3976 * superblock to initialise info and check validity.
3977 * Otherwise, only checking done is that in bind_rdev_to_array,
3978 * which mainly checks size.
3979 */
3980 char *e;
3981 int major = simple_strtoul(buf, &e, 10);
3982 int minor;
3983 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003984 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003985 int err;
3986
3987 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3988 return -EINVAL;
3989 minor = simple_strtoul(e+1, &e, 10);
3990 if (*e && *e != '\n')
3991 return -EINVAL;
3992 dev = MKDEV(major, minor);
3993 if (major != MAJOR(dev) ||
3994 minor != MINOR(dev))
3995 return -EOVERFLOW;
3996
3997
3998 if (mddev->persistent) {
3999 rdev = md_import_device(dev, mddev->major_version,
4000 mddev->minor_version);
4001 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004002 struct md_rdev *rdev0
4003 = list_entry(mddev->disks.next,
4004 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004005 err = super_types[mddev->major_version]
4006 .load_super(rdev, rdev0, mddev->minor_version);
4007 if (err < 0)
4008 goto out;
4009 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004010 } else if (mddev->external)
4011 rdev = md_import_device(dev, -2, -1);
4012 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004013 rdev = md_import_device(dev, -1, -1);
4014
4015 if (IS_ERR(rdev))
4016 return PTR_ERR(rdev);
4017 err = bind_rdev_to_array(rdev, mddev);
4018 out:
4019 if (err)
4020 export_rdev(rdev);
4021 return err ? err : len;
4022}
4023
4024static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004025__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004026
4027static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004028bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004029{
4030 char *end;
4031 unsigned long chunk, end_chunk;
4032
4033 if (!mddev->bitmap)
4034 goto out;
4035 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4036 while (*buf) {
4037 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4038 if (buf == end) break;
4039 if (*end == '-') { /* range */
4040 buf = end + 1;
4041 end_chunk = simple_strtoul(buf, &end, 0);
4042 if (buf == end) break;
4043 }
4044 if (*end && !isspace(*end)) break;
4045 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004046 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004047 }
4048 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4049out:
4050 return len;
4051}
4052
4053static struct md_sysfs_entry md_bitmap =
4054__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4055
4056static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004057size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004058{
Andre Noll58c0fed2009-03-31 14:33:13 +11004059 return sprintf(page, "%llu\n",
4060 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004061}
4062
NeilBrownfd01b882011-10-11 16:47:53 +11004063static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004064
4065static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004066size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004067{
4068 /* If array is inactive, we can reduce the component size, but
4069 * not increase it (except from 0).
4070 * If array is active, we can try an on-line resize
4071 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004072 sector_t sectors;
4073 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004074
Andre Noll58c0fed2009-03-31 14:33:13 +11004075 if (err < 0)
4076 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004077 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004078 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004079 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004080 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004081 if (mddev->dev_sectors == 0 ||
4082 mddev->dev_sectors > sectors)
4083 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004084 else
4085 err = -ENOSPC;
4086 }
4087 return err ? err : len;
4088}
4089
4090static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004091__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004092
NeilBrown8bb93aa2006-01-06 00:20:50 -08004093
Masanari Iida83f0d772012-10-30 00:18:08 +09004094/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004095 * This is one of
4096 * 'none' for arrays with no metadata (good luck...)
4097 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004098 * or N.M for internally known formats
4099 */
4100static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004101metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004102{
4103 if (mddev->persistent)
4104 return sprintf(page, "%d.%d\n",
4105 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004106 else if (mddev->external)
4107 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004108 else
4109 return sprintf(page, "none\n");
4110}
4111
4112static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004113metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004114{
4115 int major, minor;
4116 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004117 /* Changing the details of 'external' metadata is
4118 * always permitted. Otherwise there must be
4119 * no devices attached to the array.
4120 */
4121 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4122 ;
4123 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004124 return -EBUSY;
4125
4126 if (cmd_match(buf, "none")) {
4127 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004128 mddev->external = 0;
4129 mddev->major_version = 0;
4130 mddev->minor_version = 90;
4131 return len;
4132 }
4133 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004134 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004135 if (namelen >= sizeof(mddev->metadata_type))
4136 namelen = sizeof(mddev->metadata_type)-1;
4137 strncpy(mddev->metadata_type, buf+9, namelen);
4138 mddev->metadata_type[namelen] = 0;
4139 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4140 mddev->metadata_type[--namelen] = 0;
4141 mddev->persistent = 0;
4142 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004143 mddev->major_version = 0;
4144 mddev->minor_version = 90;
4145 return len;
4146 }
4147 major = simple_strtoul(buf, &e, 10);
4148 if (e==buf || *e != '.')
4149 return -EINVAL;
4150 buf = e+1;
4151 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004152 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004153 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004154 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004155 return -ENOENT;
4156 mddev->major_version = major;
4157 mddev->minor_version = minor;
4158 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004159 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004160 return len;
4161}
4162
4163static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004164__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004165
NeilBrowna35b0d62006-01-06 00:20:49 -08004166static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004167action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004168{
NeilBrown7eec3142005-11-08 21:39:44 -08004169 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004170 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4171 type = "frozen";
4172 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004173 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004174 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4175 type = "reshape";
4176 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004177 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4178 type = "resync";
4179 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4180 type = "check";
4181 else
4182 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004183 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004184 type = "recover";
4185 }
4186 return sprintf(page, "%s\n", type);
4187}
4188
4189static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004190action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004191{
NeilBrown7eec3142005-11-08 21:39:44 -08004192 if (!mddev->pers || !mddev->pers->sync_request)
4193 return -EINVAL;
4194
NeilBrownb6a9ce62009-05-26 09:41:17 +10004195 if (cmd_match(page, "frozen"))
4196 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4197 else
4198 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4199
4200 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004201 if (mddev->sync_thread) {
4202 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10004203 md_reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004204 }
NeilBrown03c902e2006-01-06 00:20:46 -08004205 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4206 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004207 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004208 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004209 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004210 else if (cmd_match(page, "recover")) {
4211 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4212 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4213 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004214 int err;
4215 if (mddev->pers->start_reshape == NULL)
4216 return -EINVAL;
4217 err = mddev->pers->start_reshape(mddev);
4218 if (err)
4219 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004220 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004221 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004222 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004223 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004224 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004225 return -EINVAL;
4226 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4227 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004228 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004229 if (mddev->ro == 2) {
4230 /* A write to sync_action is enough to justify
4231 * canceling read-auto mode
4232 */
4233 mddev->ro = 0;
4234 md_wakeup_thread(mddev->sync_thread);
4235 }
NeilBrown03c902e2006-01-06 00:20:46 -08004236 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004237 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004238 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004239 return len;
4240}
4241
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004242static struct md_sysfs_entry md_scan_mode =
4243__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4244
4245static ssize_t
4246last_sync_action_show(struct mddev *mddev, char *page)
4247{
4248 return sprintf(page, "%s\n", mddev->last_sync_action);
4249}
4250
4251static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4252
NeilBrown9d888832005-11-08 21:39:26 -08004253static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004254mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004255{
4256 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004257 (unsigned long long)
4258 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004259}
4260
NeilBrown80ca3a42006-07-10 04:44:18 -07004261static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004262
NeilBrown88202a02006-01-06 00:21:36 -08004263static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004264sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004265{
4266 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4267 mddev->sync_speed_min ? "local": "system");
4268}
4269
4270static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004271sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004272{
4273 int min;
4274 char *e;
4275 if (strncmp(buf, "system", 6)==0) {
4276 mddev->sync_speed_min = 0;
4277 return len;
4278 }
4279 min = simple_strtoul(buf, &e, 10);
4280 if (buf == e || (*e && *e != '\n') || min <= 0)
4281 return -EINVAL;
4282 mddev->sync_speed_min = min;
4283 return len;
4284}
4285
4286static struct md_sysfs_entry md_sync_min =
4287__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4288
4289static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004290sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004291{
4292 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4293 mddev->sync_speed_max ? "local": "system");
4294}
4295
4296static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004297sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004298{
4299 int max;
4300 char *e;
4301 if (strncmp(buf, "system", 6)==0) {
4302 mddev->sync_speed_max = 0;
4303 return len;
4304 }
4305 max = simple_strtoul(buf, &e, 10);
4306 if (buf == e || (*e && *e != '\n') || max <= 0)
4307 return -EINVAL;
4308 mddev->sync_speed_max = max;
4309 return len;
4310}
4311
4312static struct md_sysfs_entry md_sync_max =
4313__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4314
Iustin Popd7f3d292007-10-16 23:30:54 -07004315static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004316degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004317{
4318 return sprintf(page, "%d\n", mddev->degraded);
4319}
4320static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004321
4322static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004323sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004324{
4325 return sprintf(page, "%d\n", mddev->parallel_resync);
4326}
4327
4328static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004329sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004330{
4331 long n;
4332
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004333 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004334 return -EINVAL;
4335
4336 if (n != 0 && n != 1)
4337 return -EINVAL;
4338
4339 mddev->parallel_resync = n;
4340
4341 if (mddev->sync_thread)
4342 wake_up(&resync_wait);
4343
4344 return len;
4345}
4346
4347/* force parallel resync, even with shared block devices */
4348static struct md_sysfs_entry md_sync_force_parallel =
4349__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4350 sync_force_parallel_show, sync_force_parallel_store);
4351
4352static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004353sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004354{
4355 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004356 if (mddev->curr_resync == 0)
4357 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004358 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4359 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004360 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004361 db = resync - mddev->resync_mark_cnt;
4362 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004363}
4364
NeilBrown80ca3a42006-07-10 04:44:18 -07004365static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004366
4367static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004368sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004369{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004370 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004371
NeilBrownacb180b2009-04-14 16:28:34 +10004372 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4373 return sprintf(page, "none\n");
4374
NeilBrown72f36d52012-10-11 14:25:57 +11004375 if (mddev->curr_resync == 1 ||
4376 mddev->curr_resync == 2)
4377 return sprintf(page, "delayed\n");
4378
NeilBrownc804cde2012-05-21 09:28:33 +10004379 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4380 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004381 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004382 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004383 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004384
NeilBrownacb180b2009-04-14 16:28:34 +10004385 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004386 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004387}
4388
NeilBrown80ca3a42006-07-10 04:44:18 -07004389static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004390
NeilBrowne464eaf2006-03-27 01:18:14 -08004391static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004392min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004393{
4394 return sprintf(page, "%llu\n",
4395 (unsigned long long)mddev->resync_min);
4396}
4397static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004398min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004399{
4400 unsigned long long min;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004401 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004402 return -EINVAL;
4403 if (min > mddev->resync_max)
4404 return -EINVAL;
4405 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4406 return -EBUSY;
4407
4408 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004409 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004410 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004411 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004412 return -EINVAL;
4413 }
4414 mddev->resync_min = min;
4415
4416 return len;
4417}
4418
4419static struct md_sysfs_entry md_min_sync =
4420__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4421
4422static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004423max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004424{
4425 if (mddev->resync_max == MaxSector)
4426 return sprintf(page, "max\n");
4427 else
4428 return sprintf(page, "%llu\n",
4429 (unsigned long long)mddev->resync_max);
4430}
4431static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004432max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004433{
4434 if (strncmp(buf, "max", 3) == 0)
4435 mddev->resync_max = MaxSector;
4436 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004437 unsigned long long max;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004438 if (kstrtoull(buf, 10, &max))
Neil Brown5e96ee62008-06-28 08:31:24 +10004439 return -EINVAL;
4440 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004441 return -EINVAL;
4442 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004443 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004444 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4445 return -EBUSY;
4446
4447 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004448 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004449 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004450 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004451 return -EINVAL;
4452 }
4453 mddev->resync_max = max;
4454 }
4455 wake_up(&mddev->recovery_wait);
4456 return len;
4457}
4458
4459static struct md_sysfs_entry md_max_sync =
4460__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4461
4462static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004463suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004464{
4465 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4466}
4467
4468static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004469suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004470{
4471 char *e;
4472 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004473 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004474
NeilBrownb8d966e2009-07-01 11:14:04 +10004475 if (mddev->pers == NULL ||
4476 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004477 return -EINVAL;
4478 if (buf == e || (*e && *e != '\n'))
4479 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004480
4481 mddev->suspend_lo = new;
4482 if (new >= old)
4483 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004484 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004485 else {
4486 /* Expanding suspended region - need to wait */
4487 mddev->pers->quiesce(mddev, 1);
4488 mddev->pers->quiesce(mddev, 0);
4489 }
4490 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004491}
4492static struct md_sysfs_entry md_suspend_lo =
4493__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4494
4495
4496static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004497suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004498{
4499 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4500}
4501
4502static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004503suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004504{
4505 char *e;
4506 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004507 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004508
NeilBrownb8d966e2009-07-01 11:14:04 +10004509 if (mddev->pers == NULL ||
4510 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004511 return -EINVAL;
4512 if (buf == e || (*e && *e != '\n'))
4513 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004514
4515 mddev->suspend_hi = new;
4516 if (new <= old)
4517 /* Shrinking suspended region */
4518 mddev->pers->quiesce(mddev, 2);
4519 else {
4520 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004521 mddev->pers->quiesce(mddev, 1);
4522 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004523 }
4524 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004525}
4526static struct md_sysfs_entry md_suspend_hi =
4527__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4528
NeilBrown08a02ec2007-05-09 02:35:38 -07004529static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004530reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004531{
4532 if (mddev->reshape_position != MaxSector)
4533 return sprintf(page, "%llu\n",
4534 (unsigned long long)mddev->reshape_position);
4535 strcpy(page, "none\n");
4536 return 5;
4537}
4538
4539static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004540reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004541{
NeilBrownc6563a82012-05-21 09:27:00 +10004542 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004543 char *e;
4544 unsigned long long new = simple_strtoull(buf, &e, 10);
4545 if (mddev->pers)
4546 return -EBUSY;
4547 if (buf == e || (*e && *e != '\n'))
4548 return -EINVAL;
4549 mddev->reshape_position = new;
4550 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004551 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004552 mddev->new_level = mddev->level;
4553 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004554 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004555 rdev_for_each(rdev, mddev)
4556 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004557 return len;
4558}
4559
4560static struct md_sysfs_entry md_reshape_position =
4561__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4562 reshape_position_store);
4563
Dan Williamsb522adc2009-03-31 15:00:31 +11004564static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004565reshape_direction_show(struct mddev *mddev, char *page)
4566{
4567 return sprintf(page, "%s\n",
4568 mddev->reshape_backwards ? "backwards" : "forwards");
4569}
4570
4571static ssize_t
4572reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4573{
4574 int backwards = 0;
4575 if (cmd_match(buf, "forwards"))
4576 backwards = 0;
4577 else if (cmd_match(buf, "backwards"))
4578 backwards = 1;
4579 else
4580 return -EINVAL;
4581 if (mddev->reshape_backwards == backwards)
4582 return len;
4583
4584 /* check if we are allowed to change */
4585 if (mddev->delta_disks)
4586 return -EBUSY;
4587
4588 if (mddev->persistent &&
4589 mddev->major_version == 0)
4590 return -EINVAL;
4591
4592 mddev->reshape_backwards = backwards;
4593 return len;
4594}
4595
4596static struct md_sysfs_entry md_reshape_direction =
4597__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4598 reshape_direction_store);
4599
4600static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004601array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004602{
4603 if (mddev->external_size)
4604 return sprintf(page, "%llu\n",
4605 (unsigned long long)mddev->array_sectors/2);
4606 else
4607 return sprintf(page, "default\n");
4608}
4609
4610static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004611array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004612{
4613 sector_t sectors;
4614
4615 if (strncmp(buf, "default", 7) == 0) {
4616 if (mddev->pers)
4617 sectors = mddev->pers->size(mddev, 0, 0);
4618 else
4619 sectors = mddev->array_sectors;
4620
4621 mddev->external_size = 0;
4622 } else {
4623 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4624 return -EINVAL;
4625 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004626 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004627
4628 mddev->external_size = 1;
4629 }
4630
4631 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004632 if (mddev->pers) {
4633 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004634 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004635 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004636 return len;
4637}
4638
4639static struct md_sysfs_entry md_array_size =
4640__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4641 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004642
NeilBrowneae17012005-11-08 21:39:23 -08004643static struct attribute *md_default_attrs[] = {
4644 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004645 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004646 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004647 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004648 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004649 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004650 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004651 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004652 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004653 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004654 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004655 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004656 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004657 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004658 NULL,
4659};
4660
4661static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004662 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004663 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004664 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004665 &md_sync_min.attr,
4666 &md_sync_max.attr,
4667 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004668 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004669 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004670 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004671 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004672 &md_suspend_lo.attr,
4673 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004674 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004675 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004676 NULL,
4677};
NeilBrown411036f2005-11-08 21:39:40 -08004678static struct attribute_group md_redundancy_group = {
4679 .name = NULL,
4680 .attrs = md_redundancy_attrs,
4681};
4682
NeilBrowneae17012005-11-08 21:39:23 -08004683
4684static ssize_t
4685md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4686{
4687 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004688 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004689 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004690
4691 if (!entry->show)
4692 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004693 spin_lock(&all_mddevs_lock);
4694 if (list_empty(&mddev->all_mddevs)) {
4695 spin_unlock(&all_mddevs_lock);
4696 return -EBUSY;
4697 }
4698 mddev_get(mddev);
4699 spin_unlock(&all_mddevs_lock);
4700
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004701 rv = mddev_lock(mddev);
4702 if (!rv) {
4703 rv = entry->show(mddev, page);
4704 mddev_unlock(mddev);
4705 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004706 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004707 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004708}
4709
4710static ssize_t
4711md_attr_store(struct kobject *kobj, struct attribute *attr,
4712 const char *page, size_t length)
4713{
4714 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004715 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004716 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004717
4718 if (!entry->store)
4719 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004720 if (!capable(CAP_SYS_ADMIN))
4721 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004722 spin_lock(&all_mddevs_lock);
4723 if (list_empty(&mddev->all_mddevs)) {
4724 spin_unlock(&all_mddevs_lock);
4725 return -EBUSY;
4726 }
4727 mddev_get(mddev);
4728 spin_unlock(&all_mddevs_lock);
NeilBrowna7a3f082012-12-11 13:35:54 +11004729 if (entry->store == new_dev_store)
4730 flush_workqueue(md_misc_wq);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004731 rv = mddev_lock(mddev);
4732 if (!rv) {
4733 rv = entry->store(mddev, page, length);
4734 mddev_unlock(mddev);
4735 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004736 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004737 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004738}
4739
4740static void md_free(struct kobject *ko)
4741{
NeilBrownfd01b882011-10-11 16:47:53 +11004742 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004743
4744 if (mddev->sysfs_state)
4745 sysfs_put(mddev->sysfs_state);
4746
4747 if (mddev->gendisk) {
4748 del_gendisk(mddev->gendisk);
4749 put_disk(mddev->gendisk);
4750 }
4751 if (mddev->queue)
4752 blk_cleanup_queue(mddev->queue);
4753
NeilBrowneae17012005-11-08 21:39:23 -08004754 kfree(mddev);
4755}
4756
Emese Revfy52cf25d2010-01-19 02:58:23 +01004757static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004758 .show = md_attr_show,
4759 .store = md_attr_store,
4760};
4761static struct kobj_type md_ktype = {
4762 .release = md_free,
4763 .sysfs_ops = &md_sysfs_ops,
4764 .default_attrs = md_default_attrs,
4765};
4766
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767int mdp_major = 0;
4768
Dan Williams5fd3a172009-03-04 00:57:25 -07004769static void mddev_delayed_delete(struct work_struct *ws)
4770{
NeilBrownfd01b882011-10-11 16:47:53 +11004771 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004772
NeilBrown43a70502009-12-14 12:49:55 +11004773 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004774 kobject_del(&mddev->kobj);
4775 kobject_put(&mddev->kobj);
4776}
4777
NeilBrownefeb53c2009-01-09 08:31:10 +11004778static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004780 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004781 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004783 int partitioned;
4784 int shift;
4785 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004786 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
4788 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004789 return -ENODEV;
4790
4791 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4792 shift = partitioned ? MdpMinorShift : 0;
4793 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Tejun Heoe804ac72010-10-15 15:36:08 +02004795 /* wait for any previous instance of this device to be
4796 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004797 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004798 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004799
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004800 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004801 error = -EEXIST;
4802 if (mddev->gendisk)
4803 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004804
4805 if (name) {
4806 /* Need to ensure that 'name' is not a duplicate.
4807 */
NeilBrownfd01b882011-10-11 16:47:53 +11004808 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004809 spin_lock(&all_mddevs_lock);
4810
4811 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4812 if (mddev2->gendisk &&
4813 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4814 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004815 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004816 }
4817 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 }
NeilBrown8b765392009-01-09 08:31:08 +11004819
NeilBrown0909dc42009-07-01 12:27:21 +10004820 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004821 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004822 if (!mddev->queue)
4823 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004824 mddev->queue->queuedata = mddev;
4825
NeilBrown409c57f2009-03-31 14:39:39 +11004826 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004827 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004828
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 disk = alloc_disk(1 << shift);
4830 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004831 blk_cleanup_queue(mddev->queue);
4832 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004833 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004835 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004837 if (name)
4838 strcpy(disk->disk_name, name);
4839 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004841 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 disk->fops = &md_fops;
4844 disk->private_data = mddev;
4845 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004846 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004847 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004848 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004849 * remove it now.
4850 */
4851 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004853 /* As soon as we call add_disk(), another thread could get
4854 * through to md_open, so make sure it doesn't get too far
4855 */
4856 mutex_lock(&mddev->open_mutex);
4857 add_disk(disk);
4858
Tejun Heoed9e1982008-08-25 19:56:05 +09004859 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4860 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004861 if (error) {
4862 /* This isn't possible, but as kobject_init_and_add is marked
4863 * __must_check, we must do something with the result
4864 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004865 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4866 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004867 error = 0;
4868 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004869 if (mddev->kobj.sd &&
4870 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004871 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004872 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004873 abort:
4874 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004875 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004876 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004877 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004878 }
NeilBrownd3374822009-01-09 08:31:10 +11004879 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004880 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004881}
4882
4883static struct kobject *md_probe(dev_t dev, int *part, void *data)
4884{
4885 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 return NULL;
4887}
4888
NeilBrownefeb53c2009-01-09 08:31:10 +11004889static int add_named_array(const char *val, struct kernel_param *kp)
4890{
4891 /* val must be "md_*" where * is not all digits.
4892 * We allocate an array with a large free minor number, and
4893 * set the name to val. val must not already be an active name.
4894 */
4895 int len = strlen(val);
4896 char buf[DISK_NAME_LEN];
4897
4898 while (len && val[len-1] == '\n')
4899 len--;
4900 if (len >= DISK_NAME_LEN)
4901 return -E2BIG;
4902 strlcpy(buf, val, len+1);
4903 if (strncmp(buf, "md_", 3) != 0)
4904 return -EINVAL;
4905 return md_alloc(0, buf);
4906}
4907
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908static void md_safemode_timeout(unsigned long data)
4909{
NeilBrownfd01b882011-10-11 16:47:53 +11004910 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
Neil Brown0fd62b82008-06-28 08:31:36 +10004912 if (!atomic_read(&mddev->writes_pending)) {
4913 mddev->safemode = 1;
4914 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004915 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 md_wakeup_thread(mddev->thread);
4918}
4919
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004920static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
NeilBrownfd01b882011-10-11 16:47:53 +11004922int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923{
NeilBrown2604b702006-01-06 00:20:36 -08004924 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004925 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004926 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927
NeilBrowna757e642005-04-16 15:26:42 -07004928 if (list_empty(&mddev->disks))
4929 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931
4932 if (mddev->pers)
4933 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004934 /* Cannot run until previous stop completes properly */
4935 if (mddev->sysfs_active)
4936 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004937
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 /*
4939 * Analyze all RAID superblock(s)
4940 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004941 if (!mddev->raid_disks) {
4942 if (!mddev->persistent)
4943 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004944 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946
NeilBrownd9d166c2006-01-06 00:20:51 -08004947 if (mddev->level != LEVEL_NONE)
4948 request_module("md-level-%d", mddev->level);
4949 else if (mddev->clevel[0])
4950 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
4952 /*
4953 * Drop all container device buffers, from now on
4954 * the only valid external interface is through the md
4955 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004957 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004958 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 continue;
4960 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004961 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004962
4963 /* perform some consistency tests on the device.
4964 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004965 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004966 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004967 if (rdev->meta_bdev) {
4968 /* Nothing to check */;
4969 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004970 if (mddev->dev_sectors &&
4971 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004972 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004973 printk("md: %s: data overlaps metadata\n",
4974 mdname(mddev));
4975 return -EINVAL;
4976 }
4977 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004978 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004979 > rdev->data_offset) {
4980 printk("md: %s: metadata overlaps data\n",
4981 mdname(mddev));
4982 return -EINVAL;
4983 }
4984 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004985 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 }
4987
NeilBrowna167f662010-10-26 18:31:13 +11004988 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07004989 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11004990
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004992 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004993 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004995 if (mddev->level != LEVEL_NONE)
4996 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4997 mddev->level);
4998 else
4999 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5000 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 return -EINVAL;
5002 }
NeilBrown2604b702006-01-06 00:20:36 -08005003 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005005 if (mddev->level != pers->level) {
5006 mddev->level = pers->level;
5007 mddev->new_level = pers->level;
5008 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005009 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
NeilBrownf6705572006-03-27 01:18:11 -08005011 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005012 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005013 /* This personality cannot handle reshaping... */
5014 mddev->pers = NULL;
5015 module_put(pers->owner);
5016 return -EINVAL;
5017 }
5018
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005019 if (pers->sync_request) {
5020 /* Warn if this is a potentially silly
5021 * configuration.
5022 */
5023 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005024 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005025 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005026
NeilBrowndafb20f2012-03-19 12:46:39 +11005027 rdev_for_each(rdev, mddev)
5028 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005029 if (rdev < rdev2 &&
5030 rdev->bdev->bd_contains ==
5031 rdev2->bdev->bd_contains) {
5032 printk(KERN_WARNING
5033 "%s: WARNING: %s appears to be"
5034 " on the same physical disk as"
5035 " %s.\n",
5036 mdname(mddev),
5037 bdevname(rdev->bdev,b),
5038 bdevname(rdev2->bdev,b2));
5039 warned = 1;
5040 }
5041 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005042
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005043 if (warned)
5044 printk(KERN_WARNING
5045 "True protection against single-disk"
5046 " failure might be compromised.\n");
5047 }
5048
NeilBrown657390d2005-08-26 18:34:16 -07005049 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005050 /* may be over-ridden by personality */
5051 mddev->resync_max_sectors = mddev->dev_sectors;
5052
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005053 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
NeilBrown0f9552b52009-12-30 12:08:50 +11005055 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005056 mddev->ro = 2; /* read-only, but switch on first write */
5057
NeilBrownb15c2e52006-01-06 00:20:16 -08005058 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005059 if (err)
5060 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005061 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5062 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5063 " but 'external_size' not in effect?\n", __func__);
5064 printk(KERN_ERR
5065 "md: invalid array_size %llu > default size %llu\n",
5066 (unsigned long long)mddev->array_sectors / 2,
5067 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5068 err = -EINVAL;
5069 mddev->pers->stop(mddev);
5070 }
NeilBrownef99bf42012-05-22 13:55:08 +10005071 if (err == 0 && mddev->pers->sync_request &&
5072 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005073 err = bitmap_create(mddev);
5074 if (err) {
5075 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5076 mdname(mddev), err);
5077 mddev->pers->stop(mddev);
5078 }
5079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 module_put(mddev->pers->owner);
5082 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005083 bitmap_destroy(mddev);
5084 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005086 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005087 if (mddev->kobj.sd &&
5088 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005089 printk(KERN_WARNING
5090 "md: cannot register extra attributes for %s\n",
5091 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005092 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005093 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005094 mddev->ro = 0;
5095
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005097 atomic_set(&mddev->max_corr_read_errors,
5098 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 mddev->safemode = 0;
5100 mddev->safemode_timer.function = md_safemode_timeout;
5101 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005102 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005104 smp_wmb();
5105 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005106 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005107 if (rdev->raid_disk >= 0)
5108 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005109 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110
5111 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5112
NeilBrown7a0a5352013-08-27 16:28:23 +10005113 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07005114 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115
NeilBrownd7603b72006-01-06 00:20:30 -08005116 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005117 sysfs_notify_dirent_safe(mddev->sysfs_state);
5118 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005119 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 return 0;
5121}
NeilBrown390ee602010-06-01 19:37:27 +10005122EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123
NeilBrownfd01b882011-10-11 16:47:53 +11005124static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005125{
5126 int err;
5127
5128 err = md_run(mddev);
5129 if (err)
5130 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005131 err = bitmap_load(mddev);
5132 if (err) {
5133 bitmap_destroy(mddev);
5134 goto out;
5135 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005136
5137 md_wakeup_thread(mddev->thread);
5138 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5139
NeilBrownfe60b012010-03-29 11:10:42 +11005140 set_capacity(mddev->gendisk, mddev->array_sectors);
5141 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005142 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005143 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5144out:
5145 return err;
5146}
5147
NeilBrownfd01b882011-10-11 16:47:53 +11005148static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149{
5150 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151
Andre Noll80fab1d2008-07-11 22:02:21 +10005152 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005154 return -ENXIO;
5155 if (!mddev->pers)
5156 return -EINVAL;
5157 if (!mddev->ro)
5158 return -EBUSY;
5159 mddev->safemode = 0;
5160 mddev->ro = 0;
5161 set_disk_ro(disk, 0);
5162 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5163 mdname(mddev));
5164 /* Kick recovery or resync if necessary */
5165 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5166 md_wakeup_thread(mddev->thread);
5167 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005168 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170}
5171
NeilBrownfd01b882011-10-11 16:47:53 +11005172static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005173{
5174 mddev->array_sectors = 0;
5175 mddev->external_size = 0;
5176 mddev->dev_sectors = 0;
5177 mddev->raid_disks = 0;
5178 mddev->recovery_cp = 0;
5179 mddev->resync_min = 0;
5180 mddev->resync_max = MaxSector;
5181 mddev->reshape_position = MaxSector;
5182 mddev->external = 0;
5183 mddev->persistent = 0;
5184 mddev->level = LEVEL_NONE;
5185 mddev->clevel[0] = 0;
5186 mddev->flags = 0;
5187 mddev->ro = 0;
5188 mddev->metadata_type[0] = 0;
5189 mddev->chunk_sectors = 0;
5190 mddev->ctime = mddev->utime = 0;
5191 mddev->layout = 0;
5192 mddev->max_disks = 0;
5193 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005194 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005195 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005196 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005197 mddev->new_level = LEVEL_NONE;
5198 mddev->new_layout = 0;
5199 mddev->new_chunk_sectors = 0;
5200 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005201 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005202 mddev->suspend_lo = mddev->suspend_hi = 0;
5203 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5204 mddev->recovery = 0;
5205 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005206 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005207 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005208 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005209 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005210 mddev->bitmap_info.offset = 0;
5211 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005212 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005213 mddev->bitmap_info.chunksize = 0;
5214 mddev->bitmap_info.daemon_sleep = 0;
5215 mddev->bitmap_info.max_write_behind = 0;
5216}
5217
NeilBrownfd01b882011-10-11 16:47:53 +11005218static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005219{
NeilBrown6b6204e2013-05-09 09:48:30 +10005220 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrowna047e122010-03-29 12:07:53 +11005221 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005222 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005223 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005224 }
5225
5226 del_timer_sync(&mddev->safemode_timer);
5227
5228 bitmap_flush(mddev);
5229 md_super_wait(mddev);
5230
NeilBrownb6d428c2013-04-24 11:42:42 +10005231 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005232 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005233 /* mark array as shutdown cleanly */
5234 mddev->in_sync = 1;
5235 md_update_sb(mddev, 1);
5236 }
5237}
NeilBrowndefad612011-01-14 09:14:33 +11005238
NeilBrownfd01b882011-10-11 16:47:53 +11005239void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005240{
NeilBrown29f097c2013-11-14 17:54:51 +11005241 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005242 __md_stop_writes(mddev);
5243 mddev_unlock(mddev);
5244}
NeilBrown390ee602010-06-01 19:37:27 +10005245EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005246
NeilBrown5eff3c42012-11-19 10:47:48 +11005247static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005248{
NeilBrown0ca69882011-01-14 09:14:33 +11005249 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005250 mddev->pers->stop(mddev);
5251 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5252 mddev->to_remove = &md_redundancy_group;
5253 module_put(mddev->pers->owner);
5254 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005255 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005256}
NeilBrown5eff3c42012-11-19 10:47:48 +11005257
5258void md_stop(struct mddev *mddev)
5259{
5260 /* stop the array and free an attached data structures.
5261 * This is called from dm-raid
5262 */
5263 __md_stop(mddev);
5264 bitmap_destroy(mddev);
5265 if (mddev->bio_set)
5266 bioset_free(mddev->bio_set);
5267}
5268
NeilBrown390ee602010-06-01 19:37:27 +10005269EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005270
NeilBrowna05b7ea2012-07-19 15:59:18 +10005271static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005272{
5273 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005274 int did_freeze = 0;
5275
5276 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5277 did_freeze = 1;
5278 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5279 md_wakeup_thread(mddev->thread);
5280 }
5281 if (mddev->sync_thread) {
5282 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5283 /* Thread might be blocked waiting for metadata update
5284 * which will now never happen */
5285 wake_up_process(mddev->sync_thread->tsk);
5286 }
5287 mddev_unlock(mddev);
5288 wait_event(resync_wait, mddev->sync_thread == NULL);
5289 mddev_lock_nointr(mddev);
5290
NeilBrowna4bd82d2010-03-29 13:23:10 +11005291 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005292 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005293 mddev->sync_thread ||
5294 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005295 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005296 if (did_freeze) {
5297 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5298 md_wakeup_thread(mddev->thread);
5299 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005300 err = -EBUSY;
5301 goto out;
5302 }
5303 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005304 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005305
5306 err = -ENXIO;
5307 if (mddev->ro==1)
5308 goto out;
5309 mddev->ro = 1;
5310 set_disk_ro(mddev->gendisk, 1);
5311 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005312 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005313 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005314 }
5315out:
5316 mutex_unlock(&mddev->open_mutex);
5317 return err;
5318}
5319
NeilBrown9e653b62006-06-26 00:27:58 -07005320/* mode:
5321 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005322 * 2 - stop but do not disassemble array
5323 */
NeilBrowna05b7ea2012-07-19 15:59:18 +10005324static int do_md_stop(struct mddev * mddev, int mode,
5325 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005328 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005329 int did_freeze = 0;
5330
5331 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5332 did_freeze = 1;
5333 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5334 md_wakeup_thread(mddev->thread);
5335 }
5336 if (mddev->sync_thread) {
5337 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5338 /* Thread might be blocked waiting for metadata update
5339 * which will now never happen */
5340 wake_up_process(mddev->sync_thread->tsk);
5341 }
5342 mddev_unlock(mddev);
5343 wait_event(resync_wait, mddev->sync_thread == NULL);
5344 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345
NeilBrownc8c00a62009-08-10 12:50:52 +10005346 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005347 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005348 mddev->sysfs_active ||
5349 mddev->sync_thread ||
5350 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005351 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005352 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005353 if (did_freeze) {
5354 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5355 md_wakeup_thread(mddev->thread);
5356 }
NeilBrown260fa032013-08-27 16:44:13 +10005357 return -EBUSY;
5358 }
NeilBrown6e17b022010-08-07 21:41:19 +10005359 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005360 if (mddev->ro)
5361 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005362
NeilBrowndefad612011-01-14 09:14:33 +11005363 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005364 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005365 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005366 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005367
NeilBrowna4bd82d2010-03-29 13:23:10 +11005368 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005369 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005370
NeilBrowndafb20f2012-03-19 12:46:39 +11005371 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005372 if (rdev->raid_disk >= 0)
5373 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005374
NeilBrowna4bd82d2010-03-29 13:23:10 +11005375 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005376 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005377 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005378 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005379
NeilBrowna4bd82d2010-03-29 13:23:10 +11005380 if (mddev->ro)
5381 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005382 } else
5383 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 /*
5385 * Free resources if final stop
5386 */
NeilBrown9e653b62006-06-26 00:27:58 -07005387 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5389
NeilBrown978f9462006-02-02 14:28:05 -08005390 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005391 if (mddev->bitmap_info.file) {
NeilBrownc3d97142009-12-14 12:49:52 +11005392 fput(mddev->bitmap_info.file);
5393 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005394 }
NeilBrownc3d97142009-12-14 12:49:52 +11005395 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005396
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 export_array(mddev);
5398
NeilBrown6177b472010-03-29 11:37:13 +11005399 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005400 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005401 if (mddev->hold_active == UNTIL_STOP)
5402 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005403 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005404 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005405 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005406 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408}
5409
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005410#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005411static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412{
NeilBrown3cb03002011-10-11 16:45:26 +11005413 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 int err;
5415
NeilBrowna757e642005-04-16 15:26:42 -07005416 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418
5419 printk(KERN_INFO "md: running: ");
5420
NeilBrowndafb20f2012-03-19 12:46:39 +11005421 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 char b[BDEVNAME_SIZE];
5423 printk("<%s>", bdevname(rdev->bdev,b));
5424 }
5425 printk("\n");
5426
NeilBrownd710e132008-10-13 11:55:12 +11005427 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 if (err) {
5429 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005430 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 }
5432}
5433
5434/*
5435 * lets try to run arrays based on all disks that have arrived
5436 * until now. (those are in pending_raid_disks)
5437 *
5438 * the method: pick the first pending disk, collect all disks with
5439 * the same UUID, remove all from the pending list and put them into
5440 * the 'same_array' list. Then order this list based on superblock
5441 * update time (freshest comes first), kick out 'old' disks and
5442 * compare superblocks. If everything's fine then run it.
5443 *
5444 * If "unit" is allocated, then bump its reference count
5445 */
5446static void autorun_devices(int part)
5447{
NeilBrown3cb03002011-10-11 16:45:26 +11005448 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005449 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 char b[BDEVNAME_SIZE];
5451
5452 printk(KERN_INFO "md: autorun ...\n");
5453 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005454 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005456 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005458 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
5460 printk(KERN_INFO "md: considering %s ...\n",
5461 bdevname(rdev0->bdev,b));
5462 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005463 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 if (super_90_load(rdev, rdev0, 0) >= 0) {
5465 printk(KERN_INFO "md: adding %s ...\n",
5466 bdevname(rdev->bdev,b));
5467 list_move(&rdev->same_set, &candidates);
5468 }
5469 /*
5470 * now we have a set of devices, with all of them having
5471 * mostly sane superblocks. It's time to allocate the
5472 * mddev.
5473 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005474 if (part) {
5475 dev = MKDEV(mdp_major,
5476 rdev0->preferred_minor << MdpMinorShift);
5477 unit = MINOR(dev) >> MdpMinorShift;
5478 } else {
5479 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5480 unit = MINOR(dev);
5481 }
5482 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5484 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5485 break;
5486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
5488 md_probe(dev, NULL, NULL);
5489 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005490 if (!mddev || !mddev->gendisk) {
5491 if (mddev)
5492 mddev_put(mddev);
5493 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 "md: cannot allocate memory for md drive.\n");
5495 break;
5496 }
5497 if (mddev_lock(mddev))
5498 printk(KERN_WARNING "md: %s locked, cannot run\n",
5499 mdname(mddev));
5500 else if (mddev->raid_disks || mddev->major_version
5501 || !list_empty(&mddev->disks)) {
5502 printk(KERN_WARNING
5503 "md: %s already running, cannot run %s\n",
5504 mdname(mddev), bdevname(rdev0->bdev,b));
5505 mddev_unlock(mddev);
5506 } else {
5507 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005508 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005509 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 list_del_init(&rdev->same_set);
5511 if (bind_rdev_to_array(rdev, mddev))
5512 export_rdev(rdev);
5513 }
5514 autorun_array(mddev);
5515 mddev_unlock(mddev);
5516 }
5517 /* on success, candidates will be empty, on error
5518 * it won't...
5519 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005520 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005521 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 mddev_put(mddev);
5525 }
5526 printk(KERN_INFO "md: ... autorun DONE.\n");
5527}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005528#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530static int get_version(void __user * arg)
5531{
5532 mdu_version_t ver;
5533
5534 ver.major = MD_MAJOR_VERSION;
5535 ver.minor = MD_MINOR_VERSION;
5536 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5537
5538 if (copy_to_user(arg, &ver, sizeof(ver)))
5539 return -EFAULT;
5540
5541 return 0;
5542}
5543
NeilBrownfd01b882011-10-11 16:47:53 +11005544static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545{
5546 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005547 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005548 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549
NeilBrown1ca69c42012-10-11 13:37:33 +11005550 nr = working = insync = failed = spare = 0;
5551 rcu_read_lock();
5552 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005554 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 failed++;
5556 else {
5557 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005558 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005559 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 else
5561 spare++;
5562 }
5563 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005564 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
5566 info.major_version = mddev->major_version;
5567 info.minor_version = mddev->minor_version;
5568 info.patch_version = MD_PATCHLEVEL_VERSION;
5569 info.ctime = mddev->ctime;
5570 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005571 info.size = mddev->dev_sectors / 2;
5572 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005573 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 info.nr_disks = nr;
5575 info.raid_disks = mddev->raid_disks;
5576 info.md_minor = mddev->md_minor;
5577 info.not_persistent= !mddev->persistent;
5578
5579 info.utime = mddev->utime;
5580 info.state = 0;
5581 if (mddev->in_sync)
5582 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005583 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005584 info.state |= (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005585 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 info.working_disks = working;
5587 info.failed_disks = failed;
5588 info.spare_disks = spare;
5589
5590 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005591 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
5593 if (copy_to_user(arg, &info, sizeof(info)))
5594 return -EFAULT;
5595
5596 return 0;
5597}
5598
NeilBrownfd01b882011-10-11 16:47:53 +11005599static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005600{
5601 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5602 char *ptr, *buf = NULL;
5603 int err = -ENOMEM;
5604
NeilBrown60559da2013-07-16 16:44:29 +10005605 file = kmalloc(sizeof(*file), GFP_NOIO);
NeilBrown2a2275d2007-01-26 00:57:11 -08005606
NeilBrown32a76272005-06-21 17:17:14 -07005607 if (!file)
5608 goto out;
5609
5610 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005611 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005612 file->pathname[0] = '\0';
5613 goto copy_out;
5614 }
5615
5616 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5617 if (!buf)
5618 goto out;
5619
NeilBrown1ec885c2012-05-22 13:55:10 +10005620 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5621 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005622 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005623 goto out;
5624
5625 strcpy(file->pathname, ptr);
5626
5627copy_out:
5628 err = 0;
5629 if (copy_to_user(arg, file, sizeof(*file)))
5630 err = -EFAULT;
5631out:
5632 kfree(buf);
5633 kfree(file);
5634 return err;
5635}
5636
NeilBrownfd01b882011-10-11 16:47:53 +11005637static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638{
5639 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005640 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
5642 if (copy_from_user(&info, arg, sizeof(info)))
5643 return -EFAULT;
5644
NeilBrown1ca69c42012-10-11 13:37:33 +11005645 rcu_read_lock();
5646 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 if (rdev) {
5648 info.major = MAJOR(rdev->bdev->bd_dev);
5649 info.minor = MINOR(rdev->bdev->bd_dev);
5650 info.raid_disk = rdev->raid_disk;
5651 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005652 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005654 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 info.state |= (1<<MD_DISK_ACTIVE);
5656 info.state |= (1<<MD_DISK_SYNC);
5657 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005658 if (test_bit(WriteMostly, &rdev->flags))
5659 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 } else {
5661 info.major = info.minor = 0;
5662 info.raid_disk = -1;
5663 info.state = (1<<MD_DISK_REMOVED);
5664 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005665 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
5667 if (copy_to_user(arg, &info, sizeof(info)))
5668 return -EFAULT;
5669
5670 return 0;
5671}
5672
NeilBrownfd01b882011-10-11 16:47:53 +11005673static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674{
5675 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005676 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 dev_t dev = MKDEV(info->major,info->minor);
5678
5679 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5680 return -EOVERFLOW;
5681
5682 if (!mddev->raid_disks) {
5683 int err;
5684 /* expecting a device which has a superblock */
5685 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5686 if (IS_ERR(rdev)) {
5687 printk(KERN_WARNING
5688 "md: md_import_device returned %ld\n",
5689 PTR_ERR(rdev));
5690 return PTR_ERR(rdev);
5691 }
5692 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005693 struct md_rdev *rdev0
5694 = list_entry(mddev->disks.next,
5695 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005696 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 .load_super(rdev, rdev0, mddev->minor_version);
5698 if (err < 0) {
5699 printk(KERN_WARNING
5700 "md: %s has different UUID to %s\n",
5701 bdevname(rdev->bdev,b),
5702 bdevname(rdev0->bdev,b2));
5703 export_rdev(rdev);
5704 return -EINVAL;
5705 }
5706 }
5707 err = bind_rdev_to_array(rdev, mddev);
5708 if (err)
5709 export_rdev(rdev);
5710 return err;
5711 }
5712
5713 /*
5714 * add_new_disk can be used once the array is assembled
5715 * to add "hot spares". They must already have a superblock
5716 * written
5717 */
5718 if (mddev->pers) {
5719 int err;
5720 if (!mddev->pers->hot_add_disk) {
5721 printk(KERN_WARNING
5722 "%s: personality does not support diskops!\n",
5723 mdname(mddev));
5724 return -EINVAL;
5725 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005726 if (mddev->persistent)
5727 rdev = md_import_device(dev, mddev->major_version,
5728 mddev->minor_version);
5729 else
5730 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 if (IS_ERR(rdev)) {
5732 printk(KERN_WARNING
5733 "md: md_import_device returned %ld\n",
5734 PTR_ERR(rdev));
5735 return PTR_ERR(rdev);
5736 }
NeilBrown1a855a02010-12-09 16:36:28 +11005737 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005738 if (!mddev->persistent) {
5739 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005740 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005741 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005742 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005743 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005744 } else
NeilBrown41158c72005-06-21 17:17:25 -07005745 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005746 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005747 } else
5748 super_types[mddev->major_version].
5749 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005750 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005751 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005752 /* This was a hot-add request, but events doesn't
5753 * match, so reject it.
5754 */
5755 export_rdev(rdev);
5756 return -EINVAL;
5757 }
5758
NeilBrownb2d444d2005-11-08 21:39:31 -08005759 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005760 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5761 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005762 else
5763 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005764
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 rdev->raid_disk = -1;
5766 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005767 if (!err && !mddev->pers->hot_remove_disk) {
5768 /* If there is hot_add_disk but no hot_remove_disk
5769 * then added disks for geometry changes,
5770 * and should be added immediately.
5771 */
5772 super_types[mddev->major_version].
5773 validate_super(mddev, rdev);
5774 err = mddev->pers->hot_add_disk(mddev, rdev);
5775 if (err)
5776 unbind_rdev_from_array(rdev);
5777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 if (err)
5779 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005780 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005781 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005782
NeilBrown7ceb17e2013-04-24 11:42:42 +10005783 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005784 if (mddev->degraded)
5785 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005786 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005787 if (!err)
5788 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005789 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 return err;
5791 }
5792
5793 /* otherwise, add_new_disk is only allowed
5794 * for major_version==0 superblocks
5795 */
5796 if (mddev->major_version != 0) {
5797 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5798 mdname(mddev));
5799 return -EINVAL;
5800 }
5801
5802 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5803 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005804 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 if (IS_ERR(rdev)) {
5806 printk(KERN_WARNING
5807 "md: error, md_import_device() returned %ld\n",
5808 PTR_ERR(rdev));
5809 return PTR_ERR(rdev);
5810 }
5811 rdev->desc_nr = info->number;
5812 if (info->raid_disk < mddev->raid_disks)
5813 rdev->raid_disk = info->raid_disk;
5814 else
5815 rdev->raid_disk = -1;
5816
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005818 if (info->state & (1<<MD_DISK_SYNC))
5819 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005821 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5822 set_bit(WriteMostly, &rdev->flags);
5823
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 if (!mddev->persistent) {
5825 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005826 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5827 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005828 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005829 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830
NeilBrown2bf071b2006-01-06 00:20:55 -08005831 err = bind_rdev_to_array(rdev, mddev);
5832 if (err) {
5833 export_rdev(rdev);
5834 return err;
5835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 }
5837
5838 return 0;
5839}
5840
NeilBrownfd01b882011-10-11 16:47:53 +11005841static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842{
5843 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005844 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 rdev = find_rdev(mddev, dev);
5847 if (!rdev)
5848 return -ENXIO;
5849
NeilBrown3ea8929d2013-04-24 11:42:41 +10005850 clear_bit(Blocked, &rdev->flags);
5851 remove_and_add_spares(mddev, rdev);
5852
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 if (rdev->raid_disk >= 0)
5854 goto busy;
5855
5856 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005857 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005858 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859
5860 return 0;
5861busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005862 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 bdevname(rdev->bdev,b), mdname(mddev));
5864 return -EBUSY;
5865}
5866
NeilBrownfd01b882011-10-11 16:47:53 +11005867static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868{
5869 char b[BDEVNAME_SIZE];
5870 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005871 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
5873 if (!mddev->pers)
5874 return -ENODEV;
5875
5876 if (mddev->major_version != 0) {
5877 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5878 " version-0 superblocks.\n",
5879 mdname(mddev));
5880 return -EINVAL;
5881 }
5882 if (!mddev->pers->hot_add_disk) {
5883 printk(KERN_WARNING
5884 "%s: personality does not support diskops!\n",
5885 mdname(mddev));
5886 return -EINVAL;
5887 }
5888
NeilBrownd710e132008-10-13 11:55:12 +11005889 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 if (IS_ERR(rdev)) {
5891 printk(KERN_WARNING
5892 "md: error, md_import_device() returned %ld\n",
5893 PTR_ERR(rdev));
5894 return -EINVAL;
5895 }
5896
5897 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005898 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005900 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
NeilBrown8190e752009-06-18 08:48:58 +10005902 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
NeilBrownb2d444d2005-11-08 21:39:31 -08005904 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 printk(KERN_WARNING
5906 "md: can not hot-add faulty %s disk to %s!\n",
5907 bdevname(rdev->bdev,b), mdname(mddev));
5908 err = -EINVAL;
5909 goto abort_export;
5910 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005911 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005913 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005914 err = bind_rdev_to_array(rdev, mddev);
5915 if (err)
5916 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917
5918 /*
5919 * The rest should better be atomic, we can have disk failures
5920 * noticed in interrupt contexts ...
5921 */
5922
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 rdev->raid_disk = -1;
5924
NeilBrown850b2b422006-10-03 01:15:46 -07005925 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
5927 /*
5928 * Kick recovery, maybe this spare has to be added to the
5929 * array immediately.
5930 */
5931 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5932 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005933 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 return 0;
5935
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936abort_export:
5937 export_rdev(rdev);
5938 return err;
5939}
5940
NeilBrownfd01b882011-10-11 16:47:53 +11005941static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005942{
NeilBrown035328c2014-04-09 12:25:40 +10005943 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07005944
NeilBrown36fa3062005-09-09 16:23:45 -07005945 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10005946 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07005947 return -EBUSY;
5948 if (mddev->recovery || mddev->sync_thread)
5949 return -EBUSY;
5950 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005951 }
5952
NeilBrown36fa3062005-09-09 16:23:45 -07005953
5954 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10005955 struct inode *inode;
NeilBrown36fa3062005-09-09 16:23:45 -07005956 if (mddev->bitmap)
5957 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005958 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005959
NeilBrownc3d97142009-12-14 12:49:52 +11005960 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005961 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5962 mdname(mddev));
5963 return -EBADF;
5964 }
5965
NeilBrown035328c2014-04-09 12:25:40 +10005966 inode = mddev->bitmap_info.file->f_mapping->host;
5967 if (!S_ISREG(inode->i_mode)) {
5968 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
5969 mdname(mddev));
5970 err = -EBADF;
5971 } else if (!(mddev->bitmap_info.file->f_mode & FMODE_WRITE)) {
5972 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
5973 mdname(mddev));
5974 err = -EBADF;
5975 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07005976 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5977 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10005978 err = -EBUSY;
5979 }
5980 if (err) {
NeilBrownc3d97142009-12-14 12:49:52 +11005981 fput(mddev->bitmap_info.file);
5982 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005983 return err;
5984 }
NeilBrownc3d97142009-12-14 12:49:52 +11005985 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005986 } else if (mddev->bitmap == NULL)
5987 return -ENOENT; /* cannot remove what isn't there */
5988 err = 0;
5989 if (mddev->pers) {
5990 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005991 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005992 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005993 if (!err)
5994 err = bitmap_load(mddev);
5995 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005996 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005997 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005998 fd = -1; /* make sure to put the file */
5999 }
NeilBrown36fa3062005-09-09 16:23:45 -07006000 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006001 }
6002 if (fd < 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006003 if (mddev->bitmap_info.file)
NeilBrownc3d97142009-12-14 12:49:52 +11006004 fput(mddev->bitmap_info.file);
NeilBrownc3d97142009-12-14 12:49:52 +11006005 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006006 }
6007
NeilBrown32a76272005-06-21 17:17:14 -07006008 return err;
6009}
6010
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011/*
6012 * set_array_info is used two different ways
6013 * The original usage is when creating a new array.
6014 * In this usage, raid_disks is > 0 and it together with
6015 * level, size, not_persistent,layout,chunksize determine the
6016 * shape of the array.
6017 * This will always create an array with a type-0.90.0 superblock.
6018 * The newer usage is when assembling an array.
6019 * In this case raid_disks will be 0, and the major_version field is
6020 * use to determine which style super-blocks are to be found on the devices.
6021 * The minor and patch _version numbers are also kept incase the
6022 * super_block handler wishes to interpret them.
6023 */
NeilBrownfd01b882011-10-11 16:47:53 +11006024static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025{
6026
6027 if (info->raid_disks == 0) {
6028 /* just setting version number for superblock loading */
6029 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006030 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 super_types[info->major_version].name == NULL) {
6032 /* maybe try to auto-load a module? */
6033 printk(KERN_INFO
6034 "md: superblock version %d not known\n",
6035 info->major_version);
6036 return -EINVAL;
6037 }
6038 mddev->major_version = info->major_version;
6039 mddev->minor_version = info->minor_version;
6040 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006041 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006042 /* ensure mddev_put doesn't delete this now that there
6043 * is some minimal configuration.
6044 */
6045 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 return 0;
6047 }
6048 mddev->major_version = MD_MAJOR_VERSION;
6049 mddev->minor_version = MD_MINOR_VERSION;
6050 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6051 mddev->ctime = get_seconds();
6052
6053 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006054 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006055 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 mddev->raid_disks = info->raid_disks;
6057 /* don't set md_minor, it is determined by which /dev/md* was
6058 * openned
6059 */
6060 if (info->state & (1<<MD_SB_CLEAN))
6061 mddev->recovery_cp = MaxSector;
6062 else
6063 mddev->recovery_cp = 0;
6064 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006065 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066
6067 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006068 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069
6070 mddev->max_disks = MD_SB_DISKS;
6071
NeilBrowne6910632008-02-06 01:39:51 -08006072 if (mddev->persistent)
6073 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006074 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
NeilBrownc3d97142009-12-14 12:49:52 +11006076 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006077 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006078 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006079
NeilBrownf6705572006-03-27 01:18:11 -08006080 mddev->reshape_position = MaxSector;
6081
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 /*
6083 * Generate a 128 bit UUID
6084 */
6085 get_random_bytes(mddev->uuid, 16);
6086
NeilBrownf6705572006-03-27 01:18:11 -08006087 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006088 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006089 mddev->new_layout = mddev->layout;
6090 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006091 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006092
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 return 0;
6094}
6095
NeilBrownfd01b882011-10-11 16:47:53 +11006096void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006097{
Dan Williamsb522adc2009-03-31 15:00:31 +11006098 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6099
6100 if (mddev->external_size)
6101 return;
6102
Dan Williams1f403622009-03-31 14:59:03 +11006103 mddev->array_sectors = array_sectors;
6104}
6105EXPORT_SYMBOL(md_set_array_sectors);
6106
NeilBrownfd01b882011-10-11 16:47:53 +11006107static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006108{
NeilBrown3cb03002011-10-11 16:45:26 +11006109 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006110 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006111 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006112
6113 if (mddev->pers->resize == NULL)
6114 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006115 /* The "num_sectors" is the number of sectors of each device that
6116 * is used. This can only make sense for arrays with redundancy.
6117 * linear and raid0 always use whatever space is available. We can only
6118 * consider changing this number if no resync or reconstruction is
6119 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006120 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006121 * of each device. If num_sectors is zero, we find the largest size
6122 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006123 */
6124 if (mddev->sync_thread)
6125 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006126 if (mddev->ro)
6127 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006128
NeilBrowndafb20f2012-03-19 12:46:39 +11006129 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006130 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006131
Andre Nolld71f9f82008-07-11 22:02:22 +10006132 if (fit && (num_sectors == 0 || num_sectors > avail))
6133 num_sectors = avail;
6134 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006135 return -ENOSPC;
6136 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006137 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006138 if (!rv)
6139 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006140 return rv;
6141}
6142
NeilBrownfd01b882011-10-11 16:47:53 +11006143static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006144{
6145 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006146 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006147 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006148 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006149 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006150 if (mddev->ro)
6151 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006152 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006153 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006154 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006155 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006156 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006157
6158 rdev_for_each(rdev, mddev) {
6159 if (mddev->raid_disks < raid_disks &&
6160 rdev->data_offset < rdev->new_data_offset)
6161 return -EINVAL;
6162 if (mddev->raid_disks > raid_disks &&
6163 rdev->data_offset > rdev->new_data_offset)
6164 return -EINVAL;
6165 }
6166
NeilBrown63c70c42006-03-27 01:18:13 -08006167 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006168 if (mddev->delta_disks < 0)
6169 mddev->reshape_backwards = 1;
6170 else if (mddev->delta_disks > 0)
6171 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006172
6173 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006174 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006175 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006176 mddev->reshape_backwards = 0;
6177 }
NeilBrownda943b992006-01-06 00:20:54 -08006178 return rv;
6179}
6180
6181
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182/*
6183 * update_array_info is used to change the configuration of an
6184 * on-line array.
6185 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6186 * fields in the info are checked against the array.
6187 * Any differences that cannot be handled will cause an error.
6188 * Normally, only one change can be managed at a time.
6189 */
NeilBrownfd01b882011-10-11 16:47:53 +11006190static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191{
6192 int rv = 0;
6193 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006194 int state = 0;
6195
6196 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006197 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006198 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199
6200 if (mddev->major_version != info->major_version ||
6201 mddev->minor_version != info->minor_version ||
6202/* mddev->patch_version != info->patch_version || */
6203 mddev->ctime != info->ctime ||
6204 mddev->level != info->level ||
6205/* mddev->layout != info->layout || */
6206 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006207 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006208 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6209 ((state^info->state) & 0xfffffe00)
6210 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 return -EINVAL;
6212 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006213 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6214 cnt++;
6215 if (mddev->raid_disks != info->raid_disks)
6216 cnt++;
6217 if (mddev->layout != info->layout)
6218 cnt++;
6219 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6220 cnt++;
6221 if (cnt == 0)
6222 return 0;
6223 if (cnt > 1)
6224 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225
6226 if (mddev->layout != info->layout) {
6227 /* Change layout
6228 * we don't need to do anything at the md level, the
6229 * personality will take care of it all.
6230 */
NeilBrown50ac1682009-06-18 08:47:55 +10006231 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006233 else {
6234 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006235 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006236 if (rv)
6237 mddev->new_layout = mddev->layout;
6238 return rv;
6239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006241 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006242 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
NeilBrownda943b992006-01-06 00:20:54 -08006244 if (mddev->raid_disks != info->raid_disks)
6245 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246
NeilBrown36fa3062005-09-09 16:23:45 -07006247 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006248 if (mddev->pers->quiesce == NULL || mddev->thread == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -07006249 return -EINVAL;
6250 if (mddev->recovery || mddev->sync_thread)
6251 return -EBUSY;
6252 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6253 /* add the bitmap */
6254 if (mddev->bitmap)
6255 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006256 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006257 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006258 mddev->bitmap_info.offset =
6259 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006260 mddev->bitmap_info.space =
6261 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006262 mddev->pers->quiesce(mddev, 1);
6263 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006264 if (!rv)
6265 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006266 if (rv)
6267 bitmap_destroy(mddev);
6268 mddev->pers->quiesce(mddev, 0);
6269 } else {
6270 /* remove the bitmap */
6271 if (!mddev->bitmap)
6272 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006273 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006274 return -EINVAL;
6275 mddev->pers->quiesce(mddev, 1);
6276 bitmap_destroy(mddev);
6277 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006278 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006279 }
6280 }
NeilBrown850b2b422006-10-03 01:15:46 -07006281 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 return rv;
6283}
6284
NeilBrownfd01b882011-10-11 16:47:53 +11006285static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286{
NeilBrown3cb03002011-10-11 16:45:26 +11006287 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006288 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289
6290 if (mddev->pers == NULL)
6291 return -ENODEV;
6292
NeilBrown1ca69c42012-10-11 13:37:33 +11006293 rcu_read_lock();
6294 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006296 err = -ENODEV;
6297 else {
6298 md_error(mddev, rdev);
6299 if (!test_bit(Faulty, &rdev->flags))
6300 err = -EBUSY;
6301 }
6302 rcu_read_unlock();
6303 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304}
6305
Andre Noll2f9618c2008-04-25 18:57:58 +02006306/*
6307 * We have a problem here : there is no easy way to give a CHS
6308 * virtual geometry. We currently pretend that we have a 2 heads
6309 * 4 sectors (with a BIG number of cylinders...). This drives
6310 * dosfs just mad... ;-)
6311 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006312static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6313{
NeilBrownfd01b882011-10-11 16:47:53 +11006314 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006315
6316 geo->heads = 2;
6317 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006318 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006319 return 0;
6320}
6321
Nicolas Schichancb335f82014-01-15 16:58:52 +01006322static inline bool md_ioctl_valid(unsigned int cmd)
6323{
6324 switch (cmd) {
6325 case ADD_NEW_DISK:
6326 case BLKROSET:
6327 case GET_ARRAY_INFO:
6328 case GET_BITMAP_FILE:
6329 case GET_DISK_INFO:
6330 case HOT_ADD_DISK:
6331 case HOT_REMOVE_DISK:
6332 case PRINT_RAID_DEBUG:
6333 case RAID_AUTORUN:
6334 case RAID_VERSION:
6335 case RESTART_ARRAY_RW:
6336 case RUN_ARRAY:
6337 case SET_ARRAY_INFO:
6338 case SET_BITMAP_FILE:
6339 case SET_DISK_FAULTY:
6340 case STOP_ARRAY:
6341 case STOP_ARRAY_RO:
6342 return true;
6343 default:
6344 return false;
6345 }
6346}
6347
Al Viroa39907f2008-03-02 10:31:15 -05006348static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 unsigned int cmd, unsigned long arg)
6350{
6351 int err = 0;
6352 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006353 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006354 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355
Nicolas Schichancb335f82014-01-15 16:58:52 +01006356 if (!md_ioctl_valid(cmd))
6357 return -ENOTTY;
6358
NeilBrown506c9e42011-12-23 10:17:26 +11006359 switch (cmd) {
6360 case RAID_VERSION:
6361 case GET_ARRAY_INFO:
6362 case GET_DISK_INFO:
6363 break;
6364 default:
6365 if (!capable(CAP_SYS_ADMIN))
6366 return -EACCES;
6367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368
6369 /*
6370 * Commands dealing with the RAID driver but not any
6371 * particular array:
6372 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006373 switch (cmd) {
6374 case RAID_VERSION:
6375 err = get_version(argp);
6376 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
NeilBrownc02c0ae2012-12-11 13:39:21 +11006378 case PRINT_RAID_DEBUG:
6379 err = 0;
6380 md_print_devices();
6381 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382
6383#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006384 case RAID_AUTORUN:
6385 err = 0;
6386 autostart_arrays(arg);
6387 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006389 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 }
6391
6392 /*
6393 * Commands creating/starting a new array:
6394 */
6395
Al Viroa39907f2008-03-02 10:31:15 -05006396 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397
6398 if (!mddev) {
6399 BUG();
6400 goto abort;
6401 }
6402
NeilBrown1ca69c42012-10-11 13:37:33 +11006403 /* Some actions do not requires the mutex */
6404 switch (cmd) {
6405 case GET_ARRAY_INFO:
6406 if (!mddev->raid_disks && !mddev->external)
6407 err = -ENODEV;
6408 else
6409 err = get_array_info(mddev, argp);
6410 goto abort;
6411
6412 case GET_DISK_INFO:
6413 if (!mddev->raid_disks && !mddev->external)
6414 err = -ENODEV;
6415 else
6416 err = get_disk_info(mddev, argp);
6417 goto abort;
6418
6419 case SET_DISK_FAULTY:
6420 err = set_disk_faulty(mddev, new_decode_dev(arg));
6421 goto abort;
6422 }
6423
NeilBrowna7a3f082012-12-11 13:35:54 +11006424 if (cmd == ADD_NEW_DISK)
6425 /* need to ensure md_delayed_delete() has completed */
6426 flush_workqueue(md_misc_wq);
6427
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006428 if (cmd == HOT_REMOVE_DISK)
6429 /* need to ensure recovery thread has run */
6430 wait_event_interruptible_timeout(mddev->sb_wait,
6431 !test_bit(MD_RECOVERY_NEEDED,
6432 &mddev->flags),
6433 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006434 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6435 /* Need to flush page cache, and ensure no-one else opens
6436 * and writes
6437 */
6438 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006439 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006440 mutex_unlock(&mddev->open_mutex);
6441 err = -EBUSY;
6442 goto abort;
6443 }
6444 set_bit(MD_STILL_CLOSED, &mddev->flags);
6445 mutex_unlock(&mddev->open_mutex);
6446 sync_blockdev(bdev);
6447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 err = mddev_lock(mddev);
6449 if (err) {
6450 printk(KERN_INFO
6451 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6452 err, cmd);
6453 goto abort;
6454 }
6455
NeilBrownc02c0ae2012-12-11 13:39:21 +11006456 if (cmd == SET_ARRAY_INFO) {
6457 mdu_array_info_t info;
6458 if (!arg)
6459 memset(&info, 0, sizeof(info));
6460 else if (copy_from_user(&info, argp, sizeof(info))) {
6461 err = -EFAULT;
6462 goto abort_unlock;
6463 }
6464 if (mddev->pers) {
6465 err = update_array_info(mddev, &info);
6466 if (err) {
6467 printk(KERN_WARNING "md: couldn't update"
6468 " array info. %d\n", err);
6469 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 }
6471 goto done_unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006472 }
6473 if (!list_empty(&mddev->disks)) {
6474 printk(KERN_WARNING
6475 "md: array %s already has disks!\n",
6476 mdname(mddev));
6477 err = -EBUSY;
6478 goto abort_unlock;
6479 }
6480 if (mddev->raid_disks) {
6481 printk(KERN_WARNING
6482 "md: array %s already initialised!\n",
6483 mdname(mddev));
6484 err = -EBUSY;
6485 goto abort_unlock;
6486 }
6487 err = set_array_info(mddev, &info);
6488 if (err) {
6489 printk(KERN_WARNING "md: couldn't set"
6490 " array info. %d\n", err);
6491 goto abort_unlock;
6492 }
6493 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494 }
6495
6496 /*
6497 * Commands querying/configuring an existing array:
6498 */
NeilBrown32a76272005-06-21 17:17:14 -07006499 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006500 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006501 if ((!mddev->raid_disks && !mddev->external)
6502 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6503 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6504 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 err = -ENODEV;
6506 goto abort_unlock;
6507 }
6508
6509 /*
6510 * Commands even a read-only array can execute:
6511 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006512 switch (cmd) {
6513 case GET_BITMAP_FILE:
6514 err = get_bitmap_file(mddev, argp);
6515 goto done_unlock;
6516
6517 case RESTART_ARRAY_RW:
6518 err = restart_array(mddev);
6519 goto done_unlock;
6520
6521 case STOP_ARRAY:
6522 err = do_md_stop(mddev, 0, bdev);
6523 goto done_unlock;
6524
6525 case STOP_ARRAY_RO:
6526 err = md_set_readonly(mddev, bdev);
6527 goto done_unlock;
6528
NeilBrown3ea8929d2013-04-24 11:42:41 +10006529 case HOT_REMOVE_DISK:
6530 err = hot_remove_disk(mddev, new_decode_dev(arg));
6531 goto done_unlock;
6532
NeilBrown7ceb17e2013-04-24 11:42:42 +10006533 case ADD_NEW_DISK:
6534 /* We can support ADD_NEW_DISK on read-only arrays
6535 * on if we are re-adding a preexisting device.
6536 * So require mddev->pers and MD_DISK_SYNC.
6537 */
6538 if (mddev->pers) {
6539 mdu_disk_info_t info;
6540 if (copy_from_user(&info, argp, sizeof(info)))
6541 err = -EFAULT;
6542 else if (!(info.state & (1<<MD_DISK_SYNC)))
6543 /* Need to clear read-only for this */
6544 break;
6545 else
6546 err = add_new_disk(mddev, &info);
6547 goto done_unlock;
6548 }
6549 break;
6550
NeilBrownc02c0ae2012-12-11 13:39:21 +11006551 case BLKROSET:
6552 if (get_user(ro, (int __user *)(arg))) {
6553 err = -EFAULT;
6554 goto done_unlock;
6555 }
6556 err = -EINVAL;
6557
6558 /* if the bdev is going readonly the value of mddev->ro
6559 * does not matter, no writes are coming
6560 */
6561 if (ro)
NeilBrown32a76272005-06-21 17:17:14 -07006562 goto done_unlock;
6563
NeilBrownc02c0ae2012-12-11 13:39:21 +11006564 /* are we are already prepared for writes? */
6565 if (mddev->ro != 1)
6566 goto done_unlock;
6567
6568 /* transitioning to readauto need only happen for
6569 * arrays that call md_write_start
6570 */
6571 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006573 if (err == 0) {
6574 mddev->ro = 2;
6575 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006576 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006577 }
6578 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 }
6580
6581 /*
6582 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006583 * superblock, so we do not allow them on read-only arrays.
6584 * However non-MD ioctls (e.g. get-size) will still come through
6585 * here and hit the 'default' below, so only disallow
6586 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006588 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006589 if (mddev->ro == 2) {
6590 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006591 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006592 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006593 /* mddev_unlock will wake thread */
6594 /* If a device failed while we were read-only, we
6595 * need to make sure the metadata is updated now.
6596 */
6597 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6598 mddev_unlock(mddev);
6599 wait_event(mddev->sb_wait,
6600 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6601 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006602 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006603 }
NeilBrownf91de922005-11-08 21:39:36 -08006604 } else {
6605 err = -EROFS;
6606 goto abort_unlock;
6607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 }
6609
NeilBrownc02c0ae2012-12-11 13:39:21 +11006610 switch (cmd) {
6611 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006613 mdu_disk_info_t info;
6614 if (copy_from_user(&info, argp, sizeof(info)))
6615 err = -EFAULT;
6616 else
6617 err = add_new_disk(mddev, &info);
6618 goto done_unlock;
6619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620
NeilBrownc02c0ae2012-12-11 13:39:21 +11006621 case HOT_ADD_DISK:
6622 err = hot_add_disk(mddev, new_decode_dev(arg));
6623 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624
NeilBrownc02c0ae2012-12-11 13:39:21 +11006625 case RUN_ARRAY:
6626 err = do_md_run(mddev);
6627 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628
NeilBrownc02c0ae2012-12-11 13:39:21 +11006629 case SET_BITMAP_FILE:
6630 err = set_bitmap_file(mddev, (int)arg);
6631 goto done_unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006632
NeilBrownc02c0ae2012-12-11 13:39:21 +11006633 default:
6634 err = -EINVAL;
6635 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 }
6637
6638done_unlock:
6639abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006640 if (mddev->hold_active == UNTIL_IOCTL &&
6641 err != -EINVAL)
6642 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 mddev_unlock(mddev);
6644
6645 return err;
6646done:
6647 if (err)
6648 MD_BUG();
6649abort:
6650 return err;
6651}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006652#ifdef CONFIG_COMPAT
6653static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6654 unsigned int cmd, unsigned long arg)
6655{
6656 switch (cmd) {
6657 case HOT_REMOVE_DISK:
6658 case HOT_ADD_DISK:
6659 case SET_DISK_FAULTY:
6660 case SET_BITMAP_FILE:
6661 /* These take in integer arg, do not convert */
6662 break;
6663 default:
6664 arg = (unsigned long)compat_ptr(arg);
6665 break;
6666 }
6667
6668 return md_ioctl(bdev, mode, cmd, arg);
6669}
6670#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671
Al Viroa39907f2008-03-02 10:31:15 -05006672static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673{
6674 /*
6675 * Succeed if we can lock the mddev, which confirms that
6676 * it isn't being stopped right now.
6677 */
NeilBrownfd01b882011-10-11 16:47:53 +11006678 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 int err;
6680
Yuanhan Liu0c098222012-05-22 13:55:32 +10006681 if (!mddev)
6682 return -ENODEV;
6683
NeilBrownd3374822009-01-09 08:31:10 +11006684 if (mddev->gendisk != bdev->bd_disk) {
6685 /* we are racing with mddev_put which is discarding this
6686 * bd_disk.
6687 */
6688 mddev_put(mddev);
6689 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006690 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006691 /* Then retry the open from the top */
6692 return -ERESTARTSYS;
6693 }
6694 BUG_ON(mddev != bdev->bd_disk->private_data);
6695
NeilBrownc8c00a62009-08-10 12:50:52 +10006696 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 goto out;
6698
6699 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006700 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006701 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006702 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006704 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 out:
6706 return err;
6707}
6708
Al Virodb2a1442013-05-05 21:52:57 -04006709static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710{
NeilBrownfd01b882011-10-11 16:47:53 +11006711 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006713 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006714 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006717
6718static int md_media_changed(struct gendisk *disk)
6719{
NeilBrownfd01b882011-10-11 16:47:53 +11006720 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006721
6722 return mddev->changed;
6723}
6724
6725static int md_revalidate(struct gendisk *disk)
6726{
NeilBrownfd01b882011-10-11 16:47:53 +11006727 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006728
6729 mddev->changed = 0;
6730 return 0;
6731}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006732static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733{
6734 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006735 .open = md_open,
6736 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006737 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006738#ifdef CONFIG_COMPAT
6739 .compat_ioctl = md_compat_ioctl,
6740#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006741 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006742 .media_changed = md_media_changed,
6743 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744};
6745
Adrian Bunk75c96f82005-05-05 16:16:09 -07006746static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747{
NeilBrown2b8bf342011-10-11 16:48:23 +11006748 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 /*
6751 * md_thread is a 'system-thread', it's priority should be very
6752 * high. We avoid resource deadlocks individually in each
6753 * raid personality. (RAID5 does preallocation) We also use RR and
6754 * the very same RT priority as kswapd, thus we will never get
6755 * into a priority inversion deadlock.
6756 *
6757 * we definitely have to have equal or higher priority than
6758 * bdflush, otherwise bdflush will deadlock if there are too
6759 * many dirty RAID5 blocks.
6760 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761
NeilBrown6985c432005-10-19 21:23:47 -07006762 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006763 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764
NeilBrown93588e22005-11-15 00:09:12 -08006765 /* We need to wait INTERRUPTIBLE so that
6766 * we don't add to the load-average.
6767 * That means we need to be sure no signals are
6768 * pending
6769 */
6770 if (signal_pending(current))
6771 flush_signals(current);
6772
6773 wait_event_interruptible_timeout
6774 (thread->wqueue,
6775 test_bit(THREAD_WAKEUP, &thread->flags)
6776 || kthread_should_stop(),
6777 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778
NeilBrown6c987912011-01-14 09:13:53 +11006779 clear_bit(THREAD_WAKEUP, &thread->flags);
6780 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006781 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006783
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 return 0;
6785}
6786
NeilBrown2b8bf342011-10-11 16:48:23 +11006787void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788{
6789 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006790 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 set_bit(THREAD_WAKEUP, &thread->flags);
6792 wake_up(&thread->wqueue);
6793 }
6794}
6795
Shaohua Li4ed87312012-10-11 13:34:00 +11006796struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6797 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798{
NeilBrown2b8bf342011-10-11 16:48:23 +11006799 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800
NeilBrown2b8bf342011-10-11 16:48:23 +11006801 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802 if (!thread)
6803 return NULL;
6804
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 init_waitqueue_head(&thread->wqueue);
6806
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 thread->run = run;
6808 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006809 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006810 thread->tsk = kthread_run(md_thread, thread,
6811 "%s_%s",
6812 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006813 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006814 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815 kfree(thread);
6816 return NULL;
6817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 return thread;
6819}
6820
NeilBrown2b8bf342011-10-11 16:48:23 +11006821void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822{
NeilBrown2b8bf342011-10-11 16:48:23 +11006823 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006824 if (!thread)
6825 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006826 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006827 /* Locking ensures that mddev_unlock does not wake_up a
6828 * non-existent thread
6829 */
6830 spin_lock(&pers_lock);
6831 *threadp = NULL;
6832 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006833
6834 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 kfree(thread);
6836}
6837
NeilBrownfd01b882011-10-11 16:47:53 +11006838void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839{
6840 if (!mddev) {
6841 MD_BUG();
6842 return;
6843 }
6844
NeilBrownb2d444d2005-11-08 21:39:31 -08006845 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006847
NeilBrownde393cd2011-07-28 11:31:48 +10006848 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 return;
6850 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006851 if (mddev->degraded)
6852 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006853 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6855 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6856 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006857 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006858 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006859 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860}
6861
6862/* seq_file implementation /proc/mdstat */
6863
6864static void status_unused(struct seq_file *seq)
6865{
6866 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006867 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868
6869 seq_printf(seq, "unused devices: ");
6870
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006871 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872 char b[BDEVNAME_SIZE];
6873 i++;
6874 seq_printf(seq, "%s ",
6875 bdevname(rdev->bdev,b));
6876 }
6877 if (!i)
6878 seq_printf(seq, "<none>");
6879
6880 seq_printf(seq, "\n");
6881}
6882
6883
NeilBrownfd01b882011-10-11 16:47:53 +11006884static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885{
NeilBrowndd71cf62009-05-07 12:49:35 +10006886 sector_t max_sectors, resync, res;
6887 unsigned long dt, db;
6888 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006889 int scale;
6890 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891
NeilBrown72f36d52012-10-11 14:25:57 +11006892 if (mddev->curr_resync <= 3)
6893 resync = 0;
6894 else
6895 resync = mddev->curr_resync
6896 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897
NeilBrownc804cde2012-05-21 09:28:33 +10006898 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6899 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006900 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006902 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903
6904 /*
6905 * Should not happen.
6906 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006907 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 MD_BUG();
6909 return;
6910 }
NeilBrown4588b422006-03-27 01:18:04 -08006911 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006912 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006913 * u32, as those are the requirements for sector_div.
6914 * Thus 'scale' must be at least 10
6915 */
6916 scale = 10;
6917 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006918 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006919 scale++;
6920 }
6921 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006922 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006923
6924 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925 {
NeilBrown4588b422006-03-27 01:18:04 -08006926 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 seq_printf(seq, "[");
6928 for (i = 0; i < x; i++)
6929 seq_printf(seq, "=");
6930 seq_printf(seq, ">");
6931 for (i = 0; i < y; i++)
6932 seq_printf(seq, ".");
6933 seq_printf(seq, "] ");
6934 }
NeilBrown4588b422006-03-27 01:18:04 -08006935 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006936 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6937 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006938 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6939 "check" :
6940 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6941 "resync" : "recovery"))),
6942 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006943 (unsigned long long) resync/2,
6944 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945
6946 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 * dt: time from mark until now
6948 * db: blocks written from mark until now
6949 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006950 *
6951 * rt is a sector_t, so could be 32bit or 64bit.
6952 * So we divide before multiply in case it is 32bit and close
6953 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006954 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006955 * near the end of resync when the number of remaining sectors
6956 * is close to 'db'.
6957 * We then divide rt by 32 after multiplying by db to compensate.
6958 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 */
6960 dt = ((jiffies - mddev->resync_mark) / HZ);
6961 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006962 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6963 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964
NeilBrowndd71cf62009-05-07 12:49:35 +10006965 rt = max_sectors - resync; /* number of remaining sectors */
6966 sector_div(rt, db/32+1);
6967 rt *= dt;
6968 rt >>= 5;
6969
6970 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6971 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972
NeilBrownff4e8d92006-07-10 04:44:16 -07006973 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974}
6975
6976static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6977{
6978 struct list_head *tmp;
6979 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006980 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981
6982 if (l >= 0x10000)
6983 return NULL;
6984 if (!l--)
6985 /* header */
6986 return (void*)1;
6987
6988 spin_lock(&all_mddevs_lock);
6989 list_for_each(tmp,&all_mddevs)
6990 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006991 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992 mddev_get(mddev);
6993 spin_unlock(&all_mddevs_lock);
6994 return mddev;
6995 }
6996 spin_unlock(&all_mddevs_lock);
6997 if (!l--)
6998 return (void*)2;/* tail */
6999 return NULL;
7000}
7001
7002static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7003{
7004 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007005 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006
7007 ++*pos;
7008 if (v == (void*)2)
7009 return NULL;
7010
7011 spin_lock(&all_mddevs_lock);
7012 if (v == (void*)1)
7013 tmp = all_mddevs.next;
7014 else
7015 tmp = mddev->all_mddevs.next;
7016 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007017 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018 else {
7019 next_mddev = (void*)2;
7020 *pos = 0x10000;
7021 }
7022 spin_unlock(&all_mddevs_lock);
7023
7024 if (v != (void*)1)
7025 mddev_put(mddev);
7026 return next_mddev;
7027
7028}
7029
7030static void md_seq_stop(struct seq_file *seq, void *v)
7031{
NeilBrownfd01b882011-10-11 16:47:53 +11007032 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033
7034 if (mddev && v != (void*)1 && v != (void*)2)
7035 mddev_put(mddev);
7036}
7037
7038static int md_seq_show(struct seq_file *seq, void *v)
7039{
NeilBrownfd01b882011-10-11 16:47:53 +11007040 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007041 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007042 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043
7044 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007045 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046 seq_printf(seq, "Personalities : ");
7047 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007048 list_for_each_entry(pers, &pers_list, list)
7049 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050
7051 spin_unlock(&pers_lock);
7052 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007053 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 return 0;
7055 }
7056 if (v == (void*)2) {
7057 status_unused(seq);
7058 return 0;
7059 }
7060
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007061 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007063
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7065 seq_printf(seq, "%s : %sactive", mdname(mddev),
7066 mddev->pers ? "" : "in");
7067 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007068 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007070 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007071 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072 seq_printf(seq, " %s", mddev->pers->name);
7073 }
7074
Andre Nolldd8ac332009-03-31 14:33:13 +11007075 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11007076 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077 char b[BDEVNAME_SIZE];
7078 seq_printf(seq, " %s[%d]",
7079 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007080 if (test_bit(WriteMostly, &rdev->flags))
7081 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007082 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083 seq_printf(seq, "(F)");
7084 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007085 }
7086 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007087 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007088 if (test_bit(Replacement, &rdev->flags))
7089 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007090 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091 }
7092
7093 if (!list_empty(&mddev->disks)) {
7094 if (mddev->pers)
7095 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007096 (unsigned long long)
7097 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098 else
7099 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007100 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007102 if (mddev->persistent) {
7103 if (mddev->major_version != 0 ||
7104 mddev->minor_version != 90) {
7105 seq_printf(seq," super %d.%d",
7106 mddev->major_version,
7107 mddev->minor_version);
7108 }
NeilBrowne6910632008-02-06 01:39:51 -08007109 } else if (mddev->external)
7110 seq_printf(seq, " super external:%s",
7111 mddev->metadata_type);
7112 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007113 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114
7115 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007116 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007118 if (mddev->pers->sync_request) {
7119 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007120 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007121 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007122 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007123 seq_printf(seq, "\tresync=DELAYED\n ");
7124 else if (mddev->recovery_cp < MaxSector)
7125 seq_printf(seq, "\tresync=PENDING\n ");
7126 }
NeilBrown32a76272005-06-21 17:17:14 -07007127 } else
7128 seq_printf(seq, "\n ");
7129
NeilBrown57148962012-03-19 12:46:40 +11007130 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131
7132 seq_printf(seq, "\n");
7133 }
7134 mddev_unlock(mddev);
7135
7136 return 0;
7137}
7138
Jan Engelhardt110518b2009-05-07 12:49:37 +10007139static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 .start = md_seq_start,
7141 .next = md_seq_next,
7142 .stop = md_seq_stop,
7143 .show = md_seq_show,
7144};
7145
7146static int md_seq_open(struct inode *inode, struct file *file)
7147{
Kay Sieversf1514632011-07-12 20:48:39 +02007148 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149 int error;
7150
7151 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007152 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007153 return error;
7154
7155 seq = file->private_data;
7156 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157 return error;
7158}
7159
NeilBrowne2f23b62014-04-09 14:33:51 +10007160static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007161static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7162{
Kay Sieversf1514632011-07-12 20:48:39 +02007163 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007164 int mask;
7165
NeilBrowne2f23b62014-04-09 14:33:51 +10007166 if (md_unloading)
7167 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;;
NeilBrownd7603b72006-01-06 00:20:30 -08007168 poll_wait(filp, &md_event_waiters, wait);
7169
7170 /* always allow read */
7171 mask = POLLIN | POLLRDNORM;
7172
Kay Sieversf1514632011-07-12 20:48:39 +02007173 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007174 mask |= POLLERR | POLLPRI;
7175 return mask;
7176}
7177
Arjan van de Venfa027c22007-02-12 00:55:33 -08007178static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007179 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180 .open = md_seq_open,
7181 .read = seq_read,
7182 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007183 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007184 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185};
7186
NeilBrown84fc4b52011-10-11 16:49:58 +11007187int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188{
Chao Yu50bd3772014-09-25 15:28:34 +08007189 printk(KERN_INFO "md: %s personality registered for level %d\n",
7190 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007192 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 spin_unlock(&pers_lock);
7194 return 0;
7195}
7196
NeilBrown84fc4b52011-10-11 16:49:58 +11007197int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198{
NeilBrown2604b702006-01-06 00:20:36 -08007199 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007201 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 spin_unlock(&pers_lock);
7203 return 0;
7204}
7205
NeilBrownfd01b882011-10-11 16:47:53 +11007206static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207{
NeilBrown3cb03002011-10-11 16:45:26 +11007208 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007210 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211
7212 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007213 rcu_read_lock();
7214 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007216 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7217 (int)part_stat_read(&disk->part0, sectors[1]) -
7218 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007219 /* sync IO will cause sync_io to increase before the disk_stats
7220 * as sync_io is counted when a request starts, and
7221 * disk_stats is counted when it completes.
7222 * So resync activity will cause curr_events to be smaller than
7223 * when there was no such activity.
7224 * non-sync IO will cause disk_stat to increase without
7225 * increasing sync_io so curr_events will (eventually)
7226 * be larger than it was before. Once it becomes
7227 * substantially larger, the test below will cause
7228 * the array to appear non-idle, and resync will slow
7229 * down.
7230 * If there is a lot of outstanding resync activity when
7231 * we set last_event to curr_events, then all that activity
7232 * completing might cause the array to appear non-idle
7233 * and resync will be slowed down even though there might
7234 * not have been non-resync activity. This will only
7235 * happen once though. 'last_events' will soon reflect
7236 * the state where there is little or no outstanding
7237 * resync requests, and further resync activity will
7238 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007239 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007241 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242 rdev->last_events = curr_events;
7243 idle = 0;
7244 }
7245 }
NeilBrown4b809912008-07-21 17:05:25 +10007246 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247 return idle;
7248}
7249
NeilBrownfd01b882011-10-11 16:47:53 +11007250void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251{
7252 /* another "blocks" (512byte) blocks have been synced */
7253 atomic_sub(blocks, &mddev->recovery_active);
7254 wake_up(&mddev->recovery_wait);
7255 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007256 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007257 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258 md_wakeup_thread(mddev->thread);
7259 // stop recovery, signal do_sync ....
7260 }
7261}
7262
7263
NeilBrown06d91a52005-06-21 17:17:12 -07007264/* md_write_start(mddev, bi)
7265 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007266 * in superblock) before writing, schedule a superblock update
7267 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007268 */
NeilBrownfd01b882011-10-11 16:47:53 +11007269void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270{
Neil Brown0fd62b82008-06-28 08:31:36 +10007271 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007272 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007273 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007274
NeilBrownf91de922005-11-08 21:39:36 -08007275 BUG_ON(mddev->ro == 1);
7276 if (mddev->ro == 2) {
7277 /* need to switch to read/write */
7278 mddev->ro = 0;
7279 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7280 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007281 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007282 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007283 }
NeilBrown06d91a52005-06-21 17:17:12 -07007284 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007285 if (mddev->safemode == 1)
7286 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007287 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007288 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007289 if (mddev->in_sync) {
7290 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007291 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007292 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007293 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007294 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007295 }
NeilBrowna9701a32005-11-08 21:39:34 -08007296 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007297 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007298 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007299 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007300 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007301 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302}
7303
NeilBrownfd01b882011-10-11 16:47:53 +11007304void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305{
7306 if (atomic_dec_and_test(&mddev->writes_pending)) {
7307 if (mddev->safemode == 2)
7308 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007309 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7311 }
7312}
7313
NeilBrown2a2275d2007-01-26 00:57:11 -08007314/* md_allow_write(mddev)
7315 * Calling this ensures that the array is marked 'active' so that writes
7316 * may proceed without blocking. It is important to call this before
7317 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7318 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007319 *
7320 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7321 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007322 */
NeilBrownfd01b882011-10-11 16:47:53 +11007323int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007324{
7325 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007326 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007327 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007328 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007329 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007330 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007331
7332 spin_lock_irq(&mddev->write_lock);
7333 if (mddev->in_sync) {
7334 mddev->in_sync = 0;
7335 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007336 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007337 if (mddev->safemode_delay &&
7338 mddev->safemode == 0)
7339 mddev->safemode = 1;
7340 spin_unlock_irq(&mddev->write_lock);
7341 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007342 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007343 } else
7344 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007345
NeilBrown070dc6d2010-08-30 17:33:34 +10007346 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007347 return -EAGAIN;
7348 else
7349 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007350}
7351EXPORT_SYMBOL_GPL(md_allow_write);
7352
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353#define SYNC_MARKS 10
7354#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007355#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007356void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357{
Shaohua Li4ed87312012-10-11 13:34:00 +11007358 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007359 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 unsigned int currspeed = 0,
7361 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007362 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007364 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 sector_t mark_cnt[SYNC_MARKS];
7366 int last_mark,m;
7367 struct list_head *tmp;
7368 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007369 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007370 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007371 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007372 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373
7374 /* just incase thread restarts... */
7375 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7376 return;
NeilBrown3991b312014-05-28 13:39:23 +10007377 if (mddev->ro) {/* never try to sync a read-only array */
7378 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007379 return;
NeilBrown3991b312014-05-28 13:39:23 +10007380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381
NeilBrown61df9d92006-10-03 01:15:57 -07007382 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007383 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007384 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007385 action = "check";
7386 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007387 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007388 action = "repair";
7389 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007390 desc = "resync";
7391 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7392 desc = "reshape";
7393 else
7394 desc = "recovery";
7395
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007396 mddev->last_sync_action = action ?: desc;
7397
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398 /* we overload curr_resync somewhat here.
7399 * 0 == not engaged in resync at all
7400 * 2 == checking that there is no conflict with another sync
7401 * 1 == like 2, but have yielded to allow conflicting resync to
7402 * commense
7403 * other == active in resync - this many blocks
7404 *
7405 * Before starting a resync we must have set curr_resync to
7406 * 2, and then checked that every "conflicting" array has curr_resync
7407 * less than ours. When we find one that is the same or higher
7408 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7409 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7410 * This will mean we have to start checking from the beginning again.
7411 *
7412 */
7413
7414 do {
7415 mddev->curr_resync = 2;
7416
7417 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007418 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007420 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 if (mddev2 == mddev)
7422 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007423 if (!mddev->parallel_resync
7424 && mddev2->curr_resync
7425 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 DEFINE_WAIT(wq);
7427 if (mddev < mddev2 && mddev->curr_resync == 2) {
7428 /* arbitrarily yield */
7429 mddev->curr_resync = 1;
7430 wake_up(&resync_wait);
7431 }
7432 if (mddev > mddev2 && mddev->curr_resync == 1)
7433 /* no need to wait here, we can wait the next
7434 * time 'round when curr_resync == 2
7435 */
7436 continue;
NeilBrown97441972008-09-19 11:49:54 +10007437 /* We need to wait 'interruptible' so as not to
7438 * contribute to the load average, and not to
7439 * be caught by 'softlockup'
7440 */
7441 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007442 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007443 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007444 printk(KERN_INFO "md: delaying %s of %s"
7445 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007447 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007449 if (signal_pending(current))
7450 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451 schedule();
7452 finish_wait(&resync_wait, &wq);
7453 goto try_again;
7454 }
7455 finish_wait(&resync_wait, &wq);
7456 }
7457 }
7458 } while (mddev->curr_resync < 2);
7459
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007460 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007461 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007463 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 */
7465 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007466 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007467 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007468 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7469 j = mddev->resync_min;
7470 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007471 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007472
NeilBrownccfcc3c2006-03-27 01:18:09 -08007473 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007474 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007475 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007477 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007478 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007479 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007480 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007481 if (rdev->raid_disk >= 0 &&
7482 !test_bit(Faulty, &rdev->flags) &&
7483 !test_bit(In_sync, &rdev->flags) &&
7484 rdev->recovery_offset < j)
7485 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007486 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007487
7488 /* If there is a bitmap, we need to make sure all
7489 * writes that started before we added a spare
7490 * complete before we start doing a recovery.
7491 * Otherwise the write might complete and (via
7492 * bitmap_endwrite) set a bit in the bitmap after the
7493 * recovery has checked that bit and skipped that
7494 * region.
7495 */
7496 if (mddev->bitmap) {
7497 mddev->pers->quiesce(mddev, 1);
7498 mddev->pers->quiesce(mddev, 0);
7499 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501
NeilBrown61df9d92006-10-03 01:15:57 -07007502 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7503 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7504 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007505 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007506 "(but not more than %d KB/sec) for %s.\n",
7507 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508
NeilBrowneea1bf32009-03-31 14:27:02 +11007509 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007510
NeilBrown57afd892005-06-21 17:17:13 -07007511 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 for (m = 0; m < SYNC_MARKS; m++) {
7513 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007514 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 }
7516 last_mark = 0;
7517 mddev->resync_mark = mark[last_mark];
7518 mddev->resync_mark_cnt = mark_cnt[last_mark];
7519
7520 /*
7521 * Tune reconstruction:
7522 */
7523 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007524 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7525 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526
7527 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528 last_check = 0;
7529
7530 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007531 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007532 "md: resuming %s of %s from checkpoint.\n",
7533 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007535 } else
7536 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007537 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007538 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7539 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007540 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541
majianpeng7c2c57c2012-07-03 12:12:26 +10007542 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007543 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007544 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545
NeilBrown57afd892005-06-21 17:17:13 -07007546 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007547
NeilBrown7a91ee12009-05-26 12:57:21 +10007548 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7549 ((mddev->curr_resync > mddev->curr_resync_completed &&
7550 (mddev->curr_resync - mddev->curr_resync_completed)
7551 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007552 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007553 (j - mddev->curr_resync_completed)*2
7554 >= mddev->resync_max - mddev->curr_resync_completed
7555 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007556 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007557 wait_event(mddev->recovery_wait,
7558 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007559 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007560 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7561 j > mddev->recovery_cp)
7562 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007563 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007564 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007565 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007566 }
NeilBrownacb180b2009-04-14 16:28:34 +10007567
NeilBrownc91abf52013-11-19 12:02:01 +11007568 while (j >= mddev->resync_max &&
7569 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007570 /* As this condition is controlled by user-space,
7571 * we can block indefinitely, so use '_interruptible'
7572 * to avoid triggering warnings.
7573 */
7574 flush_signals(current); /* just in case */
7575 wait_event_interruptible(mddev->recovery_wait,
7576 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007577 || test_bit(MD_RECOVERY_INTR,
7578 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007579 }
NeilBrownacb180b2009-04-14 16:28:34 +10007580
NeilBrownc91abf52013-11-19 12:02:01 +11007581 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7582 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007583
NeilBrown57afd892005-06-21 17:17:13 -07007584 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007585 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007586 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007587 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007588 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589 }
NeilBrown57afd892005-06-21 17:17:13 -07007590
7591 if (!skipped) { /* actual IO requested */
7592 io_sectors += sectors;
7593 atomic_add(sectors, &mddev->recovery_active);
7594 }
7595
NeilBrowne875ece2011-07-28 11:39:24 +10007596 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7597 break;
7598
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007600 if (j > 2)
7601 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007602 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007603 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007604 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007605 * visible in /proc/mdstat
7606 */
7607 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007608
7609 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 continue;
7611
NeilBrown57afd892005-06-21 17:17:13 -07007612 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613 repeat:
7614 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7615 /* step marks */
7616 int next = (last_mark+1) % SYNC_MARKS;
7617
7618 mddev->resync_mark = mark[next];
7619 mddev->resync_mark_cnt = mark_cnt[next];
7620 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007621 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622 last_mark = next;
7623 }
7624
NeilBrownc91abf52013-11-19 12:02:01 +11007625 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7626 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627
7628 /*
7629 * this loop exits only if either when we are slower than
7630 * the 'hard' speed limit, or the system was IO-idle for
7631 * a jiffy.
7632 * the system might be non-idle CPU-wise, but we only care
7633 * about not overloading the IO subsystem. (things like an
7634 * e2fsck being done on the RAID array should execute fast)
7635 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636 cond_resched();
7637
Xiao Niac7e50a2014-08-07 09:37:41 -04007638 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7639 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07007640 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641
NeilBrown88202a02006-01-06 00:21:36 -08007642 if (currspeed > speed_min(mddev)) {
7643 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007644 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007645 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646 goto repeat;
7647 }
7648 }
7649 }
NeilBrownc91abf52013-11-19 12:02:01 +11007650 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7651 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7652 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653 /*
7654 * this also signals 'finished resyncing' to md_stop
7655 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007656 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7658
7659 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007660 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661
NeilBrowndfc70642008-05-23 13:04:39 -07007662 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007663 mddev->curr_resync > 2) {
7664 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7665 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7666 if (mddev->curr_resync >= mddev->recovery_cp) {
7667 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007668 "md: checkpointing %s of %s.\n",
7669 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007670 if (test_bit(MD_RECOVERY_ERROR,
7671 &mddev->recovery))
7672 mddev->recovery_cp =
7673 mddev->curr_resync_completed;
7674 else
7675 mddev->recovery_cp =
7676 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007677 }
7678 } else
7679 mddev->recovery_cp = MaxSector;
7680 } else {
7681 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7682 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007683 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007684 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007685 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007686 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007687 !test_bit(Faulty, &rdev->flags) &&
7688 !test_bit(In_sync, &rdev->flags) &&
7689 rdev->recovery_offset < mddev->curr_resync)
7690 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007691 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007694 skip:
NeilBrown17571282006-12-10 02:20:52 -08007695 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696
NeilBrownc07b70a2009-12-14 12:49:48 +11007697 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7698 /* We completed so min/max setting can be forgotten if used. */
7699 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7700 mddev->resync_min = 0;
7701 mddev->resync_max = MaxSector;
7702 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7703 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007704 mddev->curr_resync = 0;
7705 wake_up(&resync_wait);
7706 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7707 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007708 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709}
NeilBrown29269552006-03-27 01:18:10 -08007710EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711
NeilBrown746d3202013-04-24 11:42:41 +10007712static int remove_and_add_spares(struct mddev *mddev,
7713 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007714{
NeilBrown3cb03002011-10-11 16:45:26 +11007715 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007716 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007717 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007718
NeilBrowndafb20f2012-03-19 12:46:39 +11007719 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007720 if ((this == NULL || rdev == this) &&
7721 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007722 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007723 (test_bit(Faulty, &rdev->flags) ||
7724 ! test_bit(In_sync, &rdev->flags)) &&
7725 atomic_read(&rdev->nr_pending)==0) {
7726 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007727 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007728 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007729 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007730 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007731 }
7732 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007733 if (removed && mddev->kobj.sd)
7734 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007735
NeilBrown746d3202013-04-24 11:42:41 +10007736 if (this)
7737 goto no_add;
7738
NeilBrowndafb20f2012-03-19 12:46:39 +11007739 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007740 if (rdev->raid_disk >= 0 &&
7741 !test_bit(In_sync, &rdev->flags) &&
7742 !test_bit(Faulty, &rdev->flags))
7743 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007744 if (rdev->raid_disk >= 0)
7745 continue;
7746 if (test_bit(Faulty, &rdev->flags))
7747 continue;
7748 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11007749 ! (rdev->saved_raid_disk >= 0 &&
7750 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10007751 continue;
7752
NeilBrown7eb41882014-01-14 15:55:14 +11007753 if (rdev->saved_raid_disk < 0)
7754 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007755 if (mddev->pers->
7756 hot_add_disk(mddev, rdev) == 0) {
7757 if (sysfs_link_rdev(mddev, rdev))
7758 /* failure here is OK */;
7759 spares++;
7760 md_new_event(mddev);
7761 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007762 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007763 }
NeilBrown746d3202013-04-24 11:42:41 +10007764no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007765 if (removed)
7766 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007767 return spares;
7768}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007769
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770/*
7771 * This routine is regularly called by all per-raid-array threads to
7772 * deal with generic issues like resync and super-block update.
7773 * Raid personalities that don't have a thread (linear/raid0) do not
7774 * need this as they never do any recovery or update the superblock.
7775 *
7776 * It does not do any resync itself, but rather "forks" off other threads
7777 * to do that as needed.
7778 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7779 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007780 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781 * and wakeups up this thread which will reap the thread and finish up.
7782 * This thread also removes any faulty devices (with nr_pending == 0).
7783 *
7784 * The overall approach is:
7785 * 1/ if the superblock needs updating, update it.
7786 * 2/ If a recovery thread is running, don't do anything else.
7787 * 3/ If recovery has finished, clean up, possibly marking spares active.
7788 * 4/ If there are any faulty devices, remove them.
7789 * 5/ If array is degraded, try to add spares devices
7790 * 6/ If array has spares or is not in-sync, start a resync thread.
7791 */
NeilBrownfd01b882011-10-11 16:47:53 +11007792void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007794 if (mddev->suspended)
7795 return;
7796
NeilBrown5f404022005-06-21 17:17:16 -07007797 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007798 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007799
NeilBrownfca4d842005-06-21 17:17:11 -07007800 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007801 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007802 printk(KERN_INFO "md: %s in immediate safe mode\n",
7803 mdname(mddev));
7804 mddev->safemode = 2;
7805 }
7806 flush_signals(current);
7807 }
7808
NeilBrownc89a8ee2008-08-05 15:54:13 +10007809 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7810 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11007812 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007814 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007815 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007816 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7817 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007818 ))
7819 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007820
NeilBrowndf5b89b2006-03-27 01:18:20 -08007821 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007822 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007823
NeilBrownc89a8ee2008-08-05 15:54:13 +10007824 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10007825 /* On a read-only array we can:
7826 * - remove failed devices
7827 * - add already-in_sync devices if the array itself
7828 * is in-sync.
7829 * As we only add devices that are already in-sync,
7830 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10007831 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10007832 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11007833 /* There is no thread, but we need to call
7834 * ->spare_active and clear saved_raid_disk
7835 */
NeilBrown2ac295a2014-05-29 11:40:03 +10007836 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11007837 md_reap_sync_thread(mddev);
7838 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownc89a8ee2008-08-05 15:54:13 +10007839 goto unlock;
7840 }
7841
NeilBrown31a59e32008-04-30 00:52:30 -07007842 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007843 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007844 spin_lock_irq(&mddev->write_lock);
7845 if (mddev->safemode &&
7846 !atomic_read(&mddev->writes_pending) &&
7847 !mddev->in_sync &&
7848 mddev->recovery_cp == MaxSector) {
7849 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007850 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007851 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007852 }
7853 if (mddev->safemode == 1)
7854 mddev->safemode = 0;
7855 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007856 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007857 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007858 }
NeilBrownfca4d842005-06-21 17:17:11 -07007859
NeilBrown7a0a5352013-08-27 16:28:23 +10007860 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07007861 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007862
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7864 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7865 /* resync/recovery still happening */
7866 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7867 goto unlock;
7868 }
7869 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007870 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007871 goto unlock;
7872 }
Neil Brown72a23c22008-06-28 08:31:41 +10007873 /* Set RUNNING before clearing NEEDED to avoid
7874 * any transients in the value of "sync_action".
7875 */
NeilBrown72f36d52012-10-11 14:25:57 +11007876 mddev->curr_resync_completed = 0;
Neil Brown72a23c22008-06-28 08:31:41 +10007877 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007878 /* Clear some bits that don't mean anything, but
7879 * might be left set
7880 */
NeilBrown24dd4692005-11-08 21:39:26 -08007881 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7882 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883
NeilBrowned209582012-04-24 10:23:14 +10007884 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7885 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007886 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887 /* no recovery is running.
7888 * remove any failed drives, then
7889 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007890 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891 * the personality to fail the re-add.
7892 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007894 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007895 if (mddev->pers->check_reshape == NULL ||
7896 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007897 /* Cannot proceed */
7898 goto unlock;
7899 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007900 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10007901 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007902 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7903 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007904 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007905 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007906 } else if (mddev->recovery_cp < MaxSector) {
7907 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007908 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007909 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7910 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007912
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007914 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007915 /* We are adding a device or devices to an array
7916 * which has the bitmap stored on all devices.
7917 * So make sure all bitmap pages get written
7918 */
7919 bitmap_write_all(mddev->bitmap);
7920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921 mddev->sync_thread = md_register_thread(md_do_sync,
7922 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007923 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924 if (!mddev->sync_thread) {
7925 printk(KERN_ERR "%s: could not start resync"
7926 " thread...\n",
7927 mdname(mddev));
7928 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007929 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7930 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7931 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7932 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7933 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007934 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007936 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007937 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938 }
7939 unlock:
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007940 wake_up(&mddev->sb_wait);
7941
Neil Brown72a23c22008-06-28 08:31:41 +10007942 if (!mddev->sync_thread) {
7943 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7944 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7945 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007946 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007947 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949 mddev_unlock(mddev);
7950 }
7951}
7952
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007953void md_reap_sync_thread(struct mddev *mddev)
7954{
7955 struct md_rdev *rdev;
7956
7957 /* resync has finished, collect result */
7958 md_unregister_thread(&mddev->sync_thread);
NeilBrown30b8feb2013-11-14 15:16:17 +11007959 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007960 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7961 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7962 /* success...*/
7963 /* activate any spares */
7964 if (mddev->pers->spare_active(mddev)) {
7965 sysfs_notify(&mddev->kobj, NULL,
7966 "degraded");
7967 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7968 }
7969 }
7970 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7971 mddev->pers->finish_reshape)
7972 mddev->pers->finish_reshape(mddev);
7973
7974 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11007975 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007976 */
NeilBrownf4667222013-12-09 12:04:56 +11007977 if (!mddev->degraded)
7978 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007979 rdev->saved_raid_disk = -1;
7980
7981 md_update_sb(mddev, 1);
7982 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7983 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7984 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7985 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7986 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7987 /* flag recovery needed just to double check */
7988 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7989 sysfs_notify_dirent_safe(mddev->sysfs_action);
7990 md_new_event(mddev);
7991 if (mddev->event_work.func)
7992 queue_work(md_misc_wq, &mddev->event_work);
7993}
7994
NeilBrownfd01b882011-10-11 16:47:53 +11007995void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007996{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007997 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007998 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007999 !test_bit(Blocked, &rdev->flags) &&
8000 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008001 msecs_to_jiffies(5000));
8002 rdev_dec_pending(rdev, mddev);
8003}
8004EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8005
NeilBrownc6563a82012-05-21 09:27:00 +10008006void md_finish_reshape(struct mddev *mddev)
8007{
8008 /* called be personality module when reshape completes. */
8009 struct md_rdev *rdev;
8010
8011 rdev_for_each(rdev, mddev) {
8012 if (rdev->data_offset > rdev->new_data_offset)
8013 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8014 else
8015 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8016 rdev->data_offset = rdev->new_data_offset;
8017 }
8018}
8019EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008020
8021/* Bad block management.
8022 * We can record which blocks on each device are 'bad' and so just
8023 * fail those blocks, or that stripe, rather than the whole device.
8024 * Entries in the bad-block table are 64bits wide. This comprises:
8025 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8026 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8027 * A 'shift' can be set so that larger blocks are tracked and
8028 * consequently larger devices can be covered.
8029 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8030 *
8031 * Locking of the bad-block table uses a seqlock so md_is_badblock
8032 * might need to retry if it is very unlucky.
8033 * We will sometimes want to check for bad blocks in a bi_end_io function,
8034 * so we use the write_seqlock_irq variant.
8035 *
8036 * When looking for a bad block we specify a range and want to
8037 * know if any block in the range is bad. So we binary-search
8038 * to the last range that starts at-or-before the given endpoint,
8039 * (or "before the sector after the target range")
8040 * then see if it ends after the given start.
8041 * We return
8042 * 0 if there are no known bad blocks in the range
8043 * 1 if there are known bad block which are all acknowledged
8044 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8045 * plus the start/length of the first bad section we overlap.
8046 */
8047int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8048 sector_t *first_bad, int *bad_sectors)
8049{
8050 int hi;
majianpengab056132012-11-06 17:13:44 +08008051 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008052 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008053 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008054 sector_t target = s + sectors;
8055 unsigned seq;
8056
8057 if (bb->shift > 0) {
8058 /* round the start down, and the end up */
8059 s >>= bb->shift;
8060 target += (1<<bb->shift) - 1;
8061 target >>= bb->shift;
8062 sectors = target - s;
8063 }
8064 /* 'target' is now the first block after the bad range */
8065
8066retry:
8067 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008068 lo = 0;
8069 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008070 hi = bb->count;
8071
8072 /* Binary search between lo and hi for 'target'
8073 * i.e. for the last range that starts before 'target'
8074 */
8075 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8076 * are known not to be the last range before target.
8077 * VARIANT: hi-lo is the number of possible
8078 * ranges, and decreases until it reaches 1
8079 */
8080 while (hi - lo > 1) {
8081 int mid = (lo + hi) / 2;
8082 sector_t a = BB_OFFSET(p[mid]);
8083 if (a < target)
8084 /* This could still be the one, earlier ranges
8085 * could not. */
8086 lo = mid;
8087 else
8088 /* This and later ranges are definitely out. */
8089 hi = mid;
8090 }
8091 /* 'lo' might be the last that started before target, but 'hi' isn't */
8092 if (hi > lo) {
8093 /* need to check all range that end after 's' to see if
8094 * any are unacknowledged.
8095 */
8096 while (lo >= 0 &&
8097 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8098 if (BB_OFFSET(p[lo]) < target) {
8099 /* starts before the end, and finishes after
8100 * the start, so they must overlap
8101 */
8102 if (rv != -1 && BB_ACK(p[lo]))
8103 rv = 1;
8104 else
8105 rv = -1;
8106 *first_bad = BB_OFFSET(p[lo]);
8107 *bad_sectors = BB_LEN(p[lo]);
8108 }
8109 lo--;
8110 }
8111 }
8112
8113 if (read_seqretry(&bb->lock, seq))
8114 goto retry;
8115
8116 return rv;
8117}
8118EXPORT_SYMBOL_GPL(md_is_badblock);
8119
8120/*
8121 * Add a range of bad blocks to the table.
8122 * This might extend the table, or might contract it
8123 * if two adjacent ranges can be merged.
8124 * We binary-search to find the 'insertion' point, then
8125 * decide how best to handle it.
8126 */
8127static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8128 int acknowledged)
8129{
8130 u64 *p;
8131 int lo, hi;
8132 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008133 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008134
8135 if (bb->shift < 0)
8136 /* badblocks are disabled */
8137 return 0;
8138
8139 if (bb->shift) {
8140 /* round the start down, and the end up */
8141 sector_t next = s + sectors;
8142 s >>= bb->shift;
8143 next += (1<<bb->shift) - 1;
8144 next >>= bb->shift;
8145 sectors = next - s;
8146 }
8147
Bian Yu905b0292013-10-12 01:10:03 -04008148 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008149
8150 p = bb->page;
8151 lo = 0;
8152 hi = bb->count;
8153 /* Find the last range that starts at-or-before 's' */
8154 while (hi - lo > 1) {
8155 int mid = (lo + hi) / 2;
8156 sector_t a = BB_OFFSET(p[mid]);
8157 if (a <= s)
8158 lo = mid;
8159 else
8160 hi = mid;
8161 }
8162 if (hi > lo && BB_OFFSET(p[lo]) > s)
8163 hi = lo;
8164
8165 if (hi > lo) {
8166 /* we found a range that might merge with the start
8167 * of our new range
8168 */
8169 sector_t a = BB_OFFSET(p[lo]);
8170 sector_t e = a + BB_LEN(p[lo]);
8171 int ack = BB_ACK(p[lo]);
8172 if (e >= s) {
8173 /* Yes, we can merge with a previous range */
8174 if (s == a && s + sectors >= e)
8175 /* new range covers old */
8176 ack = acknowledged;
8177 else
8178 ack = ack && acknowledged;
8179
8180 if (e < s + sectors)
8181 e = s + sectors;
8182 if (e - a <= BB_MAX_LEN) {
8183 p[lo] = BB_MAKE(a, e-a, ack);
8184 s = e;
8185 } else {
8186 /* does not all fit in one range,
8187 * make p[lo] maximal
8188 */
8189 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8190 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8191 s = a + BB_MAX_LEN;
8192 }
8193 sectors = e - s;
8194 }
8195 }
8196 if (sectors && hi < bb->count) {
8197 /* 'hi' points to the first range that starts after 's'.
8198 * Maybe we can merge with the start of that range */
8199 sector_t a = BB_OFFSET(p[hi]);
8200 sector_t e = a + BB_LEN(p[hi]);
8201 int ack = BB_ACK(p[hi]);
8202 if (a <= s + sectors) {
8203 /* merging is possible */
8204 if (e <= s + sectors) {
8205 /* full overlap */
8206 e = s + sectors;
8207 ack = acknowledged;
8208 } else
8209 ack = ack && acknowledged;
8210
8211 a = s;
8212 if (e - a <= BB_MAX_LEN) {
8213 p[hi] = BB_MAKE(a, e-a, ack);
8214 s = e;
8215 } else {
8216 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8217 s = a + BB_MAX_LEN;
8218 }
8219 sectors = e - s;
8220 lo = hi;
8221 hi++;
8222 }
8223 }
8224 if (sectors == 0 && hi < bb->count) {
8225 /* we might be able to combine lo and hi */
8226 /* Note: 's' is at the end of 'lo' */
8227 sector_t a = BB_OFFSET(p[hi]);
8228 int lolen = BB_LEN(p[lo]);
8229 int hilen = BB_LEN(p[hi]);
8230 int newlen = lolen + hilen - (s - a);
8231 if (s >= a && newlen < BB_MAX_LEN) {
8232 /* yes, we can combine them */
8233 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8234 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8235 memmove(p + hi, p + hi + 1,
8236 (bb->count - hi - 1) * 8);
8237 bb->count--;
8238 }
8239 }
8240 while (sectors) {
8241 /* didn't merge (it all).
8242 * Need to add a range just before 'hi' */
8243 if (bb->count >= MD_MAX_BADBLOCKS) {
8244 /* No room for more */
8245 rv = 0;
8246 break;
8247 } else {
8248 int this_sectors = sectors;
8249 memmove(p + hi + 1, p + hi,
8250 (bb->count - hi) * 8);
8251 bb->count++;
8252
8253 if (this_sectors > BB_MAX_LEN)
8254 this_sectors = BB_MAX_LEN;
8255 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8256 sectors -= this_sectors;
8257 s += this_sectors;
8258 }
8259 }
8260
8261 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008262 if (!acknowledged)
8263 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008264 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008265
8266 return rv;
8267}
8268
NeilBrown3cb03002011-10-11 16:45:26 +11008269int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008270 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008271{
NeilBrownc6563a82012-05-21 09:27:00 +10008272 int rv;
8273 if (is_new)
8274 s += rdev->new_data_offset;
8275 else
8276 s += rdev->data_offset;
8277 rv = md_set_badblocks(&rdev->badblocks,
8278 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008279 if (rv) {
8280 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008281 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008282 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8283 md_wakeup_thread(rdev->mddev->thread);
8284 }
8285 return rv;
8286}
8287EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8288
8289/*
8290 * Remove a range of bad blocks from the table.
8291 * This may involve extending the table if we spilt a region,
8292 * but it must not fail. So if the table becomes full, we just
8293 * drop the remove request.
8294 */
8295static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8296{
8297 u64 *p;
8298 int lo, hi;
8299 sector_t target = s + sectors;
8300 int rv = 0;
8301
8302 if (bb->shift > 0) {
8303 /* When clearing we round the start up and the end down.
8304 * This should not matter as the shift should align with
8305 * the block size and no rounding should ever be needed.
8306 * However it is better the think a block is bad when it
8307 * isn't than to think a block is not bad when it is.
8308 */
8309 s += (1<<bb->shift) - 1;
8310 s >>= bb->shift;
8311 target >>= bb->shift;
8312 sectors = target - s;
8313 }
8314
8315 write_seqlock_irq(&bb->lock);
8316
8317 p = bb->page;
8318 lo = 0;
8319 hi = bb->count;
8320 /* Find the last range that starts before 'target' */
8321 while (hi - lo > 1) {
8322 int mid = (lo + hi) / 2;
8323 sector_t a = BB_OFFSET(p[mid]);
8324 if (a < target)
8325 lo = mid;
8326 else
8327 hi = mid;
8328 }
8329 if (hi > lo) {
8330 /* p[lo] is the last range that could overlap the
8331 * current range. Earlier ranges could also overlap,
8332 * but only this one can overlap the end of the range.
8333 */
8334 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8335 /* Partial overlap, leave the tail of this range */
8336 int ack = BB_ACK(p[lo]);
8337 sector_t a = BB_OFFSET(p[lo]);
8338 sector_t end = a + BB_LEN(p[lo]);
8339
8340 if (a < s) {
8341 /* we need to split this range */
8342 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008343 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008344 goto out;
8345 }
8346 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8347 bb->count++;
8348 p[lo] = BB_MAKE(a, s-a, ack);
8349 lo++;
8350 }
8351 p[lo] = BB_MAKE(target, end - target, ack);
8352 /* there is no longer an overlap */
8353 hi = lo;
8354 lo--;
8355 }
8356 while (lo >= 0 &&
8357 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8358 /* This range does overlap */
8359 if (BB_OFFSET(p[lo]) < s) {
8360 /* Keep the early parts of this range. */
8361 int ack = BB_ACK(p[lo]);
8362 sector_t start = BB_OFFSET(p[lo]);
8363 p[lo] = BB_MAKE(start, s - start, ack);
8364 /* now low doesn't overlap, so.. */
8365 break;
8366 }
8367 lo--;
8368 }
8369 /* 'lo' is strictly before, 'hi' is strictly after,
8370 * anything between needs to be discarded
8371 */
8372 if (hi - lo > 1) {
8373 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8374 bb->count -= (hi - lo - 1);
8375 }
8376 }
8377
8378 bb->changed = 1;
8379out:
8380 write_sequnlock_irq(&bb->lock);
8381 return rv;
8382}
8383
NeilBrownc6563a82012-05-21 09:27:00 +10008384int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8385 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008386{
NeilBrownc6563a82012-05-21 09:27:00 +10008387 if (is_new)
8388 s += rdev->new_data_offset;
8389 else
8390 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008391 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008392 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008393}
8394EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8395
8396/*
8397 * Acknowledge all bad blocks in a list.
8398 * This only succeeds if ->changed is clear. It is used by
8399 * in-kernel metadata updates
8400 */
8401void md_ack_all_badblocks(struct badblocks *bb)
8402{
8403 if (bb->page == NULL || bb->changed)
8404 /* no point even trying */
8405 return;
8406 write_seqlock_irq(&bb->lock);
8407
majianpengecb178b2012-03-19 12:46:42 +11008408 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008409 u64 *p = bb->page;
8410 int i;
8411 for (i = 0; i < bb->count ; i++) {
8412 if (!BB_ACK(p[i])) {
8413 sector_t start = BB_OFFSET(p[i]);
8414 int len = BB_LEN(p[i]);
8415 p[i] = BB_MAKE(start, len, 1);
8416 }
8417 }
NeilBrownde393cd2011-07-28 11:31:48 +10008418 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008419 }
8420 write_sequnlock_irq(&bb->lock);
8421}
8422EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8423
NeilBrown16c791a2011-07-28 11:31:47 +10008424/* sysfs access to bad-blocks list.
8425 * We present two files.
8426 * 'bad-blocks' lists sector numbers and lengths of ranges that
8427 * are recorded as bad. The list is truncated to fit within
8428 * the one-page limit of sysfs.
8429 * Writing "sector length" to this file adds an acknowledged
8430 * bad block list.
8431 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8432 * been acknowledged. Writing to this file adds bad blocks
8433 * without acknowledging them. This is largely for testing.
8434 */
8435
8436static ssize_t
8437badblocks_show(struct badblocks *bb, char *page, int unack)
8438{
8439 size_t len;
8440 int i;
8441 u64 *p = bb->page;
8442 unsigned seq;
8443
8444 if (bb->shift < 0)
8445 return 0;
8446
8447retry:
8448 seq = read_seqbegin(&bb->lock);
8449
8450 len = 0;
8451 i = 0;
8452
8453 while (len < PAGE_SIZE && i < bb->count) {
8454 sector_t s = BB_OFFSET(p[i]);
8455 unsigned int length = BB_LEN(p[i]);
8456 int ack = BB_ACK(p[i]);
8457 i++;
8458
8459 if (unack && ack)
8460 continue;
8461
8462 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8463 (unsigned long long)s << bb->shift,
8464 length << bb->shift);
8465 }
NeilBrownde393cd2011-07-28 11:31:48 +10008466 if (unack && len == 0)
8467 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008468
8469 if (read_seqretry(&bb->lock, seq))
8470 goto retry;
8471
8472 return len;
8473}
8474
8475#define DO_DEBUG 1
8476
8477static ssize_t
8478badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8479{
8480 unsigned long long sector;
8481 int length;
8482 char newline;
8483#ifdef DO_DEBUG
8484 /* Allow clearing via sysfs *only* for testing/debugging.
8485 * Normally only a successful write may clear a badblock
8486 */
8487 int clear = 0;
8488 if (page[0] == '-') {
8489 clear = 1;
8490 page++;
8491 }
8492#endif /* DO_DEBUG */
8493
8494 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8495 case 3:
8496 if (newline != '\n')
8497 return -EINVAL;
8498 case 2:
8499 if (length <= 0)
8500 return -EINVAL;
8501 break;
8502 default:
8503 return -EINVAL;
8504 }
8505
8506#ifdef DO_DEBUG
8507 if (clear) {
8508 md_clear_badblocks(bb, sector, length);
8509 return len;
8510 }
8511#endif /* DO_DEBUG */
8512 if (md_set_badblocks(bb, sector, length, !unack))
8513 return len;
8514 else
8515 return -ENOSPC;
8516}
8517
Adrian Bunk75c96f82005-05-05 16:16:09 -07008518static int md_notify_reboot(struct notifier_block *this,
8519 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520{
8521 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008522 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008523 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008524
NeilBrownc744a652012-03-19 12:46:37 +11008525 for_each_mddev(mddev, tmp) {
8526 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008527 if (mddev->pers)
8528 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008529 if (mddev->persistent)
8530 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008531 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008532 }
NeilBrownc744a652012-03-19 12:46:37 +11008533 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534 }
NeilBrownc744a652012-03-19 12:46:37 +11008535 /*
8536 * certain more exotic SCSI devices are known to be
8537 * volatile wrt too early system reboots. While the
8538 * right place to handle this issue is the given
8539 * driver, we do want to have a safe RAID driver ...
8540 */
8541 if (need_delay)
8542 mdelay(1000*1);
8543
Linus Torvalds1da177e2005-04-16 15:20:36 -07008544 return NOTIFY_DONE;
8545}
8546
Adrian Bunk75c96f82005-05-05 16:16:09 -07008547static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008548 .notifier_call = md_notify_reboot,
8549 .next = NULL,
8550 .priority = INT_MAX, /* before any real devices */
8551};
8552
8553static void md_geninit(void)
8554{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008555 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008557 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558}
8559
Adrian Bunk75c96f82005-05-05 16:16:09 -07008560static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008561{
Tejun Heoe804ac72010-10-15 15:36:08 +02008562 int ret = -ENOMEM;
8563
Tejun Heoada609e2011-01-25 14:35:54 +01008564 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008565 if (!md_wq)
8566 goto err_wq;
8567
8568 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8569 if (!md_misc_wq)
8570 goto err_misc_wq;
8571
8572 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8573 goto err_md;
8574
8575 if ((ret = register_blkdev(0, "mdp")) < 0)
8576 goto err_mdp;
8577 mdp_major = ret;
8578
NeilBrownaf5628f2014-07-31 13:54:54 +10008579 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008580 md_probe, NULL, NULL);
8581 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582 md_probe, NULL, NULL);
8583
Linus Torvalds1da177e2005-04-16 15:20:36 -07008584 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008585 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008586
8587 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008589
Tejun Heoe804ac72010-10-15 15:36:08 +02008590err_mdp:
8591 unregister_blkdev(MD_MAJOR, "md");
8592err_md:
8593 destroy_workqueue(md_misc_wq);
8594err_misc_wq:
8595 destroy_workqueue(md_wq);
8596err_wq:
8597 return ret;
8598}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008599
8600#ifndef MODULE
8601
8602/*
8603 * Searches all registered partitions for autorun RAID arrays
8604 * at boot time.
8605 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008606
8607static LIST_HEAD(all_detected_devices);
8608struct detected_devices_node {
8609 struct list_head list;
8610 dev_t dev;
8611};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612
8613void md_autodetect_dev(dev_t dev)
8614{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008615 struct detected_devices_node *node_detected_dev;
8616
8617 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8618 if (node_detected_dev) {
8619 node_detected_dev->dev = dev;
8620 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8621 } else {
8622 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8623 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008625}
8626
8627
8628static void autostart_arrays(int part)
8629{
NeilBrown3cb03002011-10-11 16:45:26 +11008630 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008631 struct detected_devices_node *node_detected_dev;
8632 dev_t dev;
8633 int i_scanned, i_passed;
8634
8635 i_scanned = 0;
8636 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008637
8638 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8639
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008640 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8641 i_scanned++;
8642 node_detected_dev = list_entry(all_detected_devices.next,
8643 struct detected_devices_node, list);
8644 list_del(&node_detected_dev->list);
8645 dev = node_detected_dev->dev;
8646 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008647 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008648 if (IS_ERR(rdev))
8649 continue;
8650
NeilBrownb2d444d2005-11-08 21:39:31 -08008651 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008652 MD_BUG();
8653 continue;
8654 }
NeilBrownd0fae182008-03-04 14:29:31 -08008655 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008656 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008657 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008658 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008659
8660 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8661 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008662
8663 autorun_devices(part);
8664}
8665
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008666#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008667
8668static __exit void md_exit(void)
8669{
NeilBrownfd01b882011-10-11 16:47:53 +11008670 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008671 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008672 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008673
NeilBrownaf5628f2014-07-31 13:54:54 +10008674 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008675 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008676
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008677 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008678 unregister_blkdev(mdp_major, "mdp");
8679 unregister_reboot_notifier(&md_notifier);
8680 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008681
8682 /* We cannot unload the modules while some process is
8683 * waiting for us in select() or poll() - wake them up
8684 */
8685 md_unloading = 1;
8686 while (waitqueue_active(&md_event_waiters)) {
8687 /* not safe to leave yet */
8688 wake_up(&md_event_waiters);
8689 msleep(delay);
8690 delay += delay;
8691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008692 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008693
NeilBrown29ac4aa2008-02-06 01:39:58 -08008694 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008695 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008696 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008697 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008698 destroy_workqueue(md_misc_wq);
8699 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008700}
8701
Dan Williams685784a2007-07-09 11:56:42 -07008702subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008703module_exit(md_exit)
8704
NeilBrownf91de922005-11-08 21:39:36 -08008705static int get_ro(char *buffer, struct kernel_param *kp)
8706{
8707 return sprintf(buffer, "%d", start_readonly);
8708}
8709static int set_ro(const char *val, struct kernel_param *kp)
8710{
8711 char *e;
8712 int num = simple_strtoul(val, &e, 10);
8713 if (*val && (*e == '\0' || *e == '\n')) {
8714 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008715 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008716 }
8717 return -EINVAL;
8718}
8719
NeilBrown80ca3a42006-07-10 04:44:18 -07008720module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8721module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008722
NeilBrownefeb53c2009-01-09 08:31:10 +11008723module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008724
Linus Torvalds1da177e2005-04-16 15:20:36 -07008725EXPORT_SYMBOL(register_md_personality);
8726EXPORT_SYMBOL(unregister_md_personality);
8727EXPORT_SYMBOL(md_error);
8728EXPORT_SYMBOL(md_done_sync);
8729EXPORT_SYMBOL(md_write_start);
8730EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008731EXPORT_SYMBOL(md_register_thread);
8732EXPORT_SYMBOL(md_unregister_thread);
8733EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008734EXPORT_SYMBOL(md_check_recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008735EXPORT_SYMBOL(md_reap_sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008737MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008738MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008739MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);